Skip to content

[Gemma4] Fix dtype casting for quantized vision/audio embedders#46933

Open
sharmax-vikas wants to merge 1 commit into
huggingface:mainfrom
sharmax-vikas:fix/gemma4-quantized-dtype-casting
Open

[Gemma4] Fix dtype casting for quantized vision/audio embedders#46933
sharmax-vikas wants to merge 1 commit into
huggingface:mainfrom
sharmax-vikas:fix/gemma4-quantized-dtype-casting

Conversation

@sharmax-vikas

Copy link
Copy Markdown
Contributor

What does this PR do?

Applies the same is_floating_point guard from PR #46904 (which fixed gemma4_unified) to the gemma4 model, which has the same bug.

Bug

In Gemma4VisionPatchEmbedder.forward() and Gemma4AudioSubSampleConvProjectionLayer.forward(), the code does:

pixel_values.to(self.input_proj.weight.dtype)

When loaded with quantization (e.g. BitsAndBytesConfig(load_in_4bit=True)), weight.dtype returns torch.uint8/torch.int8. This silently casts float pixel values to integers, destroying image/audio data. The model produces garbage outputs with no error.

Fix

Add is_floating_point guard (same pattern as #46904):

if (target_dtype := self.input_proj.weight.dtype).is_floating_point:
    pixel_values = pixel_values.to(target_dtype)
hidden_states = self.input_proj(pixel_values)

Fixes #46932

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • - [x] Did you make sure to update the documentation with your changes?
  • - [ ] Did you write any new necessary tests?

@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 Dashboard: View test results in Grafana

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.

[Gemma4] Fix dtype casting for quantized vision/audio embedders (same as #46904)

1 participant