Skip to content

Fix Gemma4 audio feature dtype mismatch in masked_scatter - #47482

Merged
Rocketknight1 merged 1 commit into
huggingface:mainfrom
danielhanchen:gemma4-audio-merge-dtype
Jul 23, 2026
Merged

Fix Gemma4 audio feature dtype mismatch in masked_scatter#47482
Rocketknight1 merged 1 commit into
huggingface:mainfrom
danielhanchen:gemma4-audio-merge-dtype

Conversation

@danielhanchen

@danielhanchen danielhanchen commented Jul 22, 2026

Copy link
Copy Markdown
Contributor

CI

What does this PR do?

Aligns the dtype of Gemma 4 audio features to the text-embedding dtype before masked_scatter, so the audio merge behaves like the image and video merges.

In Gemma4Model.forward, the image and video branches cast their features to inputs_embeds.dtype before scattering them into the text embeddings:

image_features = image_features.to(inputs_embeds.device, inputs_embeds.dtype)
...
video_features = video_features.to(inputs_embeds.device, inputs_embeds.dtype)

but the audio branch only moved the features to the correct device, never the dtype:

inputs_embeds = inputs_embeds.masked_scatter(
    audio_mask.to(inputs_embeds.device), audio_features.to(inputs_embeds.device)  # dtype not aligned
)

When inputs_embeds and the audio encoder output end up in different dtypes (for example a float32 residual stream with a float16 audio tower, which happens under mixed-precision setups), this raises:

RuntimeError: masked_scatter_: expected self and source to have same dtypes but got Float and Half

The fix casts audio_features to inputs_embeds.dtype in the same call, matching the image and video paths. Text-only, image, and video inference are unaffected since they already cast. The change is made in modular_gemma4.py and mirrored in the generated modeling_gemma4.py.

Before submitting

  • This PR fixes a bug (audio inference crash under mixed precision).
  • Did you make sure to update the documentation with your changes? Not needed.
  • Did you write any new necessary tests? Not needed; this aligns audio with the existing image/video dtype handling.

Who can review?

@ArthurZucker @Cyrilvallez

@github-actions

Copy link
Copy Markdown
Contributor

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

run-slow: gemma4

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 29929449179
Result: success | Grafana metrics are not available yet.

@Rocketknight1 Rocketknight1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yes, LGTM!

@Rocketknight1
Rocketknight1 enabled auto-merge July 23, 2026 11:28
@Rocketknight1
Rocketknight1 added this pull request to the merge queue Jul 23, 2026
@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.

Merged via the queue into huggingface:main with commit bc14d7e Jul 23, 2026
37 checks passed
stevhliu pushed a commit to stevhliu/transformers that referenced this pull request Jul 30, 2026
Sainava pushed a commit to Sainava/Sai-transformers that referenced this pull request Aug 3, 2026
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