omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / arboris_spread_plan.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Zero-heap 5-region GB10 UVM arena planner for Arboris scalar spreading activation. |
| 3 | note Slab 0: SpreadHeader (128 B, 16 x ulong, RO during dispatch) |
| 4 | note Slab 1: IncomingCSR (128 B aligned, RO after validation: in_row_off, in_src, in_weight each 128 B padded) |
| 5 | note Slab 2: StatePing (4 KiB guard + 128 B aligned, RW: node-major [V, batch_stride] FP32) |
| 6 | note Slab 3: StatePong (4 KiB guard + 128 B aligned, RW: identical [V, batch_stride] double buffer) |
| 7 | note Slab 4: SpreadReceipt (4 KiB guard + 128 B aligned, RW: 8 x ulong = 64 B) |
| 8 | om2 |
| 9 | |
| 10 | const SP_MAGIC_VER ulong 0x4152425300000001 |
| 11 | const SP_MAX_NODES ulong 0x1fffffff |
| 12 | const SP_MAX_EDGES ulong 0xffffffff |
| 13 | const SP_MAX_BATCH ulong 65535 |
| 14 | const SP_F32_INF ulong 0x7f800000 |
| 15 | |
| 16 | fn sp_add_checked a ulong b ulong limit ulong to ulong |
| 17 | if or gt a limit gt b sub limit a |
| 18 | ret 0 |
| 19 | end |
| 20 | ret add a b |
| 21 | end |
| 22 | |
| 23 | fn sp_mul_checked a ulong b ulong limit ulong to ulong |
| 24 | if or eq a 0 eq b 0 |
| 25 | ret 0 |
| 26 | end |
| 27 | if gt a div limit b |
| 28 | ret 0 |
| 29 | end |
| 30 | ret mul a b |
| 31 | end |
| 32 | |
| 33 | fn sp_align_checked n ulong alignment ulong limit ulong to ulong |
| 34 | if or eq n 0 gt n limit |
| 35 | ret 0 |
| 36 | end |
| 37 | mask = sub alignment 1 |
| 38 | pad = and sub ulong 0 n mask |
| 39 | if gt pad sub limit n |
| 40 | ret 0 |
| 41 | end |
| 42 | ret add n pad |
| 43 | end |
| 44 | |
| 45 | fn sp_expected_stride batch ulong to ulong |
| 46 | if eq batch 1 |
| 47 | ret 1 |
| 48 | end |
| 49 | ret and add batch 31 0xffffffffffffffe0 |
| 50 | end |
| 51 | |
| 52 | fn sp_header_inputs_ok hdr view ulong 16 to ulong |
| 53 | if ne get hdr 0 SP_MAGIC_VER |
| 54 | ret 1 |
| 55 | end |
| 56 | if ne get hdr 15 0 |
| 57 | ret 2 |
| 58 | end |
| 59 | v_cnt = get hdr 1 |
| 60 | e_cnt = get hdr 2 |
| 61 | batch = get hdr 3 |
| 62 | if or eq v_cnt 0 gt v_cnt SP_MAX_NODES |
| 63 | ret 3 |
| 64 | end |
| 65 | if gt e_cnt SP_MAX_EDGES |
| 66 | ret 3 |
| 67 | end |
| 68 | if or eq batch 0 gt batch SP_MAX_BATCH |
| 69 | ret 3 |
| 70 | end |
| 71 | stride = sp_expected_stride batch |
| 72 | if and ne get hdr 4 0 ne get hdr 4 stride |
| 73 | ret 4 |
| 74 | end |
| 75 | hops = get hdr 5 |
| 76 | if or eq hops 0 gt hops 0xffffffff |
| 77 | ret 5 |
| 78 | end |
| 79 | decay = get hdr 6 |
| 80 | if or ge shr decay 32 1 ge and decay 0xffffffff SP_F32_INF |
| 81 | ret 6 |
| 82 | end |
| 83 | mode = get hdr 7 |
| 84 | if gt mode 3 |
| 85 | ret 7 |
| 86 | end |
| 87 | if and eq batch 1 ne and mode 1 0 |
| 88 | ret 7 |
| 89 | end |
| 90 | if and gt batch 1 ne and mode 1 1 |
| 91 | ret 7 |
| 92 | end |
| 93 | if gt get hdr 8 1 |
| 94 | ret 8 |
| 95 | end |
| 96 | ret 0 |
| 97 | end |
| 98 | |
| 99 | note Compute Slab 1 CSR sizes into res[4]: 0=off_in_src, 1=off_in_weight, 2=csr_sz, 3=state_sz. |
| 100 | fn sp_calc_sizes hdr view ulong 16 budget ulong res edit ulong 4 to ulong |
| 101 | v_cnt = get hdr 1 |
| 102 | e_cnt = get hdr 2 |
| 103 | stride = sp_expected_stride get hdr 3 |
| 104 | row_sz = sp_align_checked sp_mul_checked add v_cnt 1 4 budget 128 budget |
| 105 | if eq row_sz 0 |
| 106 | ret 11 |
| 107 | end |
| 108 | e_words = e_cnt |
| 109 | if eq e_words 0 |
| 110 | e_words = ulong 1 |
| 111 | end |
| 112 | src_sz = sp_align_checked sp_mul_checked e_words 4 budget 128 budget |
| 113 | if eq src_sz 0 |
| 114 | ret 11 |
| 115 | end |
| 116 | off_w = sp_add_checked row_sz src_sz budget |
| 117 | if eq off_w 0 |
| 118 | ret 11 |
| 119 | end |
| 120 | csr_sz = sp_add_checked off_w src_sz budget |
| 121 | if eq csr_sz 0 |
| 122 | ret 11 |
| 123 | end |
| 124 | elems = sp_mul_checked v_cnt stride budget |
| 125 | state_sz = sp_align_checked sp_mul_checked elems 4 budget 128 budget |
| 126 | if eq state_sz 0 |
| 127 | ret 12 |
| 128 | end |
| 129 | put res 0 row_sz |
| 130 | put res 1 off_w |
| 131 | put res 2 csr_sz |
| 132 | put res 3 state_sz |
| 133 | ret 0 |
| 134 | end |
| 135 | |
| 136 | fn sp_layout_slabs payload view ulong 5 page ulong budget ulong starts edit ulong 5 mapped edit ulong 5 to ulong |
| 137 | cur = ulong 0 |
| 138 | for i ulong 5 |
| 139 | put starts i cur |
| 140 | ext = sp_align_checked get payload i page sub budget cur |
| 141 | if eq ext 0 |
| 142 | ret 0 |
| 143 | end |
| 144 | put mapped i ext |
| 145 | cur = add cur ext |
| 146 | if lt i 4 |
| 147 | if gt page sub budget cur |
| 148 | ret 0 |
| 149 | end |
| 150 | cur = add cur page |
| 151 | end |
| 152 | end |
| 153 | ret cur |
| 154 | end |
| 155 | |
| 156 | note Plan the 5-region UVM arena with 4 KiB unmapped guard pages between slabs. |
| 157 | note Populates hdr[4] (batch_stride), hdr[11] (off_in_src), hdr[12] (off_in_weight), |
| 158 | note hdr[13] (state_plane_bytes), hdr[14] (total_arena_bytes), offsets[0..4], mapped_sizes[0..4]. |
| 159 | export fn arboris_spread_plan hdr edit ulong 16 budget ulong page ulong offsets edit ulong 5 mapped_sizes edit ulong 5 to ulong |
| 160 | rc = sp_header_inputs_ok hdr |
| 161 | if ne rc 0 |
| 162 | ret rc |
| 163 | end |
| 164 | if or lt page 4096 ne and page sub page 1 0 |
| 165 | ret 9 |
| 166 | end |
| 167 | if lt budget page |
| 168 | ret 10 |
| 169 | end |
| 170 | res = new ulong 4 0 |
| 171 | rc = sp_calc_sizes hdr budget res |
| 172 | if ne rc 0 |
| 173 | ret rc |
| 174 | end |
| 175 | payload = new ulong 5 0 |
| 176 | starts = new ulong 5 0 |
| 177 | msizes = new ulong 5 0 |
| 178 | put payload 0 128 |
| 179 | put payload 1 get res 2 |
| 180 | put payload 2 get res 3 |
| 181 | put payload 3 get res 3 |
| 182 | put payload 4 64 |
| 183 | total = sp_layout_slabs payload page budget starts msizes |
| 184 | if eq total 0 |
| 185 | ret 13 |
| 186 | end |
| 187 | put hdr 4 sp_expected_stride get hdr 3 |
| 188 | put hdr 11 get res 0 |
| 189 | put hdr 12 get res 1 |
| 190 | put hdr 13 get res 3 |
| 191 | put hdr 14 total |
| 192 | for i ulong 5 |
| 193 | put offsets i get starts i |
| 194 | put mapped_sizes i get msizes i |
| 195 | end |
| 196 | ret 0 |
| 197 | end |
| 198 | |
| 199 | note Verify an already-planned SpreadHeader against a budget and page size. |
| 200 | export fn arboris_spread_verify_header hdr view ulong 16 budget ulong page ulong to ulong |
| 201 | rc = sp_header_inputs_ok hdr |
| 202 | if ne rc 0 |
| 203 | ret rc |
| 204 | end |
| 205 | if or lt page 4096 ne and page sub page 1 0 |
| 206 | ret 9 |
| 207 | end |
| 208 | if ne get hdr 4 sp_expected_stride get hdr 3 |
| 209 | ret 4 |
| 210 | end |
| 211 | res = new ulong 4 0 |
| 212 | rc = sp_calc_sizes hdr budget res |
| 213 | if ne rc 0 |
| 214 | ret rc |
| 215 | end |
| 216 | if or ne get hdr 11 get res 0 or ne get hdr 12 get res 1 ne get hdr 13 get res 3 |
| 217 | ret 14 |
| 218 | end |
| 219 | payload = new ulong 5 0 |
| 220 | offs = new ulong 5 0 |
| 221 | msz = new ulong 5 0 |
| 222 | put payload 0 128 |
| 223 | put payload 1 get res 2 |
| 224 | put payload 2 get res 3 |
| 225 | put payload 3 get res 3 |
| 226 | put payload 4 64 |
| 227 | total = sp_layout_slabs payload page budget offs msz |
| 228 | if or eq total 0 ne get hdr 14 total |
| 229 | ret 14 |
| 230 | end |
| 231 | ret 0 |
| 232 | end |