This commit is contained in:
Daniel Han-Chen 2024-02-25 02:09:08 +11:00
commit ae473e0f2b
2 changed files with 5 additions and 9 deletions

View file

@ -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

View file

@ -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