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.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note FP32 scalar oracle for the incident-edge spreading implementation in |
| 3 | note Arboris abd34338c8b10bb8c6a43784dfb782879d78ffd6. A bank stores |
| 4 | note [batch, raw frontier then highest decayed scores], two N-word planes. |
| 5 | note State header is batch, active bank, decay(hop) bits, threshold bits. |
| 6 | note Initially raw and highest scores both hold the seeds. The caller supplies |
| 7 | note the selected decay each hop. A zero decay stops upstream traversal. |
| 8 | fn ar_spread_shape topo view uint * weight_count ulong to bool |
| 9 | if lt len topo 4 |
| 10 | ret false |
| 11 | end |
| 12 | if ne get topo 0 ar_topo_v1 |
| 13 | ret false |
| 14 | end |
| 15 | n = ulong get topo 1 |
| 16 | if or eq n 0 ne weight_count ulong get topo 2 |
| 17 | ret false |
| 18 | end |
| 19 | if ne len topo add add n 4 weight_count |
| 20 | ret false |
| 21 | end |
| 22 | ret true |
| 23 | end |
| 24 | |
| 25 | fn ar_state_shape state view uint * n ulong to bool |
| 26 | if lt len state 4 |
| 27 | ret false |
| 28 | end |
| 29 | batch = ulong get state 0 |
| 30 | if or eq batch 0 gt get state 1 1 |
| 31 | ret false |
| 32 | end |
| 33 | if or gt get state 2 ar_one gt get state 3 ar_one |
| 34 | ret false |
| 35 | end |
| 36 | rows = sub len state 4 |
| 37 | stride = mul n 4 |
| 38 | ret and eq mul div rows stride stride rows eq div rows stride batch |
| 39 | end |
| 40 | |
| 41 | fn ar_spread_topology topo view uint * weights view uint * to bool |
| 42 | n = ulong get topo 1 |
| 43 | if or ne get topo 3 0 ne ulong get topo add 3 n len weights |
| 44 | ret false |
| 45 | end |
| 46 | for i ulong n bound ar_u32_max |
| 47 | if gt get topo add 3 i get topo add 4 i |
| 48 | ret false |
| 49 | end |
| 50 | end |
| 51 | first = add n 4 |
| 52 | for j ulong len weights bound ar_u32_max |
| 53 | if or ge ulong get topo add first j n gt get weights j ar_one |
| 54 | ret false |
| 55 | end |
| 56 | end |
| 57 | ret true |
| 58 | end |
| 59 | |
| 60 | fn ar_spread_bank state view uint * bank ulong to ulong |
| 61 | size = div sub len state 4 2 |
| 62 | ret add 4 mul bank size |
| 63 | end |
| 64 | |
| 65 | fn ar_spread_inputs state view uint * to bool |
| 66 | size = div sub len state 4 2 |
| 67 | start = ar_spread_bank state ulong get state 1 |
| 68 | for i ulong size bound 0x7fffffffffffffff |
| 69 | if gt get state add start i ar_one |
| 70 | ret false |
| 71 | end |
| 72 | end |
| 73 | ret true |
| 74 | end |
| 75 | |
| 76 | fn ar_source topo view uint * ix view ulong 6 to ulong |
| 77 | start = add get ix 3 4 |
| 78 | ret ulong get topo add start get ix 4 |
| 79 | end |
| 80 | |
| 81 | note Nonnegative binary32 in [0,1], round to nearest even with gradual |
| 82 | note underflow. The freestanding bridge admits integer instructions only. |
| 83 | fn ar_significand bits uint to ulong |
| 84 | fraction = ulong and bits 0x7fffff |
| 85 | if ge bits 0x800000 |
| 86 | ret or fraction 0x800000 |
| 87 | end |
| 88 | ret fraction |
| 89 | end |
| 90 | |
| 91 | fn ar_exponent bits uint to ulong |
| 92 | e = ulong shr bits 23 |
| 93 | if eq e 0 |
| 94 | ret 1 |
| 95 | end |
| 96 | ret e |
| 97 | end |
| 98 | |
| 99 | fn ar_product a uint b uint to uint |
| 100 | product = mul ar_significand a ar_significand b |
| 101 | exponents = add ar_exponent a ar_exponent b |
| 102 | shift = ulong 23 |
| 103 | if ge product 0x800000000000 |
| 104 | shift = ulong 24 |
| 105 | end |
| 106 | if lt add exponents shift 151 |
| 107 | shift = sub ulong 151 exponents |
| 108 | end |
| 109 | if gt shift 48 |
| 110 | ret 0 |
| 111 | end |
| 112 | sig = shr product shift |
| 113 | tail = sub product shl sig shift |
| 114 | halfway = shl ulong 1 sub shift 1 |
| 115 | if or gt tail halfway and eq tail halfway ne and sig 1 0 |
| 116 | sig = add sig 1 |
| 117 | end |
| 118 | if lt sig 0x800000 |
| 119 | ret uint sig |
| 120 | end |
| 121 | exponent = sub add exponents shift 150 |
| 122 | if eq sig 0x1000000 |
| 123 | exponent = add exponent 1 |
| 124 | sig = shr sig 1 |
| 125 | end |
| 126 | ret uint or shl exponent 23 and sig 0x7fffff |
| 127 | end |
| 128 | |
| 129 | fn ar_weighted state view uint * ix view ulong 6 src ulong w uint to uint |
| 130 | ret ar_product get state add get ix 0 src w |
| 131 | end |
| 132 | |
| 133 | note Work indices: input row, output row, destination, N, arc cursor and row end. |
| 134 | fn ar_advance state edit uint * ix view ulong 6 peak uint to void |
| 135 | score = ar_product peak get state 2 |
| 136 | raw = peak |
| 137 | if lt score get state 3 |
| 138 | score = uint 0 |
| 139 | raw = uint 0 |
| 140 | end |
| 141 | v = get ix 2 |
| 142 | prev = get state add add get ix 0 get ix 3 v |
| 143 | dest = add get ix 1 v |
| 144 | put state dest raw |
| 145 | if lt score prev |
| 146 | score = prev |
| 147 | end |
| 148 | put state add dest get ix 3 score |
| 149 | end |
| 150 | |
| 151 | export fn arboris_spread_step topo view uint * weights view uint * state edit uint * to ulong |
| 152 | if not ar_spread_shape topo len weights |
| 153 | ret 1 |
| 154 | end |
| 155 | if not ar_state_shape state ulong get topo 1 |
| 156 | ret 1 |
| 157 | end |
| 158 | if not ar_spread_topology topo weights |
| 159 | ret 2 |
| 160 | end |
| 161 | if not ar_spread_inputs state |
| 162 | ret 3 |
| 163 | end |
| 164 | if eq get state 2 0 |
| 165 | ret 0 |
| 166 | end |
| 167 | ix = new ulong 6 0 |
| 168 | put ix 0 ar_spread_bank state ulong get state 1 |
| 169 | put ix 1 ar_spread_bank state xor ulong get state 1 1 |
| 170 | put ix 3 ulong get topo 1 |
| 171 | todo = div sub len state 4 4 |
| 172 | while ne todo 0 bound 0x7fffffffffffffff |
| 173 | put ix 4 ulong get topo add 3 get ix 2 |
| 174 | put ix 5 ulong get topo add 4 get ix 2 |
| 175 | peak = uint 0 |
| 176 | while lt get ix 4 get ix 5 bound ar_u32_max |
| 177 | src = ar_source topo ix |
| 178 | weight = get weights get ix 4 |
| 179 | candidate = ar_weighted state ix src weight |
| 180 | if gt candidate peak |
| 181 | peak = candidate |
| 182 | end |
| 183 | put ix 4 add get ix 4 1 |
| 184 | end |
| 185 | ar_advance state ix peak |
| 186 | put ix 2 add get ix 2 1 |
| 187 | if eq get ix 2 get ix 3 |
| 188 | put ix 2 0 |
| 189 | put ix 0 add get ix 0 mul get ix 3 2 |
| 190 | put ix 1 add get ix 1 mul get ix 3 2 |
| 191 | end |
| 192 | todo = sub todo 1 |
| 193 | end |
| 194 | put state 1 xor get state 1 1 |
| 195 | ret 0 |
| 196 | end |
| 197 | |
| 198 | note Monotone max-product step. State header: batch, active bank, gamma bits, |
| 199 | note reserved zero. One N-word score vector per query in each bank. Incoming |
| 200 | note edges are exactly the ordered arcs recorded in CSR. Round gamma*weight to |
| 201 | note binary32, then multiply by the source score and round again. |
| 202 | fn ar_factor state view uint * weights view uint * ix view ulong 6 to uint |
| 203 | ret ar_product get state 2 get weights get ix 4 |
| 204 | end |
| 205 | |
| 206 | export fn arboris_relax_step topo view uint * weights view uint * state edit uint * to ulong |
| 207 | if not ar_spread_shape topo len weights |
| 208 | ret 1 |
| 209 | end |
| 210 | if lt len state 4 |
| 211 | ret 1 |
| 212 | end |
| 213 | if or eq get state 0 0 gt get state 1 1 |
| 214 | ret 1 |
| 215 | end |
| 216 | if or gt get state 2 ar_one ne get state 3 0 |
| 217 | ret 1 |
| 218 | end |
| 219 | size = mul ulong get topo 1 ulong get state 0 |
| 220 | if or gt size 0x7ffffffffffffffd ne len state add 4 mul size 2 |
| 221 | ret 1 |
| 222 | end |
| 223 | if not ar_spread_topology topo weights |
| 224 | ret 2 |
| 225 | end |
| 226 | if not ar_spread_inputs state |
| 227 | ret 3 |
| 228 | end |
| 229 | ix = new ulong 6 0 |
| 230 | put ix 0 ar_spread_bank state ulong get state 1 |
| 231 | put ix 1 ar_spread_bank state xor ulong get state 1 1 |
| 232 | put ix 3 ulong get topo 1 |
| 233 | while ne size 0 bound 0x7ffffffffffffffd |
| 234 | put ix 4 ulong get topo add 3 get ix 2 |
| 235 | put ix 5 ulong get topo add 4 get ix 2 |
| 236 | best = get state add get ix 0 get ix 2 |
| 237 | while lt get ix 4 get ix 5 bound ar_u32_max |
| 238 | src = ar_source topo ix |
| 239 | factor = ar_factor state weights ix |
| 240 | score = ar_weighted state ix src factor |
| 241 | if gt score best |
| 242 | best = score |
| 243 | end |
| 244 | put ix 4 add get ix 4 1 |
| 245 | end |
| 246 | put state add get ix 1 get ix 2 best |
| 247 | put ix 2 add get ix 2 1 |
| 248 | size = sub size 1 |
| 249 | if eq get ix 2 get ix 3 |
| 250 | put ix 2 0 |
| 251 | put ix 0 add get ix 0 get ix 3 |
| 252 | put ix 1 add get ix 1 get ix 3 |
| 253 | end |
| 254 | end |
| 255 | put state 1 xor get state 1 1 |
| 256 | ret 0 |
| 257 | end |