diff --git a/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/README.md b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/README.md new file mode 100644 index 0000000000..b4af74ddfe --- /dev/null +++ b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/README.md @@ -0,0 +1,130 @@ +# Nemotron-H Inspired Mamba-3 Hybrid + Hinge Point Depth Recurrence + +**Non-record submission. First Mamba depth recurrence and first hinge-point multi-recurrence in the competition.** + +## Summary + +This submission explores a hybrid Mamba-3 / Transformer architecture inspired by NVIDIA's Nemotron-H, with a novel depth recurrence strategy focused on the U-Net hinge point. While the absolute bpb does not beat SOTA, the architectural insights and systematic ablation study provide new findings for the SSM track. + +**Key result:** post-quant val_bpb = **1.4765** (1000 steps, 1xH100, SP1024, GPTQ int6+LZMA, 8.2MB artifact) + +## Architecture + +- **7 Mamba-3 SISO layers + 1 Attention layer** (8 physical layers) +- Mamba-3 config: d_state=64, expand=2, headdim=64, chunk_size=64, ngroups=1 +- Attention: GQA with 8 heads, 4 KV heads, RoPE base=10000 +- Attention placed at layer 4 (evenly spaced, Nemotron-H style) +- U-Net encoder-decoder with skip connections +- `torch.compile(dynamic=False, fullgraph=False)` + +### Depth Recurrence (Novel) + +**Hinge point multi-recurrence:** Layers 3 and 4 (the U-Net hinge) are repeated twice, creating 12 virtual layers from 8 physical layers with zero extra parameters. + +``` +Physical: [M0, M1, M2, M3, A4, M5, M6, M7] +Virtual: [M0, M1, M2, M3, A4, M3, A4, M3, A4, M5, M6, M7] + ↑ hinge layers 3,4 repeated 2x +``` + +Recurrence is enabled at 35% of training (step 350/1000) to allow initial convergence without the overhead. + +## Ablation Results + +### Depth Recurrence (first-ever on Mamba layers) + +| Config | val_bpb (2000 steps) | Virtual layers | vs no-recur | +|--------|---------------------|----------------|-------------| +| No recurrence | 1.2916 | 8 | — | +| Block recur 2,3 | 1.2851 | 10 | -0.0065 | +| Block recur 2,3,4 | 1.2830 | 11 | -0.0086 | +| **Hinge recur 3,4 x2** | **1.2824** | **12** | **-0.0092** | +| 4-layer recur 2,3,4,5 | 1.2864 | 12 | -0.0052 | +| Dual Attn@hinge | 1.2899 | 11 | -0.0017 | + +**Finding:** Focused recurrence at the hinge point outperforms spread recurrence. Repeating hinge layers 2x (12 virtual) beats 4-layer 1x (also 12 virtual) by 0.004 bpb. + +### Approaches Tested and Ruled Out + +| Approach | Result | Finding | +|----------|--------|---------| +| Remove RoPE (ROPE_FRACTION=0) | +0.072 worse | Small models (26M) need explicit position encoding, unlike Jamba (1.3B) | +| Ternary Mamba (BitLinear 1.58-bit) | +0.397 worse | 26M params insufficient for ternary (literature confirms min ~1.3B) | +| Q-Mamba DSQ (A=FP16 + mixed precision) | +0.066 worse than standard GPTQ | Full Hessian GPTQ already handles SSM outliers well | + +### Quantization + +Standard Full Hessian GPTQ int6 with AR self-generated calibration data (from PR #1355 pipeline). LZMA-9 compression. + +- Pre-quant val_bpb: 1.3948 +- Post-quant val_bpb: 1.4765 +- Quantization gap: 0.082 +- Artifact size: 8.2MB (well under 16MB cap) + +## Reproduction + +### Setup (RunPod or Modal with H100) + +```bash +# Install dependencies +pip install -r requirements.txt + +# Additionally, Mamba-3 modules need to be copied from mamba3-release branch: +git clone --depth 1 --branch mamba3-release https://github.com/state-spaces/mamba.git /tmp/mamba3src +PKG=$(python -c 'import mamba_ssm,os; print(os.path.dirname(mamba_ssm.__file__))') +cp /tmp/mamba3src/mamba_ssm/modules/mamba3.py $PKG/modules/ +cp -r /tmp/mamba3src/mamba_ssm/ops/triton/mamba3 $PKG/ops/triton/ +cp /tmp/mamba3src/mamba_ssm/ops/triton/angle_cumsum.py $PKG/ops/triton/ +rm -rf /tmp/mamba3src + +# Download dataset +python3 data/cached_challenge_fineweb.py --variant sp1024 --train-shards 10 +``` + +### Training (1xH100, ~17 min for 1000 steps + GPTQ) + +```bash +RUN_ID=nemotron_hinge \ +DATA_PATH=./data/datasets/fineweb10B_sp1024/ \ +TOKENIZER_PATH=./data/tokenizers/fineweb_1024_bpe.model \ +VOCAB_SIZE=1024 \ +NUM_LAYERS=8 \ +NUM_ATTN_LAYERS=1 \ +ATTN_PLACEMENT=even \ +MAMBA3_D_STATE=64 \ +RECUR_LAYERS=3,4 \ +RECUR_MODE=block \ +RECUR_REPEATS=2 \ +RECUR_START_FRAC=0.35 \ +ITERATIONS=1000 \ +torchrun --standalone --nproc_per_node=1 train_nemotron_hybrid.py +``` + +### Training (8xH100, 10 min — pending compute grant) + +```bash +# Same config but with: +# torchrun --standalone --nproc_per_node=8 +# MAX_WALLCLOCK_SECONDS=600 +# Expected: val_bpb ~1.25-1.30 post-quant +``` + +## Credits / Built On + +- **PR #1355** (@mamba3-hybrid author): Mamba-3 Hybrid base, GPTQ pipeline, MuonEq-R optimizer +- **NVIDIA Nemotron-H** (arXiv 2504.03624): Hybrid architecture inspiration (92% SSM + 8% attention) +- **Mamba-3** (ICLR 2026, Gu et al.): SISO SSM with complex-valued states +- **PR #1204** (@sisovic): Depth recurrence concept (adapted from Transformer to SSM) +- **Q-Mamba, Mamba-PTQ, Quamba2**: Mamba quantization research informing our ablations + +## Compute + +All experiments run on Modal.com 1xH100 instances. Pending OpenAI compute grant for 8xH100 runs. +Total compute used: ~$30 Modal credits across 20+ experiments. + +## What's Next + +1. Full 8xH100 10-min run with best config (pending compute) +2. SP8192 tokenizer (expected ~0.05 bpb improvement) +3. Long-context evaluation (Mamba's O(n) advantage for 8K-32K eval) +4. Enable TTT and EMA for additional gains diff --git a/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/requirements.txt b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/requirements.txt new file mode 100644 index 0000000000..4d7bdbe399 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/requirements.txt @@ -0,0 +1,3 @@ +mamba_ssm @ https://github.com/state-spaces/mamba/releases/download/v2.3.1/mamba_ssm-2.3.1%2Bcu12torch2.9cxx11abiTRUE-cp312-cp312-linux_x86_64.whl +causal_conv1d @ https://github.com/Dao-AILab/causal-conv1d/releases/download/v1.6.1.post4/causal_conv1d-1.6.1%2Bcu12torch2.9cxx11abiTRUE-cp312-cp312-linux_x86_64.whl +einops diff --git a/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/submission.json b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/submission.json new file mode 100644 index 0000000000..7687f35944 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/submission.json @@ -0,0 +1,12 @@ +{ + "author": "Yongkang Zou", + "github_id": "inin-zou", + "name": "Nemotron-H Inspired Mamba-3 Hybrid + Hinge Point Depth Recurrence", + "blurb": "First Mamba depth recurrence in Parameter Golf: 7 Mamba-3 + 1 Attention hybrid with hinge-point multi-recurrence (12 virtual layers from 8 physical). Inspired by NVIDIA Nemotron-H architecture.", + "date": "2026-04-13T23:00:00Z", + "val_loss": 2.4930, + "val_bpb": 1.4765, + "bytes_total": 8295138, + "bytes_code": 90450, + "notes": "Run on 1xH100 (1000 steps). Pending OpenAI compute grant for full 8xH100 10-min run. Pre-quant val_bpb=1.3948, estimated 2000-step post-quant ~1.35." +} diff --git a/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train.log b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train.log new file mode 100644 index 0000000000..3f7626f9e9 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train.log @@ -0,0 +1,77 @@ +logs/baseline_gptq.txt +val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path=/app/data/tokenizers/fineweb_1024_bpe.model +train_loader:dataset:fineweb10B_sp1024 train_shards:1 +val_loader:shards pattern=/app/data/datasets/fineweb10B_sp1024/fineweb_val_*.bin tokens:62021632 +attn_placement:even attn_indices:[4] +depth_recurrence: layers=[3, 4] mode=block +model_params:26216040 +world_size:1 grad_accum_steps:8 +mode:mamba3_hybrid num_attn_layers:1 attn_indices:[4] +ssd: d_state:64 expand:2 headdim:64 +attn: num_heads:8 num_kv_heads:4 rope_base:10000.0 +num_layers:8 mlp_mult:3 +tie_embeddings:True embed_lr:0.05 head_lr:0.0 matrix_lr:0.025 scalar_lr:0.02 +train_batch_tokens:524288 train_seq_len:4096 iterations:1000 warmup_steps:20 max_wallclock_seconds:0.000 +seed:1337 +warmup_step:1/20 +warmup_step:2/20 +warmup_step:3/20 +warmup_step:4/20 +warmup_step:5/20 +warmup_step:6/20 +warmup_step:7/20 +warmup_step:8/20 +warmup_step:9/20 +warmup_step:10/20 +warmup_step:11/20 +warmup_step:12/20 +warmup_step:13/20 +warmup_step:14/20 +warmup_step:15/20 +warmup_step:16/20 +warmup_step:17/20 +warmup_step:18/20 +warmup_step:19/20 +warmup_step:20/20 +step:0/1000 val_loss:6.9357 val_bpb:4.1077 train_time:0ms step_avg:0.01ms +step:1/1000 train_loss:6.9356 train_time:458ms step_avg:458.05ms +step:2/1000 train_loss:6.5663 train_time:916ms step_avg:458.24ms +step:3/1000 train_loss:6.0412 train_time:1374ms step_avg:458.04ms +step:4/1000 train_loss:5.8702 train_time:1832ms step_avg:458.07ms +step:5/1000 train_loss:5.5853 train_time:2290ms step_avg:458.04ms +step:6/1000 train_loss:5.3060 train_time:2748ms step_avg:458.07ms +step:7/1000 train_loss:5.0688 train_time:3206ms step_avg:458.05ms +step:8/1000 train_loss:4.8584 train_time:3664ms step_avg:458.03ms +step:9/1000 train_loss:4.7453 train_time:4122ms step_avg:458.01ms +step:10/1000 train_loss:4.6856 train_time:4580ms step_avg:458.03ms +step:200/1000 train_loss:2.9027 train_time:91875ms step_avg:459.37ms +depth_recurrence:enabled at step 350 frac=0.35 schedule=[0, 1, 2, 3, 4, 3, 4, 3, 4, 5, 6, 7] +step:400/1000 train_loss:2.5676 train_time:195471ms step_avg:488.68ms +late_qat:enabled bits=6 at step 476 scale=0.1497 +step:500/1000 val_loss:2.6478 val_bpb:1.5682 train_time:276969ms step_avg:553.94ms +step:600/1000 train_loss:2.6001 train_time:346158ms step_avg:576.93ms +step:800/1000 train_loss:2.3457 train_time:485299ms step_avg:606.62ms +step:1000/1000 train_loss:2.4538 train_time:624107ms step_avg:624.11ms +step:1000/1000 val_loss:2.3551 val_bpb:1.3948 train_time:624107ms step_avg:624.11ms +peak memory allocated: 21397 MiB reserved: 21700 MiB +ema:applying EMA weights +Serialized model: 102806059 bytes +Code size: 90450 bytes +Total submission size: 102896509 bytes +gptq:generating autoregressive calibration data... +gptq:generated 32 seqs in 966.8s +gptq:collecting hessians... +gptq:collected hessians for 35 layers +gptq:quantization complete in 993.9s total +Serialized model int6+lzma-9: 8204688 bytes (payload:26989856 raw_torch:27033765 payload_ratio:3.81x) +Total submission size int6+lzma-9: 8295138 bytes +[rank0]:W0414 00:04:21.959000 9 site-packages/torch/_dynamo/convert_frame.py:1358] [13/8] torch._dynamo hit config.recompile_limit (8) +[rank0]:W0414 00:04:21.959000 9 site-packages/torch/_dynamo/convert_frame.py:1358] [13/8] function: 'forward' (/app/train_nemotron_hybrid.py:1102) +[rank0]:W0414 00:04:21.959000 9 site-packages/torch/_dynamo/convert_frame.py:1358] [13/8] last reason: 13/7: tensor 'self._modules['attn']._modules['rotary']._cos_cached' size mismatch at index 2. expected 4095, actual 4096 +[rank0]:W0414 00:04:21.959000 9 site-packages/torch/_dynamo/convert_frame.py:1358] [13/8] To log all recompilation reasons, use TORCH_LOGS="recompiles". +[rank0]:W0414 00:04:21.959000 9 site-packages/torch/_dynamo/convert_frame.py:1358] [13/8] To diagnose recompilation issues, see https://pytorch.org/docs/main/torch.compiler_troubleshooting.html +final_int8_zlib_roundtrip val_loss:2.4930 val_bpb:1.4765 eval_mode:standard eval_time:40523ms +final_int8_zlib_roundtrip_exact val_loss:2.49298788 val_bpb:1.47648785 +Stopping app - local entrypoint completed. +✓ App completed. View run at +https://modal.com/apps/yongkang-zou1999/main/ap-g38RYWF6UCTTyFVL8834tz diff --git a/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train_nemotron_hybrid.py b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train_nemotron_hybrid.py new file mode 100644 index 0000000000..dfa6b56583 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-13_NemotronMamba3_HingeRecurrence/train_nemotron_hybrid.py @@ -0,0 +1,1941 @@ +"""Nemotron-H inspired Mamba-3 Hybrid for Parameter Golf. + +Based on PR #1355 (best SSM, 1.1526 bpb) with Nemotron-H architectural insights: +- Configurable attention placement strategy (evenly spaced, first-layers, last-layers) +- Configurable d_state (64 or 128, Nemotron-H uses 128 for 8B) +- Configurable ngroups (1 or 8, Nemotron-H uses 8) +- Ablation-friendly env-var driven config +""" + +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 lzma +import zlib +from pathlib import Path + +try: + import zstandard as zstd + HAS_ZSTD = True +except ImportError: + HAS_ZSTD = False + +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 +# ----------------------------- + +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") + 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)) + + # 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", 3500)) + warmdown_shape = os.environ.get("WARMDOWN_SHAPE", "linear") # "linear" or "cosine" + warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) + train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 1_048_576)) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 4096)) + max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) + sweep_mode = bool(int(os.environ.get("SWEEP_MODE", "0"))) # skip post-training (quant, serialize, TTT) + + # Evaluation. + eval_stride = int(os.environ.get("EVAL_STRIDE", 32)) # sliding window stride (0 = disabled) + eval_batch_seqs = int(os.environ.get("EVAL_BATCH_SEQS", 32)) # batch size for sliding eval + # Test-Time Training (TTT): online adaptation on val data during scoring + ttt_enabled = bool(int(os.environ.get("TTT_ENABLED", "0"))) + ttt_lr = float(os.environ.get("TTT_LR", 0.002)) + ttt_freeze_blocks = int(os.environ.get("TTT_FREEZE_BLOCKS", 0)) # freeze first N blocks during TTT + ttt_optimizer = os.environ.get("TTT_OPTIMIZER", "adamw") # "sgd" or "adamw" + ttt_epochs = int(os.environ.get("TTT_EPOCHS", 1)) # adaptation passes per chunk + ttt_momentum = float(os.environ.get("TTT_MOMENTUM", 0.9)) # SGD momentum + + # Quantization. + fp16_embed = bool(int(os.environ.get("FP16_EMBED", "1"))) # keep embeddings in FP16 + quant_bits = int(os.environ.get("QUANT_BITS", 6)) # quantization bit width for attention (6 or 8) + quant_bits_mlp = int(os.environ.get("QUANT_BITS_MLP", 0)) # MLP bit width (0 = same as quant_bits) + qat_start_frac = float(os.environ.get("QAT_START_FRAC", 0.0)) # QAT: start at this fraction of training (0 = disabled) + gptq_lite = bool(int(os.environ.get("GPTQ_LITE", "1"))) # search for optimal clip percentile per tensor + use_zstd = bool(int(os.environ.get("USE_ZSTD", "1"))) # use zstd instead of zlib + use_lzma = bool(int(os.environ.get("USE_LZMA", "1"))) # use lzma instead of zlib (better ratio, slower) + eval_temp = float(os.environ.get("EVAL_TEMP", "0.9")) # temperature scaling at eval (T<1 sharpens, improves bpb) + use_gptq = bool(int(os.environ.get("USE_GPTQ", "1"))) # Full Hessian GPTQ instead of per-row min-max + gptq_num_seqs = int(os.environ.get("GPTQ_NUM_SEQS", "32")) # AR self-gen sequences for Hessian collection + gptq_gen_len = int(os.environ.get("GPTQ_GEN_LEN", "4096")) # tokens per generated sequence (match train_seq_len) + gptq_gen_temp = float(os.environ.get("GPTQ_GEN_TEMP", "0.8")) # sampling temperature during generation + gptq_damp = float(os.environ.get("GPTQ_DAMP", "0.01")) # Hessian damping factor (λI added for stability) + late_qat_threshold = float(os.environ.get("LATE_QAT_THRESHOLD", "0.15")) # enable QAT when lr_mul drops below this (SOTA uses 0.15) + fp16_inproj_rows = bool(int(os.environ.get("FP16_INPROJ_ROWS", "0"))) # store recurrence rows in FP16 (0 = quantize all rows) + + # Model shape. + vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) + num_layers = int(os.environ.get("NUM_LAYERS", 8)) # unique layers + + # SmearGate + BigramHash. + use_smeargate = bool(int(os.environ.get("USE_SMEARGATE", "1"))) + use_bigram_hash = bool(int(os.environ.get("USE_BIGRAM_HASH", "1"))) + bigram_buckets = int(os.environ.get("BIGRAM_BUCKETS", 4096)) + bigram_hash_dim = int(os.environ.get("BIGRAM_HASH_DIM", 128)) + + # OrthoInit + SWA. + use_ortho_init = bool(int(os.environ.get("USE_ORTHO_INIT", "1"))) + swa_enabled = bool(int(os.environ.get("SWA_ENABLED", "1"))) + swa_start_frac = float(os.environ.get("SWA_START_FRAC", 0.4)) + swa_every = int(os.environ.get("SWA_EVERY", 50)) + model_dim = int(os.environ.get("MODEL_DIM", 512)) + mlp_mult = int(os.environ.get("MLP_MULT", 3)) + mamba3_d_state = int(os.environ.get("MAMBA3_D_STATE", 64)) + mamba3_expand = int(os.environ.get("MAMBA3_EXPAND", 2)) + mamba3_headdim = int(os.environ.get("MAMBA3_HEADDIM", 64)) + mamba3_chunk_size = int(os.environ.get("MAMBA3_CHUNK_SIZE", 64)) + mamba3_ngroups = int(os.environ.get("MAMBA3_NGROUPS", 1)) # Nemotron-H uses 8 + # Attention layers (evenly spaced among SSD layers). + num_attn_layers = int(os.environ.get("NUM_ATTN_LAYERS", 1)) + attn_placement = os.environ.get("ATTN_PLACEMENT", "even") + num_heads = int(os.environ.get("NUM_HEADS", 8)) + num_kv_heads = int(os.environ.get("NUM_KV_HEADS", 4)) + rope_base = float(os.environ.get("ROPE_BASE", 10000.0)) + rope_fraction = float(os.environ.get("ROPE_FRACTION", 1.0)) # partial RoPE: 0.5 = half dims + + # Depth recurrence: reuse specified layers to create virtual depth without extra params + # RECUR_LAYERS: comma-separated physical layer indices to repeat, e.g. "2,3" + # RECUR_MODE: "block" = repeat entire block, "untie_mlp" = share SSM/Attn but separate MLPs + # RECUR_START_FRAC: fraction of training before enabling recurrence (0.35 = after 35%) + recur_layers_str = os.environ.get("RECUR_LAYERS", "") + recur_mode = os.environ.get("RECUR_MODE", "block") # "block" or "untie_mlp" + recur_start_frac = float(os.environ.get("RECUR_START_FRAC", 0.35)) + recur_repeats = int(os.environ.get("RECUR_REPEATS", 1)) # how many times to repeat (1=double, 2=triple) + qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) + ve_enabled = bool(int(os.environ.get("VE_ENABLED", "0"))) + ve_dim = int(os.environ.get("VE_DIM", 64)) + tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1"))) + logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0)) + + # Optimizer hyperparameters. + weight_decay = float(os.environ.get("WEIGHT_DECAY", 0.04)) + ema_enabled = bool(int(os.environ.get("EMA_ENABLED", "1"))) + ema_decay = float(os.environ.get("EMA_DECAY", 0.997)) + 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_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005)) + matrix_lr = float(os.environ.get("MATRIX_LR", 0.025)) + scalar_lr = float(os.environ.get("SCALAR_LR", 0.02)) + 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)) + 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)) + +# 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"] + weight_decay = 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 weight_decay > 0: + p.data.mul_(1.0 - lr * weight_decay) + 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 + 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, grad_accum_steps: int, val_tokens: Tensor, + base_bytes_lut: Tensor, has_leading_space_lut: Tensor, is_boundary_token_lut: Tensor, +) -> tuple[float, float]: + 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 + 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) + 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() + + 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_sliding( + 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, +) -> tuple[float, float]: + """Sliding window evaluation: score each token with maximal left-context.""" + seq_len = args.train_seq_len + stride = args.eval_stride + batch_size = args.eval_batch_seqs + total_tokens = val_tokens.numel() - 1 + + window_starts = [ws for ws in range(0, total_tokens - seq_len + 1, stride)] + if window_starts[-1] + seq_len < total_tokens: + window_starts.append(total_tokens - seq_len) + + my_starts = window_starts[rank::world_size] + + loss_sum = torch.zeros((), device=device, dtype=torch.float64) + token_count = torch.zeros((), device=device, dtype=torch.float64) + byte_count = torch.zeros((), device=device, dtype=torch.float64) + + base_model.eval() + with torch.inference_mode(): + for batch_start in range(0, len(my_starts), batch_size): + batch_ws = my_starts[batch_start:batch_start + batch_size] + bsz = len(batch_ws) + + x_list, y_list = [], [] + for ws in batch_ws: + chunk = val_tokens[ws:ws + seq_len + 1].to(dtype=torch.int64) + x_list.append(chunk[:-1]) + y_list.append(chunk[1:]) + x_batch = torch.stack(x_list).to(device=device, non_blocking=True) + y_batch = torch.stack(y_list).to(device=device, non_blocking=True) + + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + logits = base_model.forward_logits(x_batch) + scaled_logits = logits.float() + if args.eval_temp != 1.0: + scaled_logits = scaled_logits / args.eval_temp + nll = F.cross_entropy( + scaled_logits.reshape(-1, scaled_logits.size(-1)), + y_batch.reshape(-1), + reduction="none", + ).reshape(bsz, seq_len) + + for i, ws in enumerate(batch_ws): + wlen = min(seq_len, total_tokens - ws) + s = 0 if ws == 0 else max(wlen - stride, 0) + scored_nll = nll[i, s:wlen].to(torch.float64) + loss_sum += scored_nll.sum() + token_count += float(wlen - s) + + prev_ids = x_batch[i, s:wlen] + tgt_ids = y_batch[i, s:wlen] + tbytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) + tbytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) + byte_count += tbytes.to(torch.float64).sum() + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(byte_count, op=dist.ReduceOp.SUM) + + val_loss = (loss_sum / token_count).item() + bits_per_token = val_loss / math.log(2.0) + tokens_per_byte = token_count.item() / byte_count.item() + base_model.train() + return float(val_loss), float(bits_per_token * tokens_per_byte) + + +# QUANTIZATION + +CONTROL_TENSOR_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "CONTROL_TENSOR_NAME_PATTERNS", + "m3_scale,mlp_scale,mlp_scales,attn_scale,resid_mix,resid_mixes,skip_weight,skip_weights,smeargate,dt_bias,B_bias,C_bias,.D,A_log,q_gain,ve_layer_scales,ve_shared.scale", + ).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 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_with_clip(t32: Tensor, clip_abs: Tensor | float, qmax: int) -> tuple[Tensor, Tensor, Tensor]: + if t32.ndim == 2 and isinstance(clip_abs, Tensor): + clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) + scale = (clip_abs / qmax).clamp_min(1.0 / qmax) + q = torch.clamp(torch.round(clipped / scale[:, None]), -qmax, qmax).to(torch.int8) + recon = q.float() * scale[:, None] + return q.contiguous(), scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous(), recon + clip_abs_f = float(clip_abs) if isinstance(clip_abs, Tensor) else clip_abs + scale_f = clip_abs_f / qmax if clip_abs_f > 0 else 1.0 + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs_f, clip_abs_f) / scale_f), -qmax, qmax).to(torch.int8) + recon = q.float() * scale_f + return q.contiguous(), torch.tensor(scale_f, dtype=torch.float32), recon + +def quantize_float_tensor(t: Tensor, bits: int = 8, search_clip: bool = False) -> tuple[Tensor, Tensor]: + qmax = (1 << (bits - 1)) - 1 + t32 = t.float() + + if not search_clip: + if t32.ndim == 2: + clip_abs = ( + torch.quantile(t32.abs(), INT8_CLIP_Q, dim=1) + if t32.numel() + else torch.empty((t32.shape[0],), dtype=torch.float32) + ) + q, scale, _ = _quantize_with_clip(t32, clip_abs, qmax) + return q, scale + clip_abs = float(torch.quantile(t32.abs().flatten(), INT8_CLIP_Q).item()) if t32.numel() else 0.0 + q, scale, _ = _quantize_with_clip(t32, clip_abs, qmax) + return q, scale + + candidates = [0.999, 0.9995, 0.9999, 0.99995, 0.99999, 0.999999, 1.0] + best_q, best_scale, best_mse = None, None, float("inf") + + for pct in candidates: + if t32.ndim == 2: + if pct >= 1.0: + clip_abs = t32.abs().amax(dim=1) + else: + clip_abs = torch.quantile(t32.abs(), pct, dim=1) + q, scale, recon = _quantize_with_clip(t32, clip_abs, qmax) + else: + if pct >= 1.0: + clip_abs = float(t32.abs().max().item()) + else: + clip_abs = float(torch.quantile(t32.abs().flatten(), pct).item()) if t32.numel() else 0.0 + q, scale, recon = _quantize_with_clip(t32, clip_abs, qmax) + mse = (t32 - recon).pow(2).mean().item() + if mse < best_mse: + best_mse = mse + best_q, best_scale = q, scale + + return best_q, best_scale + +def generate_autoregressive_calib( + model: nn.Module, device: torch.device, num_seqs: int = 64, seq_len: int = 2048, + vocab_size: int = 1024, temperature: float = 0.8, batch_size: int = 8, seed: int = 42, +) -> list[Tensor]: + """Generate sequences autoregressively from the model for GPTQ calibration. + No external data accessed — fully self-contained and legal.""" + model.eval() + rng = torch.Generator(device=device) + rng.manual_seed(seed) + all_tokens = [] + with torch.inference_mode(), torch.autocast(device_type="cuda", dtype=torch.bfloat16): + for batch_start in range(0, num_seqs, batch_size): + bs = min(batch_size, num_seqs - batch_start) + tokens = torch.randint(0, vocab_size, (bs, 1), device=device, generator=rng) + for _ in range(seq_len - 1): + logits = model.forward_logits(tokens) + next_logit = logits[:, -1, :] + probs = torch.softmax(next_logit / temperature, dim=-1) + next_tok = torch.multinomial(probs, 1, generator=rng) + tokens = torch.cat([tokens, next_tok], dim=1) + for i in range(bs): + all_tokens.append(tokens[i:i+1]) + return all_tokens + + +def get_mamba3_in_proj_fp16_row_mask(model: nn.Module) -> dict[str, Tensor]: + """Bool mask (out_features,): True = keep at original precision (recurrence-propagating rows). + + Mamba-3 in_proj output split order: [z | x | B | C | dd_dt | dd_A | trap | angles] + Rows for B, dd_dt, dd_A, trap feed directly into the SSD recurrence h_t = A*h_{t-1} + B*x_t. + Quantization errors in these rows compound over sequence length — keep them unquantized. + """ + masks = {} + for name, module in model.named_modules(): + if hasattr(module, 'mamba3') and hasattr(module.mamba3, 'in_proj'): + m3 = module.mamba3 + splits = [ + m3.d_inner, # z + m3.d_inner, # x + m3.d_state * m3.num_bc_heads * m3.mimo_rank, # B ← keep + m3.d_state * m3.num_bc_heads * m3.mimo_rank, # C + m3.nheads, # dd_dt ← keep + m3.nheads, # dd_A ← keep + m3.nheads, # trap ← keep + m3.num_rope_angles, # angles + ] + mask = torch.zeros(sum(splits), dtype=torch.bool) + fp16_groups = {2, 4, 5, 6} # B, dd_dt, dd_A, trap + offset = 0 + for i, sz in enumerate(splits): + if i in fp16_groups: + mask[offset:offset + sz] = True + offset += sz + masks[name + '.mamba3.in_proj.weight'] = mask + return masks + + +def collect_hessians_from_tokens( + model: nn.Module, token_seqs: list[Tensor], device: torch.device, +) -> dict[str, Tensor]: + """Collect H = X^T X from pre-generated token sequences via forward hooks on CastedLinear layers.""" + hessians: dict[str, Tensor] = {} + hooks = [] + for name, module in model.named_modules(): + if isinstance(module, CastedLinear): + param_name = name + ".weight" + cols = module.weight.shape[1] + hessians[param_name] = torch.zeros(cols, cols, dtype=torch.float32, device="cpu") + def make_hook(pname: str): + def hook_fn(mod: nn.Module, inp: tuple, out: Tensor) -> None: + x = inp[0].detach().float() + if x.ndim == 3: + x = x.reshape(-1, x.shape[-1]) + hessians[pname] += (x.T @ x).cpu() + return hook_fn + hooks.append(module.register_forward_hook(make_hook(param_name))) + model.eval() + with torch.inference_mode(), torch.autocast(device_type="cuda", dtype=torch.bfloat16): + for seq in token_seqs: + x = seq[:, :-1].to(device) + y = seq[:, 1:].to(device) + model(x, y) + for h in hooks: + h.remove() + num_batches = len(token_seqs) + for name in hessians: + H = hessians[name] + H /= num_batches + damp = 0.01 * torch.diag(H).mean().clamp_min(1e-6) + H += damp * torch.eye(H.shape[0]) + hessians[name] = H + return hessians + + +def quantize_int6_gptq( + weight: Tensor, hessian: Tensor | None = None, clip_range: int = 31, block_size: int = 128, +) -> tuple[Tensor, Tensor]: + """Full GPTQ: Hessian-aware int6 quantization with Cholesky error compensation and column reordering. + Falls back to percentile search if hessian is None (same as existing gptq_lite path).""" + t32 = weight.float() + if t32.ndim != 2 or hessian is None: + return _quantize_int6_percentile(t32, clip_range) + rows, cols = t32.shape + H = hessian.float().clone() + dead = torch.diag(H) == 0 + H[dead, dead] = 1 + damp = 0.01 * torch.mean(torch.diag(H)) + H[torch.arange(cols), torch.arange(cols)] += damp + # Column reordering: quantize most-activated (sensitive) columns first + perm = torch.argsort(torch.diag(H), descending=True) + inv_perm = torch.argsort(perm) + W = t32[:, perm].clone() + W[:, dead[perm]] = 0 + H = H[perm][:, perm] + # Compute upper Cholesky of H_inv for the error propagation sweep + try: + Hinv = torch.linalg.cholesky(H) + Hinv = torch.cholesky_inverse(Hinv) + Hinv = torch.linalg.cholesky(Hinv, upper=True) + except torch.linalg.LinAlgError: + return _quantize_int6_percentile(weight, clip_range=clip_range) + best_q, best_scale, best_err = None, None, float("inf") + for pct in [0.9990, 0.9995, 0.9999, 0.99999, 1.0]: + row_clip = torch.quantile(t32.abs(), pct, dim=1) if pct < 1.0 else t32.abs().amax(dim=1) + s = (row_clip / clip_range).clamp_min(1.0 / clip_range).to(torch.float16) + sf = s.float() + Q = torch.zeros_like(W, dtype=torch.int8) + W_work = W.clone() + for i1 in range(0, cols, block_size): + i2 = min(i1 + block_size, cols) + count = i2 - i1 + W1 = W_work[:, i1:i2].clone() + Q1 = torch.zeros(rows, count, dtype=torch.int8) + Err1 = torch.zeros(rows, count) + Hinv1 = Hinv[i1:i2, i1:i2] + for i in range(count): + w = W1[:, i] + d = Hinv1[i, i] + q = torch.clamp(torch.round(w / sf), -clip_range, clip_range).to(torch.int8) + Q1[:, i] = q + err = (w - q.float() * sf) / d + W1[:, i:] -= err.unsqueeze(1) * Hinv1[i, i:].unsqueeze(0) + Err1[:, i] = err + Q[:, i1:i2] = Q1 + if i2 < cols: + W_work[:, i2:] -= Err1 @ Hinv[i1:i2, i2:] + recon = Q.float() * sf[:, None] + mse = (W - recon).pow(2).mean().item() + if mse < best_err: + best_q, best_scale, best_err = Q, s, mse + best_q = best_q[:, inv_perm] + return best_q, best_scale + + +def _quantize_int6_percentile(t32: Tensor, clip_range: int = 31) -> tuple[Tensor, Tensor]: + """Fallback percentile-search quantization (no Hessian).""" + if t32.ndim == 2: + best_q, best_s, best_err = None, None, float("inf") + for pct in [0.9990, 0.9995, 0.9999, 0.99999, 1.0]: + row_clip = torch.quantile(t32.abs(), pct, dim=1) if pct < 1.0 else t32.abs().amax(dim=1) + s = (row_clip / clip_range).clamp_min(1.0 / clip_range).to(torch.float16) + q = torch.clamp(torch.round(t32 / s.float()[:, None]), -clip_range, clip_range).to(torch.int8) + err = (t32 - q.float() * s.float()[:, None]).pow(2).mean().item() + if err < best_err: + best_q, best_s, best_err = q, s, err + return best_q, best_s + amax = t32.abs().max().item() + scale = torch.tensor(amax / clip_range if amax > 0 else 1.0, dtype=torch.float16) + q = torch.clamp(torch.round(t32 / scale.float()), -clip_range, clip_range).to(torch.int8) + return q, scale + + +def quantize_state_dict_int8(state_dict: dict[str, Tensor], fp16_embed: bool = False, quant_bits: int = 8, quant_bits_mlp: int = 0, search_clip: bool = False, fp16_row_masks: dict[str, Tensor] | None = None): + 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 + + # FP16 row mask: split tensor into FP16 passthrough rows + quantized rows + _fp16_mask = fp16_row_masks.get(name) if fp16_row_masks else None + if _fp16_mask is not None and t.is_floating_point() and t.ndim == 2: + fp16_part = t[_fp16_mask].to(INT8_KEEP_FLOAT_STORE_DTYPE) + quant_part = t[~_fp16_mask].float() + passthrough[name + ".__fp16_rows"] = fp16_part + passthrough[name + ".__fp16_mask"] = _fp16_mask.cpu() + stats["int8_payload_bytes"] += tensor_nbytes(fp16_part) + tensor_nbytes(_fp16_mask) + bits = quant_bits + if quant_bits_mlp > 0 and "mlp" in name: + bits = quant_bits_mlp + q, s = quantize_float_tensor(quant_part, bits=bits, search_clip=search_clip) + quantized[name] = q + scales[name] = s + dtypes[name] = str(t.dtype).removeprefix("torch.") + qmeta[name] = {"scheme": "per_row", "axis": 0, "split_fp16": True} + stats["num_float_tensors"] += 1 + stats["int8_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s) + continue + + if fp16_embed and "tok_emb" in name: + kept = keep_float_tensor(name, t, passthrough_orig_dtypes) + passthrough[name] = kept + stats["int8_payload_bytes"] += tensor_nbytes(kept) + continue + + if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL: + 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 + bits = quant_bits + if quant_bits_mlp > 0 and "mlp" in name: + bits = quant_bits_mlp + q, s = quantize_float_tensor(t, bits=bits, search_clip=search_clip) + 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", {}) + passthrough_data = obj["passthrough"] + 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) + deq = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous() + else: + scale = float(s.item()) + deq = (q.float() * scale).to(dtype=dtype).contiguous() + # Reassemble split FP16 rows if present + fp16_key = name + ".__fp16_rows" + mask_key = name + ".__fp16_mask" + if fp16_key in passthrough_data: + fp16_rows = passthrough_data[fp16_key].to(dtype=dtype) + mask = passthrough_data[mask_key] + full = torch.empty(mask.shape[0], deq.shape[1], dtype=dtype) + full[~mask] = deq + full[mask] = fp16_rows + out[name] = full.contiguous() + else: + out[name] = deq + for name, t in passthrough_data.items(): + if name.endswith(".__fp16_rows") or name.endswith(".__fp16_mask"): + continue # already consumed above + 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 FakeQuantizeSTE(torch.autograd.Function): + """Simulated quantization with Straight-Through Estimator for QAT.""" + @staticmethod + def forward(ctx, w: Tensor, bits: int) -> Tensor: + qmax = (1 << (bits - 1)) - 1 + if w.ndim == 2: + scale = w.detach().abs().amax(dim=1, keepdim=True) / qmax + scale = scale.clamp_min(1.0 / qmax) + return (torch.clamp(torch.round(w / scale), -qmax, qmax) * scale).to(w.dtype) + scale = w.detach().abs().amax() / qmax + scale = scale.clamp_min(1.0 / qmax) + return (torch.clamp(torch.round(w / scale), -qmax, qmax) * scale).to(w.dtype) + + @staticmethod + def backward(ctx, grad: Tensor) -> tuple[Tensor, None]: + return grad, None + + +class CastedLinear(nn.Linear): + _qat_bits: int = 0 + + def forward(self, x: Tensor) -> Tensor: + w = self.weight.to(x.dtype) + if self._qat_bits > 0 and self.weight.numel() > 65536: + w = FakeQuantizeSTE.apply(w, self._qat_bits) + bias = self.bias.to(x.dtype) if self.bias is not None else None + return F.linear(x, w, bias) + + +class SmearGate(nn.Module): + def __init__(self, dim: int): + super().__init__() + self.gate = nn.Parameter(torch.full((dim,), 3.0, dtype=torch.float32)) + + def forward(self, x: Tensor) -> Tensor: + g = torch.sigmoid(self.gate).to(dtype=x.dtype) + x_prev = torch.cat([torch.zeros_like(x[:, :1]), x[:, :-1]], dim=1) + return g * x + (1.0 - g) * x_prev + + +class BigramHash(nn.Module): + def __init__(self, num_buckets: int, hash_dim: int, model_dim: int): + super().__init__() + self.num_buckets = num_buckets + self.table = nn.Embedding(num_buckets, hash_dim) + self.proj = CastedLinear(hash_dim, model_dim, bias=False) + self.proj._zero_init = True + nn.init.normal_(self.table.weight, std=0.01) + + def forward(self, input_ids: Tensor) -> Tensor: + bsz, seqlen = input_ids.shape + prev_ids = torch.cat([torch.zeros(bsz, 1, dtype=input_ids.dtype, device=input_ids.device), + input_ids[:, :-1]], dim=1) + h = ((prev_ids.long() * 92821 + input_ids.long()) % self.num_buckets).long() + return self.proj(self.table(h)) + + +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 Mamba3Layer(nn.Module): + """Pure Mamba-3 SISO layer. Uses the Mamba3 module directly.""" + def __init__(self, dim: int, d_state: int = 64, expand: int = 2, + headdim: int = 64, chunk_size: int = 64, ngroups: int = 1): + super().__init__() + from mamba_ssm.modules.mamba3 import Mamba3 + self.mamba3 = Mamba3( + d_model=dim, d_state=d_state, expand=expand, + headdim=headdim, is_mimo=False, chunk_size=chunk_size, ngroups=ngroups, + ) + # Replace nn.Linear with CastedLinear so QAT fake-quant and float32 master + # weights apply to Mamba-3's projections (which have the worst outlier problem). + for attr in ("in_proj", "out_proj"): + src = getattr(self.mamba3, attr) + dst = CastedLinear(src.in_features, src.out_features, bias=src.bias is not None) + dst.weight = src.weight + if src.bias is not None: + dst.bias = src.bias + setattr(self.mamba3, attr, dst) + + def forward(self, x: Tensor) -> Tensor: + return self.mamba3(x) + + +class Rotary(nn.Module): + def __init__(self, dim: int, base: float = 10000.0): + super().__init__() + inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2, dtype=torch.float32) / dim)) + 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) -> Tensor: + 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 AttentionLayer(nn.Module): + """Standalone causal self-attention with GQA and RoPE.""" + def __init__(self, dim: int, num_heads: int, num_kv_heads: int, + rope_base: float, qk_gain_init: float, rope_fraction: float = 1.0): + super().__init__() + self.num_heads = num_heads + self.num_kv_heads = num_kv_heads + self.head_dim = dim // num_heads + kv_dim = 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.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) + self.rope_dims = max(2, int(self.head_dim * rope_fraction) // 2 * 2) + self.rotary = Rotary(self.rope_dims, base=rope_base) + self.proj = CastedLinear(dim, dim, bias=False) + self.proj._zero_init = True + + def forward(self, x: Tensor, v_embed: Tensor | None = None) -> 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) + if v_embed is not None: + v = v + v_embed.reshape(bsz, seqlen, self.num_kv_heads, self.head_dim) + v = v.transpose(1, 2) + v = v * v.sigmoid() + q = F.rms_norm(q, (q.size(-1),)) + k = F.rms_norm(k, (k.size(-1),)) + cos, sin = self.rotary(seqlen, x.device, q.dtype) + if self.rope_dims < self.head_dim: + q_rope, q_pass = q[..., :self.rope_dims], q[..., self.rope_dims:] + k_rope, k_pass = k[..., :self.rope_dims], k[..., self.rope_dims:] + q_rope = apply_rotary_emb(q_rope, cos, sin) + k_rope = apply_rotary_emb(k_rope, cos, sin) + q = torch.cat([q_rope, q_pass], dim=-1) + k = torch.cat([k_rope, k_pass], dim=-1) + else: + q = apply_rotary_emb(q, cos, sin) + k = apply_rotary_emb(k, cos, sin) + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + y = F.scaled_dot_product_attention( + q, k, v, attn_mask=None, is_causal=True, + enable_gqa=(self.num_kv_heads != self.num_heads), + ) + return self.proj(y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim)) + + +class MLP(nn.Module): + def __init__(self, dim: int, mlp_mult: int): + super().__init__() + hidden = 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 ValueEmbedding(nn.Module): + """Reinject token identity into attention values. Shared table across all attn layers.""" + def __init__(self, vocab_size: int, ve_dim: int, kv_dim: int): + super().__init__() + self.embed = nn.Embedding(vocab_size, ve_dim) + nn.init.normal_(self.embed.weight, std=0.01) + self.proj = CastedLinear(ve_dim, kv_dim, bias=False) if ve_dim != kv_dim else None + if self.proj is not None: + nn.init.zeros_(self.proj.weight) + self.scale = nn.Parameter(torch.tensor(0.1, dtype=torch.float32)) + + def forward(self, token_ids: Tensor) -> Tensor: + h = self.embed(token_ids) + if self.proj is not None: + h = self.proj(h) + return h * self.scale.to(dtype=h.dtype) + + +class Block(nn.Module): + def __init__( + self, dim: int, mlp_mult: int, + mamba3_d_state: int = 64, mamba3_expand: int = 2, + mamba3_headdim: int = 64, mamba3_chunk_size: int = 64, + mamba3_ngroups: int = 1, + layer_idx: int = 0, + ): + super().__init__() + self.m3_norm = RMSNorm() + self.mlp_norm = RMSNorm() + self.mamba3 = Mamba3Layer( + dim, d_state=mamba3_d_state, expand=mamba3_expand, + headdim=mamba3_headdim, chunk_size=mamba3_chunk_size, ngroups=mamba3_ngroups, + ) + self.mlp = MLP(dim, mlp_mult) + self.m3_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, v_embed: Tensor | None = None) -> Tensor: + mix = self.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + m3_out = self.mamba3(self.m3_norm(x)) + x = x + self.m3_scale.to(dtype=x.dtype)[None, None, :] * m3_out + x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x)) + return x + + +class AttnBlock(nn.Module): + """Block with standalone attention (no SSM).""" + def __init__( + self, dim: int, mlp_mult: int, + num_heads: int = 8, num_kv_heads: int = 4, + rope_base: float = 10000.0, qk_gain_init: float = 1.0, + rope_fraction: float = 1.0, + layer_idx: int = 0, + ): + super().__init__() + self.attn_norm = RMSNorm() + self.mlp_norm = RMSNorm() + self.attn = AttentionLayer(dim, num_heads, num_kv_heads, rope_base, qk_gain_init, rope_fraction=rope_fraction) + 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, v_embed: Tensor | None = None) -> 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), v_embed=v_embed) + 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 + + +class GPT(nn.Module): + def __init__( + self, vocab_size: int, num_layers: int, model_dim: int, + mlp_mult: int, tie_embeddings: bool, tied_embed_init_std: float, + logit_softcap: float, + use_smeargate: bool = False, use_bigram_hash: bool = False, + bigram_buckets: int = 4096, bigram_hash_dim: int = 128, + use_ortho_init: bool = False, + mamba3_d_state: int = 64, mamba3_expand: int = 2, + mamba3_headdim: int = 64, mamba3_chunk_size: int = 64, + mamba3_ngroups: int = 1, + num_attn_layers: int = 1, num_heads: int = 8, num_kv_heads: int = 4, + rope_base: float = 10000.0, qk_gain_init: float = 1.0, + rope_fraction: float = 1.0, + ve_enabled: bool = False, ve_dim: int = 64, + ): + 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_ortho_init = use_ortho_init + self.tok_emb = nn.Embedding(vocab_size, model_dim) + + self.smeargate = SmearGate(model_dim) if use_smeargate else None + self.bigram_hash = BigramHash(bigram_buckets, bigram_hash_dim, model_dim) if use_bigram_hash else None + + 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(1, self.num_skip_weights, model_dim, dtype=torch.float32) + ) + + # Compute attention layer indices based on placement strategy + attn_placement = os.environ.get("ATTN_PLACEMENT", "even") + attn_indices = set() + if num_attn_layers > 0: + if attn_placement == "even": + for i in range(1, num_attn_layers + 1): + attn_indices.add(round(i * num_layers / (num_attn_layers + 1))) + elif attn_placement == "first": + attn_indices = set(range(num_attn_layers)) + elif attn_placement == "last": + attn_indices = set(range(num_layers - num_attn_layers, num_layers)) + elif attn_placement == "nemotron": + for i in range(1, num_attn_layers + 1): + idx = round(i * num_layers / (num_attn_layers + 1)) + if idx == 0: + idx = 1 + attn_indices.add(idx) + else: + attn_indices = {int(x.strip()) for x in attn_placement.split(",")} + self.attn_indices = sorted(attn_indices) + if int(os.environ.get("RANK", 0)) == 0: + print(f"attn_placement:{attn_placement} attn_indices:{self.attn_indices}") + + self.blocks = nn.ModuleList() + for i in range(num_layers): + if i in attn_indices: + self.blocks.append(AttnBlock( + model_dim, mlp_mult, + num_heads=num_heads, num_kv_heads=num_kv_heads, + rope_base=rope_base, qk_gain_init=qk_gain_init, + rope_fraction=rope_fraction, + layer_idx=i, + )) + else: + self.blocks.append(Block( + model_dim, mlp_mult, + mamba3_d_state=mamba3_d_state, mamba3_expand=mamba3_expand, + mamba3_headdim=mamba3_headdim, mamba3_chunk_size=mamba3_chunk_size, + mamba3_ngroups=mamba3_ngroups, + layer_idx=i, + )) + + kv_dim = num_kv_heads * (model_dim // num_heads) + if ve_enabled and self.attn_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.attn_indices] + ) + else: + self.ve_shared = None + self.ve_layer_scales = nn.ParameterList() + + # Depth recurrence config + recur_layers_str = os.environ.get("RECUR_LAYERS", "") + self.recur_layers = sorted(int(x) for x in recur_layers_str.split(",") if x.strip()) if recur_layers_str.strip() else [] + self.recur_mode = os.environ.get("RECUR_MODE", "block") + self.recur_enabled = False # toggled during training based on recur_start_frac + + # For "untie_mlp" mode: create separate MLPs for the repeated pass + if self.recur_mode == "untie_mlp" and self.recur_layers: + self.recur_mlps = nn.ModuleDict() + for li in self.recur_layers: + orig_mlp = self.blocks[li].mlp + new_mlp = MLP(model_dim, mlp_mult) + self.recur_mlps[str(li)] = new_mlp + + if self.recur_layers and int(os.environ.get("RANK", 0)) == 0: + print(f"depth_recurrence: layers={self.recur_layers} mode={self.recur_mode}") + + 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) + num_layers = len(self.blocks) + for name, module in self.named_modules(): + if isinstance(module, nn.Linear): + if getattr(module, "_zero_init", False): + nn.init.zeros_(module.weight) + elif self.use_ortho_init and module.weight.ndim == 2 and min(module.weight.shape) >= 16: + nn.init.orthogonal_(module.weight, gain=1.0) + if ".proj" in name and name.split(".")[-1] in ("proj", "proj_D"): + with torch.no_grad(): + module.weight.mul_(1.0 / math.sqrt(2 * num_layers)) + + def _compute_logits_and_loss(self, x: Tensor, target_ids: Tensor) -> Tensor: + 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) + 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") + + def _get_ve(self, block_idx: int, ve_cache: Tensor | None) -> Tensor | None: + if ve_cache is None or block_idx not in self.attn_indices: + return None + ve_idx = self.attn_indices.index(block_idx) + return ve_cache * self.ve_layer_scales[ve_idx].to(dtype=ve_cache.dtype) + + def _embed(self, input_ids: Tensor) -> Tensor: + x = self.tok_emb(input_ids) + if self.bigram_hash is not None: + x = x + self.bigram_hash(input_ids) + if self.smeargate is not None: + x = self.smeargate(x) + return F.rms_norm(x, (x.size(-1),)) + + def forward(self, input_ids: Tensor, target_ids: Tensor) -> Tensor: + x = self._embed(input_ids) + x = self._run_blocks(x, x, input_ids) + loss = self._compute_logits_and_loss(x, target_ids) + # DDP fix: ensure recur_mlps participate in the computation graph even before + # recurrence is enabled, so DDP doesn't complain about unused parameters. + if hasattr(self, 'recur_mlps') and not self.recur_enabled: + dummy = sum(p.sum() * 0.0 for mlp in self.recur_mlps.values() for p in mlp.parameters()) + loss = loss + dummy + return loss + + def _build_layer_schedule(self) -> list[int]: + """Build the virtual layer schedule. Without recurrence, it's [0,1,...,N-1]. + With recurrence, the recur_layers are repeated recur_repeats times.""" + num_layers = len(self.blocks) + schedule = list(range(num_layers)) + if self.recur_enabled and self.recur_layers: + insert_pos = max(self.recur_layers) + 1 + repeats = int(os.environ.get("RECUR_REPEATS", 1)) + schedule = schedule[:insert_pos] + self.recur_layers * repeats + schedule[insert_pos:] + return schedule + + def _run_blocks(self, x: Tensor, x0: Tensor, input_ids: Tensor | None = None) -> Tensor: + ve_cache = self.ve_shared(input_ids) if (self.ve_shared is not None and input_ids is not None) else None + schedule = self._build_layer_schedule() + num_physical = len(self.blocks) + + # Split schedule into encoder (first half) and decoder (second half) + mid = len(schedule) // 2 + encoder_schedule = schedule[:mid] + decoder_schedule = schedule[mid:] + + skips: list[Tensor] = [] + recur_pass_count: dict[int, int] = {} # track how many times each layer has been run + + for vi, bi in enumerate(encoder_schedule): + recur_pass_count[bi] = recur_pass_count.get(bi, 0) + 1 + is_repeat = recur_pass_count[bi] > 1 + + if is_repeat and self.recur_mode == "untie_mlp" and str(bi) in getattr(self, 'recur_mlps', {}): + # Run block with separate MLP for repeated pass + block = self.blocks[bi] + mix = block.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + m3_out = block.mamba3(block.m3_norm(x)) if hasattr(block, 'mamba3') else block.attn(block.attn_norm(x)) + scale_name = 'm3_scale' if hasattr(block, 'm3_scale') else 'attn_scale' + x = x + getattr(block, scale_name).to(dtype=x.dtype)[None, None, :] * m3_out + mlp_out = self.recur_mlps[str(bi)](block.mlp_norm(x)) + x = x + block.mlp_scale.to(dtype=x.dtype)[None, None, :] * mlp_out + else: + x = self.blocks[bi](x, x0, v_embed=self._get_ve(bi, ve_cache)) + skips.append(x) + + skip_idx = 0 + for vi, bi in enumerate(decoder_schedule): + recur_pass_count[bi] = recur_pass_count.get(bi, 0) + 1 + is_repeat = recur_pass_count[bi] > 1 + + if skip_idx < len(skips): + x = x + self.skip_weights[0, min(skip_idx, self.num_skip_weights - 1)].to(dtype=x.dtype)[None, None, :] * skips.pop() + skip_idx += 1 + + if is_repeat and self.recur_mode == "untie_mlp" and str(bi) in getattr(self, 'recur_mlps', {}): + block = self.blocks[bi] + mix = block.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + m3_out = block.mamba3(block.m3_norm(x)) if hasattr(block, 'mamba3') else block.attn(block.attn_norm(x)) + scale_name = 'm3_scale' if hasattr(block, 'm3_scale') else 'attn_scale' + x = x + getattr(block, scale_name).to(dtype=x.dtype)[None, None, :] * m3_out + mlp_out = self.recur_mlps[str(bi)](block.mlp_norm(x)) + x = x + block.mlp_scale.to(dtype=x.dtype)[None, None, :] * mlp_out + else: + x = self.blocks[bi](x, x0, v_embed=self._get_ve(bi, ve_cache)) + return x + + def forward_logits(self, input_ids: Tensor) -> Tensor: + bsz, seqlen = input_ids.shape + x = self._embed(input_ids) + x = self._run_blocks(x, x, input_ids) + x = self.final_norm(x).reshape(-1, x.size(-1)) + w = self.tok_emb.weight if self.tie_embeddings else self.lm_head.weight + logits = self.logit_softcap * torch.tanh(F.linear(x, w) / self.logit_softcap) + return logits.reshape(bsz, seqlen, -1) + + +# ----------------------------- +# 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 = "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 = int(os.environ.get("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 + torch.backends.cuda.enable_flash_sdp(True) + torch.backends.cuda.enable_mem_efficient_sdp(False) + torch.backends.cuda.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) + + 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}") + + base_model = GPT( + vocab_size=args.vocab_size, num_layers=args.num_layers, model_dim=args.model_dim, + mlp_mult=args.mlp_mult, + tie_embeddings=args.tie_embeddings, tied_embed_init_std=args.tied_embed_init_std, + logit_softcap=args.logit_softcap, + use_smeargate=args.use_smeargate, use_bigram_hash=args.use_bigram_hash, + bigram_buckets=args.bigram_buckets, bigram_hash_dim=args.bigram_hash_dim, + use_ortho_init=args.use_ortho_init, + mamba3_d_state=args.mamba3_d_state, mamba3_expand=args.mamba3_expand, + mamba3_headdim=args.mamba3_headdim, mamba3_chunk_size=args.mamba3_chunk_size, + mamba3_ngroups=args.mamba3_ngroups, + num_attn_layers=args.num_attn_layers, num_heads=args.num_heads, + num_kv_heads=args.num_kv_heads, rope_base=args.rope_base, + qk_gain_init=args.qk_gain_init, + rope_fraction=args.rope_fraction, + ve_enabled=args.ve_enabled, ve_dim=args.ve_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 + + 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 hasattr(base_model, 'skip_weights') and base_model.skip_weights.numel() > 0: + scalar_params.append(base_model.skip_weights) + if base_model.smeargate is not None: + scalar_params.append(base_model.smeargate.gate) + if base_model.bigram_hash is not None: + scalar_params.append(base_model.bigram_hash.table.weight) + matrix_params.append(base_model.bigram_hash.proj.weight) + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr + tok_params = [{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr}] + if base_model.ve_shared is not None: + tok_params.append({"params": [base_model.ve_shared.embed.weight], "lr": token_lr, "base_lr": token_lr}) + scalar_params.append(base_model.ve_shared.scale) + for p in base_model.ve_layer_scales: + scalar_params.append(p) + if base_model.ve_shared.proj is not None: + matrix_params.append(base_model.ve_shared.proj.weight) + optimizer_tok = torch.optim.Adam( + tok_params, + betas=(args.beta1, args.beta2), eps=args.adam_eps, fused=True, + ) + optimizer_muon = Muon( + matrix_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(f"mode:mamba3_hybrid num_attn_layers:{args.num_attn_layers} attn_indices:{base_model.attn_indices}") + log0(f"ssd: d_state:{args.mamba3_d_state} expand:{args.mamba3_expand} headdim:{args.mamba3_headdim}") + log0(f"attn: num_heads:{args.num_heads} num_kv_heads:{args.num_kv_heads} rope_base:{args.rope_base}") + log0(f"num_layers:{args.num_layers} mlp_mult:{args.mlp_mult}") + 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}") + + 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) + frac = max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0 + else: + step_ms = elapsed_ms / max(step, 1) + warmdown_ms = args.warmdown_iters * step_ms + remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0) + frac = remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0 + if args.warmdown_shape == "cosine" and frac < 1.0: + return 0.5 * (1.0 + math.cos(math.pi * (1.0 - frac))) + return frac + + 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) + + training_time_ms = 0.0 + stop_after_step: int | None = None + swa_state: dict[str, Tensor] | None = None + swa_count = 0 + + ema_state: dict[str, Tensor] | None = None + if args.ema_enabled: + ema_state = {name: t.detach().float().clone() for name, t in base_model.state_dict().items()} + 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, grad_accum_steps, + 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) + + if args.qat_start_frac > 0 and max_wallclock_ms: + elapsed_frac_qat = elapsed_ms / max_wallclock_ms + qat_active = elapsed_frac_qat >= args.qat_start_frac + qat_bits = args.quant_bits if qat_active else 0 + if qat_active and any(m._qat_bits == 0 for m in base_model.modules() if isinstance(m, CastedLinear)): + log0(f"qat:enabled bits={qat_bits} at step {step} frac={elapsed_frac_qat:.2f}") + for m in base_model.modules(): + if isinstance(m, CastedLinear): + m._qat_bits = qat_bits + + # Late QAT: trigger when lr_mul drops below threshold (SOTA approach) + if args.late_qat_threshold > 0 and scale < args.late_qat_threshold: + if any(m._qat_bits == 0 for m in base_model.modules() if isinstance(m, CastedLinear)): + qat_bits = args.quant_bits + log0(f"late_qat:enabled bits={qat_bits} at step {step} scale={scale:.4f}") + for m in base_model.modules(): + if isinstance(m, CastedLinear): + m._qat_bits = qat_bits + + # Depth recurrence: enable after recur_start_frac of training + if args.recur_layers_str.strip() and not base_model.recur_enabled: + train_frac = step / max(args.iterations, 1) + if train_frac >= args.recur_start_frac: + base_model.recur_enabled = True + schedule = base_model._build_layer_schedule() + log0(f"depth_recurrence:enabled at step {step} frac={train_frac:.2f} schedule={schedule}") + + zero_grad_all() + train_loss = torch.zeros((), device=device) + cur_seq_len = args.train_seq_len + 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, cur_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() + + step += 1 + + if ema_state is not None: + d = args.ema_decay + with torch.no_grad(): + for name, t in base_model.state_dict().items(): + ema_state[name].mul_(d).add_(t.detach().float(), alpha=1.0 - d) + + if args.swa_enabled and scale < args.swa_start_frac and step % args.swa_every == 0: + if swa_state is None: + swa_state = {name: t.detach().cpu().clone().float() for name, t in base_model.state_dict().items()} + swa_count = 1 + else: + for name, t in base_model.state_dict().items(): + swa_state[name] += t.detach().cpu().float() + swa_count += 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" + ) + + if args.sweep_mode: + log0("sweep_mode:exiting after training loop (skipping quantization/serialization)") + if distributed: + dist.destroy_process_group() + return + + for m in base_model.modules(): + if isinstance(m, CastedLinear): + m._qat_bits = 0 + + if ema_state is not None: + log0("ema:applying EMA weights") + current_state = base_model.state_dict() + avg_state = {name: t.to(dtype=current_state[name].dtype) for name, t in ema_state.items()} + del ema_state + base_model.load_state_dict(avg_state, strict=True) + elif args.swa_enabled and swa_state is not None and swa_count > 1: + log0(f"swa:applying averaged {swa_count} checkpoints") + current_state = base_model.state_dict() + avg_state = { + name: (tensor / swa_count).to(dtype=current_state[name].dtype) + for name, tensor in swa_state.items() + } + del swa_state + base_model.load_state_dict(avg_state, strict=True) + + 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") + + if args.use_gptq and master_process: + log0("gptq:generating autoregressive calibration data...") + t_gptq = time.perf_counter() + ar_tokens = generate_autoregressive_calib( + base_model, device, + num_seqs=args.gptq_num_seqs, seq_len=args.gptq_gen_len, + vocab_size=args.vocab_size, temperature=args.gptq_gen_temp, + batch_size=8, seed=1337, + ) + log0(f"gptq:generated {len(ar_tokens)} seqs in {time.perf_counter()-t_gptq:.1f}s") + log0("gptq:collecting hessians...") + hessians = collect_hessians_from_tokens(base_model, ar_tokens, device) + log0(f"gptq:collected hessians for {len(hessians)} layers") + del ar_tokens + torch.cuda.empty_cache() + # Apply GPTQ to each quantizable layer in the state dict. + # For Mamba-3 in_proj: keep recurrence-propagating rows (B, dd_dt, dd_A, trap) + # at original precision — errors in these rows compound through the SSD state. + fp16_masks = get_mamba3_in_proj_fp16_row_mask(base_model) if args.fp16_inproj_rows else {} + sd = base_model.state_dict() + gptq_sd = {} + for name, t in sd.items(): + t_cpu = t.detach().cpu() + H = hessians.get(name) + fp16_mask = fp16_masks.get(name) + if fp16_mask is not None and H is not None: + # Mixed-precision: GPTQ only on non-propagating rows (z, x, C, angles) + quant_rows = ~fp16_mask + clip_range = (1 << (args.quant_bits - 1)) - 1 + q, s = quantize_int6_gptq(t_cpu[quant_rows], hessian=H, clip_range=clip_range) + out = t_cpu.clone() + out[quant_rows] = (q.float() * s.float()[:, None]).to(t_cpu.dtype) + # fp16_mask rows stay at original BF16 — quantize_state_dict_int8 will + # store them in FP16 passthrough (not INT6 quantized) + gptq_sd[name] = out + elif H is not None and t_cpu.is_floating_point() and t_cpu.ndim == 2 and t_cpu.numel() > INT8_KEEP_FLOAT_MAX_NUMEL: + q, s = quantize_int6_gptq(t_cpu, hessian=H, clip_range=(1 << (args.quant_bits - 1)) - 1) + gptq_sd[name] = (q.float() * s.float()[:, None]).to(t_cpu.dtype) + else: + gptq_sd[name] = t_cpu + base_model.load_state_dict(gptq_sd, strict=True) + del hessians, gptq_sd + torch.cuda.empty_cache() + log0(f"gptq:quantization complete in {time.perf_counter()-t_gptq:.1f}s total") + + mlp_bits = args.quant_bits_mlp if args.quant_bits_mlp > 0 else args.quant_bits + fp16_masks_for_quant = get_mamba3_in_proj_fp16_row_mask(base_model) if args.fp16_inproj_rows else None + quant_obj, quant_stats = quantize_state_dict_int8( + base_model.state_dict(), fp16_embed=args.fp16_embed, quant_bits=args.quant_bits, + quant_bits_mlp=args.quant_bits_mlp, search_clip=args.gptq_lite, + fp16_row_masks=fp16_masks_for_quant, + ) + quant_buf = io.BytesIO() + torch.save(quant_obj, quant_buf) + quant_raw = quant_buf.getvalue() + if args.use_lzma: + quant_blob = lzma.compress(quant_raw, preset=9) + compress_fmt = "lzma-9" + elif args.use_zstd and HAS_ZSTD: + cctx = zstd.ZstdCompressor(level=22) + quant_blob = cctx.compress(quant_raw) + compress_fmt = "zstd-22" + else: + quant_blob = zlib.compress(quant_raw, level=9) + compress_fmt = "zlib-9" + 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 int{args.quant_bits}+{compress_fmt}: {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 int{args.quant_bits}+{compress_fmt}: {quant_file_bytes + code_bytes} bytes") + + if distributed: + dist.barrier() + with open("final_model.int8.ptz", "rb") as f: + quant_blob_disk = f.read() + if args.use_lzma: + quant_decompressed = lzma.decompress(quant_blob_disk) + elif args.use_zstd and HAS_ZSTD: + dctx = zstd.ZstdDecompressor() + quant_decompressed = dctx.decompress(quant_blob_disk) + else: + quant_decompressed = zlib.decompress(quant_blob_disk) + quant_state = torch.load(io.BytesIO(quant_decompressed), map_location="cpu") + base_model.load_state_dict(dequantize_state_dict_int8(quant_state), strict=True) + + if args.ttt_enabled: + torch.cuda.synchronize() + t_ttt = time.perf_counter() + + num_frozen = min(args.ttt_freeze_blocks, len(base_model.blocks)) + for i in range(num_frozen): + for p in base_model.blocks[i].parameters(): + p.requires_grad_(False) + ttt_params = [p for p in base_model.parameters() if p.requires_grad] + if args.ttt_optimizer == "adamw": + ttt_opt = torch.optim.AdamW(ttt_params, lr=args.ttt_lr, weight_decay=0.01) + else: + ttt_opt = torch.optim.SGD(ttt_params, lr=args.ttt_lr, momentum=args.ttt_momentum) + ttt_seq_len = args.train_seq_len + + total_tokens_val = val_tokens.numel() - 1 + total_seqs = total_tokens_val // ttt_seq_len + total_chunks = (total_seqs + args.eval_batch_seqs - 1) // args.eval_batch_seqs + + ttt_loss_sum = torch.zeros((), device=device, dtype=torch.float64) + ttt_token_count = torch.zeros((), device=device, dtype=torch.float64) + ttt_byte_count = torch.zeros((), device=device, dtype=torch.float64) + ttt_step = 0 + + log0(f"ttt:starting optimizer={args.ttt_optimizer} lr={args.ttt_lr} freeze_blocks={num_frozen} epochs={args.ttt_epochs} chunks={total_chunks}") + + for seq_idx in range(0, total_seqs, args.eval_batch_seqs): + batch_end = min(seq_idx + args.eval_batch_seqs, total_seqs) + bsz = batch_end - seq_idx + raw_start = seq_idx * ttt_seq_len + raw_end = batch_end * ttt_seq_len + 1 + chunk = val_tokens[raw_start:raw_end].to(device=device, dtype=torch.int64) + x = chunk[:-1].reshape(bsz, ttt_seq_len) + y = chunk[1:].reshape(bsz, ttt_seq_len) + + base_model.eval() + with torch.inference_mode(): + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + logits = base_model.forward_logits(x) + nll = F.cross_entropy( + logits.reshape(-1, logits.size(-1)).float(), y.reshape(-1), reduction="none", + ) + ttt_loss_sum += nll.to(torch.float64).sum() + n_tokens = float(y.numel()) + ttt_token_count += n_tokens + prev_ids = x.reshape(-1) + tgt_ids = y.reshape(-1) + tbytes = base_bytes_lut[tgt_ids].to(dtype=torch.int16) + tbytes += (has_leading_space_lut[tgt_ids] & ~is_boundary_token_lut[prev_ids]).to(dtype=torch.int16) + ttt_byte_count += tbytes.to(torch.float64).sum() + + base_model.train() + for _epoch in range(args.ttt_epochs): + ttt_opt.zero_grad() + with torch.autocast(device_type="cuda", dtype=torch.bfloat16): + loss = base_model(x, y) + loss.backward() + ttt_opt.step() + ttt_step += 1 + + for p in base_model.parameters(): + p.requires_grad_(True) + base_model.eval() + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(ttt_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(ttt_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(ttt_byte_count, op=dist.ReduceOp.SUM) + + q_val_loss = (ttt_loss_sum / ttt_token_count).item() + bits_per_token = q_val_loss / math.log(2.0) + tokens_per_byte = ttt_token_count.item() / ttt_byte_count.item() + q_val_bpb = float(bits_per_token * tokens_per_byte) + eval_mode = "online_ttt" + log0(f"ttt:completed steps:{ttt_step} time:{time.perf_counter() - t_ttt:.1f}s") + + else: + torch.cuda.synchronize() + t_ttt = time.perf_counter() + + torch.cuda.synchronize() + t_qeval = time.perf_counter() + + if not args.ttt_enabled: + use_sliding = args.eval_stride > 0 and args.eval_stride < args.train_seq_len + if use_sliding: + q_val_loss, q_val_bpb = eval_val_sliding( + args, base_model, rank, world_size, device, + val_tokens, base_bytes_lut, has_leading_space_lut, is_boundary_token_lut, + ) + eval_mode = "sliding" + else: + 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, + ) + eval_mode = "standard" + torch.cuda.synchronize() + log0( + f"final_int8_zlib_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} " + f"eval_mode:{eval_mode} 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}") + + if distributed: + dist.destroy_process_group() + + +if __name__ == "__main__": + main()