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
Update llama.py
Daniel Hanc571e2395e8a
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 4b64c74f3eb..051cd441c8a 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -2510,18 +2510,24 @@ class FastLlamaModel:# return# pass- internal_model = model- internal_model.gradient_checkpointing = False- internal_model.training = False-- while hasattr(internal_model, "model"):- internal_model = internal_model.model- internal_model.gradient_checkpointing = False- internal_model.training = False- pass- if hasattr(internal_model, "training"):- internal_model.training = False- pass+ m = model+ while hasattr(m, "model"):+ if hasattr(m, "gradient_checkpointing"):+ m.gradient_checkpointing = False+ if hasattr(m, "training"):+ m.training = False+ # Pad tokenizer to the left+ if hasattr(m, "_saved_temp_tokenizer"):+ m._saved_temp_tokenizer.padding_side = "left"+ m = m.model+ pass+ if hasattr(m, "gradient_checkpointing"):+ m.gradient_checkpointing = False+ if hasattr(m, "training"):+ m.training = False+ # Pad tokenizer to the left+ if hasattr(m, "_saved_temp_tokenizer"):+ m._saved_temp_tokenizer.padding_side = "left"# Also check if lm_head / embeddings are trainedinternal_model = model@@ -2530,30 +2536,13 @@ class FastLlamaModel:passlm_head = internal_model.lm_head.weightdevice_type = lm_head.device.type- dtype = model.config.torch_dtype-- if type(dtype) is str:- if dtype == "float16": dtype = torch.float16- elif dtype == "bfloat16": dtype = torch.bfloat16- pass+ dtype = _get_dtype(model.config.torch_dtype)# Wrap model.generateif model.generate.__name__ != "_fast_generate":model._unwrapped_old_generate = model.generatemodel.generate = _wrap_fast_inference(model.generate, device_type, dtype, model)pass-- # Patch tokenizer to pad to the left- internal_model = model- while hasattr(internal_model, "model"):- if hasattr(internal_model, "_saved_temp_tokenizer"):- internal_model._saved_temp_tokenizer.padding_side = "left"- pass- internal_model = internal_model.model- pass- if hasattr(internal_model, "_saved_temp_tokenizer"):- internal_model._saved_temp_tokenizer.padding_side = "left"- pass# Also disable training for embeddings for NEFTuneif hasattr(model, "get_input_embeddings"):@@ -2571,9 +2560,6 @@ class FastLlamaModel:@staticmethoddef for_training(model, use_gradient_checkpointing = True):- internal_model = model- internal_model.gradient_checkpointing = use_gradient_checkpointing- internal_model.training = True# Delete all fast inference lorasfor param in model.parameters():@@ -2581,14 +2567,24 @@ class FastLlamaModel:del param._fast_lorapass- while hasattr(internal_model, "model"):- internal_model = internal_model.model- internal_model.gradient_checkpointing = use_gradient_checkpointing- internal_model.training = True- pass- if hasattr(internal_model, "training"):- internal_model.training = True- pass+ m = model+ while hasattr(m, "model"):+ if hasattr(m, "gradient_checkpointing"):+ m.gradient_checkpointing = use_gradient_checkpointing+ if hasattr(m, "training"):+ m.training = True+ # Pad tokenizer to the right+ if hasattr(m, "_saved_temp_tokenizer"):+ m._saved_temp_tokenizer.padding_side = "right"+ m = m.model+ pass+ if hasattr(m, "gradient_checkpointing"):+ m.gradient_checkpointing = use_gradient_checkpointing+ if hasattr(m, "training"):+ m.training = True+ # Pad tokenizer to the right+ if hasattr(m, "_saved_temp_tokenizer"):+ m._saved_temp_tokenizer.padding_side = "right"# Also revert model.generateif hasattr(model, "_unwrapped_old_generate"):@@ -2596,18 +2592,6 @@ class FastLlamaModel:del model._unwrapped_old_generatepass- # Patch tokenizer to pad to the right- internal_model = model- while hasattr(internal_model, "model"):- if hasattr(internal_model, "_saved_temp_tokenizer"):- internal_model._saved_temp_tokenizer.padding_side = "right"- pass- internal_model = internal_model.model- pass- if hasattr(internal_model, "_saved_temp_tokenizer"):- internal_model._saved_temp_tokenizer.padding_side = "right"- pass-# Also re-enable training for embeddings for NEFTuneif hasattr(model, "get_input_embeddings"):embeddings = model.get_input_embeddings()@@ -2617,7 +2601,7 @@ class FastLlamaModel:embeddings = model.get_output_embeddings()if hasattr(embeddings, "training"): embeddings.training = Truepass-+return modelpasspass