Merge pull request #3879 from ducviet00/fix-gc
Disable gradient checkpointing when explicitly off for vision
This commit is contained in:
commit
b55a2e30eb
2 changed files with 5 additions and 3 deletions
|
|
@ -264,17 +264,19 @@ def prepare_for_training_mode(f):
|
|||
def wrapper(self, *args, **kwargs):
|
||||
# Enable training mode
|
||||
_was_training = None
|
||||
# Get gradient checkpointing setting from training arguments
|
||||
use_gc = getattr(self.args, 'gradient_checkpointing', True)
|
||||
if hasattr(self, 'model') and hasattr(self.model, "training"):
|
||||
_was_training = self.model.training
|
||||
if hasattr(self, 'model') and hasattr(self.model, "for_training"):
|
||||
self.model.for_training()
|
||||
self.model.for_training(use_gradient_checkpointing=use_gc)
|
||||
output = f(self, *args, **kwargs)
|
||||
# Restore previous mode when possible
|
||||
if hasattr(self, 'model') and hasattr(self.model, "for_inference"):
|
||||
if _was_training is False:
|
||||
self.model.for_inference()
|
||||
elif _was_training is True and hasattr(self.model, "for_training"):
|
||||
self.model.for_training()
|
||||
self.model.for_training(use_gradient_checkpointing=use_gc)
|
||||
# Reset gradient checkpointing buffers to free memory while staying ready for next run
|
||||
try:
|
||||
reset_unsloth_gradient_checkpointing_buffers()
|
||||
|
|
|
|||
|
|
@ -1273,7 +1273,7 @@ class FastBaseModel:
|
|||
# Since transformers 4.53, must turn on explicitly
|
||||
for module in model.modules():
|
||||
if hasattr(module, "gradient_checkpointing"):
|
||||
module.gradient_checkpointing = True
|
||||
module.gradient_checkpointing = use_gradient_checkpointing
|
||||
|
||||
# Also re-enable training for embeddings for NEFTune
|
||||
if hasattr(model, "get_input_embeddings"):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue