Skip to content

[AudioFlamingo3] Fix bfloat16 dtype mismatch in audio encoder positional embedding - #47258

Merged
vasqu merged 10 commits into
huggingface:mainfrom
snkii:fix/audioflamingo3-embed-positions-fp32-dtype
Jul 16, 2026
Merged

[AudioFlamingo3] Fix bfloat16 dtype mismatch in audio encoder positional embedding#47258
vasqu merged 10 commits into
huggingface:mainfrom
snkii:fix/audioflamingo3-embed-positions-fp32-dtype

Conversation

@snkii

@snkii snkii commented Jul 11, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

Fixes a RuntimeError when loading AudioFlamingo3ForConditionalGeneration with torch_dtype=torch.bfloat16.

AudioFlamingo3ForConditionalGeneration inherits _keep_in_fp32_modules_strict = ["embed_positions"] from VoxtralForConditionalGeneration, keeping embed_positions in float32 even when the model is loaded in bfloat16. In AudioFlamingo3AudioEncoder.forward(), the positional embedding addition was missing the dtype cast that VoxtralEncoder already applies:

# Before (AudioFlamingo3) — missing cast
hidden_states = inputs_embeds + self.embed_positions.weight
# inputs_embeds: bfloat16, embed_positions.weight: float32
# → hidden_states upcast to float32
# → LayerNorm(float32 input, bfloat16 weight) → RuntimeError: expected scalar type Float but found BFloat16

# After — consistent with VoxtralEncoder.forward()
hidden_states = (inputs_embeds + self.embed_positions.weight).to(inputs_embeds.dtype)

Verification

MMAU-test (9,000 samples), bfloat16 + FlashAttention-2, scored via the official sonalkum/MMAU-Eval space:

accuracy
bs=5, before fix (dtype mismatch causes audio embedding contamination across samples) 57.08%
bs=5, after fix 72.40%

Who can review?

@eustlb @vasqu

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%).
@snkii
snkii force-pushed the fix/audioflamingo3-embed-positions-fp32-dtype branch from e498f7b to 6537be5 Compare July 11, 2026 06:21

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I checked the original weights in one of the models
Image

Imo, doesn't it mean that we should rather override the keep module flag instead to not have the embedding there? It should have never been casted to float.

cc @ebezzam as well

@vasqu

vasqu commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

run-slow: audioflamingo3

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/audioflamingo3"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 756eb3d5 workflow commit (merge commit)
PR 88f842f1 branch commit (from PR)
main f1a2a70f base commit (on main)

⚠️ Model CI failed to report results

The test failure analysis could not be completed. Please check the workflow run for details.

@vasqu

vasqu commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

Run slow failed but the tests passed all 🤷 waiting for the others on a final confirmation but should be good

@ebezzam ebezzam 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.

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

Comment thread src/transformers/models/audioflamingo3/modular_audioflamingo3.py Outdated
Co-authored-by: Eric Bezzam <4757445+ebezzam@users.noreply.github.com>
@ebezzam

ebezzam commented Jul 14, 2026

Copy link
Copy Markdown
Contributor

@vasqu, @snkii can we check if other models that were refactor by #45534 need this fix? E.g. musicflamingo, glmasr, and vibevoice ASR come to mind as they did modular from audioflamingo3

You'll probably have to regenerate the modeling files anyway

@vasqu

vasqu commented Jul 14, 2026

Copy link
Copy Markdown
Collaborator

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

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.

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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.
@ebezzam

ebezzam commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

@snkii can you run python utils/modular_model_converter.py and delete this line from Qwen3 ASR modular? that will resolve the failing repo consistency check

Comment thread src/transformers/models/minicpmv4_6/modeling_minicpmv4_6.py Outdated
Comment thread src/transformers/models/mask2former/image_processing_mask2former.py
Comment thread src/transformers/models/mask2former/image_processing_mask2former.py Outdated
Comment thread src/transformers/models/mask2former/image_processing_mask2former.py
@github-actions

Copy link
Copy Markdown
Contributor

[For maintainers] Suggested jobs to run (before merge)

run-slow: audioflamingo3, glmasr, musicflamingo, qwen3_asr

@ebezzam

ebezzam commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

run-slow: audioflamingo3, glmasr, musicflamingo, qwen3_asr

@github-actions

Copy link
Copy Markdown
Contributor

Workflow Run ⚙️

This comment contains run-slow, running the specified jobs:

models: ["models/audioflamingo3", "models/glmasr", "models/musicflamingo", "models/qwen3_asr"]
quantizations: []

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29349983865:2
Result: success | Jobs: 5 | Tests: 918 | Failures: 1 | Duration: 1m 27s

@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

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.

@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — automatic approval blocked

This PR was not automatically approved for CI because the security gate failed.

Possible reasons:

  • The PR touches 50 or more files — only PRs with fewer than 50 changed files are automatically approved
  • A changed file is outside the allowed directories (src/, tests/, docs/, utils/), has a disallowed extension (only .py, .txt, .md permitted outside tests/ and docs/), or is not .md/.yml inside docs/
  • A new high-severity security issue was detected in the changed Python files (Bandit check)

See the workflow run for the exact violations.

A maintainer can review and manually approve CI if a finding is a false positive.

@github-actions

Copy link
Copy Markdown
Contributor

CI Results

Workflow Run ⚙️

Commit Info

Context Commit Description
RUN 8446c858 workflow commit (merge commit)
PR b652c8a4 branch commit (from PR)
main 0f33294a base commit (on main)

✅ No failing test specific to this PR 🎉 👏 !

@vasqu
vasqu added this pull request to the merge queue Jul 16, 2026
@vasqu

vasqu commented Jul 16, 2026

Copy link
Copy Markdown
Collaborator

Thanks for the fix(es), merging now 🤗

Merged via the queue into huggingface:main with commit b932004 Jul 16, 2026
45 of 49 checks passed
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
…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>
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
…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>
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.

4 participants