diff --git a/unsloth/models/llama.py b/unsloth/models/llama.py index ecc09b2145..4e46023a80 100644 --- a/unsloth/models/llama.py +++ b/unsloth/models/llama.py @@ -812,11 +812,15 @@ 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) ) - t = torch.arange(self.max_seq_len_cached, device="cpu", dtype=torch.int64).float() - - freqs = torch.outer(t, inv_freq) - # Different from paper, but it uses a different permutation in order to obtain the same calculation + position_ids = torch.arange(self.max_seq_len_cached, device="cpu", dtype=torch.int64)#.float() + inv_freq_expanded = self.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) + + # 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) 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