diff --git a/unsloth/models/gemma.py b/unsloth/models/gemma.py index 646aec488d..e73d0a6688 100644 --- a/unsloth/models/gemma.py +++ b/unsloth/models/gemma.py @@ -518,8 +518,8 @@ class FastGemmaModel(FastLlamaModel): # Inferene can now be CUDAGraphed, but we shall retain the old rotary embeddings. # https://github.com/huggingface/transformers/pull/27931 # https://github.com/huggingface/transformers/blob/v4.37.2/src/transformers/models/llama/modeling_llama.py - import transformers.models.gemma.modeling_gemma - transformers.models.gemma.modeling_gemma.GemmaRotaryEmbedding = LlamaRotaryEmbedding + # import transformers.models.gemma.modeling_gemma + # transformers.models.gemma.modeling_gemma.GemmaRotaryEmbedding = LlamaRotaryEmbedding return pass diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index 94fc954875..ecc09b2145 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -812,15 +812,11 @@ class LlamaRotaryEmbedding(torch.nn.Module): inv_freq = 1.0 / ( self.base ** (torch.arange(0, self.dim, 2, dtype=torch.int64, device="cpu").float() / self.dim) ) - position_ids = torch.arange(self.max_seq_len_cached, device="cpu", dtype=torch.int64).unsqueeze(-1)#.float() - inv_freq_expanded = inv_freq[None, :, None].float().expand(position_ids.shape[0], -1, 1) - position_ids_expanded = position_ids[:, None, :].float() - freqs = (inv_freq_expanded @ position_ids_expanded).transpose(1, 2) - emb = torch.cat((freqs, freqs), dim=-1) + t = torch.arange(self.max_seq_len_cached, device="cpu", dtype=torch.int64).float() - # freqs = torch.outer(t, inv_freq) + freqs = torch.outer(t, inv_freq) # Different from paper, but it uses a different permutation in order to obtain the same calculation - # emb = torch.cat((freqs, freqs), dim=-1) + emb = torch.cat((freqs, freqs), dim=-1) self.register_buffer("cos_cached", emb.cos().to(dtype=dtype, device=device, non_blocking=True), persistent=False) self.register_buffer("sin_cached", emb.sin().to(dtype=dtype, device=device, non_blocking=True), persistent=False) pass