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