Reproducible CPU Lab · LLM
Modern Transformer Blocks Lab
Companion Writing
From Vanilla Transformer to Llama-3 and Mixtral: Five Architectural Mutations →
Trace the five exact structural mutations—Pre-RMSNorm, RoPE, Grouped-Query Attention, SwiGLU, and Top-K Sparse MoE—that turned the 2017 Transformer into modern open-weights frontier models.
CPU reference implementations and unit tests for the five core architectural mutations from the 2017 Vanilla Transformer to Llama-3 and Mixtral:
- Pre-RMSNorm with explicit
float32variance accumulation. - Rotary Position Embeddings (RoPE) verifying exact relative-distance invariance .
- Unified MHA / GQA / MQA measuring pre-expansion KV cache footprint.
- SwiGLU Feed-Forward Network with gated element-wise projection.
- Top-K Sparse Mixture-of-Experts (MoE) decoupling total capacity from active FLOPs per token.
Run#
labs/.venv/bin/python -m unittest discover -s labs/llm/modern-transformer -p 'test_*.py' -v
labs/.venv/bin/python labs/llm/modern-transformer/modern_blocks.pySource & Verification Files
Primary implementation expanded · Click any file header to toggleImplementationmodern_blocks.py125 lines
modern_blocks.py"""CPU reference blocks for modern Decoder-only Transformers: RMSNorm, RoPE, GQA, SwiGLU, and Top-K MoE."""
import math
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch import Tensor
# region:rmsnorm_rope
class RMSNorm(nn.Module):
def __init__(self, dim: int, eps: float = 1e-6):
super().__init__()
self.eps = eps
self.weight = nn.Parameter(torch.ones(dim))
def forward(self, x: Tensor) -> Tensor:
# Accumulate mean-square in float32 to prevent BF16/FP16 overflow before scaling.
orig_dtype = x.dtype
x_fp32 = x.float()
rms = torch.rsqrt(x_fp32.pow(2).mean(dim=-1, keepdim=True) + self.eps)
return (x_fp32 * rms).to(orig_dtype) * self.weight
def precompute_rope_cos_sin(head_dim: int, max_seq_len: int, base: float = 10000.0) -> tuple[Tensor, Tensor]:
if head_dim % 2 != 0:
raise ValueError("head_dim must be even for 2D complex-plane rotation")
inv_freq = 1.0 / (base ** (torch.arange(0, head_dim, 2, dtype=torch.float32) / head_dim))
positions = torch.arange(max_seq_len, dtype=torch.float32)
angles = torch.outer(positions, inv_freq)
return torch.cos(angles), torch.sin(angles)
def apply_rope(x: Tensor, cos: Tensor, sin: Tensor) -> Tensor:
# x shape: [B, H, T, D]
seq_len = x.shape[2]
cos_t = cos[:seq_len, :][None, None, :, :]
sin_t = sin[:seq_len, :][None, None, :, :]
x_even = x[..., 0::2]
x_odd = x[..., 1::2]
rot_even = x_even * cos_t - x_odd * sin_t
rot_odd = x_even * sin_t + x_odd * cos_t
return torch.stack([rot_even, rot_odd], dim=-1).flatten(-2)
# endregion:rmsnorm_rope
# region:gqa_moe
class GroupedQueryAttention(nn.Module):
def __init__(self, hidden_size: int, num_q_heads: int, num_kv_heads: int, head_dim: int):
super().__init__()
if num_q_heads % num_kv_heads != 0:
raise ValueError("num_q_heads must be divisible by num_kv_heads")
self.num_q_heads = num_q_heads
self.num_kv_heads = num_kv_heads
self.group_size = num_q_heads // num_kv_heads
self.head_dim = head_dim
self.wq = nn.Linear(hidden_size, num_q_heads * head_dim, bias=False)
self.wk = nn.Linear(hidden_size, num_kv_heads * head_dim, bias=False)
self.wv = nn.Linear(hidden_size, num_kv_heads * head_dim, bias=False)
self.wo = nn.Linear(num_q_heads * head_dim, hidden_size, bias=False)
def forward(self, x: Tensor, cos: Tensor, sin: Tensor) -> tuple[Tensor, int]:
b, s, _ = x.shape
q = self.wq(x).view(b, s, self.num_q_heads, self.head_dim).transpose(1, 2)
k = self.wk(x).view(b, s, self.num_kv_heads, self.head_dim).transpose(1, 2)
v = self.wv(x).view(b, s, self.num_kv_heads, self.head_dim).transpose(1, 2)
q = apply_rope(q, cos, sin)
k = apply_rope(k, cos, sin)
# Only the unexpanded KV heads are stored in the persistent KV cache (2 bytes per BF16 element).
kv_cache_bytes = 2 * k.numel() * 2
if self.group_size > 1:
k = k.repeat_interleave(self.group_size, dim=1)
v = v.repeat_interleave(self.group_size, dim=1)
scores = (q @ k.transpose(-2, -1)) / math.sqrt(self.head_dim)
mask = torch.triu(torch.full((s, s), float("-inf"), device=x.device), diagonal=1)
probs = F.softmax((scores + mask).float(), dim=-1).to(x.dtype)
out = (probs @ v).transpose(1, 2).contiguous().view(b, s, -1)
return self.wo(out), kv_cache_bytes
class SwiGLUFFN(nn.Module):
def __init__(self, hidden_size: int, intermediate_size: int):
super().__init__()
self.w_gate = nn.Linear(hidden_size, intermediate_size, bias=False)
self.w_up = nn.Linear(hidden_size, intermediate_size, bias=False)
self.w_down = nn.Linear(intermediate_size, hidden_size, bias=False)
def forward(self, x: Tensor) -> Tensor:
return self.w_down(F.silu(self.w_gate(x)) * self.w_up(x))
class SparseMoEBlock(nn.Module):
def __init__(self, hidden_size: int, intermediate_size: int, num_experts: int = 8, top_k: int = 2):
super().__init__()
self.num_experts = num_experts
self.top_k = top_k
self.router = nn.Linear(hidden_size, num_experts, bias=False)
self.experts = nn.ModuleList([SwiGLUFFN(hidden_size, intermediate_size) for _ in range(num_experts)])
def forward(self, x: Tensor) -> tuple[Tensor, Tensor]:
b, s, h = x.shape
flat = x.view(-1, h)
logits = self.router(flat)
top_logits, top_indices = torch.topk(logits, self.top_k, dim=-1)
weights = F.softmax(top_logits.float(), dim=-1).to(x.dtype)
out = torch.zeros_like(flat)
for idx in range(self.num_experts):
token_idx, k_pos = torch.where(top_indices == idx)
if token_idx.numel() > 0:
expert_out = self.experts[idx](flat[token_idx])
out.index_add_(0, token_idx, expert_out * weights[token_idx, k_pos].unsqueeze(-1))
counts = torch.bincount(top_indices.view(-1), minlength=self.num_experts).float()
return out.view(b, s, h), counts / counts.sum()
# endregion:gqa_moe
if __name__ == "__main__":
torch.manual_seed(42)
cos, sin = precompute_rope_cos_sin(64, 128)
q = torch.randn(1, 1, 1, 64).expand(1, 1, 100, 64)
k = torch.randn(1, 1, 1, 64).expand(1, 1, 100, 64)
q_r, k_r = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
d1 = (q_r[0, 0, 10] * k_r[0, 0, 5]).sum().item()
d2 = (q_r[0, 0, 85] * k_r[0, 0, 80]).sum().item()
print({"rope_delta_5_error": abs(d1 - d2)})Verification Suitetest_modern_blocks.py42 lines
test_modern_blocks.pyimport unittest
import torch
from modern_blocks import (
RMSNorm,
precompute_rope_cos_sin,
apply_rope,
GroupedQueryAttention,
SparseMoEBlock,
)
class TestModernBlocks(unittest.TestCase):
def test_rope_relative_distance_invariance(self):
torch.manual_seed(7)
cos, sin = precompute_rope_cos_sin(32, 64)
q = torch.randn(1, 1, 1, 32).expand(1, 1, 50, 32)
k = torch.randn(1, 1, 1, 32).expand(1, 1, 50, 32)
qr, kr = apply_rope(q, cos, sin), apply_rope(k, cos, sin)
dot_a = (qr[0, 0, 12] * kr[0, 0, 4]).sum()
dot_b = (qr[0, 0, 41] * kr[0, 0, 33]).sum()
self.assertTrue(torch.allclose(dot_a, dot_b, atol=1e-5))
def test_gqa_cache_shrinks_with_kv_heads(self):
x = torch.randn(2, 16, 64)
cos, sin = precompute_rope_cos_sin(16, 16)
mha = GroupedQueryAttention(64, num_q_heads=4, num_kv_heads=4, head_dim=16)
gqa = GroupedQueryAttention(64, num_q_heads=4, num_kv_heads=1, head_dim=16)
_, bytes_mha = mha(x, cos, sin)
_, bytes_gqa = gqa(x, cos, sin)
self.assertEqual(bytes_mha, 4 * bytes_gqa)
def test_rmsnorm_and_moe_shapes(self):
x = torch.randn(2, 8, 32)
norm = RMSNorm(32)
moe = SparseMoEBlock(32, 64, num_experts=4, top_k=2)
y, ratio = moe(norm(x))
self.assertEqual(y.shape, x.shape)
self.assertAlmostEqual(ratio.sum().item(), 1.0, places=5)
if __name__ == "__main__":
unittest.main()