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 / swa_replay.om

117 lines · 3637 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note DeepInfer SWA Bounded Replay policy for DeepSeek-V4.1-Flash (om/gpu/dsv41/decoder/).
3note Mirrors DeepInfer's YOCO / CED SWA bounded replay rules (doc/inference_deployment_qa.md):
4note 1. Block KV cache hit length L1 is capped at L - (SWA_WINDOW + SWA_BACKTRACK_MARGIN) = L - 134,
5note aligned down to the 128-token block size.
6note 2. If the SWA KV cache at [L1 - 128, L1) is missing, back off by one block (L1 - 128) so the
7note last 128 tokens of the hit prefix are replayed to rebuild SWA state while block KV is reused.
8note 3. During prompt prefill in compact mode, decoder SWA layers (layer >= 20) only run on the final
9note keep_tokens (default 140 = 128 SWA window + 12 backtracking margin), nearly halving prefill.
10note 4. During bounded replay starting at position s, query i attends only to keys in [max(s, i - 127), i].
11
12const SWA_WINDOW ulong 128
13const SWA_BLOCK ulong 128
14const SWA_BACKTRACK_MARGIN ulong 6
15const SWA_YOCO_CAP_MARGIN ulong 134
16const SWA_DEFAULT_KEEP ulong 140
17const SWA_DECODER_FIRST_LAYER ulong 20
18
19global swaCompactMode ulong 1
20global swaKeepTokens ulong 1
21global swaReplayStart ulong 1
22global swaSkippedLayers ulong 1
23global swaExecutedLayers ulong 1
24
25fn swaInit compact bool keepTokens ulong to bool
26if lt keepTokens SWA_YOCO_CAP_MARGIN
27ret false
28end
29if compact
30put swaCompactMode 0 1
31else
32put swaCompactMode 0 0
33end
34put swaKeepTokens 0 keepTokens
35put swaReplayStart 0 0
36put swaSkippedLayers 0 0
37put swaExecutedLayers 0 0
38ret true
39end
40
41fn swaInitDefault to void
42ok = swaInit true SWA_DEFAULT_KEEP
43end
44
45note Compute effective prefix cache hit length under DeepInfer's block+SWA hit policy.
46fn swaComputeHitLen promptLen ulong blockHitLen ulong hasSwaAtHit bool to ulong
47if le promptLen SWA_YOCO_CAP_MARGIN
48ret 0
49end
50capRaw = sub promptLen SWA_YOCO_CAP_MARGIN
51capAligned = mul div capRaw SWA_BLOCK SWA_BLOCK
52l1 = mul div blockHitLen SWA_BLOCK SWA_BLOCK
53if gt l1 capAligned
54l1 = capAligned
55end
56if eq l1 0
57ret 0
58end
59if hasSwaAtHit
60ret l1
61end
62if lt l1 SWA_WINDOW
63ret 0
64end
65ret sub l1 SWA_WINDOW
66end
67
68note Compute decoder bounded-replay start position s for a prompt of length promptLen.
69fn swaDecoderReplayStart promptLen ulong to ulong
70if eq get swaCompactMode 0 0
71ret 0
72end
73keep = get swaKeepTokens 0
74if le promptLen keep
75ret 0
76end
77ret sub promptLen keep
78end
79
80note Return true if layer L must execute at prompt position pos (0..promptLen-1) during prefill.
81note Encoder layers (< 20) always run (they produce the compressed KV projections);
82note decoder SWA layers (>= 20) only run inside the bounded suffix [promptLen - keepTokens, promptLen).
83fn swaShouldRunPrefillLayer layer ulong pos ulong promptLen ulong to bool
84if or eq get swaCompactMode 0 0 ge pos promptLen
85put swaExecutedLayers 0 add get swaExecutedLayers 0 1
86ret true
87end
88if lt layer SWA_DECODER_FIRST_LAYER
89put swaExecutedLayers 0 add get swaExecutedLayers 0 1
90ret true
91end
92startPos = swaDecoderReplayStart promptLen
93if ge pos startPos
94put swaExecutedLayers 0 add get swaExecutedLayers 0 1
95ret true
96end
97put swaSkippedLayers 0 add get swaSkippedLayers 0 1
98ret false
99end
100
101note Compute truncated SWA key range [outRange[0], outRange[1]] for query at position pos
102note when replaying from replayStart: kStart = max(replayStart, pos >= 127 ? pos - 127 : 0), kEnd = pos.
103fn swaTruncatedKeyRange pos ulong replayStart ulong outRange edit ulong 2 to bool
104if lt pos replayStart
105ret false
106end
107kStart = ulong 0
108if ge pos sub SWA_WINDOW 1
109kStart = sub pos sub SWA_WINDOW 1
110end
111if lt kStart replayStart
112kStart = replayStart
113end
114put outRange 0 kStart
115put outRange 1 pos
116ret true
117end