Skip to content

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.

Update llama.py

Daniel Han0f6b518ee1a5
diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py
index c94514966f9..d3b51b68354 100644
--- a/unsloth/models/llama.py
+++ b/unsloth/models/llama.py
@@ -1968,8 +1968,18 @@ class FastLlamaModel:
print("Unsloth: Training embed_tokens in mixed precision to save VRAM")
dtype = model.model.model.embed_tokens.modules_to_save.default.weight.dtype
+ # Now patch lm_head and embed_tokens
+ if dtype == torch.float16:
+ # See https://github.com/unslothai/unsloth/pull/1200
+ # Tesla T4 must use float32 and not float16
+ modules_to_save_dtype = torch.float32
+ else:
+ # Can be bfloat16
+ modules_to_save_dtype = dtype
+ pass
+
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)
+ .to(device = "cuda:0", dtype = modules_to_save_dtype, non_blocking = True)
model.model.model.embed_tokens.modules_to_save.default.requires_grad_(True)
# [TODO] Move old embed_tokens to CPU - should be disk!
@@ -1982,8 +1992,17 @@ class FastLlamaModel:
print("Unsloth: Training lm_head in mixed precision to save VRAM")
dtype = model.model.model.lm_head.modules_to_save.default.weight.dtype
+ # Now patch lm_head and embed_tokens
+ if dtype == torch.float16:
+ # See https://github.com/unslothai/unsloth/pull/1200
+ # Tesla T4 must use float32 and not float16
+ modules_to_save_dtype = torch.float32
+ else:
+ # Can be bfloat16
+ modules_to_save_dtype = dtype
+ pass
model.model.lm_head.modules_to_save.default\
- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)
+ .to(device = "cuda:0", dtype = modules_to_save_dtype, non_blocking = True)
model.model.lm_head.modules_to_save.default.requires_grad_(True)
# [TODO] Move old lm_head to CPU - should be disk!
@@ -2216,14 +2235,23 @@ class FastLlamaModel:
model = FastLlamaModel.patch_peft_model(model, use_gradient_checkpointing)
- # Now patch lm_head and embed_tokens
if train_embed_tokens:
print("Unsloth: Training embed_tokens in mixed precision to save VRAM")
assert(hasattr(model.model.model.embed_tokens, "modules_to_save"))
dtype = model.model.model.embed_tokens.modules_to_save.default.weight.dtype
+ # Now patch lm_head and embed_tokens
+ if dtype == torch.float16:
+ # See https://github.com/unslothai/unsloth/pull/1200
+ # Tesla T4 must use float32 and not float16
+ modules_to_save_dtype = torch.float32
+ else:
+ # Can be bfloat16
+ modules_to_save_dtype = dtype
+ pass
+
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)
+ .to(device = "cuda:0", dtype = modules_to_save_dtype, non_blocking = True)
model.model.model.embed_tokens.modules_to_save.default.requires_grad_(True)
pass
@@ -2232,8 +2260,18 @@ class FastLlamaModel:
assert(hasattr(model.model.lm_head, "modules_to_save"))
dtype = model.model.lm_head.modules_to_save.default.weight.dtype
+ # Now patch lm_head and embed_tokens
+ if dtype == torch.float16:
+ # See https://github.com/unslothai/unsloth/pull/1200
+ # Tesla T4 must use float32 and not float16
+ modules_to_save_dtype = torch.float32
+ else:
+ # Can be bfloat16
+ modules_to_save_dtype = dtype
+ pass
+
model.model.lm_head.modules_to_save.default\
- .to(device = "cuda:0", dtype=(dtype if (dtype != torch.float16) else torch.float32), non_blocking = True)
+ .to(device = "cuda:0", dtype = modules_to_save_dtype, non_blocking = True)
model.model.lm_head.modules_to_save.default.requires_grad_(True)
pass