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 Hanc4720f1baf15
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.pyindex 128e0fd7628..0cfa1d04a81 100644--- a/unsloth/models/llama.py+++ b/unsloth/models/llama.py@@ -1967,29 +1967,41 @@ class FastLlamaModel:if "embed_tokens" in new_target_modules:print("Unsloth: Training embed_tokens in mixed precision to save VRAM")- dtype = model.base_model.model.embed_tokens.modules_to_save.default.weight.dtype- model.base_model.model.embed_tokens.modules_to_save.default\- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)- model.base_model.model.embed_tokens.modules_to_save.default.requires_grad_(True)+ new_dtype = model.get_input_embeddings().modules_to_save.default.weight.dtype+ if new_dtype == torch.float16:+ # See https://github.com/unslothai/unsloth/pull/1200+ # Tesla T4 must use float32 and not float16+ new_dtype = torch.float32+ pass++ model.get_input_embeddings().modules_to_save.default\+ .to(device = "cuda:0", dtype = new_dtype, non_blocking = True)+ model.get_input_embeddings().modules_to_save.default.requires_grad_(True)# [TODO] Move old embed_tokens to CPU - should be disk!- model.base_model.model.embed_tokens.original_module\+ model.get_input_embeddings().original_module\.to(device = "cpu", non_blocking = True)- model.base_model.model.embed_tokens.original_module.requires_grad_(False)+ model.get_input_embeddings().original_module.requires_grad_(False)passif "lm_head" in new_target_modules:print("Unsloth: Training lm_head in mixed precision to save VRAM")- dtype = model.base_model.model.lm_head.modules_to_save.default.weight.dtype- model.base_model.lm_head.modules_to_save.default\- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)- model.base_model.lm_head.modules_to_save.default.requires_grad_(True)+ new_dtype = model.get_output_embeddings().modules_to_save.default.weight.dtype+ if new_dtype == torch.float16:+ # See https://github.com/unslothai/unsloth/pull/1200+ # Tesla T4 must use float32 and not float16+ new_dtype = torch.float32+ pass++ model.get_output_embeddings().modules_to_save.default\+ .to(device = "cuda:0", dtype = new_dtype, non_blocking = True)+ model.get_output_embeddings().modules_to_save.default.requires_grad_(True)# [TODO] Move old lm_head to CPU - should be disk!- model.base_model.lm_head.original_module\+ model.get_output_embeddings().original_module\.to(device = "cpu", non_blocking = True)- model.base_model.lm_head.original_module.requires_grad_(False)+ model.get_output_embeddings().original_module.requires_grad_(False)passreturn model@@ -2216,25 +2228,36 @@ class FastLlamaModel:model = FastLlamaModel.patch_peft_model(model, use_gradient_checkpointing)- # Now patch lm_head and embed_tokensif train_embed_tokens:print("Unsloth: Training embed_tokens in mixed precision to save VRAM")- assert(hasattr(model.model.model.embed_tokens, "modules_to_save"))+ assert(hasattr(model.get_input_embeddings(), "modules_to_save"))- dtype = model.model.model.embed_tokens.modules_to_save.default.weight.dtype- model.model.model.embed_tokens.modules_to_save.default\- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)- model.model.model.embed_tokens.modules_to_save.default.requires_grad_(True)+ new_dtype = model.get_input_embeddings().modules_to_save.default.weight.dtype+ if new_dtype == torch.float16:+ # See https://github.com/unslothai/unsloth/pull/1200+ # Tesla T4 must use float32 and not float16+ new_dtype = torch.float32+ pass++ model.get_input_embeddings().modules_to_save.default\+ .to(device = "cuda:0", dtype = new_dtype, non_blocking = True)+ model.get_input_embeddings().modules_to_save.default.requires_grad_(True)passif train_lm_head:print("Unsloth: Training lm_head in mixed precision to save VRAM")- assert(hasattr(model.model.lm_head, "modules_to_save"))+ assert(hasattr(model.get_output_embeddings(), "modules_to_save"))++ new_dtype = model.get_output_embeddings().modules_to_save.default.weight.dtype+ if new_dtype == torch.float16:+ # See https://github.com/unslothai/unsloth/pull/1200+ # Tesla T4 must use float32 and not float16+ new_dtype = torch.float32+ pass- dtype = model.model.lm_head.modules_to_save.default.weight.dtype- model.model.lm_head.modules_to_save.default\- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)- model.model.lm_head.modules_to_save.default.requires_grad_(True)+ model.get_output_embeddings().modules_to_save.default\+ .to(device = "cuda:0", dtype = new_dtype, non_blocking = True)+ model.get_output_embeddings().modules_to_save.default.requires_grad_(True)pass# Patch tokenizer to pad to the right