Repository navigation
Disable gradient checkpointing when explicitly off for vision - #3879
Conversation
…nt_checkpointing=False`
Summary of ChangesHello @ducviet00, 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 implements a comprehensive solution to fully control and disable gradient checkpointing for vision models within the framework. By integrating the 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 correctly implements the ability to disable gradient checkpointing by propagating the gradient_checkpointing setting from training arguments down to the model's modules. The changes in unsloth/models/rl.py and unsloth/models/vision.py work together to achieve this. I've added one comment in unsloth/models/vision.py regarding a minor code redundancy that could be refactored for better clarity and maintainability. Overall, the changes are logical and address the intended goal.
| for module in model.modules(): | ||
| if hasattr(module, "gradient_checkpointing"): | ||
| module.gradient_checkpointing = True | ||
| module.gradient_checkpointing = use_gradient_checkpointing |
There was a problem hiding this comment.
While this change is correct and necessary to propagate the use_gradient_checkpointing setting, there appears to be a redundancy in how gradient_checkpointing is being set.
The _for_training function (defined at line 1250) also sets m.gradient_checkpointing = use_gradient_checkpointing inside a while loop that iterates through the model hierarchy (lines 1266-1270). This for loop here does the same, but for all modules recursively.
To improve code clarity and avoid setting the attribute twice on some modules, consider consolidating this logic. Relying only on this for loop would be sufficient and clearer, as model.modules() is more comprehensive. You could then remove the gradient_checkpointing logic from the _for_training helper function.
gradient_checkpointing for vision
Datta0
left a comment
There was a problem hiding this comment.
Yeah this does make sense to me
|
It may be worth mentioning that this patch in |
Disable gradient checkpointing when explicitly off for vision
When training Qwen3-VL with
gradient_checkpointing=False, the process fails with an AttributeError. The error originates fromQwen3VLVisionBlockexpecting a_gradient_checkpointing_funcattribute that is not present when checkpointing is disabled.The issue appears to be located in
unsloth_compiled_cache/UnslothSFTTrainer.py, generated byrl.py.While
FastVisionModel.for_trainingis called, it re-enable/configure gradient checkpointing do not properly invision.py:Version:
Training Script
```python USE_GRAD_CHECKPOINTING = False model, tokenizer = FastVisionModel.from_pretrained( MODEL_PATH, max_seq_length=MAX_LEN, load_in_4bit=False, load_in_8bit=False, load_in_16bit=True, use_gradient_checkpointing=USE_GRAD_CHECKPOINTING, device_map=device_map, ) ... model = FastVisionModel.get_peft_model( model, finetune_vision_layers=False, finetune_language_layers=True, finetune_attention_modules=True, finetune_mlp_modules=True, use_gradient_checkpointing=USE_GRAD_CHECKPOINTING, r=LORA_RANK, lora_alpha=LORA_RANK, lora_dropout=0, bias="none", random_state=3407, use_rslora=False, loftq_config=None, max_seq_length=MAX_LEN, )