Commit
·
365f542
1
Parent(s):
a2fbb2f
transformer new
Browse files- __pycache__/attn.cpython-312.pyc +0 -0
- __pycache__/attn_masks.cpython-312.pyc +0 -0
- __pycache__/attn_mods.cpython-312.pyc +0 -0
- __pycache__/configuration_minitransformer.cpython-312.pyc +0 -0
- __pycache__/layers.cpython-312.pyc +0 -0
- __pycache__/modeling_minitransformer.cpython-312.pyc +0 -0
- rotary_emb.py +99 -0
__pycache__/attn.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/attn.cpython-312.pyc and b/__pycache__/attn.cpython-312.pyc differ
|
|
|
__pycache__/attn_masks.cpython-312.pyc
ADDED
|
Binary file (8.24 kB). View file
|
|
|
__pycache__/attn_mods.cpython-312.pyc
ADDED
|
Binary file (5.77 kB). View file
|
|
|
__pycache__/configuration_minitransformer.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/configuration_minitransformer.cpython-312.pyc and b/__pycache__/configuration_minitransformer.cpython-312.pyc differ
|
|
|
__pycache__/layers.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/layers.cpython-312.pyc and b/__pycache__/layers.cpython-312.pyc differ
|
|
|
__pycache__/modeling_minitransformer.cpython-312.pyc
CHANGED
|
Binary files a/__pycache__/modeling_minitransformer.cpython-312.pyc and b/__pycache__/modeling_minitransformer.cpython-312.pyc differ
|
|
|
rotary_emb.py
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
|
| 3 |
+
def precompute_freqs_cis(head_dim: int, max_seq_len: int, theta: float = 10000.0):
|
| 4 |
+
# For half the dimensions, build the scale factor:
|
| 5 |
+
freq_seq = torch.arange(0, head_dim, 2).float() / head_dim
|
| 6 |
+
freqs = 1.0 / (theta ** freq_seq)
|
| 7 |
+
|
| 8 |
+
# Outer product with positions
|
| 9 |
+
t = torch.arange(max_seq_len, dtype=torch.float32)
|
| 10 |
+
angles = torch.outer(t, freqs)
|
| 11 |
+
|
| 12 |
+
# Build a complex exponential e^{i * theta}
|
| 13 |
+
freqs_cis = torch.polar(
|
| 14 |
+
torch.ones_like(angles),
|
| 15 |
+
angles
|
| 16 |
+
)
|
| 17 |
+
return freqs_cis
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def reshape_for_broadcast(freqs_cis: torch.Tensor, x: torch.Tensor):
|
| 21 |
+
"""
|
| 22 |
+
x is [B, n_heads, seq_len, head_dim_as_complex],
|
| 23 |
+
so we want to broadcast freqs_cis from [max_seq_len, half_dim]
|
| 24 |
+
to [1, 1, seq_len, half_dim].
|
| 25 |
+
"""
|
| 26 |
+
seq_len = x.shape[2]
|
| 27 |
+
freqs_cis = freqs_cis[:seq_len] # slice down to current seq_len
|
| 28 |
+
return freqs_cis.view(1, 1, seq_len, -1)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def apply_rotary_emb(
|
| 32 |
+
xq: torch.Tensor,
|
| 33 |
+
xk: torch.Tensor,
|
| 34 |
+
freqs_cis: torch.Tensor,
|
| 35 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 36 |
+
# Convert real -> complex by grouping last dim in pairs
|
| 37 |
+
# shape => [B, n_heads, seq_len, head_dim//2, 2] => complex => [B, n_heads, seq_len, head_dim//2]
|
| 38 |
+
xq_complex = torch.view_as_complex(xq.float().reshape(*xq.shape[:-1], -1, 2))
|
| 39 |
+
xk_complex = torch.view_as_complex(xk.float().reshape(*xk.shape[:-1], -1, 2))
|
| 40 |
+
|
| 41 |
+
# Broadcast the frequencies to match [B, n_heads, seq_len, head_dim//2]
|
| 42 |
+
freqs_cis = reshape_for_broadcast(freqs_cis, xq_complex)
|
| 43 |
+
|
| 44 |
+
# Multiply => apply rotation
|
| 45 |
+
xq_complex = xq_complex * freqs_cis
|
| 46 |
+
xk_complex = xk_complex * freqs_cis
|
| 47 |
+
|
| 48 |
+
# Convert back to real => shape [B, n_heads, seq_len, head_dim]
|
| 49 |
+
xq_out = torch.view_as_real(xq_complex).reshape(*xq.shape)
|
| 50 |
+
xk_out = torch.view_as_real(xk_complex).reshape(*xk.shape)
|
| 51 |
+
return xq_out.type_as(xq), xk_out.type_as(xk)
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
def main():
|
| 55 |
+
import math
|
| 56 |
+
from torch.testing import assert_close
|
| 57 |
+
|
| 58 |
+
# Test 1: No rotation at position 0
|
| 59 |
+
dim = 2
|
| 60 |
+
freqs_cis = precompute_freqs_cis(dim=dim, max_seq_len=1, theta=1.0)
|
| 61 |
+
xq = torch.tensor([[[[1.0, 0.0]]]])
|
| 62 |
+
xq_out, _ = apply_rotary_emb(xq, xq.clone(), freqs_cis)
|
| 63 |
+
assert_close(xq_out, xq, msg="Test 1 failed")
|
| 64 |
+
print("Test 1 passed.")
|
| 65 |
+
|
| 66 |
+
# Test 2: Verify rotation at positions [0..4] in 2D
|
| 67 |
+
L = 5
|
| 68 |
+
freqs_cis = precompute_freqs_cis(dim=dim, max_seq_len=L, theta=1.0)
|
| 69 |
+
xq = torch.tensor([[[[1.0, 0.0] for _ in range(L)]]])
|
| 70 |
+
xq_out, _ = apply_rotary_emb(xq, xq.clone(), freqs_cis)
|
| 71 |
+
expected = torch.tensor([[[[math.cos(p), math.sin(p)] for p in range(L)]]])
|
| 72 |
+
assert_close(xq_out, expected, rtol=1e-6, atol=1e-6, msg="Test 2 failed")
|
| 73 |
+
print("Test 2 passed.")
|
| 74 |
+
|
| 75 |
+
# Test 3: Higher dimension at position 0
|
| 76 |
+
xq = torch.tensor([[[[1.0, 0.0, 1.0, 0.0]]]])
|
| 77 |
+
freqs_cis = precompute_freqs_cis(dim=4, max_seq_len=1, theta=1.0)
|
| 78 |
+
xq_out, _ = apply_rotary_emb(xq, xq.clone(), freqs_cis)
|
| 79 |
+
assert_close(xq_out, xq, msg="Test 3 failed")
|
| 80 |
+
print("Test 3 passed.")
|
| 81 |
+
|
| 82 |
+
# Test 4: Random shape & norm checks
|
| 83 |
+
torch.manual_seed(1337)
|
| 84 |
+
B, H, L, D = 2, 3, 5, 8
|
| 85 |
+
xq = torch.randn(B, H, L, D)
|
| 86 |
+
xk = torch.randn(B, H, L, D)
|
| 87 |
+
freqs_cis = precompute_freqs_cis(dim=D, max_seq_len=L, theta=1.0)
|
| 88 |
+
xq_out, xk_out = apply_rotary_emb(xq, xk, freqs_cis)
|
| 89 |
+
assert xq_out.shape == (B, H, L, D), "Test 4 Q shape failed"
|
| 90 |
+
assert xk_out.shape == (B, H, L, D), "Test 4 K shape failed"
|
| 91 |
+
for b in range(B):
|
| 92 |
+
for h in range(H):
|
| 93 |
+
for l in range(L):
|
| 94 |
+
assert torch.allclose(xq[b,h,l].norm(), xq_out[b,h,l].norm(), atol=1e-5), "Test 4 Q norm failed"
|
| 95 |
+
assert torch.allclose(xk[b,h,l].norm(), xk_out[b,h,l].norm(), atol=1e-5), "Test 4 K norm failed"
|
| 96 |
+
print("Test 4 passed.\nAll tests passed successfully!")
|
| 97 |
+
|
| 98 |
+
if __name__ == "__main__":
|
| 99 |
+
main()
|