|
| 1 | +import torch |
| 2 | + |
| 3 | +import lit_llama.model as lit_llama |
| 4 | + |
| 5 | + |
| 6 | +def build_rope_cache_old(seq_len: int, n_elem: int, dtype: torch.dtype, base: int = 10000) -> torch.Tensor: |
| 7 | + """This is the `build_rope_cache` implementation we initially intended to use, but it is numerically not |
| 8 | + exactly equivalent to the one in the Meta model. We keep it here for posterity. |
| 9 | +
|
| 10 | + Derived from:mers/rope/__init__.py |
| 11 | + https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/master/license MIT License: |
| 12 | + """ # noqa: E501 |
| 13 | + # $\Theta = {\theta_i = 10000^{\frac{2(i-1)}{d}}, i \in [1, 2, ..., \frac{d}{2}]}$ |
| 14 | + theta = 1.0 / (base ** (torch.arange(0, n_elem, 2, dtype=dtype) / n_elem)) |
| 15 | + |
| 16 | + # Create position indexes `[0, 1, ..., seq_len - 1]` |
| 17 | + seq_idx = torch.arange(seq_len, dtype=dtype) |
| 18 | + |
| 19 | + # Calculate the product of position index and $\theta_i$ |
| 20 | + idx_theta = torch.outer(seq_idx, theta) |
| 21 | + |
| 22 | + # Concatenate so that for row $m$ we have |
| 23 | + # $[m \theta_0, m \theta_1, ..., m \theta_{\frac{d}{2}}, m \theta_0, m \theta_1, ..., m \theta_{\frac{d}{2}}]$ |
| 24 | + idx_theta2 = torch.cat([idx_theta, idx_theta], dim=1) |
| 25 | + |
| 26 | + # Cache them |
| 27 | + cos_cache = idx_theta2.cos()[None, None, :, :] |
| 28 | + sin_cache = idx_theta2.sin()[None, None, :, :] |
| 29 | + |
| 30 | + return torch.stack((cos_cache, sin_cache), dim=0) |
| 31 | + |
| 32 | + |
| 33 | +def rotate_neg_half(x: torch.Tensor) -> torch.Tensor: |
| 34 | + # $\frac{d}{2}$ |
| 35 | + d_2 = x.shape[-1] // 2 |
| 36 | + # Calculate $[-x^{(\frac{d}{2} + 1)}, -x^{(\frac{d}{2} + 2)}, ..., -x^{(d)}, x^{(1)}, x^{(2)}, ..., x^{(\frac{d}{2})}]$ # noqa: E501 |
| 37 | + return torch.cat([-x[:, :, :, d_2:], x[:, :, :, :d_2]], dim=-1) |
| 38 | + |
| 39 | + |
| 40 | +def apply_rope_old(x: torch.Tensor, rope_cache: torch.Tensor) -> torch.Tensor: |
| 41 | + """This is the `apply_rope` implementation we initially intended to use, but it is numerically not exactly |
| 42 | + equivalent to the one in the Meta model. |
| 43 | +
|
| 44 | + We keep it here for posterity. |
| 45 | + """ |
| 46 | + neg_half_x = rotate_neg_half(x) |
| 47 | + cos, sin = rope_cache |
| 48 | + # truncate to support variable sizes |
| 49 | + T = x.size(2) |
| 50 | + cos = cos[:, :, :T] |
| 51 | + sin = sin[:, :, :T] |
| 52 | + return (x * cos) + (neg_half_x * sin) |
| 53 | + |
| 54 | + |
| 55 | +@torch.no_grad() |
| 56 | +def test_rope(orig_llama) -> None: |
| 57 | + bs, seq_len, n_head, n_embed = 1, 6, 2, 8 |
| 58 | + x = torch.randint(0, 10000, size=(bs, seq_len, n_head, n_embed // n_head)).float() |
| 59 | + |
| 60 | + freqs_cis = orig_llama.precompute_freqs_cis(n_embed // n_head, seq_len) |
| 61 | + llama_rope_cache = lit_llama.build_rope_cache(seq_len, n_embed // n_head, dtype=x.dtype) |
| 62 | + assert torch.equal(freqs_cis, llama_rope_cache) |
| 63 | + |
| 64 | + llama_x_rope = lit_llama.apply_rope(x.transpose(1, 2), llama_rope_cache).transpose(1, 2) |
| 65 | + orig_llama_x_rope, _ = orig_llama.apply_rotary_emb(x, x, freqs_cis) |
| 66 | + |
| 67 | + assert torch.equal(llama_x_rope, orig_llama_x_rope) |
| 68 | + |
| 69 | + # For posterity, we show here that our older implementation we initially wanted to use |
| 70 | + # is not numerically equivalent to Meta's rope implementation |
| 71 | + llama_rope_cache_old = build_rope_cache_old(seq_len, n_embed // n_head, dtype=x.dtype) |
| 72 | + llama_x_rope_old = apply_rope_old(x, llama_rope_cache_old) |
| 73 | + assert not torch.allclose(llama_x_rope_old, orig_llama_x_rope) |
0 commit comments