| 377 | |
| 378 | |
| 379 | class RotaryEmbedding(nn.Module): |
| 380 | def __init__(self, dim, original_impl=False, device=None, dtype=None): |
| 381 | super().__init__() |
| 382 | inv_freq = 1.0 / (10000 ** (torch.arange(0, dim, 2, device=device).to(dtype=dtype) / dim)) |
| 383 | self.register_buffer("inv_freq", inv_freq) |
| 384 | self.dim = dim |
| 385 | self.original_impl = original_impl |
| 386 | |
| 387 | def forward_impl( |
| 388 | self, seq_len: int, n_elem: int, dtype: torch.dtype, device: torch.device, base: int = 10000 |
| 389 | ): |
| 390 | """Enhanced Transformer with Rotary Position Embedding. |
| 391 | |
| 392 | Derived from: https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/master/labml_nn/ |
| 393 | transformers/rope/__init__.py. MIT License: |
| 394 | https://github.com/labmlai/annotated_deep_learning_paper_implementations/blob/master/license. |
| 395 | """ |
| 396 | # $\Theta = {\theta_i = 10000^{\frac{2(i-1)}{d}}, i \in [1, 2, ..., \frac{d}{2}]}$ |
| 397 | theta = 1.0 / (base ** (torch.arange(0, n_elem, 2, dtype=torch.float, device=device) / n_elem)) |
| 398 | |
| 399 | # Create position indexes `[0, 1, ..., seq_len - 1]` |
| 400 | seq_idx = torch.arange(seq_len, dtype=torch.float, device=device) |
| 401 | |
| 402 | # Calculate the product of position index and $\theta_i$ |
| 403 | idx_theta = torch.outer(seq_idx, theta).float() |
| 404 | |
| 405 | cache = torch.stack([torch.cos(idx_theta), torch.sin(idx_theta)], dim=-1) |
| 406 | |
| 407 | # this is to mimic the behaviour of complex32, else we will get different results |
| 408 | if dtype in (torch.float16, torch.bfloat16, torch.int8): |
| 409 | cache = cache.bfloat16() if dtype == torch.bfloat16 else cache.half() |
| 410 | return cache |
| 411 | |
| 412 | def forward(self, max_seq_len, offset=0): |
| 413 | return self.forward_impl( |
| 414 | max_seq_len, self.dim, dtype=self.inv_freq.dtype, device=self.inv_freq.device |
| 415 | ) |
| 416 | |
| 417 | |
| 418 | @torch.jit.script |