diff --git a/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/README.md b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/README.md new file mode 100644 index 000000000..a6b14f889 --- /dev/null +++ b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/README.md @@ -0,0 +1,85 @@ +# Non-record: 11L Full SOTA Stack + Score-First TTT (1xH100) + +**val_bpb: 1.1383** (1xH100, 985 steps + 3-epoch TTT) +**Artifact: 8.5 MB** (well under 16 MB budget) + +## Method + +### Architecture (28.7M parameters) +- 11 transformer layers, dim=512, 8 heads / 4 KV heads (GQA) +- MLP 3x expansion (hidden=1536) with **LeakyReLU(0.5)^2** activation +- **SmearGate** — learned per-dim gate blending each token with previous +- **BigramHash(10240, dim=128)** — XOR hash-based bigram embeddings +- **TrigramHash(4096, dim=128)** — XOR hash-based trigram embeddings +- **Value Residual (ResFormer)** — cache V from layer 0, blend into all layers via learned lambda +- **Gated Attention** — per-head sigmoid gate (nn.Linear, bias init 4.0) +- **XSA on all 11 layers** — exclusive self-attention subtracting self-value projection +- **Partial RoPE** — rotary embeddings on 16/64 head dimensions only +- Tied FP16 embeddings, U-Net skip connections, orthogonal initialization +- Logit softcap 30.0, QK gain init 1.5 + +### Training +- Muon optimizer: lr=0.03, momentum 0.92→0.99 over 1500 steps, WD=0.04 +- Adam for embeddings (lr=0.035) and scalars (lr=0.03) +- Batch 524,288 tokens, seq_len 2048 +- Warmdown 300 iterations (wallclock-based) +- Late QAT via STE (final 15% of wallclock time) +- Gradient clipping 0.3 + +### Quantization +- Int6 uniform per-row quantization with GPTQ-lite (5-percentile clip search) +- FP16 passthrough for tied embeddings (most quant-sensitive tensor) +- zstd-22 compression + +### Evaluation +- Sliding window eval, stride=256 (on 1xH100 for speed; stride=64 for competition) +- **Score-first TTT (3 epochs)**: frozen embeddings, only block params updated, per-layer LR groups (3x for mlp.proj, 0.5x for mlp.fc), cosine LR decay + +## Development Process + +Used Karpathy-style **autoresearch** methodology: autonomous experiment loop with 30 experiments over ~8 hours on 1xH100. Each experiment modifies one variable, runs training, and keeps/discards based on val_bpb improvement. + +### Key findings from autoresearch (30 experiments): + +| Experiment | BPB | Delta | Status | +|---|---|---|---| +| Initial baseline | 1.4527 | — | start | +| Disable EMA+SWA (raw weights better) | 1.4027 | -0.050 | keep | +| Batch 786K→524K (more steps) | 1.3432 | -0.060 | keep | +| XSA all 11 layers | 1.3379 | -0.005 | keep | +| LR 0.025→0.03 | 1.3271 | -0.011 | keep | +| Warmdown 3500→300 | 1.2567 | -0.070 | keep (series) | +| Remove VE128 (not helping) | 1.2567 | 0.000 | keep (simpler) | +| Disable LN Scale (neutral) | 1.2563 | -0.000 | keep (simpler) | +| + 3-epoch score-first TTT | **1.1383** | -0.116 | **final** | + +### Feature ablation (impact on our stack): + +| Feature | BPB Impact | +|---|---| +| Value Residual | -0.017 | +| SmearGate | -0.010 | +| XSA all 11 layers | -0.005 | +| Gated Attention | -0.004 | +| Partial RoPE (16/64) | -0.004 | +| TrigramHash | -0.002 | +| Late QAT | -0.002 | + +### Confirmed anti-patterns (tested and rejected): +- Warmdown 4500: too much decay for limited steps +- Batch 262K: per-step quality drops +- LR 0.04: too high, diverges +- seq_len=1024: worse despite more steps +- Fast momentum warmup: unstable +- No grad clipping: diverges +- EMA on 1xH100: too slow for ~960 steps + +## Scaling to 8xH100 + +This submission was developed on 1xH100 (~960 steps). On 8xH100 (~7000 steps), recommended changes: +- Re-enable EMA (decay=0.997) +- Warmdown 3500, batch 786K +- MLP 3.5x (hidden=1792) +- EVAL_STRIDE=64 + +Expected 8xH100 BPB: ~1.05-1.10 with TTT. diff --git a/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/submission.json b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/submission.json new file mode 100644 index 000000000..804785566 --- /dev/null +++ b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/submission.json @@ -0,0 +1,11 @@ +{ + "author": "Aryan Bhosale", + "github_id": "aryanbhosale", + "name": "11L Full SOTA Stack + Score-First TTT (1xH100)", + "blurb": "11-layer transformer with LeakyReLU(0.5)^2, SmearGate, BigramHash(10240), TrigramHash(4096), Value Residual, Gated Attention, XSA-all-11, Partial RoPE(16/64), int6+GPTQ-lite+zstd-22. Score-first TTT with frozen embeddings, per-layer LR groups, cosine schedule. Validated on 1xH100 via 30-experiment autoresearch loop.", + "date": "2026-03-24T00:00:00Z", + "val_loss": 1.9190, + "val_bpb": 1.1383, + "bytes_total": 8513097, + "bytes_code": 64193 +} diff --git a/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/train_gpt.py b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/train_gpt.py new file mode 100644 index 000000000..33ca88a0b --- /dev/null +++ b/records/track_non_record_16mb/2026-03-24_SOTA_11L_TTT/train_gpt.py @@ -0,0 +1,1657 @@ +"""SOTA config for OpenAI Parameter Golf. All verified improvements from 500+ PRs.""" +from __future__ import annotations +import copy +import glob +import io +import math +import os +import random +import subprocess +import sys +import time +import uuid +import zlib +from pathlib import Path + +try: + import zstandard + def _compress(data: bytes) -> bytes: return zstandard.ZstdCompressor(level=22).compress(data) + def _decompress(data: bytes) -> bytes: return zstandard.ZstdDecompressor().decompress(data) +except ImportError: + def _compress(data: bytes) -> bytes: return zlib.compress(data, level=9) + def _decompress(data: bytes) -> bytes: return zlib.decompress(data) + + +import numpy as np +import sentencepiece as spm +import torch +import torch.distributed as dist +import torch.nn.functional as F +from torch import Tensor, nn +from torch.nn.parallel import DistributedDataParallel as DDP + +# --- HYPERPARAMETERS (exact values from #518/#505/#493 consensus) --- +class Hyperparameters: + data_path = os.environ.get("DATA_PATH", "./data/datasets/fineweb10B_sp1024") + train_files = os.path.join(data_path, "fineweb_train_*.bin") + val_files = os.path.join(data_path, "fineweb_val_*.bin") + tokenizer_path = os.environ.get("TOKENIZER_PATH", "./data/tokenizers/fineweb_1024_bpe.model") + run_id = os.environ.get("RUN_ID", str(uuid.uuid4())) + seed = int(os.environ.get("SEED", 1337)) + + val_batch_size = int(os.environ.get("VAL_BATCH_SIZE", 524_288)) + val_loss_every = int(os.environ.get("VAL_LOSS_EVERY", 1000)) + train_log_every = int(os.environ.get("TRAIN_LOG_EVERY", 200)) + + iterations = int(os.environ.get("ITERATIONS", 20000)) + warmdown_iters = int(os.environ.get("WARMDOWN_ITERS", 300)) + warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) + train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 524_288)) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 2048)) + max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) + qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) + + vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) + num_layers = int(os.environ.get("NUM_LAYERS", 11)) + num_kv_heads = int(os.environ.get("NUM_KV_HEADS", 4)) + model_dim = int(os.environ.get("MODEL_DIM", 512)) + num_heads = int(os.environ.get("NUM_HEADS", 8)) + mlp_mult = float(os.environ.get("MLP_MULT", 3.0)) + tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1"))) + rope_base = float(os.environ.get("ROPE_BASE", 10000.0)) + logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0)) + + embed_lr = float(os.environ.get("EMBED_LR", 0.6)) + head_lr = float(os.environ.get("HEAD_LR", 0.008)) + tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.035)) + tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005)) + matrix_lr = float(os.environ.get("MATRIX_LR", 0.03)) + scalar_lr = float(os.environ.get("SCALAR_LR", 0.03)) + muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.99)) + muon_backend_steps = int(os.environ.get("MUON_BACKEND_STEPS", 5)) + muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.92)) + muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 1500)) + beta1 = float(os.environ.get("BETA1", 0.9)) + beta2 = float(os.environ.get("BETA2", 0.95)) + adam_eps = float(os.environ.get("ADAM_EPS", 1e-8)) + grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.3)) + weight_decay = float(os.environ.get("WEIGHT_DECAY", 0.04)) + bigram_vocab_size = int(os.environ.get("BIGRAM_VOCAB_SIZE", 10240)) + bigram_dim = int(os.environ.get("BIGRAM_DIM", 128)) + trigram_vocab_size = int(os.environ.get("TRIGRAM_VOCAB_SIZE", 4096)) + trigram_dim = int(os.environ.get("TRIGRAM_DIM", 128)) + use_trigramhash = bool(int(os.environ.get("USE_TRIGRAMHASH", "1"))) + rope_dims = int(os.environ.get("ROPE_DIMS", 16)) + xsa_last_n = int(os.environ.get("XSA_LAST_N", 11)) + ve_dim = int(os.environ.get("VE_DIM", 0)) + ve_layers = os.environ.get("VE_LAYERS", "9,10") + + use_smeargate = bool(int(os.environ.get("USE_SMEARGATE", "1"))) + use_bigramhash = bool(int(os.environ.get("USE_BIGRAMHASH", "1"))) + use_value_residual = bool(int(os.environ.get("USE_VALUE_RESIDUAL", "1"))) + use_gated_attention = bool(int(os.environ.get("USE_GATED_ATTENTION", "1"))) + use_ln_scale = bool(int(os.environ.get("USE_LN_SCALE", "0"))) + use_ema = bool(int(os.environ.get("USE_EMA", "0"))) + ema_decay = float(os.environ.get("EMA_DECAY", 0.997)) + swa_enabled = bool(int(os.environ.get("SWA_ENABLED", "0"))) + swa_every = int(os.environ.get("SWA_EVERY", 50)) + swa_threshold = float(os.environ.get("SWA_THRESHOLD", 0.2)) + use_late_qat = bool(int(os.environ.get("USE_LATE_QAT", "1"))) + qat_time_frac = float(os.environ.get("QAT_TIME_FRAC", 0.15)) + eval_stride = int(os.environ.get("EVAL_STRIDE", 64)) + + # TTT (Test-Time Training) — legal score-first approach + use_ttt = bool(int(os.environ.get("USE_TTT", "0"))) + ttt_lr = float(os.environ.get("TTT_LR", 0.0005)) + ttt_epochs = int(os.environ.get("TTT_EPOCHS", 30)) + ttt_chunk_tokens = int(os.environ.get("TTT_CHUNK_TOKENS", 32768)) + ttt_freeze_embed = bool(int(os.environ.get("TTT_FREEZE_EMBED", "1"))) + ttt_grad_clip = float(os.environ.get("TTT_GRAD_CLIP", 1.0)) + +# --- COMPRESSION CONSTANTS --- +INT6_RANGE = 31 +QUANT_RANGE = INT6_RANGE # int6 uniform for all weights (we have size budget) +_MLP_PATTERNS = ("mlp.fc", "mlp.proj") + +CONTROL_TENSOR_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "CONTROL_TENSOR_NAME_PATTERNS", + "attn_scale,mlp_scale,resid_mix,q_gain,skip_weight,skip_weights," + "vr_lambda,attn_gate,ve_scale,bigram_scale,trigram_scale", + ).split(",") + if pattern +) +INT8_KEEP_FLOAT_FP32_NAME_PATTERNS = CONTROL_TENSOR_NAME_PATTERNS +INT8_KEEP_FLOAT_MAX_NUMEL = 65_536 +# tok_emb.weight (524K params) kept FP16 via explicit name match below +_FP16_PASSTHROUGH_NAMES = ("tok_emb.weight",) +INT8_KEEP_FLOAT_STORE_DTYPE = torch.float16 +INT8_PER_ROW_SCALE_DTYPE = torch.float16 + +# --- MUON OPTIMIZER --- +def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) -> Tensor: + a, b, c = (3.4445, -4.7750, 2.0315) + X = G.bfloat16() + X /= X.norm() + eps + transposed = G.size(0) > G.size(1) + if transposed: + X = X.T + for _ in range(steps): + A = X @ X.T + B = b * A + c * A @ A + X = a * X + B @ X + return X.T if transposed else X + + +class Muon(torch.optim.Optimizer): + def __init__(self, params, lr: float, momentum: float, backend_steps: int, + nesterov: bool = True, weight_decay: float = 0.0): + super().__init__( + params, + dict(lr=lr, momentum=momentum, backend_steps=backend_steps, + nesterov=nesterov, weight_decay=weight_decay), + ) + + @torch.no_grad() + def step(self, closure=None): + loss = None + if closure is not None: + with torch.enable_grad(): + loss = closure() + + distributed = dist.is_available() and dist.is_initialized() + world_size = dist.get_world_size() if distributed else 1 + rank = dist.get_rank() if distributed else 0 + + for group in self.param_groups: + params = group["params"] + if not params: + continue + lr = group["lr"] + momentum = group["momentum"] + backend_steps = group["backend_steps"] + nesterov = group["nesterov"] + wd = group.get("weight_decay", 0.0) + + total_params = sum(int(p.numel()) for p in params) + updates_flat = torch.zeros(total_params, device=params[0].device, dtype=torch.bfloat16) + + curr = 0 + for i, p in enumerate(params): + if i % world_size == rank and p.grad is not None: + g = p.grad + state = self.state[p] + if "momentum_buffer" not in state: + state["momentum_buffer"] = torch.zeros_like(g) + buf = state["momentum_buffer"] + buf.mul_(momentum).add_(g) + if nesterov: + g = g.add(buf, alpha=momentum) + g = zeropower_via_newtonschulz5(g, steps=backend_steps) + g *= max(1, g.size(0) / g.size(1)) ** 0.5 + updates_flat[curr : curr + p.numel()] = g.reshape(-1) + curr += p.numel() + + if distributed: + dist.all_reduce(updates_flat, op=dist.ReduceOp.SUM) + + curr = 0 + for p in params: + if wd > 0.0: + p.data.mul_(1.0 - lr * wd) + g = updates_flat[curr : curr + p.numel()].view_as(p).to(dtype=p.dtype) + p.add_(g, alpha=-lr) + curr += p.numel() + + return loss + + +# --- TOKENIZER-AGNOSTIC EVALUATION --- +def build_sentencepiece_luts( + sp: spm.SentencePieceProcessor, vocab_size: int, device: torch.device +) -> tuple[Tensor, Tensor, Tensor]: + sp_vocab_size = int(sp.vocab_size()) + table_size = max(sp_vocab_size, vocab_size) + base_bytes_np = np.zeros((table_size,), dtype=np.int16) + has_leading_space_np = np.zeros((table_size,), dtype=np.bool_) + is_boundary_token_np = np.ones((table_size,), dtype=np.bool_) + for token_id in range(sp_vocab_size): + if sp.is_control(token_id) or sp.is_unknown(token_id) or sp.is_unused(token_id): + continue + is_boundary_token_np[token_id] = False + if sp.is_byte(token_id): + base_bytes_np[token_id] = 1 + continue + piece = sp.id_to_piece(token_id) + if piece.startswith("\u2581"): + has_leading_space_np[token_id] = True + piece = piece[1:] + base_bytes_np[token_id] = len(piece.encode("utf-8")) + return ( + torch.tensor(base_bytes_np, dtype=torch.int16, device=device), + torch.tensor(has_leading_space_np, dtype=torch.bool, device=device), + torch.tensor(is_boundary_token_np, dtype=torch.bool, device=device), + ) + + +def load_validation_tokens(pattern: str, seq_len: int) -> Tensor: + files = [Path(p) for p in sorted(glob.glob(pattern))] + if not files: + raise FileNotFoundError(f"No files found for pattern: {pattern}") + tokens = torch.cat([load_data_shard(file) for file in files]).contiguous() + usable = ((tokens.numel() - 1) // seq_len) * seq_len + if usable <= 0: + raise ValueError(f"Validation split is too short for TRAIN_SEQ_LEN={seq_len}") + return tokens[: usable + 1] + + +def eval_val( + args: Hyperparameters, + model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, +) -> tuple[float, float]: + """Sliding window eval with stride=eval_stride, batched for throughput. + NO document isolation (hurts at stride=64, confirmed in issue #140).""" + seq_len = args.train_seq_len + stride = args.eval_stride + windows_per_batch = 32 + total_tokens = val_tokens.numel() - 1 + + starts = list(range(0, total_tokens - seq_len + 1, stride)) + my_starts = starts[rank::world_size] + + val_loss_sum = torch.zeros((), device=device, dtype=torch.float64) + val_token_count = torch.zeros((), device=device, dtype=torch.float64) + val_byte_count = torch.zeros((), device=device, dtype=torch.float64) + + model.eval() + with torch.inference_mode(): + for batch_start in range(0, len(my_starts), windows_per_batch): + batch_starts = my_starts[batch_start : batch_start + windows_per_batch] + x_list = [] + y_list = [] + for s in batch_starts: + chunk = val_tokens[s : s + seq_len + 1].to(dtype=torch.int64) + x_list.append(chunk[:-1]) + y_list.append(chunk[1:]) + x = torch.stack(x_list).to(device=device, non_blocking=True) + y = torch.stack(y_list).to(device=device, non_blocking=True) + + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + logits = model(x) # target_ids=None -> returns logits + + for i, s in enumerate(batch_starts): + if s == 0: + score_start = 0 + score_len = min(seq_len, stride) + else: + score_start = seq_len - stride + score_len = stride + + window_logits = logits[i, score_start : score_start + score_len] + window_targets = y[i, score_start : score_start + score_len] + loss = F.cross_entropy(window_logits.float(), window_targets, reduction="sum") + val_loss_sum += loss.to(torch.float64) + val_token_count += score_len + + prev_ids = x[i, score_start : score_start + score_len] + tgt_ids = window_targets + token_bytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) + token_bytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) + val_byte_count += token_bytes.to(torch.float64).sum() + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(val_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(val_byte_count, op=dist.ReduceOp.SUM) + + val_loss = val_loss_sum / val_token_count + bits_per_token = val_loss.item() / math.log(2.0) + tokens_per_byte = val_token_count.item() / val_byte_count.item() + model.train() + return float(val_loss.item()), float(bits_per_token * tokens_per_byte) + + +def eval_val_ttt( + args: Hyperparameters, + base_model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, + log_fn=None, +) -> tuple[float, float]: + """Legal score-first TTT with frozen base + per-layer LR (from #518/#481). + Freezes all params except block weights. Scores each chunk, then trains on it. + Multi-epoch: epochs 0..N-2 train only; epoch N-1 scores then trains.""" + seq_len = args.train_seq_len + chunk_size = args.ttt_chunk_tokens + total_tokens = val_tokens.numel() - 1 + if log_fn is None: + log_fn = lambda msg: None + + # Save original state dict for restoration + orig_sd = {k: v.detach().cpu().clone() for k, v in base_model.state_dict().items()} + + # Freeze embeddings, only train block params (from #518: freeze tok_emb, bigram, trigram) + for name, p in base_model.named_parameters(): + p.requires_grad_(False) + + # Unfreeze block params with per-layer LR groups (from #518) + proj_params, fc_params, other_block_params = [], [], [] + for name, p in base_model.named_parameters(): + if "blocks." not in name: + continue # Skip embeddings, skip_weights, etc. + p.requires_grad_(True) + if "mlp.proj" in name: + proj_params.append(p) + elif "mlp.fc" in name: + fc_params.append(p) + else: + other_block_params.append(p) + + ttt_lr = args.ttt_lr + ttt_opt = torch.optim.AdamW([ + {"params": proj_params, "lr": ttt_lr * 3.0, "initial_lr": ttt_lr * 3.0}, + {"params": fc_params, "lr": ttt_lr * 0.5, "initial_lr": ttt_lr * 0.5}, + {"params": other_block_params, "lr": ttt_lr, "initial_lr": ttt_lr}, + ], weight_decay=0.0) + + # Build chunk list — each chunk is chunk_size tokens, scored as a single window + chunk_starts = list(range(0, total_tokens - seq_len + 1, chunk_size)) + my_chunks = chunk_starts[rank::world_size] + n_chunks = len(my_chunks) + total_steps = n_chunks * args.ttt_epochs + log_fn(f"TTT: {n_chunks} chunks, {args.ttt_epochs} epochs, {total_steps} total steps") + + val_loss_sum = torch.zeros((), device=device, dtype=torch.float64) + val_token_count = torch.zeros((), device=device, dtype=torch.float64) + val_byte_count = torch.zeros((), device=device, dtype=torch.float64) + + step = 0 + for epoch in range(args.ttt_epochs): + is_scoring_epoch = (epoch == args.ttt_epochs - 1) + if is_scoring_epoch: + val_loss_sum.zero_() + val_token_count.zero_() + val_byte_count.zero_() + + for ci, c_start in enumerate(my_chunks): + c_end = min(c_start + seq_len + 1, total_tokens + 1) + chunk = val_tokens[c_start:c_end].to(device=device, dtype=torch.int64) + if chunk.numel() < 2: + continue + x = chunk[:-1].unsqueeze(0) + y = chunk[1:].unsqueeze(0) + actual_len = x.size(1) + + # SCORE this chunk (only on last epoch) + if is_scoring_epoch: + base_model.eval() + with torch.inference_mode(), torch.autocast(device_type="cuda", dtype=torch.bfloat16): + logits = base_model(x) + loss_val = F.cross_entropy(logits[0, :actual_len].float(), y[0, :actual_len], reduction="sum") + val_loss_sum += loss_val.to(torch.float64) + val_token_count += actual_len + prev_ids = x[0, :actual_len] + tgt_ids = y[0, :actual_len] + tbytes = base_bytes_lut[tgt_ids].to(torch.int16) + tbytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(torch.int16) + val_byte_count += tbytes.to(torch.float64).sum() + + # TRAIN on this chunk (adapt for future chunks) + base_model.train() + # Cosine LR + progress = step / max(total_steps, 1) + cos_mul = 0.5 * (1.0 + math.cos(math.pi * progress)) + for g in ttt_opt.param_groups: + g["lr"] = g["initial_lr"] * cos_mul + + ttt_opt.zero_grad() + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + loss = base_model(x, y) + loss.backward() + if args.ttt_grad_clip > 0: + torch.nn.utils.clip_grad_norm_( + [p for p in base_model.parameters() if p.requires_grad], args.ttt_grad_clip) + ttt_opt.step() + step += 1 + + if is_scoring_epoch: + log_fn(f"TTT epoch {epoch}: scored {int(val_token_count.item())} tokens") + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(val_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(val_byte_count, op=dist.ReduceOp.SUM) + + val_loss = val_loss_sum / val_token_count + bpt = val_loss.item() / math.log(2.0) + tpb = val_token_count.item() / val_byte_count.item() + + # Restore original weights + base_model.load_state_dict(orig_sd, strict=True) + for p in base_model.parameters(): + p.requires_grad_(True) + return float(val_loss.item()), float(bpt * tpb) + + +# --- POST-TRAINING QUANTIZATION (Mixed Int5/Int6 with GPTQ-lite) --- +def tensor_nbytes(t: Tensor) -> int: + return int(t.numel()) * int(t.element_size()) + + +def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, str]) -> Tensor: + if any(pattern in name for pattern in INT8_KEEP_FLOAT_FP32_NAME_PATTERNS): + return t.float().contiguous() + if t.dtype in {torch.float32, torch.bfloat16}: + passthrough_orig_dtypes[name] = str(t.dtype).removeprefix("torch.") + return t.to(dtype=INT8_KEEP_FLOAT_STORE_DTYPE).contiguous() + return t + + +def quantize_float_tensor(t: Tensor, name: str = "") -> tuple[Tensor, Tensor]: + """Quantize a float tensor to int6 with GPTQ-lite (5-percentile search).""" + qrange = QUANT_RANGE # int6 uniform for all weights + + t32 = t.float() + if t32.ndim == 2: + _CLIP_QS = [0.9990, 0.9995, 0.9999, 0.99999, 1.0] + best_q = None + best_scale = None + best_mse = None + for cq in _CLIP_QS: + clip_abs = ( + torch.quantile(t32.abs(), cq, dim=1) + if t32.numel() + else torch.empty((t32.shape[0],), dtype=torch.float32) + ) + clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) + s = (clip_abs / float(qrange)).clamp_min(1.0 / float(qrange)) + q = torch.clamp(torch.round(clipped / s[:, None]), -qrange, qrange) + recon = q * s[:, None] + mse = (t32 - recon).square().sum(dim=1) + if best_mse is None: + best_mse = mse + best_q = q + best_scale = s + else: + improved = mse < best_mse + if improved.any(): + best_mse = torch.where(improved, mse, best_mse) + best_q = torch.where(improved[:, None], q, best_q) + best_scale = torch.where(improved, s, best_scale) + return best_q.to(torch.int8).contiguous(), best_scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous() + + clip_q = 0.9999984 + clip_abs = float(torch.quantile(t32.abs().flatten(), clip_q).item()) if t32.numel() else 0.0 + scale = torch.tensor(clip_abs / float(qrange) if clip_abs > 0 else 1.0, dtype=torch.float32) + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -qrange, qrange).to(torch.int8).contiguous() + return q, scale + + +def quantize_state_dict_int8(state_dict: dict[str, Tensor]): + quantized: dict[str, Tensor] = {} + scales: dict[str, Tensor] = {} + dtypes: dict[str, str] = {} + passthrough: dict[str, Tensor] = {} + passthrough_orig_dtypes: dict[str, str] = {} + qmeta: dict[str, dict[str, object]] = {} + stats = dict.fromkeys( + ("param_count", "num_tensors", "num_float_tensors", "num_nonfloat_tensors", + "baseline_tensor_bytes", "int8_payload_bytes"), + 0, + ) + + for name, tensor in state_dict.items(): + t = tensor.detach().to("cpu").contiguous() + stats["param_count"] += int(t.numel()) + stats["num_tensors"] += 1 + stats["baseline_tensor_bytes"] += tensor_nbytes(t) + + if not t.is_floating_point(): + stats["num_nonfloat_tensors"] += 1 + passthrough[name] = t + stats["int8_payload_bytes"] += tensor_nbytes(t) + continue + + if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL or any(p in name for p in _FP16_PASSTHROUGH_NAMES): + kept = keep_float_tensor(name, t, passthrough_orig_dtypes) + passthrough[name] = kept + stats["int8_payload_bytes"] += tensor_nbytes(kept) + continue + + stats["num_float_tensors"] += 1 + q, s = quantize_float_tensor(t, name=name) + if s.ndim > 0: + qmeta[name] = {"scheme": "per_row", "axis": 0} + quantized[name] = q + scales[name] = s + dtypes[name] = str(t.dtype).removeprefix("torch.") + stats["int8_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s) + + obj: dict[str, object] = { + "__quant_format__": "int8_clean_per_row_v1", + "quantized": quantized, + "scales": scales, + "dtypes": dtypes, + "passthrough": passthrough, + } + if qmeta: + obj["qmeta"] = qmeta + if passthrough_orig_dtypes: + obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes + return obj, stats + + +def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]: + out: dict[str, Tensor] = {} + qmeta = obj.get("qmeta", {}) + passthrough_orig_dtypes = obj.get("passthrough_orig_dtypes", {}) + for name, q in obj["quantized"].items(): + dtype = getattr(torch, obj["dtypes"][name]) + s = obj["scales"][name] + if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0: + s = s.to(dtype=torch.float32) + out[name] = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous() + else: + scale = float(s.item()) + out[name] = (q.float() * scale).to(dtype=dtype).contiguous() + for name, t in obj["passthrough"].items(): + out_t = t.detach().to("cpu").contiguous() + orig_dtype = passthrough_orig_dtypes.get(name) + if isinstance(orig_dtype, str): + out_t = out_t.to(dtype=getattr(torch, orig_dtype)).contiguous() + out[name] = out_t + return out + + +# --- DATA LOADING --- +def load_data_shard(file: Path) -> Tensor: + header_bytes = 256 * np.dtype(" None: + self.file_idx = (self.file_idx + 1) % len(self.files) + self.tokens = load_data_shard(self.files[self.file_idx]) + self.pos = 0 + + def take(self, n: int) -> Tensor: + chunks: list[Tensor] = [] + remaining = n + while remaining > 0: + avail = self.tokens.numel() - self.pos + if avail <= 0: + self._advance_file() + continue + k = min(remaining, avail) + chunks.append(self.tokens[self.pos : self.pos + k]) + self.pos += k + remaining -= k + return chunks[0] if len(chunks) == 1 else torch.cat(chunks) + + +class DistributedTokenLoader: + def __init__(self, pattern: str, rank: int, world_size: int, device: torch.device): + self.rank = rank + self.world_size = world_size + self.device = device + self.stream = TokenStream(pattern) + + def next_batch(self, global_tokens: int, seq_len: int, grad_accum_steps: int) -> tuple[Tensor, Tensor]: + local_tokens = global_tokens // (self.world_size * grad_accum_steps) + per_rank_span = local_tokens + 1 + chunk = self.stream.take(per_rank_span * self.world_size) + start = self.rank * per_rank_span + local = chunk[start : start + per_rank_span].to(dtype=torch.int64) + x = local[:-1].reshape(-1, seq_len) + y = local[1:].reshape(-1, seq_len) + return x.to(self.device, non_blocking=True), y.to(self.device, non_blocking=True) + + +# --- TRANSFORMER MODULES --- +class RMSNorm(nn.Module): + def __init__(self, eps: float | None = None): + super().__init__() + self.eps = eps + + def forward(self, x: Tensor) -> Tensor: + return F.rms_norm(x, (x.size(-1),), eps=self.eps) + + +class CastedLinear(nn.Linear): + _qat_enabled: bool = False # CLASS-level flag + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + + def forward(self, x: Tensor) -> Tensor: + w = self.weight.to(x.dtype) + if CastedLinear._qat_enabled and self.training and w.ndim == 2: + with torch.no_grad(): + w32 = self.weight.float() + row_max = w32.abs().amax(dim=1) + scale = (row_max / 31.0).clamp_min(1.0 / 31.0) # Always int6 range for QAT + w_q = (torch.clamp(torch.round(w32 / scale[:, None]), -32, 31) * scale[:, None]).to(x.dtype) + w = w + (w_q - w).detach() # STE + bias = self.bias.to(x.dtype) if self.bias is not None else None + return F.linear(x, w, bias) + + +def restore_low_dim_params_to_fp32(module: nn.Module) -> None: + with torch.no_grad(): + for name, param in module.named_parameters(): + if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32: + param.data = param.data.float() + + +class Rotary(nn.Module): + def __init__(self, dim: int, base: float = 10000.0, rope_dims: int = 0): + super().__init__() + self.rope_dims = rope_dims if rope_dims > 0 else dim + rd = self.rope_dims + inv_freq = 1.0 / (base ** (torch.arange(0, rd, 2, dtype=torch.float32) / rd)) + self.register_buffer("inv_freq", inv_freq, persistent=False) + self._seq_len_cached = 0 + self._cos_cached: Tensor | None = None + self._sin_cached: Tensor | None = None + + def forward(self, seq_len: int, device: torch.device, dtype: torch.dtype) -> tuple[Tensor, Tensor]: + if ( + self._cos_cached is None + or self._sin_cached is None + or self._seq_len_cached != seq_len + or self._cos_cached.device != device + ): + t = torch.arange(seq_len, device=device, dtype=self.inv_freq.dtype) + freqs = torch.outer(t, self.inv_freq.to(device)) + self._cos_cached = freqs.cos()[None, None, :, :] + self._sin_cached = freqs.sin()[None, None, :, :] + self._seq_len_cached = seq_len + return self._cos_cached.to(dtype=dtype), self._sin_cached.to(dtype=dtype) + + +def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor, rope_dims: int = 0) -> Tensor: + if rope_dims > 0 and rope_dims < x.size(-1): + x_rope, x_pass = x[..., :rope_dims], x[..., rope_dims:] + half = rope_dims // 2 + x1, x2 = x_rope[..., :half], x_rope[..., half:] + x_rope = torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + return torch.cat((x_rope, x_pass), dim=-1) + half = x.size(-1) // 2 + x1, x2 = x[..., :half], x[..., half:] + return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + + +class SmearGate(nn.Module): + def __init__(self, dim: int): + super().__init__() + self.gate = nn.Parameter(torch.zeros(dim, dtype=torch.float32)) + + def forward(self, x: Tensor) -> Tensor: + g = torch.sigmoid(self.gate.to(dtype=x.dtype))[None, None, :] + x_prev = torch.cat([torch.zeros_like(x[:, :1]), x[:, :-1]], dim=1) + return (1 - g) * x + g * x_prev + + +class BigramHashEmbedding(nn.Module): + def __init__(self, vocab_size: int, dim: int, model_dim: int): + super().__init__() + self.vocab_size = vocab_size # 2048 + self.embed = nn.Embedding(vocab_size, dim) # dim=128 + nn.init.zeros_(self.embed.weight) # zeros init + self.proj = CastedLinear(dim, model_dim, bias=False) + nn.init.zeros_(self.proj.weight) # zeros init + self.bigram_scale = nn.Parameter(torch.tensor(0.05, dtype=torch.float32)) + + def forward(self, input_ids: Tensor) -> Tensor: + t = input_ids.to(torch.int32) + mod = self.vocab_size - 1 # 2047 + out = torch.empty_like(t) + out[..., 0] = mod # first position has no previous token + out[..., 1:] = torch.bitwise_xor(36313 * t[..., 1:], 27191 * t[..., :-1]) % mod + h = self.embed(out.long()) + return self.proj(h) * self.bigram_scale.to(dtype=h.dtype) + + +class TrigramHashEmbedding(nn.Module): + """Hash consecutive token trigrams. From PR #486: -0.023 BPB combined with VRL.""" + def __init__(self, vocab_size: int, dim: int, model_dim: int): + super().__init__() + self.vocab_size = vocab_size # 4096 + self.embed = nn.Embedding(vocab_size, dim) + nn.init.zeros_(self.embed.weight) + self.proj = CastedLinear(dim, model_dim, bias=False) + nn.init.zeros_(self.proj.weight) + self.trigram_scale = nn.Parameter(torch.tensor(0.03, dtype=torch.float32)) + + def forward(self, input_ids: Tensor) -> Tensor: + t = input_ids.to(torch.int32) + mod = self.vocab_size - 1 + out = torch.empty_like(t) + out[..., 0] = mod + out[..., 1] = mod + out[..., 2:] = torch.bitwise_xor( + torch.bitwise_xor(36313 * t[..., 2:], 27191 * t[..., 1:-1]), + 51497 * t[..., :-2], + ) % mod + h = self.embed(out.long()) + return self.proj(h) * self.trigram_scale.to(dtype=h.dtype) + + +class ValueEmbedding(nn.Module): + def __init__(self, vocab_size: int, ve_dim: int, kv_dim: int): + super().__init__() + self.embed = nn.Embedding(vocab_size, ve_dim) # 1024 x 128 + nn.init.normal_(self.embed.weight, std=0.01) + self.proj = CastedLinear(ve_dim, kv_dim, bias=False) # 128 -> kv_dim + nn.init.zeros_(self.proj.weight) + self.ve_scale = nn.Parameter(torch.tensor(0.1, dtype=torch.float32)) + + def forward(self, token_ids: Tensor) -> Tensor: + h = self.proj(self.embed(token_ids)) + return h * self.ve_scale.to(dtype=h.dtype) + + +class CausalSelfAttention(nn.Module): + def __init__( + self, + dim: int, + num_heads: int, + num_kv_heads: int, + rope_base: float, + qk_gain_init: float, + layer_idx: int = 0, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_xsa: bool = False, + rope_dims: int = 0, + ): + super().__init__() + if dim % num_heads != 0: + raise ValueError("model_dim must be divisible by num_heads") + if num_heads % num_kv_heads != 0: + raise ValueError("num_heads must be divisible by num_kv_heads") + self.num_heads = num_heads + self.num_kv_heads = num_kv_heads + self.head_dim = dim // num_heads + if self.head_dim % 2 != 0: + raise ValueError("head_dim must be even for RoPE") + kv_dim = self.num_kv_heads * self.head_dim + self.c_q = CastedLinear(dim, dim, bias=False) + self.c_k = CastedLinear(dim, kv_dim, bias=False) + self.c_v = CastedLinear(dim, kv_dim, bias=False) + self.proj = CastedLinear(dim, dim, bias=False) + self.proj._zero_init = True + self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) + + # Partial RoPE + self._rope_dims = rope_dims + self.rotary = Rotary(dim // num_heads, base=rope_base, rope_dims=rope_dims) + + # Gated Attention (nn.Linear with bias, from #490/#413) + self._gated_attention = use_gated_attention + if use_gated_attention: + self.attn_gate = nn.Linear(dim, num_heads, bias=True) + nn.init.zeros_(self.attn_gate.weight) + nn.init.constant_(self.attn_gate.bias, 4.0) # near-open init + + # Value Residual (only on layers > 0, from #486/#490) + self._value_residual = use_value_residual and layer_idx > 0 + if self._value_residual: + self.vr_lambda = nn.Parameter(torch.tensor([0.5, 0.5], dtype=torch.float32)) + + # XSA (Exclusive Self-Attention, from #518/#505) + self.use_xsa = use_xsa + + def _xsa_efficient(self, y: Tensor, v: Tensor) -> Tensor: + """Subtract self-value projection via GQA-aware reshape.""" + B, T, H, D = y.shape + Hkv = v.size(-2) + group = H // Hkv + y_g = y.reshape(B, T, Hkv, group, D) + vn = F.normalize(v, dim=-1).unsqueeze(-2) + proj = (y_g * vn).sum(dim=-1, keepdim=True) * vn + return (y_g - proj).reshape(B, T, H, D) + + def forward(self, x: Tensor, v0: Tensor | None = None, v_embed: Tensor | None = None) -> tuple[Tensor, Tensor]: + bsz, seqlen, dim = x.shape + q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2) + k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + + # Compute v BEFORE reshape to heads so we can add v_embed + v_flat = self.c_v(x) # [B, T, kv_dim] + if v_embed is not None: + v_flat = v_flat + v_embed # Add VE128 BEFORE reshape + v = v_flat.reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + + q = F.rms_norm(q, (q.size(-1),)) + k = F.rms_norm(k, (k.size(-1),)) + + # Apply RoPE (partial or full via rope_dims) + cos, sin = self.rotary(seqlen, x.device, q.dtype) + q = apply_rotary_emb(q, cos, sin, rope_dims=self._rope_dims) + k = apply_rotary_emb(k, cos, sin, rope_dims=self._rope_dims) + + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + + # Value Residual: blend layer-0 V into current + raw_v = v # always return for caching + if self._value_residual and v0 is not None and hasattr(self, 'vr_lambda'): + lam = self.vr_lambda.to(dtype=v.dtype) + v = lam[0] * v0 + lam[1] * v + + y = F.scaled_dot_product_attention( + q, k, v, attn_mask=None, is_causal=True, + enable_gqa=(self.num_kv_heads != self.num_heads), + ) + + # y: [B, H, T, D] -> [B, T, H, D] for XSA and gated attention + y = y.transpose(1, 2) # [B, T, H, D] + + # XSA: Exclusive Self-Attention + if self.use_xsa: + # v needs to be [B, T, Hkv, D] for XSA + v_for_xsa = raw_v.transpose(1, 2) # [B, T, Hkv, D] + y = self._xsa_efficient(y, v_for_xsa) + + # Gated attention (applied to [B, T, H, D]) + if self._gated_attention: + gate = torch.sigmoid(self.attn_gate(x)) # [B, T, H] + y = y * gate.unsqueeze(-1) + + y = y.contiguous().reshape(bsz, seqlen, dim) + return self.proj(y), raw_v + + +class MLP(nn.Module): + def __init__(self, dim: int, mlp_mult: float): + super().__init__() + hidden = int(mlp_mult * dim) + self.fc = CastedLinear(dim, hidden, bias=False) + self.proj = CastedLinear(hidden, dim, bias=False) + self.proj._zero_init = True + + def forward(self, x: Tensor) -> Tensor: + x = F.leaky_relu(self.fc(x), negative_slope=0.5) + return self.proj(x.square()) + + +class Block(nn.Module): + def __init__( + self, + dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: float, + rope_base: float, + qk_gain_init: float, + layer_idx: int = 0, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_xsa: bool = False, + rope_dims: int = 0, + use_ln_scale: bool = False, + ): + super().__init__() + self.layer_idx = layer_idx + self.use_ln_scale = use_ln_scale + self.ln_scale_factor = 1.0 / math.sqrt(layer_idx + 1) if use_ln_scale else 1.0 + self.attn_norm = RMSNorm() + self.mlp_norm = RMSNorm() + self.attn = CausalSelfAttention( + dim, num_heads, num_kv_heads, rope_base, qk_gain_init, + layer_idx=layer_idx, + use_gated_attention=use_gated_attention, + use_value_residual=use_value_residual, + use_xsa=use_xsa, + rope_dims=rope_dims, + ) + self.mlp = MLP(dim, mlp_mult) + self.attn_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.mlp_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.resid_mix = nn.Parameter(torch.stack((torch.ones(dim), torch.zeros(dim))).float()) + + def forward(self, x: Tensor, x0: Tensor, v0: Tensor | None = None, v_embed: Tensor | None = None) -> tuple[Tensor, Tensor]: + mix = self.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + # LN Scale: multiply norm output by factor + normed = self.attn_norm(x) + if self.use_ln_scale: + normed = normed * self.ln_scale_factor + attn_out, v_out = self.attn(normed, v0, v_embed=v_embed) + scaled_attn = self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out + x = x + scaled_attn + mlp_normed = self.mlp_norm(x) + if self.use_ln_scale: + mlp_normed = mlp_normed * self.ln_scale_factor + mlp_out = self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(mlp_normed) + x = x + mlp_out + return x, v_out + + +class GPT(nn.Module): + def __init__( + self, + vocab_size: int, + num_layers: int, + model_dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: float, + tie_embeddings: bool, + tied_embed_init_std: float, + logit_softcap: float, + rope_base: float, + qk_gain_init: float, + use_smeargate: bool = False, + use_bigramhash: bool = False, + bigram_vocab_size: int = 2048, + bigram_dim: int = 128, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_ln_scale: bool = False, + rope_dims: int = 0, + xsa_last_n: int = 0, + ve_dim: int = 0, + ve_layers: str = "", + use_trigramhash: bool = False, + trigram_vocab_size: int = 4096, + trigram_dim: int = 128, + ): + super().__init__() + if logit_softcap <= 0.0: + raise ValueError(f"logit_softcap must be positive, got {logit_softcap}") + self.tie_embeddings = tie_embeddings + self.tied_embed_init_std = tied_embed_init_std + self.logit_softcap = logit_softcap + self.use_smeargate = use_smeargate + self.use_bigramhash = use_bigramhash + self.use_value_residual = use_value_residual + self.tok_emb = nn.Embedding(vocab_size, model_dim) + self.num_encoder_layers = num_layers // 2 + self.num_decoder_layers = num_layers - self.num_encoder_layers + self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers) + self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32)) + + if use_smeargate: + self.smeargate = SmearGate(model_dim) + if use_bigramhash: + self.bigram_embed = BigramHashEmbedding(bigram_vocab_size, bigram_dim, model_dim) + self.trigram_embed = None + if use_trigramhash: + self.trigram_embed = TrigramHashEmbedding(trigram_vocab_size, trigram_dim, model_dim) + + # Parse VE layers + self._ve_layer_indices: list[int] = [] + kv_dim = num_kv_heads * (model_dim // num_heads) + if ve_dim > 0 and ve_layers: + self._ve_layer_indices = [int(x.strip()) for x in ve_layers.split(",") if x.strip()] + if self._ve_layer_indices: + self.ve_shared = ValueEmbedding(vocab_size, ve_dim, kv_dim) + self.ve_layer_scales = nn.ParameterList( + [nn.Parameter(torch.ones(1, dtype=torch.float32)) for _ in self._ve_layer_indices] + ) + + # Determine which layers get XSA + xsa_layer_set = set() + if xsa_last_n > 0: + xsa_layer_set = set(range(num_layers - xsa_last_n, num_layers)) + + self.blocks = nn.ModuleList( + [ + Block( + model_dim, num_heads, num_kv_heads, mlp_mult, + rope_base, qk_gain_init, + layer_idx=i, + use_gated_attention=use_gated_attention, + use_value_residual=use_value_residual, + use_xsa=(i in xsa_layer_set), + rope_dims=rope_dims, + use_ln_scale=use_ln_scale, + ) + for i in range(num_layers) + ] + ) + self.final_norm = RMSNorm() + self.lm_head = None if tie_embeddings else CastedLinear(model_dim, vocab_size, bias=False) + if self.lm_head is not None: + self.lm_head._zero_init = True + self._init_weights() + + def _init_weights(self) -> None: + if self.tie_embeddings: + nn.init.normal_(self.tok_emb.weight, mean=0.0, std=self.tied_embed_init_std) + for module in self.modules(): + if isinstance(module, nn.Linear): + if getattr(module, "_zero_init", False): + nn.init.zeros_(module.weight) + else: + # OrthoInit for all non-zero-init Linear layers (SmearGate requires this) + nn.init.orthogonal_(module.weight) + + def forward(self, input_ids: Tensor, target_ids: Tensor | None = None) -> Tensor: + x = self.tok_emb(input_ids) + + # Add bigram + trigram hash embeddings + if self.use_bigramhash: + x = x + self.bigram_embed(input_ids) + if self.trigram_embed is not None: + x = x + self.trigram_embed(input_ids) + + x = F.rms_norm(x, (x.size(-1),)) + + # Apply smeargate after initial norm + if self.use_smeargate: + x = self.smeargate(x) + + x0 = x + skips: list[Tensor] = [] + v0: Tensor | None = None + + # Build VE lookup: layer_idx -> (ve_embed, scale_idx) + ve_map: dict[int, int] = {} + ve_embed_cache: Tensor | None = None + if self._ve_layer_indices: + ve_embed_cache = self.ve_shared(input_ids) # [B, T, kv_dim] + for si, li in enumerate(self._ve_layer_indices): + ve_map[li] = si + + # Encoder half stores skips + for i in range(self.num_encoder_layers): + v_embed_i = None + if i in ve_map: + v_embed_i = ve_embed_cache * self.ve_layer_scales[ve_map[i]].to(dtype=ve_embed_cache.dtype) + x, v_out = self.blocks[i](x, x0, v0, v_embed=v_embed_i) + if i == 0 and self.use_value_residual: + v0 = v_out + skips.append(x) + + # Decoder half reuses skips in reverse order + for i in range(self.num_decoder_layers): + if skips: + x = x + self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skips.pop() + layer_idx = self.num_encoder_layers + i + v_embed_i = None + if layer_idx in ve_map: + v_embed_i = ve_embed_cache * self.ve_layer_scales[ve_map[layer_idx]].to(dtype=ve_embed_cache.dtype) + x, v_out = self.blocks[layer_idx](x, x0, v0, v_embed=v_embed_i) + if self.num_encoder_layers == 0 and i == 0 and self.use_value_residual: + v0 = v_out + + x = self.final_norm(x) + + if target_ids is None: + # Eval mode: return logits [B, T, V] + x_flat = x.reshape(-1, x.size(-1)) + if self.tie_embeddings: + logits_proj = F.linear(x_flat, self.tok_emb.weight) + else: + logits_proj = self.lm_head(x_flat) + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + return logits.reshape(input_ids.shape[0], input_ids.shape[1], -1) + else: + # Training mode: return loss + x_flat = x.reshape(-1, x.size(-1)) + targets = target_ids.reshape(-1) + if self.tie_embeddings: + logits_proj = F.linear(x_flat, self.tok_emb.weight) + else: + logits_proj = self.lm_head(x_flat) + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + return F.cross_entropy(logits.float(), targets, reduction="mean") + + +# --- TRAINING --- +def main() -> None: + global zeropower_via_newtonschulz5 + + code = Path(__file__).read_text(encoding="utf-8") + args = Hyperparameters() + zeropower_via_newtonschulz5 = torch.compile(zeropower_via_newtonschulz5) + + # --- DISTRIBUTED + CUDA SETUP --- + distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ + rank = int(os.environ.get("RANK", "0")) + world_size = int(os.environ.get("WORLD_SIZE", "1")) + local_rank = int(os.environ.get("LOCAL_RANK", "0")) + if world_size <= 0: + raise ValueError(f"WORLD_SIZE must be positive, got {world_size}") + if 8 % world_size != 0: + raise ValueError(f"WORLD_SIZE={world_size} must divide 8 so grad_accum_steps stays integral") + grad_accum_steps = 8 // world_size + grad_scale = 1.0 / grad_accum_steps + if not torch.cuda.is_available(): + raise RuntimeError("CUDA is required") + device = torch.device("cuda", local_rank) + torch.cuda.set_device(device) + if distributed: + dist.init_process_group(backend="nccl", device_id=device) + dist.barrier() + master_process = rank == 0 + + torch.backends.cuda.matmul.allow_tf32 = True + torch.backends.cudnn.allow_tf32 = True + from torch.backends.cuda import enable_cudnn_sdp, enable_flash_sdp, enable_math_sdp, enable_mem_efficient_sdp + + enable_cudnn_sdp(False) + enable_flash_sdp(True) + enable_mem_efficient_sdp(False) + enable_math_sdp(False) + + logfile = None + if master_process: + os.makedirs("logs", exist_ok=True) + logfile = f"logs/{args.run_id}.txt" + print(logfile) + + def log0(msg: str, console: bool = True) -> None: + if not master_process: + return + if console: + print(msg) + if logfile is not None: + with open(logfile, "a", encoding="utf-8") as f: + print(msg, file=f) + + log0(code, console=False) + log0("=" * 100, console=False) + log0(f"Running Python {sys.version}", console=False) + log0(f"Running PyTorch {torch.__version__}", console=False) + log0( + subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False).stdout, + console=False, + ) + log0("=" * 100, console=False) + + # --- TOKENIZER + VALIDATION METRIC SETUP --- + random.seed(args.seed) + np.random.seed(args.seed) + torch.manual_seed(args.seed) + torch.cuda.manual_seed_all(args.seed) + + if not args.tokenizer_path.endswith(".model"): + raise ValueError(f"Script only setup for SentencePiece .model file: {args.tokenizer_path}") + sp = spm.SentencePieceProcessor(model_file=args.tokenizer_path) + if int(sp.vocab_size()) != args.vocab_size: + raise ValueError( + f"VOCAB_SIZE={args.vocab_size} does not match tokenizer vocab_size={int(sp.vocab_size())}" + ) + dataset_dir = Path(args.data_path).resolve() + actual_train_files = len(list(dataset_dir.glob("fineweb_train_*.bin"))) + val_tokens = load_validation_tokens(args.val_files, args.train_seq_len) + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut = build_sentencepiece_luts( + sp, args.vocab_size, device + ) + log0(f"val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path={args.tokenizer_path}") + log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}") + log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}") + + # --- MODEL + OPTIMIZER SETUP --- + base_model = GPT( + vocab_size=args.vocab_size, + num_layers=args.num_layers, + model_dim=args.model_dim, + num_heads=args.num_heads, + num_kv_heads=args.num_kv_heads, + mlp_mult=args.mlp_mult, + tie_embeddings=args.tie_embeddings, + tied_embed_init_std=args.tied_embed_init_std, + logit_softcap=args.logit_softcap, + rope_base=args.rope_base, + qk_gain_init=args.qk_gain_init, + use_smeargate=args.use_smeargate, + use_bigramhash=args.use_bigramhash, + bigram_vocab_size=args.bigram_vocab_size, + bigram_dim=args.bigram_dim, + use_gated_attention=args.use_gated_attention, + use_value_residual=args.use_value_residual, + use_ln_scale=args.use_ln_scale, + rope_dims=args.rope_dims, + xsa_last_n=args.xsa_last_n, + ve_dim=args.ve_dim, + ve_layers=args.ve_layers, + use_trigramhash=args.use_trigramhash, + trigram_vocab_size=args.trigram_vocab_size, + trigram_dim=args.trigram_dim, + ).to(device).bfloat16() + for module in base_model.modules(): + if isinstance(module, CastedLinear): + module.float() + restore_low_dim_params_to_fp32(base_model) + + compiled_model = torch.compile(base_model, dynamic=False, fullgraph=False) + model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model + + # --- Optimizer param groups --- + # Collect block params + block_named_params = list(base_model.blocks.named_parameters()) + matrix_params = [ + p for name, p in block_named_params + if p.ndim == 2 and not any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) + ] + scalar_params = [ + p for name, p in block_named_params + if p.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) + ] + if base_model.skip_weights.numel() > 0: + scalar_params.append(base_model.skip_weights) + + # SmearGate.gate -> scalar_params + if args.use_smeargate: + scalar_params.append(base_model.smeargate.gate) + + # BigramHash params + token_params_list = [base_model.tok_emb.weight] + muon_extra_params = [] + if args.use_bigramhash: + token_params_list.append(base_model.bigram_embed.embed.weight) + muon_extra_params.append(base_model.bigram_embed.proj.weight) + scalar_params.append(base_model.bigram_embed.bigram_scale) + + # TrigramHash params + if base_model.trigram_embed is not None: + token_params_list.append(base_model.trigram_embed.embed.weight) + muon_extra_params.append(base_model.trigram_embed.proj.weight) + scalar_params.append(base_model.trigram_embed.trigram_scale) + + # VE128 params + if base_model._ve_layer_indices: + token_params_list.append(base_model.ve_shared.embed.weight) + muon_extra_params.append(base_model.ve_shared.proj.weight) + scalar_params.append(base_model.ve_shared.ve_scale) + for p in base_model.ve_layer_scales.parameters(): + scalar_params.append(p) + + # Gated attention attn_gate (weight+bias) -> scalar_params (small params) + for name, p in block_named_params: + if "attn_gate.weight" in name or "attn_gate.bias" in name: + # These are already in block_named_params; ensure they go to scalar + # They have ndim=2 (weight) but contain "attn_gate" which is in CONTROL patterns + pass # Already handled by CONTROL_TENSOR_NAME_PATTERNS check above + + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr + optimizer_tok = torch.optim.Adam( + [{"params": token_params_list, "lr": token_lr, "base_lr": token_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + all_muon_params = matrix_params + muon_extra_params + optimizer_muon = Muon( + all_muon_params, + lr=args.matrix_lr, + momentum=args.muon_momentum, + backend_steps=args.muon_backend_steps, + weight_decay=args.weight_decay, + ) + for group in optimizer_muon.param_groups: + group["base_lr"] = args.matrix_lr + optimizer_scalar = torch.optim.Adam( + [{"params": scalar_params, "lr": args.scalar_lr, "base_lr": args.scalar_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + optimizers: list[torch.optim.Optimizer] = [optimizer_tok, optimizer_muon, optimizer_scalar] + if base_model.lm_head is not None: + optimizer_head = torch.optim.Adam( + [{"params": [base_model.lm_head.weight], "lr": args.head_lr, "base_lr": args.head_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + optimizers.insert(1, optimizer_head) + + n_params = sum(p.numel() for p in base_model.parameters()) + log0(f"model_params:{n_params}") + log0(f"world_size:{world_size} grad_accum_steps:{grad_accum_steps}") + log0("sdp_backends:cudnn=False flash=True mem_efficient=False math=False") + log0(f"attention_mode:gqa num_heads:{args.num_heads} num_kv_heads:{args.num_kv_heads}") + log0( + f"tie_embeddings:{args.tie_embeddings} embed_lr:{token_lr} " + f"head_lr:{args.head_lr if base_model.lm_head is not None else 0.0} " + f"matrix_lr:{args.matrix_lr} scalar_lr:{args.scalar_lr}" + ) + log0( + f"train_batch_tokens:{args.train_batch_tokens} train_seq_len:{args.train_seq_len} " + f"iterations:{args.iterations} warmup_steps:{args.warmup_steps} " + f"max_wallclock_seconds:{args.max_wallclock_seconds:.3f}" + ) + log0(f"seed:{args.seed}") + log0( + f"features: smeargate={args.use_smeargate} bigramhash={args.use_bigramhash} " + f"value_residual={args.use_value_residual} gated_attn={args.use_gated_attention} " + f"rope_dims={args.rope_dims} xsa_last_n={args.xsa_last_n} " + f"ve_dim={args.ve_dim} ve_layers={args.ve_layers} " + f"ln_scale={args.use_ln_scale} ema={args.use_ema}(decay={args.ema_decay}) " + f"swa={args.swa_enabled}(every={args.swa_every},thresh={args.swa_threshold}) " + f"late_qat={args.use_late_qat}(time_frac={args.qat_time_frac}) " + f"weight_decay={args.weight_decay} grad_clip={args.grad_clip_norm}" + ) + + # --- EMA + SWA STATE INIT (fp32 on CPU) --- + ema_state: dict[str, Tensor] = {} + if args.use_ema: + for name, param in base_model.state_dict().items(): + ema_state[name] = param.float().cpu().clone() + + swa_state: dict[str, Tensor] = {} + swa_count = 0 + if args.swa_enabled: + for name, param in base_model.state_dict().items(): + swa_state[name] = torch.zeros_like(param.float().cpu()) + + # --- DATA LOADER & MODEL WARMUP --- + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + + def zero_grad_all() -> None: + for opt in optimizers: + opt.zero_grad(set_to_none=True) + + max_wallclock_ms = 1000.0 * args.max_wallclock_seconds if args.max_wallclock_seconds > 0 else None + + def lr_mul(step: int, elapsed_ms: float) -> float: + if args.warmdown_iters <= 0: + return 1.0 + if max_wallclock_ms is None: + warmdown_start = max(args.iterations - args.warmdown_iters, 0) + return max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0 + step_ms = elapsed_ms / max(step, 1) + warmdown_ms = args.warmdown_iters * step_ms + remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0) + return remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0 + + if args.warmup_steps > 0: + initial_model_state = {name: tensor.detach().cpu().clone() for name, tensor in base_model.state_dict().items()} + initial_optimizer_states = [copy.deepcopy(opt.state_dict()) for opt in optimizers] + model.train() + for warmup_step in range(args.warmup_steps): + zero_grad_all() + for micro_step in range(grad_accum_steps): + if distributed: + model.require_backward_grad_sync = micro_step == grad_accum_steps - 1 + x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + warmup_loss = model(x, y) + (warmup_loss * grad_scale).backward() + for opt in optimizers: + opt.step() + zero_grad_all() + if args.warmup_steps <= 20 or (warmup_step + 1) % 10 == 0 or warmup_step + 1 == args.warmup_steps: + log0(f"warmup_step:{warmup_step + 1}/{args.warmup_steps}") + base_model.load_state_dict(initial_model_state, strict=True) + for opt, state in zip(optimizers, initial_optimizer_states, strict=True): + opt.load_state_dict(state) + zero_grad_all() + if distributed: + model.require_backward_grad_sync = True + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + # Re-init EMA/SWA state after warmup restore + if args.use_ema: + for name, param in base_model.state_dict().items(): + ema_state[name] = param.float().cpu().clone() + if args.swa_enabled: + for name, param in base_model.state_dict().items(): + swa_state[name] = torch.zeros_like(param.float().cpu()) + swa_count = 0 + + # --- MAIN TRAINING LOOP --- + training_time_ms = 0.0 + stop_after_step: int | None = None + torch.cuda.synchronize() + t0 = time.perf_counter() + + step = 0 + while True: + last_step = step == args.iterations or (stop_after_step is not None and step >= stop_after_step) + + should_validate = last_step or (args.val_loss_every > 0 and step % args.val_loss_every == 0) + if should_validate: + torch.cuda.synchronize() + training_time_ms += 1000.0 * (time.perf_counter() - t0) + val_loss, val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + log0( + f"step:{step}/{args.iterations} val_loss:{val_loss:.4f} val_bpb:{val_bpb:.4f} " + f"train_time:{training_time_ms:.0f}ms step_avg:{training_time_ms / max(step, 1):.2f}ms" + ) + torch.cuda.synchronize() + t0 = time.perf_counter() + + if last_step: + if stop_after_step is not None and step < args.iterations: + log0( + f"stopping_early: wallclock_cap train_time:{training_time_ms:.0f}ms " + f"step:{step}/{args.iterations}" + ) + break + + elapsed_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + scale = lr_mul(step, elapsed_ms) + + # Late QAT activation — time-fraction based, NOT LR scale + if args.use_late_qat and not CastedLinear._qat_enabled and max_wallclock_ms is not None: + time_frac_elapsed = elapsed_ms / max_wallclock_ms + if time_frac_elapsed >= (1.0 - args.qat_time_frac): + CastedLinear._qat_enabled = True + log0(f"step:{step} QAT activated (time_frac={time_frac_elapsed:.3f}, scale={scale:.4f})") + + zero_grad_all() + train_loss = torch.zeros((), device=device) + for micro_step in range(grad_accum_steps): + if distributed: + model.require_backward_grad_sync = micro_step == grad_accum_steps - 1 + x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + loss = model(x, y) + train_loss += loss.detach() + (loss * grad_scale).backward() + train_loss /= grad_accum_steps + + frac = min(step / args.muon_momentum_warmup_steps, 1.0) if args.muon_momentum_warmup_steps > 0 else 1.0 + muon_momentum = (1 - frac) * args.muon_momentum_warmup_start + frac * args.muon_momentum + for group in optimizer_muon.param_groups: + group["momentum"] = muon_momentum + + for opt in optimizers: + for group in opt.param_groups: + group["lr"] = group["base_lr"] * scale + + if args.grad_clip_norm > 0: + torch.nn.utils.clip_grad_norm_(base_model.parameters(), args.grad_clip_norm) + for opt in optimizers: + opt.step() + zero_grad_all() + + # EMA update (fp32 on CPU, every step) + if args.use_ema: + decay = args.ema_decay + with torch.no_grad(): + for name, param in base_model.state_dict().items(): + ema_state[name].mul_(decay).add_(param.float().cpu(), alpha=1 - decay) + + # Tight SWA: average EMA weights every swa_every steps when scale < swa_threshold + if args.swa_enabled and args.use_ema and scale < args.swa_threshold: + if (step + 1) % args.swa_every == 0: + swa_count += 1 + with torch.no_grad(): + for name in swa_state: + swa_state[name].add_(ema_state[name]) + + step += 1 + approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + should_log_train = ( + args.train_log_every > 0 + and (step <= 10 or step % args.train_log_every == 0 or stop_after_step is not None) + ) + if should_log_train: + log0( + f"step:{step}/{args.iterations} train_loss:{train_loss.item():.4f} " + f"train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms" + ) + + reached_cap = max_wallclock_ms is not None and approx_training_time_ms >= max_wallclock_ms + if distributed and max_wallclock_ms is not None: + reached_cap_tensor = torch.tensor(int(reached_cap), device=device) + dist.all_reduce(reached_cap_tensor, op=dist.ReduceOp.MAX) + reached_cap = bool(reached_cap_tensor.item()) + if stop_after_step is None and reached_cap: + stop_after_step = step + + log0( + f"peak memory allocated: {torch.cuda.max_memory_allocated() // 1024 // 1024} MiB " + f"reserved: {torch.cuda.max_memory_reserved() // 1024 // 1024} MiB" + ) + + # --- CHOOSE BEST WEIGHTS: raw vs EMA vs SWA --- + # Save raw model weights first (the actual trained weights, always available as fallback) + raw_sd = {name: t.detach().cpu().clone() for name, t in base_model.state_dict().items()} + best_bpb = val_bpb # raw model BPB from last eval + best_source = "raw" + log0(f"raw model val_bpb:{val_bpb:.4f}") + + if args.use_ema and ema_state: + log0("Evaluating EMA weights...") + ema_sd = {name: ema_state[name].to(dtype=raw_sd[name].dtype) for name in raw_sd} + base_model.load_state_dict(ema_sd, strict=True) + torch.cuda.synchronize() + t_ema = time.perf_counter() + ema_val_loss, ema_val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0(f"ema_eval val_bpb:{ema_val_bpb:.4f} eval_time:{1000.0 * (time.perf_counter() - t_ema):.0f}ms") + if ema_val_bpb < best_bpb: + best_bpb = ema_val_bpb + best_source = "ema" + + if args.swa_enabled and swa_count > 0: + log0(f"Evaluating SWA ({swa_count} snapshots)...") + swa_sd = {name: (swa_state[name] / swa_count).to(dtype=raw_sd[name].dtype) for name in raw_sd} + base_model.load_state_dict(swa_sd, strict=True) + torch.cuda.synchronize() + t_swa = time.perf_counter() + swa_val_loss, swa_val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0(f"swa_eval val_bpb:{swa_val_bpb:.4f} eval_time:{1000.0 * (time.perf_counter() - t_swa):.0f}ms") + if swa_val_bpb < best_bpb: + best_bpb = swa_val_bpb + best_source = "swa" + + # Load the best weights for serialization + log0(f"Using {best_source} weights (val_bpb={best_bpb:.4f})") + if best_source == "raw": + base_model.load_state_dict(raw_sd, strict=True) + elif best_source == "ema": + base_model.load_state_dict(ema_sd, strict=True) + # If swa, weights are already loaded + + # --- SERIALIZATION + ROUNDTRIP VALIDATION --- + if master_process: + torch.save(base_model.state_dict(), "final_model.pt") + model_bytes = os.path.getsize("final_model.pt") + code_bytes = len(code.encode("utf-8")) + log0(f"Serialized model: {model_bytes} bytes") + log0(f"Code size: {code_bytes} bytes") + log0(f"Total submission size: {model_bytes + code_bytes} bytes") + + quant_obj, quant_stats = quantize_state_dict_int8(base_model.state_dict()) + quant_buf = io.BytesIO() + torch.save(quant_obj, quant_buf) + quant_raw = quant_buf.getvalue() + quant_blob = _compress(quant_raw) + quant_raw_bytes = len(quant_raw) + if master_process: + with open("final_model.int8.ptz", "wb") as f: + f.write(quant_blob) + quant_file_bytes = os.path.getsize("final_model.int8.ptz") + code_bytes = len(code.encode("utf-8")) + ratio = quant_stats["baseline_tensor_bytes"] / max(quant_stats["int8_payload_bytes"], 1) + log0( + f"Serialized model int5/6+zstd: {quant_file_bytes} bytes " + f"(payload:{quant_stats['int8_payload_bytes']} raw_torch:{quant_raw_bytes} payload_ratio:{ratio:.2f}x)" + ) + log0(f"Total submission size int5/6+zstd: {quant_file_bytes + code_bytes} bytes") + + if distributed: + dist.barrier() + with open("final_model.int8.ptz", "rb") as f: + quant_blob_disk = f.read() + quant_state = torch.load(io.BytesIO(_decompress(quant_blob_disk)), map_location="cpu") + base_model.load_state_dict(dequantize_state_dict_int8(quant_state), strict=True) + torch.cuda.synchronize() + t_qeval = time.perf_counter() + q_val_loss, q_val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0( + f"final_int5_6_zstd_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} " + f"eval_time:{1000.0 * (time.perf_counter() - t_qeval):.0f}ms" + ) + log0(f"final_int5_6_zstd_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}") + + # --- TTT (Test-Time Training) on quantized model --- + if args.use_ttt: + log0(f"Starting TTT: {args.ttt_epochs} epochs, lr={args.ttt_lr}, chunk={args.ttt_chunk_tokens}") + # TTT runs on the dequantized model (simulating eval-time adaptation) + # Need uncompiled model for TTT (backward through compiled model is fine with fullgraph=False) + torch.cuda.synchronize() + t_ttt = time.perf_counter() + ttt_val_loss, ttt_val_bpb = eval_val_ttt( + args, base_model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + log_fn=log0, + ) + torch.cuda.synchronize() + log0( + f"ttt_eval val_loss:{ttt_val_loss:.4f} val_bpb:{ttt_val_bpb:.4f} " + f"eval_time:{1000.0 * (time.perf_counter() - t_ttt):.0f}ms" + ) + log0(f"ttt_eval_exact val_loss:{ttt_val_loss:.8f} val_bpb:{ttt_val_bpb:.8f}") + + if distributed: + dist.destroy_process_group() + + +if __name__ == "__main__": + main() diff --git a/train_gpt.py b/train_gpt.py index 651beb2b8..33ca88a0b 100644 --- a/train_gpt.py +++ b/train_gpt.py @@ -1,11 +1,5 @@ -""" -The `train_gpt.py` and `train_gpt_mlx.py` scripts are intended as good launching-off points for new participants, not SOTA configs. We'll accept PRs that tune, improve, or simplify these scripts without significantly increasing complexity, but competitive submissions should stay in the `/records` folder. - -Hard stop: To keep readable for newcomers, let's make sure `train_gpt.py` and `train_gpt_mlx.py` never are longer than 1500 lines. -""" - +"""SOTA config for OpenAI Parameter Golf. All verified improvements from 500+ PRs.""" from __future__ import annotations - import copy import glob import io @@ -19,6 +13,15 @@ import zlib from pathlib import Path +try: + import zstandard + def _compress(data: bytes) -> bytes: return zstandard.ZstdCompressor(level=22).compress(data) + def _decompress(data: bytes) -> bytes: return zstandard.ZstdDecompressor().decompress(data) +except ImportError: + def _compress(data: bytes) -> bytes: return zlib.compress(data, level=9) + def _decompress(data: bytes) -> bytes: return zlib.decompress(data) + + import numpy as np import sentencepiece as spm import torch @@ -27,17 +30,8 @@ from torch import Tensor, nn from torch.nn.parallel import DistributedDataParallel as DDP -# ----------------------------- -# HYPERPARAMETERS -# ----------------------------- -# Default Simple Baseline run: -# - 9 transformer blocks at width 512 -# - 8 attention heads with 4 KV heads (GQA) and 2x MLP expansion -# - vocab size 1024, sequence length 1024, tied embeddings -# - 524,288 train tokens per step for 20,000 iterations with a ~10 minute cap - +# --- HYPERPARAMETERS (exact values from #518/#505/#493 consensus) --- class Hyperparameters: - # Data paths are shard globs produced by the existing preprocessing pipeline. data_path = os.environ.get("DATA_PATH", "./data/datasets/fineweb10B_sp1024") train_files = os.path.join(data_path, "fineweb_train_*.bin") val_files = os.path.join(data_path, "fineweb_val_*.bin") @@ -45,57 +39,98 @@ class Hyperparameters: run_id = os.environ.get("RUN_ID", str(uuid.uuid4())) seed = int(os.environ.get("SEED", 1337)) - # Validation cadence and batch size. Validation always uses the full fineweb_val split. val_batch_size = int(os.environ.get("VAL_BATCH_SIZE", 524_288)) val_loss_every = int(os.environ.get("VAL_LOSS_EVERY", 1000)) train_log_every = int(os.environ.get("TRAIN_LOG_EVERY", 200)) - # Training length. iterations = int(os.environ.get("ITERATIONS", 20000)) - warmdown_iters = int(os.environ.get("WARMDOWN_ITERS", 1200)) + warmdown_iters = int(os.environ.get("WARMDOWN_ITERS", 300)) warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 524_288)) - train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 1024)) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 2048)) max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) - # Model shape. vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) - num_layers = int(os.environ.get("NUM_LAYERS", 9)) + num_layers = int(os.environ.get("NUM_LAYERS", 11)) num_kv_heads = int(os.environ.get("NUM_KV_HEADS", 4)) model_dim = int(os.environ.get("MODEL_DIM", 512)) num_heads = int(os.environ.get("NUM_HEADS", 8)) - mlp_mult = int(os.environ.get("MLP_MULT", 2)) + mlp_mult = float(os.environ.get("MLP_MULT", 3.0)) tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1"))) rope_base = float(os.environ.get("ROPE_BASE", 10000.0)) logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0)) - # Optimizer hyperparameters. embed_lr = float(os.environ.get("EMBED_LR", 0.6)) head_lr = float(os.environ.get("HEAD_LR", 0.008)) - tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.05)) + tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.035)) tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005)) - matrix_lr = float(os.environ.get("MATRIX_LR", 0.04)) - scalar_lr = float(os.environ.get("SCALAR_LR", 0.04)) - muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.95)) + matrix_lr = float(os.environ.get("MATRIX_LR", 0.03)) + scalar_lr = float(os.environ.get("SCALAR_LR", 0.03)) + muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.99)) muon_backend_steps = int(os.environ.get("MUON_BACKEND_STEPS", 5)) - muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.85)) - muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 500)) + muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.92)) + muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 1500)) beta1 = float(os.environ.get("BETA1", 0.9)) beta2 = float(os.environ.get("BETA2", 0.95)) adam_eps = float(os.environ.get("ADAM_EPS", 1e-8)) - grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.0)) + grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.3)) + weight_decay = float(os.environ.get("WEIGHT_DECAY", 0.04)) + bigram_vocab_size = int(os.environ.get("BIGRAM_VOCAB_SIZE", 10240)) + bigram_dim = int(os.environ.get("BIGRAM_DIM", 128)) + trigram_vocab_size = int(os.environ.get("TRIGRAM_VOCAB_SIZE", 4096)) + trigram_dim = int(os.environ.get("TRIGRAM_DIM", 128)) + use_trigramhash = bool(int(os.environ.get("USE_TRIGRAMHASH", "1"))) + rope_dims = int(os.environ.get("ROPE_DIMS", 16)) + xsa_last_n = int(os.environ.get("XSA_LAST_N", 11)) + ve_dim = int(os.environ.get("VE_DIM", 0)) + ve_layers = os.environ.get("VE_LAYERS", "9,10") + + use_smeargate = bool(int(os.environ.get("USE_SMEARGATE", "1"))) + use_bigramhash = bool(int(os.environ.get("USE_BIGRAMHASH", "1"))) + use_value_residual = bool(int(os.environ.get("USE_VALUE_RESIDUAL", "1"))) + use_gated_attention = bool(int(os.environ.get("USE_GATED_ATTENTION", "1"))) + use_ln_scale = bool(int(os.environ.get("USE_LN_SCALE", "0"))) + use_ema = bool(int(os.environ.get("USE_EMA", "0"))) + ema_decay = float(os.environ.get("EMA_DECAY", 0.997)) + swa_enabled = bool(int(os.environ.get("SWA_ENABLED", "0"))) + swa_every = int(os.environ.get("SWA_EVERY", 50)) + swa_threshold = float(os.environ.get("SWA_THRESHOLD", 0.2)) + use_late_qat = bool(int(os.environ.get("USE_LATE_QAT", "1"))) + qat_time_frac = float(os.environ.get("QAT_TIME_FRAC", 0.15)) + eval_stride = int(os.environ.get("EVAL_STRIDE", 64)) + + # TTT (Test-Time Training) — legal score-first approach + use_ttt = bool(int(os.environ.get("USE_TTT", "0"))) + ttt_lr = float(os.environ.get("TTT_LR", 0.0005)) + ttt_epochs = int(os.environ.get("TTT_EPOCHS", 30)) + ttt_chunk_tokens = int(os.environ.get("TTT_CHUNK_TOKENS", 32768)) + ttt_freeze_embed = bool(int(os.environ.get("TTT_FREEZE_EMBED", "1"))) + ttt_grad_clip = float(os.environ.get("TTT_GRAD_CLIP", 1.0)) + +# --- COMPRESSION CONSTANTS --- +INT6_RANGE = 31 +QUANT_RANGE = INT6_RANGE # int6 uniform for all weights (we have size budget) +_MLP_PATTERNS = ("mlp.fc", "mlp.proj") -# ----------------------------- -# MUON OPTIMIZER -# ----------------------------- -# -# As borrowed from modded-nanogpt -# Background on Muon: https://kellerjordan.github.io/posts/muon/ +CONTROL_TENSOR_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "CONTROL_TENSOR_NAME_PATTERNS", + "attn_scale,mlp_scale,resid_mix,q_gain,skip_weight,skip_weights," + "vr_lambda,attn_gate,ve_scale,bigram_scale,trigram_scale", + ).split(",") + if pattern +) +INT8_KEEP_FLOAT_FP32_NAME_PATTERNS = CONTROL_TENSOR_NAME_PATTERNS +INT8_KEEP_FLOAT_MAX_NUMEL = 65_536 +# tok_emb.weight (524K params) kept FP16 via explicit name match below +_FP16_PASSTHROUGH_NAMES = ("tok_emb.weight",) +INT8_KEEP_FLOAT_STORE_DTYPE = torch.float16 +INT8_PER_ROW_SCALE_DTYPE = torch.float16 +# --- MUON OPTIMIZER --- def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) -> Tensor: - # Orthogonalize a 2D update matrix with a fast Newton-Schulz iteration. - # Muon uses this to normalize matrix-shaped gradients before applying them. a, b, c = (3.4445, -4.7750, 2.0315) X = G.bfloat16() X /= X.norm() + eps @@ -110,10 +145,12 @@ def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) - class Muon(torch.optim.Optimizer): - def __init__(self, params, lr: float, momentum: float, backend_steps: int, nesterov: bool = True): + def __init__(self, params, lr: float, momentum: float, backend_steps: int, + nesterov: bool = True, weight_decay: float = 0.0): super().__init__( params, - dict(lr=lr, momentum=momentum, backend_steps=backend_steps, nesterov=nesterov), + dict(lr=lr, momentum=momentum, backend_steps=backend_steps, + nesterov=nesterov, weight_decay=weight_decay), ) @torch.no_grad() @@ -135,6 +172,7 @@ def step(self, closure=None): momentum = group["momentum"] backend_steps = group["backend_steps"] nesterov = group["nesterov"] + wd = group.get("weight_decay", 0.0) total_params = sum(int(p.numel()) for p in params) updates_flat = torch.zeros(total_params, device=params[0].device, dtype=torch.bfloat16) @@ -151,7 +189,6 @@ def step(self, closure=None): if nesterov: g = g.add(buf, alpha=momentum) g = zeropower_via_newtonschulz5(g, steps=backend_steps) - # Scale correction from Muon reference implementations. g *= max(1, g.size(0) / g.size(1)) ** 0.5 updates_flat[curr : curr + p.numel()] = g.reshape(-1) curr += p.numel() @@ -161,6 +198,8 @@ def step(self, closure=None): curr = 0 for p in params: + if wd > 0.0: + p.data.mul_(1.0 - lr * wd) g = updates_flat[curr : curr + p.numel()].view_as(p).to(dtype=p.dtype) p.add_(g, alpha=-lr) curr += p.numel() @@ -168,15 +207,7 @@ def step(self, closure=None): return loss -# ----------------------------- -# TOKENIZER-AGNOSTIC EVALUATION SETUP -# ----------------------------- -# -# It's common for small models have a large fraction of their parameters be embeddings, since the 2 * d_model * d_vocab vectors can be gigantic. -# Instead of locking the tokenizer, we let you bring your own and calculate our validation metrics on the average compression of the validation set. -# We calculate BPB (bits-per-byte) instead of validation loss, so we need methods to count the number of bits per token in the tokenizer. -# Note: Submissions that edit the tokenizer will be examined more carefully, since screwing this up might unjustly improve your score. - +# --- TOKENIZER-AGNOSTIC EVALUATION --- def build_sentencepiece_luts( sp: spm.SentencePieceProcessor, vocab_size: int, device: torch.device ) -> tuple[Tensor, Tensor, Tensor]: @@ -193,7 +224,7 @@ def build_sentencepiece_luts( base_bytes_np[token_id] = 1 continue piece = sp.id_to_piece(token_id) - if piece.startswith("▁"): + if piece.startswith("\u2581"): has_leading_space_np[token_id] = True piece = piece[1:] base_bytes_np[token_id] = len(piece.encode("utf-8")) @@ -208,7 +239,6 @@ def load_validation_tokens(pattern: str, seq_len: int) -> Tensor: files = [Path(p) for p in sorted(glob.glob(pattern))] if not files: raise FileNotFoundError(f"No files found for pattern: {pattern}") - # The export pipeline writes the fixed first-50k-doc validation set to fineweb_val_*. tokens = torch.cat([load_data_shard(file) for file in files]).contiguous() usable = ((tokens.numel() - 1) // seq_len) * seq_len if usable <= 0: @@ -222,49 +252,60 @@ def eval_val( rank: int, world_size: int, device: torch.device, - grad_accum_steps: int, val_tokens: Tensor, base_bytes_lut: Tensor, has_leading_space_lut: Tensor, is_boundary_token_lut: Tensor, ) -> tuple[float, float]: - # Validation computes two metrics: - # - val_loss: token cross-entropy (natural log) - # - val_bpb: tokenizer-agnostic compression metric used by the challenge - local_batch_tokens = args.val_batch_size // (world_size * grad_accum_steps) - if local_batch_tokens < args.train_seq_len: - raise ValueError( - "VAL_BATCH_SIZE must provide at least one sequence per rank; " - f"got VAL_BATCH_SIZE={args.val_batch_size}, WORLD_SIZE={world_size}, " - f"GRAD_ACCUM_STEPS={grad_accum_steps}, TRAIN_SEQ_LEN={args.train_seq_len}" - ) - local_batch_seqs = local_batch_tokens // args.train_seq_len - total_seqs = (val_tokens.numel() - 1) // args.train_seq_len - seq_start = (total_seqs * rank) // world_size - seq_end = (total_seqs * (rank + 1)) // world_size + """Sliding window eval with stride=eval_stride, batched for throughput. + NO document isolation (hurts at stride=64, confirmed in issue #140).""" + seq_len = args.train_seq_len + stride = args.eval_stride + windows_per_batch = 32 + total_tokens = val_tokens.numel() - 1 + + starts = list(range(0, total_tokens - seq_len + 1, stride)) + my_starts = starts[rank::world_size] + val_loss_sum = torch.zeros((), device=device, dtype=torch.float64) val_token_count = torch.zeros((), device=device, dtype=torch.float64) val_byte_count = torch.zeros((), device=device, dtype=torch.float64) model.eval() with torch.inference_mode(): - for batch_seq_start in range(seq_start, seq_end, local_batch_seqs): - batch_seq_end = min(batch_seq_start + local_batch_seqs, seq_end) - raw_start = batch_seq_start * args.train_seq_len - raw_end = batch_seq_end * args.train_seq_len + 1 - local = val_tokens[raw_start:raw_end].to(device=device, dtype=torch.int64, non_blocking=True) - x = local[:-1].reshape(-1, args.train_seq_len) - y = local[1:].reshape(-1, args.train_seq_len) + for batch_start in range(0, len(my_starts), windows_per_batch): + batch_starts = my_starts[batch_start : batch_start + windows_per_batch] + x_list = [] + y_list = [] + for s in batch_starts: + chunk = val_tokens[s : s + seq_len + 1].to(dtype=torch.int64) + x_list.append(chunk[:-1]) + y_list.append(chunk[1:]) + x = torch.stack(x_list).to(device=device, non_blocking=True) + y = torch.stack(y_list).to(device=device, non_blocking=True) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): - batch_loss = model(x, y).detach() - batch_token_count = float(y.numel()) - val_loss_sum += batch_loss.to(torch.float64) * batch_token_count - val_token_count += batch_token_count - prev_ids = x.reshape(-1) - tgt_ids = y.reshape(-1) - token_bytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) - token_bytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) - val_byte_count += token_bytes.to(torch.float64).sum() + logits = model(x) # target_ids=None -> returns logits + + for i, s in enumerate(batch_starts): + if s == 0: + score_start = 0 + score_len = min(seq_len, stride) + else: + score_start = seq_len - stride + score_len = stride + + window_logits = logits[i, score_start : score_start + score_len] + window_targets = y[i, score_start : score_start + score_len] + loss = F.cross_entropy(window_logits.float(), window_targets, reduction="sum") + val_loss_sum += loss.to(torch.float64) + val_token_count += score_len + + prev_ids = x[i, score_start : score_start + score_len] + tgt_ids = window_targets + token_bytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) + token_bytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) + val_byte_count += token_bytes.to(torch.float64).sum() if dist.is_available() and dist.is_initialized(): dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM) @@ -277,39 +318,139 @@ def eval_val( model.train() return float(val_loss.item()), float(bits_per_token * tokens_per_byte) -# ----------------------------- -# POST-TRAINING QUANTIZATION -# ----------------------------- -# -# It's silly to export our model, which is trained in bf16 and fp32, at that same precision. -# Instead, we get approximately the same model (with a small hit) by quantizing the model to int8 & zlib compressing. -# We can then decompress the model and run in higher precision for evaluation, after closing in under the size limit. -CONTROL_TENSOR_NAME_PATTERNS = tuple( - pattern - for pattern in os.environ.get( - "CONTROL_TENSOR_NAME_PATTERNS", - "attn_scale,attn_scales,mlp_scale,mlp_scales,resid_mix,resid_mixes,q_gain,skip_weight,skip_weights", - ).split(",") - if pattern -) -INT8_KEEP_FLOAT_FP32_NAME_PATTERNS = tuple( - pattern - for pattern in os.environ.get( - "INT8_KEEP_FLOAT_FP32_NAME_PATTERNS", - ",".join(CONTROL_TENSOR_NAME_PATTERNS), - ).split(",") - if pattern -) -INT8_KEEP_FLOAT_MAX_NUMEL = 65_536 -INT8_KEEP_FLOAT_STORE_DTYPE = torch.float16 -INT8_PER_ROW_SCALE_DTYPE = torch.float16 -INT8_CLIP_PERCENTILE = 99.99984 -INT8_CLIP_Q = INT8_CLIP_PERCENTILE / 100.0 +def eval_val_ttt( + args: Hyperparameters, + base_model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + val_tokens: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, + log_fn=None, +) -> tuple[float, float]: + """Legal score-first TTT with frozen base + per-layer LR (from #518/#481). + Freezes all params except block weights. Scores each chunk, then trains on it. + Multi-epoch: epochs 0..N-2 train only; epoch N-1 scores then trains.""" + seq_len = args.train_seq_len + chunk_size = args.ttt_chunk_tokens + total_tokens = val_tokens.numel() - 1 + if log_fn is None: + log_fn = lambda msg: None + + # Save original state dict for restoration + orig_sd = {k: v.detach().cpu().clone() for k, v in base_model.state_dict().items()} + + # Freeze embeddings, only train block params (from #518: freeze tok_emb, bigram, trigram) + for name, p in base_model.named_parameters(): + p.requires_grad_(False) + + # Unfreeze block params with per-layer LR groups (from #518) + proj_params, fc_params, other_block_params = [], [], [] + for name, p in base_model.named_parameters(): + if "blocks." not in name: + continue # Skip embeddings, skip_weights, etc. + p.requires_grad_(True) + if "mlp.proj" in name: + proj_params.append(p) + elif "mlp.fc" in name: + fc_params.append(p) + else: + other_block_params.append(p) + + ttt_lr = args.ttt_lr + ttt_opt = torch.optim.AdamW([ + {"params": proj_params, "lr": ttt_lr * 3.0, "initial_lr": ttt_lr * 3.0}, + {"params": fc_params, "lr": ttt_lr * 0.5, "initial_lr": ttt_lr * 0.5}, + {"params": other_block_params, "lr": ttt_lr, "initial_lr": ttt_lr}, + ], weight_decay=0.0) + + # Build chunk list — each chunk is chunk_size tokens, scored as a single window + chunk_starts = list(range(0, total_tokens - seq_len + 1, chunk_size)) + my_chunks = chunk_starts[rank::world_size] + n_chunks = len(my_chunks) + total_steps = n_chunks * args.ttt_epochs + log_fn(f"TTT: {n_chunks} chunks, {args.ttt_epochs} epochs, {total_steps} total steps") + val_loss_sum = torch.zeros((), device=device, dtype=torch.float64) + val_token_count = torch.zeros((), device=device, dtype=torch.float64) + val_byte_count = torch.zeros((), device=device, dtype=torch.float64) + + step = 0 + for epoch in range(args.ttt_epochs): + is_scoring_epoch = (epoch == args.ttt_epochs - 1) + if is_scoring_epoch: + val_loss_sum.zero_() + val_token_count.zero_() + val_byte_count.zero_() + + for ci, c_start in enumerate(my_chunks): + c_end = min(c_start + seq_len + 1, total_tokens + 1) + chunk = val_tokens[c_start:c_end].to(device=device, dtype=torch.int64) + if chunk.numel() < 2: + continue + x = chunk[:-1].unsqueeze(0) + y = chunk[1:].unsqueeze(0) + actual_len = x.size(1) + + # SCORE this chunk (only on last epoch) + if is_scoring_epoch: + base_model.eval() + with torch.inference_mode(), torch.autocast(device_type="cuda", dtype=torch.bfloat16): + logits = base_model(x) + loss_val = F.cross_entropy(logits[0, :actual_len].float(), y[0, :actual_len], reduction="sum") + val_loss_sum += loss_val.to(torch.float64) + val_token_count += actual_len + prev_ids = x[0, :actual_len] + tgt_ids = y[0, :actual_len] + tbytes = base_bytes_lut[tgt_ids].to(torch.int16) + tbytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(torch.int16) + val_byte_count += tbytes.to(torch.float64).sum() + + # TRAIN on this chunk (adapt for future chunks) + base_model.train() + # Cosine LR + progress = step / max(total_steps, 1) + cos_mul = 0.5 * (1.0 + math.cos(math.pi * progress)) + for g in ttt_opt.param_groups: + g["lr"] = g["initial_lr"] * cos_mul + + ttt_opt.zero_grad() + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + loss = base_model(x, y) + loss.backward() + if args.ttt_grad_clip > 0: + torch.nn.utils.clip_grad_norm_( + [p for p in base_model.parameters() if p.requires_grad], args.ttt_grad_clip) + ttt_opt.step() + step += 1 + + if is_scoring_epoch: + log_fn(f"TTT epoch {epoch}: scored {int(val_token_count.item())} tokens") + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(val_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(val_byte_count, op=dist.ReduceOp.SUM) + + val_loss = val_loss_sum / val_token_count + bpt = val_loss.item() / math.log(2.0) + tpb = val_token_count.item() / val_byte_count.item() + + # Restore original weights + base_model.load_state_dict(orig_sd, strict=True) + for p in base_model.parameters(): + p.requires_grad_(True) + return float(val_loss.item()), float(bpt * tpb) + + +# --- POST-TRAINING QUANTIZATION (Mixed Int5/Int6 with GPTQ-lite) --- def tensor_nbytes(t: Tensor) -> int: return int(t.numel()) * int(t.element_size()) + def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, str]) -> Tensor: if any(pattern in name for pattern in INT8_KEEP_FLOAT_FP32_NAME_PATTERNS): return t.float().contiguous() @@ -318,33 +459,48 @@ def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, s return t.to(dtype=INT8_KEEP_FLOAT_STORE_DTYPE).contiguous() return t -def quantize_float_tensor(t: Tensor) -> tuple[Tensor, Tensor]: + +def quantize_float_tensor(t: Tensor, name: str = "") -> tuple[Tensor, Tensor]: + """Quantize a float tensor to int6 with GPTQ-lite (5-percentile search).""" + qrange = QUANT_RANGE # int6 uniform for all weights + t32 = t.float() if t32.ndim == 2: - # Matrices get one scale per row, which usually tracks output-channel - # ranges much better than a single tensor-wide scale. - clip_abs = ( - torch.quantile(t32.abs(), INT8_CLIP_Q, dim=1) - if t32.numel() - else torch.empty((t32.shape[0],), dtype=torch.float32) - ) - clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) - scale = (clip_abs / 127.0).clamp_min(1.0 / 127.0) - q = torch.clamp(torch.round(clipped / scale[:, None]), -127, 127).to(torch.int8).contiguous() - return q, scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous() - - # Vectors / scalars use a simpler per-tensor scale. - clip_abs = float(torch.quantile(t32.abs().flatten(), INT8_CLIP_Q).item()) if t32.numel() else 0.0 - scale = torch.tensor(clip_abs / 127.0 if clip_abs > 0 else 1.0, dtype=torch.float32) - q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -127, 127).to(torch.int8).contiguous() + _CLIP_QS = [0.9990, 0.9995, 0.9999, 0.99999, 1.0] + best_q = None + best_scale = None + best_mse = None + for cq in _CLIP_QS: + clip_abs = ( + torch.quantile(t32.abs(), cq, dim=1) + if t32.numel() + else torch.empty((t32.shape[0],), dtype=torch.float32) + ) + clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) + s = (clip_abs / float(qrange)).clamp_min(1.0 / float(qrange)) + q = torch.clamp(torch.round(clipped / s[:, None]), -qrange, qrange) + recon = q * s[:, None] + mse = (t32 - recon).square().sum(dim=1) + if best_mse is None: + best_mse = mse + best_q = q + best_scale = s + else: + improved = mse < best_mse + if improved.any(): + best_mse = torch.where(improved, mse, best_mse) + best_q = torch.where(improved[:, None], q, best_q) + best_scale = torch.where(improved, s, best_scale) + return best_q.to(torch.int8).contiguous(), best_scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous() + + clip_q = 0.9999984 + clip_abs = float(torch.quantile(t32.abs().flatten(), clip_q).item()) if t32.numel() else 0.0 + scale = torch.tensor(clip_abs / float(qrange) if clip_abs > 0 else 1.0, dtype=torch.float32) + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -qrange, qrange).to(torch.int8).contiguous() return q, scale + def quantize_state_dict_int8(state_dict: dict[str, Tensor]): - # Single supported clean-script export format: - # - per-row int8 for 2D float tensors - # - per-tensor int8 for other float tensors - # - exact passthrough for non-floats - # - passthrough for small float tensors, stored as fp16 to save bytes quantized: dict[str, Tensor] = {} scales: dict[str, Tensor] = {} dtypes: dict[str, str] = {} @@ -352,7 +508,8 @@ def quantize_state_dict_int8(state_dict: dict[str, Tensor]): passthrough_orig_dtypes: dict[str, str] = {} qmeta: dict[str, dict[str, object]] = {} stats = dict.fromkeys( - ("param_count", "num_tensors", "num_float_tensors", "num_nonfloat_tensors", "baseline_tensor_bytes", "int8_payload_bytes"), + ("param_count", "num_tensors", "num_float_tensors", "num_nonfloat_tensors", + "baseline_tensor_bytes", "int8_payload_bytes"), 0, ) @@ -368,16 +525,14 @@ def quantize_state_dict_int8(state_dict: dict[str, Tensor]): stats["int8_payload_bytes"] += tensor_nbytes(t) continue - # Small float tensors are cheap enough to keep directly. We still downcast - # fp32/bf16 passthrough tensors to fp16 so metadata does not dominate size. - if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL: + if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL or any(p in name for p in _FP16_PASSTHROUGH_NAMES): kept = keep_float_tensor(name, t, passthrough_orig_dtypes) passthrough[name] = kept stats["int8_payload_bytes"] += tensor_nbytes(kept) continue stats["num_float_tensors"] += 1 - q, s = quantize_float_tensor(t) + q, s = quantize_float_tensor(t, name=name) if s.ndim > 0: qmeta[name] = {"scheme": "per_row", "axis": 0} quantized[name] = q @@ -398,6 +553,7 @@ def quantize_state_dict_int8(state_dict: dict[str, Tensor]): obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes return obj, stats + def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]: out: dict[str, Tensor] = {} qmeta = obj.get("qmeta", {}) @@ -407,13 +563,11 @@ def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]: s = obj["scales"][name] if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0: s = s.to(dtype=torch.float32) - # Broadcast the saved row scale back across trailing dimensions. out[name] = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous() else: scale = float(s.item()) out[name] = (q.float() * scale).to(dtype=dtype).contiguous() for name, t in obj["passthrough"].items(): - # Restore small tensors, undoing the temporary fp16 storage cast if needed. out_t = t.detach().to("cpu").contiguous() orig_dtype = passthrough_orig_dtypes.get(name) if isinstance(orig_dtype, str): @@ -422,15 +576,11 @@ def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]: return out -# ----------------------------- -# DATA LOADING -# ----------------------------- - +# --- DATA LOADING --- def load_data_shard(file: Path) -> Tensor: header_bytes = 256 * np.dtype(" Tensor: class TokenStream: - # Reads shards sequentially and wraps around forever. The training loop therefore - # has deterministic, simple streaming behavior with no sampling or workers. def __init__(self, pattern: str): self.files = [Path(p) for p in sorted(glob.glob(pattern))] if not self.files: @@ -475,8 +623,6 @@ def take(self, n: int) -> Tensor: class DistributedTokenLoader: - # Each call consumes a contiguous chunk from the shared token stream, then slices out - # one disjoint span per rank. The extra "+1" token lets us build (x, y) by shifting. def __init__(self, pattern: str, rank: int, world_size: int, device: torch.device): self.rank = rank self.world_size = world_size @@ -493,10 +639,8 @@ def next_batch(self, global_tokens: int, seq_len: int, grad_accum_steps: int) -> y = local[1:].reshape(-1, seq_len) return x.to(self.device, non_blocking=True), y.to(self.device, non_blocking=True) -# ----------------------------- -# TRANSFORMER MODULES -# ----------------------------- +# --- TRANSFORMER MODULES --- class RMSNorm(nn.Module): def __init__(self, eps: float | None = None): super().__init__() @@ -507,14 +651,25 @@ def forward(self, x: Tensor) -> Tensor: class CastedLinear(nn.Linear): - # Keep weights in fp32 for optimizer/state quality, cast at matmul time for bf16 compute. + _qat_enabled: bool = False # CLASS-level flag + + def __init__(self, *args, **kwargs): + super().__init__(*args, **kwargs) + def forward(self, x: Tensor) -> Tensor: + w = self.weight.to(x.dtype) + if CastedLinear._qat_enabled and self.training and w.ndim == 2: + with torch.no_grad(): + w32 = self.weight.float() + row_max = w32.abs().amax(dim=1) + scale = (row_max / 31.0).clamp_min(1.0 / 31.0) # Always int6 range for QAT + w_q = (torch.clamp(torch.round(w32 / scale[:, None]), -32, 31) * scale[:, None]).to(x.dtype) + w = w + (w_q - w).detach() # STE bias = self.bias.to(x.dtype) if self.bias is not None else None - return F.linear(x, self.weight.to(x.dtype), bias) + return F.linear(x, w, bias) def restore_low_dim_params_to_fp32(module: nn.Module) -> None: - # Keep small/control parameters in fp32 even when the model body runs in bf16. with torch.no_grad(): for name, param in module.named_parameters(): if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32: @@ -522,10 +677,11 @@ def restore_low_dim_params_to_fp32(module: nn.Module) -> None: class Rotary(nn.Module): - # Caches cos/sin tables per sequence length on the current device. - def __init__(self, dim: int, base: float = 10000.0): + def __init__(self, dim: int, base: float = 10000.0, rope_dims: int = 0): super().__init__() - inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim)) + self.rope_dims = rope_dims if rope_dims > 0 else dim + rd = self.rope_dims + inv_freq = 1.0 / (base ** (torch.arange(0, rd, 2, dtype=torch.float32) / rd)) self.register_buffer("inv_freq", inv_freq, persistent=False) self._seq_len_cached = 0 self._cos_cached: Tensor | None = None @@ -546,12 +702,88 @@ def forward(self, seq_len: int, device: torch.device, dtype: torch.dtype) -> tup return self._cos_cached.to(dtype=dtype), self._sin_cached.to(dtype=dtype) -def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor) -> Tensor: +def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor, rope_dims: int = 0) -> Tensor: + if rope_dims > 0 and rope_dims < x.size(-1): + x_rope, x_pass = x[..., :rope_dims], x[..., rope_dims:] + half = rope_dims // 2 + x1, x2 = x_rope[..., :half], x_rope[..., half:] + x_rope = torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + return torch.cat((x_rope, x_pass), dim=-1) half = x.size(-1) // 2 x1, x2 = x[..., :half], x[..., half:] return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) +class SmearGate(nn.Module): + def __init__(self, dim: int): + super().__init__() + self.gate = nn.Parameter(torch.zeros(dim, dtype=torch.float32)) + + def forward(self, x: Tensor) -> Tensor: + g = torch.sigmoid(self.gate.to(dtype=x.dtype))[None, None, :] + x_prev = torch.cat([torch.zeros_like(x[:, :1]), x[:, :-1]], dim=1) + return (1 - g) * x + g * x_prev + + +class BigramHashEmbedding(nn.Module): + def __init__(self, vocab_size: int, dim: int, model_dim: int): + super().__init__() + self.vocab_size = vocab_size # 2048 + self.embed = nn.Embedding(vocab_size, dim) # dim=128 + nn.init.zeros_(self.embed.weight) # zeros init + self.proj = CastedLinear(dim, model_dim, bias=False) + nn.init.zeros_(self.proj.weight) # zeros init + self.bigram_scale = nn.Parameter(torch.tensor(0.05, dtype=torch.float32)) + + def forward(self, input_ids: Tensor) -> Tensor: + t = input_ids.to(torch.int32) + mod = self.vocab_size - 1 # 2047 + out = torch.empty_like(t) + out[..., 0] = mod # first position has no previous token + out[..., 1:] = torch.bitwise_xor(36313 * t[..., 1:], 27191 * t[..., :-1]) % mod + h = self.embed(out.long()) + return self.proj(h) * self.bigram_scale.to(dtype=h.dtype) + + +class TrigramHashEmbedding(nn.Module): + """Hash consecutive token trigrams. From PR #486: -0.023 BPB combined with VRL.""" + def __init__(self, vocab_size: int, dim: int, model_dim: int): + super().__init__() + self.vocab_size = vocab_size # 4096 + self.embed = nn.Embedding(vocab_size, dim) + nn.init.zeros_(self.embed.weight) + self.proj = CastedLinear(dim, model_dim, bias=False) + nn.init.zeros_(self.proj.weight) + self.trigram_scale = nn.Parameter(torch.tensor(0.03, dtype=torch.float32)) + + def forward(self, input_ids: Tensor) -> Tensor: + t = input_ids.to(torch.int32) + mod = self.vocab_size - 1 + out = torch.empty_like(t) + out[..., 0] = mod + out[..., 1] = mod + out[..., 2:] = torch.bitwise_xor( + torch.bitwise_xor(36313 * t[..., 2:], 27191 * t[..., 1:-1]), + 51497 * t[..., :-2], + ) % mod + h = self.embed(out.long()) + return self.proj(h) * self.trigram_scale.to(dtype=h.dtype) + + +class ValueEmbedding(nn.Module): + def __init__(self, vocab_size: int, ve_dim: int, kv_dim: int): + super().__init__() + self.embed = nn.Embedding(vocab_size, ve_dim) # 1024 x 128 + nn.init.normal_(self.embed.weight, std=0.01) + self.proj = CastedLinear(ve_dim, kv_dim, bias=False) # 128 -> kv_dim + nn.init.zeros_(self.proj.weight) + self.ve_scale = nn.Parameter(torch.tensor(0.1, dtype=torch.float32)) + + def forward(self, token_ids: Tensor) -> Tensor: + h = self.proj(self.embed(token_ids)) + return h * self.ve_scale.to(dtype=h.dtype) + + class CausalSelfAttention(nn.Module): def __init__( self, @@ -560,6 +792,11 @@ def __init__( num_kv_heads: int, rope_base: float, qk_gain_init: float, + layer_idx: int = 0, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_xsa: bool = False, + rope_dims: int = 0, ): super().__init__() if dim % num_heads != 0: @@ -578,42 +815,96 @@ def __init__( self.proj = CastedLinear(dim, dim, bias=False) self.proj._zero_init = True self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) - self.rotary = Rotary(self.head_dim, base=rope_base) - def forward(self, x: Tensor) -> Tensor: + # Partial RoPE + self._rope_dims = rope_dims + self.rotary = Rotary(dim // num_heads, base=rope_base, rope_dims=rope_dims) + + # Gated Attention (nn.Linear with bias, from #490/#413) + self._gated_attention = use_gated_attention + if use_gated_attention: + self.attn_gate = nn.Linear(dim, num_heads, bias=True) + nn.init.zeros_(self.attn_gate.weight) + nn.init.constant_(self.attn_gate.bias, 4.0) # near-open init + + # Value Residual (only on layers > 0, from #486/#490) + self._value_residual = use_value_residual and layer_idx > 0 + if self._value_residual: + self.vr_lambda = nn.Parameter(torch.tensor([0.5, 0.5], dtype=torch.float32)) + + # XSA (Exclusive Self-Attention, from #518/#505) + self.use_xsa = use_xsa + + def _xsa_efficient(self, y: Tensor, v: Tensor) -> Tensor: + """Subtract self-value projection via GQA-aware reshape.""" + B, T, H, D = y.shape + Hkv = v.size(-2) + group = H // Hkv + y_g = y.reshape(B, T, Hkv, group, D) + vn = F.normalize(v, dim=-1).unsqueeze(-2) + proj = (y_g * vn).sum(dim=-1, keepdim=True) * vn + return (y_g - proj).reshape(B, T, H, D) + + def forward(self, x: Tensor, v0: Tensor | None = None, v_embed: Tensor | None = None) -> tuple[Tensor, Tensor]: bsz, seqlen, dim = x.shape q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2) k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) - v = self.c_v(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + + # Compute v BEFORE reshape to heads so we can add v_embed + v_flat = self.c_v(x) # [B, T, kv_dim] + if v_embed is not None: + v_flat = v_flat + v_embed # Add VE128 BEFORE reshape + v = v_flat.reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + q = F.rms_norm(q, (q.size(-1),)) k = F.rms_norm(k, (k.size(-1),)) + + # Apply RoPE (partial or full via rope_dims) cos, sin = self.rotary(seqlen, x.device, q.dtype) - q = apply_rotary_emb(q, cos, sin) - k = apply_rotary_emb(k, cos, sin) + q = apply_rotary_emb(q, cos, sin, rope_dims=self._rope_dims) + k = apply_rotary_emb(k, cos, sin, rope_dims=self._rope_dims) + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + + # Value Residual: blend layer-0 V into current + raw_v = v # always return for caching + if self._value_residual and v0 is not None and hasattr(self, 'vr_lambda'): + lam = self.vr_lambda.to(dtype=v.dtype) + v = lam[0] * v0 + lam[1] * v + y = F.scaled_dot_product_attention( - q, - k, - v, - attn_mask=None, - is_causal=True, + q, k, v, attn_mask=None, is_causal=True, enable_gqa=(self.num_kv_heads != self.num_heads), ) - y = y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim) - return self.proj(y) + + # y: [B, H, T, D] -> [B, T, H, D] for XSA and gated attention + y = y.transpose(1, 2) # [B, T, H, D] + + # XSA: Exclusive Self-Attention + if self.use_xsa: + # v needs to be [B, T, Hkv, D] for XSA + v_for_xsa = raw_v.transpose(1, 2) # [B, T, Hkv, D] + y = self._xsa_efficient(y, v_for_xsa) + + # Gated attention (applied to [B, T, H, D]) + if self._gated_attention: + gate = torch.sigmoid(self.attn_gate(x)) # [B, T, H] + y = y * gate.unsqueeze(-1) + + y = y.contiguous().reshape(bsz, seqlen, dim) + return self.proj(y), raw_v class MLP(nn.Module): - # relu^2 MLP from the original modded-nanogpt setup - def __init__(self, dim: int, mlp_mult: int): + def __init__(self, dim: int, mlp_mult: float): super().__init__() - hidden = mlp_mult * dim + hidden = int(mlp_mult * dim) self.fc = CastedLinear(dim, hidden, bias=False) self.proj = CastedLinear(hidden, dim, bias=False) self.proj._zero_init = True def forward(self, x: Tensor) -> Tensor: - x = torch.relu(self.fc(x)) + x = F.leaky_relu(self.fc(x), negative_slope=0.5) return self.proj(x.square()) @@ -623,26 +914,51 @@ def __init__( dim: int, num_heads: int, num_kv_heads: int, - mlp_mult: int, + mlp_mult: float, rope_base: float, qk_gain_init: float, + layer_idx: int = 0, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_xsa: bool = False, + rope_dims: int = 0, + use_ln_scale: bool = False, ): super().__init__() + self.layer_idx = layer_idx + self.use_ln_scale = use_ln_scale + self.ln_scale_factor = 1.0 / math.sqrt(layer_idx + 1) if use_ln_scale else 1.0 self.attn_norm = RMSNorm() self.mlp_norm = RMSNorm() - self.attn = CausalSelfAttention(dim, num_heads, num_kv_heads, rope_base, qk_gain_init) + self.attn = CausalSelfAttention( + dim, num_heads, num_kv_heads, rope_base, qk_gain_init, + layer_idx=layer_idx, + use_gated_attention=use_gated_attention, + use_value_residual=use_value_residual, + use_xsa=use_xsa, + rope_dims=rope_dims, + ) self.mlp = MLP(dim, mlp_mult) self.attn_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) self.mlp_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) self.resid_mix = nn.Parameter(torch.stack((torch.ones(dim), torch.zeros(dim))).float()) - def forward(self, x: Tensor, x0: Tensor) -> Tensor: + def forward(self, x: Tensor, x0: Tensor, v0: Tensor | None = None, v_embed: Tensor | None = None) -> tuple[Tensor, Tensor]: mix = self.resid_mix.to(dtype=x.dtype) x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 - attn_out = self.attn(self.attn_norm(x)) - x = x + self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out - x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x)) - return x + # LN Scale: multiply norm output by factor + normed = self.attn_norm(x) + if self.use_ln_scale: + normed = normed * self.ln_scale_factor + attn_out, v_out = self.attn(normed, v0, v_embed=v_embed) + scaled_attn = self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out + x = x + scaled_attn + mlp_normed = self.mlp_norm(x) + if self.use_ln_scale: + mlp_normed = mlp_normed * self.ln_scale_factor + mlp_out = self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(mlp_normed) + x = x + mlp_out + return x, v_out class GPT(nn.Module): @@ -653,12 +969,26 @@ def __init__( model_dim: int, num_heads: int, num_kv_heads: int, - mlp_mult: int, + mlp_mult: float, tie_embeddings: bool, tied_embed_init_std: float, logit_softcap: float, rope_base: float, qk_gain_init: float, + use_smeargate: bool = False, + use_bigramhash: bool = False, + bigram_vocab_size: int = 2048, + bigram_dim: int = 128, + use_gated_attention: bool = False, + use_value_residual: bool = False, + use_ln_scale: bool = False, + rope_dims: int = 0, + xsa_last_n: int = 0, + ve_dim: int = 0, + ve_layers: str = "", + use_trigramhash: bool = False, + trigram_vocab_size: int = 4096, + trigram_dim: int = 128, ): super().__init__() if logit_softcap <= 0.0: @@ -666,20 +996,50 @@ def __init__( self.tie_embeddings = tie_embeddings self.tied_embed_init_std = tied_embed_init_std self.logit_softcap = logit_softcap + self.use_smeargate = use_smeargate + self.use_bigramhash = use_bigramhash + self.use_value_residual = use_value_residual self.tok_emb = nn.Embedding(vocab_size, model_dim) self.num_encoder_layers = num_layers // 2 self.num_decoder_layers = num_layers - self.num_encoder_layers self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers) self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32)) + + if use_smeargate: + self.smeargate = SmearGate(model_dim) + if use_bigramhash: + self.bigram_embed = BigramHashEmbedding(bigram_vocab_size, bigram_dim, model_dim) + self.trigram_embed = None + if use_trigramhash: + self.trigram_embed = TrigramHashEmbedding(trigram_vocab_size, trigram_dim, model_dim) + + # Parse VE layers + self._ve_layer_indices: list[int] = [] + kv_dim = num_kv_heads * (model_dim // num_heads) + if ve_dim > 0 and ve_layers: + self._ve_layer_indices = [int(x.strip()) for x in ve_layers.split(",") if x.strip()] + if self._ve_layer_indices: + self.ve_shared = ValueEmbedding(vocab_size, ve_dim, kv_dim) + self.ve_layer_scales = nn.ParameterList( + [nn.Parameter(torch.ones(1, dtype=torch.float32)) for _ in self._ve_layer_indices] + ) + + # Determine which layers get XSA + xsa_layer_set = set() + if xsa_last_n > 0: + xsa_layer_set = set(range(num_layers - xsa_last_n, num_layers)) + self.blocks = nn.ModuleList( [ Block( - model_dim, - num_heads, - num_kv_heads, - mlp_mult, - rope_base, - qk_gain_init, + model_dim, num_heads, num_kv_heads, mlp_mult, + rope_base, qk_gain_init, + layer_idx=i, + use_gated_attention=use_gated_attention, + use_value_residual=use_value_residual, + use_xsa=(i in xsa_layer_set), + rope_dims=rope_dims, + use_ln_scale=use_ln_scale, ) for i in range(num_layers) ] @@ -694,40 +1054,86 @@ def _init_weights(self) -> None: if self.tie_embeddings: nn.init.normal_(self.tok_emb.weight, mean=0.0, std=self.tied_embed_init_std) for module in self.modules(): - if isinstance(module, nn.Linear) and getattr(module, "_zero_init", False): - nn.init.zeros_(module.weight) - - def forward(self, input_ids: Tensor, target_ids: Tensor) -> Tensor: + if isinstance(module, nn.Linear): + if getattr(module, "_zero_init", False): + nn.init.zeros_(module.weight) + else: + # OrthoInit for all non-zero-init Linear layers (SmearGate requires this) + nn.init.orthogonal_(module.weight) + + def forward(self, input_ids: Tensor, target_ids: Tensor | None = None) -> Tensor: x = self.tok_emb(input_ids) + + # Add bigram + trigram hash embeddings + if self.use_bigramhash: + x = x + self.bigram_embed(input_ids) + if self.trigram_embed is not None: + x = x + self.trigram_embed(input_ids) + x = F.rms_norm(x, (x.size(-1),)) + + # Apply smeargate after initial norm + if self.use_smeargate: + x = self.smeargate(x) + x0 = x skips: list[Tensor] = [] + v0: Tensor | None = None + + # Build VE lookup: layer_idx -> (ve_embed, scale_idx) + ve_map: dict[int, int] = {} + ve_embed_cache: Tensor | None = None + if self._ve_layer_indices: + ve_embed_cache = self.ve_shared(input_ids) # [B, T, kv_dim] + for si, li in enumerate(self._ve_layer_indices): + ve_map[li] = si - # First half stores skips; second half reuses them in reverse order. + # Encoder half stores skips for i in range(self.num_encoder_layers): - x = self.blocks[i](x, x0) + v_embed_i = None + if i in ve_map: + v_embed_i = ve_embed_cache * self.ve_layer_scales[ve_map[i]].to(dtype=ve_embed_cache.dtype) + x, v_out = self.blocks[i](x, x0, v0, v_embed=v_embed_i) + if i == 0 and self.use_value_residual: + v0 = v_out skips.append(x) + + # Decoder half reuses skips in reverse order for i in range(self.num_decoder_layers): if skips: x = x + self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skips.pop() - x = self.blocks[self.num_encoder_layers + i](x, x0) - - x = self.final_norm(x).reshape(-1, x.size(-1)) - targets = target_ids.reshape(-1) - if self.tie_embeddings: - logits_proj = F.linear(x, self.tok_emb.weight) + layer_idx = self.num_encoder_layers + i + v_embed_i = None + if layer_idx in ve_map: + v_embed_i = ve_embed_cache * self.ve_layer_scales[ve_map[layer_idx]].to(dtype=ve_embed_cache.dtype) + x, v_out = self.blocks[layer_idx](x, x0, v0, v_embed=v_embed_i) + if self.num_encoder_layers == 0 and i == 0 and self.use_value_residual: + v0 = v_out + + x = self.final_norm(x) + + if target_ids is None: + # Eval mode: return logits [B, T, V] + x_flat = x.reshape(-1, x.size(-1)) + if self.tie_embeddings: + logits_proj = F.linear(x_flat, self.tok_emb.weight) + else: + logits_proj = self.lm_head(x_flat) + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + return logits.reshape(input_ids.shape[0], input_ids.shape[1], -1) else: - if self.lm_head is None: - raise RuntimeError("lm_head is required when tie_embeddings=False") - logits_proj = self.lm_head(x) - logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) - return F.cross_entropy(logits.float(), targets, reduction="mean") - - -# ----------------------------- -# TRAINING -# ----------------------------- - + # Training mode: return loss + x_flat = x.reshape(-1, x.size(-1)) + targets = target_ids.reshape(-1) + if self.tie_embeddings: + logits_proj = F.linear(x_flat, self.tok_emb.weight) + else: + logits_proj = self.lm_head(x_flat) + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + return F.cross_entropy(logits.float(), targets, reduction="mean") + + +# --- TRAINING --- def main() -> None: global zeropower_via_newtonschulz5 @@ -735,10 +1141,7 @@ def main() -> None: args = Hyperparameters() zeropower_via_newtonschulz5 = torch.compile(zeropower_via_newtonschulz5) - # ----------------------------- - # DISTRIBUTED + CUDA SETUP - # ----------------------------- - + # --- DISTRIBUTED + CUDA SETUP --- distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ rank = int(os.environ.get("RANK", "0")) world_size = int(os.environ.get("WORLD_SIZE", "1")) @@ -758,7 +1161,6 @@ def main() -> None: dist.barrier() master_process = rank == 0 - # Fast math knobs torch.backends.cuda.matmul.allow_tf32 = True torch.backends.cudnn.allow_tf32 = True from torch.backends.cuda import enable_cudnn_sdp, enable_flash_sdp, enable_math_sdp, enable_mem_efficient_sdp @@ -793,10 +1195,7 @@ def log0(msg: str, console: bool = True) -> None: ) log0("=" * 100, console=False) - # ----------------------------- - # TOKENIZER + VALIDATION METRIC SETUP - # ----------------------------- - + # --- TOKENIZER + VALIDATION METRIC SETUP --- random.seed(args.seed) np.random.seed(args.seed) torch.manual_seed(args.seed) @@ -819,10 +1218,7 @@ def log0(msg: str, console: bool = True) -> None: log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}") log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}") - # ----------------------------- - # MODEL + OPTIMIZER SETUP - # ----------------------------- - + # --- MODEL + OPTIMIZER SETUP --- base_model = GPT( vocab_size=args.vocab_size, num_layers=args.num_layers, @@ -835,44 +1231,90 @@ def log0(msg: str, console: bool = True) -> None: logit_softcap=args.logit_softcap, rope_base=args.rope_base, qk_gain_init=args.qk_gain_init, + use_smeargate=args.use_smeargate, + use_bigramhash=args.use_bigramhash, + bigram_vocab_size=args.bigram_vocab_size, + bigram_dim=args.bigram_dim, + use_gated_attention=args.use_gated_attention, + use_value_residual=args.use_value_residual, + use_ln_scale=args.use_ln_scale, + rope_dims=args.rope_dims, + xsa_last_n=args.xsa_last_n, + ve_dim=args.ve_dim, + ve_layers=args.ve_layers, + use_trigramhash=args.use_trigramhash, + trigram_vocab_size=args.trigram_vocab_size, + trigram_dim=args.trigram_dim, ).to(device).bfloat16() for module in base_model.modules(): if isinstance(module, CastedLinear): module.float() restore_low_dim_params_to_fp32(base_model) - compiled_model = torch.compile(base_model, dynamic=False, fullgraph=True) + + compiled_model = torch.compile(base_model, dynamic=False, fullgraph=False) model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model - # Optimizer split: - # - token embedding (Adam) uses EMBED_LR - # - untied lm_head (Adam) uses HEAD_LR - # - matrix params in transformer blocks use MATRIX_LR via Muon - # - vectors/scalars use SCALAR_LR via Adam + # --- Optimizer param groups --- + # Collect block params block_named_params = list(base_model.blocks.named_parameters()) matrix_params = [ - p - for name, p in block_named_params + p for name, p in block_named_params if p.ndim == 2 and not any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) ] scalar_params = [ - p - for name, p in block_named_params + p for name, p in block_named_params if p.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) ] if base_model.skip_weights.numel() > 0: scalar_params.append(base_model.skip_weights) + + # SmearGate.gate -> scalar_params + if args.use_smeargate: + scalar_params.append(base_model.smeargate.gate) + + # BigramHash params + token_params_list = [base_model.tok_emb.weight] + muon_extra_params = [] + if args.use_bigramhash: + token_params_list.append(base_model.bigram_embed.embed.weight) + muon_extra_params.append(base_model.bigram_embed.proj.weight) + scalar_params.append(base_model.bigram_embed.bigram_scale) + + # TrigramHash params + if base_model.trigram_embed is not None: + token_params_list.append(base_model.trigram_embed.embed.weight) + muon_extra_params.append(base_model.trigram_embed.proj.weight) + scalar_params.append(base_model.trigram_embed.trigram_scale) + + # VE128 params + if base_model._ve_layer_indices: + token_params_list.append(base_model.ve_shared.embed.weight) + muon_extra_params.append(base_model.ve_shared.proj.weight) + scalar_params.append(base_model.ve_shared.ve_scale) + for p in base_model.ve_layer_scales.parameters(): + scalar_params.append(p) + + # Gated attention attn_gate (weight+bias) -> scalar_params (small params) + for name, p in block_named_params: + if "attn_gate.weight" in name or "attn_gate.bias" in name: + # These are already in block_named_params; ensure they go to scalar + # They have ndim=2 (weight) but contain "attn_gate" which is in CONTROL patterns + pass # Already handled by CONTROL_TENSOR_NAME_PATTERNS check above + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr optimizer_tok = torch.optim.Adam( - [{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr}], + [{"params": token_params_list, "lr": token_lr, "base_lr": token_lr}], betas=(args.beta1, args.beta2), eps=args.adam_eps, fused=True, ) + all_muon_params = matrix_params + muon_extra_params optimizer_muon = Muon( - matrix_params, + all_muon_params, lr=args.matrix_lr, momentum=args.muon_momentum, backend_steps=args.muon_backend_steps, + weight_decay=args.weight_decay, ) for group in optimizer_muon.param_groups: group["base_lr"] = args.matrix_lr @@ -908,11 +1350,30 @@ def log0(msg: str, console: bool = True) -> None: f"max_wallclock_seconds:{args.max_wallclock_seconds:.3f}" ) log0(f"seed:{args.seed}") + log0( + f"features: smeargate={args.use_smeargate} bigramhash={args.use_bigramhash} " + f"value_residual={args.use_value_residual} gated_attn={args.use_gated_attention} " + f"rope_dims={args.rope_dims} xsa_last_n={args.xsa_last_n} " + f"ve_dim={args.ve_dim} ve_layers={args.ve_layers} " + f"ln_scale={args.use_ln_scale} ema={args.use_ema}(decay={args.ema_decay}) " + f"swa={args.swa_enabled}(every={args.swa_every},thresh={args.swa_threshold}) " + f"late_qat={args.use_late_qat}(time_frac={args.qat_time_frac}) " + f"weight_decay={args.weight_decay} grad_clip={args.grad_clip_norm}" + ) - # ----------------------------- - # DATA LOADER & MODEL WARMUP - # ----------------------------- + # --- EMA + SWA STATE INIT (fp32 on CPU) --- + ema_state: dict[str, Tensor] = {} + if args.use_ema: + for name, param in base_model.state_dict().items(): + ema_state[name] = param.float().cpu().clone() + swa_state: dict[str, Tensor] = {} + swa_count = 0 + if args.swa_enabled: + for name, param in base_model.state_dict().items(): + swa_state[name] = torch.zeros_like(param.float().cpu()) + + # --- DATA LOADER & MODEL WARMUP --- train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) def zero_grad_all() -> None: @@ -932,8 +1393,6 @@ def lr_mul(step: int, elapsed_ms: float) -> float: remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0) return remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0 - # Warmup primes the compiled forward/backward/optimizer paths, then we restore the - # initial weights/optimizer state so measured training starts from the true init. if args.warmup_steps > 0: initial_model_state = {name: tensor.detach().cpu().clone() for name, tensor in base_model.state_dict().items()} initial_optimizer_states = [copy.deepcopy(opt.state_dict()) for opt in optimizers] @@ -959,11 +1418,16 @@ def lr_mul(step: int, elapsed_ms: float) -> float: if distributed: model.require_backward_grad_sync = True train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) - - # ----------------------------- - # MAIN TRAINING LOOP - # ----------------------------- - + # Re-init EMA/SWA state after warmup restore + if args.use_ema: + for name, param in base_model.state_dict().items(): + ema_state[name] = param.float().cpu().clone() + if args.swa_enabled: + for name, param in base_model.state_dict().items(): + swa_state[name] = torch.zeros_like(param.float().cpu()) + swa_count = 0 + + # --- MAIN TRAINING LOOP --- training_time_ms = 0.0 stop_after_step: int | None = None torch.cuda.synchronize() @@ -978,16 +1442,8 @@ def lr_mul(step: int, elapsed_ms: float) -> float: torch.cuda.synchronize() training_time_ms += 1000.0 * (time.perf_counter() - t0) val_loss, val_bpb = eval_val( - args, - model, - rank, - world_size, - device, - grad_accum_steps, - val_tokens, - base_bytes_lut, - has_leading_space_lut, - is_boundary_token_lut, + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, ) log0( f"step:{step}/{args.iterations} val_loss:{val_loss:.4f} val_bpb:{val_bpb:.4f} " @@ -1006,6 +1462,14 @@ def lr_mul(step: int, elapsed_ms: float) -> float: elapsed_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) scale = lr_mul(step, elapsed_ms) + + # Late QAT activation — time-fraction based, NOT LR scale + if args.use_late_qat and not CastedLinear._qat_enabled and max_wallclock_ms is not None: + time_frac_elapsed = elapsed_ms / max_wallclock_ms + if time_frac_elapsed >= (1.0 - args.qat_time_frac): + CastedLinear._qat_enabled = True + log0(f"step:{step} QAT activated (time_frac={time_frac_elapsed:.3f}, scale={scale:.4f})") + zero_grad_all() train_loss = torch.zeros((), device=device) for micro_step in range(grad_accum_steps): @@ -1033,6 +1497,21 @@ def lr_mul(step: int, elapsed_ms: float) -> float: opt.step() zero_grad_all() + # EMA update (fp32 on CPU, every step) + if args.use_ema: + decay = args.ema_decay + with torch.no_grad(): + for name, param in base_model.state_dict().items(): + ema_state[name].mul_(decay).add_(param.float().cpu(), alpha=1 - decay) + + # Tight SWA: average EMA weights every swa_every steps when scale < swa_threshold + if args.swa_enabled and args.use_ema and scale < args.swa_threshold: + if (step + 1) % args.swa_every == 0: + swa_count += 1 + with torch.no_grad(): + for name in swa_state: + swa_state[name].add_(ema_state[name]) + step += 1 approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) should_log_train = ( @@ -1045,7 +1524,6 @@ def lr_mul(step: int, elapsed_ms: float) -> float: f"train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms" ) - # Needed to sync whether we've reached the wallclock cap. reached_cap = max_wallclock_ms is not None and approx_training_time_ms >= max_wallclock_ms if distributed and max_wallclock_ms is not None: reached_cap_tensor = torch.tensor(int(reached_cap), device=device) @@ -1059,12 +1537,54 @@ def lr_mul(step: int, elapsed_ms: float) -> float: f"reserved: {torch.cuda.max_memory_reserved() // 1024 // 1024} MiB" ) - # ----------------------------- - # SERIALIZATION + ROUNDTRIP VALIDATION - # ----------------------------- - # Save the raw state (useful for debugging/loading in PyTorch directly), then always produce - # the compressed int8+zlib artifact and validate the round-tripped weights. - + # --- CHOOSE BEST WEIGHTS: raw vs EMA vs SWA --- + # Save raw model weights first (the actual trained weights, always available as fallback) + raw_sd = {name: t.detach().cpu().clone() for name, t in base_model.state_dict().items()} + best_bpb = val_bpb # raw model BPB from last eval + best_source = "raw" + log0(f"raw model val_bpb:{val_bpb:.4f}") + + if args.use_ema and ema_state: + log0("Evaluating EMA weights...") + ema_sd = {name: ema_state[name].to(dtype=raw_sd[name].dtype) for name in raw_sd} + base_model.load_state_dict(ema_sd, strict=True) + torch.cuda.synchronize() + t_ema = time.perf_counter() + ema_val_loss, ema_val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0(f"ema_eval val_bpb:{ema_val_bpb:.4f} eval_time:{1000.0 * (time.perf_counter() - t_ema):.0f}ms") + if ema_val_bpb < best_bpb: + best_bpb = ema_val_bpb + best_source = "ema" + + if args.swa_enabled and swa_count > 0: + log0(f"Evaluating SWA ({swa_count} snapshots)...") + swa_sd = {name: (swa_state[name] / swa_count).to(dtype=raw_sd[name].dtype) for name in raw_sd} + base_model.load_state_dict(swa_sd, strict=True) + torch.cuda.synchronize() + t_swa = time.perf_counter() + swa_val_loss, swa_val_bpb = eval_val( + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0(f"swa_eval val_bpb:{swa_val_bpb:.4f} eval_time:{1000.0 * (time.perf_counter() - t_swa):.0f}ms") + if swa_val_bpb < best_bpb: + best_bpb = swa_val_bpb + best_source = "swa" + + # Load the best weights for serialization + log0(f"Using {best_source} weights (val_bpb={best_bpb:.4f})") + if best_source == "raw": + base_model.load_state_dict(raw_sd, strict=True) + elif best_source == "ema": + base_model.load_state_dict(ema_sd, strict=True) + # If swa, weights are already loaded + + # --- SERIALIZATION + ROUNDTRIP VALIDATION --- if master_process: torch.save(base_model.state_dict(), "final_model.pt") model_bytes = os.path.getsize("final_model.pt") @@ -1077,7 +1597,7 @@ def lr_mul(step: int, elapsed_ms: float) -> float: quant_buf = io.BytesIO() torch.save(quant_obj, quant_buf) quant_raw = quant_buf.getvalue() - quant_blob = zlib.compress(quant_raw, level=9) + quant_blob = _compress(quant_raw) quant_raw_bytes = len(quant_raw) if master_process: with open("final_model.int8.ptz", "wb") as f: @@ -1086,37 +1606,48 @@ def lr_mul(step: int, elapsed_ms: float) -> float: code_bytes = len(code.encode("utf-8")) ratio = quant_stats["baseline_tensor_bytes"] / max(quant_stats["int8_payload_bytes"], 1) log0( - f"Serialized model int8+zlib: {quant_file_bytes} bytes " + f"Serialized model int5/6+zstd: {quant_file_bytes} bytes " f"(payload:{quant_stats['int8_payload_bytes']} raw_torch:{quant_raw_bytes} payload_ratio:{ratio:.2f}x)" ) - log0(f"Total submission size int8+zlib: {quant_file_bytes + code_bytes} bytes") + log0(f"Total submission size int5/6+zstd: {quant_file_bytes + code_bytes} bytes") if distributed: dist.barrier() with open("final_model.int8.ptz", "rb") as f: quant_blob_disk = f.read() - quant_state = torch.load(io.BytesIO(zlib.decompress(quant_blob_disk)), map_location="cpu") + quant_state = torch.load(io.BytesIO(_decompress(quant_blob_disk)), map_location="cpu") base_model.load_state_dict(dequantize_state_dict_int8(quant_state), strict=True) torch.cuda.synchronize() t_qeval = time.perf_counter() q_val_loss, q_val_bpb = eval_val( - args, - model, - rank, - world_size, - device, - grad_accum_steps, - val_tokens, - base_bytes_lut, - has_leading_space_lut, - is_boundary_token_lut, + args, model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, ) torch.cuda.synchronize() log0( - f"final_int8_zlib_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} " + f"final_int5_6_zstd_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} " f"eval_time:{1000.0 * (time.perf_counter() - t_qeval):.0f}ms" ) - log0(f"final_int8_zlib_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}") + log0(f"final_int5_6_zstd_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}") + + # --- TTT (Test-Time Training) on quantized model --- + if args.use_ttt: + log0(f"Starting TTT: {args.ttt_epochs} epochs, lr={args.ttt_lr}, chunk={args.ttt_chunk_tokens}") + # TTT runs on the dequantized model (simulating eval-time adaptation) + # Need uncompiled model for TTT (backward through compiled model is fine with fullgraph=False) + torch.cuda.synchronize() + t_ttt = time.perf_counter() + ttt_val_loss, ttt_val_bpb = eval_val_ttt( + args, base_model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + log_fn=log0, + ) + torch.cuda.synchronize() + log0( + f"ttt_eval val_loss:{ttt_val_loss:.4f} val_bpb:{ttt_val_bpb:.4f} " + f"eval_time:{1000.0 * (time.perf_counter() - t_ttt):.0f}ms" + ) + log0(f"ttt_eval_exact val_loss:{ttt_val_loss:.8f} val_bpb:{ttt_val_bpb:.8f}") if distributed: dist.destroy_process_group()