Skip to content

Conversation

wtomin
Copy link
Collaborator

@wtomin wtomin commented Oct 10, 2025

What does this PR do?

Fixes # (issue)

  1. While running fast ut of ShieldGemma2, the Tensor.masked_scatter operator has an error under BF16 data type:
TyperError: For primitivce[MaskedScatter], the input argument[updates] must be a type of {Tensor[Bool], Tensor[Float16], Tensor[Float32], Tensor[Float64], Tensor[Int16], Tensor[Int32]...

Fixed this error by setting the inputs_embeds and image_features to float32 tensors.

  1. Enlarged the bf16 threshold of AltClip, UDOP, Jetmoe, and GroundingDino. refer to: 🐛 Inappropriate Test Tolerance Threshold for BF16 Precision Validation​ #1357

Before submitting

  • This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
  • Did you read the contributor guideline?
  • Did you make sure to update the documentation with your changes? E.g. record bug fixes or new features in What's New. Here are the
    documentation guidelines
  • Did you build and run the code without any errors?
  • Did you report the running environment (NPU type/MS version) and performance in the doc? (better record it for data loading, model inference, or training tasks)
  • Did you write any new necessary tests?

Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@SamitHuang @vigo999

@wtomin wtomin requested a review from vigo999 as a code owner October 10, 2025 10:40
Copy link
Contributor

Summary of Changes

Hello @wtomin, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical data type incompatibility issue within the masked_scatter operation when the model is configured to use BF16 precision in MindSpore version 2.6.0. By introducing explicit type casting for the input tensors before the operation, the change ensures the model can execute without encountering TypeError exceptions, thereby enhancing the stability and usability of the Gemma3 model under BF16 mixed-precision training for users on the specified MindSpore version.

Highlights

  • BF16 masked_scatter error fix: Addresses a TypeError in the Tensor.masked_scatter operator when using BF16 data type in MindSpore 2.6.0, which previously caused runtime failures.
  • Data type casting workaround: Implements a temporary workaround by explicitly casting inputs_embeds and image_features to float32 before performing the masked_scatter operation, and then casting the result back to the original data type. This is noted as a fix for MindSpore 2.6.0, with the issue being resolved in MindSpore 2.7.0.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@wtomin wtomin added the bug Something isn't working label Oct 10, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request addresses a TypeError with masked_scatter when using the bfloat16 data type by casting the tensors to float32 before the operation. The fix is correct and includes a helpful FIXME comment explaining the workaround. However, the change is applied to mindone/transformers/models/gemma3/modeling_gemma3.py, which is an auto-generated file. As my review comment points out, this change should be made in the source file src/transformers/models/gemma3/modular_gemma3.py to prevent it from being overwritten.

Comment on lines +1298 to +1300
inputs_embeds = (
inputs_embeds.float().masked_scatter(special_image_mask, image_features.float()).to(inputs_embeds.dtype)
) # FIXME: ms 2.6.0 does not support masked_scatter under bf16. Problem was fixed in ms 2.7.0
Copy link
Contributor

Choose a reason for hiding this comment

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

critical

This file is auto-generated from src/transformers/models/gemma3/modular_gemma3.py. As stated in the file header, direct modifications to this file will be overwritten. Please apply your changes to the source file src/transformers/models/gemma3/modular_gemma3.py instead.

@wtomin wtomin changed the title fix(transformers): ShieldGemma2 masked_scatter data type error under bf16 fix(transformers): fix fast UT errors of ShieldGemma2, AltClip, GroundingDino Oct 10, 2025
@wtomin wtomin changed the title fix(transformers): fix fast UT errors of ShieldGemma2, AltClip, GroundingDino fix(transformers): fix fast UT errors of ShieldGemma2, AltClip, GroundingDino, UDOP, Jetmoe Oct 13, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants