ereniko commited on
Commit
5405013
·
verified ·
1 Parent(s): 724d0e3

Delete rope.py

Browse files
Files changed (1) hide show
  1. rope.py +0 -28
rope.py DELETED
@@ -1,28 +0,0 @@
1
- import torch
2
-
3
-
4
- def precompute_rope_freqs(head_dim: int, max_seq_len: int, theta: float = 10_000.0):
5
- """Precompute the rotation angles used by RoPE (Section 4.5).
6
-
7
- Returns a complex tensor of shape (max_seq_len, head_dim // 2) where each
8
- entry encodes the rotation to apply at that position/frequency pair.
9
- """
10
- assert head_dim % 2 == 0, "RoPE requires an even head_dim"
11
- freqs = 1.0 / (theta ** (torch.arange(0, head_dim, 2).float() / head_dim))
12
- positions = torch.arange(max_seq_len).float()
13
- angles = torch.outer(positions, freqs) # (seq_len, head_dim/2)
14
- return torch.polar(torch.ones_like(angles), angles) # complex64
15
-
16
-
17
- def apply_rope(x: torch.Tensor, rope_freqs: torch.Tensor) -> torch.Tensor:
18
- """Apply rotary position embedding to a tensor of shape (B, n_heads, T, head_dim).
19
-
20
- rope_freqs should be pre-sliced to the current sequence length T before
21
- being passed in, i.e. rope_freqs[:T].
22
- """
23
- B, H, T, D = x.shape
24
- x_complex = torch.view_as_complex(x.float().reshape(B, H, T, D // 2, 2))
25
- freqs = rope_freqs.view(1, 1, T, D // 2)
26
- x_rotated = x_complex * freqs
27
- out = torch.view_as_real(x_rotated).reshape(B, H, T, D)
28
- return out.type_as(x)