[AudioFlamingo3] Fix bfloat16 dtype mismatch in audio encoder positional embedding - #47258
Conversation
In AudioFlamingo3AudioEncoder.forward(), the positional embedding addition
was missing a dtype cast:
hidden_states = inputs_embeds + self.embed_positions.weight
Since _keep_in_fp32_modules_strict keeps embed_positions in float32,
this addition upcasts hidden_states to float32. The subsequent LayerNorm,
whose weight is bfloat16, then raises:
RuntimeError: expected scalar type Float but found BFloat16
Fix: cast the result back to inputs_embeds.dtype, matching the pattern
already used in VoxtralEncoder.forward:
hidden_states = (inputs_embeds + self.embed_positions.weight).to(inputs_embeds.dtype)
Verified: AudioFlamingo3 on MMAU-full (9000 samples), bf16 + flash_attn2,
bs=20 → 72.49% accuracy (matches bs=1 baseline of 72.44%).
e498f7b to
6537be5
Compare
|
run-slow: audioflamingo3 |
|
This comment contains models: ["models/audioflamingo3"] |
CI ResultsCommit Info
The test failure analysis could not be completed. Please check the workflow run for details. |
|
Run slow failed but the tests passed all 🤷 waiting for the others on a final confirmation but should be good |
ebezzam
left a comment
There was a problem hiding this comment.
Thanks @snkii for spotting this! It seems like this bug snuck in with #45534 as overwriting _keep_in_fp32_modules_strict was kept with the base model here as was done for the original AudioFlamingo3ForConditionalGeneration. But the PR did not do it for the refactored AudioFlamingo3ForConditionalGeneration
Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com>
|
Sounds good! Best to group the fixes if possible indeed |
| ) | ||
| class AudioFlamingo3ForConditionalGeneration(AudioFlamingo3PreTrainedModel, GenerationMixin): | ||
| _keep_in_fp32_modules_strict = ["embed_positions"] | ||
| _keep_in_fp32_modules_strict = None |
There was a problem hiding this comment.
You'll have to regenerate the modeling file, e.g. with
python utils/modular_model_converter.py \
--files-to-parse src/transformers/models/audioflamingo3/modular_audioflamingo3.py
And the modeling of other models will also need to be regenerated as they inherited from AudioFlamingo3
python utils/modular_model_converter.py \
--files-to-parse src/transformers/models/glmasr/modular_glmasr.py
python utils/modular_model_converter.py \
--files-to-parse src/transformers/models/musicflamingo/modular_musicflamingo.py
python utils/modular_model_converter.py \
--files-to-parse src/transformers/models/qwen3_asr/modular_qwen3_asr.py
There was a problem hiding this comment.
You can also just run make fix-repo or just python utils/modular_model_converter.py (that will run all conversions for you)
Propagate _keep_in_fp32_modules_strict = None from audioflamingo3 to derived modular models.
|
[For maintainers] Suggested jobs to run (before merge) run-slow: audioflamingo3, glmasr, musicflamingo, qwen3_asr |
|
run-slow: audioflamingo3, glmasr, musicflamingo, qwen3_asr |
|
This comment contains models: ["models/audioflamingo3", "models/glmasr", "models/musicflamingo", "models/qwen3_asr"] |
CI recapDashboard: View test results in Grafana |
|
The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update. |
|
Thank you for your contribution 🤗! CI Security Gate — automatic approval blockedThis PR was not automatically approved for CI because the security gate failed. Possible reasons:
See the workflow run for the exact violations. A maintainer can review and manually approve CI if a finding is a false positive. |
|
Thanks for the fix(es), merging now 🤗 |
…nal embedding (huggingface#47258) * [AudioFlamingo3] Fix dtype mismatch when loading with bfloat16 In AudioFlamingo3AudioEncoder.forward(), the positional embedding addition was missing a dtype cast: hidden_states = inputs_embeds + self.embed_positions.weight Since _keep_in_fp32_modules_strict keeps embed_positions in float32, this addition upcasts hidden_states to float32. The subsequent LayerNorm, whose weight is bfloat16, then raises: RuntimeError: expected scalar type Float but found BFloat16 Fix: cast the result back to inputs_embeds.dtype, matching the pattern already used in VoxtralEncoder.forward: hidden_states = (inputs_embeds + self.embed_positions.weight).to(inputs_embeds.dtype) Verified: AudioFlamingo3 on MMAU-full (9000 samples), bf16 + flash_attn2, bs=20 → 72.49% accuracy (matches bs=1 baseline of 72.44%). * [AudioFlamingo3] Do not force positional embeddings to fp32 * Update src/transformers/models/audioflamingo3/modular_audioflamingo3.py Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com> * Regenerate modeling files for glmasr and musicflamingo Propagate _keep_in_fp32_modules_strict = None from audioflamingo3 to derived modular models. * Fix repo consistency check for Qwen3 ASR modular * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam --------- Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com>
…nal embedding (huggingface#47258) * [AudioFlamingo3] Fix dtype mismatch when loading with bfloat16 In AudioFlamingo3AudioEncoder.forward(), the positional embedding addition was missing a dtype cast: hidden_states = inputs_embeds + self.embed_positions.weight Since _keep_in_fp32_modules_strict keeps embed_positions in float32, this addition upcasts hidden_states to float32. The subsequent LayerNorm, whose weight is bfloat16, then raises: RuntimeError: expected scalar type Float but found BFloat16 Fix: cast the result back to inputs_embeds.dtype, matching the pattern already used in VoxtralEncoder.forward: hidden_states = (inputs_embeds + self.embed_positions.weight).to(inputs_embeds.dtype) Verified: AudioFlamingo3 on MMAU-full (9000 samples), bf16 + flash_attn2, bs=20 → 72.49% accuracy (matches bs=1 baseline of 72.44%). * [AudioFlamingo3] Do not force positional embeddings to fp32 * Update src/transformers/models/audioflamingo3/modular_audioflamingo3.py Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com> * Regenerate modeling files for glmasr and musicflamingo Propagate _keep_in_fp32_modules_strict = None from audioflamingo3 to derived modular models. * Fix repo consistency check for Qwen3 ASR modular * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam * Apply suggestion from @ebezzam --------- Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com>

What does this PR do?
Fixes a
RuntimeErrorwhen loadingAudioFlamingo3ForConditionalGenerationwithtorch_dtype=torch.bfloat16.AudioFlamingo3ForConditionalGenerationinherits_keep_in_fp32_modules_strict = ["embed_positions"]fromVoxtralForConditionalGeneration, keepingembed_positionsin float32 even when the model is loaded in bfloat16. InAudioFlamingo3AudioEncoder.forward(), the positional embedding addition was missing the dtype cast thatVoxtralEncoderalready applies:Verification
MMAU-test (9,000 samples), bfloat16 + FlashAttention-2, scored via the official sonalkum/MMAU-Eval space:
Who can review?
@eustlb @vasqu