File size: 12,259 Bytes
a2a2112 47ed852 a2a2112 47ed852 a2a2112 a5fce59 a2a2112 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | """Fractus-1B: a 1B-capacity model trainable on CPU.
Architecture:
- BPE embedding (vocab 50257, d_model 1024)
- 12 × FractalBlockSparse (attention + Kuramoto + StructuredSiren MoE-64)
- LayerNorm + LM head
The key innovation: StructuredSirenLinear experts give ~1B of effective
matrix capacity from ~20M trainable parameters. Combined with top-2 sparse
routing, each token only computes 2/64 experts.
"""
import math
import torch
import torch.nn as nn
from .nn.attention import FractalLinearAttention
from .nn.phase_ode import KuramotoLayer
from .nn.stats import elu_plus_one, stable_softmax
from .nn.farey import expert_phases
from .nn.structured_siren import StructuredSirenLinear
from .nn.cached_siren import CachedStructuredSirenLinear
from .nn.lazy_siren import LazyStructuredSirenLinear
class BPEEmbedding(nn.Module):
"""Embedding for BPE tokens: table + position + Mandelbrot Fourier boost."""
def __init__(self, vocab_size: int, d_model: int, max_seq_len: int = 512):
super().__init__()
self.tok_embed = nn.Embedding(vocab_size, d_model)
self.pos_embed = nn.Embedding(max_seq_len, d_model)
self.norm = nn.LayerNorm(d_model)
# Init.
nn.init.normal_(self.tok_embed.weight, mean=0.0, std=0.02)
nn.init.normal_(self.pos_embed.weight, mean=0.0, std=0.02)
def forward(self, ids: torch.Tensor) -> torch.Tensor:
B, L = ids.shape
pos = torch.arange(L, device=ids.device).unsqueeze(0).expand(B, L)
x = self.tok_embed(ids) + self.pos_embed(pos)
return self.norm(x)
class SparseStructuredMoE(nn.Module):
"""64-expert sparse MoE using StructuredSirenLinear experts.
Only top_k=2 experts are computed per token (gather-first sparse dispatch).
Each expert is a 2-layer MLP with StructuredSirenLinear weight matrices,
giving high capacity from low param count.
"""
def __init__(
self,
d_model: int,
n_experts: int = 64,
top_k: int = 2,
d_ff: int = 1024,
siren_rank: int = 64,
kappa: float = 4.0,
):
super().__init__()
self.d_model = d_model
self.n_experts = n_experts
self.top_k = top_k
self.d_ff = d_ff
# Expert phases (Farey precomputation).
phases = expert_phases(n_experts)
self.register_buffer("expert_phases", torch.tensor(phases, dtype=torch.float32))
self.kappa = kappa
# Experts: each is w1 (d_model→d_ff) + w2 (d_ff→d_model) via LazyStructuredSiren.
# Lazy = LoRA-style low-rank, NO grid memory → fits 64 experts in RAM.
self.experts_w1 = nn.ModuleList([
LazyStructuredSirenLinear(d_model, d_ff, rank=siren_rank)
for _ in range(n_experts)
])
self.experts_w2 = nn.ModuleList([
LazyStructuredSirenLinear(d_ff, d_model, rank=siren_rank)
for _ in range(n_experts)
])
def _compute_gates(self, phases: torch.Tensor) -> torch.Tensor:
sin_p = torch.sin(phases).sum(dim=-1)
cos_p = torch.cos(phases).sum(dim=-1)
theta_bar = torch.atan2(sin_p, cos_p)
diff = theta_bar.unsqueeze(-1) - self.expert_phases.view(
*[1] * (phases.dim() - 1), self.n_experts
)
gates = torch.exp(self.kappa * torch.cos(diff))
gates_sum = gates.sum(dim=-1, keepdim=True)
uniform = torch.full_like(gates, 1.0 / self.n_experts)
return torch.where(gates_sum > 1e-10, gates / gates_sum, uniform)
def forward(self, h: torch.Tensor, phases: torch.Tensor):
"""h: (B, L, d_model), phases: (B, L, n_phases).
Returns (output, load_balance_loss).
VECTORIZED SPARSE MoE — preserves 64 experts + LazyStructuredSiren
(low-rank W = scale·U·Vᵀ) but kills the Python double-loop that
launched 128 separate expert calls (~1955 kernels/block, GPU at 9%).
Strategy: gather the low-rank FACTORS of the top_k selected experts
per token, flatten (B,L,K) into one batch of N·K "slot forwards", and
run the LazySiren two-matmul (x@V)@(Uᵀ) as a single grouped bmm.
No expert weight matrix is ever materialized — memory stays O(rank).
Mathematically identical to the loop version (verified ≤ 1e-8).
"""
B, L, D = h.shape
K = self.top_k
gates = self._compute_gates(phases)
topk_vals, topk_idx = gates.topk(K, dim=-1) # (B,L,K)
topk_sum = topk_vals.sum(dim=-1, keepdim=True)
topk_norm = torch.where(
topk_sum > 1e-10, topk_vals / topk_sum,
torch.full_like(topk_vals, 1.0 / K),
)
N = B * L
flat_idx = topk_idx.reshape(-1) # (N*K,) expert id per (token, slot)
# Stack expert low-rank factors (E, ...) then gather the K selected.
w1_V = torch.stack([e.V for e in self.experts_w1]) # (E, D, R)
w1_U = torch.stack([e.U for e in self.experts_w1]) # (E, F, R)
w1_s = torch.stack([e.scale for e in self.experts_w1]) # (E,)
w1_b = torch.stack([e.bias for e in self.experts_w1]) # (E, F)
w2_V = torch.stack([e.V for e in self.experts_w2]) # (E, F, R)
w2_U = torch.stack([e.U for e in self.experts_w2]) # (E, D, R)
w2_s = torch.stack([e.scale for e in self.experts_w2]) # (E,)
w2_b = torch.stack([e.bias for e in self.experts_w2]) # (E, D)
g1V = w1_V.index_select(0, flat_idx) # (N*K, D, R)
g1U = w1_U.index_select(0, flat_idx) # (N*K, F, R)
g1s = w1_s.index_select(0, flat_idx) # (N*K,)
g1b = w1_b.index_select(0, flat_idx) # (N*K, F)
g2V = w2_V.index_select(0, flat_idx) # (N*K, F, R)
g2U = w2_U.index_select(0, flat_idx) # (N*K, D, R)
g2s = w2_s.index_select(0, flat_idx) # (N*K,)
g2b = w2_b.index_select(0, flat_idx) # (N*K, D)
# Each token repeated K times: (N, D) → (N*K, D).
h_rep = h.reshape(N, D).unsqueeze(1).expand(N, K, D).reshape(N * K, D)
# LazySiren w1: y = scale * (x @ V) @ U^T + bias — one grouped bmm.
proj1 = torch.bmm(h_rep.unsqueeze(1), g1V).squeeze(1) # (N*K, R)
h1 = torch.bmm(proj1.unsqueeze(1), g1U.transpose(1, 2)).squeeze(1) # (N*K, F)
h1 = g1s.unsqueeze(-1) * h1 + g1b # scale + bias
h1_act = torch.nn.functional.gelu(h1) # (N*K, F)
# LazySiren w2.
proj2 = torch.bmm(h1_act.unsqueeze(1), g2V).squeeze(1) # (N*K, R)
out_nk = torch.bmm(proj2.unsqueeze(1), g2U.transpose(1, 2)).squeeze(1) # (N*K, D)
out_nk = g2s.unsqueeze(-1) * out_nk + g2b # (N*K, D)
# Reshape and weight-sum over the K experts.
out_k = out_nk.reshape(B, L, K, D)
output = (topk_norm.unsqueeze(-1) * out_k).sum(dim=2) # (B, L, D)
# Load-balance loss.
P = gates.mean(dim=(0, 1))
lb_loss = self.n_experts * ((P - 1.0 / self.n_experts) ** 2).sum()
return output, lb_loss
class FractalBlockSparse(nn.Module):
"""One transformer block: attention + Kuramoto + sparse MoE."""
def __init__(
self,
d_model: int,
n_heads: int,
d_head: int,
n_levels: int,
n_experts: int = 64,
top_k: int = 2,
expert_d_ff: int = 1024,
siren_rank: int = 64,
):
super().__init__()
self.norm1 = nn.LayerNorm(d_model)
self.attn = FractalLinearAttention(d_model, n_heads, d_head, n_levels)
self.norm_kur = nn.LayerNorm(d_model)
# n_steps=1 for training (4 was overkill — phases converge in 1 step,
# matches the CTE which uses n_steps=1). Measured ~2x faster on Kuramoto.
self.kuramoto = KuramotoLayer(d_model, n_oscillators=16, rank=8,
n_steps=1, dt=0.1)
self.norm_moe = nn.LayerNorm(d_model)
self.moe = SparseStructuredMoE(
d_model, n_experts=n_experts, top_k=top_k,
d_ff=expert_d_ff, siren_rank=siren_rank,
)
def forward(self, x: torch.Tensor):
# Attention.
x = x + self.attn(self.norm1(x))
# Kuramoto phases.
phases = self.kuramoto(self.norm_kur(x))
# Sparse MoE.
moe_out, lb_loss = self.moe(self.norm_moe(x), phases)
x = x + moe_out
return x, lb_loss
class Fractus1B(nn.Module):
"""Fractus-1B: 1B-capacity, ~20M trainable params, CPU-trainable.
Config (default):
vocab=50257, d_model=1024, n_layers=12, n_heads=16, d_head=64,
n_levels=4, n_experts=64, top_k=2, expert_d_ff=1024, siren_rank=64.
"""
def __init__(
self,
vocab_size: int = 50257,
d_model: int = 1024,
n_layers: int = 12,
n_heads: int = 16,
d_head: int = 64,
n_levels: int = 4,
n_experts: int = 64,
top_k: int = 2,
expert_d_ff: int = 1024,
siren_rank: int = 64,
max_seq_len: int = 512,
):
super().__init__()
self.vocab_size = vocab_size
self.d_model = d_model
self.config = {
"vocab_size": vocab_size, "d_model": d_model, "n_layers": n_layers,
"n_heads": n_heads, "d_head": d_head, "n_levels": n_levels,
"n_experts": n_experts, "top_k": top_k, "expert_d_ff": expert_d_ff,
"siren_rank": siren_rank, "max_seq_len": max_seq_len,
}
self.embed = BPEEmbedding(vocab_size, d_model, max_seq_len)
self.blocks = nn.ModuleList([
FractalBlockSparse(
d_model, n_heads, d_head, n_levels,
n_experts=n_experts, top_k=top_k,
expert_d_ff=expert_d_ff, siren_rank=siren_rank,
)
for _ in range(n_layers)
])
self.norm = nn.LayerNorm(d_model)
# Tied with embedding (saves params).
self.lm_head = nn.Linear(d_model, vocab_size, bias=False)
self.lm_head.weight = self.embed.tok_embed.weight
def forward(self, ids: torch.Tensor):
"""ids: (B, L) → (logits (B, L, vocab), aux_loss scalar).
L9 GRADIENT CHECKPOINTING: each block is checkpointed so the autograd
graph is NOT retained between layers. During backward, the forward is
recomputed per-block. This reduces peak memory from O(n_layers ×
activation_size) to O(activation_size), making the 1B model trainable
on CPU without OOM.
"""
from torch.utils.checkpoint import checkpoint
x = self.embed(ids)
aux_loss = torch.tensor(0.0, device=x.device)
for block in self.blocks:
# No gradient checkpointing — the model fits in RAM with LazySiren
# and removing checkpointing gives 3-5x speedup (no recompute).
x_new, lb = block(x)
x = x_new
aux_loss = aux_loss + lb
x = self.norm(x)
if getattr(self, "_return_hidden", False):
# Chunked-CE mode: skip lm_head here, caller computes it per chunk.
return x, aux_loss
logits = self.lm_head(x)
return logits, aux_loss
def n_params(self) -> int:
"""Actual trainable parameter count."""
return sum(p.numel() for p in self.parameters() if p.requires_grad)
def n_effective_capacity(self) -> int:
"""Approximate effective matrix capacity (what a dense model would have)."""
# Attention QKV+out per layer: 4 * d_model^2
attn = 4 * self.d_model ** 2
# Each expert: d_model*d_ff + d_ff*d_model (dense-equivalent)
moe_per_layer = self.config["n_experts"] * 2 * self.d_model * self.config["expert_d_ff"]
# Embedding + head.
emb = self.vocab_size * self.d_model
total = self.config["n_layers"] * (attn + moe_per_layer) + emb
return total
|