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 / gpu / dsv41 / decoder / check-swa-replay.om

114 lines · 2777 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Pure Seeded OM-2 Host Verification & Negative-Control Gate for SWA Bounded Replay (dsv41).
3note Compose after: om/gpu/dsv41/decoder/swa_replay.om
4
5text swaMsgOk "PASS: dsv41 swa_replay gate (5 checks, skipped_pct="
6text swaMsgEnd "%, 0 failures)\n"
7text swaMsgBad "FAIL: swa_replay check="
8text swaMsgNl "\n"
9
10fn swaPrint s view uchar * to void
11r = write 1 s 0 len s
12end
13
14fn swaPrintU x ulong to void
15b = new uchar 24 0
16pos = ulong 24
17v = x
18while true
19pos = sub pos 1
20put b pos uchar add 48 rem v 10
21v = div v 10
22if eq v 0
23break
24end
25end
26r = write 1 b pos sub 24 pos
27end
28
29fn swaFail code ulong to int
30swaPrint swaMsgBad
31swaPrintU code
32swaPrint swaMsgNl
33ret int code
34end
35
36fn main to int
37swaInitDefault
38
39note Check 1: Short prompt <= 134 always yields 0 prefix hit.
40if ne swaComputeHitLen 134 128 true 0
41ret swaFail 1
42end
43if ne swaComputeHitLen 100 100 true 0
44ret swaFail 2
45end
46
47note Check 2: Long prompt L=4096 with full block hit (4096) caps at floor((4096-134)/128)*128 = 3840.
48hHit = swaComputeHitLen 4096 4096 true
49if ne hHit 3840
50ret swaFail 3
51end
52note On SWA KV cache miss, backs off by exactly one 128-token block: 3840 - 128 = 3712.
53hMiss = swaComputeHitLen 4096 4096 false
54if ne hMiss 3712
55ret swaFail 4
56end
57note Partial block hit L1=1024 with SWA miss -> 896; with SWA hit -> 1024.
58if ne swaComputeHitLen 4096 1024 true 1024
59ret swaFail 5
60end
61if ne swaComputeHitLen 4096 1024 false 896
62ret swaFail 6
63end
64
65note Check 3: Compact prefill (L=2560, 40 layers) runs all 20 encoder layers for all 2560 tokens,
66note and runs 20 decoder layers ONLY for the last 140 tokens (2420..2559).
67put swaSkippedLayers 0 0
68put swaExecutedLayers 0 0
69for p ulong 2560
70for L ulong 40
71run = swaShouldRunPrefillLayer L p 2560
72end
73end
74note Expected executed = 20*2560 + 20*140 = 51200 + 2800 = 54000; skipped = 20*2420 = 48400 (47% saved!).
75if or ne get swaExecutedLayers 0 54000 ne get swaSkippedLayers 0 48400
76ret swaFail 7
77end
78
79note Check 4: Truncated SWA key range during bounded replay starting at s = 2420.
80rng = new ulong 2 0
81if not swaTruncatedKeyRange 2420 2420 rng
82ret swaFail 8
83end
84if or ne get rng 0 2420 ne get rng 1 2420
85ret swaFail 9
86end
87if not swaTruncatedKeyRange 2500 2420 rng
88ret swaFail 10
89end
90if or ne get rng 0 2420 ne get rng 1 2500
91ret swaFail 11
92end
93if not swaTruncatedKeyRange 2559 2420 rng
94ret swaFail 12
95end
96if or ne get rng 0 2432 ne get rng 1 2559
97ret swaFail 13
98end
99
100note Check 5: Negative controls (keep_tokens < 134 refused; pos < replayStart refused).
101if swaInit true 100
102ret swaFail 14
103end
104if swaTruncatedKeyRange 2400 2420 rng
105ret swaFail 15
106end
107
108tot = add get swaExecutedLayers 0 get swaSkippedLayers 0
109pct = div mul get swaSkippedLayers 0 100 tot
110swaPrint swaMsgOk
111swaPrintU pct
112swaPrint swaMsgEnd
113ret 0
114end