Skip to content

Load VibeVoice-ASR, MusicGen, Audio Flamingo 3 and Music Flamingo through FastModel - #14781

Merged
danielhanchen merged 3 commits into
mainfrom
fix-audio-attn-routing
Oct 23, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
fix-audio-attn-routing

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Oct 22, 2026 •

Copy link
Copy Markdown
Member

Four audio models could not be loaded through FastModel.from_pretrained at all. This PR fixes the two causes.

What was broken

Model Before Cause
VibeVoice-ASR ValueError: VibeVoiceAcousticTokenizerEncoderModel does not support an attention implementation through torch.nn.functional.scaled_dot_product_attention yet resolve_attention_implementation returns a plain "sdpa", which transformers applies to every sub-config, including towers that cannot run SDPA.
MusicGen ValueError: EncodecModel does not support ... scaled_dot_product_attention yet Same: the EnCodec audio encoder has no SDPA path.
Audio Flamingo 3, Music Flamingo ValueError: Unrecognized configuration class AudioFlamingo3Config ... On transformers 5.x they register only under AutoModelForSeq2SeqLM, so the loader's fallback chain ended at AutoModelForImageTextToText.

Changes

  • vision.py: the sub-config class lookup the flash-attention scoping already does is now one shared generator, and a new _scoped_sdpa gives towers without SDPA eager through transformers' per-sub-config attention dict while every other tower keeps SDPA. Applied wherever the resolver settles on "sdpa"; transformers versions without the dict mapping get eager.
  • loader.py: _resolve_audio_lm_auto_model sits just before the final fallback and accepts AutoModelForCausalLM / AutoModelForSeq2SeqLM only when it builds the config's own class and the config has an audio_config, so the only path it can change is today's crash.
  • vision.py: _audio_lm_class makes processor selection pick AutoProcessor for those loads.

Over the 114 SDPA-capable composite configs in transformers 5.17, the SDPA scoping changes only VibeVoice-ASR and VibeVoice TTS (0 false positives).

Evidence

Tiny random-weight copies (real config shrunk, real processor), 5 training steps per case on a Colab GPU in bf16, main vs this branch:

Model LoRA 16 bit before / after QLoRA 4 bit before / after
VibeVoice-ASR load error / pass load error / pass
Audio Flamingo 3 load error / pass load error / pass
Music Flamingo load error / pass load error / pass
MusicGen load error / loads and trains (eager) load error / loads and trains (eager)
Qwen2-Audio, Qwen3-ASR (controls) pass / pass pass / pass

MusicGen's compiled path also needs unslothai/unsloth-zoo#1978 (absolute imports in the generated module, EnCodec's LSTM left uncompiled, label masking only over matching positions); with both, MusicGen LoRA and QLoRA train compiled with 0 graph breaks.

14 models that already loaded (Whisper, Qwen2-Audio, Qwen3-ASR, Gemma 3n, Gemma 4, Orpheus, Qwen2.5-Omni, Granite Speech, CSM, Dia, SeamlessM4T v2, Moonshine, Canary, wav2vec2) load with the identical model class, processor class and per-tower attention on both sides. New CPU tests in tests/test_audio_load_routing.py; the 18 existing attention / processor routing test files pass (224 passed, 6 skipped).

Related: #14744 (training fixes for speech, CTC and classification models).

@danielhanchen danielhanchen self-assigned this Oct 22, 2026
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-22T19:12:51.465653Z 870c2e6 Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 870c2e6819

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread unsloth/models/loader.py
auto_model = (
_resolve_omni_auto_model(model_config, **_probe_hub_kwargs)
or _resolve_speech_seq2seq_auto_model(model_config, **_probe_hub_kwargs)
or _resolve_audio_lm_auto_model(model_config, **_probe_hub_kwargs)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Route audio LM configs before the architecture-name gate

When the caller passes a freshly constructed AudioFlamingo3Config or MusicFlamingoConfig through config=, architectures is normally unset and there is no vision config, so the earlier is_vlm check is false. This new resolver is therefore never reached; execution instead selects AutoModelForCausalLM, which does not map these seq2seq-only audio configs, and loading still fails. Probe _resolve_audio_lm_auto_model independently of the elif is_vlm branch (or include recognized audio configs in that branch's predicate).

Useful? React with 👍 / 👎.

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.

A freshly built AudioFlamingo3Config passed via config= without architectures is an unusual path; checkpoints carry architectures and load through this resolver.

Comment thread unsloth/models/_utils.py
Comment on lines +2710 to +2711
if final_attn_impl == "sdpa":
final_attn_impl = _set_attn_impl(config, _scoped_sdpa(config, model_class))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Scope SDPA when the request is a mapping

When callers use Transformers' supported mapping form, such as attn_implementation={"": "sdpa"}, final_attn_impl is a dict and this condition skips _scoped_sdpa. The root value is consequently propagated to VibeVoice-ASR's tokenizer towers or MusicGen's EnCodec tower without the required eager overrides, reproducing the same scaled_dot_product_attention load failure that this change fixes for the scalar/default path. Merge the unsupported-tower overrides whenever the mapping's default is SDPA, while preserving explicit compatible entries.

Useful? React with 👍 / 👎.

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.

The mapping form with an explicit sdpa root is an opt-in; the default and scalar paths are what users hit, and those are scoped.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

1 similar comment
@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Swish!

Reviewed commit: 870c2e6819

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

danielhanchen added a commit that referenced this pull request Oct 22, 2026
pre-commit.ci fails `ruff-format-with-kwargs` on every open PR (#14806,
#14805, #14803, #14790, #14785, #14781, #14780, #14777, #14776, #14775,
#14774, #14773, #14772, #14770, #14769, #14768, and others): "files were
modified by this hook". pre-commit.ci runs the hooks on all files, and
20 files on main (26 by the time this lands) are not in the formatter's
output, so every PR is reformatted outside its own diff. Where
pre-commit.ci can push, its autofix commit drags those unrelated files
into the PR (for example #14790's touches `tests/test_block_compile.py`,
`tests/test_bnb_integration_compile.py` and `unsloth/models/_utils.py`).

The drift came in with PRs merged today, among them #14702, #14751,
#14759, #14784, #14786, #14787, #14788, #14791 and the FP8 block decode,
GRPO routing replay, 4-bit decode, compiled blocks and MoE LoRA target
changes.

### Change

`python scripts/run_ruff_format.py` (the hook's own entry point,
`ruff==0.6.9` as pinned in `.pre-commit-config.yaml`) applied to those
20 files, plus 6 more from #14739, #14779 and #14806, which merged while
this was open. Formatting only.

### Verification

- Every changed file parses to an identical AST before and after.
- A second run of the hook over the same files changes nothing (fixed
point), and over all tracked Python files on this branch it changes
nothing.
- `ruff check` passes on the changed files;
`tests/test_formatter_fixed_point.py` passes (16).

Open PRs pick this up on their next pre-commit.ci run after merging
main.
…g-main

# Conflicts:
#	tests/test_repo_code_vlm_processor.py
#	unsloth/models/loader.py
#	unsloth/models/vision.py
@danielhanchen
danielhanchen merged commit d0e8c6b into main Oct 23, 2026
3 of 20 checks passed
@danielhanchen
danielhanchen deleted the fix-audio-attn-routing branch October 23, 2026 04:40
shimmyshimmer pushed a commit to shimmyshimmer/unsloth-zoo-staging-2 that referenced this pull request Oct 23, 2026
…udio Flamingo and Granite Speech collator (unslothai#1978)

Companion to unslothai/unsloth#14744: the unsloth_zoo side of
fine-tuning audio models with the default `FastModel` flow, in LoRA (16
bit) and QLoRA (4 bit).

## What was broken and what changes

| Model | Before | Fix |
|---|---|---|
| Sesame CSM | `get_peft_model` raises `NoMatchingPeftModuleError` |
`get_peft_regex`'s untagged-decoder fallback checked its regex with
`re.search`, but PEFT matches targets with `re.fullmatch`, so the
fallback was judged to match and the empty regex reached PEFT. Now
checked with `fullmatch`, like PEFT. |
| CSM, Higgs Audio, Dia | The fallback could put LoRA on the frozen
audio codec / tokenizer model | The fallback regex excludes
`codec_model`, `audio_tokenizer`, `acoustic_tokenizer` and
`semantic_tokenizer` submodules. |
| Canary, Parakeet (QLoRA, compiled) | `RuntimeError: Expected weight to
have type Float but got BFloat16` in the compiled `BatchNorm1d` forward
| The compiler already casts the input to the weight dtype for norms on
the eager path; BatchNorm now gets the same cast in the compiled path.
No-op when the dtypes already match; `affine = False` (no weight) is
left alone. |
| Audio Flamingo 3, Music Flamingo | `UnslothVisionDataCollator`'s
top-level `padding = True` shortened the audio features, which these
towers need at the processor's full 30 s | Padding for these two model
types goes to the text kwargs only. |
| Dia (gradient checkpointing on, the default) | The text encoder's LoRA
got no gradients (0 of 12 encoder adapters; 12 of 12 with checkpointing
off) | The encoder takes only token ids and has no
`get_input_embeddings`, so the checkpointing pre-hook found no float
input to mark. Its own `Embedding` output is now made to require grad,
outside torch.compile only. |
| AST (compiled, the default) | `Unsupported: returning intermediate
with requires_grad_()` on the first step | The input-embedding hook ran
inside the compiled `ASTEmbeddings`. When the compiler swapped in the
class that calls the input embedding, the hook now goes on that class's
output, outside the compiled region. |
| MusicGen (compiled) | `ImportError: attempted relative import with no
known parent package`, then `Dynamo does not support RNN, GRU, or LSTM`,
then `The shape of the mask [2, 7] ... does not match ... [2, 50, 4]` |
The compiler copied `MusicgenForConditionalGeneration.__init__`'s
function-local `from ..auto.modeling_auto import ...` into the generated
module; those relative imports are now made absolute. Modules that own
an `nn.LSTM` / `GRU` / `RNN` (EnCodec's `EncodecLSTM`) are left
uncompiled. The generated `mask_attention_mask_out` now masks labels
only when the attention mask covers the labels' positions (MusicGen's
mask is the text encoder's; its labels are decoder codebook frames). |
| Granite Speech, Granite Speech Plus | `ValueError: Audio features and
audio tokens do not match, tokens: 0` | Their chat templates drop an
`{"type": "audio"}` part (Plus) or print the content list's repr (3.3);
the model expects the `<|audio|>` token inside a string turn. The
collator now detects that once (render with and without an audio part)
and flattens content to a string with the processor's audio token,
through the existing string-content shim. Templates that render audio
are untouched. |

Detection for the Granite change was run against the real processors of
Granite Speech, Granite Speech Plus, Qwen2-Audio, Qwen2.5-Omni,
Qwen3-ASR, Gemma 3n, Gemma 4, GLM-ASR, Audio Flamingo 3, Music Flamingo,
Fun-ASR-Nano and Voxtral: only the two Granite processors change.

## Evidence

Tiny random-weight copies of each model (real config shrunk, real
processor), 5 training steps per case on a Colab GPU in bf16, with
unslothai/unsloth#14744:

| Model | Before | After |
|---|---|---|
| Canary QLoRA | BatchNorm dtype error | pass |
| Parakeet CTC QLoRA | BatchNorm dtype error | pass |
| CSM LoRA / QLoRA | `NoMatchingPeftModuleError` | trains |
| Dia LoRA / QLoRA | encoder adapters never move | pass (encoder and
decoder adapters move) |
| AST LoRA / QLoRA, compiled | `requires_grad_()` Dynamo error | pass, 0
graph breaks |
| Granite Speech / Plus LoRA / QLoRA | audio token count mismatch | pass
|
| MusicGen LoRA / QLoRA, compiled (with unslothai/unsloth#14781) | load
/ compile errors | trains, 0 graph breaks |
| Gemma 3n LoRA (control) | 6 graph breaks | 6 graph breaks |
| Qwen2-Audio, Qwen3-ASR (controls) | pass | pass |

New CPU tests (fail on `main`, pass here):
`tests/test_peft_regex_untagged_decoders.py`,
`tests/test_compiler_batchnorm_dtype.py`,
`tests/test_vision_collator_audio_full_length.py`,
`tests/test_vision_collator_audio_token_in_text.py`,
`tests/test_requires_grad_token_id_tower.py`,
`tests/test_requires_grad_compiled_embeddings.py`,
`tests/test_compiler_relative_imports.py`,
`tests/test_compiler_mask_labels_shape.py`. The existing collator, pad
token and PEFT regex suites pass (324 passed, 5 skipped).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant