Skip to content

feat(export): env-gated true-dynamic KV-cache decode graphs (Gemma + Moonshine v2) - #266

Merged
michalharakal merged 4 commits into
developfrom
feat/true-dynamic-decode-export
Jul 31, 2026
Merged

feat(export): env-gated true-dynamic KV-cache decode graphs (Gemma + Moonshine v2)#266
michalharakal merged 4 commits into
developfrom
feat/true-dynamic-decode-export

Conversation

@michalharakal

Copy link
Copy Markdown
Contributor

Adds opt-in flags that trace the KV-cache seq dim as a real dynamic extent (Dim.DYNAMIC) instead of the fixed/sentinel path, so a single compiled vmfb serves every autoregressive decode position.

What

  • FunctionGemmaExportGEMMA_TRUE_DYNAMIC=1 threads Dim.DYNAMIC through the with_past trace and skips the old SENTINEL_PAST + relaxSeqDimToDynamic text-hack (which never actually iree-compiled).
  • MoonshineV2DecoderBakeTestMOONSHINE_V2_TRUE_DYNAMIC=1 traces both the self-cache seq dim (grows per step) and the cross-cache frames dim (varies per utterance) as Dim.DYNAMIC.

Both env-gated and off by default — the existing export paths are unchanged.

Requires

The core dynamic-shape capability (Dim + dynamic-safe tracer/emitter, SKaiNET#891, now on develop). Until a core release ships it, build with -PuseLocalSkainet=true.

Verification

Both graphs self-compile to dynamic vmfbs; Moonshine v2 growing-cache decode matches onnxruntime cos = 1.0 at every step.

…Moonshine v2)

Add opt-in flags that trace the KV-cache seq dim as a real dynamic extent
(Dim.DYNAMIC) instead of the fixed/sentinel path, so one compiled vmfb serves
every autoregressive decode position:

- FunctionGemmaExport: GEMMA_TRUE_DYNAMIC=1 threads Dim.DYNAMIC through the
  with_past trace and skips the SENTINEL_PAST + relaxSeqDimToDynamic text-hack
  (which never actually iree-compiled). Default export path unchanged.
- MoonshineV2DecoderBakeTest: MOONSHINE_V2_TRUE_DYNAMIC=1 traces both the
  self-cache seq dim (grows per step) and the cross-cache frames dim (varies per
  utterance) as Dim.DYNAMIC.

Both env-gated and off by default. Requires the core dynamic-shape capability
(sk.ainet.core Dim + dynamic-safe tracer/emitter, PR SKaiNET#891) — build with
-PuseLocalSkainet=true until that lands in a release. Verified: both graphs
self-compile to dynamic vmfbs, and Moonshine v2 growing-cache decode matches
onnxruntime cos=1.0 at every step.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
SKaiNET development team and others added 3 commits July 30, 2026 13:10
…blished)

0.38.0 (cut from develop, on Maven Central) carries the SKaiNET#891 dynamic-shape
core (Dim + dynamic tracer/emitter). Lets the true-dynamic decode graphs + Gemma
GEMMA_TRUE_DYNAMIC export build against published core, no -PuseLocalSkainet needed.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
… for streaming decode

Streaming ASR finalizes a variable-length encoder memory, but the decoder prefill
is fixed-shape (iree rejects dynamic_reshape for the memory head-split). Fix by
zero-padding the cross (encoder) memory to a fixed MAX and masking the padding out
of cross-attention, so ONE prefill + ONE with_past vmfb pair serve any encoder
length ≤ MAX while the self-cache stays dynamic (growing).

- transformer-core MultiHeadAttention: optional trailing `crossMask` on
  attentionImpl + forwardWithKV, applied as SDPA `mask = slidingMask ?: crossMask`.
  Default null → byte-identical for all existing callers (verified: llm-core 101 +
  gemma 77 tests green, incl. MHA/SDPA/sliding-window coverage).
- MoonshineDecoder: thread `crossMask` through the layer/model forwardPrefill
  (via MHA.forwardWithKV) and the hand-wired forwardWithPast (sdpaMerge). Backward
  compatible (trailing default; v1 positional callers untouched).
- MoonshineV2DecoderBakeTest: MOONSHINE_V2_MAX_MEM=N pads both prefill memory and
  with_past cross cache to N and adds a crossMask input [1,1,1,N] to both graphs.

Verified end-to-end: masked decode over memory padded 64→96 (with a garbage tail)
== onnxruntime unpadded-64 token-for-token on real audio; control (no mask) corrupts.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
…memory-mask

feat(moonshine,transformer-core): fixed-max cross-memory padding mask for streaming decode
@michalharakal
michalharakal merged commit ceb054a into develop Jul 31, 2026
2 checks passed
@michalharakal
michalharakal deleted the feat/true-dynamic-decode-export branch July 31, 2026 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant