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 / native / orig4 / shadow.om

473 lines · 10481 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note omega-mv#731 ORIG-4: default-off shadow advice for the matvec arm chooser
3note (spec/shadow-advice-v0.md). Unit after the pinned units add32 mul32 metadata affine
4note fixture world omexp omexpfix select (native/orig4/orig4.pin); the program is this unit
5note plus shadowmain.om (fn main), the tests this unit plus tests/orig4/shadow-test.om.
6note Modes (first stdin byte, dispatched by shadowmain.om): S shadow, G fixture.
7note S: config 64 bytes (8 LE words: 0 enable, 1 scoring budget in records (<= 2048),
8note 2 image length in the stream (1..32768; select.om's slload decides admissibility),
9note 3 admission version (0 v1 oxload, 1 v2 oxadm2: ORIG1-CONTRACT-v2.md; with advisor kind
10note 0 the image's v2 flag SS[20] must equal it, else FALLBACK all reason=image-9), 4 advisor
11note kind (0 the image's scorer, 1 retrieval: the v2/v1 nearest-TRAIN-record rule of
12note select.om slnear, used to measure its per-decision overhead; the image is admitted
13note but not consulted), 5..7 reserved 0), the allow-list block (4096 bytes), the
14note checkpoint image, then omexp-v1 records to end of input (<= 524288 bytes).
15note Admission in order: enable word (anything but 1 prints SHADOW disabled, exit 0) ->
16note allow-list status of the records' FNV-1a 64 (oxallow) -> record admission (oxload) ->
17note checkpoint admission (sladmit: every slload check except the training-dataset binding;
18note slbound is printed as bound_matches=yes|no and is information, not a fallback reason:
19note the advisor by design scores records the model never trained on) -> per record: past the
20note budget, out-of-distribution state (core class 2 or pressure above 1), fewer than two
21note admissible arms, any non-finite score -> FALLBACK; otherwise ADVICE with the argmin
22note score among the admissible arms (lowest index on a tie) under the best-on-DEV params.
23note The advisor writes only to fd 1, never reads past the end of input, and never widens
24note the candidate set: hvscore ranks only arms for which oxcand holds.
25note Exit: 0 (also for a refused image), 64 bad mode, 66 records refused, 67 unauthorized
26note or revoked, 68 config invalid, 74 I/O.
27note G: 16 bytes (seed, count <= 2048); the SYNTHETIC records of sxgen go to fd 1 raw.
28note H: the record bytes to end of input; their FNV-1a 64 in decimal on fd 1 (the glue
29note authorizes a mutated fixture through it for the record-refusal runs).
30note HV: 0 score bits of the last hvscore, 1 scored, 2 per-record fallbacks, 3 advised
31note cost, 4 oracle cost, 5 incumbent cost (ps per call, scored records only), 6 budget,
32note 7 advisor kind. A v2-refused record (oxpart 3) falls back with reason=refused.
33global HM uchar 1
34global HK uchar 64
35global HV ulong 8
36text HNL "\n"
37text HSDIS "SHADOW disabled"
38text HSREC "SHADOW records="
39text HSDF " data_fnv="
40text HSAL " allow="
41text HSIMG "SHADOW image_fnv="
42text HSKI " kind="
43text HSNP " nparams="
44text HSDB " data_bound="
45text HSBM " bound_matches="
46text HSYES "yes"
47text HSNO "no"
48text HFREC "FALLBACK all reason=records-"
49text HFALW "FALLBACK all reason=allow-"
50text HFIMG "FALLBACK all reason=image-"
51text HFSEQ "FALLBACK seq="
52text HREAS " reason="
53text HROOD "ood"
54text HRSIN "single"
55text HRNON "nonfinite"
56text HRBUD "budget"
57text HRREF "refused"
58text HSV2 "SHADOW admission=v2 admitted="
59text HSKD " advisor="
60text HASEQ "ADVICE seq="
61text HAARM " arm="
62text HAINC " incumbent="
63text HAORA " oracle="
64text HASCO " score="
65text HCREC "COST records="
66text HCSCO " scored="
67text HCFB " fallback="
68text HCADV " adv_cost="
69text HCORA " oracle_cost="
70text HCINC " incumbent_cost="
71
72fn hvnl to void
73w = write 1 HNL 0 1
74end
75
76fn hkw i ulong to ulong
77note Config word i (little-endian) from HK.
78v = ulong 0
79for k ulong 8
80v = or v shl ulong get HK add mul i 8 k mul k 8
81end
82ret v
83end
84
85fn hvrdk n ulong to ulong
86note Up to n bytes into HK[0..n); returns the count read.
87o = ulong 0
88while lt o n bound 64
89r = read 0 HK o sub n o
90if lt r 0
91exit 74
92end
93if eq r 0
94ret o
95end
96o = add o ulong r
97end
98ret o
99end
100
101fn hvrdlb to ulong
102note The allow-list block: 4096 bytes, zero padded; returns the used length.
103o = ulong 0
104while lt o 4096 bound 4096
105r = read 0 LB o sub 4096 o
106if lt r 0
107exit 74
108end
109if eq r 0
110ret o
111end
112o = add o ulong r
113end
114n = ulong 0
115for i ulong 4096
116if ne get LB i 0
117n = add i 1
118end
119end
120ret n
121end
122
123fn hvrdimg n ulong to ulong
124note Up to n image bytes into SI[0..n); never reads past them.
125o = ulong 0
126while lt o n bound 32768
127r = read 0 SI o sub n o
128if lt r 0
129exit 74
130end
131if eq r 0
132ret o
133end
134o = add o ulong r
135end
136ret o
137end
138
139fn hvrdrb to ulong
140note Records to end of input into RB; 524289 means more than the capacity arrived.
141o = ulong 0
142while lt o 524288 bound 999999
143r = read 0 RB o sub 524288 o
144if lt r 0
145exit 74
146end
147if eq r 0
148ret o
149end
150o = add o ulong r
151end
152r = read 0 HM 0 1
153if ne r 0
154ret 524289
155end
156ret o
157end
158
159fn hvwrb n ulong to void
160o = ulong 0
161while lt o n bound 999999
162w = write 1 RB o sub n o
163if le w 0
164exit 74
165end
166o = add o ulong w
167end
168end
169
170fn hvood r ulong to bool
171note Never in training: core class 2 or a pressure bucket above 1.
172ret or eq oxcore r 2 gt oxpress r 1
173end
174
175fn hvnarms r ulong to ulong
176note Admissible arms of record r (oxcand: eligible, measured, verified).
177c = ulong 0
178for a ulong 8
179if oxcand r a
180c = add c 1
181end
182end
183ret c
184end
185
186fn hvbudget r ulong to bool
187note Record index r is past the declared scoring budget HV[6].
188ret ge r get HV 6
189end
190
191fn hvscore r ulong to long
192note The advice: argmin score over the admissible arms under the current params (lowest
193note index on a tie); HV[0] keeps the winning score bits. Negative: -1 out of distribution,
194note -2 fewer than two admissible arms, -3 a non-finite score among the admissible arms.
195if hvood r
196ret -1
197end
198if lt hvnarms r 2
199ret -2
200end
201best = ulong 8
202if eq get HV 7 1
203put HV 0 0
204ret long slnear r
205end
206note Seed from the lowest admissible arm, never from arm 0 unchecked (oxchk2 and oxpair2
207note make arm 0 admissible today; this loop does not rely on it).
208bs = ulong 0
209ok = true
210for a ulong 8
211if oxcand r a
212s = slfwd r a
213if not affinite s
214ok = false
215end
216if or eq best 8 sllt s bs
217bs = s
218best = a
219end
220end
221end
222put HV 0 bs
223if not ok
224ret -3
225end
226ret long best
227end
228
229fn hvimage n ulong to int
230note Checkpoint admission through select.om's sladmit: every image check except the
231note training-dataset binding (0 admitted, state installed; negative refused, nothing
232note installed). The binding is information (slbound, printed as bound_matches), never a
233note refusal: the advisor by design scores records the model never trained on.
234ret sladmit 0 n
235end
236
237fn hvcfgok to bool
238note budget <= 2048, image length 1..32768 (the SI capacity), admission version and
239note advisor kind 0 or 1, reserved words zero.
240if gt hkw 1 2048
241ret false
242end
243if or eq hkw 2 0 gt hkw 2 32768
244ret false
245end
246if or gt hkw 3 1 gt hkw 4 1
247ret false
248end
249for i ulong 3
250if ne hkw add 5 i 0
251ret false
252end
253end
254ret true
255end
256
257fn hvload n ulong to int
258note Record admission by the config's version word: 1 oxadm2 (v2), otherwise oxload (v1).
259if eq hkw 3 1
260ret oxadm2 n
261end
262ret oxload n
263end
264
265fn hvadmit n ulong lbn ulong to int
266note Allow-list status (67) then record admission (66); 0 when admitted, with the line
267note SHADOW records=N data_fnv=H allow=S.
268if gt n 524288
269w = write 1 HFREC 0 len HFREC
270wnum 1
271hvnl
272ret 66
273end
274st = oxallow lbn oxfnv n
275if lt st 0
276w = write 1 HFALW 0 len HFALW
277wnum ulong neg long st
278hvnl
279ret 67
280end
281ld = hvload n
282if ne ld 0
283w = write 1 HFREC 0 len HFREC
284wnum ulong neg long ld
285hvnl
286ret 66
287end
288w = write 1 HSREC 0 len HSREC
289wnum get RC 0
290w = write 1 HSDF 0 len HSDF
291wnum get RC 2
292w = write 1 HSAL 0 len HSAL
293wnum ulong st
294hvnl
295if eq hkw 3 1
296w = write 1 HSV2 0 len HSV2
297wnum get RC 10
298w = write 1 HSKD 0 len HSKD
299wnum get HV 7
300hvnl
301end
302ret 0
303end
304
305fn hvimg n ulong to int
306note Checkpoint admission with its SHADOW line; 1 when refused (FALLBACK all printed).
307st = hvimage n
308if and ge st 0 and eq get HV 7 0 ne get SS 20 hkw 3
309st = -9
310end
311if lt st 0
312w = write 1 HFIMG 0 len HFIMG
313wnum ulong neg long st
314hvnl
315ret 1
316end
317w = write 1 HSIMG 0 len HSIMG
318wnum siget sub n 8
319w = write 1 HSKI 0 len HSKI
320wnum get SS 0
321w = write 1 HSNP 0 len HSNP
322wnum get SS 15
323w = write 1 HSDB 0 len HSDB
324wnum get SS 11
325w = write 1 HSBM 0 len HSBM
326if slbound 0
327w = write 1 HSYES 0 len HSYES
328else
329w = write 1 HSNO 0 len HSNO
330end
331hvnl
332ret 0
333end
334
335fn hvfb r ulong code long to void
336note FALLBACK seq=S reason=... for one record; counts it.
337w = write 1 HFSEQ 0 len HFSEQ
338wnum oxseq r
339w = write 1 HREAS 0 len HREAS
340if eq code -1
341w = write 1 HROOD 0 len HROOD
342end
343if eq code -2
344w = write 1 HRSIN 0 len HRSIN
345end
346if eq code -3
347w = write 1 HRNON 0 len HRNON
348end
349if eq code -4
350w = write 1 HRBUD 0 len HRBUD
351end
352if eq code -5
353w = write 1 HRREF 0 len HRREF
354end
355hvnl
356put HV 2 add get HV 2 1
357end
358
359fn hvadv r ulong a ulong to void
360note ADVICE seq=S arm=A incumbent=R oracle=O score=BITS; cost sums over scored records.
361w = write 1 HASEQ 0 len HASEQ
362wnum oxseq r
363w = write 1 HAARM 0 len HAARM
364wnum a
365w = write 1 HAINC 0 len HAINC
366wnum oxrule r
367w = write 1 HAORA 0 len HAORA
368wnum oxbest r
369w = write 1 HASCO 0 len HASCO
370wnum get HV 0
371hvnl
372put HV 1 add get HV 1 1
373put HV 3 add get HV 3 oxcost r a
374put HV 4 add get HV 4 oxcost r oxbest r
375put HV 5 add get HV 5 oxcost r oxrule r
376end
377
378fn hvrec r ulong to void
379if hvbudget r
380hvfb r -4
381ret
382end
383if eq oxpart r 3
384hvfb r -5
385ret
386end
387c = hvscore r
388if lt c 0
389hvfb r c
390ret
391end
392hvadv r ulong c
393end
394
395fn hvcost to void
396w = write 1 HCREC 0 len HCREC
397wnum get RC 0
398w = write 1 HCSCO 0 len HCSCO
399wnum get HV 1
400w = write 1 HCFB 0 len HCFB
401wnum get HV 2
402w = write 1 HCADV 0 len HCADV
403wnum get HV 3
404w = write 1 HCORA 0 len HCORA
405wnum get HV 4
406w = write 1 HCINC 0 len HCINC
407wnum get HV 5
408hvnl
409end
410
411fn hvrun to int
412note S: read the stream in admission order, then advise record by record.
413if ne hvrdk 64 64
414ret 68
415end
416if ne hkw 0 1
417w = write 1 HSDIS 0 len HSDIS
418hvnl
419ret 0
420end
421if not hvcfgok
422ret 68
423end
424for i ulong 8
425put HV i 0
426end
427put HV 6 hkw 1
428put HV 7 hkw 4
429lbn = hvrdlb
430img = hkw 2
431if ne hvrdimg img img
432w = write 1 HFIMG 0 len HFIMG
433wnum 1
434hvnl
435ret 0
436end
437n = hvrdrb
438st = hvadmit n lbn
439if ne st 0
440ret st
441end
442if ne hvimg img 0
443ret 0
444end
445slbest
446for r ulong get RC 0
447hvrec r
448end
449hvcost
450ret 0
451end
452
453fn hvgen to int
454if ne hvrdk 16 16
455ret 68
456end
457if or eq hkw 1 0 gt hkw 1 2048
458ret 68
459end
460hvwrb sxgen hkw 0 hkw 1
461ret 0
462end
463
464fn hvhash to int
465note H: the FNV-1a 64 of the record bytes on stdin (the allow-list identity), decimal.
466n = hvrdrb
467if gt n 524288
468ret 66
469end
470wnum oxfnv n
471hvnl
472ret 0
473end