Skip to content

[misc] add megatron trainer state to align with transformer trainer#7199

Merged
hjh0119 merged 1 commit intomodelscope:mainfrom
hjh0119:mega-state
Dec 24, 2025
Merged

[misc] add megatron trainer state to align with transformer trainer#7199
hjh0119 merged 1 commit intomodelscope:mainfrom
hjh0119:mega-state

Conversation

@hjh0119
Copy link
Collaborator

@hjh0119 hjh0119 commented Dec 24, 2025

No description provided.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @hjh0119, 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 enhances the Megatron GRPO trainer by introducing a MegatronTrainerState class. This new class provides a standardized and compatible way for reward functions to access critical training progress information, such as the current global step and total training steps, mirroring the functionality found in the transformers library's TrainerState. This change streamlines the integration of reward functions and improves overall code consistency. Additionally, the PR includes minor documentation adjustments to reflect these updates and correct a detail about the clip_ratio.

Highlights

  • Standardized Trainer State: Introduced a new MegatronTrainerState class to provide a consistent interface for accessing training progress (global step, max steps) within the Megatron GRPO trainer, aligning it with the transformers.TrainerState.
  • Reward Function Integration: The GRPOTrainer now initializes and passes an instance of MegatronTrainerState to reward functions, enabling them to access global_step and max_steps during reward computation.
  • Documentation Updates: Updated developer guides in both Chinese and English to reflect the new method of accessing training steps and corrected a minor detail regarding the clip_ratio in GRPO documentation.

🧠 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
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 introduces a MegatronTrainerState class to provide a transformers.TrainerState-like interface for reward functions in Megatron GRPO training. This is a good change that improves compatibility and makes it easier for reward functions to access training progress like global_step and max_steps. The implementation is clean and well-integrated into the trainer and utility files, and the documentation has been updated accordingly. I have one minor suggestion to improve code readability.

def get_trainer_state(self):
args = get_args()
self.state.update(
global_step=getattr(args, 'curr_iteration', 0) or 0, max_steps=getattr(args, 'train_iters', 0) or 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

medium

The expression getattr(args, 'curr_iteration', 0) or 0 is functionally correct, but can be slightly confusing. A more idiomatic and readable pattern to achieve the same result (get an attribute, defaulting to 0 if it's missing or None) is getattr(args, 'curr_iteration', None) or 0. This makes the intent clearer: first attempt to get the attribute (defaulting to None if absent), then coalesce a None result to 0.

Suggested change
global_step=getattr(args, 'curr_iteration', 0) or 0, max_steps=getattr(args, 'train_iters', 0) or 0)
global_step=getattr(args, 'curr_iteration', None) or 0, max_steps=getattr(args, 'train_iters', None) or 0)

@hjh0119 hjh0119 merged commit eb5c769 into modelscope:main Dec 24, 2025
1 of 3 checks passed
@hjh0119 hjh0119 deleted the mega-state branch December 24, 2025 10:05
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.

2 participants