Create rotary.py
Browse files
rotary.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# From https://github.com/facebookresearch/llama/blob/main/llama/model.py
|
| 2 |
+
|
| 3 |
+
import torch
|
| 4 |
+
from typing import Tuple
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
def precompute_freqs_cis(dim: int, end: int, theta: float = 10000.0):
|
| 8 |
+
"""
|
| 9 |
+
Precompute the frequency tensor for complex exponentials (cis) with given dimensions.
|
| 10 |
+
This function calculates a frequency tensor with complex exponentials using the given dimension 'dim'
|
| 11 |
+
and the end index 'end'. The 'theta' parameter scales the frequencies.
|
| 12 |
+
The returned tensor contains complex values in complex64 data type.
|
| 13 |
+
Args:
|
| 14 |
+
dim (int): Dimension of the frequency tensor.
|
| 15 |
+
end (int): End index for precomputing frequencies.
|
| 16 |
+
theta (float, optional): Scaling factor for frequency computation. Defaults to 10000.0.
|
| 17 |
+
Returns:
|
| 18 |
+
torch.Tensor: Precomputed frequency tensor with complex exponentials.
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
freqs = 1.0 / (theta ** (torch.arange(0, dim, 2)[: (dim // 2)].float() / dim))
|
| 22 |
+
t = torch.arange(end, device=freqs.device)
|
| 23 |
+
freqs = torch.outer(t, freqs).float()
|
| 24 |
+
return torch.polar(torch.ones_like(freqs), freqs)
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor):
|
| 28 |
+
assert freqs_cis.shape[1:] == (x.shape[1], x.shape[-1])
|
| 29 |
+
return freqs_cis.contiguous().unsqueeze(2)
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
def apply_rotary_emb(
|
| 33 |
+
xq: torch.Tensor,
|
| 34 |
+
xk: torch.Tensor,
|
| 35 |
+
freqs_cis: torch.Tensor,
|
| 36 |
+
) -> Tuple[torch.Tensor, torch.Tensor]:
|
| 37 |
+
"""
|
| 38 |
+
Apply rotary embeddings to input tensors using the given frequency tensor.
|
| 39 |
+
This function applies rotary embeddings to the given query 'xq' and key 'xk' tensors using the provided
|
| 40 |
+
frequency tensor 'freqs_cis'. The input tensors are reshaped as complex numbers, and the frequency tensor
|
| 41 |
+
is reshaped for broadcasting compatibility. The resulting tensors contain rotary embeddings and are
|
| 42 |
+
returned as real tensors.
|
| 43 |
+
Args:
|
| 44 |
+
xq (torch.Tensor): Query tensor to apply rotary embeddings.
|
| 45 |
+
xk (torch.Tensor): Key tensor to apply rotary embeddings.
|
| 46 |
+
freqs_cis (torch.Tensor): Precomputed frequency tensor for complex exponentials.
|
| 47 |
+
Returns:
|
| 48 |
+
Tuple[torch.Tensor, torch.Tensor]: Tuple of modified query tensor and key tensor with rotary embeddings.
|
| 49 |
+
"""
|
| 50 |
+
xq_ = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
|
| 51 |
+
xk_ = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2))
|
| 52 |
+
freqs_cis = reshape_for_broadcast(freqs_cis, xq_)
|
| 53 |
+
xq_out = torch.view_as_real(xq_ * freqs_cis).flatten(3)
|
| 54 |
+
xk_out = torch.view_as_real(xk_ * freqs_cis).flatten(3)
|
| 55 |
+
return xq_out.type_as(xq), xk_out.type_as(xk)
|