feat(moonshine,transformer-core): fixed-max cross-memory padding mask for streaming decode - #267
Merged
michalharakal merged 1 commit intoJul 30, 2026
Conversation
… 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>
michalharakal
force-pushed
the
feat/streaming-cross-memory-mask
branch
from
July 30, 2026 11:11
dd0495f to
96d925f
Compare
michalharakal
merged commit Jul 30, 2026
72fa86f
into
feat/true-dynamic-decode-export
2 checks passed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Streaming ASR finalizes a variable-length encoder memory, but the decoder prefill is fixed-shape (IREE rejects
dynamic_reshapefor the memory head-split). This zero-pads the cross (encoder) memory to a fixedMAXand masks the padding out of cross-attention — so one prefill + onewith_pastvmfb pair serve any encoder length ≤ MAX, while the self-cache stays dynamic (growing).What
MultiHeadAttention— optional trailingcrossMaskonattentionImpl+forwardWithKV, applied as SDPAmask = slidingMask ?: crossMask. Defaultnull→ byte-identical for all existing callers (verified: llm-core 101 + gemma 77 tests green, incl. MHA/SDPA/sliding-window coverage).MoonshineDecoder— threadscrossMaskthrough the layer/modelforwardPrefill(viaMHA.forwardWithKV) and the hand-wiredforwardWithPast(sdpaMerge). Backward compatible (trailing default; v1 positional callers untouched).MoonshineV2DecoderBakeTest—MOONSHINE_V2_MAX_MEM=Npads both prefill memory and the with_past cross cache to N and adds acrossMaskinput[1,1,1,N]to both graphs.Verification
End-to-end: masked decode over memory padded 64→96 (with a garbage tail) == onnxruntime unpadded-64 token-for-token on real audio; the control (no mask) corrupts. Since verified on the SL2610 board (full v2 pipeline decodes the reference clip identically to ONNX).