From 7f9c0d8c201b9fba24454721cffecabd21054aa2 Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Sun, 4 Aug 2024 23:49:35 -0700 Subject: [PATCH] Update llama.py --- unsloth/models/llama.py | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 445e5026f0..cec743e59d 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -570,14 +570,7 @@ def LlamaModel_fast_forward( # Embed positions if inputs_embeds is None: inputs_embeds = self.embed_tokens(input_ids) - - if self.config.torch_dtype == "float32": - self.config.torch_dtype = torch.float32 - elif self.config.torch_dtype == "bfloat16": - self.config.torch_dtype = torch.bfloat16 - elif self.config.torch_dtype == "float16": - self.config.torch_dtype = torch.float16 - + inputs_embeds = inputs_embeds.to(self.config.torch_dtype) # Normalized from Gemma @@ -1580,6 +1573,30 @@ class FastLlamaModel: internal_model = internal_model.model pass internal_model._saved_temp_tokenizer = tokenizer + + # Also fix torch_dtype + internal_model = model + while hasattr(internal_model, "model"): + if hasattr(internal_model, "config"): + if internal_model.config.torch_dtype == "float32": + internal_model.config.torch_dtype = torch.float32 + elif internal_model.config.torch_dtype == "bfloat16": + internal_model.config.torch_dtype = torch.bfloat16 + elif internal_model.config.torch_dtype == "float16": + internal_model.config.torch_dtype = torch.float16 + pass + pass + internal_model = internal_model.model + pass + if hasattr(internal_model, "config"): + if internal_model.config.torch_dtype == "float32": + internal_model.config.torch_dtype = torch.float32 + elif internal_model.config.torch_dtype == "bfloat16": + internal_model.config.torch_dtype = torch.bfloat16 + elif internal_model.config.torch_dtype == "float16": + internal_model.config.torch_dtype = torch.float16 + pass + pass return model, tokenizer pass