interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / arboris_spread_cpu.om

275 lines · 6723 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Pure OM-2 CPU reference oracle for Arboris scalar max-over-paths spreading activation.
3note Operates on the exact 5-region arena layout:
4note Slab 0: hdr[16] (SpreadHeader)
5note Slab 1: csr[*] (IncomingCSR: in_row_off at 0, in_src at hdr[11]/4, in_weight at hdr[12]/4)
6note Slabs 2+3: state[*] (StatePing at [0..plane_words-1], StatePong at [plane_words..2*plane_words-1])
7note Slab 4: receipt[8] (SpreadReceipt)
8note Evaluates best = fmaxf(best, (w * gamma) * act_u) in exact IEEE-754 FP32 order (no FMA).
9om2
10
11const CPU_MAGIC_VER ulong 0x4152425300000001
12const CPU_MAX_NODES ulong 0x1fffffff
13const CPU_MAX_BATCH ulong 65535
14const CPU_F32_INF uint 0x7f800000
15
16fn cpu_expected_stride batch ulong to ulong
17 if eq batch 1
18 ret 1
19 end
20 ret and add batch 31 0xffffffffffffffe0
21end
22
23fn cpu_validate_hdr hdr view ulong 16 to ulong
24 if ne get hdr 0 CPU_MAGIC_VER
25 ret 1
26 end
27 if ne get hdr 15 0
28 ret 2
29 end
30 v_cnt = get hdr 1
31 batch = get hdr 3
32 if or eq v_cnt 0 gt v_cnt CPU_MAX_NODES
33 ret 3
34 end
35 if or eq batch 0 gt batch CPU_MAX_BATCH
36 ret 3
37 end
38 if ne get hdr 4 cpu_expected_stride batch
39 ret 4
40 end
41 if or eq get hdr 5 0 gt get hdr 5 0xffffffff
42 ret 5
43 end
44 decay = get hdr 6
45 if or ge shr decay 32 1 ge uint and decay 0xffffffff CPU_F32_INF
46 ret 6
47 end
48 mode = get hdr 7
49 if gt mode 3
50 ret 7
51 end
52 if and eq batch 1 ne and mode 1 0
53 ret 7
54 end
55 if and gt batch 1 ne and mode 1 1
56 ret 7
57 end
58 if gt get hdr 8 1
59 ret 8
60 end
61 if or ne and get hdr 11 127 0 ne and get hdr 12 127 0
62 ret 9
63 end
64 ret 0
65end
66
67fn cpu_validate_csr hdr view ulong 16 csr view uint * to ulong
68 v_cnt = get hdr 1
69 e_cnt = get hdr 2
70 off_src = div get hdr 11 4
71 off_w = div get hdr 12 4
72 if or lt off_src add v_cnt 1 lt off_w add off_src e_cnt
73 ret 9
74 end
75 if lt len csr add off_w e_cnt
76 ret 9
77 end
78 if ne get csr 0 0
79 ret 10
80 end
81 prev = ulong 0
82 for i ulong v_cnt bound 0x1fffffff
83 nxt = ulong get csr add i 1
84 if or lt nxt prev gt nxt e_cnt
85 ret 10
86 end
87 prev = nxt
88 end
89 if ne prev e_cnt
90 ret 10
91 end
92 for e ulong e_cnt bound 0xffffffff
93 if ge ulong get csr add off_src e v_cnt
94 ret 11
95 end
96 if ge get csr add off_w e CPU_F32_INF
97 ret 12
98 end
99 end
100 ret 0
101end
102
103fn cpu_edge_cand hdr view ulong 16 csr view uint * state view uint * ix view ulong 4 to uint
104 e = get ix 3
105 u = ulong get csr add div get hdr 11 4 e
106 u_off = add get ix 2 mul u get hdr 4
107 u_bits = get state add u_off get ix 1
108 if ge u_bits CPU_F32_INF
109 ret CPU_F32_INF
110 end
111 w = bitcast f32 get csr add div get hdr 12 4 e
112 wg = mul w bitcast f32 uint and get hdr 6 0xffffffff
113 ret bitcast uint mul wg bitcast f32 u_bits
114end
115
116fn cpu_eval_vb hdr view ulong 16 csr view uint * state view uint * ix edit ulong 4 to uint
117 v = get ix 0
118 old_bits = get state add get ix 2 add mul v get hdr 4 get ix 1
119 if ge old_bits CPU_F32_INF
120 ret CPU_F32_INF
121 end
122 agg = shr get hdr 7 1
123 best = bitcast f32 old_bits
124 e = ulong get csr v
125 e_end = ulong get csr add v 1
126 while lt e e_end bound 0xffffffff
127 put ix 3 e
128 cbits = cpu_edge_cand hdr csr state ix
129 if ge cbits CPU_F32_INF
130 ret CPU_F32_INF
131 end
132 cand = bitcast f32 cbits
133 if eq agg 0
134 if gt cand best
135 best = cand
136 end
137 else
138 best = add best cand
139 if ge bitcast uint best CPU_F32_INF
140 ret CPU_F32_INF
141 end
142 end
143 e = add e 1
144 end
145 ret bitcast uint best
146end
147
148fn cpu_update_stats stats edit ulong 6 old_bits uint best_bits uint to void
149 if gt best_bits old_bits
150 put stats 0 add get stats 0 1
151 end
152 if gt ulong best_bits get stats 1
153 put stats 1 ulong best_bits
154 end
155 if gt best_bits 0
156 put stats 2 add get stats 2 1
157 end
158end
159
160fn cpu_step_row hdr view ulong 16 csr view uint * state edit uint * stats edit ulong 6 to ulong
161 ix = new ulong 4 0
162 put ix 0 get stats 3
163 put ix 2 get stats 4
164 row_off = mul get stats 3 get hdr 4
165 out_row = add get stats 5 row_off
166 for b ulong get hdr 3 bound 65535
167 put ix 1 b
168 old_bits = get state add get stats 4 add row_off b
169 best_bits = cpu_eval_vb hdr csr state ix
170 if ge best_bits CPU_F32_INF
171 ret 14
172 end
173 put state add out_row b best_bits
174 cpu_update_stats stats old_bits best_bits
175 end
176 pad_b = get hdr 3
177 while lt pad_b get hdr 4 bound 65536
178 put state add out_row pad_b 0
179 pad_b = add pad_b 1
180 end
181 ret 0
182end
183
184fn cpu_step_bank hdr view ulong 16 csr view uint * state edit uint * stats edit ulong 6 to ulong
185 put stats 0 0
186 put stats 1 0
187 put stats 2 0
188 plane = mul get hdr 1 get hdr 4
189 put stats 4 mul get hdr 8 plane
190 put stats 5 mul xor get hdr 8 1 plane
191 for v ulong get hdr 1 bound 0x1fffffff
192 put stats 3 v
193 err = cpu_step_row hdr csr state stats
194 if ne err 0
195 ret err
196 end
197 end
198 ret 0
199end
200
201fn cpu_validate_all hdr view ulong 16 csr view uint * state_len ulong to ulong
202 rc = cpu_validate_hdr hdr
203 if ne rc 0
204 ret rc
205 end
206 rc = cpu_validate_csr hdr csr
207 if ne rc 0
208 ret rc
209 end
210 need = mul mul get hdr 1 get hdr 4 2
211 if lt state_len need
212 ret 13
213 end
214 ret 0
215end
216
217note Execute one spreading-activation hop on the 5-region arena.
218note Flips hdr[8] (active_pingpong) and updates SpreadReceipt words [0,1,2,3,4,7].
219export fn arboris_spread_cpu_hop hdr edit ulong 16 csr view uint * state edit uint * receipt edit ulong 8 to ulong
220 rc = cpu_validate_all hdr csr len state
221 if ne rc 0
222 put receipt 1 rc
223 put receipt 7 rc
224 ret rc
225 end
226 stats = new ulong 6 0
227 err = cpu_step_bank hdr csr state stats
228 if ne err 0
229 put receipt 1 err
230 put receipt 7 err
231 ret err
232 end
233 put hdr 8 xor get hdr 8 1
234 put receipt 0 get stats 0
235 put receipt 1 0
236 put receipt 2 add get receipt 2 1
237 put receipt 3 get stats 1
238 put receipt 4 get stats 2
239 put receipt 7 0
240 ret 0
241end
242
243note Execute up to hdr[5] (max_hops) hops, stopping early when changed_count == 0.
244export fn arboris_spread_cpu_run hdr edit ulong 16 csr view uint * state edit uint * receipt edit ulong 8 to ulong
245 rc = cpu_validate_all hdr csr len state
246 if ne rc 0
247 put receipt 1 rc
248 put receipt 7 rc
249 ret rc
250 end
251 for k ulong 8
252 put receipt k 0
253 end
254 max_hops = get hdr 5
255 stats = new ulong 6 0
256 for hop ulong max_hops bound 0xffffffff
257 err = cpu_step_bank hdr csr state stats
258 if ne err 0
259 put receipt 1 err
260 put receipt 7 err
261 ret err
262 end
263 put hdr 8 xor get hdr 8 1
264 put receipt 0 get stats 0
265 put receipt 1 0
266 put receipt 2 add hop 1
267 put receipt 3 get stats 1
268 put receipt 4 get stats 2
269 put receipt 7 0
270 if eq get stats 0 0
271 ret 0
272 end
273 end
274 ret 0
275end