Skip to content

Add Moonshine speech recognition support - #463

Merged
justinchuby merged 8 commits into
mainfrom
justinchuby-add-moonshine-asr
Aug 13, 2026
Merged

justinchuby merged 8 commits into
mainfrom
justinchuby-add-moonshine-asr

Conversation

@justinchuby

@justinchuby justinchuby commented Aug 8, 2026 •

Copy link
Copy Markdown
Member

Summary

  • add native moonshine support for moonshine-ai/moonshine-tiny without aliasing it to Whisper
  • implement the raw-waveform convolution encoder, GroupNorm, partial interleaved RoPE, bias-free encoder/decoder attention, gated decoder MLP, cross-attention masking, tied output projection, and cached decoding
  • generalize SpeechToTextTask while preserving Whisper, including correct 80/128 num_mel_bins input-channel handling
  • export tokenizer, audio_processor.json, and composite onnx-genai metadata
  • add L1 graph/config/alignment, L2 real-config, L3 synthetic/real-weight parity, and real-audio L4/L5 goldens

Updated from main

Rebased onto origin/main at 1e6f61f42b0ddbc90f60bccbf00eff26cd484256, including main's fused Swish support. Moonshine now uses the public SiLU component and emits one ONNX Swish op per decoder layer instead of Sigmoid + Mul.

Fusion and optimized-graph audit

Audited fp16 opset-24 graphs with ONNX Runtime GPU 1.28.0 on CUDA EP; CUDAExecutionProvider was asserted first for encoder and decoder.

Graph Mobius nodes ORT-optimized nodes Key optimized ops Identity Unused initializers Memcpy
encoder 115 131 6 Attention, 6 BiasGelu, 12 SkipLayerNormalization 0 0 0
decoder 151 151 12 Attention, 6 QuickGelu(alpha=1.0), 18 SkipLayerNormalization 0 0 0

Additional structural evidence:

  • Mobius decoder graph: 6 Swish, 0 Sigmoid
  • ORT folds each Swish to QuickGelu(alpha=1.0)
  • encoder GroupNormalization is optimized without CPU fallback
  • only one encoder transpose remains, required for Conv1d [B,C,T] to transformer [B,T,C]
  • no protobuf construction APIs
  • no opset-lowering workaround needed on ORT 1.28

The standard ONNX Attention op is intentional: encoder and cross-attention require explicit padding bias, which contrib GQA rejects, and Moonshine's head dimension 36 is not XQA-eligible.

Validation

CPU and CUDA parity

Environment: NVIDIA RTX A1000, driver 573.44, CUDA 12.8, ONNX Runtime GPU 1.28.0.

Real 652-129742-0006.flac, fp32:

Boundary Max abs diff Mean abs diff Result
encoder hidden states 0.0045447 0.0001437 finite; masks exact
decoder prefill logits 0.0098586 0.0011758 finite; identical argmax 29892
greedy transcription exact exact CPU == CUDA == 32-token golden

Dtype coverage

  • fp16 CUDA: finite encoder/decoder state and exact 32/32 fp32-golden transcription tokens
  • bf16 CUDA: finite state and exact Hugging Face bf16 CUDA generation tokens; bf16 output differs from fp32 at the expected near-tie prefix

Test commands/results

  • python -m pytest tests/build_graph_test.py -k "moonshine or whisper" -q: 17 passed
  • python -m pytest tests/moonshine_integration_test.py -q: 5 passed
  • speech config/alignment/parity selection: 18 passed
  • CUDA tests/e2e_golden_test.py -m "golden or generation" -k moonshine: 2 passed
  • fusion/task/export selection: 13 passed
  • YAML schema + model coverage: 945 passed, 215 skipped
  • lintrunner -a: no lint issues
  • automated review findings resolved, including explicit Whisper 128-mel config/graph regression coverage

Checklist / waivers

  • L1 graph construction and weight alignment
  • L2 real Hugging Face config build
  • L3 synthetic and real-weight encoder/decoder/cache parity
  • L4 real-audio logits golden on CPU and CUDA
  • L5 deterministic transcription golden on CPU and CUDA
  • fp32/fp16/bf16 CUDA validation
  • CLI export, processor/tokenizer wiring, runtime load, and generation
  • ORT 1.28 optimized-graph and fusion audit with zero Memcpy
  • full lintrunner -a, schema, coverage, and targeted regressions

Waivers:

  • Native onnxruntime-genai: upstream's audio-language runtime is Whisper-specific and hardcodes 3000 input / 1500 encoder frames plus Whisper alignment tensors. Moonshine uses variable-length raw waveforms, so this PR explicitly rejects falsely mapping it to Whisper and validates onnx-genai metadata plus direct ONNX Runtime CUDA generation instead.
  • Foundry Local: not installed in this environment.
  • Olive quantization: Olive is not installed in this environment.

@justinchuby
justinchuby requested review from a team and a lite review from Copilot August 8, 2026 00:03
@github-actions

github-actions Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing 1e6f61f → 483da44

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 60 60 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 105 105 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 54 54 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 60 60 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 56 56 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 296 KB 296 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 94 94 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 58 58 +0.0% ⚪
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 54 54 +0.0% ⚪
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 60 60 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 56 56 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 264 264 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 126 126 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 428 428 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 166 166 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@github-actions

github-actions Bot commented Aug 8, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 1e6f61f → 483da44

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gemma4 (gemma4) decoder 0 ⚪
gemma4 (gemma4) embedding 0 ⚪
gemma4 (gemma4) vision_encoder 0 ⚪
gemma4_text model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 0 ⚪
phi3 (static-cache) model 0 ⚪
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision_encoder 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪

No architecture changes detected. ✅


Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds first-class support for the Hugging Face Moonshine raw-waveform ASR architecture to mobius, extending the existing speech-to-text pipeline beyond Whisper while keeping export + test infrastructure consistent with the repo’s task/model/registry stack.

Changes:

  • Introduces native moonshine model/config/registry support and ONNX graph construction (encoder + cached decoder).
  • Generalizes SpeechToTextTask and onnx-genai metadata/export paths to support model-declared audio inputs and optional encoder attention-mask routing.
  • Adds/extends L1–L5 coverage: build/weight-alignment/integration parity tests plus committed real-audio goldens and cases.

Reviewed changes

Copilot reviewed 26 out of 26 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/weight_alignment_test.py Adds speech encoder-decoder weight-alignment parametrization.
tests/moonshine_integration_test.py New parity tests for Moonshine synthetic + real checkpoints (encoder/decoder/cache).
tests/e2e_golden_test.py Generalizes ASR e2e runner to respect encoder input dtypes and optional encoder masks.
tests/build_graph_test.py Adds L1 graph/IO/initializer assertions for Moonshine package build.
tests/_test_configs.py Adds Moonshine to shared SPEECH_CONFIGS test matrix.
testdata/golden/audio/moonshine-tiny.json Adds Moonshine L4 logits golden.
testdata/golden/audio/moonshine-tiny_generation.json Adds Moonshine L5 generation golden.
testdata/cases/audio/moonshine-tiny.yaml Adds Moonshine golden test case definition.
src/mobius/tasks/_speech_to_text.py Generalizes SpeechToTextTask encoder/decoder IO to model-declared audio inputs + optional mask routing.
src/mobius/models/moonshine.py Implements Moonshine encoder/decoder modules (raw-waveform conv stem, partial interleaved RoPE, cache).
src/mobius/models/init.py Exposes Moonshine model in public models API.
src/mobius/integrations/ort_genai/auto_export.py Adds explicit guard for native onnxruntime-genai Moonshine export.
src/mobius/integrations/ort_genai/auto_export_test.py Tests the Moonshine native-runtime rejection path.
src/mobius/integrations/onnx_genai/inference_metadata.py Adds optional encoder_attention_mask routing to speech-to-text composite metadata.
src/mobius/integrations/onnx_genai/auto_export.py Emits audio_processor.json and auto-detects encoder mask dataflow for ASR packages.
src/mobius/integrations/onnx_genai/auto_export_test.py Adds coverage for encoder attention-mask dataflow emission.
src/mobius/components/_whisper.py Makes Conv1d bias optional to support bias-free conv stems.
src/mobius/components/init.py Exports apply_rotary_pos_emb for shared RoPE usage.
src/mobius/_testing/generation.py Updates speech-to-text generator to optionally pass encoder attention masks.
src/mobius/_registry.py Registers moonshine model_type and adds a default model ID mapping.
src/mobius/_configs/_base.py Introduces SpeechToTextConfig base + MoonshineConfig; refactors WhisperConfig inheritance/signature.
src/mobius/_configs/init.py Re-exports MoonshineConfig and SpeechToTextConfig.
src/mobius/_config_resolver_test.py Adds Moonshine config extraction + default task coverage.
src/mobius/init.py Exposes new configs in public package API.
scripts/generate_golden.py Generalizes speech-to-text golden generation to AutoModel/AutoProcessor (not Whisper-only).
README.md Adds Moonshine to supported Speech-to-Text models list.
Suppressed comments (1)

src/mobius/tasks/_speech_to_text.py:82

  • WhisperConfig still carries num_mel_bins, but SpeechToTextTask now sizes the encoder input using encoder_input_channels (default=80). This can silently produce an invalid graph when num_mel_bins != encoder_input_channels (e.g. the tiny Whisper configs in tests set num_mel_bins=16), because Conv1d weights are created for num_mel_bins while the task declares a different channel dimension. Derive the channel dimension from num_mel_bins when building mel-feature encoders to keep task I/O consistent with the module.
        input_shape = [batch, audio_seq_len]
        if config.encoder_input_channels is not None:
            input_shape.insert(1, config.encoder_input_channels)
        encoder_input = builder.input(
            config.encoder_input_name,
            dtype=config.dtype,
            shape=input_shape,
        )

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/mobius/tasks/_speech_to_text.py Resolved
Comment thread src/mobius/models/moonshine.py Resolved

@justinchuby justinchuby left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed the current diff for correctness; no additional high-confidence issues found beyond the existing unresolved review threads.

justinchuby and others added 8 commits August 13, 2026 15:14
Implement Moonshine's raw-waveform convolutional encoder, interleaved partial-RoPE encoder-decoder attention, cached decoding, config and weight mapping, and generalized speech-to-text packaging. Add synthetic and checkpoint parity tests, real-audio L4/L5 goldens, processor metadata, and explicit native ORT GenAI capability handling.

Co-authored-by: Copilot App <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Decompose Moonshine's single-group normalization into reduction and affine operations so CUDA execution no longer falls back to CPU for the full convolution activation. Add graph coverage that prevents the CPU-only GroupNormalization op from returning.

Co-authored-by: Copilot App <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Synchronize the generic speech encoder channel contract with Whisper's num_mel_bins so 128-bin large-v3 and turbo checkpoints do not retain the legacy 80-channel default. Add resolver and graph-shape regression coverage.

Co-authored-by: Copilot App <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Initialize speech-to-text and seq2seq empty KV caches from each decoder input's declared dtype so fp16 and bf16 CUDA generation do not receive fp32 cache tensors. Update the helper contract documentation accordingly.

Co-authored-by: Copilot App <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Describe the configured encoder input and optional mask dataflow instead of implying that every speech-to-text model follows Whisper-specific I/O.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
Clarify configurable speech encoder and mask routing, use the public ONNX GroupNorm component with parity coverage, remove the experimental runtime suggestion from the ORT GenAI error, and validate the declared Moonshine L5 reference.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: justinchuby <[email protected]>
Apply the repository formatter to the L5 golden reference validation added during review.

Co-authored-by: Copilot <[email protected]>
Signed-off-by: justinchuby <[email protected]>
Use the public SiLU component added on main so Moonshine emits one ONNX Swish op per decoder layer instead of Sigmoid plus Mul. Add structural coverage that prevents the decomposed activation from returning.

Co-authored-by: Copilot App <[email protected]>
Signed-off-by: Justin Chu <[email protected]>
@justinchuby
justinchuby force-pushed the justinchuby-add-moonshine-asr branch from 922628e to 483da44 Compare August 13, 2026 22:22
@justinchuby
justinchuby merged commit ed0319d into main Aug 13, 2026
21 of 22 checks passed
@justinchuby
justinchuby deleted the justinchuby-add-moonshine-asr branch August 13, 2026 22:28
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.

3 participants