[bugfix] Fix abnormal grad_norm under GRPO LoRA + DeepSpeed ZeRO-0 (fix #6815)#8341
[bugfix] Fix abnormal grad_norm under GRPO LoRA + DeepSpeed ZeRO-0 (fix #6815)#8341alphadl wants to merge 1 commit intomodelscope:mainfrom
Conversation
…odelscope#6815) - Add _get_reduced_grad_norm_for_logging to reduce grad_norm across processes when not ZeRO-3 and world_size > 1 (AVG) - Use it in _maybe_log_save_evaluate for logs['grad_norm'] - Add tests/train/test_grad_norm_reduce.py
Summary of ChangesHello, 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 addresses an issue where 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. Changelog
Activity
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
|
|
Please take a look when you have time. This fixes abnormal |
There was a problem hiding this comment.
Code Review
This pull request fixes an issue with abnormally large grad_norm logging when using DeepSpeed ZeRO-0 by introducing a reduction step. The changes look solid, with a new utility function _get_reduced_grad_norm_for_logging to average the gradient norm across processes and comprehensive unit tests to cover various scenarios. My only suggestion is to improve the exception handling in the new function to log any errors during reduction, which will help in debugging potential issues instead of silently falling back to the old behavior.
| except Exception: | ||
| return grad_norm.item() |
There was a problem hiding this comment.
The broad except Exception: without logging can hide issues during gradient norm reduction. If an error occurs, it will silently fall back to using the un-reduced gradient norm, which could be misleading for monitoring. It's better to log the exception to make debugging easier.
| except Exception: | |
| return grad_norm.item() | |
| except Exception as e: | |
| logger.warning(f'Failed to reduce grad_norm for logging: {e}. Returning un-reduced value.') | |
| return grad_norm.item() |
#6815
Under DeepSpeed ZeRO-0 (or plain DDP), the logged
grad_normwas abnormally large (e.g. ~1656) compared to ZeRO-3 (e.g. ~0.025) with the same loss. This made monitoring and debugging difficult and was reported by multiple users.Root cause: With ZeRO-0/DDP, each rank may report a different gradient norm (e.g. local view before or after reduce). ZeRO-3 reports a global norm. The trainer was logging the per-rank value without reduction, so the logged
grad_normwas inconsistent across ZeRO stages.Changes:
swift/trainers/mixin.py:_get_reduced_grad_norm_for_logging(grad_norm)to reducegrad_normacross processes when not ZeRO-3 andworld_size > 1(all-reduce withReduceOp.AVG), so that the logged value is consistent and comparable across ZeRO stages._maybe_log_save_evaluate, use_get_reduced_grad_norm_for_logging(args[0])when buildinglogs['grad_norm'](transformers >= 4.38).tests/train/test_grad_norm_reduce.py: Add unit tests for_get_reduced_grad_norm_for_logging(None/float/tensor, single-process, ZeRO-3 no reduce, ZeRO-0 with all_reduce).Behavior:
grad_normis all-reduced (average) before logging, so logs show a single consistent value.