-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(megatron): disable checkpointing when calculate KL #7828
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Summary of ChangesHello @zzc0430, 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 fixes a critical bug affecting RLHF training, particularly when using Megatron-LM, LoRA, and TransformerEngine. The issue stemmed from reference models incorrectly remaining in training mode during KL divergence calculation, leading to Highlights
🧠 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 AssistThe 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
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 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
|
There was a problem hiding this 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 effectively resolves a critical bug that caused runtime errors and incorrect KL divergence calculations during RLHF training with Megatron-LM, LoRA, and TransformerEngine. The issue stemmed from the reference model not being switched to evaluation mode. The proposed solution, which involves using a try...finally block to set the model to eval() mode and reliably restore its state, is well-implemented and correct. My review includes a minor suggestion to further improve the robustness of this state management.
| training_states = [m.training for m in ref_models] | ||
| for m in ref_models: | ||
| m.eval() | ||
| try: | ||
| yield ref_models | ||
| finally: | ||
| for m, was_training in zip(ref_models, training_states): | ||
| if was_training: | ||
| m.train() | ||
| if has_ref_adapter: | ||
| for m in self.peft_models: | ||
| m.set_adapter('default') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The use of try...finally is a good practice for ensuring resource cleanup. To enhance robustness, I recommend moving the state modification logic (setting models to eval() mode) inside the try block. This change ensures that if an exception occurs during the m.eval() call on one of the models, the finally block will still be executed, guaranteeing that the training states of all models are correctly restored. This prevents potential state corruption that could affect subsequent training steps.
| training_states = [m.training for m in ref_models] | |
| for m in ref_models: | |
| m.eval() | |
| try: | |
| yield ref_models | |
| finally: | |
| for m, was_training in zip(ref_models, training_states): | |
| if was_training: | |
| m.train() | |
| if has_ref_adapter: | |
| for m in self.peft_models: | |
| m.set_adapter('default') | |
| training_states = [m.training for m in ref_models] | |
| try: | |
| for m in ref_models: | |
| m.eval() | |
| yield ref_models | |
| finally: | |
| for m, was_training in zip(ref_models, training_states): | |
| if was_training: | |
| m.train() | |
| if has_ref_adapter: | |
| for m in self.peft_models: | |
| m.set_adapter('default') |
|
hello! Could you provide a training script that can reproduce your issue? |
2*8 A800 |
|
The error message above is a bug that will be fixed in this PR. |
PR Type
PR Information
Problem
When running RLHF (e.g., KTO, DPO) with Megatron-LM + LoRA + TransformerEngine, a
RuntimeErroroccurs during the reference model's forward pass when calculating KL divergence.Root Cause:
The
null_ref_contextreuses the training model instance (or its unwrapped version) as the reference model without switching it toeval()mode. This leads to two critical issues:train()mode, causing Gradient Checkpointing (Recompute) to stay enabled. The combination ofno_grad, Gradient Checkpointing, LoRA, and TransformerEngine triggers an internal state error in TE (Input x is not allocated).Solution
Forcibly switch the reference models to
eval()mode within thenull_ref_contextmanager and restore their original training state upon exit.eval()disables Gradient Checkpointing, resolving the TE crash.eval()disables Dropout, ensuring deterministic reference outputs.Error Log