Spaces:
Running on Zero
Running on Zero
beanapologist commited on
Commit Β·
150301a
1
Parent(s): cc19abe
Add train_gpt_kernel.py directly + simplify app
Browse files- app.py +15 -76
- train_gpt_kernel.py +1094 -0
app.py
CHANGED
|
@@ -1,11 +1,9 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
| 3 |
-
import os
|
| 4 |
import sys
|
| 5 |
-
from pathlib import Path
|
| 6 |
|
| 7 |
def train_model():
|
| 8 |
-
"""Train the ΞΌβΈ Kernel model
|
| 9 |
log = []
|
| 10 |
|
| 11 |
def log_step(msg):
|
|
@@ -13,85 +11,31 @@ def train_model():
|
|
| 13 |
return "\n".join(log)
|
| 14 |
|
| 15 |
try:
|
| 16 |
-
|
| 17 |
-
if not Path("parameter-golf").exists():
|
| 18 |
-
yield log_step("π Cloning repository...")
|
| 19 |
-
result = subprocess.run(
|
| 20 |
-
["git", "clone", "https://github.com/beanapologist/parameter-golf"],
|
| 21 |
-
capture_output=True, text=True, timeout=120
|
| 22 |
-
)
|
| 23 |
-
if result.returncode != 0:
|
| 24 |
-
yield log_step(f"β Clone failed:\n{result.stderr[:500]}")
|
| 25 |
-
return
|
| 26 |
-
yield log_step("β
Repository cloned")
|
| 27 |
-
else:
|
| 28 |
-
yield log_step("β
Repository exists")
|
| 29 |
-
|
| 30 |
-
os.chdir("parameter-golf")
|
| 31 |
-
|
| 32 |
-
# Fetch and checkout the kernel PR branch
|
| 33 |
-
yield log_step("π Checking out kernel branch...")
|
| 34 |
-
|
| 35 |
-
# Track and checkout the remote branch
|
| 36 |
-
subprocess.run(["git", "fetch", "origin"], capture_output=True)
|
| 37 |
-
result = subprocess.run(
|
| 38 |
-
["git", "checkout", "-b", "kernel", "origin/copilot/integrate-critical-eigenvalue-functionality"],
|
| 39 |
-
capture_output=True, text=True
|
| 40 |
-
)
|
| 41 |
-
|
| 42 |
-
if result.returncode == 0:
|
| 43 |
-
yield log_step("β
Kernel branch active")
|
| 44 |
-
else:
|
| 45 |
-
# Maybe branch already exists locally, try switching
|
| 46 |
-
result2 = subprocess.run(
|
| 47 |
-
["git", "checkout", "origin/copilot/integrate-critical-eigenvalue-functionality"],
|
| 48 |
-
capture_output=True, text=True
|
| 49 |
-
)
|
| 50 |
-
if result2.returncode == 0:
|
| 51 |
-
yield log_step("β
Kernel branch active (detached HEAD)")
|
| 52 |
-
else:
|
| 53 |
-
yield log_step(f"β οΈ Branch checkout failed: {result.stderr[:200]}")
|
| 54 |
-
yield log_step("β οΈ Trying direct file download...")
|
| 55 |
-
# Fallback: download the file directly
|
| 56 |
-
import urllib.request
|
| 57 |
-
url = "https://raw.githubusercontent.com/beanapologist/parameter-golf/copilot/integrate-critical-eigenvalue-functionality/train_gpt_kernel.py"
|
| 58 |
-
urllib.request.urlretrieve(url, "train_gpt_kernel.py")
|
| 59 |
-
yield log_step("β
Downloaded train_gpt_kernel.py directly")
|
| 60 |
-
|
| 61 |
-
# Check for kernel training script
|
| 62 |
-
if not Path("train_gpt_kernel.py").exists():
|
| 63 |
-
yield log_step("β train_gpt_kernel.py not found")
|
| 64 |
-
yield log_step("Available: " + ", ".join([f.name for f in Path(".").glob("train_*.py")]))
|
| 65 |
-
return
|
| 66 |
-
|
| 67 |
-
yield log_step("β
Found train_gpt_kernel.py")
|
| 68 |
-
|
| 69 |
-
# Install deps
|
| 70 |
-
yield log_step("π Installing torch...")
|
| 71 |
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "torch", "numpy", "tiktoken"], timeout=180)
|
| 72 |
yield log_step("β
Dependencies ready")
|
| 73 |
|
| 74 |
# GPU check
|
| 75 |
gpu_result = subprocess.run(
|
| 76 |
-
[sys.executable, "-c", "import torch; print(f'GPU
|
| 77 |
capture_output=True, text=True
|
| 78 |
)
|
| 79 |
yield log_step(gpu_result.stdout.strip())
|
| 80 |
|
| 81 |
-
# Run minimal training test (avoid data download OOM)
|
| 82 |
yield log_step("=" * 60)
|
| 83 |
-
yield log_step("π Starting
|
|
|
|
| 84 |
yield log_step("=" * 60)
|
| 85 |
|
| 86 |
-
#
|
|
|
|
| 87 |
env = os.environ.copy()
|
| 88 |
env.update({
|
| 89 |
"NUM_LAYERS": "4",
|
| 90 |
-
"MODEL_DIM": "192",
|
| 91 |
"MAX_STEPS": "100",
|
| 92 |
})
|
| 93 |
|
| 94 |
-
# Stream training output
|
| 95 |
process = subprocess.Popen(
|
| 96 |
[sys.executable, "train_gpt_kernel.py"],
|
| 97 |
stdout=subprocess.PIPE,
|
|
@@ -109,30 +53,25 @@ def train_model():
|
|
| 109 |
|
| 110 |
yield log_step("=" * 60)
|
| 111 |
if process.returncode == 0:
|
| 112 |
-
yield log_step("β
Training
|
| 113 |
else:
|
| 114 |
-
yield log_step(f"β οΈ
|
| 115 |
|
| 116 |
-
except subprocess.TimeoutExpired:
|
| 117 |
-
yield log_step("β±οΈ Timeout exceeded")
|
| 118 |
except Exception as e:
|
| 119 |
yield log_step(f"β Error: {str(e)}")
|
| 120 |
|
| 121 |
-
# Gradio UI
|
| 122 |
with gr.Blocks(title="ΞΌβΈ Kernel") as demo:
|
| 123 |
gr.Markdown("""
|
| 124 |
# ΞΌβΈ Kernel Training - Parameter Golf
|
| 125 |
|
| 126 |
-
Formally verified LM architecture:
|
| 127 |
-
- **C(r) = 2r/(1+rΒ²)** coherence activation
|
| 128 |
-
- **Ξ΄_S = 1+β2** silver MLP expansion
|
| 129 |
-
- **ΞΌβΈ = 1** eight-
|
| 130 |
-
|
| 131 |
-
464 Lean 4 proofs, 0 sorry.
|
| 132 |
""")
|
| 133 |
|
| 134 |
btn = gr.Button("π Start Training", variant="primary", size="lg")
|
| 135 |
-
out = gr.Textbox(label="Log", lines=25, max_lines=40, autoscroll=True)
|
| 136 |
|
| 137 |
btn.click(fn=train_model, outputs=out)
|
| 138 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import subprocess
|
|
|
|
| 3 |
import sys
|
|
|
|
| 4 |
|
| 5 |
def train_model():
|
| 6 |
+
"""Train the ΞΌβΈ Kernel model - simplified version"""
|
| 7 |
log = []
|
| 8 |
|
| 9 |
def log_step(msg):
|
|
|
|
| 11 |
return "\n".join(log)
|
| 12 |
|
| 13 |
try:
|
| 14 |
+
yield log_step("π Installing dependencies...")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
subprocess.run([sys.executable, "-m", "pip", "install", "-q", "torch", "numpy", "tiktoken"], timeout=180)
|
| 16 |
yield log_step("β
Dependencies ready")
|
| 17 |
|
| 18 |
# GPU check
|
| 19 |
gpu_result = subprocess.run(
|
| 20 |
+
[sys.executable, "-c", "import torch; print(f'GPU: {torch.cuda.is_available()}')"],
|
| 21 |
capture_output=True, text=True
|
| 22 |
)
|
| 23 |
yield log_step(gpu_result.stdout.strip())
|
| 24 |
|
|
|
|
| 25 |
yield log_step("=" * 60)
|
| 26 |
+
yield log_step("π Starting ΞΌβΈ Kernel training")
|
| 27 |
+
yield log_step("Coherence activation C(r)=2r/(1+rΒ²) | Silver ratio Ξ΄_S=1+β2 | 8-head attention")
|
| 28 |
yield log_step("=" * 60)
|
| 29 |
|
| 30 |
+
# Run training (small config for Zero GPU)
|
| 31 |
+
import os
|
| 32 |
env = os.environ.copy()
|
| 33 |
env.update({
|
| 34 |
"NUM_LAYERS": "4",
|
| 35 |
+
"MODEL_DIM": "192",
|
| 36 |
"MAX_STEPS": "100",
|
| 37 |
})
|
| 38 |
|
|
|
|
| 39 |
process = subprocess.Popen(
|
| 40 |
[sys.executable, "train_gpt_kernel.py"],
|
| 41 |
stdout=subprocess.PIPE,
|
|
|
|
| 53 |
|
| 54 |
yield log_step("=" * 60)
|
| 55 |
if process.returncode == 0:
|
| 56 |
+
yield log_step("β
Training complete!")
|
| 57 |
else:
|
| 58 |
+
yield log_step(f"β οΈ Exit code {process.returncode}")
|
| 59 |
|
|
|
|
|
|
|
| 60 |
except Exception as e:
|
| 61 |
yield log_step(f"β Error: {str(e)}")
|
| 62 |
|
|
|
|
| 63 |
with gr.Blocks(title="ΞΌβΈ Kernel") as demo:
|
| 64 |
gr.Markdown("""
|
| 65 |
# ΞΌβΈ Kernel Training - Parameter Golf
|
| 66 |
|
| 67 |
+
Formally verified LM architecture (464 Lean 4 proofs):
|
| 68 |
+
- **C(r) = 2r/(1+rΒ²)** coherence activation
|
| 69 |
+
- **Ξ΄_S = 1+β2 β 2.414** silver MLP expansion
|
| 70 |
+
- **ΞΌβΈ = 1** eight-cycle attention
|
|
|
|
|
|
|
| 71 |
""")
|
| 72 |
|
| 73 |
btn = gr.Button("π Start Training", variant="primary", size="lg")
|
| 74 |
+
out = gr.Textbox(label="Training Log", lines=25, max_lines=40, autoscroll=True)
|
| 75 |
|
| 76 |
btn.click(fn=train_model, outputs=out)
|
| 77 |
|
train_gpt_kernel.py
ADDED
|
@@ -0,0 +1,1094 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
train_gpt_kernel.py β Parameter-Golf training pipeline informed by the
|
| 3 |
+
formal Lean 4 theorems in formal-lean/CriticalEigenvalue.lean.
|
| 4 |
+
|
| 5 |
+
Three mathematical objects from the Kernel formalization are wired
|
| 6 |
+
directly into the model architecture:
|
| 7 |
+
|
| 8 |
+
1. Coherence activation C(r) = 2r / (1 + rΒ²)
|
| 9 |
+
Defined in Β§5 of CriticalEigenvalue.lean and proved to satisfy:
|
| 10 |
+
β’ C(r) β€ 1 for r β₯ 0 (AMβGM bound)
|
| 11 |
+
β’ C(1) = 1 (unique maximum)
|
| 12 |
+
β’ C(βr) = βC(r) (odd symmetry)
|
| 13 |
+
β’ C(r)Β² + ((rΒ²β1)/(1+rΒ²))Β² = 1 (Pythagorean identity Β§18)
|
| 14 |
+
β’ C(exp Ξ») = sech Ξ» (Lyapunov duality Β§10)
|
| 15 |
+
Used here as the nonlinearity inside every MLP block, replacing the
|
| 16 |
+
standard reluΒ² activation from the baseline.
|
| 17 |
+
|
| 18 |
+
2. Silver ratio Ξ΄S = 1 + β2 β 2.414
|
| 19 |
+
Defined in Β§7 (Proposition 4) and proved to satisfy:
|
| 20 |
+
β’ Ξ΄S = 2 + 1/Ξ΄S (self-similarity Β§20)
|
| 21 |
+
β’ Ξ΄S is the positive root of xΒ²β2xβ1 = 0
|
| 22 |
+
Used here as the MLP hidden-dimension multiplier, so the MLP
|
| 23 |
+
expands from d_model to βΞ΄S Β· d_modelβ neurons.
|
| 24 |
+
|
| 25 |
+
3. Z/8Z rotational memory (ΞΌ^8 = 1, Β§2 and Β§15)
|
| 26 |
+
The critical eigenvalue ΞΌ = exp(3Οi/4) generates an exact 8-cycle.
|
| 27 |
+
The default number of attention heads is set to 8, so that each head
|
| 28 |
+
occupies one slot of the cyclic group, providing uniform coverage of
|
| 29 |
+
the 8 distinct phase positions proven distinct in Β§3.
|
| 30 |
+
|
| 31 |
+
Hard stop: to stay readable for newcomers, keep this file under 1500 lines.
|
| 32 |
+
"""
|
| 33 |
+
|
| 34 |
+
from __future__ import annotations
|
| 35 |
+
|
| 36 |
+
import copy
|
| 37 |
+
import glob
|
| 38 |
+
import io
|
| 39 |
+
import math
|
| 40 |
+
import os
|
| 41 |
+
import random
|
| 42 |
+
import subprocess
|
| 43 |
+
import sys
|
| 44 |
+
import time
|
| 45 |
+
import uuid
|
| 46 |
+
import zlib
|
| 47 |
+
from pathlib import Path
|
| 48 |
+
|
| 49 |
+
import numpy as np
|
| 50 |
+
import sentencepiece as spm
|
| 51 |
+
import torch
|
| 52 |
+
import torch.distributed as dist
|
| 53 |
+
import torch.nn.functional as F
|
| 54 |
+
from torch import Tensor, nn
|
| 55 |
+
from torch.nn.parallel import DistributedDataParallel as DDP
|
| 56 |
+
|
| 57 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 58 |
+
# KERNEL CONSTANTS (from formal-lean/CriticalEigenvalue.lean)
|
| 59 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 60 |
+
|
| 61 |
+
# Silver ratio Ξ΄S = 1 + β2 (CriticalEigenvalue.lean Β§7, Proposition 4).
|
| 62 |
+
# Self-similar: Ξ΄S = 2 + 1/Ξ΄S. Positive root of xΒ² β 2x β 1 = 0.
|
| 63 |
+
SILVER_RATIO: float = 1.0 + math.sqrt(2) # β 2.4142135β¦
|
| 64 |
+
|
| 65 |
+
# Critical eigenvalue angle ΞΈ = 3Ο/4, so ΞΌ = exp(IΒ·ΞΈ).
|
| 66 |
+
# ΞΌ^8 = 1 (CriticalEigenvalue.lean Β§2) and gcd(3,8)=1 (Β§3).
|
| 67 |
+
MU_ANGLE: float = 3.0 * math.pi / 4.0 # 135Β°
|
| 68 |
+
|
| 69 |
+
# Number of distinct ΞΌ-orbit slots: Z/8Z (Β§15).
|
| 70 |
+
MU_ORBIT_SIZE: int = 8
|
| 71 |
+
|
| 72 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
# HYPERPARAMETERS
|
| 74 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 75 |
+
|
| 76 |
+
class Hyperparameters:
|
| 77 |
+
# Data paths are shard globs produced by the existing preprocessing pipeline.
|
| 78 |
+
data_path = os.environ.get("DATA_PATH", "./data/datasets/fineweb10B_sp1024")
|
| 79 |
+
train_files = os.path.join(data_path, "fineweb_train_*.bin")
|
| 80 |
+
val_files = os.path.join(data_path, "fineweb_val_*.bin")
|
| 81 |
+
tokenizer_path = os.environ.get("TOKENIZER_PATH", "./data/tokenizers/fineweb_1024_bpe.model")
|
| 82 |
+
run_id = os.environ.get("RUN_ID", str(uuid.uuid4()))
|
| 83 |
+
seed = int(os.environ.get("SEED", 1337))
|
| 84 |
+
|
| 85 |
+
# Validation cadence and batch size.
|
| 86 |
+
val_batch_size = int(os.environ.get("VAL_BATCH_SIZE", 524_288))
|
| 87 |
+
val_loss_every = int(os.environ.get("VAL_LOSS_EVERY", 1000))
|
| 88 |
+
train_log_every = int(os.environ.get("TRAIN_LOG_EVERY", 200))
|
| 89 |
+
|
| 90 |
+
# Training length.
|
| 91 |
+
iterations = int(os.environ.get("ITERATIONS", 20000))
|
| 92 |
+
warmdown_iters = int(os.environ.get("WARMDOWN_ITERS", 1200))
|
| 93 |
+
warmup_steps = int(os.environ.get("WARMUP_STEPS", 20))
|
| 94 |
+
train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 524_288))
|
| 95 |
+
train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 1024))
|
| 96 |
+
max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0))
|
| 97 |
+
qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5))
|
| 98 |
+
|
| 99 |
+
# Model shape.
|
| 100 |
+
# num_heads=8: one head per slot of the Z/8Z orbit (CriticalEigenvalue.lean Β§15).
|
| 101 |
+
vocab_size = int(os.environ.get("VOCAB_SIZE", 1024))
|
| 102 |
+
num_layers = int(os.environ.get("NUM_LAYERS", 9))
|
| 103 |
+
num_kv_heads = int(os.environ.get("NUM_KV_HEADS", 4))
|
| 104 |
+
model_dim = int(os.environ.get("MODEL_DIM", 512))
|
| 105 |
+
num_heads = int(os.environ.get("NUM_HEADS", MU_ORBIT_SIZE)) # 8 β Z/8Z
|
| 106 |
+
tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1")))
|
| 107 |
+
rope_base = float(os.environ.get("ROPE_BASE", 10000.0))
|
| 108 |
+
logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0))
|
| 109 |
+
|
| 110 |
+
# MLP hidden width is βΞ΄S Β· model_dimβ unless overridden.
|
| 111 |
+
# Ξ΄S β 2.414 (CriticalEigenvalue.lean Β§7).
|
| 112 |
+
# Re-read MODEL_DIM from env so that overrides are honoured even when MLP_HIDDEN is unset.
|
| 113 |
+
mlp_hidden = int(os.environ.get("MLP_HIDDEN", round(SILVER_RATIO * int(os.environ.get("MODEL_DIM", 512)))))
|
| 114 |
+
|
| 115 |
+
# Optimizer hyperparameters.
|
| 116 |
+
embed_lr = float(os.environ.get("EMBED_LR", 0.6))
|
| 117 |
+
head_lr = float(os.environ.get("HEAD_LR", 0.008))
|
| 118 |
+
tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.05))
|
| 119 |
+
tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005))
|
| 120 |
+
matrix_lr = float(os.environ.get("MATRIX_LR", 0.04))
|
| 121 |
+
scalar_lr = float(os.environ.get("SCALAR_LR", 0.04))
|
| 122 |
+
muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.95))
|
| 123 |
+
muon_backend_steps = int(os.environ.get("MUON_BACKEND_STEPS", 5))
|
| 124 |
+
muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.85))
|
| 125 |
+
muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 500))
|
| 126 |
+
beta1 = float(os.environ.get("BETA1", 0.9))
|
| 127 |
+
beta2 = float(os.environ.get("BETA2", 0.95))
|
| 128 |
+
adam_eps = float(os.environ.get("ADAM_EPS", 1e-8))
|
| 129 |
+
grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.0))
|
| 130 |
+
|
| 131 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 132 |
+
# MUON OPTIMIZER (unchanged from baseline train_gpt.py)
|
| 133 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 134 |
+
|
| 135 |
+
def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) -> Tensor:
|
| 136 |
+
a, b, c = (3.4445, -4.7750, 2.0315)
|
| 137 |
+
X = G.bfloat16()
|
| 138 |
+
X /= X.norm() + eps
|
| 139 |
+
transposed = G.size(0) > G.size(1)
|
| 140 |
+
if transposed:
|
| 141 |
+
X = X.T
|
| 142 |
+
for _ in range(steps):
|
| 143 |
+
A = X @ X.T
|
| 144 |
+
B = b * A + c * A @ A
|
| 145 |
+
X = a * X + B @ X
|
| 146 |
+
return X.T if transposed else X
|
| 147 |
+
|
| 148 |
+
|
| 149 |
+
class Muon(torch.optim.Optimizer):
|
| 150 |
+
def __init__(self, params, lr: float, momentum: float, backend_steps: int, nesterov: bool = True):
|
| 151 |
+
super().__init__(
|
| 152 |
+
params,
|
| 153 |
+
dict(lr=lr, momentum=momentum, backend_steps=backend_steps, nesterov=nesterov),
|
| 154 |
+
)
|
| 155 |
+
|
| 156 |
+
@torch.no_grad()
|
| 157 |
+
def step(self, closure=None):
|
| 158 |
+
loss = None
|
| 159 |
+
if closure is not None:
|
| 160 |
+
with torch.enable_grad():
|
| 161 |
+
loss = closure()
|
| 162 |
+
|
| 163 |
+
distributed = dist.is_available() and dist.is_initialized()
|
| 164 |
+
world_size = dist.get_world_size() if distributed else 1
|
| 165 |
+
rank = dist.get_rank() if distributed else 0
|
| 166 |
+
|
| 167 |
+
for group in self.param_groups:
|
| 168 |
+
params = group["params"]
|
| 169 |
+
if not params:
|
| 170 |
+
continue
|
| 171 |
+
lr = group["lr"]
|
| 172 |
+
momentum = group["momentum"]
|
| 173 |
+
backend_steps = group["backend_steps"]
|
| 174 |
+
nesterov = group["nesterov"]
|
| 175 |
+
|
| 176 |
+
total_params = sum(int(p.numel()) for p in params)
|
| 177 |
+
updates_flat = torch.zeros(total_params, device=params[0].device, dtype=torch.bfloat16)
|
| 178 |
+
|
| 179 |
+
curr = 0
|
| 180 |
+
for i, p in enumerate(params):
|
| 181 |
+
if i % world_size == rank and p.grad is not None:
|
| 182 |
+
g = p.grad
|
| 183 |
+
state = self.state[p]
|
| 184 |
+
if "momentum_buffer" not in state:
|
| 185 |
+
state["momentum_buffer"] = torch.zeros_like(g)
|
| 186 |
+
buf = state["momentum_buffer"]
|
| 187 |
+
buf.mul_(momentum).add_(g)
|
| 188 |
+
if nesterov:
|
| 189 |
+
g = g.add(buf, alpha=momentum)
|
| 190 |
+
g = zeropower_via_newtonschulz5(g, steps=backend_steps)
|
| 191 |
+
g *= max(1, g.size(0) / g.size(1)) ** 0.5
|
| 192 |
+
updates_flat[curr : curr + p.numel()] = g.reshape(-1)
|
| 193 |
+
curr += p.numel()
|
| 194 |
+
|
| 195 |
+
if distributed:
|
| 196 |
+
dist.all_reduce(updates_flat, op=dist.ReduceOp.SUM)
|
| 197 |
+
|
| 198 |
+
curr = 0
|
| 199 |
+
for p in params:
|
| 200 |
+
g = updates_flat[curr : curr + p.numel()].view_as(p).to(dtype=p.dtype)
|
| 201 |
+
p.add_(g, alpha=-lr)
|
| 202 |
+
curr += p.numel()
|
| 203 |
+
|
| 204 |
+
return loss
|
| 205 |
+
|
| 206 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 207 |
+
# TOKENIZER-AGNOSTIC EVALUATION (unchanged from baseline)
|
| 208 |
+
# βββββββββββββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 209 |
+
|
| 210 |
+
def build_sentencepiece_luts(
|
| 211 |
+
sp: spm.SentencePieceProcessor, vocab_size: int, device: torch.device
|
| 212 |
+
) -> tuple[Tensor, Tensor, Tensor]:
|
| 213 |
+
sp_vocab_size = int(sp.vocab_size())
|
| 214 |
+
table_size = max(sp_vocab_size, vocab_size)
|
| 215 |
+
base_bytes_np = np.zeros((table_size,), dtype=np.int16)
|
| 216 |
+
has_leading_space_np = np.zeros((table_size,), dtype=np.bool_)
|
| 217 |
+
is_boundary_token_np = np.ones((table_size,), dtype=np.bool_)
|
| 218 |
+
for token_id in range(sp_vocab_size):
|
| 219 |
+
if sp.is_control(token_id) or sp.is_unknown(token_id) or sp.is_unused(token_id):
|
| 220 |
+
continue
|
| 221 |
+
is_boundary_token_np[token_id] = False
|
| 222 |
+
if sp.is_byte(token_id):
|
| 223 |
+
base_bytes_np[token_id] = 1
|
| 224 |
+
continue
|
| 225 |
+
piece = sp.id_to_piece(token_id)
|
| 226 |
+
if piece.startswith("β"):
|
| 227 |
+
has_leading_space_np[token_id] = True
|
| 228 |
+
piece = piece[1:]
|
| 229 |
+
base_bytes_np[token_id] = len(piece.encode("utf-8"))
|
| 230 |
+
return (
|
| 231 |
+
torch.tensor(base_bytes_np, dtype=torch.int16, device=device),
|
| 232 |
+
torch.tensor(has_leading_space_np, dtype=torch.bool, device=device),
|
| 233 |
+
torch.tensor(is_boundary_token_np, dtype=torch.bool, device=device),
|
| 234 |
+
)
|
| 235 |
+
|
| 236 |
+
|
| 237 |
+
def load_validation_tokens(pattern: str, seq_len: int) -> Tensor:
|
| 238 |
+
files = [Path(p) for p in sorted(glob.glob(pattern))]
|
| 239 |
+
if not files:
|
| 240 |
+
raise FileNotFoundError(f"No files found for pattern: {pattern}")
|
| 241 |
+
tokens = torch.cat([load_data_shard(file) for file in files]).contiguous()
|
| 242 |
+
usable = ((tokens.numel() - 1) // seq_len) * seq_len
|
| 243 |
+
if usable <= 0:
|
| 244 |
+
raise ValueError(f"Validation split is too short for TRAIN_SEQ_LEN={seq_len}")
|
| 245 |
+
return tokens[: usable + 1]
|
| 246 |
+
|
| 247 |
+
|
| 248 |
+
def eval_val(
|
| 249 |
+
args: Hyperparameters,
|
| 250 |
+
model: nn.Module,
|
| 251 |
+
rank: int,
|
| 252 |
+
world_size: int,
|
| 253 |
+
device: torch.device,
|
| 254 |
+
grad_accum_steps: int,
|
| 255 |
+
val_tokens: Tensor,
|
| 256 |
+
base_bytes_lut: Tensor,
|
| 257 |
+
has_leading_space_lut: Tensor,
|
| 258 |
+
is_boundary_token_lut: Tensor,
|
| 259 |
+
) -> tuple[float, float]:
|
| 260 |
+
local_batch_tokens = args.val_batch_size // (world_size * grad_accum_steps)
|
| 261 |
+
if local_batch_tokens < args.train_seq_len:
|
| 262 |
+
raise ValueError(
|
| 263 |
+
"VAL_BATCH_SIZE must provide at least one sequence per rank; "
|
| 264 |
+
f"got VAL_BATCH_SIZE={args.val_batch_size}, WORLD_SIZE={world_size}, "
|
| 265 |
+
f"GRAD_ACCUM_STEPS={grad_accum_steps}, TRAIN_SEQ_LEN={args.train_seq_len}"
|
| 266 |
+
)
|
| 267 |
+
local_batch_seqs = local_batch_tokens // args.train_seq_len
|
| 268 |
+
total_seqs = (val_tokens.numel() - 1) // args.train_seq_len
|
| 269 |
+
seq_start = (total_seqs * rank) // world_size
|
| 270 |
+
seq_end = (total_seqs * (rank + 1)) // world_size
|
| 271 |
+
val_loss_sum = torch.zeros((), device=device, dtype=torch.float64)
|
| 272 |
+
val_token_count = torch.zeros((), device=device, dtype=torch.float64)
|
| 273 |
+
val_byte_count = torch.zeros((), device=device, dtype=torch.float64)
|
| 274 |
+
|
| 275 |
+
model.eval()
|
| 276 |
+
with torch.inference_mode():
|
| 277 |
+
for batch_seq_start in range(seq_start, seq_end, local_batch_seqs):
|
| 278 |
+
batch_seq_end = min(batch_seq_start + local_batch_seqs, seq_end)
|
| 279 |
+
raw_start = batch_seq_start * args.train_seq_len
|
| 280 |
+
raw_end = batch_seq_end * args.train_seq_len + 1
|
| 281 |
+
local = val_tokens[raw_start:raw_end].to(device=device, dtype=torch.int64, non_blocking=True)
|
| 282 |
+
x = local[:-1].reshape(-1, args.train_seq_len)
|
| 283 |
+
y = local[1:].reshape(-1, args.train_seq_len)
|
| 284 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True):
|
| 285 |
+
batch_loss = model(x, y).detach()
|
| 286 |
+
batch_token_count = float(y.numel())
|
| 287 |
+
val_loss_sum += batch_loss.to(torch.float64) * batch_token_count
|
| 288 |
+
val_token_count += batch_token_count
|
| 289 |
+
prev_ids = x.reshape(-1)
|
| 290 |
+
tgt_ids = y.reshape(-1)
|
| 291 |
+
token_bytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16)
|
| 292 |
+
token_bytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16)
|
| 293 |
+
val_byte_count += token_bytes.to(torch.float64).sum()
|
| 294 |
+
|
| 295 |
+
if dist.is_available() and dist.is_initialized():
|
| 296 |
+
dist.all_reduce(val_loss_sum, op=dist.ReduceOp.SUM)
|
| 297 |
+
dist.all_reduce(val_token_count, op=dist.ReduceOp.SUM)
|
| 298 |
+
dist.all_reduce(val_byte_count, op=dist.ReduceOp.SUM)
|
| 299 |
+
|
| 300 |
+
val_loss = val_loss_sum / val_token_count
|
| 301 |
+
bits_per_token = val_loss.item() / math.log(2.0)
|
| 302 |
+
tokens_per_byte = val_token_count.item() / val_byte_count.item()
|
| 303 |
+
model.train()
|
| 304 |
+
return float(val_loss.item()), float(bits_per_token * tokens_per_byte)
|
| 305 |
+
|
| 306 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 307 |
+
# POST-TRAINING QUANTIZATION (unchanged from baseline)
|
| 308 |
+
# βββοΏ½οΏ½βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 309 |
+
|
| 310 |
+
CONTROL_TENSOR_NAME_PATTERNS = tuple(
|
| 311 |
+
pattern
|
| 312 |
+
for pattern in os.environ.get(
|
| 313 |
+
"CONTROL_TENSOR_NAME_PATTERNS",
|
| 314 |
+
"attn_scale,attn_scales,mlp_scale,mlp_scales,resid_mix,resid_mixes,q_gain,skip_weight,skip_weights",
|
| 315 |
+
).split(",")
|
| 316 |
+
if pattern
|
| 317 |
+
)
|
| 318 |
+
INT8_KEEP_FLOAT_FP32_NAME_PATTERNS = tuple(
|
| 319 |
+
pattern
|
| 320 |
+
for pattern in os.environ.get(
|
| 321 |
+
"INT8_KEEP_FLOAT_FP32_NAME_PATTERNS",
|
| 322 |
+
",".join(CONTROL_TENSOR_NAME_PATTERNS),
|
| 323 |
+
).split(",")
|
| 324 |
+
if pattern
|
| 325 |
+
)
|
| 326 |
+
INT8_KEEP_FLOAT_MAX_NUMEL = 65_536
|
| 327 |
+
INT8_KEEP_FLOAT_STORE_DTYPE = torch.float16
|
| 328 |
+
INT8_PER_ROW_SCALE_DTYPE = torch.float16
|
| 329 |
+
INT8_CLIP_PERCENTILE = 99.99984
|
| 330 |
+
INT8_CLIP_Q = INT8_CLIP_PERCENTILE / 100.0
|
| 331 |
+
|
| 332 |
+
|
| 333 |
+
def tensor_nbytes(t: Tensor) -> int:
|
| 334 |
+
return int(t.numel()) * int(t.element_size())
|
| 335 |
+
|
| 336 |
+
|
| 337 |
+
def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, str]) -> Tensor:
|
| 338 |
+
if any(pattern in name for pattern in INT8_KEEP_FLOAT_FP32_NAME_PATTERNS):
|
| 339 |
+
return t.float().contiguous()
|
| 340 |
+
if t.dtype in {torch.float32, torch.bfloat16}:
|
| 341 |
+
passthrough_orig_dtypes[name] = str(t.dtype).removeprefix("torch.")
|
| 342 |
+
return t.to(dtype=INT8_KEEP_FLOAT_STORE_DTYPE).contiguous()
|
| 343 |
+
return t
|
| 344 |
+
|
| 345 |
+
|
| 346 |
+
def quantize_float_tensor(t: Tensor) -> tuple[Tensor, Tensor]:
|
| 347 |
+
t32 = t.float()
|
| 348 |
+
if t32.ndim == 2:
|
| 349 |
+
clip_abs = (
|
| 350 |
+
torch.quantile(t32.abs(), INT8_CLIP_Q, dim=1)
|
| 351 |
+
if t32.numel()
|
| 352 |
+
else torch.empty((t32.shape[0],), dtype=torch.float32)
|
| 353 |
+
)
|
| 354 |
+
clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None])
|
| 355 |
+
scale = (clip_abs / 127.0).clamp_min(1.0 / 127.0)
|
| 356 |
+
q = torch.clamp(torch.round(clipped / scale[:, None]), -127, 127).to(torch.int8).contiguous()
|
| 357 |
+
return q, scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous()
|
| 358 |
+
clip_abs = float(torch.quantile(t32.abs().flatten(), INT8_CLIP_Q).item()) if t32.numel() else 0.0
|
| 359 |
+
scale = torch.tensor(clip_abs / 127.0 if clip_abs > 0 else 1.0, dtype=torch.float32)
|
| 360 |
+
q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -127, 127).to(torch.int8).contiguous()
|
| 361 |
+
return q, scale
|
| 362 |
+
|
| 363 |
+
|
| 364 |
+
def quantize_state_dict_int8(state_dict: dict[str, Tensor]):
|
| 365 |
+
quantized: dict[str, Tensor] = {}
|
| 366 |
+
scales: dict[str, Tensor] = {}
|
| 367 |
+
dtypes: dict[str, str] = {}
|
| 368 |
+
passthrough: dict[str, Tensor] = {}
|
| 369 |
+
passthrough_orig_dtypes: dict[str, str] = {}
|
| 370 |
+
qmeta: dict[str, dict[str, object]] = {}
|
| 371 |
+
stats = dict.fromkeys(
|
| 372 |
+
("param_count", "num_tensors", "num_float_tensors", "num_nonfloat_tensors", "baseline_tensor_bytes", "int8_payload_bytes"),
|
| 373 |
+
0,
|
| 374 |
+
)
|
| 375 |
+
|
| 376 |
+
for name, tensor in state_dict.items():
|
| 377 |
+
t = tensor.detach().to("cpu").contiguous()
|
| 378 |
+
stats["param_count"] += int(t.numel())
|
| 379 |
+
stats["num_tensors"] += 1
|
| 380 |
+
stats["baseline_tensor_bytes"] += tensor_nbytes(t)
|
| 381 |
+
|
| 382 |
+
if not t.is_floating_point():
|
| 383 |
+
stats["num_nonfloat_tensors"] += 1
|
| 384 |
+
passthrough[name] = t
|
| 385 |
+
stats["int8_payload_bytes"] += tensor_nbytes(t)
|
| 386 |
+
continue
|
| 387 |
+
|
| 388 |
+
if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL:
|
| 389 |
+
kept = keep_float_tensor(name, t, passthrough_orig_dtypes)
|
| 390 |
+
passthrough[name] = kept
|
| 391 |
+
stats["int8_payload_bytes"] += tensor_nbytes(kept)
|
| 392 |
+
continue
|
| 393 |
+
|
| 394 |
+
stats["num_float_tensors"] += 1
|
| 395 |
+
q, s = quantize_float_tensor(t)
|
| 396 |
+
if s.ndim > 0:
|
| 397 |
+
qmeta[name] = {"scheme": "per_row", "axis": 0}
|
| 398 |
+
quantized[name] = q
|
| 399 |
+
scales[name] = s
|
| 400 |
+
dtypes[name] = str(t.dtype).removeprefix("torch.")
|
| 401 |
+
stats["int8_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s)
|
| 402 |
+
|
| 403 |
+
obj: dict[str, object] = {
|
| 404 |
+
"__quant_format__": "int8_clean_per_row_v1",
|
| 405 |
+
"quantized": quantized,
|
| 406 |
+
"scales": scales,
|
| 407 |
+
"dtypes": dtypes,
|
| 408 |
+
"passthrough": passthrough,
|
| 409 |
+
}
|
| 410 |
+
if qmeta:
|
| 411 |
+
obj["qmeta"] = qmeta
|
| 412 |
+
if passthrough_orig_dtypes:
|
| 413 |
+
obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes
|
| 414 |
+
return obj, stats
|
| 415 |
+
|
| 416 |
+
|
| 417 |
+
def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]:
|
| 418 |
+
out: dict[str, Tensor] = {}
|
| 419 |
+
qmeta = obj.get("qmeta", {})
|
| 420 |
+
passthrough_orig_dtypes = obj.get("passthrough_orig_dtypes", {})
|
| 421 |
+
for name, q in obj["quantized"].items():
|
| 422 |
+
dtype = getattr(torch, obj["dtypes"][name])
|
| 423 |
+
s = obj["scales"][name]
|
| 424 |
+
if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0:
|
| 425 |
+
s = s.to(dtype=torch.float32)
|
| 426 |
+
out[name] = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous()
|
| 427 |
+
else:
|
| 428 |
+
scale = float(s.item())
|
| 429 |
+
out[name] = (q.float() * scale).to(dtype=dtype).contiguous()
|
| 430 |
+
for name, t in obj["passthrough"].items():
|
| 431 |
+
out_t = t.detach().to("cpu").contiguous()
|
| 432 |
+
orig_dtype = passthrough_orig_dtypes.get(name)
|
| 433 |
+
if isinstance(orig_dtype, str):
|
| 434 |
+
out_t = out_t.to(dtype=getattr(torch, orig_dtype)).contiguous()
|
| 435 |
+
out[name] = out_t
|
| 436 |
+
return out
|
| 437 |
+
|
| 438 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 439 |
+
# DATA LOADING (unchanged from baseline)
|
| 440 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 441 |
+
|
| 442 |
+
def load_data_shard(file: Path) -> Tensor:
|
| 443 |
+
header_bytes = 256 * np.dtype("<i4").itemsize
|
| 444 |
+
token_bytes = np.dtype("<u2").itemsize
|
| 445 |
+
header = np.fromfile(file, dtype="<i4", count=256)
|
| 446 |
+
if header.size != 256 or int(header[0]) != 20240520 or int(header[1]) != 1:
|
| 447 |
+
raise ValueError(f"Unexpected shard header for {file}")
|
| 448 |
+
num_tokens = int(header[2])
|
| 449 |
+
expected_size = header_bytes + num_tokens * token_bytes
|
| 450 |
+
if file.stat().st_size != expected_size:
|
| 451 |
+
raise ValueError(f"Shard size mismatch for {file}: expected {expected_size} bytes")
|
| 452 |
+
tokens_np = np.fromfile(file, dtype="<u2", count=num_tokens, offset=header_bytes)
|
| 453 |
+
if tokens_np.size != num_tokens:
|
| 454 |
+
raise ValueError(f"Short read for {file}")
|
| 455 |
+
return torch.from_numpy(tokens_np.astype(np.uint16, copy=False))
|
| 456 |
+
|
| 457 |
+
|
| 458 |
+
class TokenStream:
|
| 459 |
+
def __init__(self, pattern: str):
|
| 460 |
+
self.files = [Path(p) for p in sorted(glob.glob(pattern))]
|
| 461 |
+
if not self.files:
|
| 462 |
+
raise FileNotFoundError(f"No files found for pattern: {pattern}")
|
| 463 |
+
self.file_idx = 0
|
| 464 |
+
self.tokens = load_data_shard(self.files[0])
|
| 465 |
+
self.pos = 0
|
| 466 |
+
|
| 467 |
+
def _advance_file(self) -> None:
|
| 468 |
+
self.file_idx = (self.file_idx + 1) % len(self.files)
|
| 469 |
+
self.tokens = load_data_shard(self.files[self.file_idx])
|
| 470 |
+
self.pos = 0
|
| 471 |
+
|
| 472 |
+
def take(self, n: int) -> Tensor:
|
| 473 |
+
chunks: list[Tensor] = []
|
| 474 |
+
remaining = n
|
| 475 |
+
while remaining > 0:
|
| 476 |
+
avail = self.tokens.numel() - self.pos
|
| 477 |
+
if avail <= 0:
|
| 478 |
+
self._advance_file()
|
| 479 |
+
continue
|
| 480 |
+
k = min(remaining, avail)
|
| 481 |
+
chunks.append(self.tokens[self.pos : self.pos + k])
|
| 482 |
+
self.pos += k
|
| 483 |
+
remaining -= k
|
| 484 |
+
return chunks[0] if len(chunks) == 1 else torch.cat(chunks)
|
| 485 |
+
|
| 486 |
+
|
| 487 |
+
class DistributedTokenLoader:
|
| 488 |
+
def __init__(self, pattern: str, rank: int, world_size: int, device: torch.device):
|
| 489 |
+
self.rank = rank
|
| 490 |
+
self.world_size = world_size
|
| 491 |
+
self.device = device
|
| 492 |
+
self.stream = TokenStream(pattern)
|
| 493 |
+
|
| 494 |
+
def next_batch(self, global_tokens: int, seq_len: int, grad_accum_steps: int) -> tuple[Tensor, Tensor]:
|
| 495 |
+
local_tokens = global_tokens // (self.world_size * grad_accum_steps)
|
| 496 |
+
per_rank_span = local_tokens + 1
|
| 497 |
+
chunk = self.stream.take(per_rank_span * self.world_size)
|
| 498 |
+
start = self.rank * per_rank_span
|
| 499 |
+
local = chunk[start : start + per_rank_span].to(dtype=torch.int64)
|
| 500 |
+
x = local[:-1].reshape(-1, seq_len)
|
| 501 |
+
y = local[1:].reshape(-1, seq_len)
|
| 502 |
+
return x.to(self.device, non_blocking=True), y.to(self.device, non_blocking=True)
|
| 503 |
+
|
| 504 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 505 |
+
# TRANSFORMER MODULES β KERNEL EDITION
|
| 506 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 507 |
+
|
| 508 |
+
class RMSNorm(nn.Module):
|
| 509 |
+
def __init__(self, eps: float | None = None):
|
| 510 |
+
super().__init__()
|
| 511 |
+
self.eps = eps
|
| 512 |
+
|
| 513 |
+
def forward(self, x: Tensor) -> Tensor:
|
| 514 |
+
return F.rms_norm(x, (x.size(-1),), eps=self.eps)
|
| 515 |
+
|
| 516 |
+
|
| 517 |
+
class CastedLinear(nn.Linear):
|
| 518 |
+
def forward(self, x: Tensor) -> Tensor:
|
| 519 |
+
bias = self.bias.to(x.dtype) if self.bias is not None else None
|
| 520 |
+
return F.linear(x, self.weight.to(x.dtype), bias)
|
| 521 |
+
|
| 522 |
+
|
| 523 |
+
def restore_low_dim_params_to_fp32(module: nn.Module) -> None:
|
| 524 |
+
with torch.no_grad():
|
| 525 |
+
for name, param in module.named_parameters():
|
| 526 |
+
if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32:
|
| 527 |
+
param.data = param.data.float()
|
| 528 |
+
|
| 529 |
+
|
| 530 |
+
class Rotary(nn.Module):
|
| 531 |
+
def __init__(self, dim: int, base: float = 10000.0):
|
| 532 |
+
super().__init__()
|
| 533 |
+
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim))
|
| 534 |
+
self.register_buffer("inv_freq", inv_freq, persistent=False)
|
| 535 |
+
self._seq_len_cached = 0
|
| 536 |
+
self._cos_cached: Tensor | None = None
|
| 537 |
+
self._sin_cached: Tensor | None = None
|
| 538 |
+
|
| 539 |
+
def forward(self, seq_len: int, device: torch.device, dtype: torch.dtype) -> tuple[Tensor, Tensor]:
|
| 540 |
+
if (
|
| 541 |
+
self._cos_cached is None
|
| 542 |
+
or self._sin_cached is None
|
| 543 |
+
or self._seq_len_cached != seq_len
|
| 544 |
+
or self._cos_cached.device != device
|
| 545 |
+
):
|
| 546 |
+
t = torch.arange(seq_len, device=device, dtype=self.inv_freq.dtype)
|
| 547 |
+
freqs = torch.outer(t, self.inv_freq.to(device))
|
| 548 |
+
self._cos_cached = freqs.cos()[None, None, :, :]
|
| 549 |
+
self._sin_cached = freqs.sin()[None, None, :, :]
|
| 550 |
+
self._seq_len_cached = seq_len
|
| 551 |
+
return self._cos_cached.to(dtype=dtype), self._sin_cached.to(dtype=dtype)
|
| 552 |
+
|
| 553 |
+
|
| 554 |
+
def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor) -> Tensor:
|
| 555 |
+
half = x.size(-1) // 2
|
| 556 |
+
x1, x2 = x[..., :half], x[..., half:]
|
| 557 |
+
return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1)
|
| 558 |
+
|
| 559 |
+
|
| 560 |
+
class CausalSelfAttention(nn.Module):
|
| 561 |
+
def __init__(self, dim: int, num_heads: int, num_kv_heads: int, rope_base: float, qk_gain_init: float):
|
| 562 |
+
super().__init__()
|
| 563 |
+
if dim % num_heads != 0:
|
| 564 |
+
raise ValueError("model_dim must be divisible by num_heads")
|
| 565 |
+
if num_heads % num_kv_heads != 0:
|
| 566 |
+
raise ValueError("num_heads must be divisible by num_kv_heads")
|
| 567 |
+
self.num_heads = num_heads
|
| 568 |
+
self.num_kv_heads = num_kv_heads
|
| 569 |
+
self.head_dim = dim // num_heads
|
| 570 |
+
if self.head_dim % 2 != 0:
|
| 571 |
+
raise ValueError("head_dim must be even for RoPE")
|
| 572 |
+
kv_dim = self.num_kv_heads * self.head_dim
|
| 573 |
+
self.c_q = CastedLinear(dim, dim, bias=False)
|
| 574 |
+
self.c_k = CastedLinear(dim, kv_dim, bias=False)
|
| 575 |
+
self.c_v = CastedLinear(dim, kv_dim, bias=False)
|
| 576 |
+
self.proj = CastedLinear(dim, dim, bias=False)
|
| 577 |
+
self.proj._zero_init = True
|
| 578 |
+
self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32))
|
| 579 |
+
self.rotary = Rotary(self.head_dim, base=rope_base)
|
| 580 |
+
|
| 581 |
+
def forward(self, x: Tensor) -> Tensor:
|
| 582 |
+
bsz, seqlen, dim = x.shape
|
| 583 |
+
q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2)
|
| 584 |
+
k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2)
|
| 585 |
+
v = self.c_v(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2)
|
| 586 |
+
q = F.rms_norm(q, (q.size(-1),))
|
| 587 |
+
k = F.rms_norm(k, (k.size(-1),))
|
| 588 |
+
cos, sin = self.rotary(seqlen, x.device, q.dtype)
|
| 589 |
+
q = apply_rotary_emb(q, cos, sin)
|
| 590 |
+
k = apply_rotary_emb(k, cos, sin)
|
| 591 |
+
q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None]
|
| 592 |
+
y = F.scaled_dot_product_attention(
|
| 593 |
+
q, k, v,
|
| 594 |
+
attn_mask=None,
|
| 595 |
+
is_causal=True,
|
| 596 |
+
enable_gqa=(self.num_kv_heads != self.num_heads),
|
| 597 |
+
)
|
| 598 |
+
y = y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim)
|
| 599 |
+
return self.proj(y)
|
| 600 |
+
|
| 601 |
+
|
| 602 |
+
def coherence(x: Tensor) -> Tensor:
|
| 603 |
+
"""Coherence activation C(r) = 2r / (1 + rΒ²).
|
| 604 |
+
|
| 605 |
+
Defined in CriticalEigenvalue.lean Β§5. Machine-checked properties:
|
| 606 |
+
β’ C(r) β€ 1 for r β₯ 0 (AMβGM: 1 + rΒ² β₯ 2r)
|
| 607 |
+
β’ C(1) = 1 (unique maximum on ββΊ)
|
| 608 |
+
β’ C(βr) = βC(r) (odd / anti-symmetric)
|
| 609 |
+
β’ Pythagorean: C(r)Β² + ((rΒ²β1)/(1+rΒ²))Β² = 1 (Β§18)
|
| 610 |
+
β’ Lyapunov: C(exp Ξ») = sech Ξ» (Β§10)
|
| 611 |
+
|
| 612 |
+
As a neural activation this is a smooth, bounded nonlinearity with
|
| 613 |
+
range (β1, 1), zero at the origin, unit gradient at zero, and graceful
|
| 614 |
+
saturation for large |r| β resembling a normalised sinc or tanh but
|
| 615 |
+
with a closed-form Pythagorean partner.
|
| 616 |
+
"""
|
| 617 |
+
return 2.0 * x / (1.0 + x.square())
|
| 618 |
+
|
| 619 |
+
|
| 620 |
+
class CoherenceMLP(nn.Module):
|
| 621 |
+
"""MLP block whose hidden width is βΞ΄SΒ·dimβ and activation is C(r).
|
| 622 |
+
|
| 623 |
+
Both choices come directly from formal-lean/CriticalEigenvalue.lean:
|
| 624 |
+
β’ Ξ΄S = 1+β2 (silver ratio, Β§7 Proposition 4) determines hidden width.
|
| 625 |
+
β’ C(r) = 2r/(1+rΒ²) (coherence function, Β§5) is the nonlinearity.
|
| 626 |
+
|
| 627 |
+
The silver ratio satisfies Ξ΄S = 2 + 1/Ξ΄S (self-similarity Β§20), so the
|
| 628 |
+
expansion is slightly larger than 2Γ but smaller than 3Γ, giving a
|
| 629 |
+
natural intermediate width that is mathematically well-motivated.
|
| 630 |
+
"""
|
| 631 |
+
|
| 632 |
+
def __init__(self, dim: int, hidden: int):
|
| 633 |
+
super().__init__()
|
| 634 |
+
self.fc = CastedLinear(dim, hidden, bias=False)
|
| 635 |
+
self.proj = CastedLinear(hidden, dim, bias=False)
|
| 636 |
+
self.proj._zero_init = True
|
| 637 |
+
|
| 638 |
+
def forward(self, x: Tensor) -> Tensor:
|
| 639 |
+
return self.proj(coherence(self.fc(x)))
|
| 640 |
+
|
| 641 |
+
|
| 642 |
+
class KernelBlock(nn.Module):
|
| 643 |
+
"""Transformer block using CoherenceMLP in place of the baseline reluΒ² MLP."""
|
| 644 |
+
|
| 645 |
+
def __init__(self, dim: int, num_heads: int, num_kv_heads: int, mlp_hidden: int, rope_base: float, qk_gain_init: float):
|
| 646 |
+
super().__init__()
|
| 647 |
+
self.attn_norm = RMSNorm()
|
| 648 |
+
self.mlp_norm = RMSNorm()
|
| 649 |
+
self.attn = CausalSelfAttention(dim, num_heads, num_kv_heads, rope_base, qk_gain_init)
|
| 650 |
+
self.mlp = CoherenceMLP(dim, mlp_hidden)
|
| 651 |
+
self.attn_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32))
|
| 652 |
+
self.mlp_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32))
|
| 653 |
+
self.resid_mix = nn.Parameter(torch.stack((torch.ones(dim), torch.zeros(dim))).float())
|
| 654 |
+
|
| 655 |
+
def forward(self, x: Tensor, x0: Tensor) -> Tensor:
|
| 656 |
+
mix = self.resid_mix.to(dtype=x.dtype)
|
| 657 |
+
x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0
|
| 658 |
+
attn_out = self.attn(self.attn_norm(x))
|
| 659 |
+
x = x + self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out
|
| 660 |
+
x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x))
|
| 661 |
+
return x
|
| 662 |
+
|
| 663 |
+
|
| 664 |
+
class KernelGPT(nn.Module):
|
| 665 |
+
"""GPT variant implementing the Kernel eigenvalue formalization.
|
| 666 |
+
|
| 667 |
+
Architectural choices derived from CriticalEigenvalue.lean:
|
| 668 |
+
β’ num_heads = 8 (one slot per Z/8Z orbit element, Β§15)
|
| 669 |
+
β’ MLP hidden = βΞ΄S Β· model_dimβ (silver ratio expansion, Β§7)
|
| 670 |
+
β’ MLP activation = C(r) = 2r/(1+rΒ²) (coherence function, Β§5)
|
| 671 |
+
"""
|
| 672 |
+
|
| 673 |
+
def __init__(
|
| 674 |
+
self,
|
| 675 |
+
vocab_size: int,
|
| 676 |
+
num_layers: int,
|
| 677 |
+
model_dim: int,
|
| 678 |
+
num_heads: int,
|
| 679 |
+
num_kv_heads: int,
|
| 680 |
+
mlp_hidden: int,
|
| 681 |
+
tie_embeddings: bool,
|
| 682 |
+
tied_embed_init_std: float,
|
| 683 |
+
logit_softcap: float,
|
| 684 |
+
rope_base: float,
|
| 685 |
+
qk_gain_init: float,
|
| 686 |
+
):
|
| 687 |
+
super().__init__()
|
| 688 |
+
if logit_softcap <= 0.0:
|
| 689 |
+
raise ValueError(f"logit_softcap must be positive, got {logit_softcap}")
|
| 690 |
+
self.tie_embeddings = tie_embeddings
|
| 691 |
+
self.tied_embed_init_std = tied_embed_init_std
|
| 692 |
+
self.logit_softcap = logit_softcap
|
| 693 |
+
self.tok_emb = nn.Embedding(vocab_size, model_dim)
|
| 694 |
+
self.num_encoder_layers = num_layers // 2
|
| 695 |
+
self.num_decoder_layers = num_layers - self.num_encoder_layers
|
| 696 |
+
self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers)
|
| 697 |
+
self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32))
|
| 698 |
+
self.blocks = nn.ModuleList(
|
| 699 |
+
[
|
| 700 |
+
KernelBlock(model_dim, num_heads, num_kv_heads, mlp_hidden, rope_base, qk_gain_init)
|
| 701 |
+
for _ in range(num_layers)
|
| 702 |
+
]
|
| 703 |
+
)
|
| 704 |
+
self.final_norm = RMSNorm()
|
| 705 |
+
self.lm_head = None if tie_embeddings else CastedLinear(model_dim, vocab_size, bias=False)
|
| 706 |
+
if self.lm_head is not None:
|
| 707 |
+
self.lm_head._zero_init = True
|
| 708 |
+
self._init_weights()
|
| 709 |
+
|
| 710 |
+
def _init_weights(self) -> None:
|
| 711 |
+
if self.tie_embeddings:
|
| 712 |
+
nn.init.normal_(self.tok_emb.weight, mean=0.0, std=self.tied_embed_init_std)
|
| 713 |
+
for module in self.modules():
|
| 714 |
+
if isinstance(module, nn.Linear) and getattr(module, "_zero_init", False):
|
| 715 |
+
nn.init.zeros_(module.weight)
|
| 716 |
+
|
| 717 |
+
def forward(self, input_ids: Tensor, target_ids: Tensor) -> Tensor:
|
| 718 |
+
x = self.tok_emb(input_ids)
|
| 719 |
+
x = F.rms_norm(x, (x.size(-1),))
|
| 720 |
+
x0 = x
|
| 721 |
+
skips: list[Tensor] = []
|
| 722 |
+
|
| 723 |
+
for i in range(self.num_encoder_layers):
|
| 724 |
+
x = self.blocks[i](x, x0)
|
| 725 |
+
skips.append(x)
|
| 726 |
+
for i in range(self.num_decoder_layers):
|
| 727 |
+
if skips:
|
| 728 |
+
x = x + self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skips.pop()
|
| 729 |
+
x = self.blocks[self.num_encoder_layers + i](x, x0)
|
| 730 |
+
|
| 731 |
+
x = self.final_norm(x).reshape(-1, x.size(-1))
|
| 732 |
+
targets = target_ids.reshape(-1)
|
| 733 |
+
if self.tie_embeddings:
|
| 734 |
+
logits_proj = F.linear(x, self.tok_emb.weight)
|
| 735 |
+
else:
|
| 736 |
+
if self.lm_head is None:
|
| 737 |
+
raise RuntimeError("lm_head is required when tie_embeddings=False")
|
| 738 |
+
logits_proj = self.lm_head(x)
|
| 739 |
+
logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap)
|
| 740 |
+
return F.cross_entropy(logits.float(), targets, reduction="mean")
|
| 741 |
+
|
| 742 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 743 |
+
# TRAINING
|
| 744 |
+
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 745 |
+
|
| 746 |
+
def main() -> None:
|
| 747 |
+
global zeropower_via_newtonschulz5
|
| 748 |
+
|
| 749 |
+
code = Path(__file__).read_text(encoding="utf-8")
|
| 750 |
+
args = Hyperparameters()
|
| 751 |
+
zeropower_via_newtonschulz5 = torch.compile(zeropower_via_newtonschulz5)
|
| 752 |
+
|
| 753 |
+
# ββ Distributed + CUDA setup ββββββββββββββββββββββββββββββββββββββββββββββ
|
| 754 |
+
|
| 755 |
+
distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ
|
| 756 |
+
rank = int(os.environ.get("RANK", "0"))
|
| 757 |
+
world_size = int(os.environ.get("WORLD_SIZE", "1"))
|
| 758 |
+
local_rank = int(os.environ.get("LOCAL_RANK", "0"))
|
| 759 |
+
if world_size <= 0:
|
| 760 |
+
raise ValueError(f"WORLD_SIZE must be positive, got {world_size}")
|
| 761 |
+
if 8 % world_size != 0:
|
| 762 |
+
raise ValueError(f"WORLD_SIZE={world_size} must divide 8 so grad_accum_steps stays integral")
|
| 763 |
+
grad_accum_steps = 8 // world_size
|
| 764 |
+
grad_scale = 1.0 / grad_accum_steps
|
| 765 |
+
if not torch.cuda.is_available():
|
| 766 |
+
raise RuntimeError("CUDA is required")
|
| 767 |
+
device = torch.device("cuda", local_rank)
|
| 768 |
+
torch.cuda.set_device(device)
|
| 769 |
+
if distributed:
|
| 770 |
+
dist.init_process_group(backend="nccl", device_id=device)
|
| 771 |
+
dist.barrier()
|
| 772 |
+
master_process = rank == 0
|
| 773 |
+
|
| 774 |
+
torch.backends.cuda.matmul.allow_tf32 = True
|
| 775 |
+
torch.backends.cudnn.allow_tf32 = True
|
| 776 |
+
from torch.backends.cuda import enable_cudnn_sdp, enable_flash_sdp, enable_math_sdp, enable_mem_efficient_sdp
|
| 777 |
+
enable_cudnn_sdp(False)
|
| 778 |
+
enable_flash_sdp(True)
|
| 779 |
+
enable_mem_efficient_sdp(False)
|
| 780 |
+
enable_math_sdp(False)
|
| 781 |
+
|
| 782 |
+
logfile = None
|
| 783 |
+
if master_process:
|
| 784 |
+
os.makedirs("logs", exist_ok=True)
|
| 785 |
+
logfile = f"logs/{args.run_id}.txt"
|
| 786 |
+
print(logfile)
|
| 787 |
+
|
| 788 |
+
def log0(msg: str, console: bool = True) -> None:
|
| 789 |
+
if not master_process:
|
| 790 |
+
return
|
| 791 |
+
if console:
|
| 792 |
+
print(msg)
|
| 793 |
+
if logfile is not None:
|
| 794 |
+
with open(logfile, "a", encoding="utf-8") as f:
|
| 795 |
+
print(msg, file=f)
|
| 796 |
+
|
| 797 |
+
log0(code, console=False)
|
| 798 |
+
log0("=" * 100, console=False)
|
| 799 |
+
log0(f"Running Python {sys.version}", console=False)
|
| 800 |
+
log0(f"Running PyTorch {torch.__version__}", console=False)
|
| 801 |
+
log0(
|
| 802 |
+
subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False).stdout,
|
| 803 |
+
console=False,
|
| 804 |
+
)
|
| 805 |
+
log0("=" * 100, console=False)
|
| 806 |
+
|
| 807 |
+
# ββ Kernel constants log ββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 808 |
+
log0(f"kernel:silver_ratio:{SILVER_RATIO:.6f} mu_angle_deg:{math.degrees(MU_ANGLE):.1f} orbit_size:{MU_ORBIT_SIZE}")
|
| 809 |
+
log0(f"kernel:mlp_hidden:{args.mlp_hidden} (={args.mlp_hidden / args.model_dim:.4f}x model_dim)")
|
| 810 |
+
|
| 811 |
+
# ββ Seed + tokenizer ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 812 |
+
|
| 813 |
+
random.seed(args.seed)
|
| 814 |
+
np.random.seed(args.seed)
|
| 815 |
+
torch.manual_seed(args.seed)
|
| 816 |
+
torch.cuda.manual_seed_all(args.seed)
|
| 817 |
+
|
| 818 |
+
if not args.tokenizer_path.endswith(".model"):
|
| 819 |
+
raise ValueError(f"Script only setup for SentencePiece .model file: {args.tokenizer_path}")
|
| 820 |
+
sp = spm.SentencePieceProcessor(model_file=args.tokenizer_path)
|
| 821 |
+
if int(sp.vocab_size()) != args.vocab_size:
|
| 822 |
+
raise ValueError(
|
| 823 |
+
f"VOCAB_SIZE={args.vocab_size} does not match tokenizer vocab_size={int(sp.vocab_size())}"
|
| 824 |
+
)
|
| 825 |
+
dataset_dir = Path(args.data_path).resolve()
|
| 826 |
+
actual_train_files = len(list(dataset_dir.glob("fineweb_train_*.bin")))
|
| 827 |
+
val_tokens = load_validation_tokens(args.val_files, args.train_seq_len)
|
| 828 |
+
base_bytes_lut, has_leading_space_lut, is_boundary_token_lut = build_sentencepiece_luts(
|
| 829 |
+
sp, args.vocab_size, device
|
| 830 |
+
)
|
| 831 |
+
log0(f"val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path={args.tokenizer_path}")
|
| 832 |
+
log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}")
|
| 833 |
+
log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}")
|
| 834 |
+
|
| 835 |
+
# ββ Model + optimizer setup βββββββββββββββββββββββββββββββββββββββββββββββ
|
| 836 |
+
|
| 837 |
+
base_model = KernelGPT(
|
| 838 |
+
vocab_size=args.vocab_size,
|
| 839 |
+
num_layers=args.num_layers,
|
| 840 |
+
model_dim=args.model_dim,
|
| 841 |
+
num_heads=args.num_heads,
|
| 842 |
+
num_kv_heads=args.num_kv_heads,
|
| 843 |
+
mlp_hidden=args.mlp_hidden,
|
| 844 |
+
tie_embeddings=args.tie_embeddings,
|
| 845 |
+
tied_embed_init_std=args.tied_embed_init_std,
|
| 846 |
+
logit_softcap=args.logit_softcap,
|
| 847 |
+
rope_base=args.rope_base,
|
| 848 |
+
qk_gain_init=args.qk_gain_init,
|
| 849 |
+
).to(device).bfloat16()
|
| 850 |
+
for module in base_model.modules():
|
| 851 |
+
if isinstance(module, CastedLinear):
|
| 852 |
+
module.float()
|
| 853 |
+
restore_low_dim_params_to_fp32(base_model)
|
| 854 |
+
compiled_model = torch.compile(base_model, dynamic=False, fullgraph=True)
|
| 855 |
+
model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model
|
| 856 |
+
|
| 857 |
+
block_named_params = list(base_model.blocks.named_parameters())
|
| 858 |
+
matrix_params = [
|
| 859 |
+
p
|
| 860 |
+
for name, p in block_named_params
|
| 861 |
+
if p.ndim == 2 and not any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)
|
| 862 |
+
]
|
| 863 |
+
scalar_params = [
|
| 864 |
+
p
|
| 865 |
+
for name, p in block_named_params
|
| 866 |
+
if p.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)
|
| 867 |
+
]
|
| 868 |
+
if base_model.skip_weights.numel() > 0:
|
| 869 |
+
scalar_params.append(base_model.skip_weights)
|
| 870 |
+
token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr
|
| 871 |
+
optimizer_tok = torch.optim.Adam(
|
| 872 |
+
[{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr}],
|
| 873 |
+
betas=(args.beta1, args.beta2), eps=args.adam_eps, fused=True,
|
| 874 |
+
)
|
| 875 |
+
optimizer_muon = Muon(matrix_params, lr=args.matrix_lr, momentum=args.muon_momentum, backend_steps=args.muon_backend_steps)
|
| 876 |
+
for group in optimizer_muon.param_groups:
|
| 877 |
+
group["base_lr"] = args.matrix_lr
|
| 878 |
+
optimizer_scalar = torch.optim.Adam(
|
| 879 |
+
[{"params": scalar_params, "lr": args.scalar_lr, "base_lr": args.scalar_lr}],
|
| 880 |
+
betas=(args.beta1, args.beta2), eps=args.adam_eps, fused=True,
|
| 881 |
+
)
|
| 882 |
+
optimizers: list[torch.optim.Optimizer] = [optimizer_tok, optimizer_muon, optimizer_scalar]
|
| 883 |
+
if base_model.lm_head is not None:
|
| 884 |
+
optimizer_head = torch.optim.Adam(
|
| 885 |
+
[{"params": [base_model.lm_head.weight], "lr": args.head_lr, "base_lr": args.head_lr}],
|
| 886 |
+
betas=(args.beta1, args.beta2), eps=args.adam_eps, fused=True,
|
| 887 |
+
)
|
| 888 |
+
optimizers.insert(1, optimizer_head)
|
| 889 |
+
|
| 890 |
+
n_params = sum(p.numel() for p in base_model.parameters())
|
| 891 |
+
log0(f"model_params:{n_params}")
|
| 892 |
+
log0(f"world_size:{world_size} grad_accum_steps:{grad_accum_steps}")
|
| 893 |
+
log0("sdp_backends:cudnn=False flash=True mem_efficient=False math=False")
|
| 894 |
+
log0(f"attention_mode:gqa num_heads:{args.num_heads} num_kv_heads:{args.num_kv_heads}")
|
| 895 |
+
log0(
|
| 896 |
+
f"tie_embeddings:{args.tie_embeddings} embed_lr:{token_lr} "
|
| 897 |
+
f"head_lr:{args.head_lr if base_model.lm_head is not None else 0.0} "
|
| 898 |
+
f"matrix_lr:{args.matrix_lr} scalar_lr:{args.scalar_lr}"
|
| 899 |
+
)
|
| 900 |
+
log0(
|
| 901 |
+
f"train_batch_tokens:{args.train_batch_tokens} train_seq_len:{args.train_seq_len} "
|
| 902 |
+
f"iterations:{args.iterations} warmup_steps:{args.warmup_steps} "
|
| 903 |
+
f"max_wallclock_seconds:{args.max_wallclock_seconds:.3f}"
|
| 904 |
+
)
|
| 905 |
+
log0(f"seed:{args.seed}")
|
| 906 |
+
|
| 907 |
+
# ββ Data loader & model warmup βββββββββββββββββββββββββββββββββββββββββββββ
|
| 908 |
+
|
| 909 |
+
train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device)
|
| 910 |
+
|
| 911 |
+
def zero_grad_all() -> None:
|
| 912 |
+
for opt in optimizers:
|
| 913 |
+
opt.zero_grad(set_to_none=True)
|
| 914 |
+
|
| 915 |
+
max_wallclock_ms = 1000.0 * args.max_wallclock_seconds if args.max_wallclock_seconds > 0 else None
|
| 916 |
+
|
| 917 |
+
def lr_mul(step: int, elapsed_ms: float) -> float:
|
| 918 |
+
if args.warmdown_iters <= 0:
|
| 919 |
+
return 1.0
|
| 920 |
+
if max_wallclock_ms is None:
|
| 921 |
+
warmdown_start = max(args.iterations - args.warmdown_iters, 0)
|
| 922 |
+
return max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0
|
| 923 |
+
step_ms = elapsed_ms / max(step, 1)
|
| 924 |
+
warmdown_ms = args.warmdown_iters * step_ms
|
| 925 |
+
remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0)
|
| 926 |
+
return remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0
|
| 927 |
+
|
| 928 |
+
if args.warmup_steps > 0:
|
| 929 |
+
initial_model_state = {name: tensor.detach().cpu().clone() for name, tensor in base_model.state_dict().items()}
|
| 930 |
+
initial_optimizer_states = [copy.deepcopy(opt.state_dict()) for opt in optimizers]
|
| 931 |
+
model.train()
|
| 932 |
+
for warmup_step in range(args.warmup_steps):
|
| 933 |
+
zero_grad_all()
|
| 934 |
+
for micro_step in range(grad_accum_steps):
|
| 935 |
+
if distributed:
|
| 936 |
+
model.require_backward_grad_sync = micro_step == grad_accum_steps - 1
|
| 937 |
+
x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps)
|
| 938 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True):
|
| 939 |
+
warmup_loss = model(x, y)
|
| 940 |
+
(warmup_loss * grad_scale).backward()
|
| 941 |
+
for opt in optimizers:
|
| 942 |
+
opt.step()
|
| 943 |
+
zero_grad_all()
|
| 944 |
+
if args.warmup_steps <= 20 or (warmup_step + 1) % 10 == 0 or warmup_step + 1 == args.warmup_steps:
|
| 945 |
+
log0(f"warmup_step:{warmup_step + 1}/{args.warmup_steps}")
|
| 946 |
+
base_model.load_state_dict(initial_model_state, strict=True)
|
| 947 |
+
for opt, state in zip(optimizers, initial_optimizer_states, strict=True):
|
| 948 |
+
opt.load_state_dict(state)
|
| 949 |
+
zero_grad_all()
|
| 950 |
+
if distributed:
|
| 951 |
+
model.require_backward_grad_sync = True
|
| 952 |
+
train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device)
|
| 953 |
+
|
| 954 |
+
# ββ Main training loop ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 955 |
+
|
| 956 |
+
training_time_ms = 0.0
|
| 957 |
+
stop_after_step: int | None = None
|
| 958 |
+
torch.cuda.synchronize()
|
| 959 |
+
t0 = time.perf_counter()
|
| 960 |
+
|
| 961 |
+
step = 0
|
| 962 |
+
while True:
|
| 963 |
+
last_step = step == args.iterations or (stop_after_step is not None and step >= stop_after_step)
|
| 964 |
+
|
| 965 |
+
should_validate = last_step or (args.val_loss_every > 0 and step % args.val_loss_every == 0)
|
| 966 |
+
if should_validate:
|
| 967 |
+
torch.cuda.synchronize()
|
| 968 |
+
training_time_ms += 1000.0 * (time.perf_counter() - t0)
|
| 969 |
+
val_loss, val_bpb = eval_val(
|
| 970 |
+
args, model, rank, world_size, device, grad_accum_steps,
|
| 971 |
+
val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut,
|
| 972 |
+
)
|
| 973 |
+
log0(
|
| 974 |
+
f"step:{step}/{args.iterations} val_loss:{val_loss:.4f} val_bpb:{val_bpb:.4f} "
|
| 975 |
+
f"train_time:{training_time_ms:.0f}ms step_avg:{training_time_ms / max(step, 1):.2f}ms"
|
| 976 |
+
)
|
| 977 |
+
torch.cuda.synchronize()
|
| 978 |
+
t0 = time.perf_counter()
|
| 979 |
+
|
| 980 |
+
if last_step:
|
| 981 |
+
if stop_after_step is not None and step < args.iterations:
|
| 982 |
+
log0(
|
| 983 |
+
f"stopping_early: wallclock_cap train_time:{training_time_ms:.0f}ms "
|
| 984 |
+
f"step:{step}/{args.iterations}"
|
| 985 |
+
)
|
| 986 |
+
break
|
| 987 |
+
|
| 988 |
+
elapsed_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0)
|
| 989 |
+
scale = lr_mul(step, elapsed_ms)
|
| 990 |
+
zero_grad_all()
|
| 991 |
+
train_loss = torch.zeros((), device=device)
|
| 992 |
+
for micro_step in range(grad_accum_steps):
|
| 993 |
+
if distributed:
|
| 994 |
+
model.require_backward_grad_sync = micro_step == grad_accum_steps - 1
|
| 995 |
+
x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps)
|
| 996 |
+
with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True):
|
| 997 |
+
loss = model(x, y)
|
| 998 |
+
train_loss += loss.detach()
|
| 999 |
+
(loss * grad_scale).backward()
|
| 1000 |
+
train_loss /= grad_accum_steps
|
| 1001 |
+
|
| 1002 |
+
frac = min(step / args.muon_momentum_warmup_steps, 1.0) if args.muon_momentum_warmup_steps > 0 else 1.0
|
| 1003 |
+
muon_momentum = (1 - frac) * args.muon_momentum_warmup_start + frac * args.muon_momentum
|
| 1004 |
+
for group in optimizer_muon.param_groups:
|
| 1005 |
+
group["momentum"] = muon_momentum
|
| 1006 |
+
|
| 1007 |
+
for opt in optimizers:
|
| 1008 |
+
for group in opt.param_groups:
|
| 1009 |
+
group["lr"] = group["base_lr"] * scale
|
| 1010 |
+
|
| 1011 |
+
if args.grad_clip_norm > 0:
|
| 1012 |
+
torch.nn.utils.clip_grad_norm_(base_model.parameters(), args.grad_clip_norm)
|
| 1013 |
+
for opt in optimizers:
|
| 1014 |
+
opt.step()
|
| 1015 |
+
zero_grad_all()
|
| 1016 |
+
|
| 1017 |
+
step += 1
|
| 1018 |
+
approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0)
|
| 1019 |
+
should_log_train = (
|
| 1020 |
+
args.train_log_every > 0
|
| 1021 |
+
and (step <= 10 or step % args.train_log_every == 0 or stop_after_step is not None)
|
| 1022 |
+
)
|
| 1023 |
+
if should_log_train:
|
| 1024 |
+
log0(
|
| 1025 |
+
f"step:{step}/{args.iterations} train_loss:{train_loss.item():.4f} "
|
| 1026 |
+
f"train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms"
|
| 1027 |
+
)
|
| 1028 |
+
|
| 1029 |
+
reached_cap = max_wallclock_ms is not None and approx_training_time_ms >= max_wallclock_ms
|
| 1030 |
+
if distributed and max_wallclock_ms is not None:
|
| 1031 |
+
reached_cap_tensor = torch.tensor(int(reached_cap), device=device)
|
| 1032 |
+
dist.all_reduce(reached_cap_tensor, op=dist.ReduceOp.MAX)
|
| 1033 |
+
reached_cap = bool(reached_cap_tensor.item())
|
| 1034 |
+
if stop_after_step is None and reached_cap:
|
| 1035 |
+
stop_after_step = step
|
| 1036 |
+
|
| 1037 |
+
log0(
|
| 1038 |
+
f"peak memory allocated: {torch.cuda.max_memory_allocated() // 1024 // 1024} MiB "
|
| 1039 |
+
f"reserved: {torch.cuda.max_memory_reserved() // 1024 // 1024} MiB"
|
| 1040 |
+
)
|
| 1041 |
+
|
| 1042 |
+
# ββ Serialization + round-trip validation βββββββββββββββββββββββββββββββββ
|
| 1043 |
+
|
| 1044 |
+
if master_process:
|
| 1045 |
+
torch.save(base_model.state_dict(), "final_model.pt")
|
| 1046 |
+
model_bytes = os.path.getsize("final_model.pt")
|
| 1047 |
+
code_bytes = len(code.encode("utf-8"))
|
| 1048 |
+
log0(f"Serialized model: {model_bytes} bytes")
|
| 1049 |
+
log0(f"Code size: {code_bytes} bytes")
|
| 1050 |
+
log0(f"Total submission size: {model_bytes + code_bytes} bytes")
|
| 1051 |
+
|
| 1052 |
+
quant_obj, quant_stats = quantize_state_dict_int8(base_model.state_dict())
|
| 1053 |
+
quant_buf = io.BytesIO()
|
| 1054 |
+
torch.save(quant_obj, quant_buf)
|
| 1055 |
+
quant_raw = quant_buf.getvalue()
|
| 1056 |
+
quant_blob = zlib.compress(quant_raw, level=9)
|
| 1057 |
+
quant_raw_bytes = len(quant_raw)
|
| 1058 |
+
if master_process:
|
| 1059 |
+
with open("final_model.int8.ptz", "wb") as f:
|
| 1060 |
+
f.write(quant_blob)
|
| 1061 |
+
quant_file_bytes = os.path.getsize("final_model.int8.ptz")
|
| 1062 |
+
code_bytes = len(code.encode("utf-8"))
|
| 1063 |
+
ratio = quant_stats["baseline_tensor_bytes"] / max(quant_stats["int8_payload_bytes"], 1)
|
| 1064 |
+
log0(
|
| 1065 |
+
f"Serialized model int8+zlib: {quant_file_bytes} bytes "
|
| 1066 |
+
f"(payload:{quant_stats['int8_payload_bytes']} raw_torch:{quant_raw_bytes} payload_ratio:{ratio:.2f}x)"
|
| 1067 |
+
)
|
| 1068 |
+
log0(f"Total submission size int8+zlib: {quant_file_bytes + code_bytes} bytes")
|
| 1069 |
+
|
| 1070 |
+
if distributed:
|
| 1071 |
+
dist.barrier()
|
| 1072 |
+
with open("final_model.int8.ptz", "rb") as f:
|
| 1073 |
+
quant_blob_disk = f.read()
|
| 1074 |
+
quant_state = torch.load(io.BytesIO(zlib.decompress(quant_blob_disk)), map_location="cpu")
|
| 1075 |
+
base_model.load_state_dict(dequantize_state_dict_int8(quant_state), strict=True)
|
| 1076 |
+
torch.cuda.synchronize()
|
| 1077 |
+
t_qeval = time.perf_counter()
|
| 1078 |
+
q_val_loss, q_val_bpb = eval_val(
|
| 1079 |
+
args, model, rank, world_size, device, grad_accum_steps,
|
| 1080 |
+
val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut,
|
| 1081 |
+
)
|
| 1082 |
+
torch.cuda.synchronize()
|
| 1083 |
+
log0(
|
| 1084 |
+
f"final_int8_zlib_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} "
|
| 1085 |
+
f"eval_time:{1000.0 * (time.perf_counter() - t_qeval):.0f}ms"
|
| 1086 |
+
)
|
| 1087 |
+
log0(f"final_int8_zlib_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}")
|
| 1088 |
+
|
| 1089 |
+
if distributed:
|
| 1090 |
+
dist.destroy_process_group()
|
| 1091 |
+
|
| 1092 |
+
|
| 1093 |
+
if __name__ == "__main__":
|
| 1094 |
+
main()
|