Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Restore config use_cache in for_inference after gradient checkpointing prep (#6137)
Daniel Han656a0875936c
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 6018a4ccb4d..d5883e4346a 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -3467,6 +3467,14 @@ class FastLlamaModel:embeddings = model.get_output_embeddings()if hasattr(embeddings, "training"):embeddings.training = False++ # Restore use_cache values that prepare_model_for_training disabled+ # for gradient checkpointing (older unsloth_zoo has no restore helper)+ try:+ from unsloth_zoo.training_utils import restore_use_cache+ restore_use_cache(model)+ except ImportError:+ passreturn model@staticmethod@@ -3514,6 +3522,18 @@ class FastLlamaModel:embeddings = model.get_output_embeddings()if hasattr(embeddings, "training"):embeddings.training = True++ # Re-disable use_cache if prepare_model_for_training had disabled it+ # and for_inference restored it (record only exists after a disable)+ if (+ use_gradient_checkpointing+ and getattr(model, "_unsloth_use_cache_originals", None) is not None+ ):+ try:+ from unsloth_zoo.training_utils import disable_use_cache+ disable_use_cache(model)+ except ImportError:+ passreturn modeldiff --git a/unsloth/models/vision.py b/unsloth/models/vision.pyindex a75d23e7109..34336aa80f9 100644--- a/unsloth/models/vision.py+++ b/unsloth/models/vision.py@@ -1706,6 +1706,14 @@ class FastBaseModel:embeddings = model.get_output_embeddings()if hasattr(embeddings, "training"):embeddings.training = False+ # Restore use_cache values that prepare_model_for_training disabled+ # for gradient checkpointing (older unsloth_zoo has no restore helper)+ try:+ from unsloth_zoo.training_utils import restore_use_cache+ restore_use_cache(model)+ except ImportError:+ pass+# Must disable returning hidden states in the case for GRPOos.environ["UNSLOTH_RETURN_HIDDEN_STATES"] = "0"# Must enable returning logits@@ -1764,6 +1772,18 @@ class FastBaseModel:embeddings = model.get_output_embeddings()if hasattr(embeddings, "training"):embeddings.training = True+ # Re-disable use_cache if prepare_model_for_training had disabled it+ # and for_inference restored it (record only exists after a disable)+ if (+ use_gradient_checkpointing+ and getattr(model, "_unsloth_use_cache_originals", None) is not None+ ):+ try:+ from unsloth_zoo.training_utils import disable_use_cache+ disable_use_cache(model)+ except ImportError:+ pass+# Can re-enable not returning logitsos.environ["UNSLOTH_RETURN_LOGITS"] = "0"# Turn off skip guards and set stance to default