Skip to content

feat(transformers): upgrade qwen3 model to v4.57.1#1492

Open
wcrzlh wants to merge 4 commits intomindspore-lab:masterfrom
wcrzlh:qwen3_dev
Open

feat(transformers): upgrade qwen3 model to v4.57.1#1492
wcrzlh wants to merge 4 commits intomindspore-lab:masterfrom
wcrzlh:qwen3_dev

Conversation

@wcrzlh
Copy link
Copy Markdown
Contributor

@wcrzlh wcrzlh commented Jan 6, 2026

What does this PR do?

Fixes # (issue)

Adds # (feature)

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.

@xxx

@wcrzlh wcrzlh requested a review from vigo999 as a code owner January 6, 2026 08:34
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @wcrzlh, 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 upgrades the Qwen3 model to version 4.57.1, focusing on enhancing modularity, reusability, and performance within the mindone/transformers framework. The changes involve a significant refactoring of the model's architecture, moving towards generic implementations for various task-specific heads and streamlining the core attention and decoder layers. This update also broadens support for advanced attention mechanisms and improves the model's overall compatibility and maintainability.

Highlights

  • Modularization of Model Heads: Replaced specific implementations of sequence classification, token classification, and question answering heads with generic base classes, significantly reducing boilerplate and improving reusability across the model.
  • Streamlined Attention and Output Handling: Refactored the Qwen3Attention and Qwen3DecoderLayer to simplify their return values and integrate gradient checkpointing more seamlessly by inheriting from a new GradientCheckpointingLayer.
  • Enhanced Model Capabilities: Updated Qwen3PreTrainedModel to explicitly support Flash Attention, SDPA, and FlexAttention, and introduced a mechanism (_can_record_outputs) for recording intermediate hidden states and attentions.
  • Improved Input Validation and Configuration: Added the @check_model_inputs decorator for robust input validation and made the sliding_window configuration conditional based on the specific layer type, allowing for more granular control.
  • Code Clean-up: Removed redundant imports and boilerplate methods (like embedding getters/setters) that are now handled by generic base classes or updated patterns, leading to a cleaner and more maintainable codebase.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

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.

Copy link
Copy Markdown
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 refactors the Qwen3 model by introducing generic classification and question-answering layers, simplifying the Qwen3Model and Qwen3ForCausalLM constructs, and updating various internal attributes and method signatures. Specifically, Qwen3DecoderLayer now inherits from GradientCheckpointingLayer, and the Qwen3Attention.construct method's return type hint was identified as incorrect. The past_key_value parameter was consistently renamed to past_key_values across relevant methods. Additionally, the PR removed several get/set embedding methods and gradient checkpointing warning logic. A significant change in the test suite involved disabling graph mode tests, which the reviewer noted contradicts the _can_compile_fullgraph = True flag in Qwen3PreTrainedModel, indicating a potential regression or incomplete feature support that needs addressing or documentation.



class Qwen3DecoderLayer(nn.Cell):
class Qwen3DecoderLayer(GradientCheckpointingLayer):
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.

critical

This change makes Qwen3DecoderLayer inherit from GradientCheckpointingLayer. However, the GradientCheckpointingLayer in mindone/transformers/modeling_layers.py has a __call__ method that raises a NotImplementedError when gradient checkpointing is enabled. This means gradient checkpointing will not work for this model, which contradicts the supports_gradient_checkpointing = True flag in Qwen3PreTrainedModel. Please implement the gradient checkpointing logic in GradientCheckpointingLayer or revert this inheritance if it's not supported.


DTYPE_AND_THRESHOLDS = {"fp32": 5e-4, "fp16": 5e-3, "bf16": 5e-2}
MODES = [0, 1]
MODES = [1]
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.

high

Disabling the graph mode test (MODES = [0, 1] to MODES = [1]) indicates a potential regression or incomplete feature support for MindSpore's graph mode. The Qwen3PreTrainedModel class sets _can_compile_fullgraph = True, which creates a contradiction. The model should be fully functional in both pynative and graph modes. Please restore the graph mode test and fix any underlying issues. If there are known limitations, they should be documented, and the _can_compile_fullgraph flag should be set to False.

past_key_values: Optional[Cache] = None,
cache_position: Optional[ms.Tensor] = None,
**kwargs: Unpack[FlashAttentionKwargs],
) -> Tuple[Optional[ms.Tensor], Optional[Tuple[ms.Tensor]]]:
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.

medium

The return type hint -> Tuple[Optional[ms.Tensor], Optional[Tuple[ms.Tensor]]] is incorrect. The function returns a tuple of (attn_output, attn_weights), where attn_output is a ms.Tensor and attn_weights is an Optional[ms.Tensor]. The type hint should be updated to -> Tuple[ms.Tensor, Optional[ms.Tensor]] to match the implementation.

Suggested change
) -> Tuple[Optional[ms.Tensor], Optional[Tuple[ms.Tensor]]]:
) -> Tuple[ms.Tensor, Optional[ms.Tensor]]:

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.

1 participant