fix: return gradients on original device for multi-GPU training#423
fix: return gradients on original device for multi-GPU training#423Vinayyyy7 wants to merge 1 commit intounslothai:mainfrom
Conversation
Summary of ChangesHello @Vinayyyy7, 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 a critical gradient device mismatch error encountered during multi-GPU training with 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.
Code Review
This pull request effectively resolves a critical multi-GPU training issue by ensuring that gradients are returned to their original device. The changes correctly identify the need to synchronize tensor devices during the forward pass and, crucially, to restore the grad_inputs to their initial device during the backward pass. This prevents RuntimeError due to device mismatches in scenarios involving device_map="auto/balanced". The implementation is clear, well-commented, and directly addresses the problem described.
|
File "/home/pitai/.conda/envs/ft-io/lib/python3.11/site-packages/unsloth/models/rl_replacements.py", line 33, in hi, can you help me? i got this error when impoty UnslothVisionDataCollator |
|
I've updated it with all changes that were implemented in 2 months by unsloth officially so should work now try: pip install git+https://github.com/Vinayyyy7/unsloth-zoo.git@fix/multi-gpu-gradient-device-mismatch
pip install git+https://github.com/Vinayyyy7/unsloth.git@fix/multi-gpu-vision-model-trainingOR simply clone both repos do build the .whl files and install it that way |
97490bf to
d9c1b3e
Compare
This PR fixes a gradient device mismatch error in UnslothFusedLoss that occurs during multi-GPU training.
when the model is split across GPUs via
device_map="auto/balanced", hidden_states might be oncuda:1while lm_head_weight is oncuda:0 as example. the forward pass moveshidden_states` to the lm_head device for computation, but backward pass was returning gradients on that same device instead of the original hidden_states device. This caused PyTorch to raise "RuntimeError: expected device cuda:1 but got cuda:0".this fix tracks the original device of hidden_states before any tensor movement, saves it to the autograd context, and in
backward()moves grad_inputs back to the original device before returning.Note: This PR works together with a corresponding fix in
unsloththat adds distributed training detection for vision models. you can see it for more info as I have made a PR both PRs this and that are essential for this fix.unslothai/unsloth#3809
Tested on Kaggle with 2x T4 GPUs using Qwen2.5-VL-2B.