diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/README.md b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/README.md new file mode 100644 index 0000000000..0b2fb77122 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/README.md @@ -0,0 +1,77 @@ +# SP1024 + Shared-V(last3) 3-seed non-record submission + +This is a stable non-record 16MB submission based on the official SP1024 tokenizer and a compact transformer with structured skip fusion. + +## Summary + +This submission uses: + +- official `fineweb_1024_bpe.model` +- standard FineWeb SP1024 dataset +- structured skip fusion (`BIFPN2_MODE=1`) +- XSA on the last 4 layers +- 2-gram scaffold with fade-out +- shared V across the last 3 layers + +This submission is intended as a stable, rule-compliant baseline submission rather than a leaderboard-top attempt. + +## Representative run + +Representative seed: **2027** + +Representative exact roundtrip BPB: **1.27717259** + +Submission size: **15973626 bytes** + +## 3-seed results + +| seed | last_val_bpb | roundtrip_exact_val_bpb | submission_bytes | +|------|--------------|-------------------------|------------------| +| 1337 | 1.2791 | 1.28079096 | 15972114 | +| 2027 | 1.2755 | 1.27717259 | 15973626 | +| 3407 | 1.2779 | 1.27952108 | 15975453 | + +3-seed mean exact roundtrip BPB: **1.27916154** + +## Files + +- `submission.json`: metadata for this submission +- `train.log`: representative training log +- `train_gpt.py`: training script snapshot used for this submission +- `config.json`: resolved config for the representative run +- `seed_runs.csv`: all 3 seed results +- `requirements.txt`: minimal environment dependencies + +## Main configuration + +Key settings: + +- tokenizer: SP1024 +- `BIFPN2_MODE=1` +- `XSA_ENABLED=1` +- `XSA_LAST_N_LAYERS=4` +- `NGRAM_MAX_N=2` +- `NGRAM_FADE_ENABLE=1` +- `CROSS_LAYER_KV_SHARING_ENABLED=1` +- `CROSS_LAYER_KV_SHARE_V=1` +- `CROSS_LAYER_KV_PAIRWISE=0` +- `CROSS_LAYER_KV_PARTIAL_HEAD=0` + +## Notes + +- This submission does **not** modify the tokenizer or dataset. +- This is a reproducibility-focused non-record submission under the 16MB artifact limit. +- The representative run uses seed 2027 because it was the best run among the 3 submission seeds. + +## Reproduction + +Typical command pattern: + +```bash +python launchv3.py config_submission_sharev3_3seed.json \ + --train-script mytrain_gpt_v2_1.py \ + --output output/submission_sharev3_3seed \ + --stop-mode steps \ + --max-steps 3000 \ + --only submission_seed2027 +''' diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/config.json b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/config.json new file mode 100644 index 0000000000..ef3a92ab95 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/config.json @@ -0,0 +1,121 @@ +{ + "_comment_TRACK": "Stable non-record submission candidate under 16MB, using official SP1024 tokenizer and no tokenizer changes", + "_comment_DATA": "Official SP1024 data/tokenizer", + "DATA_PATH": "./data/datasets/fineweb10B_sp1024", + "TOKENIZER_PATH": "./data/tokenizers/fineweb_1024_bpe.model", + "VOCAB_SIZE": 1024, + "_comment_CORE": "Core model shape", + "NUM_LAYERS": 9, + "MODEL_DIM": 512, + "NUM_HEADS": 8, + "NUM_KV_HEADS": 4, + "MLP_MULT": 2, + "TIE_EMBEDDINGS": 1, + "ROPE_BASE": 10000.0, + "LOGIT_SOFTCAP": 30.0, + "QK_GAIN_INIT": 1.5, + "_comment_TRAIN": "Train schedule", + "GRAD_ACCUM_STEPS": 4, + "TRAIN_BATCH_TOKENS": 524288, + "TRAIN_SEQ_LEN": 1024, + "ITERATIONS": 20000, + "WARMUP_STEPS": 20, + "WARMDOWN_ITERS": 1200, + "STOP_MODE": "steps", + "MAX_TRAIN_STEPS": 3000, + "MAX_WALLCLOCK_SECONDS": 3600.0, + "_comment_OPTIM": "Optimizer", + "MATRIX_LR": 0.04, + "SCALAR_LR": 0.04, + "EMBED_LR": 0.6, + "HEAD_LR": 0.008, + "TIED_EMBED_LR": 0.05, + "TIED_EMBED_INIT_STD": 0.005, + "MUON_MOMENTUM": 0.95, + "MUON_BACKEND_STEPS": 5, + "MUON_MOMENTUM_WARMUP_START": 0.85, + "MUON_MOMENTUM_WARMUP_STEPS": 500, + "BETA1": 0.9, + "BETA2": 0.95, + "ADAM_EPS": 1e-08, + "GRAD_CLIP_NORM": 0.0, + "_comment_SKIP": "Best stable under-size stack", + "FDA_MODE": 0, + "BIFPN_MODE": 0, + "BIFPN2_MODE": 1, + "BIFPN_GROUP_COUNT": 8, + "BIFPN_BAND_WIDTH": 1, + "BIFPN_NORM_EPS": 0.0001, + "BIFPN_INIT_MAIN": 1.0, + "BIFPN_INIT_NEIGHBOR": 0.15, + "BIFPN_INIT_FAR": 0.0, + "_comment_STAB": "Stability toggles", + "SCALEDLM_HEAD": 1, + "SMEAR_MODE": 0, + "SMEAR_WINDOW": 4, + "SMEAR_GATE": 0, + "ROPE_DIMS": -1, + "LEARNABLE_ROPE": 0, + "LN_SCALE": 1, + "LEARNABLE_LN_SCALE": 0, + "AFFINE_NORM": 0, + "_comment_XSA": "Keep XSA on last 4 layers", + "XSA_ENABLED": 1, + "XSA_LAST_N_LAYERS": 4, + "XSA_EPS": 1e-06, + "_comment_VALUE_PATH": "Use plain shared V only; this was the under-16MB stable candidate", + "V_SKIP_ENABLED": 0, + "V_SKIP_LAST_N_LAYERS": 4, + "V_SKIP_MODE": "scalar", + "V_SKIP_GROUP_COUNT": 8, + "CROSS_LAYER_V_ENABLED": 0, + "CROSS_LAYER_V_LAST_N_LAYERS": 4, + "CROSS_LAYER_V_MODE": "residual", + "CROSS_LAYER_V_GROUP_COUNT": 4, + "_comment_MEMORY_PATH": "Share V across later layers, no K sharing", + "CROSS_LAYER_KV_SHARING_ENABLED": 1, + "CROSS_LAYER_KV_LAST_N_LAYERS": 3, + "CROSS_LAYER_KV_SHARE_K": 0, + "CROSS_LAYER_KV_SHARE_V": 1, + "CROSS_LAYER_KV_PAIRWISE": 0, + "CROSS_LAYER_KV_PARTIAL_HEAD": 0, + "CROSS_LAYER_KV_PARTIAL_HEAD_COUNT": 2, + "_comment_PLE": "Disabled for this stable submission", + "PLE_ENABLED": 0, + "PLE_TEMPORAL_CONV": 0, + "PLE_DIM": 32, + "PLE_MODE": "post_attn", + "PLE_TOKEN_SCALE_INIT": 1.0, + "PLE_CTX_SCALE_INIT": 1.0, + "PLE_RESID_SCALE_INIT": 0.01, + "_comment_MTP": "Disabled", + "MTP_NUM_HEADS": 0, + "MTP_LOSS_WEIGHT": 0.2, + "MTPHEAD_MLPMODE": 0, + "_comment_NGRAM": "Keep 2-gram scaffold + fade-out", + "NGRAM_VOCAB_SIZE": 2048, + "NGRAM_DIM": 128, + "NGRAM_MAX_N": 2, + "NGRAM_FADE_ENABLE": 1, + "NGRAM_FADE_START_FRAC": 0.15, + "NGRAM_FADE_END_FRAC": 0.45, + "NGRAM_FADE_MIN_SCALE": 0.0, + "_comment_EMA_QAT": "Keep EMA and conservative late QAT", + "EMA_ENABLED": 1, + "EMA_DECAY": 0.997, + "DYNAMIC_CLIP_PERCENTILES": "100.0,99.9999,99.9995,99.995,99.9", + "LATE_QAT_RATIO": 0.15, + "_comment_EVAL": "Submission run should use non-sliding eval for direct comparability", + "VAL_LOSS_EVERY": 1000, + "VAL_BATCH_SIZE": 524288, + "EVAL_USE_SLIDING_WINDOW": 0, + "EVAL_STRIDE": 1024, + "EVAL_BATCH_SEQS": 16, + "_comment_LOGGING": "Telemetry/logging", + "TELEMETRY_EVERY": 50, + "TRAIN_LOG_EVERY": 200, + "PROFILE_RUN": 0, + "PROFILE_WARMUP_STEPS": 5, + "PROFILE_ACTIVE_STEPS": 10, + "SEED": 2027 +} \ No newline at end of file diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/requirements.txt b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/requirements.txt new file mode 100644 index 0000000000..911b0e52f0 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/requirements.txt @@ -0,0 +1,10 @@ +numpy +tqdm +torch +huggingface-hub +kernels +setuptools +typing-extensions==4.15.0 +datasets +tiktoken +sentencepiece \ No newline at end of file diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/seed_runs.csv b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/seed_runs.csv new file mode 100644 index 0000000000..db4903a3cf --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/seed_runs.csv @@ -0,0 +1,4 @@ +experiment,seed,last_val_bpb,roundtrip_val_bpb,roundtrip_exact_val_bpb,submission_bytes,stopped_step,output_dir +submission_seed1337,1337,1.2791,1.2808,1.28079096,15972114,3000,output/submission_sharev3_3seed/submission_seed1337_20260418_100202 +submission_seed2027,2027,1.2755,1.2772,1.27717259,15973626,3000,output/submission_sharev3_3seed/submission_seed2027_20260418_103507 +submission_seed3407,3407,1.2779,1.2795,1.27952108,15975453,3000,output/submission_sharev3_3seed/submission_seed3407_20260418_110812 diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/submission.json b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/submission.json new file mode 100644 index 0000000000..593c1e3b09 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/submission.json @@ -0,0 +1,17 @@ +{ + "title": "SP1024 + Shared-V(last3) + BIFPN2 + XSA4 + NGram Fade", + "author": "Kaikai Liu", + "github_id": "lkk688", + "track": "non-record-16mb", + "description": "Stable SP1024 non-record submission under the 16MB artifact cap.", + "val_bpb": 1.27717259, + "artifact_bytes": 15973626, + "representative_seed": 2027, + "seeds": [ + 1337, + 2027, + 3407 + ], + "tokenizer": "official fineweb_1024_bpe.model", + "dataset": "official fineweb10B_sp1024" +} diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train.log b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train.log new file mode 100644 index 0000000000..9dcace2b19 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train.log @@ -0,0 +1,76 @@ +output/submission_sharev3_3seed/submission_seed2027_20260418_103507/20260418_103511.txt +val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path=./data/tokenizers/fineweb_1024_bpe.model +train_loader:dataset:fineweb10B_sp1024 train_shards:80 +val_loader:shards pattern=./data/datasets/fineweb10B_sp1024/fineweb_val_*.bin tokens:62021632 +Architecture: Discrete N-Gram Hash (Max N=2) +Architecture: StructuredGroupSignedBiFPN (groups=8, band=1) +model_params:17390313 +world_size:1 grad_accum_steps:4 +sdp_backends:cudnn=False flash=True mem_efficient=False math=False +attention_mode:gqa num_heads:8 num_kv_heads:4 +tie_embeddings:True embed_lr:0.05 head_lr:0.0 matrix_lr:0.04 scalar_lr:0.04 +train_batch_tokens:524288 train_seq_len:1024 iterations:20000 warmup_steps:20 max_wallclock_seconds:3600.000 +seed:2027 +Architecture Skip Mode: Symmetric U-Net +Enhancement: Discrete N-Gram Hash (Max N=2) +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 +EMA Enabled: decay=0.997 +Scheduled Late QAT to start at step 2550 (last 15.0%) +step:0/3000 val_loss:6.9310 val_bpb:4.1049 train_time:5ms step_avg:4.66ms +step:1/3000 train_loss:6.9310 train_time:4328ms step_avg:4328.03ms +step:2/3000 train_loss:6.7809 train_time:7824ms step_avg:3912.03ms +step:3/3000 train_loss:6.3509 train_time:8434ms step_avg:2811.31ms +step:4/3000 train_loss:6.0286 train_time:9048ms step_avg:2262.10ms +step:5/3000 train_loss:5.8585 train_time:9663ms step_avg:1932.65ms +step:6/3000 train_loss:5.7350 train_time:10276ms step_avg:1712.72ms +step:7/3000 train_loss:5.6178 train_time:10890ms step_avg:1555.74ms +step:8/3000 train_loss:5.5590 train_time:11507ms step_avg:1438.42ms +step:9/3000 train_loss:5.4568 train_time:12123ms step_avg:1346.98ms +step:10/3000 train_loss:5.3681 train_time:12735ms step_avg:1273.49ms +step:200/3000 train_loss:2.7164 train_time:130023ms step_avg:650.12ms +step:400/3000 train_loss:2.3737 train_time:253575ms step_avg:633.94ms +step:600/3000 train_loss:2.4822 train_time:377129ms step_avg:628.55ms +step:800/3000 train_loss:2.3391 train_time:500560ms step_avg:625.70ms +step:1000/3000 train_loss:2.3517 train_time:624011ms step_avg:624.01ms +step:1000/3000 val_loss:2.3286 val_bpb:1.3791 train_time:624012ms step_avg:624.01ms +step:1200/3000 train_loss:2.2892 train_time:747509ms step_avg:622.92ms +step:1400/3000 train_loss:2.3483 train_time:870940ms step_avg:622.10ms +step:1600/3000 train_loss:2.2245 train_time:994295ms step_avg:621.43ms +step:1800/3000 train_loss:2.2709 train_time:1117660ms step_avg:620.92ms +step:2000/3000 train_loss:2.2152 train_time:1240992ms step_avg:620.50ms +step:2000/3000 val_loss:2.2320 val_bpb:1.3219 train_time:1240994ms step_avg:620.50ms +step:2200/3000 train_loss:2.1446 train_time:1364383ms step_avg:620.17ms +step:2400/3000 train_loss:2.1720 train_time:1487687ms step_avg:619.87ms +[Step 2550] Activating Late QAT — enabling branchless STE quantization. +step:2600/3000 train_loss:2.2332 train_time:1610963ms step_avg:619.60ms +step:2800/3000 train_loss:2.1837 train_time:1734230ms step_avg:619.37ms +step:3000/3000 train_loss:2.1042 train_time:1857569ms step_avg:619.19ms +step:3000/3000 val_loss:2.1537 val_bpb:1.2755 train_time:1857570ms step_avg:619.19ms +peak memory allocated: 22182 MiB reserved: 24640 MiB +Applying EMA weights for final evaluation... +Serialized model: 67895209 bytes +Code size: 126855 bytes +Total submission size: 68022064 bytes +Serialized model int8+zlib: 15846771 bytes (payload:17577610 raw_torch:17627197 payload_ratio:3.86x) +Total submission size int8+zlib: 15973626 bytes +final_int8_zlib_roundtrip val_loss:2.1565 val_bpb:1.2772 eval_time:18650ms +final_int8_zlib_roundtrip_exact val_loss:2.15645243 val_bpb:1.27717259 diff --git a/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train_gpt.py b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train_gpt.py new file mode 100644 index 0000000000..e6f1d7cec7 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-18_SP1024_ShareVLast3_3Seed/train_gpt.py @@ -0,0 +1,2654 @@ +""" +The `train_gpt.py` and `train_gpt_mlx.py` scripts are intended as good launching-off points for new participants, not SOTA configs. We'll accept PRs that tune, improve, or simplify these scripts without significantly increasing complexity, but competitive submissions should stay in the `/records` folder. + +Hard stop: To keep readable for newcomers, let's make sure `train_gpt.py` and `train_gpt_mlx.py` never are longer than 1500 lines. +""" + +from __future__ import annotations + +import copy +import glob +import io +import math +import os +import random +import subprocess +import sys +import time +import uuid +from xml.parsers.expat import model +import zlib +from pathlib import Path + +import numpy as np +import sentencepiece as spm +import torch +import torch.profiler +import torch.distributed as dist +import torch.nn.functional as F +from torch import Tensor, nn +from torch.nn.parallel import DistributedDataParallel as DDP + +import json # === NEW: For telemetry logging === + +# ----------------------------- +# HYPERPARAMETERS +# ----------------------------- +# Default Simple Baseline run: +# - 9 transformer blocks at width 512 +# - 8 attention heads with 4 KV heads (GQA) and 2x MLP expansion +# - vocab size 1024, sequence length 1024, tied embeddings +# - 524,288 train tokens per step for 20,000 iterations with a ~10 minute cap + +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", 1200)) + warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) + train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 524_288)) + + # NEW: Sliding window validation control + eval_use_sliding_window = bool(int(os.environ.get("EVAL_USE_SLIDING_WINDOW", "0"))) + eval_stride = int(os.environ.get("EVAL_STRIDE", "128")) #64, 128, 1024 + #With TRAIN_SEQ_LEN=1024, EVAL_STRIDE=1024 means no real overlap. That mostly defeats the purpose of sliding eval. + eval_batch_seqs = int(os.environ.get("EVAL_BATCH_SEQS", "16")) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 1024)) + max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) + qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) + + # Model shape. + vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) + num_layers = int(os.environ.get("NUM_LAYERS", 9)) + num_kv_heads = int(os.environ.get("NUM_KV_HEADS", 4)) + model_dim = int(os.environ.get("MODEL_DIM", 512)) + num_heads = int(os.environ.get("NUM_HEADS", 8)) + mlp_mult = int(os.environ.get("MLP_MULT", 2)) + tie_embeddings = bool(int(os.environ.get("TIE_EMBEDDINGS", "1"))) + rope_base = float(os.environ.get("ROPE_BASE", 10000.0)) + logit_softcap = float(os.environ.get("LOGIT_SOFTCAP", 30.0)) + + # Optimizer hyperparameters. + embed_lr = float(os.environ.get("EMBED_LR", 0.6)) + head_lr = float(os.environ.get("HEAD_LR", 0.008)) + tied_embed_lr = float(os.environ.get("TIED_EMBED_LR", 0.05)) + tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.005)) + matrix_lr = float(os.environ.get("MATRIX_LR", 0.04)) + scalar_lr = float(os.environ.get("SCALAR_LR", 0.04)) + muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.95)) + 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)) + + # === NEW: TELEMETRY AND ABLATION CONTROL === + # Set FDA_MODE=1 to use Forward Dense Addition skips instead of U-Net skips. + fda_mode = bool(int(os.environ.get("FDA_MODE", "0"))) + # Set to > 0 (e.g., 50) to log internal states without hurting speed. + output_dir = os.environ.get("OUTPUT_DIR", "") + telemetry_every = int(os.environ.get("TELEMETRY_EVERY", "0")) + telemetry_file = os.environ.get("TELEMETRY_FILE", os.path.join(output_dir, "telemetry.jsonl") if output_dir else "logs/telemetry.jsonl") + + # === NEW: XSA & Profiling Suite === + xsa_enabled = bool(int(os.environ.get("XSA_ENABLED", "0"))) + xsa_last_n_layers = int(os.environ.get("XSA_LAST_N_LAYERS", "0")) + xsa_eps = float(os.environ.get("XSA_EPS", "1e-6")) + + profile_run = bool(int(os.environ.get("PROFILE_RUN", "0"))) + profile_warmup_steps = int(os.environ.get("PROFILE_WARMUP_STEPS", "5")) + profile_active_steps = int(os.environ.get("PROFILE_ACTIVE_STEPS", "10")) + profile_output_dir = os.environ.get("PROFILE_OUTPUT_DIR", "output/prof_base") + + # === NEW: MTP Control === + mtp_num_heads = int(os.environ.get("MTP_NUM_HEADS", "0")) # Set to e.g., 2 to predict t+2 and t+3 + mtp_loss_weight = float(os.environ.get("MTP_LOSS_WEIGHT", "0.2")) + mtphead_mlpmode = bool(int(os.environ.get("MTPHEAD_MLPMODE", "0"))) + + # === 新增:N-Gram / SMEAR 控制 === + ngram_vocab_size = int(os.environ.get("NGRAM_VOCAB_SIZE", "2048")) + ngram_dim = int(os.environ.get("NGRAM_DIM", "128")) + ngram_max_n = int(os.environ.get("NGRAM_MAX_N", "4")) # 2=Bigram, 3=Trigram, 4=4-gram + smear_mode = bool(int(os.environ.get("SMEAR_MODE", "0"))) + smear_window = int(os.environ.get("SMEAR_WINDOW", "4")) + + # === N-Gram fade-out schedule === + ngram_fade_enable = bool(int(os.environ.get("NGRAM_FADE_ENABLE", "0"))) + ngram_fade_start_frac = float(os.environ.get("NGRAM_FADE_START_FRAC", "0.15")) + ngram_fade_end_frac = float(os.environ.get("NGRAM_FADE_END_FRAC", "0.45")) + ngram_fade_min_scale = float(os.environ.get("NGRAM_FADE_MIN_SCALE", "0.0")) + + # === 新增: 架构微调标志 === + rope_dims = int(os.environ.get("ROPE_DIMS", "-1")) # -1 表示由 head_dim 决定 + learnable_rope = bool(int(os.environ.get("LEARNABLE_ROPE", "0"))) + ln_scale = bool(int(os.environ.get("LN_SCALE", "1"))) + learnable_ln_scale = bool(int(os.environ.get("LEARNABLE_LN_SCALE", "0"))) + scaledlm_head = bool(int(os.environ.get("SCALEDLM_HEAD", "1"))) + bifpn_mode = bool(int(os.environ.get("BIFPN_MODE", "0"))) + affine_norm = bool(int(os.environ.get("AFFINE_NORM", "0"))) + smear_gate = bool(int(os.environ.get("SMEAR_GATE", "0"))) + late_qat_ratio = float(os.environ.get("LATE_QAT_RATIO", "0.15")) + stop_mode = os.environ.get("STOP_MODE", "walltime") # walltime | steps + max_train_steps = int(os.environ.get("MAX_TRAIN_STEPS", "0")) + + # === Structured sparse + group-wise signed BiFPN === + bifpn2_mode = bool(int(os.environ.get("BIFPN2_MODE", "0"))) + bifpn_group_count = int(os.environ.get("BIFPN_GROUP_COUNT", "8")) + bifpn_band_width = int(os.environ.get("BIFPN_BAND_WIDTH", "1")) # 0=only symmetric, 1=neighbor band + bifpn_norm_eps = float(os.environ.get("BIFPN_NORM_EPS", "1e-4")) + bifpn_init_main = float(os.environ.get("BIFPN_INIT_MAIN", "1.0")) + bifpn_init_neighbor = float(os.environ.get("BIFPN_INIT_NEIGHBOR", "0.15")) + bifpn_init_far = float(os.environ.get("BIFPN_INIT_FAR", "0.0")) + + # === Value path / memory path research flags === + v_skip_enabled = bool(int(os.environ.get("V_SKIP_ENABLED", "0"))) + v_skip_last_n_layers = int(os.environ.get("V_SKIP_LAST_N_LAYERS", "0")) + v_skip_mode = os.environ.get("V_SKIP_MODE", "scalar") # scalar|group + v_skip_group_count = int(os.environ.get("V_SKIP_GROUP_COUNT", "8")) + + cross_layer_v_enabled = bool(int(os.environ.get("CROSS_LAYER_V_ENABLED", "0"))) + cross_layer_v_last_n_layers = int(os.environ.get("CROSS_LAYER_V_LAST_N_LAYERS", "0")) + cross_layer_v_mode = os.environ.get("CROSS_LAYER_V_MODE", "residual") # residual|blend + cross_layer_v_group_count = int(os.environ.get("CROSS_LAYER_V_GROUP_COUNT", "8")) + + cross_layer_kv_sharing_enabled = bool(int(os.environ.get("CROSS_LAYER_KV_SHARING_ENABLED", "0"))) + cross_layer_kv_last_n_layers = int(os.environ.get("CROSS_LAYER_KV_LAST_N_LAYERS", "0")) + cross_layer_kv_share_k = bool(int(os.environ.get("CROSS_LAYER_KV_SHARE_K", "1"))) + cross_layer_kv_share_v = bool(int(os.environ.get("CROSS_LAYER_KV_SHARE_V", "1"))) + cross_layer_kv_pairwise = bool(int(os.environ.get("CROSS_LAYER_KV_PAIRWISE", "0"))) + cross_layer_kv_partial_head = bool(int(os.environ.get("CROSS_LAYER_KV_PARTIAL_HEAD", "0"))) + cross_layer_kv_partial_head_count = int(os.environ.get("CROSS_LAYER_KV_PARTIAL_HEAD_COUNT", "2")) + + +@torch.compile(dynamic=False, fullgraph=True) +def update_ema_fused(ema_tensors: list[Tensor], model_tensors: list[Tensor], decay: float): + for e, m in zip(ema_tensors, model_tensors): + e.mul_(decay).add_(m.float(), alpha=1.0 - decay) +# ----------------------------- +# MUON OPTIMIZER +# ----------------------------- +# +# As borrowed from modded-nanogpt +# Background on Muon: https://kellerjordan.github.io/posts/muon/ + +def zeropower_via_newtonschulz5(G: Tensor, steps: int = 10, eps: float = 1e-7) -> Tensor: + # Orthogonalize a 2D update matrix with a fast Newton-Schulz iteration. + # Muon uses this to normalize matrix-shaped gradients before applying them. + a, b, c = (3.4445, -4.7750, 2.0315) + X = G.bfloat16() + X /= X.norm() + eps + 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): + super().__init__( + params, + dict(lr=lr, momentum=momentum, backend_steps=backend_steps, nesterov=nesterov), + ) + + @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"] + + 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) + # Scale correction from Muon reference implementations. + g *= max(1, g.size(0) / g.size(1)) ** 0.5 + updates_flat[curr : curr + p.numel()] = g.reshape(-1) + curr += p.numel() + + if distributed: + dist.all_reduce(updates_flat, op=dist.ReduceOp.SUM) + + curr = 0 + for p in params: + g = updates_flat[curr : curr + p.numel()].view_as(p).to(dtype=p.dtype) + p.add_(g, alpha=-lr) + curr += p.numel() + + return loss + + +# ----------------------------- +# TOKENIZER-AGNOSTIC EVALUATION SETUP +# ----------------------------- +# +# It's common for small models have a large fraction of their parameters be embeddings, since the 2 * d_model * d_vocab vectors can be gigantic. +# Instead of locking the tokenizer, we let you bring your own and calculate our validation metrics on the average compression of the validation set. +# We calculate BPB (bits-per-byte) instead of validation loss, so we need methods to count the number of bits per token in the tokenizer. +# Note: Submissions that edit the tokenizer will be examined more carefully, since screwing this up might unjustly improve your score. + +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("▁"): + 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}") + # The export pipeline writes the fixed first-50k-doc validation set to fineweb_val_*. + tokens = torch.cat([load_data_shard(file) for file in files]).contiguous() + usable = ((tokens.numel() - 1) // seq_len) * seq_len + if usable <= 0: + raise ValueError(f"Validation split is too short for TRAIN_SEQ_LEN={seq_len}") + return tokens[: usable + 1] + + +def tokens_to_bytes_count( + xb: Tensor, + yb: Tensor, + base_bytes_lut: Tensor, + has_leading_space_lut: Tensor, + is_boundary_token_lut: Tensor, +) -> Tensor: + prev_ids = xb.reshape(-1) + tgt_ids = yb.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) + return token_bytes.sum() + + +@torch.no_grad() +def eval_val_sliding( + 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]: + """ + Sliding-window validation. + + Each scored token is counted exactly once. + For each overlapping window, only the rightmost `eval_stride` target tokens + are scored, so tokens are evaluated with near-max left context. + """ + + model.eval() + + seq_len = args.train_seq_len + stride = args.eval_stride + batch_seqs = args.eval_batch_seqs + + if stride <= 0: + raise ValueError(f"EVAL_STRIDE must be > 0, got {stride}") + if stride > seq_len: + raise ValueError( + f"EVAL_STRIDE must be <= TRAIN_SEQ_LEN, got stride={stride}, seq_len={seq_len}" + ) + if batch_seqs <= 0: + raise ValueError(f"EVAL_BATCH_SEQS must be > 0, got {batch_seqs}") + + # val_tokens has shape [N+1], where x = tokens[:-1], y = tokens[1:] + total_loss_sum = torch.zeros(1, device=device, dtype=torch.float64) + total_token_count = torch.zeros(1, device=device, dtype=torch.float64) + total_byte_count = torch.zeros(1, device=device, dtype=torch.float64) + + max_start = val_tokens.numel() - 1 - seq_len + if max_start < 0: + raise ValueError(f"Validation split is too short for TRAIN_SEQ_LEN={seq_len}") + + starts = list(range(0, max_start + 1, stride)) + if starts[-1] != max_start: + starts.append(max_start) + + # shard starts across ranks + starts = starts[rank::world_size] + + def _score_batch(xb: Tensor, yb: Tensor) -> tuple[Tensor, Tensor, Tensor]: + # xb: [B, T], yb: [B, T] + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + _, loss_tokens = model(xb, yb, reduction="none") + # loss_tokens expected shape [B, T] + score_loss = loss_tokens[:, -stride:] # only score rightmost stride positions + + # token count + token_count = torch.tensor(score_loss.numel(), device=device, dtype=torch.float64) + + # byte count + scored_y = yb[:, -stride:] + scored_x = xb[:, -stride:] # Required to find inter-token boundaries + byte_count = tokens_to_bytes_count( + scored_x, + scored_y, + base_bytes_lut=base_bytes_lut, + has_leading_space_lut=has_leading_space_lut, + is_boundary_token_lut=is_boundary_token_lut, + ).to(torch.float64) + + loss_sum = score_loss.sum(dtype=torch.float64) + return loss_sum, token_count, byte_count + + batch_x = [] + batch_y = [] + + for start in starts: + chunk = val_tokens[start : start + seq_len + 1].to(device=device, dtype=torch.int64, non_blocking=True) + x = chunk[:-1] + y = chunk[1:] + + batch_x.append(x) + batch_y.append(y) + + if len(batch_x) == batch_seqs: + xb = torch.stack(batch_x) + yb = torch.stack(batch_y) + + loss_sum, token_count, byte_count = _score_batch(xb, yb) + total_loss_sum += loss_sum + total_token_count += token_count + total_byte_count += byte_count + + batch_x.clear() + batch_y.clear() + + if batch_x: + xb = torch.stack(batch_x) + yb = torch.stack(batch_y) + + loss_sum, token_count, byte_count = _score_batch(xb, yb) + total_loss_sum += loss_sum + total_token_count += token_count + total_byte_count += byte_count + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(total_loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(total_token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(total_byte_count, op=dist.ReduceOp.SUM) + + val_loss = (total_loss_sum / total_token_count).item() + val_bpb = (total_loss_sum / (math.log(2.0) * total_byte_count)).item() + + model.train() + return val_loss, val_bpb + + +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]: + # Validation computes two metrics: + # - val_loss: token cross-entropy (natural log) + # - val_bpb: tokenizer-agnostic compression metric used by the challenge + local_batch_tokens = args.val_batch_size // (world_size * grad_accum_steps) + if local_batch_tokens < args.train_seq_len: + raise ValueError( + "VAL_BATCH_SIZE must provide at least one sequence per rank; " + f"got VAL_BATCH_SIZE={args.val_batch_size}, WORLD_SIZE={world_size}, " + f"GRAD_ACCUM_STEPS={grad_accum_steps}, TRAIN_SEQ_LEN={args.train_seq_len}" + ) + local_batch_seqs = local_batch_tokens // args.train_seq_len + total_seqs = (val_tokens.numel() - 1) // args.train_seq_len + seq_start = (total_seqs * rank) // world_size + seq_end = (total_seqs * (rank + 1)) // world_size + 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) + +# ----------------------------- +# POST-TRAINING QUANTIZATION +# ----------------------------- +# +# It's silly to export our model, which is trained in bf16 and fp32, at that same precision. +# Instead, we get approximately the same model (with a small hit) by quantizing the model to int8 & zlib compressing. +# We can then decompress the model and run in higher precision for evaluation, after closing in under the size limit. + +CONTROL_TENSOR_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "CONTROL_TENSOR_NAME_PATTERNS", + "attn_scale,attn_scales,mlp_scale,mlp_scales,resid_mix,resid_mixes,q_gain,skip_weight,skip_weights", + ).split(",") + if pattern +) +INT8_KEEP_FLOAT_FP32_NAME_PATTERNS = tuple( + pattern + for pattern in os.environ.get( + "INT8_KEEP_FLOAT_FP32_NAME_PATTERNS", + ",".join(CONTROL_TENSOR_NAME_PATTERNS), + ).split(",") + if pattern +) +INT8_KEEP_FLOAT_MAX_NUMEL = 65_536 +INT8_KEEP_FLOAT_STORE_DTYPE = torch.float16 +INT8_PER_ROW_SCALE_DTYPE = torch.float16 +INT8_CLIP_PERCENTILE = 99.99984 +INT8_CLIP_Q = INT8_CLIP_PERCENTILE / 100.0 + +def tensor_nbytes(t: Tensor) -> int: + return int(t.numel()) * int(t.element_size()) + +def keep_float_tensor(name: str, t: Tensor, passthrough_orig_dtypes: dict[str, str]) -> Tensor: + if any(pattern in name for pattern in INT8_KEEP_FLOAT_FP32_NAME_PATTERNS): + return t.float().contiguous() + if t.dtype in {torch.float32, torch.bfloat16}: + passthrough_orig_dtypes[name] = str(t.dtype).removeprefix("torch.") + return t.to(dtype=INT8_KEEP_FLOAT_STORE_DTYPE).contiguous() + return t + +# def quantize_float_tensor(t: Tensor) -> tuple[Tensor, Tensor]: +# t32 = t.float() +# if t32.ndim == 2: +# # Matrices get one scale per row, which usually tracks output-channel +# # ranges much better than a single tensor-wide scale. +# clip_abs = ( +# torch.quantile(t32.abs(), INT8_CLIP_Q, dim=1) +# if t32.numel() +# else torch.empty((t32.shape[0],), dtype=torch.float32) +# ) +# clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) +# scale = (clip_abs / 127.0).clamp_min(1.0 / 127.0) +# q = torch.clamp(torch.round(clipped / scale[:, None]), -127, 127).to(torch.int8).contiguous() +# return q, scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous() + +# # Vectors / scalars use a simpler per-tensor scale. +# clip_abs = float(torch.quantile(t32.abs().flatten(), INT8_CLIP_Q).item()) if t32.numel() else 0.0 +# scale = torch.tensor(clip_abs / 127.0 if clip_abs > 0 else 1.0, dtype=torch.float32) +# q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -127, 127).to(torch.int8).contiguous() +# return q, scale + +# ========================================================================= +# 原始常量保持不变,作为默认降级选项 +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 + +# === 新增:支持从环境变量读取动态扫描的百分位列表 === +# 比如: "100.0,99.9999,99.9995,99.999,99.99" +DYNAMIC_CLIP_Q_LIST = [ + float(p) / 100.0 + for p in os.environ.get("DYNAMIC_CLIP_PERCENTILES", "100.0").split(",") + if p.strip() +] +# ========================================================================= + +def quantize_float_tensor(t: Tensor) -> tuple[Tensor, Tensor]: + t32 = t.float() + + if t32.ndim == 2: + # === 动态百分位扫描逻辑 (GPTQ-lite 风格) === + best_q = None + best_scale = None + best_mse = float('inf') + + # 遍历所有候选的截断百分比 + for q_percentile in DYNAMIC_CLIP_Q_LIST: + if q_percentile >= 1.0: + # 100% 表示不截断,直接取绝对值最大值 + clip_abs = t32.abs().max(dim=1).values + else: + clip_abs = ( + torch.quantile(t32.abs(), q_percentile, dim=1) + if t32.numel() + else torch.empty((t32.shape[0],), dtype=torch.float32, device=t32.device) + ) + + clipped = torch.maximum(torch.minimum(t32, clip_abs[:, None]), -clip_abs[:, None]) + scale = (clip_abs / 127.0).clamp_min(1.0 / 127.0) + q = torch.clamp(torch.round(clipped / scale[:, None]), -127, 127).to(torch.int8) + + # 反量化并计算 MSE 损失 + dequantized = q.float() * scale[:, None] + mse = torch.nn.functional.mse_loss(dequantized, t32).item() + + # 如果是列表里的第一个(默认),或者发现了更小的 MSE + if best_q is None or mse < best_mse: + best_mse = mse + best_q = q + best_scale = scale + + # 必须加 contiguous 保证序列化正常 + return best_q.contiguous(), best_scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous() + + # Vectors / scalars 维度较小,不值得做昂贵的扫描,保留原逻辑 + clip_abs = float(torch.quantile(t32.abs().flatten(), INT8_CLIP_Q).item()) if t32.numel() else 0.0 + scale = torch.tensor(clip_abs / 127.0 if clip_abs > 0 else 1.0, dtype=torch.float32) + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -127, 127).to(torch.int8).contiguous() + return q, scale + +def quantize_state_dict_int8(state_dict: dict[str, Tensor]): + # Single supported clean-script export format: + # - per-row int8 for 2D float tensors + # - per-tensor int8 for other float tensors + # - exact passthrough for non-floats + # - passthrough for small float tensors, stored as fp16 to save bytes + quantized: dict[str, Tensor] = {} + scales: dict[str, Tensor] = {} + dtypes: dict[str, str] = {} + 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 + + # Small float tensors are cheap enough to keep directly. We still downcast + # fp32/bf16 passthrough tensors to fp16 so metadata does not dominate size. + if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL: + kept = keep_float_tensor(name, t, passthrough_orig_dtypes) + passthrough[name] = kept + stats["int8_payload_bytes"] += tensor_nbytes(kept) + continue + + stats["num_float_tensors"] += 1 + q, s = quantize_float_tensor(t) + if s.ndim > 0: + qmeta[name] = {"scheme": "per_row", "axis": 0} + quantized[name] = q + scales[name] = s + dtypes[name] = str(t.dtype).removeprefix("torch.") + stats["int8_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s) + + obj: dict[str, object] = { + "__quant_format__": "int8_clean_per_row_v1", + "quantized": quantized, + "scales": scales, + "dtypes": dtypes, + "passthrough": passthrough, + } + if qmeta: + obj["qmeta"] = qmeta + if passthrough_orig_dtypes: + obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes + return obj, stats + +def dequantize_state_dict_int8(obj: dict[str, object]) -> dict[str, Tensor]: + out: dict[str, Tensor] = {} + qmeta = obj.get("qmeta", {}) + passthrough_orig_dtypes = obj.get("passthrough_orig_dtypes", {}) + for name, q in obj["quantized"].items(): + dtype = getattr(torch, obj["dtypes"][name]) + s = obj["scales"][name] + if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0: + s = s.to(dtype=torch.float32) + # Broadcast the saved row scale back across trailing dimensions. + out[name] = (q.float() * s.view(q.shape[0], *([1] * (q.ndim - 1)))).to(dtype=dtype).contiguous() + else: + scale = float(s.item()) + out[name] = (q.float() * scale).to(dtype=dtype).contiguous() + for name, t in obj["passthrough"].items(): + # Restore small tensors, undoing the temporary fp16 storage cast if needed. + out_t = t.detach().to("cpu").contiguous() + orig_dtype = passthrough_orig_dtypes.get(name) + if isinstance(orig_dtype, str): + 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: + # Each call consumes a contiguous chunk from the shared token stream, then slices out + # one disjoint span per rank. The extra "+1" token lets us build (x, y) by shifting. + def __init__(self, pattern: str, rank: int, world_size: int, device: torch.device): + self.rank = rank + self.world_size = world_size + 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 +# ----------------------------- + +def _expand_group_gates(g: Tensor, total_dim: int) -> Tensor: + """ + g: [G] + return: [D], where D % G == 0 + """ + if total_dim % g.numel() != 0: + raise ValueError(f"total_dim ({total_dim}) must be divisible by num_groups ({g.numel()})") + group_dim = total_dim // g.numel() + return g.repeat_interleave(group_dim) + + +def apply_v_skip( + y: Tensor, + v: Tensor, + gate: Tensor, + mode: str = "scalar", + num_heads: int | None = None, + num_kv_heads: int | None = None, +) -> Tensor: + """ + y: [B, H, T, D] + v: [B, Hkv, T, D] + gate: + scalar mode: shape [1] or [] + group mode: shape [G] + Returns: + y + gated(V path) with GQA-aware expansion + """ + b, h, t, d = y.shape + hkv = v.shape[1] + + if h == hkv: + v_exp = v + else: + if num_heads is None or num_kv_heads is None: + raise ValueError("num_heads and num_kv_heads required for GQA") + if num_heads % num_kv_heads != 0: + raise ValueError("num_heads must be divisible by num_kv_heads") + group_size = num_heads // num_kv_heads + v_exp = v.unsqueeze(2).expand(b, hkv, group_size, t, d).reshape(b, h, t, d) + + if mode == "scalar": + g = torch.sigmoid(gate.to(dtype=y.dtype)).reshape(1, 1, 1, 1) + return y + g * v_exp + elif mode == "group": + g = torch.sigmoid(gate.to(dtype=y.dtype)) + g = _expand_group_gates(g, d).view(1, 1, 1, d) + return y + g * v_exp + else: + raise ValueError(f"Unknown V_SKIP_MODE: {mode}") + + +def mix_cross_layer_v( + v_cur: Tensor, + v_prev: Tensor, + gate: Tensor, + mode: str = "residual", + group_mode: str = "scalar", +) -> Tensor: + """ + v_cur, v_prev: [B, Hkv, T, D] + mode: + residual: v_cur + g * v_prev + blend: (1-g) * v_cur + g * v_prev + group_mode: + scalar or group + """ + d = v_cur.shape[-1] + + if group_mode == "scalar": + g = torch.sigmoid(gate.to(dtype=v_cur.dtype)).reshape(1, 1, 1, 1) + elif group_mode == "group": + g = torch.sigmoid(gate.to(dtype=v_cur.dtype)) + g = _expand_group_gates(g, d).view(1, 1, 1, d) + else: + raise ValueError(f"Unknown cross-layer V group mode: {group_mode}") + + if mode == "residual": + return v_cur + g * v_prev + elif mode == "blend": + return (1.0 - g) * v_cur + g * v_prev + else: + raise ValueError(f"Unknown CROSS_LAYER_V_MODE: {mode}") + + +def apply_partial_head_sharing( + cur: Tensor, + shared: Tensor, + share_head_count: int, +) -> Tensor: + """ + cur, shared: [B, H, T, D] or [B, Hkv, T, D] + Replace the first N heads with shared heads. + """ + h = cur.shape[1] + n = min(max(share_head_count, 0), h) + if n == 0: + return cur + out = cur.clone() + out[:, :n] = shared[:, :n] + return out + +class RMSNorm(nn.Module): + def __init__(self, dim: int | None = None, eps: float | None = None, affine: bool = False): + super().__init__() + self.eps = eps + self.weight = nn.Parameter(torch.ones(dim)) if (affine and dim is not None) else None + + def forward(self, x: Tensor) -> Tensor: + w = self.weight.to(x.dtype) if self.weight is not None else None + return F.rms_norm(x, (x.size(-1),), weight=w, eps=self.eps) + + +class CastedLinear(nn.Linear): + def __init__(self, in_features: int, out_features: int, bias: bool = False): + super().__init__(in_features, out_features, bias=bias) + # Branchless QAT gate: 0.0 = no-op, 1.0 = STE fake-quantize. + # A float buffer avoids any Python branch, so torch.compile(fullgraph=True) + # always traces the same single graph regardless of QAT phase. + # To activate late QAT: call module.qat_alpha.fill_(1.0) — no recompile needed. + self.register_buffer("qat_alpha", torch.tensor(0.0, dtype=torch.float32), persistent=False) + + def forward(self, x: torch.Tensor) -> torch.Tensor: + w = self.weight + # STE: forward uses w + alpha*(w_quant - w), backward flows through w. + # When qat_alpha=0 the delta is zeroed out entirely (pure fp pass-through). + w_max = w.detach().abs().amax(dim=1, keepdim=True) + scale = (w_max / 127.0).clamp_min(1e-7) + w_quant = torch.clamp(torch.round(w / scale), -127, 127) * scale + w = w + (self.qat_alpha * (w_quant - w)).detach() + return F.linear(x, w.to(x.dtype), self.bias.to(x.dtype) if self.bias is not None else None) + +def restore_low_dim_params_to_fp32(module: nn.Module) -> None: + # Keep small/control parameters in fp32 even when the model body runs in bf16. + with torch.no_grad(): + for name, param in module.named_parameters(): + if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32: + param.data = param.data.float() + + +class Rotary(nn.Module): + # Caches cos/sin tables per sequence length on the current device. + 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) +def apply_rotary_emb(x: Tensor, cos: Tensor, sin: Tensor, rope_dims: int = 0) -> Tensor: + """ + 极速版 Partial RoPE:将多重拼接合并为单次拼接。 + """ + if rope_dims > 0 and rope_dims < x.size(-1): + half = rope_dims // 2 + # 切片全是视图 (View),零内存开销 + x1 = x[..., :half] + x2 = x[..., half:rope_dims] + x_pass = x[..., rope_dims:] + + cos_part = cos[..., :half] + sin_part = sin[..., :half] + + # 将两次 cat 合并为一次,Inductor 能将其编译为单个极速的 Triton Kernel + return torch.cat(( + x1 * cos_part + x2 * sin_part, + x1 * (-sin_part) + x2 * cos_part, + x_pass + ), dim=-1) + + # 全维度旋转 + half = x.size(-1) // 2 + x1, x2 = x[..., :half], x[..., half:] + return torch.cat((x1 * cos + x2 * sin, x1 * (-sin) + x2 * cos), dim=-1) + +def apply_xsa_gqa_efficient( + y: Tensor, + v: Tensor, + num_heads: int, + num_kv_heads: int, + eps: float = 1e-6, +) -> Tensor: + """ + Efficient XSA postprocess for GQA/MHA. + + Args: + y: attention output before final proj, shape [B, H, T, D] + v: value tensor before GQA expansion, shape [B, Hkv, T, D] + num_heads: query heads + num_kv_heads: kv heads + eps: numerical epsilon + + Returns: + y_xsa: same shape as y, with self-value-direction component removed + + Notes: + - Keeps standard attention path intact. + - Avoids repeat_interleave for GQA. + - Works for MHA too when num_heads == num_kv_heads. + """ + if num_heads == num_kv_heads: + # Standard MHA case + # Normalize self-value direction + vn = v / (v.norm(dim=-1, keepdim=True) + eps) # [B, H, T, D] + proj = (y * vn).sum(dim=-1, keepdim=True) # [B, H, T, 1] + return y - proj * vn + + # GQA case + if num_heads % num_kv_heads != 0: + raise ValueError( + f"num_heads ({num_heads}) must be divisible by num_kv_heads ({num_kv_heads})" + ) + + group_size = num_heads // num_kv_heads + + # y: [B, H, T, D] -> [B, Hkv, group, T, D] + b, h, t, d = y.shape + yg = y.view(b, num_kv_heads, group_size, t, d) + + # v: [B, Hkv, T, D] -> normalize -> [B, Hkv, 1, T, D] + vn = v / (v.norm(dim=-1, keepdim=True) + eps) + vn = vn.unsqueeze(2) + + # Remove projection onto normalized self-value direction + proj = (yg * vn).sum(dim=-1, keepdim=True) # [B, Hkv, group, T, 1] + yg = yg - proj * vn + + return yg.view(b, h, t, d) + +class StructuredGroupSignedBiFPN(nn.Module): + """ + Structured sparse + group-wise signed fusion. + + Features: + - structured sparse connectivity via band mask + - group-wise signed weights instead of single scalar + - normalized by sum of absolute weights per decoder/group + + Shape convention: + skips: list of encoder features, each [B, T, D] + output for one decoder layer: [B, T, D] + """ + def __init__( + self, + num_decoder_layers: int, + num_encoder_layers: int, + model_dim: int, + group_count: int = 8, + band_width: int = 1, + norm_eps: float = 1e-4, + init_main: float = 1.0, + init_neighbor: float = 0.15, + init_far: float = 0.0, + ): + super().__init__() + if model_dim % group_count != 0: + raise ValueError( + f"model_dim ({model_dim}) must be divisible by group_count ({group_count})" + ) + + self.num_decoder_layers = num_decoder_layers + self.num_encoder_layers = num_encoder_layers + self.model_dim = model_dim + self.group_count = group_count + self.group_dim = model_dim // group_count + self.band_width = band_width + self.norm_eps = norm_eps + + # Signed weights per decoder, encoder, group + # shape: [Dec, Enc, G] + w = torch.full( + (num_decoder_layers, num_encoder_layers, group_count), + init_far, + dtype=torch.float32, + ) + + # Structured sparse prior: + # main symmetric connection + neighbor band + for d in range(num_decoder_layers): + sym = num_encoder_layers - 1 - d + for e in range(num_encoder_layers): + dist = abs(e - sym) + if dist == 0: + w[d, e, :] = init_main + elif dist <= band_width: + w[d, e, :] = init_neighbor + + # Keep a binary mask for allowed connections + mask = torch.zeros( + (num_decoder_layers, num_encoder_layers, 1), + dtype=torch.float32, + ) + for d in range(num_decoder_layers): + sym = num_encoder_layers - 1 - d + for e in range(num_encoder_layers): + if abs(e - sym) <= band_width: + mask[d, e, 0] = 1.0 + + self.weights = nn.Parameter(w) + self.register_buffer("mask", mask, persistent=True) + + def forward(self, skips: list[Tensor], decoder_idx: int, x_dtype: torch.dtype) -> Tensor: + """ + skips: list of encoder outputs, len = Enc, each [B,T,D] + decoder_idx: which decoder layer is requesting fusion + returns fused skip feature [B,T,D] + """ + if len(skips) != self.num_encoder_layers: + raise ValueError( + f"Expected {self.num_encoder_layers} skips, got {len(skips)}" + ) + + # [Enc, B, T, D] + stacked = torch.stack(skips, dim=0) + + # reshape feature dim into groups: [Enc, B, T, G, Gd] + enc, b, t, d = stacked.shape + stacked_g = stacked.view(enc, b, t, self.group_count, self.group_dim) + + # signed group-wise weights for this decoder: [Enc, G] + w = self.weights[decoder_idx] * self.mask[decoder_idx] # [Enc, G] + w = w.to(dtype=x_dtype) + + # Normalize by sum of abs weights per group + denom = w.abs().sum(dim=0, keepdim=True).clamp_min(self.norm_eps) # [1, G] + w_norm = w / denom # [Enc, G] + + # weighted sum: [Enc,G] x [Enc,B,T,G,Gd] -> [B,T,G,Gd] + fused = torch.einsum("eg,ebtgd->btgd", w_norm, stacked_g) + + # back to [B,T,D] + fused = fused.reshape(b, t, d) + return fused + + @torch.no_grad() + def export_effective_matrix(self) -> Tensor: + """ + Returns decoder-encoder scalar summary matrix [Dec, Enc] + by averaging group-wise signed weights after normalization. + Useful for visualization. + """ + w = self.weights * self.mask + denom = w.abs().sum(dim=1, keepdim=True).clamp_min(self.norm_eps) # [Dec,1,G] + w_norm = w / denom + return w_norm.mean(dim=-1) # [Dec, Enc] + + +class CausalSelfAttention(nn.Module): + def __init__( + self, + args, + layer_idx: int, + xsa_enabled: bool = False, + xsa_eps: float = 1e-6, + ): + super().__init__() + + dim = args.model_dim + num_heads = args.num_heads + num_kv_heads = args.num_kv_heads + rope_base = args.rope_base + qk_gain_init = args.qk_gain_init + + if dim % num_heads != 0: + raise ValueError("model_dim must be divisible by num_heads") + if num_heads % num_kv_heads != 0: + raise ValueError("num_heads must be divisible by num_kv_heads") + + self.layer_idx = layer_idx + self.num_heads = num_heads + self.num_kv_heads = num_kv_heads + self.head_dim = dim // num_heads + if self.head_dim % 2 != 0: + raise ValueError("head_dim must be even for RoPE") + + self.rope_dims = args.rope_dims if args.rope_dims > 0 else self.head_dim + self.xsa_enabled = xsa_enabled + self.xsa_eps = xsa_eps + + kv_dim = self.num_kv_heads * self.head_dim + self.c_q = CastedLinear(dim, dim, bias=False) + self.c_k = CastedLinear(dim, kv_dim, bias=False) + self.c_v = CastedLinear(dim, kv_dim, bias=False) + self.proj = CastedLinear(dim, dim, bias=False) + self.proj._zero_init = True + self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) + self.rotary = Rotary(self.head_dim, base=rope_base) + + self.learnable_rope = args.learnable_rope + if self.learnable_rope: + init_logits = torch.full((self.head_dim // 2,), -4.0, dtype=torch.float32) + init_logits[:8] = 4.0 + self.rope_mix_logits = nn.Parameter(init_logits) + + # ----------------------------- + # Value path research flags + # ----------------------------- + self.v_skip_enabled = args.v_skip_enabled and (layer_idx >= args.num_layers - args.v_skip_last_n_layers) + self.v_skip_mode = args.v_skip_mode + self.v_skip_group_count = args.v_skip_group_count + + if self.v_skip_enabled: + if self.v_skip_mode == "scalar": + self.v_skip_gate = nn.Parameter(torch.zeros(1, dtype=torch.float32)) + elif self.v_skip_mode == "group": + self.v_skip_gate = nn.Parameter(torch.zeros(self.v_skip_group_count, dtype=torch.float32)) + else: + raise ValueError(f"Unknown V_SKIP_MODE: {self.v_skip_mode}") + + self.cross_layer_v_enabled = ( + args.cross_layer_v_enabled and + (layer_idx >= args.num_layers - args.cross_layer_v_last_n_layers) + ) + self.cross_layer_v_mode = args.cross_layer_v_mode + self.cross_layer_v_group_count = args.cross_layer_v_group_count + + if self.cross_layer_v_enabled: + if args.cross_layer_v_group_count <= 1: + self.cross_layer_v_gate = nn.Parameter(torch.zeros(1, dtype=torch.float32)) + self.cross_layer_v_gate_mode = "scalar" + else: + self.cross_layer_v_gate = nn.Parameter(torch.zeros(args.cross_layer_v_group_count, dtype=torch.float32)) + self.cross_layer_v_gate_mode = "group" + + # ----------------------------- + # Memory path research flags + # ----------------------------- + self.cross_layer_kv_sharing_enabled = ( + args.cross_layer_kv_sharing_enabled and + (layer_idx >= args.num_layers - args.cross_layer_kv_last_n_layers) + ) + self.cross_layer_kv_share_k = args.cross_layer_kv_share_k + self.cross_layer_kv_share_v = args.cross_layer_kv_share_v + self.cross_layer_kv_pairwise = args.cross_layer_kv_pairwise + self.cross_layer_kv_partial_head = args.cross_layer_kv_partial_head + self.cross_layer_kv_partial_head_count = args.cross_layer_kv_partial_head_count + + + def forward( + self, + x: Tensor, + shared_k: Tensor | None = None, + shared_v: Tensor | None = None, + prev_v: Tensor | None = None, + ) -> tuple[Tensor, Tensor, Tensor]: + """ + Returns: + out: [B,T,D] + k_eff: [B,Hkv,T,D] + v_eff: [B,Hkv,T,D] + """ + bsz, seqlen, dim = x.shape + + q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2) + k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + v = self.c_v(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + + 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.learnable_rope: + q_rotated = apply_rotary_emb(q, cos, sin, rope_dims=0) + k_rotated = apply_rotary_emb(k, cos, sin, rope_dims=0) + gamma = torch.sigmoid(self.rope_mix_logits.to(q.dtype)) + gamma = gamma.unsqueeze(-1).expand(-1, 2).reshape(-1) + q = gamma * q_rotated + (1 - gamma) * q + k = gamma * k_rotated + (1 - gamma) * k + else: + q = apply_rotary_emb(q, cos, sin, self.rope_dims) + k = apply_rotary_emb(k, cos, sin, self.rope_dims) + + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + + # ---------------------------------------------------- + # Memory path: cross-layer KV sharing + # ---------------------------------------------------- + k_eff = k + v_eff = v + + if self.cross_layer_kv_sharing_enabled: + if self.cross_layer_kv_share_k and shared_k is not None: + if self.cross_layer_kv_partial_head: + k_eff = apply_partial_head_sharing(k_eff, shared_k, self.cross_layer_kv_partial_head_count) + else: + k_eff = shared_k + + if self.cross_layer_kv_share_v and shared_v is not None: + if self.cross_layer_kv_partial_head: + v_eff = apply_partial_head_sharing(v_eff, shared_v, self.cross_layer_kv_partial_head_count) + else: + v_eff = shared_v + + # ---------------------------------------------------- + # Value path: cross-layer V sharing + # ---------------------------------------------------- + if self.cross_layer_v_enabled and prev_v is not None: + v_eff = mix_cross_layer_v( + v_cur=v_eff, + v_prev=prev_v, + gate=self.cross_layer_v_gate, + mode=self.cross_layer_v_mode, + group_mode=self.cross_layer_v_gate_mode, + ) + + y = F.scaled_dot_product_attention( + q, + k_eff, + v_eff, + attn_mask=None, + is_causal=True, + enable_gqa=(self.num_kv_heads != self.num_heads), + ) + + if self.xsa_enabled: + y = apply_xsa_gqa_efficient( + y=y, + v=v_eff, + num_heads=self.num_heads, + num_kv_heads=self.num_kv_heads, + eps=self.xsa_eps, + ) + + if self.v_skip_enabled: + y = apply_v_skip( + y=y, + v=v_eff, + gate=self.v_skip_gate, + mode=self.v_skip_mode, + num_heads=self.num_heads, + num_kv_heads=self.num_kv_heads, + ) + + y = y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim) + out = self.proj(y) + return out, k_eff, v_eff + + +class MLP(nn.Module): + # relu^2 MLP from the original modded-nanogpt setup + 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 = torch.relu(self.fc(x)) + return self.proj(x.square()) + + +class Block(nn.Module): + def __init__( + self, + args, + layer_idx=0, + xsa_enabled=False, + xsa_eps=1e-6 + ): + super().__init__() + dim = args.model_dim + self.layer_idx = layer_idx + + self.attn_norm = RMSNorm(dim, affine=args.affine_norm) + self.mlp_norm = RMSNorm(dim, affine=args.affine_norm) + self.attn = CausalSelfAttention( + args, + layer_idx=layer_idx, + xsa_enabled=xsa_enabled, + xsa_eps=xsa_eps, + ) + self.mlp = MLP(dim, args.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()) + + # === 新增:环境变量控制的 Layer Scale === + self.learnable_ln_scale = args.learnable_ln_scale + + # 计算初始值 (衰减系数) + #init_scale = 1.0 / math.sqrt(layer_idx + 1) if ln_scale else 1.0 + # === 利用实验得出的经验公式:让深层衰减得更快一点,压制后期的方差爆炸 === + # 原版:1.0 / math.sqrt(layer_idx + 1) + # 优化版先验: + init_scale = 1.0 / (math.sqrt(layer_idx + 1) + 0.1 * layer_idx) if args.ln_scale else 1.0 + + self.layer_scale = init_scale # 直接作为浮点数乘上去,无需学习 + + if self.learnable_ln_scale: + # 声明为 1D 参数,以便被 PyTorch 的 parameters() 追踪 + self.layer_scale = nn.Parameter(torch.tensor([init_scale], dtype=torch.float32)) + else: + # 声明为普通 Python 标量,不参与梯度更新 + self.layer_scale = init_scale + + def forward( + self, + x: Tensor, + x0: Tensor, + shared_k: Tensor | None = None, + shared_v: Tensor | None = None, + prev_v: Tensor | None = None, + ) -> tuple[Tensor, Tensor, Tensor]: + mix = self.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + + scale = self.layer_scale.to(dtype=x.dtype) if isinstance(self.layer_scale, nn.Parameter) else self.layer_scale + + attn_out, k_eff, v_eff = self.attn( + self.attn_norm(x) * scale, + shared_k=shared_k, + shared_v=shared_v, + prev_v=prev_v, + ) + 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) * scale) + return x, k_eff, v_eff + + # attn_out = self.attn(self.attn_norm(x)) + # x = x + self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out + # x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x)) + # return x + + +# === NEW: ABLATION ARCHITECTURE IN GPT CLASS === +class SmearGate(nn.Module): + def __init__(self, dim: int): + super().__init__() + self.gate = nn.Parameter(torch.zeros(dim, dtype=torch.float32)) + def forward(self, x: Tensor) -> Tensor: + g = torch.sigmoid(self.gate.to(dtype=x.dtype))[None, None, :] + x_prev = torch.cat([torch.zeros_like(x[:, :1]), x[:, :-1]], dim=1) + return (1 - g) * x + g * x_prev + +class CausalLocalMixing(nn.Module): + """ + Causal local context mixing via depthwise conv1d. + Per-channel learned softmax weights over a causal window. + Compiles to a single fused kernel instead of window_size tiny kernels. + """ + def __init__(self, dim: int, window_size: int = 4): + super().__init__() + self.window_size = window_size + self.dim = dim + # Learnable logits [window_size, dim]: position 0 = current token + w = torch.zeros(window_size, dim, dtype=torch.float32) + w[0, :] = 3.0 # Initial bias toward current token (~0.95) + self.mix_logits = nn.Parameter(w) + + def forward(self, x: Tensor) -> Tensor: + if self.window_size <= 1: + return x + # x: [B, T, D] -> conv1d expects [B, D, T] + # Build depthwise conv weights from softmax logits: [D, 1, W] + w_soft = F.softmax(self.mix_logits.to(x.dtype), dim=0) # [W, D] + # Flip so that index 0 (current token) is the rightmost tap + kernel = w_soft.flip(0).T.unsqueeze(1) # [D, 1, W] + # Left-pad for causal convolution + x_t = x.transpose(1, 2) # [B, D, T] + x_padded = F.pad(x_t, (self.window_size - 1, 0)) # [B, D, T+W-1] + out = F.conv1d(x_padded, kernel, groups=self.dim) # [B, D, T] + return out.transpose(1, 2) # [B, T, D] + +class NGramHashEmbedding(nn.Module): + def __init__(self, vocab_size: int, dim: int, model_dim: int, max_n: int = 4): + """ + max_n: 支持到几元组。比如 max_n=4 代表同时包含 Bigram(2), Trigram(3), 4-gram(4) + """ + super().__init__() + self.max_n = max_n + self.vocab_size = vocab_size + + # 为每一种 N-gram 创建一个独立的 Embedding 表 + self.embeds = nn.ModuleList([ + nn.Embedding(vocab_size, dim) for _ in range(2, max_n + 1) + ]) + for emb in self.embeds: + # nn.init.zeros_(emb.weight) + nn.init.normal_(emb.weight, std=0.01) # 给一点微小的初始特征,打破零梯度僵局 + + self.proj = nn.Linear(dim, model_dim, bias=False) if dim != model_dim else None + if self.proj is not None: + nn.init.zeros_(self.proj.weight) + + # 为每一个 N-gram 分配一个独立的可学习权重,初始给 0.05 + self.ngram_scales = nn.Parameter(torch.full((max_n - 1,), 0.05, dtype=torch.float32)) + + def ngram_hash(self, tokens: Tensor, n: int) -> Tensor: + t = tokens.to(torch.int64) # 使用 int64 防止乘法溢出 + mod = self.vocab_size - 1 + out = torch.empty_like(t) + out[..., :n-1] = mod + + primes = [36313, 27191, 19393, 13127, 9767] + hash_val = t[..., n-1:] * primes[0] + for i in range(1, n): + hash_val = torch.bitwise_xor(hash_val, t[..., n-1-i : -i] * primes[i]) + + out[..., n-1:] = hash_val % mod + return out.long() + + def forward(self, token_ids: Tensor) -> Tensor: + fused_h = None + for idx, n in enumerate(range(2, self.max_n + 1)): + hashed_ids = self.ngram_hash(token_ids, n) + h_n = self.embeds[idx](hashed_ids) + scaled_h = h_n * self.ngram_scales[idx].to(dtype=h_n.dtype) + + if fused_h is None: + fused_h = scaled_h + else: + fused_h = fused_h + scaled_h + + if self.proj is not None: + fused_h = self.proj(fused_h) + return fused_h + + +def compute_ngram_fade_scale( + step: int, + total_steps: int, + enabled: bool, + start_frac: float, + end_frac: float, + min_scale: float = 0.0, +) -> float: + """ + Piecewise linear fade-out schedule for N-Gram features. + + Before start_frac: scale = 1 + Between start_frac and end_frac: linearly decay to min_scale + After end_frac: scale = min_scale + """ + if not enabled: + return 1.0 + + if total_steps <= 0: + return 1.0 + + p = step / float(total_steps) + start_frac = max(0.0, min(1.0, start_frac)) + end_frac = max(start_frac + 1e-8, min(1.0, end_frac)) + min_scale = max(0.0, min(1.0, min_scale)) + + if p <= start_frac: + return 1.0 + if p >= end_frac: + return min_scale + + alpha = (p - start_frac) / (end_frac - start_frac) + return (1.0 - alpha) * 1.0 + alpha * min_scale + +class GPT(nn.Module): + def __init__(self, args, master_process: bool = True): + super().__init__() + self.fda_mode = args.fda_mode + self.skip_distance = 2 # Configurable skip distance for FDA mode + + model_dim = args.model_dim + num_layers = args.num_layers + self.num_layers = args.num_layers + self.cross_layer_kv_sharing_enabled = args.cross_layer_kv_sharing_enabled + self.cross_layer_kv_last_n_layers = args.cross_layer_kv_last_n_layers + self.cross_layer_kv_pairwise = args.cross_layer_kv_pairwise + + + self.ln_scale = args.ln_scale + self.scaledlm_head = args.scaledlm_head + self.mtphead_mlpmode = args.mtphead_mlpmode + + self.tie_embeddings = args.tie_embeddings + self.tied_embed_init_std = args.tied_embed_init_std + self.logit_softcap = args.logit_softcap + self.tok_emb = nn.Embedding(args.vocab_size, model_dim) + + self.smear_mode = args.smear_mode + self.smear_window = args.smear_window + if self.smear_mode: + self.local_mix = CausalLocalMixing(model_dim, window_size=self.smear_window) + if master_process: + print(f"Architecture: Local Causal Mixing (Window={self.smear_window})") + + self.smear_gate = args.smear_gate + if self.smear_gate: + self.smear_gate_module = SmearGate(model_dim) + if master_process: + print(f"Architecture: SmearGate (1-step causal blend)") + + self.ngram_max_n = args.ngram_max_n + + if args.ngram_vocab_size > 0 and self.ngram_max_n >= 2: + self.ngram = NGramHashEmbedding(args.ngram_vocab_size, args.ngram_dim, model_dim, max_n=self.ngram_max_n) + if master_process: + print(f"Architecture: Discrete N-Gram Hash (Max N={self.ngram_max_n})") + else: + self.ngram = None + + self.register_buffer("ngram_global_scale_buf", torch.tensor(1.0, dtype=torch.float32), persistent=False) + + self.blocks = nn.ModuleList( + [ + Block( + args, + layer_idx=i, + xsa_enabled=(args.xsa_enabled and i >= num_layers - args.xsa_last_n_layers), + xsa_eps=args.xsa_eps + ) + for i in range(num_layers) + ] + ) + + self.num_encoder_layers = num_layers // 2 + self.num_decoder_layers = num_layers - self.num_encoder_layers + + # --- 新增: BiFPN 多路径加法融合开关 --- + self.bifpn_mode = args.bifpn_mode + self.bifpn2_mode = args.bifpn2_mode + if self.bifpn_mode: + w = torch.full((self.num_decoder_layers, self.num_encoder_layers), 0.1, dtype=torch.float32) + # 1. 基础的对称 U-Net 连接 (对角线) + for i in range(self.num_decoder_layers): + sym_idx = self.num_encoder_layers - 1 - i + if sym_idx >= 0: + w[i, sym_idx] = 1.0 + + # === 新增:根据先验直接注入“特征重构”结构 === + if self.num_decoder_layers >= 2 and self.num_encoder_layers >= 2: + # 倒数第一层 Decoder: 极度排斥最底层的 2 个 Encoder (做高通滤波) + w[-1, 0] = -1.5 + w[-1, 1] = -1.0 + # 倒数第二层 Decoder: 极度渴求最底层的 2 个 Encoder (做基础特征组装) + w[-2, 0] = 0.8 + w[-2, 1] = 0.5 + + self.bifpn_weights = nn.Parameter(w) + if master_process: + print("Architecture: BiFPN Weighted Addition Fusion") + elif self.bifpn2_mode: + self.structured_bifpn = StructuredGroupSignedBiFPN( + num_decoder_layers=self.num_decoder_layers, + num_encoder_layers=self.num_encoder_layers, + model_dim=model_dim, + group_count=args.bifpn_group_count, + band_width=args.bifpn_band_width, + norm_eps=args.bifpn_norm_eps, + init_main=args.bifpn_init_main, + init_neighbor=args.bifpn_init_neighbor, + init_far=args.bifpn_init_far, + ) + if int(os.environ.get("RANK", "0")) == 0: + print( + f"Architecture: StructuredGroupSignedBiFPN " + f"(groups={args.bifpn_group_count}, band={args.bifpn_band_width})" + ) + + elif self.fda_mode: + num_conn = max(0, num_layers - self.skip_distance) + self.skip_weights = nn.Parameter(torch.ones(num_conn, model_dim, dtype=torch.float32)) + else: + self.num_encoder_layers = num_layers // 2 + self.num_decoder_layers = num_layers - self.num_encoder_layers + self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers) + self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32)) + + self.final_norm = RMSNorm(model_dim, affine=args.affine_norm) + self.lm_head = None if args.tie_embeddings else CastedLinear(model_dim, args.vocab_size, bias=False) + if self.lm_head is not None: + self.lm_head._zero_init = True + + self.mtp_num_heads = args.mtp_num_heads + self.mtp_loss_weight = args.mtp_loss_weight + + # === 终极修复:只有当头数 > 0 时,才去创建和挂载参数! === + if self.mtp_num_heads > 0: + if self.mtphead_mlpmode: + self.mtp_heads = nn.ModuleList([ + nn.Sequential( + nn.Linear(model_dim, model_dim * 2, bias=False), + nn.GELU(), + nn.Linear(model_dim * 2, args.vocab_size, bias=False) + ) for _ in range(self.mtp_num_heads) + ]) + else: + self.mtp_heads = nn.ModuleList( + [CastedLinear(model_dim, args.vocab_size, bias=False) for _ in range(self.mtp_num_heads)] + ) + + for head in self.mtp_heads: + if isinstance(head, nn.Sequential): + head[2]._zero_init = True + else: + head._zero_init = True + else: + # 如果配置为 0,显式设为 None,确保参数量干干净净! + self.mtp_heads = nn.ModuleList([]) + + self.max_logit_pre_cap = 0.0 # Telemetry tracking + self._init_weights() + + def _init_weights(self) -> None: + if self.tie_embeddings: + nn.init.normal_(self.tok_emb.weight, mean=0.0, std=self.tied_embed_init_std) + for module in self.modules(): + if isinstance(module, nn.Linear) and getattr(module, "_zero_init", False): + nn.init.zeros_(module.weight) + + def _should_share_from_prev_layer(self, block_idx: int) -> bool: + if not getattr(self, "cross_layer_kv_sharing_enabled", False): + return False + return block_idx >= self.num_layers - self.cross_layer_kv_last_n_layers + + def _should_pair_share(self, block_idx: int) -> bool: + return getattr(self, "cross_layer_kv_pairwise", False) and (block_idx % 2 == 1) + + + def forward(self, input_ids: Tensor, + target_ids: Tensor, + reduction: str = "mean", + ngram_global_scale: float = 1.0) -> Tensor | tuple[Tensor, Tensor]: + + last_v_for_cross_layer_v: Tensor | None = None + last_k_for_kv_sharing: Tensor | None = None + last_v_for_kv_sharing: Tensor | None = None + + x = self.tok_emb(input_ids) + + # === 叠加 N-Gram 特征 === + # if getattr(self, 'ngram', None) is not None: + # x = x + self.ngram(input_ids) + #add N-Gram fade-out + if getattr(self, 'ngram', None) is not None: + scale = self.ngram_global_scale_buf.to(dtype=x.dtype) + x = x + scale * self.ngram(input_ids) + + x = F.rms_norm(x, (x.size(-1),)) + + if self.smear_mode: + x = self.local_mix(x) + + if self.smear_gate: + x = self.smear_gate_module(x) + + x0 = x + if self.bifpn2_mode: + skips: list[Tensor] = [] + + # ----------------------------- + # Encoder + # ----------------------------- + for i in range(self.num_encoder_layers): + shared_k = None + shared_v = None + prev_v = last_v_for_cross_layer_v + + if self._should_share_from_prev_layer(i): + shared_k = last_k_for_kv_sharing + shared_v = last_v_for_kv_sharing + + x, k_eff, v_eff = self.blocks[i]( + x, + x0, + shared_k=shared_k, + shared_v=shared_v, + prev_v=prev_v, + ) + skips.append(x) + + last_v_for_cross_layer_v = v_eff + last_k_for_kv_sharing = k_eff + last_v_for_kv_sharing = v_eff + + # ----------------------------- + # Decoder with structured BiFPN2 fusion + # ----------------------------- + for i in range(self.num_decoder_layers): + fusion_feature = self.structured_bifpn( + skips=skips, + decoder_idx=i, + x_dtype=x.dtype, + ) + x = x + fusion_feature + + block_idx = self.num_encoder_layers + i + shared_k = None + shared_v = None + prev_v = last_v_for_cross_layer_v + + if self._should_share_from_prev_layer(block_idx): + if self._should_pair_share(block_idx): + shared_k = last_k_for_kv_sharing + shared_v = last_v_for_kv_sharing + else: + shared_k = last_k_for_kv_sharing + shared_v = last_v_for_kv_sharing + + x, k_eff, v_eff = self.blocks[block_idx]( + x, + x0, + shared_k=shared_k, + shared_v=shared_v, + prev_v=prev_v, + ) + + last_v_for_cross_layer_v = v_eff + last_k_for_kv_sharing = k_eff + last_v_for_kv_sharing = v_eff + + elif self.bifpn_mode: + skips: list[Tensor] = [] + for i in range(self.num_encoder_layers): + x = self.blocks[i](x, x0) + skips.append(x) + + stacked_skips = torch.stack(skips, dim=0) # [E, B, T, D] + for i in range(self.num_decoder_layers): + w = self.bifpn_weights[i].to(dtype=x.dtype) # [E] + fusion_feature = torch.einsum("e,ebtd->btd", w, stacked_skips) + x = x + fusion_feature + x = self.blocks[self.num_encoder_layers + i](x, x0) + + elif self.fda_mode: + # FDA: Route earlier outputs directly to future inputs + history: list[Tensor] = [] + for i, block in enumerate(self.blocks): + lookback_idx = i - self.skip_distance + if lookback_idx >= 0: + w = self.skip_weights[lookback_idx].to(dtype=x.dtype)[None, None, :] + x = x + w * history[lookback_idx] + x = block(x, x0) + history.append(x) + else: + # Baseline: Encoder/Decoder U-Net Skips + skips: list[Tensor] = [] + for i in range(self.num_encoder_layers): + x = self.blocks[i](x, x0) + skips.append(x) + for i in range(self.num_decoder_layers): + if skips: + x = x + self.skip_weights[i].to(dtype=x.dtype)[None, None, :] * skips.pop() + x = self.blocks[self.num_encoder_layers + i](x, x0) + + #x = self.final_norm(x).reshape(-1, x.size(-1)) + #targets = target_ids.reshape(-1) + x = self.final_norm(x) + + # === 新增:在展平(reshape)之前,把保持 3D 形状的特征存下来给 MTP 用 === + x_original = x + + x = x.reshape(-1, x.size(-1)) + targets = target_ids.reshape(-1) + + if self.tie_embeddings: + if self.scaledlm_head: + # 缩放点积 + logits_proj = F.linear(x, self.tok_emb.weight) / math.sqrt(x.size(-1)) + else: + 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") + if self.scaledlm_head: + logits_proj = self.lm_head(x) / math.sqrt(x.size(-1)) + else: + logits_proj = self.lm_head(x) + + + # 遥测记录 + if not self.training or (hasattr(self, "_log_logits") and self._log_logits): + self.max_logit_pre_cap = logits_proj.detach().abs().max() + + logits = self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + + if reduction == "none": + # Sliding-window eval: return per-token loss shaped [B, T] + # logits is [B*T, vocab], targets is [B*T] — compute flat then reshape + loss_flat = F.cross_entropy(logits.float(), targets, reduction="none") + loss_tokens = loss_flat.view(input_ids.shape[0], input_ids.shape[1]) + return loss_tokens.mean(), loss_tokens + + main_loss = F.cross_entropy(logits.float(), targets, reduction="mean") + + # --- MTP Loss 计算 --- + if self.training and getattr(self, 'mtp_num_heads', 0) > 0 and getattr(self, 'mtp_loss_weight', 0.0) > 0.0: + _, seqlen, dim = x_original.shape + mtp_loss_sum = x_original.new_zeros(()) + mtp_loss_count = 0 + for k, mtp_head in enumerate(self.mtp_heads): + valid_t = seqlen - (k + 1) + if valid_t <= 0: + continue + + mtp_hidden = x_original[:, :valid_t, :].reshape(-1, dim) + mtp_targets = target_ids[:, k + 1 :].reshape(-1) + + mtp_logits_proj = mtp_head(mtp_hidden) + mtp_logits = self.logit_softcap * torch.tanh(mtp_logits_proj / self.logit_softcap) + mtp_loss_sum = mtp_loss_sum + F.cross_entropy(mtp_logits.float(), mtp_targets, reduction="mean") + mtp_loss_count += 1 + + if mtp_loss_count > 0: + main_loss = main_loss + self.mtp_loss_weight * (mtp_loss_sum / mtp_loss_count) + + return main_loss + + +# ----------------------------- +# TRAINING +# ----------------------------- + +def main() -> None: + global zeropower_via_newtonschulz5 + + code = Path(__file__).read_text(encoding="utf-8") + args = Hyperparameters() + zeropower_via_newtonschulz5 = torch.compile(zeropower_via_newtonschulz5) + + # ----------------------------- + # DISTRIBUTED + CUDA SETUP + # ----------------------------- + + distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ + rank = int(os.environ.get("RANK", "0")) + world_size = int(os.environ.get("WORLD_SIZE", "1")) + local_rank = int(os.environ.get("LOCAL_RANK", "0")) + if world_size <= 0: + raise ValueError(f"WORLD_SIZE must be positive, got {world_size}") + if 8 % world_size != 0: + raise ValueError(f"WORLD_SIZE={world_size} must divide 8 so grad_accum_steps stays integral") + grad_accum_steps = 8 // world_size + grad_accum_steps = int(os.environ.get("GRAD_ACCUM_STEPS", grad_accum_steps)) + + 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 + + # Fast math knobs + torch.backends.cuda.matmul.allow_tf32 = True + torch.backends.cudnn.allow_tf32 = True + from torch.backends.cuda import enable_cudnn_sdp, enable_flash_sdp, enable_math_sdp, enable_mem_efficient_sdp + + enable_cudnn_sdp(False) + enable_flash_sdp(True) + enable_mem_efficient_sdp(False) + enable_math_sdp(False) + + logfile = None + if master_process: + if args.output_dir: + os.makedirs(args.output_dir, exist_ok=True) + logfile = os.path.join(args.output_dir, f"{time.strftime('%Y%m%d_%H%M%S')}.txt") + else: + os.makedirs("logs", exist_ok=True) + logfile = f"logs/{args.run_id}.txt" + print(logfile) + + def log0(msg: str, console: bool = True) -> None: + if not master_process: + return + if console: + print(msg) + if logfile is not None: + with open(logfile, "a", encoding="utf-8") as f: + print(msg, file=f) + + log0(code, console=False) + log0("=" * 100, console=False) + log0(f"Running Python {sys.version}", console=False) + log0(f"Running PyTorch {torch.__version__}", console=False) + log0( + subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False).stdout, + console=False, + ) + log0("=" * 100, console=False) + + # ----------------------------- + # TOKENIZER + VALIDATION METRIC SETUP + # ----------------------------- + + random.seed(args.seed) + np.random.seed(args.seed) + torch.manual_seed(args.seed) + torch.cuda.manual_seed_all(args.seed) + + if not args.tokenizer_path.endswith(".model"): + raise ValueError(f"Script only setup for SentencePiece .model file: {args.tokenizer_path}") + sp = spm.SentencePieceProcessor(model_file=args.tokenizer_path) + if int(sp.vocab_size()) != args.vocab_size: + raise ValueError( + f"VOCAB_SIZE={args.vocab_size} does not match tokenizer vocab_size={int(sp.vocab_size())}" + ) + dataset_dir = Path(args.data_path).resolve() + actual_train_files = len(list(dataset_dir.glob("fineweb_train_*.bin"))) + val_tokens = load_validation_tokens(args.val_files, args.train_seq_len) + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut = build_sentencepiece_luts( + sp, args.vocab_size, device + ) + log0(f"val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path={args.tokenizer_path}") + log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}") + log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}") + + # ----------------------------- + # MODEL + OPTIMIZER SETUP + # ----------------------------- + + base_model = GPT(args, master_process=master_process).to(device).bfloat16() + for module in base_model.modules(): + if isinstance(module, CastedLinear): + module.float() + restore_low_dim_params_to_fp32(base_model) + + # Single compiled graph. QAT is a branchless buffer multiply inside CastedLinear, + # so the same graph handles both phases — no recompile, no DDP rebuild at qat_start_step. + compiled_model = torch.compile(base_model, dynamic=False, fullgraph=True) + model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model + + # Optimizer split: + # - token embedding & N-gram embedding (Adam) uses EMBED_LR + # - untied lm_head (Adam) uses HEAD_LR + # - matrix params in transformer blocks use MATRIX_LR via Muon + # - vectors/scalars/auxiliary heads use SCALAR_LR via Adam + block_named_params = list(base_model.blocks.named_parameters()) + matrix_params = [ + p + for name, p in block_named_params + if p.ndim == 2 and not any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) + ] + scalar_params = [ + p + for name, p in block_named_params + if p.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS) + ] + + # if base_model.skip_weights.numel() > 0: + # scalar_params.append(base_model.skip_weights) + # 1. 兼容各种架构的跳连权重 + if hasattr(base_model, 'skip_weights') and base_model.skip_weights.numel() > 0: + scalar_params.append(base_model.skip_weights) + if hasattr(base_model, 'bifpn_weights') and base_model.bifpn_weights.numel() > 0: + scalar_params.append(base_model.bifpn_weights) + if hasattr(base_model, 'structured_bifpn'): + scalar_params.append(base_model.structured_bifpn.weights) + + # 2. 将 MTP 的多层网络参数全部加入 scalar_params (修复了之前的 BUG) + if hasattr(base_model, 'mtp_heads') and base_model.mtp_heads is not None: + for p in base_model.mtp_heads.parameters(): + scalar_params.append(p) + # if hasattr(base_model, 'mtp_heads'): + # for head in base_model.mtp_heads: + # scalar_params.append(head.weight) + + # 3. 将 N-Gram 的投影层和小尺度参数加入 scalar_params + if hasattr(base_model, 'ngram') and base_model.ngram is not None: + if base_model.ngram.proj is not None: + scalar_params.append(base_model.ngram.proj.weight) + scalar_params.append(base_model.ngram.ngram_scales) + + +# 4. 配置 Token Optimizer (将普通的 Embedding 和 N-Gram Embedding 放在一起) + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr + tok_param_groups = [{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr}] + + if hasattr(base_model, 'ngram') and base_model.ngram is not None: + for emb in base_model.ngram.embeds: + tok_param_groups.append({"params": [emb.weight], "lr": token_lr, "base_lr": token_lr}) + + optimizer_tok = torch.optim.Adam( + tok_param_groups, + 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, + ) + for group in optimizer_muon.param_groups: + group["base_lr"] = args.matrix_lr + + optimizer_scalar = torch.optim.Adam( + [{"params": scalar_params, "lr": args.scalar_lr, "base_lr": args.scalar_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + optimizers: list[torch.optim.Optimizer] = [optimizer_tok, optimizer_muon, optimizer_scalar] + if base_model.lm_head is not None: + optimizer_head = torch.optim.Adam( + [{"params": [base_model.lm_head.weight], "lr": args.head_lr, "base_lr": args.head_lr}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + optimizers.insert(1, optimizer_head) + + n_params = sum(p.numel() for p in base_model.parameters()) + log0(f"model_params:{n_params}") + log0(f"world_size:{world_size} grad_accum_steps:{grad_accum_steps}") + log0("sdp_backends:cudnn=False flash=True mem_efficient=False math=False") + log0(f"attention_mode:gqa num_heads:{args.num_heads} num_kv_heads:{args.num_kv_heads}") + log0( + f"tie_embeddings:{args.tie_embeddings} embed_lr:{token_lr} " + f"head_lr:{args.head_lr if base_model.lm_head is not None else 0.0} " + f"matrix_lr:{args.matrix_lr} scalar_lr:{args.scalar_lr}" + ) + log0( + f"train_batch_tokens:{args.train_batch_tokens} train_seq_len:{args.train_seq_len} " + f"iterations:{args.iterations} warmup_steps:{args.warmup_steps} " + f"max_wallclock_seconds:{args.max_wallclock_seconds:.3f}" + ) + log0(f"seed:{args.seed}") + + if master_process: + mode_str = "Forward Dense (k=2)" if getattr(args, 'fda_mode', False) else "Symmetric U-Net" + if getattr(base_model, 'bifpn_mode', False): + mode_str = "BiFPN Weighted Addition Fusion" + print(f"Architecture Skip Mode: {mode_str}") + if getattr(base_model, 'ngram', None) is not None: + print(f"Enhancement: Discrete N-Gram Hash (Max N={base_model.ngram.max_n})") + + # ----------------------------- + # DATA LOADER & MODEL WARMUP + # ----------------------------- + + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + + def zero_grad_all() -> None: + for opt in optimizers: + opt.zero_grad(set_to_none=True) + + use_walltime_stop = args.stop_mode == "walltime" + use_steps_stop = args.stop_mode == "steps" + max_wallclock_ms = 1000.0 * args.max_wallclock_seconds if (use_walltime_stop and args.max_wallclock_seconds > 0) else None + # In steps mode the hard budget is max_train_steps (0 means fall back to iterations). + hard_step_limit = args.max_train_steps if (use_steps_stop and args.max_train_steps > 0) else args.iterations + + def lr_mul(step: int, elapsed_ms: float) -> float: + if args.warmdown_iters <= 0: + return 1.0 + if use_steps_stop: + warmdown_start = max(hard_step_limit - args.warmdown_iters, 0) + return max((hard_step_limit - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < hard_step_limit else 1.0 + if max_wallclock_ms is None: + warmdown_start = max(args.iterations - args.warmdown_iters, 0) + return max((args.iterations - step) / max(args.warmdown_iters, 1), 0.0) if warmdown_start <= step < args.iterations else 1.0 + step_ms = elapsed_ms / max(step, 1) + warmdown_ms = args.warmdown_iters * step_ms + remaining_ms = max(max_wallclock_ms - elapsed_ms, 0.0) + return remaining_ms / max(warmdown_ms, 1e-9) if remaining_ms <= warmdown_ms else 1.0 + + # Warmup primes the compiled forward/backward/optimizer paths, then we restore the + # initial weights/optimizer state so measured training starts from the true init. + if args.warmup_steps > 0: + initial_model_state = {name: tensor.detach().cpu().clone() for name, tensor in base_model.state_dict().items()} + initial_optimizer_states = [copy.deepcopy(opt.state_dict()) for opt in optimizers] + 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) + + # ----------------------------- + # MAIN TRAINING LOOP + # ----------------------------- + + training_time_ms = 0.0 + stop_after_step: int | None = None + torch.cuda.synchronize() + t0 = time.perf_counter() + + ema_enabled = bool(int(os.environ.get("EMA_ENABLED", "0"))) + ema_decay = float(os.environ.get("EMA_DECAY", "0.997")) + + if ema_enabled: + log0(f"EMA Enabled: decay={ema_decay}") + # 在 CPU 或 GPU 上维护一份 FP32 的高精度影子权重 + ema_state = {name: p.detach().float().clone() for name, p in base_model.state_dict().items()} + # 预先提取列表,加速循环内读取 + ema_tensors_list = list(ema_state.values()) + model_tensors_list = list(base_model.state_dict().values()) + + # === Late QAT: compute trigger step === + qat_start_step = int(hard_step_limit * (1.0 - args.late_qat_ratio)) + if args.late_qat_ratio > 0: + log0(f"Scheduled Late QAT to start at step {qat_start_step} (last {args.late_qat_ratio*100:.1f}%)") + + + if master_process: + non_embed_params = sum(p.numel() for name, p in base_model.named_parameters() if 'tok_emb' not in name) + event_fwd_start = torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None + event_bwd_end = torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None + event_opt_end = torch.cuda.Event(enable_timing=True) if torch.cuda.is_available() else None + + # === NEW: A/B PyTorch Profiler Integration === + prof = None + if args.profile_run: + def trace_handler(p): + if master_process: + out_d = args.profile_output_dir + os.makedirs(out_d, exist_ok=True) + + # Trace + p.export_chrome_trace(os.path.join(out_d, "trace.json")) + + # Summaries + with open(os.path.join(out_d, "summary_self_cuda.txt"), "w") as f: + f.write(p.key_averages().table(sort_by="self_cuda_time_total", row_limit=50)) + with open(os.path.join(out_d, "summary_cuda_total.txt"), "w") as f: + f.write(p.key_averages().table(sort_by="cuda_time_total", row_limit=50)) + with open(os.path.join(out_d, "summary_memory.txt"), "w") as f: + f.write(p.key_averages().table(sort_by="self_cuda_memory_usage", row_limit=50)) + + # Extract Specific Metrics + target_ops = [ + "aten::contiguous", "aten::clone", "aten::copy_", "aten::to", + "aten::repeat_interleave", "aten::permute", "aten::reshape", "aten::view", "Torch-Compiled Region" + ] + op_counts = {op: 0 for op in target_ops} + + max_cuda_alloc = 0 + max_cuda_res = 0 + if torch.cuda.is_available(): + torch.cuda.synchronize() + max_cuda_alloc = torch.cuda.max_memory_allocated() / (1024*1024) + max_cuda_res = torch.cuda.max_memory_reserved() / (1024*1024) + + for evt in p.key_averages(): + if evt.key in op_counts: + op_counts[evt.key] += evt.count + if "CompiledFunction" in evt.key or "Torch-Compiled Region" in evt.key: + if evt.key not in op_counts: + op_counts[evt.key] = 0 + op_counts[evt.key] += evt.count + + time_sorted = sorted(p.key_averages(), key=lambda x: getattr(x, 'self_device_time_total', getattr(x, 'self_cuda_time_total', 0)), reverse=True)[:15] + top_time = [{"name": e.key, "calls": e.count, "ms": getattr(e, 'self_device_time_total', getattr(e, 'self_cuda_time_total', 0))/1000.0} for e in time_sorted] + + mem_sorted = sorted(p.key_averages(), key=lambda x: getattr(x, 'self_device_memory_usage', getattr(x, 'self_cuda_memory_usage', 0)), reverse=True)[:15] + top_mem = [{"name": e.key, "calls": e.count, "mb": getattr(e, 'self_device_memory_usage', getattr(e, 'self_cuda_memory_usage', 0))/(1024*1024)} for e in mem_sorted] + + avg_step_ms = sum(profile_step_times) / len(profile_step_times) if profile_step_times else 0.0 + p50_step_ms = float(np.percentile(profile_step_times, 50)) if profile_step_times else 0.0 + p90_step_ms = float(np.percentile(profile_step_times, 90)) if profile_step_times else 0.0 + # tokens_per_sec = (args.train_batch_tokens * args.train_seq_len) / (avg_step_ms / 1000.0) if avg_step_ms > 0 else 0.0 + #train_batch_tokens is already a token count. Multiplying by train_seq_len is incorrect. + tokens_per_sec = args.train_batch_tokens / (avg_step_ms / 1000.0) + + metrics = { + "avg_step_ms": round(avg_step_ms, 2), + "p50_step_ms": round(p50_step_ms, 2), + "p90_step_ms": round(p90_step_ms, 2), + "tokens_per_sec": round(tokens_per_sec, 2), + "max_cuda_alloc_mb": round(max_cuda_alloc, 2), + "max_cuda_res_mb": round(max_cuda_res, 2), + "op_counts": op_counts, + "top_ops_time": top_time, + "top_ops_memory": top_mem + } + + with open(os.path.join(out_d, "metrics.json"), "w") as f: + json.dump(metrics, f, indent=2) + print(f"✅ Profiling complete. Artifacts saved inside: {out_d}") + + print(f"🔍 Starting Scheduled Profiler (Warmup: {args.profile_warmup_steps}, Active: {args.profile_active_steps})...") + prof = torch.profiler.profile( + activities=[torch.profiler.ProfilerActivity.CPU, torch.profiler.ProfilerActivity.CUDA], + schedule=torch.profiler.schedule(wait=1, warmup=args.profile_warmup_steps, active=args.profile_active_steps, repeat=1), + on_trace_ready=trace_handler, + record_shapes=True, + profile_memory=True, + with_flops=True, + with_modules=True, + with_stack=True + ) + prof.start() + + step = 0 + profile_step_times = [] + # Initialize muon_momentum before the loop so telemetry on step 0 doesn't crash + muon_momentum = args.muon_momentum_warmup_start if args.muon_momentum_warmup_steps > 0 else args.muon_momentum + while True: + last_step = step == hard_step_limit or (stop_after_step is not None and step >= stop_after_step) + + # Suppress validation and telemetry during active profiling to keep traces clean. + # Profiler schedule: wait=1, warmup=profile_warmup_steps, active=profile_active_steps. + profiling_active = args.profile_run and step < (1 + args.profile_warmup_steps + args.profile_active_steps) + + should_validate = (last_step or (args.val_loss_every > 0 and step % args.val_loss_every == 0)) and not profiling_active + if should_validate: + torch.cuda.synchronize() + training_time_ms += 1000.0 * (time.perf_counter() - t0) + + eval_fn = eval_val_sliding if args.eval_use_sliding_window else eval_val + # Sliding window passes reduction="none" which graph-breaks torch.compile, + # so we pass base_model (uncompiled) for that path. + eval_model = base_model if args.eval_use_sliding_window else model + val_loss, val_bpb = eval_fn( + args, + eval_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}/{hard_step_limit} 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 < hard_step_limit: + log0( + f"stopping_early: {args.stop_mode}_cap train_time:{training_time_ms:.0f}ms " + f"step:{step}/{hard_step_limit}" + ) + break + + + # === Late QAT: flip buffer to 1.0 — same compiled graph, no recompile === + if step == qat_start_step and args.late_qat_ratio > 0.0: + log0(f"[Step {step}] Activating Late QAT — enabling branchless STE quantization.") + for mod in base_model.modules(): + if isinstance(mod, CastedLinear): + mod.qat_alpha.fill_(1.0) + + step_t0 = time.perf_counter() + elapsed_ms = training_time_ms + 1000.0 * (step_t0 - t0) + scale = lr_mul(step, elapsed_ms) + zero_grad_all() + train_loss = torch.zeros((), device=device) + + should_telemetry = (args.telemetry_every > 0) and (step % args.telemetry_every == 0) and not profiling_active + + if should_telemetry and master_process and event_fwd_start is not None: + event_fwd_start.record() + + 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) + + if should_telemetry and micro_step == grad_accum_steps - 1: + base_model._log_logits = True + + ngram_global_scale = compute_ngram_fade_scale( + step=step, + total_steps=hard_step_limit, + enabled=args.ngram_fade_enable, + start_frac=args.ngram_fade_start_frac, + end_frac=args.ngram_fade_end_frac, + min_scale=args.ngram_fade_min_scale, + ) + + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + #loss = model(x, y) + #loss = model(x, y, ngram_global_scale=ngram_global_scale) + base_model.ngram_global_scale_buf.fill_(float(ngram_global_scale)) + loss = model(x, y) + + if should_telemetry and micro_step == grad_accum_steps - 1: + base_model._log_logits = False + + train_loss += loss.detach() + (loss * grad_scale).backward() + train_loss /= grad_accum_steps + + if should_telemetry and master_process and event_bwd_end is not None: + event_bwd_end.record() + + if should_telemetry and master_process: + with torch.no_grad(): + # --- Update how we extract the max_logit_pre_cap --- + logit_val = base_model.max_logit_pre_cap + logit_item = logit_val.item() if isinstance(logit_val, torch.Tensor) else logit_val + + telemetry_data = { + "step": step, + "train_loss": round(train_loss.item(), 4), + "max_logit_pre_cap": round(logit_item, 4), + } + + if base_model.tok_emb.weight.grad is not None: + telemetry_data["grad_norm_embed"] = round(base_model.tok_emb.weight.grad.norm().item(), 4) + + if hasattr(base_model, 'skip_weights') and base_model.skip_weights.numel() > 0: + telemetry_data["skip_weight_mean"] = round(base_model.skip_weights.mean().item(), 4) + telemetry_data["skip_weight_max"] = round(base_model.skip_weights.max().item(), 4) + + if hasattr(base_model, 'bifpn_weights') and base_model.bifpn_weights.numel() > 0: + telemetry_data["bifpn_weight_mean"] = round(base_model.bifpn_weights.mean().item(), 4) + telemetry_data["bifpn_weight_max"] = round(base_model.bifpn_weights.max().item(), 4) + # 偷窥一下模型是不是在用额外的 FPN 路径 (去对角线后的均值) + # 注意:我们这里简单算一下所有权重的均值,如果在变大,说明密集连接生效了! + + # === 新增:将局部混合权重加入标量优化器 === + # if hasattr(base_model, 'smear_mode') and base_model.smear_mode: + # scalar_params.append(base_model.local_mix.mix_logits) + + if hasattr(base_model, 'ngram') and base_model.ngram is not None: + scales = base_model.ngram.ngram_scales.detach().cpu().float().numpy() + if len(scales) > 0: telemetry_data["scale_bigram"] = round(float(scales[0]), 4) + if len(scales) > 1: telemetry_data["scale_trigram"] = round(float(scales[1]), 4) + if len(scales) > 2: telemetry_data["scale_4gram"] = round(float(scales[2]), 4) + + if hasattr(base_model, 'bifpn_mode') and base_model.bifpn_mode: + # 记录整个矩阵的摊平状态,或者直接观察非对角线元素是否变大了 + telemetry_data["bifpn_off_diag_mean"] = round( + base_model.bifpn_weights.detach().clone().fill_diagonal_(0).mean().item(), 4 + ) + + + + # Unfortunately, the way we wrote the forward pass, we only return the combined loss. + # To monitor MTP separately without breaking the compile graph, it's best to observe how the gradient norms of the mtp_heads change. + if hasattr(base_model, 'mtp_heads') and len(base_model.mtp_heads) > 0: + mtp_grad_norm = 0.0 + for p in base_model.mtp_heads.parameters(): + if p.grad is not None: + mtp_grad_norm += p.grad.norm().item() + telemetry_data["grad_norm_mtp"] = round(mtp_grad_norm, 4) + + # === Suggestion 9: XSA config telemetry === + telemetry_data["xsa_enabled"] = int(args.xsa_enabled) + telemetry_data["xsa_last_n_layers"] = args.xsa_last_n_layers + + # === Suggestion 10: Optimizer state logging === + telemetry_data["lr_warmdown_scale"] = round(scale, 6) + telemetry_data["muon_momentum"] = round(muon_momentum, 6) + # Log the actual LR being applied to each optimizer group + for opt_idx, opt in enumerate(optimizers): + for gidx, group in enumerate(opt.param_groups): + key = f"lr_opt{opt_idx}_g{gidx}" + telemetry_data[key] = round(group.get("lr", 0.0), 8) + telemetry_data["qat_active"] = int(step >= qat_start_step and args.late_qat_ratio > 0) + + telemetry_data["ngram_fade_enable"] = int(args.ngram_fade_enable) + telemetry_data["ngram_global_scale"] = round(float(ngram_global_scale), 4) + if hasattr(base_model, 'structured_bifpn'): + eff = base_model.structured_bifpn.export_effective_matrix().detach().cpu() + telemetry_data["bifpn2_eff_mean"] = round(float(eff.mean().item()), 4) + telemetry_data["bifpn2_eff_max"] = round(float(eff.max().item()), 4) + telemetry_data["bifpn2_eff_min"] = round(float(eff.min().item()), 4) + + telemetry_data["v_skip_enabled"] = int(args.v_skip_enabled) + telemetry_data["v_skip_last_n_layers"] = args.v_skip_last_n_layers + telemetry_data["cross_layer_v_enabled"] = int(args.cross_layer_v_enabled) + telemetry_data["cross_layer_v_last_n_layers"] = args.cross_layer_v_last_n_layers + telemetry_data["cross_layer_kv_sharing_enabled"] = int(args.cross_layer_kv_sharing_enabled) + telemetry_data["cross_layer_kv_last_n_layers"] = args.cross_layer_kv_last_n_layers + telemetry_data["cross_layer_kv_pairwise"] = int(args.cross_layer_kv_pairwise) + telemetry_data["cross_layer_kv_partial_head"] = int(args.cross_layer_kv_partial_head) + telemetry_data["cross_layer_kv_partial_head_count"] = args.cross_layer_kv_partial_head_count + + # ========================================== + + 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() + + if prof is not None: + if step > args.profile_warmup_steps and step <= args.profile_warmup_steps + args.profile_active_steps: + profile_step_times.append(1000.0 * (time.perf_counter() - step_t0)) + prof.step() + + if should_telemetry and master_process and event_opt_end is not None: + event_opt_end.record() + event_opt_end.synchronize() + time_fwd_bwd_ms = event_fwd_start.elapsed_time(event_bwd_end) + time_optim_ms = event_bwd_end.elapsed_time(event_opt_end) + step_time_s = (time_fwd_bwd_ms + time_optim_ms) / 1000.0 + + tokens_per_sec = args.train_batch_tokens / max(step_time_s, 0.001) + flops_per_step = 6.0 * non_embed_params * args.train_batch_tokens + tflops_achieved = (flops_per_step / max(step_time_s, 0.001)) / 1e12 + + telemetry_data["time_fwd_bwd_ms"] = round(time_fwd_bwd_ms, 2) + telemetry_data["time_optim_ms"] = round(time_optim_ms, 2) + telemetry_data["tokens_per_sec"] = round(tokens_per_sec, 2) + telemetry_data["mfu_tflops"] = round(tflops_achieved, 2) + + if torch.cuda.is_available(): + telemetry_data["memory_allocated_mb"] = round(torch.cuda.max_memory_allocated() / (1024 * 1024), 2) + telemetry_data["memory_reserved_mb"] = round(torch.cuda.max_memory_reserved() / (1024 * 1024), 2) + torch.cuda.reset_peak_memory_stats() + + os.makedirs(os.path.dirname(args.telemetry_file) or ".", exist_ok=True) + with open(args.telemetry_file, "a", encoding="utf-8") as f: + f.write(json.dumps(telemetry_data) + "\n") + zero_grad_all() + + # === EMA Update === + if ema_enabled: + with torch.no_grad(): + update_ema_fused(ema_tensors_list, model_tensors_list, ema_decay) + #Eager mode is slow due to Python loop and multiple dispatch on each parameter, so we use a fused CUDA kernel instead. The commented code below is the naive eager implementation for reference. + # for name, p in base_model.state_dict().items(): + # # ema_weight = decay * ema_weight + (1 - decay) * current_weight + # ema_state[name].mul_(ema_decay).add_(p.detach().float(), alpha=1.0 - ema_decay) + + step += 1 + approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + should_log_train = ( + args.train_log_every > 0 + and (step <= 10 or step % args.train_log_every == 0 or stop_after_step is not None) + ) + if should_log_train: + log0( + f"step:{step}/{hard_step_limit} train_loss:{train_loss.item():.4f} " + f"train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms" + ) + + # Determine whether we've hit the configured stop budget. + if use_steps_stop: + reached_cap = step >= hard_step_limit + else: + 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 ema_enabled: + log0("Applying EMA weights for final evaluation...") + current_state = base_model.state_dict() + avg_state = {name: t.to(dtype=current_state[name].dtype) for name, t in ema_state.items()} + base_model.load_state_dict(avg_state, strict=True) + + # ----------------------------- + # SERIALIZATION + ROUNDTRIP VALIDATION + # ----------------------------- + # Save the raw state (useful for debugging/loading in PyTorch directly), then always produce + # the compressed int8+zlib artifact and validate the round-tripped weights. + + model_path = os.path.join(args.output_dir, "final_model.pt") if args.output_dir else "final_model.pt" + if master_process: + torch.save(base_model.state_dict(), model_path) + model_bytes = os.path.getsize(model_path) + code_bytes = len(code.encode("utf-8")) + log0(f"Serialized model: {model_bytes} bytes") + log0(f"Code size: {code_bytes} bytes") + log0(f"Total submission size: {model_bytes + code_bytes} bytes") + + quant_obj, quant_stats = quantize_state_dict_int8(base_model.state_dict()) + quant_buf = io.BytesIO() + torch.save(quant_obj, quant_buf) + quant_raw = quant_buf.getvalue() + quant_blob = zlib.compress(quant_raw, level=9) + quant_raw_bytes = len(quant_raw) + quant_model_path = os.path.join(args.output_dir, "final_model.int8.ptz") if args.output_dir else "final_model.int8.ptz" + if master_process: + with open(quant_model_path, "wb") as f: + f.write(quant_blob) + quant_file_bytes = os.path.getsize(quant_model_path) + code_bytes = len(code.encode("utf-8")) + ratio = quant_stats["baseline_tensor_bytes"] / max(quant_stats["int8_payload_bytes"], 1) + log0( + f"Serialized model int8+zlib: {quant_file_bytes} bytes " + f"(payload:{quant_stats['int8_payload_bytes']} raw_torch:{quant_raw_bytes} payload_ratio:{ratio:.2f}x)" + ) + log0(f"Total submission size int8+zlib: {quant_file_bytes + code_bytes} bytes") + + if distributed: + dist.barrier() + with open(quant_model_path, "rb") as f: + quant_blob_disk = f.read() + quant_state = torch.load(io.BytesIO(zlib.decompress(quant_blob_disk)), map_location="cpu") + base_model.load_state_dict(dequantize_state_dict_int8(quant_state), strict=True) + torch.cuda.synchronize() + t_qeval = time.perf_counter() + q_val_loss, q_val_bpb = eval_val( + args, + model, + rank, + world_size, + device, + grad_accum_steps, + val_tokens, + base_bytes_lut, + has_leading_space_lut, + is_boundary_token_lut, + ) + torch.cuda.synchronize() + log0( + f"final_int8_zlib_roundtrip val_loss:{q_val_loss:.4f} val_bpb:{q_val_bpb:.4f} " + f"eval_time:{1000.0 * (time.perf_counter() - t_qeval):.0f}ms" + ) + log0(f"final_int8_zlib_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}") + + if distributed: + dist.destroy_process_group() + + if prof is not None: + prof.stop() + + +if __name__ == "__main__": + main() +