diff --git a/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/README.md b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/README.md new file mode 100644 index 0000000000..836fa6d317 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/README.md @@ -0,0 +1,277 @@ +# FlashMuon + Linear Scale Init + XSA5 Last-Gated + RReLU2 + Int6 AWQ + +## Summary + +This record is built around a simple idea: keep the model small enough to quantize well, but make the late layers stronger and more useful during the short training budget. + +Final construction: + +- `XSA` enabled on the last 5 layers +- only the final XSA layer is gated +- `RReLU2` MLP activation +- `Muon` weight decay `0.01` +- `int6_awq + lzma` export +- validation-tail calibration for quantization +- late EMA and post-train candidate selection +- depth-aware constant initialization for `attn_scale` and `mlp_scale` + +## Main Thesis + +The best improvements came from four places: + +1. Better late-layer attention routing +2. Better post-train quantization +3. Better training-time LR / warmdown behavior +4. Better initialization of layer scales + +## Speed Features + +- Flash attention on `H100` gives a large attention-speed improvement. +- Flash Muon improves multi-GPU throughput and uses fast Triton kernels. + +## Quantization + +Weight-only quantization methods were compared by roundtrip quality. + +Quality ranking: + +- `int8_sq`: `1.8232` +- `int8_awq`: `1.8238` +- `int8`: `1.8238` +- `int6_awq`: `1.8493` +- `int6`: `1.8540` +- `int6_sq`: `1.8541` +- `int4`: `2.4294` + +Chosen method: + +- `int6_awq` + +Reason: + +- best compression / quality tradeoff under the submission budget + +### Calibration Sources + +Tested: + +- `val_first` +- `val_random` +- `val_strided` +- `val_tail` +- `train_stream` +- `ar_selfgen` + +Best choice: + +- `val_tail` + +### Compression Backend + +Tested: + +- `zlib` +- `zstd` +- `lzma` + +Best choice for the chosen `int6_awq` path: + +- `lzma` + +## Single-GPU Architecture and Optimizer Experiments + +### Setup: 2000 seconds + +Baseline: + +- Naive: `val_bpb 1.3208`, size `10,683,499` + +Muon decay: + +- `muon_wd=0.01`: `1.3198`, size `10,085,675` +- `muon_wd=0.1`: `1.3563`, size `6,739,476` + +Conclusion: + +- `muon_wd=0.01` helped +- `muon_wd=0.1` over-compressed / hurt quality too much + +### XSA structure + +Results: + +- `XSA 2L`: `1.3187` +- `XSA 2L gated sigmoid`: `1.3216` +- `XSA 2L gated relu2`: `1.3190` +- `XSA 2L gated rrelu2`: `1.3185` +- `XSA 4L gated rrelu2`: `1.3197` +- `XSA 4L last gated`: `1.3170` +- `XSA 3L last gated`: `1.3174` +- `XSA 5L last gated`: `1.3158` + +Conclusion: + +- enabling XSA on the last few layers helps +- gating only the last XSA layer is better than gating all of them +- `XSA 5L last gated` was the best result in this sweep + +### Setup: 2000 iterations + +Results: + +- `XSA 5L last gated`: `1.3125` +- `+ rotary`: `1.3187` +- `+ MLP rrelu2`: `1.3095` +- `+ MLP silu_mul`: `1.3117` +- `+ MLP mish`: `1.3239` + +Conclusion: + +- `RReLU2` was the best MLP activation for this branch +- the tested rotary variant was worse here + +## Initialization Experiments + +### Residual / phase initialization + +Tested ideas included: + +- reverse linear +- balanced +- local residual bias +- root skip bias +- quadratic +- sqrt +- sigmoid +- staged / windowed +- random around prior +- normalized pair + +Best choice: + +- simple linear phase initialization + +### Scale initialization + +Experiments showed that giving later layers stronger initial residual branch scales helps. + +Chosen initialization: + +- `attn_scale` + - early: `1.0` + - mid: `1.75` + - late: `2.5` +- `mlp_scale` + - early: `1.0` + - mid: `1.15` + - late: `1.3` + +Conclusion: + +- simple depth-aware constant scales worked better than the more aggressive donor-inspired random scale initializations + +## Post-Train Selection + +Tested: + +- `SWA` +- `LAWA` +- `EMA` + +Conclusions: + +- `SWA` and `LAWA` did not help this branch +- late-start `EMA` gave a small but real improvement +- post-train candidate evaluation is useful because the best late state is not always the raw final step + +### Best-Choice module + +The final branch uses a small post-train model-selection module instead of trusting the last checkpoint blindly. + +How it works: + +- `EMA` starts only late in training +- late checkpoints are collected near the end of the run +- after training finishes, several candidates are evaluated on validation +- the candidate with the best validation score is selected for export + +Candidate set: + +- raw final checkpoint +- `EMA` checkpoint +- selected late checkpoints +- average of the selected late checkpoints + +Reason: + +- under a short wallclock budget, late training is noisy +- the numerically last checkpoint is often not the best one +- averaging all late checkpoints equally was not strong enough for this branch +- explicit post-train comparison gave a more reliable final exported model + +Practical effect: + +- separates training from export-time model selection +- keeps the training loop cheap +- spends extra time only after training, where the budget is less sensitive +- improves the chance that quantization is applied to the best available float model + +### Sliding-window evaluation + +Used: + +- sliding-window validation with stride `64` + +Purpose: + +- bring quantized roundtrip evaluation closer to the raw loss ranking +- reduce mismatch between contiguous eval and final exported model quality + +## Vocabulary Experiments + +Small tests on `4000s`, `1 GPU`: + +- `1024`: `val_bpb 1.210`, under `16 MB` +- `1536`: `val_bpb 1.201`, over `16 MB` +- `1792`: `val_bpb 1.192`, over `16 MB` +- `2048`: `val_bpb 1.189`, over `16 MB` + +Conclusion: + +- larger vocabulary improves raw quality +- but `1024` is the best fit for the current size budget + +## Architecture Notes + +- best current branch uses `10` layers +- deeper models can improve raw validation loss +- but the final compressed model becomes too large for the `16 MB` target +- `MLP_MULT=3` remained the best practical choice in this branch + +## Training Process + +Important training-side decisions: + +- warmdown changed from step-based to progress-based +- best result came from starting warmdown at about `75%` of total training progress +- added a small initial LR decay before warmdown +- added `AdamW` decay `0.01` for linear Adam-side weights +- increased `MUON_MOMENTUM_WARMUP_STEPS` to `1200` +- increased sequence length to `2048` +- increased train batch tokens to `786432` +- decreased `TIED_EMBED_INIT_STD` + +## Final Recipe + +The final recipe for this record is: + +- `10` layers +- `XSA` on last `5` layers +- only the last XSA layer gated +- `RReLU2` MLP +- `Muon WD = 0.01` +- linear phase initialization +- depth-aware constant `attn_scale` / `mlp_scale` initialization +- late EMA +- `int6_awq + lzma` +- `val_tail` calibration diff --git a/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ flash_attn_3.txt b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ flash_attn_3.txt new file mode 100644 index 0000000000..893119260f --- /dev/null +++ b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ flash_attn_3.txt @@ -0,0 +1,241 @@ + +==================================================================================================== +Running Python 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0] +Running PyTorch 2.9.1+cu128 +Tue Apr 7 17:36:56 2026 ++-----------------------------------------------------------------------------------------+ +| NVIDIA-SMI 580.126.09 Driver Version: 580.126.09 CUDA Version: 13.0 | ++-----------------------------------------+------------------------+----------------------+ +| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|=========================================+========================+======================| +| 0 NVIDIA H100 80GB HBM3 On | 00000000:19:00.0 Off | 0 | +| N/A 44C P0 128W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 1 NVIDIA H100 80GB HBM3 On | 00000000:3B:00.0 Off | 0 | +| N/A 36C P0 117W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 2 NVIDIA H100 80GB HBM3 On | 00000000:4C:00.0 Off | 0 | +| N/A 34C P0 117W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 3 NVIDIA H100 80GB HBM3 On | 00000000:5D:00.0 Off | 0 | +| N/A 44C P0 120W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 4 NVIDIA H100 80GB HBM3 On | 00000000:9B:00.0 Off | 0 | +| N/A 46C P0 128W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 5 NVIDIA H100 80GB HBM3 On | 00000000:BB:00.0 Off | 0 | +| N/A 35C P0 122W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 6 NVIDIA H100 80GB HBM3 On | 00000000:CB:00.0 Off | 0 | +| N/A 44C P0 124W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 7 NVIDIA H100 80GB HBM3 On | 00000000:DB:00.0 Off | 0 | +| N/A 36C P0 123W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ + ++-----------------------------------------------------------------------------------------+ +| Processes: | +| GPU GI CI PID Type Process name GPU Memory | +| ID ID Usage | +|=========================================================================================| +| No running processes found | ++-----------------------------------------------------------------------------------------+ + +==================================================================================================== +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 +model_params:24140369 +world_size:8 grad_accum_steps:1 +sdp_backends:cudnn=False flash=True mem_efficient=False math=False +attention_backend:flash_attn_3 +attention_mode:gqa num_heads:8 num_kv_heads:4 +tie_embeddings:True embed_lr:0.056249999999999994 head_lr:0.0 matrix_lr:0.045 scalar_lr:0.045 +init_scales:attn_scale=(early=1.0,mid=1.75,late=2.5) mlp_scale=(early=1.0,mid=1.15,late=1.3) +adam_wd:0.0100 muon_wd:0.0100 +train_batch_tokens:786432 train_seq_len:2048 iterations:20000 warmup_steps:20 warmdown_start:0.750 max_wallclock_seconds:600.000 +lr_scheduler:warmdown piecewise_matrix_lrs:- +seed:1337 +quant_export:int6_awq val_tail_calib quant_calib_seqs:32 quant_awq_alpha:0.500 +val_sliding_stride:64 val_sliding_batch_seqs:32 val_sliding_seq_len:0 val_sliding_log_every:10000 +ema_enabled:1 ema_decay:0.9970 ema_start:0.940 best_ckpt_k:10 best_ckpt_start:0.980 +warmup_step:1/20 +warmup_step:2/20 +warmup_step:3/20 +warmup_step:4/20 +warmup_step:5/20 +warmup_step:6/20 +warmup_step:7/20 +warmup_step:8/20 +warmup_step:9/20 +warmup_step:10/20 +warmup_step:11/20 +warmup_step:12/20 +warmup_step:13/20 +warmup_step:14/20 +warmup_step:15/20 +warmup_step:16/20 +warmup_step:17/20 +warmup_step:18/20 +warmup_step:19/20 +warmup_step:20/20 +step:1/20000 train_loss:6.9303 train_bpb:4.0923 train_time:127ms step_avg:126.67ms lr_matrix:0.045000 +step:2/20000 train_loss:18.5855 train_bpb:11.2766 train_time:214ms step_avg:106.99ms lr_matrix:0.044991 +step:3/20000 train_loss:8.9140 train_bpb:5.3433 train_time:301ms step_avg:100.44ms lr_matrix:0.044985 +step:4/20000 train_loss:6.4042 train_bpb:3.8000 train_time:389ms step_avg:97.17ms lr_matrix:0.044979 +step:5/20000 train_loss:6.2312 train_bpb:3.5539 train_time:478ms step_avg:95.68ms lr_matrix:0.044973 +step:6/20000 train_loss:6.2105 train_bpb:3.5638 train_time:566ms step_avg:94.26ms lr_matrix:0.044967 +step:7/20000 train_loss:6.0995 train_bpb:3.5950 train_time:653ms step_avg:93.27ms lr_matrix:0.044961 +step:8/20000 train_loss:6.0183 train_bpb:3.5241 train_time:740ms step_avg:92.54ms lr_matrix:0.044955 +step:9/20000 train_loss:5.9224 train_bpb:3.4229 train_time:829ms step_avg:92.15ms lr_matrix:0.044949 +step:10/20000 train_loss:5.8133 train_bpb:3.4352 train_time:917ms step_avg:91.66ms lr_matrix:0.044943 +step:100/20000 train_loss:4.3551 train_bpb:2.5707 train_time:8802ms step_avg:88.02ms lr_matrix:0.044400 +step:200/20000 train_loss:2.8988 train_bpb:1.7162 train_time:17638ms step_avg:88.19ms lr_matrix:0.043800 +step:300/20000 train_loss:2.5563 train_bpb:1.5083 train_time:26491ms step_avg:88.30ms lr_matrix:0.043207 +step:400/20000 train_loss:2.4532 train_bpb:1.4399 train_time:35368ms step_avg:88.42ms lr_matrix:0.042620 +step:500/20000 train_loss:2.3760 train_bpb:1.4034 train_time:44182ms step_avg:88.36ms lr_matrix:0.042045 +step:600/20000 train_loss:2.3376 train_bpb:1.3866 train_time:53096ms step_avg:88.49ms lr_matrix:0.041472 +step:700/20000 train_loss:2.3057 train_bpb:1.3640 train_time:62009ms step_avg:88.58ms lr_matrix:0.040906 +step:800/20000 train_loss:2.2577 train_bpb:1.3279 train_time:70936ms step_avg:88.67ms lr_matrix:0.040348 +step:900/20000 train_loss:2.2405 train_bpb:1.3164 train_time:79934ms step_avg:88.82ms lr_matrix:0.039792 +step:1000/20000 train_loss:2.2332 train_bpb:1.3178 train_time:88802ms step_avg:88.80ms lr_matrix:0.039252 +step:1100/20000 train_loss:2.2285 train_bpb:1.3219 train_time:97724ms step_avg:88.84ms lr_matrix:0.038717 +step:1200/20000 train_loss:2.2120 train_bpb:1.3071 train_time:106654ms step_avg:88.88ms lr_matrix:0.038188 +step:1300/20000 train_loss:2.2130 train_bpb:1.3121 train_time:115577ms step_avg:88.91ms lr_matrix:0.037667 +step:1400/20000 train_loss:2.1760 train_bpb:1.2905 train_time:124493ms step_avg:88.92ms lr_matrix:0.037153 +step:1500/20000 train_loss:2.1523 train_bpb:1.2726 train_time:133328ms step_avg:88.89ms lr_matrix:0.036651 +step:1600/20000 train_loss:2.1486 train_bpb:1.2637 train_time:142238ms step_avg:88.90ms lr_matrix:0.036151 +step:1700/20000 train_loss:2.1551 train_bpb:1.2771 train_time:151129ms step_avg:88.90ms lr_matrix:0.035659 +step:1800/20000 train_loss:2.1461 train_bpb:1.2744 train_time:160016ms step_avg:88.90ms lr_matrix:0.035174 +step:1900/20000 train_loss:2.1368 train_bpb:1.2646 train_time:168838ms step_avg:88.86ms lr_matrix:0.034700 +step:2000/20000 train_loss:2.1329 train_bpb:1.2616 train_time:177733ms step_avg:88.87ms lr_matrix:0.034228 +step:2100/20000 train_loss:2.1227 train_bpb:1.2547 train_time:186635ms step_avg:88.87ms lr_matrix:0.033761 +step:2200/20000 train_loss:2.1062 train_bpb:1.2409 train_time:195529ms step_avg:88.88ms lr_matrix:0.033302 +step:2300/20000 train_loss:2.0965 train_bpb:1.2385 train_time:204423ms step_avg:88.88ms lr_matrix:0.032849 +step:2400/20000 train_loss:2.1025 train_bpb:1.2383 train_time:213252ms step_avg:88.86ms lr_matrix:0.032405 +step:2500/20000 train_loss:2.1045 train_bpb:1.2414 train_time:222141ms step_avg:88.86ms lr_matrix:0.031965 +step:2600/20000 train_loss:2.1094 train_bpb:1.2500 train_time:231030ms step_avg:88.86ms lr_matrix:0.031530 +step:2700/20000 train_loss:2.0876 train_bpb:1.2354 train_time:239924ms step_avg:88.86ms lr_matrix:0.031101 +step:2800/20000 train_loss:2.0792 train_bpb:1.2304 train_time:248817ms step_avg:88.86ms lr_matrix:0.030678 +step:2900/20000 train_loss:2.0808 train_bpb:1.2243 train_time:257641ms step_avg:88.84ms lr_matrix:0.030264 +step:3000/20000 train_loss:2.0811 train_bpb:1.2248 train_time:266525ms step_avg:88.84ms lr_matrix:0.029852 +step:3100/20000 train_loss:2.0612 train_bpb:1.2156 train_time:275425ms step_avg:88.85ms lr_matrix:0.029446 +step:3200/20000 train_loss:2.0594 train_bpb:1.2118 train_time:284316ms step_avg:88.85ms lr_matrix:0.029045 +step:3300/20000 train_loss:2.0652 train_bpb:1.2191 train_time:293131ms step_avg:88.83ms lr_matrix:0.028654 +step:3400/20000 train_loss:2.0707 train_bpb:1.2291 train_time:302015ms step_avg:88.83ms lr_matrix:0.028264 +step:3500/20000 train_loss:2.0665 train_bpb:1.2219 train_time:310908ms step_avg:88.83ms lr_matrix:0.027880 +step:3600/20000 train_loss:2.0491 train_bpb:1.2083 train_time:319793ms step_avg:88.83ms lr_matrix:0.027501 +step:3700/20000 train_loss:2.0361 train_bpb:1.1965 train_time:328687ms step_avg:88.83ms lr_matrix:0.027127 +step:3800/20000 train_loss:2.0489 train_bpb:1.2102 train_time:337497ms step_avg:88.82ms lr_matrix:0.026761 +step:3900/20000 train_loss:2.0655 train_bpb:1.2229 train_time:346382ms step_avg:88.82ms lr_matrix:0.026397 +step:4000/20000 train_loss:2.0683 train_bpb:1.2180 train_time:355265ms step_avg:88.82ms lr_matrix:0.026038 +step:4100/20000 train_loss:2.0574 train_bpb:1.2179 train_time:364147ms step_avg:88.82ms lr_matrix:0.025685 +step:4200/20000 train_loss:2.0584 train_bpb:1.2164 train_time:373022ms step_avg:88.81ms lr_matrix:0.025336 +step:4300/20000 train_loss:2.0371 train_bpb:1.2073 train_time:381829ms step_avg:88.80ms lr_matrix:0.024995 +step:4400/20000 train_loss:2.0406 train_bpb:1.2131 train_time:390712ms step_avg:88.80ms lr_matrix:0.024655 +step:4500/20000 train_loss:2.0392 train_bpb:1.2038 train_time:399587ms step_avg:88.80ms lr_matrix:0.024320 +step:4600/20000 train_loss:2.0288 train_bpb:1.1954 train_time:408464ms step_avg:88.80ms lr_matrix:0.023990 +step:4700/20000 train_loss:2.0256 train_bpb:1.1962 train_time:417280ms step_avg:88.78ms lr_matrix:0.023666 +step:4800/20000 train_loss:2.0391 train_bpb:1.2046 train_time:426145ms step_avg:88.78ms lr_matrix:0.023345 +step:4900/20000 train_loss:2.0333 train_bpb:1.1998 train_time:435013ms step_avg:88.78ms lr_matrix:0.023029 +step:5000/20000 train_loss:2.0326 train_bpb:1.2002 train_time:443902ms step_avg:88.78ms lr_matrix:0.022715 +step:5100/20000 train_loss:2.0342 train_bpb:1.2063 train_time:452787ms step_avg:88.78ms lr_matrix:0.022095 +step:5200/20000 train_loss:2.0309 train_bpb:1.1964 train_time:461586ms step_avg:88.77ms lr_matrix:0.020775 +step:5300/20000 train_loss:2.0262 train_bpb:1.1980 train_time:470449ms step_avg:88.76ms lr_matrix:0.019446 +step:5400/20000 train_loss:2.0340 train_bpb:1.2050 train_time:479330ms step_avg:88.76ms lr_matrix:0.018114 +step:5500/20000 train_loss:2.0205 train_bpb:1.1950 train_time:488201ms step_avg:88.76ms lr_matrix:0.016783 +step:5600/20000 train_loss:2.0272 train_bpb:1.2010 train_time:497079ms step_avg:88.76ms lr_matrix:0.015451 +step:5700/20000 train_loss:2.0223 train_bpb:1.1913 train_time:505887ms step_avg:88.75ms lr_matrix:0.014130 +step:5800/20000 train_loss:2.0130 train_bpb:1.1965 train_time:514746ms step_avg:88.75ms lr_matrix:0.012801 +step:5900/20000 train_loss:2.0020 train_bpb:1.1849 train_time:523606ms step_avg:88.75ms lr_matrix:0.011472 +step:6000/20000 train_loss:1.9921 train_bpb:1.1767 train_time:532468ms step_avg:88.74ms lr_matrix:0.010143 +step:6100/20000 train_loss:1.9852 train_bpb:1.1716 train_time:541283ms step_avg:88.73ms lr_matrix:0.008821 +step:6200/20000 train_loss:1.9879 train_bpb:1.1732 train_time:550151ms step_avg:88.73ms lr_matrix:0.007491 +step:6300/20000 train_loss:2.0000 train_bpb:1.1811 train_time:559024ms step_avg:88.73ms lr_matrix:0.006160 +ema:start step:6358 progress:0.9401 +step:6400/20000 train_loss:1.9898 train_bpb:1.1771 train_time:569440ms step_avg:88.97ms lr_matrix:0.004602 +step:6500/20000 train_loss:1.9748 train_bpb:1.1642 train_time:581793ms step_avg:89.51ms lr_matrix:0.002749 +best_ckpt:save step:6552 train_loss:2.1322 train_bpb:1.2614 pool:1/10 +best_ckpt:save step:6553 train_loss:1.9306 train_bpb:1.1416 pool:2/10 +best_ckpt:save step:6554 train_loss:2.0013 train_bpb:1.1389 pool:3/10 +best_ckpt:save step:6555 train_loss:2.0720 train_bpb:1.2042 pool:4/10 +best_ckpt:save step:6556 train_loss:1.9347 train_bpb:1.1306 pool:5/10 +best_ckpt:save step:6557 train_loss:1.9654 train_bpb:1.1358 pool:6/10 +best_ckpt:save step:6558 train_loss:1.8720 train_bpb:1.0970 pool:7/10 +best_ckpt:save step:6559 train_loss:2.0301 train_bpb:1.1752 pool:8/10 +best_ckpt:save step:6560 train_loss:2.0760 train_bpb:1.2230 pool:9/10 +best_ckpt:save step:6561 train_loss:2.0116 train_bpb:1.1982 pool:10/10 +best_ckpt:save step:6562 train_loss:1.9961 train_bpb:1.1569 pool:10/10 +best_ckpt:save step:6563 train_loss:1.8393 train_bpb:1.0582 pool:10/10 +best_ckpt:save step:6564 train_loss:1.9506 train_bpb:1.1281 pool:10/10 +best_ckpt:save step:6565 train_loss:1.9496 train_bpb:1.1619 pool:10/10 +best_ckpt:save step:6566 train_loss:1.9307 train_bpb:1.1430 pool:10/10 +best_ckpt:save step:6567 train_loss:1.9168 train_bpb:1.1592 pool:10/10 +best_ckpt:save step:6574 train_loss:1.9231 train_bpb:1.1201 pool:10/10 +best_ckpt:save step:6575 train_loss:1.9267 train_bpb:1.1500 pool:10/10 +best_ckpt:save step:6578 train_loss:1.8764 train_bpb:1.0845 pool:10/10 +best_ckpt:save step:6579 train_loss:1.7551 train_bpb:1.0603 pool:10/10 +best_ckpt:save step:6580 train_loss:1.8481 train_bpb:1.0849 pool:10/10 +best_ckpt:save step:6581 train_loss:1.8700 train_bpb:1.0612 pool:10/10 +best_ckpt:save step:6582 train_loss:1.9534 train_bpb:1.1257 pool:10/10 +best_ckpt:save step:6589 train_loss:1.9427 train_bpb:1.1203 pool:10/10 +best_ckpt:save step:6594 train_loss:1.9457 train_bpb:1.1229 pool:10/10 +best_ckpt:save step:6595 train_loss:1.8674 train_bpb:1.1190 pool:10/10 +best_ckpt:save step:6598 train_loss:1.9128 train_bpb:1.0921 pool:10/10 +best_ckpt:save step:6599 train_loss:1.8539 train_bpb:1.0648 pool:10/10 +step:6600/20000 train_loss:1.9619 train_bpb:1.1573 train_time:595948ms step_avg:90.30ms lr_matrix:0.000626 +best_ckpt:save step:6605 train_loss:1.7757 train_bpb:1.0333 pool:10/10 +best_ckpt:save step:6609 train_loss:1.9137 train_bpb:1.1185 pool:10/10 +best_ckpt:save step:6616 train_loss:1.8840 train_bpb:1.0833 pool:10/10 +best_ckpt:save step:6617 train_loss:1.9057 train_bpb:1.0883 pool:10/10 +step:6630/20000 val_loss:1.9623 val_bpb:1.1622 train_time:600044ms step_avg:90.50ms +stopping_early: wallclock_cap train_time:600044ms step:6630/20000 +peak memory allocated: 20386 MiB reserved: 20486 MiB +candidate_eval:raw_final val_loss:1.9631 val_bpb:1.1627 eval_time:5897ms +candidate_eval:ema val_loss:1.9648 val_bpb:1.1637 eval_time:5853ms +candidate_eval:best_ckpt_step_6605 val_loss:1.9638 val_bpb:1.1631 eval_time:5850ms +candidate_eval:best_ckpt_step_6563 val_loss:1.9642 val_bpb:1.1633 eval_time:5849ms +candidate_eval:best_ckpt_step_6579 val_loss:1.9641 val_bpb:1.1633 eval_time:5849ms +candidate_eval:best_ckpt_step_6581 val_loss:1.9642 val_bpb:1.1633 eval_time:5845ms +candidate_eval:best_ckpt_step_6599 val_loss:1.9639 val_bpb:1.1631 eval_time:5844ms +candidate_eval:best_ckpt_step_6616 val_loss:1.9635 val_bpb:1.1629 eval_time:5843ms +candidate_eval:best_ckpt_step_6578 val_loss:1.9640 val_bpb:1.1632 eval_time:5854ms +candidate_eval:best_ckpt_step_6580 val_loss:1.9645 val_bpb:1.1635 eval_time:5823ms +candidate_eval:best_ckpt_step_6617 val_loss:1.9634 val_bpb:1.1628 eval_time:5824ms +candidate_eval:best_ckpt_step_6598 val_loss:1.9643 val_bpb:1.1634 eval_time:5823ms +candidate_eval:best_ckpt_avg val_loss:1.9635 val_bpb:1.1629 eval_time:5859ms +selected_state:raw_final val_loss:1.9631 val_bpb:1.1627 +Serialized model: 95544357 bytes +Code size: 80305 bytes +Total submission size: 95624662 bytes +quant_calib:val_tail sequences=32 seq_len=2048 +quant_calib:activation_stats_tensors=60 +Serialized model int6_awq+lzma:final_model.int6_awq.ptz bytes:15262900 (payload:24365378 raw_torch:24432511 payload_ratio:3.92x) +Total submission size int6_awq+lzma: 15343205 bytes +final_int6_awq_lzma_val_tail_roundtrip val_loss:2.0376 val_bpb:1.2068 eval_time:5842ms +final_int6_awq_lzma_val_tail_roundtrip_exact val_loss:2.03757525 val_bpb:1.20676683 +sliding_window [10016/121136] bpb=1.188318 time=15.8s +sliding_window [20000/121136] bpb=1.189627 time=31.6s +sliding_window [30016/121136] bpb=1.194229 time=47.4s +sliding_window [40000/121136] bpb=1.190332 time=63.1s +sliding_window [50016/121136] bpb=1.189316 time=79.0s +sliding_window [60000/121136] bpb=1.185407 time=94.7s +sliding_window [70016/121136] bpb=1.184273 time=110.5s +sliding_window [80000/121136] bpb=1.184837 time=126.3s +sliding_window [90016/121136] bpb=1.188293 time=142.1s +sliding_window [100000/121136] bpb=1.189974 time=157.9s +sliding_window [110016/121136] bpb=1.189148 time=173.7s +sliding_window [120000/121136] bpb=1.189353 time=189.4s +sliding_window [121136/121136] bpb=1.189722 time=191.2s +final_int6_awq_lzma_val_tail_sliding_window val_loss:1.9985 val_bpb:1.1836 stride:64 eval_time:191673ms +final_int6_awq_lzma_val_tail_sliding_window_exact val_loss:1.99848601 val_bpb:1.18361912 diff --git a/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ sdpa_fallback.txt b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ sdpa_fallback.txt new file mode 100644 index 0000000000..a8a762be32 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/log_ sdpa_fallback.txt @@ -0,0 +1,232 @@ +==================================================================================================== +Running Python 3.12.3 (main, Nov 6 2025, 13:44:16) [GCC 13.3.0] +Running PyTorch 2.9.1+cu128 +Tue Apr 7 17:19:47 2026 ++-----------------------------------------------------------------------------------------+ +| NVIDIA-SMI 580.126.09 Driver Version: 580.126.09 CUDA Version: 13.0 | ++-----------------------------------------+------------------------+----------------------+ +| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC | +| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. | +| | | MIG M. | +|=========================================+========================+======================| +| 0 NVIDIA H100 80GB HBM3 On | 00000000:19:00.0 Off | 0 | +| N/A 44C P0 127W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 1 NVIDIA H100 80GB HBM3 On | 00000000:3B:00.0 Off | 0 | +| N/A 36C P0 118W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 2 NVIDIA H100 80GB HBM3 On | 00000000:4C:00.0 Off | 0 | +| N/A 34C P0 117W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 3 NVIDIA H100 80GB HBM3 On | 00000000:5D:00.0 Off | 0 | +| N/A 43C P0 119W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 4 NVIDIA H100 80GB HBM3 On | 00000000:9B:00.0 Off | 0 | +| N/A 45C P0 127W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 5 NVIDIA H100 80GB HBM3 On | 00000000:BB:00.0 Off | 0 | +| N/A 36C P0 121W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 6 NVIDIA H100 80GB HBM3 On | 00000000:CB:00.0 Off | 0 | +| N/A 43C P0 125W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ +| 7 NVIDIA H100 80GB HBM3 On | 00000000:DB:00.0 Off | 0 | +| N/A 36C P0 122W / 700W | 1521MiB / 81559MiB | 0% Default | +| | | Disabled | ++-----------------------------------------+------------------------+----------------------+ + ++-----------------------------------------------------------------------------------------+ +| Processes: | +| GPU GI CI PID Type Process name GPU Memory | +| ID ID Usage | +|=========================================================================================| +| No running processes found | ++-----------------------------------------------------------------------------------------+ + +==================================================================================================== +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 +model_params:24140369 +world_size:8 grad_accum_steps:1 +sdp_backends:cudnn=False flash=True mem_efficient=False math=False +attention_backend:sdpa_fallback +attention_mode:gqa num_heads:8 num_kv_heads:4 +tie_embeddings:True embed_lr:0.056249999999999994 head_lr:0.0 matrix_lr:0.045 scalar_lr:0.045 +init_scales:attn_scale=(early=1.0,mid=1.75,late=2.5) mlp_scale=(early=1.0,mid=1.15,late=1.3) +adam_wd:0.0100 muon_wd:0.0100 +train_batch_tokens:786432 train_seq_len:2048 iterations:20000 warmup_steps:20 warmdown_start:0.750 max_wallclock_seconds:600.000 +lr_scheduler:warmdown piecewise_matrix_lrs:- +seed:1337 +quant_export:int6_awq val_tail_calib quant_calib_seqs:32 quant_awq_alpha:0.500 +val_sliding_stride:64 val_sliding_batch_seqs:32 val_sliding_seq_len:0 val_sliding_log_every:10000 +ema_enabled:1 ema_decay:0.9970 ema_start:0.940 best_ckpt_k:10 best_ckpt_start:0.980 +warmup_step:1/20 +warmup_step:2/20 +warmup_step:3/20 +warmup_step:4/20 +warmup_step:5/20 +warmup_step:6/20 +warmup_step:7/20 +warmup_step:8/20 +warmup_step:9/20 +warmup_step:10/20 +warmup_step:11/20 +warmup_step:12/20 +warmup_step:13/20 +warmup_step:14/20 +warmup_step:15/20 +warmup_step:16/20 +warmup_step:17/20 +warmup_step:18/20 +warmup_step:19/20 +warmup_step:20/20 +step:1/20000 train_loss:6.9303 train_bpb:4.0923 train_time:125ms step_avg:125.22ms lr_matrix:0.045000 +step:2/20000 train_loss:18.5854 train_bpb:11.2765 train_time:223ms step_avg:111.67ms lr_matrix:0.044991 +step:3/20000 train_loss:8.9135 train_bpb:5.3429 train_time:322ms step_avg:107.20ms lr_matrix:0.044985 +step:4/20000 train_loss:6.4042 train_bpb:3.8000 train_time:420ms step_avg:105.09ms lr_matrix:0.044978 +step:5/20000 train_loss:6.2315 train_bpb:3.5541 train_time:519ms step_avg:103.76ms lr_matrix:0.044971 +step:6/20000 train_loss:6.2105 train_bpb:3.5638 train_time:617ms step_avg:102.86ms lr_matrix:0.044964 +step:7/20000 train_loss:6.0996 train_bpb:3.5950 train_time:715ms step_avg:102.21ms lr_matrix:0.044957 +step:8/20000 train_loss:6.0182 train_bpb:3.5240 train_time:814ms step_avg:101.76ms lr_matrix:0.044950 +step:9/20000 train_loss:5.9222 train_bpb:3.4228 train_time:912ms step_avg:101.33ms lr_matrix:0.044944 +step:10/20000 train_loss:5.8131 train_bpb:3.4351 train_time:1010ms step_avg:101.02ms lr_matrix:0.044937 +step:100/20000 train_loss:4.3462 train_bpb:2.5655 train_time:9885ms step_avg:98.85ms lr_matrix:0.044327 +step:200/20000 train_loss:2.9068 train_bpb:1.7210 train_time:19804ms step_avg:99.02ms lr_matrix:0.043655 +step:300/20000 train_loss:2.5532 train_bpb:1.5065 train_time:29752ms step_avg:99.17ms lr_matrix:0.042991 +step:400/20000 train_loss:2.4501 train_bpb:1.4381 train_time:39746ms step_avg:99.37ms lr_matrix:0.042334 +step:500/20000 train_loss:2.3733 train_bpb:1.4018 train_time:49725ms step_avg:99.45ms lr_matrix:0.041688 +step:600/20000 train_loss:2.3347 train_bpb:1.3849 train_time:59747ms step_avg:99.58ms lr_matrix:0.041050 +step:700/20000 train_loss:2.3018 train_bpb:1.3617 train_time:69807ms step_avg:99.72ms lr_matrix:0.040419 +step:800/20000 train_loss:2.2552 train_bpb:1.3264 train_time:79866ms step_avg:99.83ms lr_matrix:0.039797 +step:900/20000 train_loss:2.2374 train_bpb:1.3146 train_time:89911ms step_avg:99.90ms lr_matrix:0.039186 +step:1000/20000 train_loss:2.2306 train_bpb:1.3162 train_time:99892ms step_avg:99.89ms lr_matrix:0.038588 +step:1100/20000 train_loss:2.2263 train_bpb:1.3205 train_time:109950ms step_avg:99.95ms lr_matrix:0.037995 +step:1200/20000 train_loss:2.2094 train_bpb:1.3056 train_time:119993ms step_avg:99.99ms lr_matrix:0.037412 +step:1300/20000 train_loss:2.2106 train_bpb:1.3107 train_time:130026ms step_avg:100.02ms lr_matrix:0.036838 +step:1400/20000 train_loss:2.1735 train_bpb:1.2890 train_time:140060ms step_avg:100.04ms lr_matrix:0.036274 +step:1500/20000 train_loss:2.1508 train_bpb:1.2718 train_time:150043ms step_avg:100.03ms lr_matrix:0.035720 +step:1600/20000 train_loss:2.1471 train_bpb:1.2628 train_time:160070ms step_avg:100.04ms lr_matrix:0.035172 +step:1700/20000 train_loss:2.1531 train_bpb:1.2759 train_time:170104ms step_avg:100.06ms lr_matrix:0.034633 +step:1800/20000 train_loss:2.1442 train_bpb:1.2732 train_time:180127ms step_avg:100.07ms lr_matrix:0.034102 +step:1900/20000 train_loss:2.1349 train_bpb:1.2634 train_time:190074ms step_avg:100.04ms lr_matrix:0.033584 +step:2000/20000 train_loss:2.1310 train_bpb:1.2605 train_time:200089ms step_avg:100.04ms lr_matrix:0.033070 +step:2100/20000 train_loss:2.1209 train_bpb:1.2536 train_time:210089ms step_avg:100.04ms lr_matrix:0.032564 +step:2200/20000 train_loss:2.1044 train_bpb:1.2398 train_time:220112ms step_avg:100.05ms lr_matrix:0.032065 +step:2300/20000 train_loss:2.0944 train_bpb:1.2373 train_time:230129ms step_avg:100.06ms lr_matrix:0.031574 +step:2400/20000 train_loss:2.1004 train_bpb:1.2371 train_time:240056ms step_avg:100.02ms lr_matrix:0.031095 +step:2500/20000 train_loss:2.1029 train_bpb:1.2405 train_time:250077ms step_avg:100.03ms lr_matrix:0.030619 +step:2600/20000 train_loss:2.1075 train_bpb:1.2488 train_time:260079ms step_avg:100.03ms lr_matrix:0.030151 +step:2700/20000 train_loss:2.0857 train_bpb:1.2343 train_time:270087ms step_avg:100.03ms lr_matrix:0.029689 +step:2800/20000 train_loss:2.0771 train_bpb:1.2292 train_time:280105ms step_avg:100.04ms lr_matrix:0.029235 +step:2900/20000 train_loss:2.0786 train_bpb:1.2230 train_time:290029ms step_avg:100.01ms lr_matrix:0.028791 +step:3000/20000 train_loss:2.0792 train_bpb:1.2236 train_time:300014ms step_avg:100.00ms lr_matrix:0.028352 +step:3100/20000 train_loss:2.0598 train_bpb:1.2147 train_time:310014ms step_avg:100.00ms lr_matrix:0.027919 +step:3200/20000 train_loss:2.0571 train_bpb:1.2105 train_time:320006ms step_avg:100.00ms lr_matrix:0.027492 +step:3300/20000 train_loss:2.0625 train_bpb:1.2175 train_time:329968ms step_avg:99.99ms lr_matrix:0.027074 +step:3400/20000 train_loss:2.0686 train_bpb:1.2278 train_time:339999ms step_avg:100.00ms lr_matrix:0.026658 +step:3500/20000 train_loss:2.0642 train_bpb:1.2206 train_time:350010ms step_avg:100.00ms lr_matrix:0.026250 +step:3600/20000 train_loss:2.0467 train_bpb:1.2069 train_time:360008ms step_avg:100.00ms lr_matrix:0.025849 +step:3700/20000 train_loss:2.0338 train_bpb:1.1952 train_time:370016ms step_avg:100.00ms lr_matrix:0.025454 +step:3800/20000 train_loss:2.0463 train_bpb:1.2087 train_time:379968ms step_avg:99.99ms lr_matrix:0.025067 +step:3900/20000 train_loss:2.0626 train_bpb:1.2212 train_time:389976ms step_avg:99.99ms lr_matrix:0.024683 +step:4000/20000 train_loss:2.0654 train_bpb:1.2163 train_time:399964ms step_avg:99.99ms lr_matrix:0.024306 +step:4100/20000 train_loss:2.0545 train_bpb:1.2162 train_time:409967ms step_avg:99.99ms lr_matrix:0.023935 +step:4200/20000 train_loss:2.0557 train_bpb:1.2148 train_time:419970ms step_avg:99.99ms lr_matrix:0.023569 +step:4300/20000 train_loss:2.0344 train_bpb:1.2057 train_time:429902ms step_avg:99.98ms lr_matrix:0.023211 +step:4400/20000 train_loss:2.0379 train_bpb:1.2114 train_time:439892ms step_avg:99.98ms lr_matrix:0.022857 +step:4500/20000 train_loss:2.0368 train_bpb:1.2024 train_time:449872ms step_avg:99.97ms lr_matrix:0.022508 +step:4600/20000 train_loss:2.0249 train_bpb:1.1931 train_time:459850ms step_avg:99.97ms lr_matrix:0.021037 +step:4700/20000 train_loss:2.0193 train_bpb:1.1925 train_time:469774ms step_avg:99.95ms lr_matrix:0.019549 +step:4800/20000 train_loss:2.0304 train_bpb:1.1995 train_time:479746ms step_avg:99.95ms lr_matrix:0.018053 +step:4900/20000 train_loss:2.0215 train_bpb:1.1928 train_time:489749ms step_avg:99.95ms lr_matrix:0.016553 +step:5000/20000 train_loss:2.0177 train_bpb:1.1914 train_time:499757ms step_avg:99.95ms lr_matrix:0.015052 +step:5100/20000 train_loss:2.0159 train_bpb:1.1955 train_time:509772ms step_avg:99.96ms lr_matrix:0.013549 +step:5200/20000 train_loss:2.0108 train_bpb:1.1846 train_time:519720ms step_avg:99.95ms lr_matrix:0.012057 +step:5300/20000 train_loss:2.0057 train_bpb:1.1858 train_time:529761ms step_avg:99.95ms lr_matrix:0.010551 +step:5400/20000 train_loss:2.0121 train_bpb:1.1920 train_time:539782ms step_avg:99.96ms lr_matrix:0.009048 +step:5500/20000 train_loss:1.9981 train_bpb:1.1818 train_time:549830ms step_avg:99.97ms lr_matrix:0.007540 +step:5600/20000 train_loss:2.0043 train_bpb:1.1874 train_time:559823ms step_avg:99.97ms lr_matrix:0.006041 +ema:start step:5644 progress:0.9402 +step:5700/20000 train_loss:1.9987 train_bpb:1.1775 train_time:571802ms step_avg:100.32ms lr_matrix:0.004249 +step:5800/20000 train_loss:1.9883 train_bpb:1.1818 train_time:585308ms step_avg:100.92ms lr_matrix:0.002224 +best_ckpt:save step:5822 train_loss:1.9279 train_bpb:1.1267 pool:1/10 +best_ckpt:save step:5823 train_loss:2.0054 train_bpb:1.1697 pool:2/10 +best_ckpt:save step:5824 train_loss:1.9093 train_bpb:1.1184 pool:3/10 +best_ckpt:save step:5825 train_loss:1.9809 train_bpb:1.1881 pool:4/10 +best_ckpt:save step:5826 train_loss:2.0033 train_bpb:1.1679 pool:5/10 +best_ckpt:save step:5827 train_loss:1.9148 train_bpb:1.1490 pool:6/10 +best_ckpt:save step:5828 train_loss:1.9504 train_bpb:1.1538 pool:7/10 +best_ckpt:save step:5829 train_loss:1.6998 train_bpb:1.0544 pool:8/10 +best_ckpt:save step:5830 train_loss:2.2133 train_bpb:1.2921 pool:9/10 +best_ckpt:save step:5831 train_loss:1.9645 train_bpb:1.1837 pool:10/10 +best_ckpt:save step:5832 train_loss:1.9751 train_bpb:1.1817 pool:10/10 +best_ckpt:save step:5834 train_loss:1.9811 train_bpb:1.1840 pool:10/10 +best_ckpt:save step:5835 train_loss:1.9835 train_bpb:1.1543 pool:10/10 +best_ckpt:save step:5837 train_loss:1.9358 train_bpb:1.1398 pool:10/10 +best_ckpt:save step:5838 train_loss:1.9678 train_bpb:1.1665 pool:10/10 +best_ckpt:save step:5841 train_loss:1.9048 train_bpb:1.1217 pool:10/10 +best_ckpt:save step:5842 train_loss:1.8890 train_bpb:1.0905 pool:10/10 +best_ckpt:save step:5843 train_loss:1.9951 train_bpb:1.1440 pool:10/10 +best_ckpt:save step:5844 train_loss:1.9939 train_bpb:1.1454 pool:10/10 +best_ckpt:save step:5845 train_loss:1.9391 train_bpb:1.1250 pool:10/10 +best_ckpt:save step:5846 train_loss:1.9376 train_bpb:1.1476 pool:10/10 +best_ckpt:save step:5848 train_loss:1.9561 train_bpb:1.1452 pool:10/10 +best_ckpt:save step:5849 train_loss:1.7858 train_bpb:1.0991 pool:10/10 +best_ckpt:save step:5853 train_loss:1.8802 train_bpb:1.1117 pool:10/10 +best_ckpt:save step:5858 train_loss:1.9386 train_bpb:1.1291 pool:10/10 +best_ckpt:save step:5860 train_loss:1.9478 train_bpb:1.1382 pool:10/10 +best_ckpt:save step:5861 train_loss:1.9441 train_bpb:1.1269 pool:10/10 +best_ckpt:save step:5862 train_loss:1.8068 train_bpb:1.1021 pool:10/10 +best_ckpt:save step:5878 train_loss:1.9465 train_bpb:1.1176 pool:10/10 +best_ckpt:save step:5883 train_loss:1.9082 train_bpb:1.0962 pool:10/10 +best_ckpt:save step:5885 train_loss:1.8521 train_bpb:1.0720 pool:10/10 +best_ckpt:save step:5886 train_loss:1.7523 train_bpb:1.1032 pool:10/10 +step:5892/20000 val_loss:1.9689 val_bpb:1.1661 train_time:600097ms step_avg:101.85ms +stopping_early: wallclock_cap train_time:600097ms step:5892/20000 +peak memory allocated: 18701 MiB reserved: 18822 MiB +candidate_eval:raw_final val_loss:1.9697 val_bpb:1.1666 eval_time:5644ms +candidate_eval:ema val_loss:1.9718 val_bpb:1.1678 eval_time:5583ms +candidate_eval:best_ckpt_step_5829 val_loss:1.9708 val_bpb:1.1672 eval_time:5582ms +candidate_eval:best_ckpt_step_5885 val_loss:1.9704 val_bpb:1.1670 eval_time:6026ms +candidate_eval:best_ckpt_step_5842 val_loss:1.9708 val_bpb:1.1672 eval_time:5582ms +candidate_eval:best_ckpt_step_5883 val_loss:1.9702 val_bpb:1.1669 eval_time:5585ms +candidate_eval:best_ckpt_step_5849 val_loss:1.9704 val_bpb:1.1670 eval_time:5583ms +candidate_eval:best_ckpt_step_5862 val_loss:1.9702 val_bpb:1.1669 eval_time:5582ms +candidate_eval:best_ckpt_step_5886 val_loss:1.9705 val_bpb:1.1670 eval_time:5582ms +candidate_eval:best_ckpt_step_5853 val_loss:1.9702 val_bpb:1.1669 eval_time:5598ms +candidate_eval:best_ckpt_step_5878 val_loss:1.9707 val_bpb:1.1671 eval_time:5584ms +candidate_eval:best_ckpt_step_5824 val_loss:1.9711 val_bpb:1.1674 eval_time:5584ms +candidate_eval:best_ckpt_avg val_loss:1.9700 val_bpb:1.1668 eval_time:5622ms +selected_state:raw_final val_loss:1.9697 val_bpb:1.1666 +Serialized model: 95544357 bytes +Code size: 79455 bytes +Total submission size: 95623812 bytes +quant_calib:val_tail sequences=32 seq_len=2048 +quant_calib:activation_stats_tensors=60 +Serialized model int6_awq+lzma:final_model.int6_awq.ptz bytes:15282216 (payload:24365378 raw_torch:24432511 payload_ratio:3.92x) +Total submission size int6_awq+lzma: 15361671 bytes +final_int6_awq_lzma_val_tail_roundtrip val_loss:2.0370 val_bpb:1.2064 eval_time:5601ms +final_int6_awq_lzma_val_tail_roundtrip_exact val_loss:2.03699998 val_bpb:1.20642612 +sliding_window [10016/121136] bpb=1.187982 time=15.2s +sliding_window [20000/121136] bpb=1.189055 time=30.4s +sliding_window [30016/121136] bpb=1.193855 time=45.6s +sliding_window [40000/121136] bpb=1.189983 time=60.7s +sliding_window [50016/121136] bpb=1.188688 time=75.9s +sliding_window [60000/121136] bpb=1.184771 time=91.1s +sliding_window [70016/121136] bpb=1.183673 time=106.3s +sliding_window [80000/121136] bpb=1.184342 time=121.5s +sliding_window [90016/121136] bpb=1.187730 time=136.7s +sliding_window [100000/121136] bpb=1.189371 time=151.8s +sliding_window [110016/121136] bpb=1.188600 time=167.0s +sliding_window [120000/121136] bpb=1.188860 time=182.2s +sliding_window [121136/121136] bpb=1.189246 time=183.9s +final_int6_awq_lzma_val_tail_sliding_window val_loss:1.9981 val_bpb:1.1834 stride:64 eval_time:184282ms +final_int6_awq_lzma_val_tail_sliding_window_exact val_loss:1.99807565 val_bpb:1.18337608 \ No newline at end of file diff --git a/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/submission.json b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/submission.json new file mode 100644 index 0000000000..5d49a3f2d9 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/submission.json @@ -0,0 +1,11 @@ +{ + "author": "Yevhen Shramov", + "github_id": "shram86", + "name": "FlashMuon + LinearScaleInit + XSA5LastGated + RReLU2 + Int6AWQ", + "blurb": "10-layer SP-1024 run with Flash Muon, XSA on the last 5 layers with only the final XSA layer gated, linear-by-depth scale init, RReLU2 MLP, late EMA plus post-train best-choice selection, and int6 AWQ + lzma with val-tail calibration. Final reported score uses stride-64 sliding-window evaluation at 1.1834 bpb; the default contiguous roundtrip score is 1.2064 bpb. Total submission size is 15,361,671 bytes.", + "date": "2026-04-07T00:00:00Z", + "val_loss": 1.99807565, + "val_bpb": 1.18337608, + "bytes_total": 15361671, + "bytes_code": 47642 +} diff --git a/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/train_gpt.py b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/train_gpt.py new file mode 100644 index 0000000000..df75b8d022 --- /dev/null +++ b/records/track_non_record_16mb/2026-04-07_FlashMuon_LinearScaleInit_XSA5LastGated_RReLU2_Int6AWQ/train_gpt.py @@ -0,0 +1,1834 @@ +""" +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 +import lzma +from pathlib import Path + +import numpy as np +import sentencepiece as spm +import torch +import torch.distributed as dist +import torch.nn.functional as F +from torch import Tensor, nn +from torch.nn.parallel import DistributedDataParallel as DDP + +import triton +import triton.language as tl + +try: + from flash_attn_interface import flash_attn_func as flash_attn_3_func +except ImportError: + flash_attn_3_func = None + +def _flash_attn_3_enabled() -> bool: + if flash_attn_3_func is None or not torch.cuda.is_available(): + return False + try: + major, _minor = torch.cuda.get_device_capability() + except Exception: + return False + return major >= 9 + + +FLASH_ATTN_3_ENABLED = _flash_attn_3_enabled() +# ----------------------------- +# 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", 0)) + train_log_every = int(os.environ.get("TRAIN_LOG_EVERY", 100)) + + # Training length. + iterations = int(os.environ.get("ITERATIONS", 20000)) + warmdown_start = float(os.environ.get("WARMDOWN_START", 0.75)) + warmup_steps = int(os.environ.get("WARMUP_STEPS", 20)) + lr_scheduler = os.environ.get("LR_SCHEDULER", "warmdown") + piecewise_matrix_lrs = os.environ.get("PIECEWISE_MATRIX_LRS", "") + train_batch_tokens = int(os.environ.get("TRAIN_BATCH_TOKENS", 786_432)) + train_seq_len = int(os.environ.get("TRAIN_SEQ_LEN", 2048)) + max_wallclock_seconds = float(os.environ.get("MAX_WALLCLOCK_SECONDS", 600.0)) + qk_gain_init = float(os.environ.get("QK_GAIN_INIT", 1.5)) + + # Model shape. + vocab_size = int(os.environ.get("VOCAB_SIZE", 1024)) + num_layers = int(os.environ.get("NUM_LAYERS", 10)) + 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", 3)) + 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)) + + + matrix_lr = float(os.environ.get("MATRIX_LR", 0.045)) + + embed_lr = matrix_lr * 15.0 + tied_embed_lr = matrix_lr * 1.25 + scalar_lr = matrix_lr + head_lr = matrix_lr * 0.2 + + + tied_embed_init_std = float(os.environ.get("TIED_EMBED_INIT_STD", 0.001)) + + muon_momentum = float(os.environ.get("MUON_MOMENTUM", 0.97)) + muon_backend_steps = int(os.environ.get("MUON_BACKEND_STEPS", 5)) + muon_momentum_warmup_start = float(os.environ.get("MUON_MOMENTUM_WARMUP_START", 0.88)) + muon_momentum_warmup_steps = int(os.environ.get("MUON_MOMENTUM_WARMUP_STEPS", 1200)) + muon_wd = float(os.environ.get("MUON_WD", 0.01)) + 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)) + adam_wd = float(os.environ.get("ADAM_WD", 0.01)) + grad_clip_norm = float(os.environ.get("GRAD_CLIP_NORM", 0.0)) + ema_enabled = bool(int(os.environ.get("EMA_ENABLED", "1"))) + ema_decay = float(os.environ.get("EMA_DECAY", 0.997)) + ema_start = float(os.environ.get("EMA_START", 0.94)) + best_ckpt_k = int(os.environ.get("BEST_CKPT_K", 10)) + best_ckpt_start = float(os.environ.get("BEST_CKPT_START", 0.98)) + + # Export path is fixed: int6 AWQ with validation-tail calibration (see quant section). + quant_calib_seqs = int(os.environ.get("QUANT_CALIB_SEQS", 32)) + quant_awq_alpha = float(os.environ.get("QUANT_AWQ_ALPHA", 0.5)) + quant_col_scale_clamp_min = float(os.environ.get("QUANT_COL_SCALE_CLAMP_MIN", 0.5)) + quant_col_scale_clamp_max = float(os.environ.get("QUANT_COL_SCALE_CLAMP_MAX", 2.0)) + quant_col_scale_max_ratio = float(os.environ.get("QUANT_COL_SCALE_MAX_RATIO", 4.0)) + val_sliding_stride = int(os.environ.get("VAL_SLIDING_STRIDE", 64)) + val_sliding_batch_seqs = int(os.environ.get("VAL_SLIDING_BATCH_SEQS", 32)) + val_sliding_seq_len = int(os.environ.get("VAL_SLIDING_SEQ_LEN", 0)) + val_sliding_log_every = int(os.environ.get("VAL_SLIDING_LOG_EVERY", 10000)) + +def fast_newtonschulz(G: Tensor, steps: int = 5, eps: float = 1e-7) -> Tensor: + assert G.ndim >= 2 + a, b, c = (3.4445, -4.7750, 2.0315) + was_2d = G.ndim == 2 + if was_2d: + G = G.unsqueeze(0) + X = G.bfloat16() + transposed = X.size(-2) > X.size(-1) + if transposed: + X = X.mT + X = X / (X.norm(dim=(-2, -1), keepdim=True) + eps) + for _ in range(steps): + if matmul_transpose_assign is not None and X.is_cuda and X.ndim == 3 and X.size(0) == 1: + x2 = X.squeeze(0) + buf1 = torch.empty(x2.size(0), x2.size(0), dtype=x2.dtype, device=x2.device) + buf2 = torch.empty(x2.size(0), x2.size(0), dtype=x2.dtype, device=x2.device) + matmul_transpose_assign(x2, buf1) + matmul_transpose_assign(buf1, buf2) + A = buf1.unsqueeze(0) + B = b * A + c * buf2.unsqueeze(0) + else: + A = X @ X.mT + B = b * A + c * (A @ A) + X = a * X + B @ X + if transposed: + X = X.mT + if was_2d: + X = X.squeeze(0) + return X + +# ----------------------------- +# MUON OPTIMIZER +# ----------------------------- +# +# As borrowed from modded-nanogpt +# Background on Muon: https://kellerjordan.github.io/posts/muon/ + +def _get_triton_autotune_config(): + return [ + triton.Config({"BLOCK_SIZE_M": blk_m, "BLOCK_SIZE_K": blk_k, "GROUP_SIZE_M": grp_sz}, num_stages=n_stages, num_warps=n_warps) + for blk_m in [32, 64, 128] + for blk_k in [32, 64] + for grp_sz in [8] + for n_stages in [3, 4, 5] + for n_warps in [4, 8] + ] + +@triton.autotune(configs=_get_triton_autotune_config(), key=["M", "K"]) +@triton.jit +def _mmt_kernel( + x, y, + M, K, + stride_xm, stride_xk, + stride_ym, stride_yn, + BLOCK_SIZE_M: tl.constexpr, + BLOCK_SIZE_K: tl.constexpr, + GROUP_SIZE_M: tl.constexpr, + ): + pid = tl.program_id(axis=0) + num_pid_m = tl.cdiv(M, BLOCK_SIZE_M) + num_pid_n = tl.cdiv(M, BLOCK_SIZE_M) + num_pid_in_group = GROUP_SIZE_M * num_pid_n + group_id = pid // num_pid_in_group + first_pid_m = group_id * GROUP_SIZE_M + group_size_m = min(num_pid_m - first_pid_m, GROUP_SIZE_M) + pid_m = first_pid_m + ((pid % num_pid_in_group) % group_size_m) + pid_n = (pid % num_pid_in_group) // group_size_m + if pid_m > pid_n: + return + offs_xm = (pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M + offs_xn = (pid_n * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M)) % M + offs_k = tl.arange(0, BLOCK_SIZE_K) + a_ptrs = x + (offs_xm[:, None] * stride_xm + offs_k[None, :] * stride_xk) + b_ptrs = x + (offs_xn[:, None] * stride_xm + offs_k[None, :] * stride_xk) + accumulator = tl.zeros((BLOCK_SIZE_M, BLOCK_SIZE_M), dtype=tl.float32) + for _ in range(0, tl.cdiv(K, BLOCK_SIZE_K)): + a = tl.load(a_ptrs, mask=offs_k[None, :] < K, other=0.0) + b = tl.load(b_ptrs, mask=offs_k[None, :] < K, other=0.0) + accumulator = tl.dot(a, tl.permute(b, (1, 0)), accumulator) + a_ptrs += BLOCK_SIZE_K * stride_xk + b_ptrs += BLOCK_SIZE_K * stride_xk + offs_k += BLOCK_SIZE_K + c = accumulator.to(x.dtype.element_ty) + offs_cm = pid_m * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) + offs_cn = pid_n * BLOCK_SIZE_M + tl.arange(0, BLOCK_SIZE_M) + c_ptrs = y + stride_ym * offs_cm[:, None] + stride_yn * offs_cn[None, :] + c_mask = (offs_cm[:, None] < M) & (offs_cn[None, :] < M) + tl.store(c_ptrs, c, mask=c_mask) + if pid_m < pid_n: + ct_ptrs = y + stride_ym * offs_cn[:, None] + stride_yn * offs_cm[None, :] + ct_mask = (offs_cn[:, None] < M) & (offs_cm[None, :] < M) + tl.store(ct_ptrs, tl.permute(c, (1, 0)), mask=ct_mask) + +def matmul_transpose_assign(d_in: Tensor, d_out: Tensor) -> None: + d_in = d_in.contiguous() + M, K = d_in.shape + grid = lambda META: (triton.cdiv(M, META["BLOCK_SIZE_M"]) * triton.cdiv(M, META["BLOCK_SIZE_M"]),) + with torch.cuda.device(d_in.device.index): + _mmt_kernel[grid](d_in, d_out, M, K, d_in.stride(0), d_in.stride(1), d_out.stride(0), d_out.stride(1)) + + +class Muon(torch.optim.Optimizer): + def __init__( + self, + params, + lr: float = 0.02, + weight_decay: float = 0.0, + momentum: float = 0.95, + nesterov: bool = True, + backend_steps: int = 5, + rank: int | None = None, + world_size: int | None = None, + ): + if rank is None or world_size is None: + raise ValueError("rank and world_size are required; pass rank=0 and world_size=1 on single GPU") + self.rank = rank + self.world_size = world_size + defaults = dict( + lr=lr, + weight_decay=weight_decay, + momentum=momentum, + nesterov=nesterov, + backend_steps=backend_steps, + ) + params = [*params] + param_groups = [] + if params and isinstance(params[0], dict): + for group in params: + bucket = list(group.get("params", [])) + if not bucket: + continue + by_size: dict[int, list[Tensor]] = {} + for param in bucket: + by_size.setdefault(param.numel(), []).append(param) + for size in sorted(by_size): + sized_bucket = by_size[size] + device = sized_bucket[0].device + update_buffer = torch.empty(world_size, size, dtype=torch.bfloat16, device=device) + merged = dict(group) + merged["params"] = sized_bucket + merged["update_buffer"] = update_buffer + merged["update_buffer_views"] = [update_buffer[i] for i in range(world_size)] + param_groups.append(merged) + else: + for size in sorted({param.numel() for param in params}): + bucket = [param for param in params if param.numel() == size] + if not bucket: + continue + device = bucket[0].device + update_buffer = torch.empty(world_size, size, dtype=torch.bfloat16, device=device) + param_groups.append( + dict( + params=bucket, + update_buffer=update_buffer, + update_buffer_views=[update_buffer[i] for i in range(world_size)], + ) + ) + super().__init__(param_groups, defaults) + + @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() and self.world_size > 1 + + for group in self.param_groups: + params = group["params"] + if not params: + continue + update_buffer: Tensor = group["update_buffer"] + update_buffer_views: list[Tensor] = group["update_buffer_views"] + lr = group["lr"] + weight_decay = group.get("weight_decay", 0.0) + momentum = group["momentum"] + nesterov = group["nesterov"] + backend_steps = group["backend_steps"] + + handle = None + params_world = None + + def update_prev(): + if params_world is None: + return + if distributed: + if handle is None: + return + handle.wait() + for p_world, g_world in zip(params_world, update_buffer_views): + p_world.mul_(1 - lr * weight_decay) + p_world.add_( + g_world.view_as(p_world).to(dtype=p_world.dtype), + alpha=-lr * max(1, p_world.size(-2) / p_world.size(-1)) ** 0.5, + ) + + for base_i in range(0, len(params), self.world_size): + # Must run before overwriting update_buffer_views: previous iter left the + # correct g in the buffer for params_world from the end of that iter. + if base_i > 0: + update_prev() + + local_i = base_i + self.rank + if local_i < len(params): + p = params[local_i] + g = p.grad + if g is None: + update_buffer_views[self.rank].zero_() + else: + state = self.state[p] + if "momentum_buffer" not in state: + state["momentum_buffer"] = torch.zeros_like(g) + buf: Tensor = state["momentum_buffer"] + buf.mul_(momentum).add_(g) + g = g.add(buf, alpha=momentum) if nesterov else buf + if g.ndim == 4: + g = g.view(len(g), -1) + g = fast_newtonschulz(g, steps=backend_steps) + update_buffer_views[self.rank].copy_(g.flatten().to(dtype=torch.bfloat16)) + else: + update_buffer_views[self.rank].zero_() + + if distributed: + handle = dist.all_gather_into_tensor(update_buffer, update_buffer_views[self.rank], async_op=True) + else: + handle = None + params_world = params[base_i : base_i + self.world_size] + + update_prev() + + 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 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) + + +def sliding_window_evaluation( + args: Hyperparameters, + model: nn.Module, + rank: int, + world_size: int, + device: torch.device, + val_tokens: Tensor, + lut_bb: Tensor, + lut_lspace: Tensor, + lut_isbtoken: Tensor, + stride: int, + batch_seqs: int = 32, + eval_seq_len: int | None = None, + log_fn=None, + log_prefix: str = "sliding_eval", + log_every: int = 100, +) -> tuple[float, float]: + """Sliding-window evaluation that scores only the newly introduced suffix of each window.""" + if stride <= 0: + raise ValueError(f"VAL_SLIDING_STRIDE must be positive, got {stride}") + seq_len = eval_seq_len or args.train_seq_len + if seq_len <= 0: + raise ValueError(f"eval_seq_len must be positive, got {seq_len}") + total_tokens = val_tokens.numel() - 1 + win_start = [ws for ws in range(0, total_tokens, stride) if min(ws + seq_len, total_tokens) - ws >= 1] + total_wins = len(win_start) + _s = (total_wins * rank) // world_size + _e = (total_wins * (rank + 1)) // world_size + wins = win_start[_s:_e] + + loss_sum = torch.zeros((), device=device, dtype=torch.float64) + token_count = torch.zeros((), device=device, dtype=torch.float64) + byte_count = torch.zeros((), device=device, dtype=torch.float64) + was_training = model.training + processed = 0 + t_start = time.perf_counter() + model.eval() + with torch.inference_mode(): + for bi in range(0, len(wins), batch_seqs): + batch_ws = wins[bi:bi + batch_seqs] + bsz = len(batch_ws) + x_batch = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + y_batch = torch.zeros(bsz, seq_len, dtype=torch.int64, device=device) + wlens: list[int] = [] + for i, ws in enumerate(batch_ws): + end = min(ws + seq_len, total_tokens) + wlen = end - ws + wlens.append(wlen) + chunk = val_tokens[ws:end + 1].to(dtype=torch.int64, device=device, non_blocking=True) + x_batch[i, :wlen] = chunk[:-1] + y_batch[i, :wlen] = chunk[1:] + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + logits = model.forward_logits(x_batch) + loss = F.cross_entropy( + logits.reshape(-1, logits.size(-1)).float(), + y_batch.reshape(-1), + reduction="none", + ).reshape(bsz, seq_len) + for i, ws in enumerate(batch_ws): + wlen = wlens[i] + score_start = 0 if ws == 0 else max(wlen - stride, 0) + scored_loss = loss[i, score_start:wlen].to(torch.float64) + loss_sum += scored_loss.sum() + scored_tokens = float(wlen - score_start) + token_count += scored_tokens + tgt = y_batch[i, score_start:wlen] + prev = x_batch[i, score_start:wlen] + token_bytes = lut_bb[tgt].to(torch.float64) + token_bytes += (lut_lspace[tgt] & ~lut_isbtoken[prev]).to(torch.float64) + byte_count += token_bytes.sum() + processed += bsz + should_log = ( + log_fn is not None + and rank == 0 + and ( + processed == len(wins) + or (log_every > 0 and processed % log_every < bsz) + ) + ) + if should_log: + partial_loss = float((loss_sum / token_count).item()) if token_count.item() > 0 else float("nan") + partial_bpb = ( + partial_loss / math.log(2.0) * (token_count.item() / max(byte_count.item(), 1.0)) + if token_count.item() > 0 and byte_count.item() > 0 + else float("nan") + ) + log_fn( + f"{log_prefix} [{processed}/{len(wins)}] " + f"bpb={partial_bpb:.6f} time={time.perf_counter() - t_start:.1f}s" + ) + + if dist.is_available() and dist.is_initialized(): + dist.all_reduce(loss_sum, op=dist.ReduceOp.SUM) + dist.all_reduce(token_count, op=dist.ReduceOp.SUM) + dist.all_reduce(byte_count, op=dist.ReduceOp.SUM) + + val_loss = loss_sum / token_count + bits_per_token = val_loss.item() / math.log(2.0) + tokens_per_byte = token_count.item() / byte_count.item() + model.train(was_training) + return float(val_loss.item()), float(bits_per_token * tokens_per_byte) + + +def parse_piecewise_lr_schedule(spec: str) -> list[tuple[float, float]]: + schedule: list[tuple[float, float]] = [] + if not spec.strip(): + return schedule + for part in spec.split(","): + item = part.strip() + if not item: + continue + frac_str, lr_str = item.split(":", 1) + frac = float(frac_str.strip()) + lr = float(lr_str.strip()) + if frac <= 0: + raise ValueError(f"PIECEWISE_MATRIX_LRS has non-positive fraction: {item}") + if lr < 0: + raise ValueError(f"PIECEWISE_MATRIX_LRS has negative lr: {item}") + schedule.append((frac, lr)) + if not schedule: + raise ValueError("PIECEWISE_MATRIX_LRS is empty after parsing") + total = sum(frac for frac, _ in schedule) + if total <= 0: + raise ValueError("PIECEWISE_MATRIX_LRS total fraction must be positive") + return [(frac / total, lr) for frac, lr in schedule] + +# ----------------------------- +# POST-TRAINING QUANTIZATION +# ----------------------------- +# +# It's silly to export our model, which is trained in bf16 and fp32, at that same precision. +# Instead, we export int6 AWQ weights (val-tail activation calibration) + lzma; decompress and dequantize for eval. + +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 sample_validation_calibration_sequences_tail(val_tokens: Tensor, seq_len: int, num_seqs: int) -> list[Tensor]: + total_seqs = max(0, (val_tokens.numel() - 1) // seq_len) + num_take = min(num_seqs, total_seqs) + if num_take <= 0: + return [] + start = max(total_seqs - num_take, 0) + indices = list(range(start, total_seqs)) + return [val_tokens[i * seq_len : (i + 1) * seq_len + 1].unsqueeze(0).clone() for i in indices] + + +def collect_activation_stats(model: nn.Module, token_seqs: list[Tensor], device: torch.device) -> dict[str, dict[str, Tensor]]: + """Per-column abs_mean of activations at CastedLinear inputs (for AWQ column scales).""" + stats: dict[str, dict[str, Tensor]] = {} + hooks = [] + for name, module in model.named_modules(): + if isinstance(module, CastedLinear): + cols = module.weight.shape[1] + stats[name + ".weight"] = { + "abs_mean": torch.zeros(cols, dtype=torch.float32, device="cpu"), + "count": torch.zeros((), dtype=torch.float32, device="cpu"), + } + + def make_hook(pname: str): + def hook_fn(_module: nn.Module, inputs: tuple[Tensor, ...], _output: Tensor) -> None: + x = inputs[0].detach().float() + if x.ndim == 3: + x = x.reshape(-1, x.shape[-1]) + entry = stats[pname] + entry["abs_mean"].add_(x.abs().mean(dim=0).cpu()) + entry["count"].add_(1.0) + + return hook_fn + + hooks.append(module.register_forward_hook(make_hook(name + ".weight"))) + was_training = model.training + model.eval() + with torch.inference_mode(), torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=(device.type == "cuda")): + for seq in token_seqs: + local = seq.to(device=device, dtype=torch.int64, non_blocking=True) + x = local[:, :-1] + y = local[:, 1:] + model(x, y) + for hook in hooks: + hook.remove() + for entry in stats.values(): + count = entry["count"].item() + if count > 0: + entry["abs_mean"] /= count + model.train(was_training) + return stats + + +def _awq_column_scales( + name: str, + t32: Tensor, + calib: dict[str, dict[str, Tensor]], + alpha: float, + clamp_min: float, + clamp_max: float, + max_ratio: float, +) -> Tensor | None: + """Per-column s with W[:,j] *= s[j] before int6 quant; dequant divides by s. (act/w)^alpha with abs_mean.""" + if t32.ndim != 2 or name not in calib: + return None + entry = calib[name] + act = entry["abs_mean"].clamp_min(1e-4) + w = t32.abs().amax(dim=0).cpu().clamp_min(1e-4) + s = (act / w).pow(alpha).clamp(1e-4, 1e4) + s = s / s.mean().clamp_min(1e-4) + if not torch.isfinite(s).all(): + return None + spread = float((s.max() / s.min().clamp_min(1e-4)).item()) if s.numel() else 1.0 + if spread > max_ratio: + s = s.clamp(min=clamp_min, max=clamp_max) + s = s / s.mean().clamp_min(1e-4) + s = s.clamp(min=clamp_min, max=clamp_max) + s = s / s.mean().clamp_min(1e-4) + if not torch.isfinite(s).all(): + return None + return s.to(torch.float32).contiguous() + + +def _quantize_int6_affine(t: Tensor, clip_q: float, col_scale: Tensor | None = None) -> tuple[Tensor, Tensor, dict[str, object]]: + t32 = t.float() + # Symmetric signed int6: q in [-(2^5-1), 2^5-1]; stored in int8 tensors. + clip_range = (1 << (6 - 1)) - 1 + info: dict[str, object] = {"bits": 6} + if t32.ndim == 2: + work = t32 if col_scale is None else t32 * col_scale[None, :] + clip_abs = torch.quantile(work.abs(), clip_q, dim=1) if work.numel() else torch.empty((work.shape[0],), dtype=torch.float32) + clipped = torch.maximum(torch.minimum(work, clip_abs[:, None]), -clip_abs[:, None]) + scale = (clip_abs / clip_range).clamp_min(1.0 / clip_range) + q = torch.clamp(torch.round(clipped / scale[:, None]), -clip_range, clip_range).to(torch.int8).contiguous() + info.update({"scheme": "per_row", "axis": 0}) + if col_scale is not None: + info["col_scale"] = col_scale.to(torch.float16).contiguous() + return q, scale.to(dtype=INT8_PER_ROW_SCALE_DTYPE).contiguous(), info + clip_abs = float(torch.quantile(t32.abs().flatten(), clip_q).item()) if t32.numel() else 0.0 + scale = torch.tensor(clip_abs / clip_range if clip_abs > 0 else 1.0, dtype=torch.float32) + q = torch.clamp(torch.round(torch.clamp(t32, -clip_abs, clip_abs) / scale), -clip_range, clip_range).to(torch.int8).contiguous() + info.update({"scheme": "per_tensor"}) + return q, scale, info + + +def quantize_state_dict_int6_awq( + state_dict: dict[str, Tensor], + calib: dict[str, dict[str, Tensor]], + awq_alpha: float, + col_scale_clamp_min: float, + col_scale_clamp_max: float, + col_scale_max_ratio: float, +) -> tuple[dict[str, object], dict[str, object]]: + 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", + "quant_payload_bytes", + "col_scale_tensors", + "col_scale_fallbacks", + ), + 0, + ) + stats["col_scale_min"] = float("inf") + stats["col_scale_max"] = 0.0 + stats["col_scale_worst_name"] = "" + 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["quant_payload_bytes"] += tensor_nbytes(t) + continue + if t.numel() <= INT8_KEEP_FLOAT_MAX_NUMEL: + kept = keep_float_tensor(name, t, passthrough_orig_dtypes) + passthrough[name] = kept + stats["quant_payload_bytes"] += tensor_nbytes(kept) + continue + stats["num_float_tensors"] += 1 + col_scale = _awq_column_scales(name, t.float(), calib, awq_alpha, col_scale_clamp_min, col_scale_clamp_max, col_scale_max_ratio) + if t.ndim == 2 and name in calib and col_scale is None: + stats["col_scale_fallbacks"] += 1 + if col_scale is not None: + stats["col_scale_tensors"] += 1 + cmin = float(col_scale.min().item()) if col_scale.numel() else 1.0 + cmax = float(col_scale.max().item()) if col_scale.numel() else 1.0 + if cmin < stats["col_scale_min"]: + stats["col_scale_min"] = cmin + if cmax > stats["col_scale_max"]: + stats["col_scale_max"] = cmax + stats["col_scale_worst_name"] = name + q, s, info = _quantize_int6_affine(t, INT8_CLIP_Q, col_scale=col_scale) + quantized[name] = q + scales[name] = s + dtypes[name] = str(t.dtype).removeprefix("torch.") + qmeta[name] = info + stats["quant_payload_bytes"] += tensor_nbytes(q) + tensor_nbytes(s) + if "col_scale" in info: + stats["quant_payload_bytes"] += tensor_nbytes(info["col_scale"]) + obj: dict[str, object] = { + "__quant_format__": "int6_awq_weight_only_v1", + "quantized": quantized, + "scales": scales, + "dtypes": dtypes, + "passthrough": passthrough, + "qmeta": qmeta, + } + if passthrough_orig_dtypes: + obj["passthrough_orig_dtypes"] = passthrough_orig_dtypes + return obj, stats + + +def dequantize_state_dict_int6_awq(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] + info = qmeta.get(name, {}) + if qmeta.get(name, {}).get("scheme") == "per_row" or s.ndim > 0: + s32 = s.to(dtype=torch.float32) + recon = q.float() * s32.view(q.shape[0], *([1] * (q.ndim - 1))) + else: + recon = q.float() * float(s.item()) + col_scale = info.get("col_scale") + if col_scale is not None: + recon = recon / col_scale.to(dtype=torch.float32)[None, :] + out[name] = recon.to(dtype=dtype).contiguous() + for name, t in obj["passthrough"].items(): + out_t = t.detach().to("cpu").contiguous() + orig_dtype = passthrough_orig_dtypes.get(name) + if isinstance(orig_dtype, str): + out_t = out_t.to(dtype=getattr(torch, orig_dtype)).contiguous() + out[name] = out_t + return out + + +# ----------------------------- +# DATA LOADING +# ----------------------------- + +def load_data_shard(file: Path) -> Tensor: + header_bytes = 256 * np.dtype(" None: + self.file_idx = (self.file_idx + 1) % len(self.files) + self.tokens = load_data_shard(self.files[self.file_idx]) + self.pos = 0 + + def take(self, n: int) -> Tensor: + chunks: list[Tensor] = [] + remaining = n + while remaining > 0: + avail = self.tokens.numel() - self.pos + if avail <= 0: + self._advance_file() + continue + k = min(remaining, avail) + chunks.append(self.tokens[self.pos : self.pos + k]) + self.pos += k + remaining -= k + return chunks[0] if len(chunks) == 1 else torch.cat(chunks) + + +class DistributedTokenLoader: + # 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 +# ----------------------------- + +class RMSNorm(nn.Module): + def __init__(self, eps: float | None = None): + super().__init__() + self.eps = eps + + def forward(self, x: Tensor) -> Tensor: + return F.rms_norm(x, (x.size(-1),), eps=self.eps) + + +class CastedLinear(nn.Linear): + # Keep weights in fp32 for optimizer/state quality, cast at matmul time for bf16 compute. + def forward(self, x: Tensor) -> Tensor: + bias = self.bias.to(x.dtype) if self.bias is not None else None + return F.linear(x, self.weight.to(x.dtype), bias) + +def restore_low_dim_params_to_fp32(module: nn.Module) -> None: + # Keep small/control parameters in fp32 even when the model body runs in bf16. + with torch.no_grad(): + for name, param in module.named_parameters(): + if (param.ndim < 2 or any(pattern in name for pattern in CONTROL_TENSOR_NAME_PATTERNS)) and param.dtype != torch.float32: + 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) + + +class CausalSelfAttention(nn.Module): + def __init__( + self, + dim: int, + num_heads: int, + num_kv_heads: int, + rope_base: float, + qk_gain_init: float, + ): + super().__init__() + if dim % num_heads != 0: + raise ValueError("model_dim must be divisible by num_heads") + if num_heads % num_kv_heads != 0: + raise ValueError("num_heads must be divisible by num_kv_heads") + self.num_heads = num_heads + self.num_kv_heads = num_kv_heads + self.head_dim = dim // num_heads + if self.head_dim % 2 != 0: + raise ValueError("head_dim must be even for RoPE") + kv_dim = self.num_kv_heads * self.head_dim + self.c_q = CastedLinear(dim, dim, bias=False) + self.c_k = CastedLinear(dim, kv_dim, bias=False) + self.c_v = CastedLinear(dim, kv_dim, bias=False) + self.proj = CastedLinear(dim, dim, bias=False) + self.proj._zero_init = True + self.q_gain = nn.Parameter(torch.full((num_heads,), qk_gain_init, dtype=torch.float32)) + self.rotary = Rotary(self.head_dim, base=rope_base) + self.use_xsa = False + self.gated_xsa = False + self.register_buffer("xsa_alpha_logit", torch.tensor(0.0, dtype=torch.float32), persistent=False) + + def _xsa_efficient(self, y: Tensor, v: Tensor) -> Tensor: + bsz, seqlen, heads, head_dim = y.shape + kv_heads = v.size(-2) + group = heads // kv_heads + y_grouped = y.reshape(bsz, seqlen, kv_heads, group, head_dim) + v_norm = F.normalize(v, dim=-1).unsqueeze(-2) + proj = (y_grouped * v_norm).sum(dim=-1, keepdim=True) * v_norm + if self.gated_xsa: + alpha = F.rrelu(self.xsa_alpha_logit, lower=0.3, upper=0.7).square().to(dtype=y.dtype) + return (y_grouped - alpha * proj).reshape(bsz, seqlen, heads, head_dim) + else: + return (y_grouped - proj).reshape(bsz, seqlen, heads, head_dim) + + def forward(self, x: Tensor) -> Tensor: + bsz, seqlen, dim = x.shape + q = self.c_q(x).reshape(bsz, seqlen, self.num_heads, self.head_dim).transpose(1, 2) + k = self.c_k(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + v = self.c_v(x).reshape(bsz, seqlen, self.num_kv_heads, self.head_dim).transpose(1, 2) + 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) + q = apply_rotary_emb(q, cos, sin) + k = apply_rotary_emb(k, cos, sin) + q = q * self.q_gain.to(dtype=q.dtype)[None, :, None, None] + + if FLASH_ATTN_3_ENABLED: + # flash-attn expects B,T,H,D layout; for GQA we explicitly repeat KV heads + # so the FA3 path matches the SDPA fallback semantics. + q_fa = q.transpose(1, 2).contiguous() + k_fa = k.transpose(1, 2).contiguous() + v_fa = v.transpose(1, 2).contiguous() + if self.num_kv_heads != self.num_heads: + repeat = self.num_heads // self.num_kv_heads + k_fa = k_fa.repeat_interleave(repeat, dim=2) + v_fa = v_fa.repeat_interleave(repeat, dim=2) + y = flash_attn_3_func(q_fa, k_fa, v_fa, causal=True).transpose(1, 2).contiguous() + else: + y = F.scaled_dot_product_attention(q, k, v, attn_mask=None, is_causal=True, enable_gqa=(self.num_kv_heads != self.num_heads)) + + if self.use_xsa: + # SDPA returns (B, H, T, D_h); XSA math matches the paper on (B, T, H, D_h) / (B, T, H_kv, D_h). + y = self._xsa_efficient(y.transpose(1, 2), v.transpose(1, 2)).transpose(1, 2) + y = y.transpose(1, 2).contiguous().reshape(bsz, seqlen, dim) + return self.proj(y) + + +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: + h = self.fc(x) + h = F.rrelu(h, lower=0.4, upper=0.6) + return self.proj(h.square()) + + +class Block(nn.Module): + def __init__( + self, + dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: int, + rope_base: float, + qk_gain_init: float, + ): + super().__init__() + self.attn_norm = RMSNorm() + self.mlp_norm = RMSNorm() + self.attn = CausalSelfAttention(dim, num_heads, num_kv_heads, rope_base, qk_gain_init) + self.mlp = MLP(dim, mlp_mult) + self.attn_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.mlp_scale = nn.Parameter(torch.ones(dim, dtype=torch.float32)) + self.resid_mix = nn.Parameter(torch.stack((torch.ones(dim), torch.zeros(dim))).float()) + + def forward(self, x: Tensor, x0: Tensor) -> Tensor: + mix = self.resid_mix.to(dtype=x.dtype) + x = mix[0][None, None, :] * x + mix[1][None, None, :] * x0 + attn_out = self.attn(self.attn_norm(x)) + x = x + self.attn_scale.to(dtype=x.dtype)[None, None, :] * attn_out + x = x + self.mlp_scale.to(dtype=x.dtype)[None, None, :] * self.mlp(self.mlp_norm(x)) + return x + + +class GPT(nn.Module): + def __init__( + self, + vocab_size: int, + num_layers: int, + model_dim: int, + num_heads: int, + num_kv_heads: int, + mlp_mult: int, + tie_embeddings: bool, + tied_embed_init_std: float, + logit_softcap: float, + rope_base: float, + qk_gain_init: float + ): + super().__init__() + if logit_softcap <= 0.0: + raise ValueError(f"logit_softcap must be positive, got {logit_softcap}") + self.tie_embeddings = tie_embeddings + self.tied_embed_init_std = tied_embed_init_std + self.logit_softcap = logit_softcap + self.tok_emb = nn.Embedding(vocab_size, model_dim) + self.num_encoder_layers = num_layers // 2 + self.num_decoder_layers = num_layers - self.num_encoder_layers + self.num_skip_weights = min(self.num_encoder_layers, self.num_decoder_layers) + self.skip_weights = nn.Parameter(torch.ones(self.num_skip_weights, model_dim, dtype=torch.float32)) + self.blocks = nn.ModuleList( + [ + Block( + model_dim, + num_heads, + num_kv_heads, + mlp_mult, + rope_base, + qk_gain_init, + ) + for i in range(num_layers) + ] + ) + self.final_norm = RMSNorm() + self.lm_head = None if tie_embeddings else CastedLinear(model_dim, vocab_size, bias=False) + if self.lm_head is not None: + self.lm_head._zero_init = True + + + for i in range(max(0, num_layers - 5), num_layers): + self.blocks[i].attn.use_xsa = True + if i == num_layers - 1: # last layer is gated + self.blocks[i].attn.gated_xsa = True + init = min(max(0.1, 1e-4), 1 - 1e-4) + self.blocks[i].attn.xsa_alpha_logit = nn.Parameter(torch.tensor(math.log(init / (1 - init)), dtype=torch.float32)) + else: # other layers are not gated + self.blocks[i].attn.gated_xsa = False + self.blocks[i].attn.xsa_alpha_logit = None + 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) + + num_layers = len(self.blocks) + + for i, block in enumerate(self.blocks): + with torch.no_grad(): + phase = i / max(num_layers - 1, 1) + block.resid_mix.data[0].fill_(phase) + block.resid_mix.data[1].fill_(1.0 - phase) + if i <= 1: + block.attn.q_gain.fill_(3.6) + elif i <= 5: + block.attn.q_gain.fill_(2.7) + else: + block.attn.q_gain.fill_(3.1) + + block.attn_scale.fill_(0.75 + 1.1 * i) + block.mlp_scale.fill_(0.8 + 0.12 * i) + + + def forward_logits(self, input_ids: Tensor) -> Tensor: + x = self.tok_emb(input_ids) + x = F.rms_norm(x, (x.size(-1),)) + x0 = x + skips: list[Tensor] = [] + + # First half stores skips; second half reuses them in reverse order. + 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) + if self.tie_embeddings: + logits_proj = F.linear(x, self.tok_emb.weight) + else: + if self.lm_head is None: + raise RuntimeError("lm_head is required when tie_embeddings=False") + logits_proj = self.lm_head(x) + return self.logit_softcap * torch.tanh(logits_proj / self.logit_softcap) + + def forward(self, input_ids: Tensor, target_ids: Tensor) -> Tensor: + x = self.forward_logits(input_ids).reshape(-1, self.tok_emb.num_embeddings) + targets = target_ids.reshape(-1) + return F.cross_entropy(x.float(), targets, reduction="mean") + + +# ----------------------------- +# TRAINING +# ----------------------------- + +def main() -> None: + global zeropower_via_newtonschulz5 + + code = Path(__file__).read_text(encoding="utf-8") + args = Hyperparameters() + + # ----------------------------- + # DISTRIBUTED + CUDA SETUP + # ----------------------------- + + distributed = "RANK" in os.environ and "WORLD_SIZE" in os.environ + rank = int(os.environ.get("RANK", "0")) + world_size = int(os.environ.get("WORLD_SIZE", "1")) + local_rank = int(os.environ.get("LOCAL_RANK", "0")) + if world_size <= 0: + raise ValueError(f"WORLD_SIZE must be positive, got {world_size}") + if 8 % world_size != 0: + raise ValueError(f"WORLD_SIZE={world_size} must divide 8 so grad_accum_steps stays integral") + grad_accum_steps = 8 // world_size + grad_scale = 1.0 / grad_accum_steps + if not torch.cuda.is_available(): + raise RuntimeError("CUDA is required") + device = torch.device("cuda", local_rank) + torch.cuda.set_device(device) + if distributed: + dist.init_process_group(backend="nccl", device_id=device) + dist.barrier() + master_process = rank == 0 + + # 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: + os.makedirs("logs", exist_ok=True) + logfile = f"logs/{args.run_id}.txt" + print(logfile) + + def log0(msg: str, console: bool = True) -> None: + if not master_process: + return + if console: + print(msg) + if logfile is not None: + with open(logfile, "a", encoding="utf-8") as f: + print(msg, file=f) + + log0(code, console=False) + log0("=" * 100, console=False) + log0(f"Running Python {sys.version}", console=False) + log0(f"Running PyTorch {torch.__version__}", console=False) + log0( + subprocess.run(["nvidia-smi"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, check=False).stdout, + console=False, + ) + log0("=" * 100, console=False) + + # ----------------------------- + # TOKENIZER + VALIDATION METRIC SETUP + # ----------------------------- + + random.seed(args.seed) + np.random.seed(args.seed) + torch.manual_seed(args.seed) + torch.cuda.manual_seed_all(args.seed) + + if not args.tokenizer_path.endswith(".model"): + raise ValueError(f"Script only setup for SentencePiece .model file: {args.tokenizer_path}") + sp = spm.SentencePieceProcessor(model_file=args.tokenizer_path) + if int(sp.vocab_size()) != args.vocab_size: + raise ValueError( + f"VOCAB_SIZE={args.vocab_size} does not match tokenizer vocab_size={int(sp.vocab_size())}" + ) + dataset_dir = Path(args.data_path).resolve() + actual_train_files = len(list(dataset_dir.glob("fineweb_train_*.bin"))) + val_tokens = load_validation_tokens(args.val_files, args.train_seq_len) + base_bytes_lut, has_leading_space_lut, is_boundary_token_lut = build_sentencepiece_luts( + sp, args.vocab_size, device + ) + log0(f"val_bpb:enabled tokenizer_kind=sentencepiece tokenizer_path={args.tokenizer_path}") + log0(f"train_loader:dataset:{dataset_dir.name} train_shards:{actual_train_files}") + log0(f"val_loader:shards pattern={args.val_files} tokens:{val_tokens.numel() - 1}") + + # ----------------------------- + # MODEL + OPTIMIZER SETUP + # ----------------------------- + + base_model = GPT( + vocab_size=args.vocab_size, + num_layers=args.num_layers, + model_dim=args.model_dim, + num_heads=args.num_heads, + num_kv_heads=args.num_kv_heads, + mlp_mult=args.mlp_mult, + tie_embeddings=args.tie_embeddings, + tied_embed_init_std=args.tied_embed_init_std, + logit_softcap=args.logit_softcap, + rope_base=args.rope_base, + qk_gain_init=args.qk_gain_init, + ).to(device).bfloat16() + for module in base_model.modules(): + if isinstance(module, CastedLinear): + module.float() + restore_low_dim_params_to_fp32(base_model) + compiled_model = torch.compile(base_model, dynamic=False, fullgraph=True) + model: nn.Module = DDP(compiled_model, device_ids=[local_rank], broadcast_buffers=False) if distributed else compiled_model + + # Optimizer split: + # - token embedding (Adam) uses EMBED_LR + # - untied lm_head (Adam) uses HEAD_LR + # - matrix params in transformer blocks use MATRIX_LR via Muon + # - vectors/scalars use SCALAR_LR via Adam + 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) + token_lr = args.tied_embed_lr if args.tie_embeddings else args.embed_lr + optimizer_tok = torch.optim.AdamW( + [{"params": [base_model.tok_emb.weight], "lr": token_lr, "base_lr": token_lr, "weight_decay": 0.0}], + betas=(args.beta1, args.beta2), + eps=args.adam_eps, + fused=True, + ) + optimizer_muon = Muon( + matrix_params, + lr=args.matrix_lr, + momentum=args.muon_momentum, + backend_steps=args.muon_backend_steps, + weight_decay=args.muon_wd, + rank=rank, + world_size=world_size, + ) + for group in optimizer_muon.param_groups: + group["base_lr"] = args.matrix_lr + optimizer_scalar = torch.optim.AdamW( + [{"params": scalar_params, "lr": args.scalar_lr, "base_lr": args.scalar_lr, "weight_decay": 0.0}], + 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.AdamW( + [{"params": [base_model.lm_head.weight], "lr": args.head_lr, "base_lr": args.head_lr, "weight_decay": args.adam_wd}], + 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_backend:{'flash_attn_3' if FLASH_ATTN_3_ENABLED else 'sdpa_fallback'}") + 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("init_scales:attn_scale=(early=1.0,mid=1.75,late=2.5) mlp_scale=(early=1.0,mid=1.15,late=1.3)") + log0(f"adam_wd:{args.adam_wd:.4f} muon_wd:{args.muon_wd:.4f}") + 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} warmdown_start:{args.warmdown_start:.3f} " + f"max_wallclock_seconds:{args.max_wallclock_seconds:.3f}" + ) + log0(f"lr_scheduler:{args.lr_scheduler} piecewise_matrix_lrs:{args.piecewise_matrix_lrs or '-'}") + log0(f"seed:{args.seed}") + log0( + f"quant_export:int6_awq val_tail_calib quant_calib_seqs:{args.quant_calib_seqs} " + f"quant_awq_alpha:{args.quant_awq_alpha:.3f}" + ) + log0( + f"val_sliding_stride:{args.val_sliding_stride} val_sliding_batch_seqs:{args.val_sliding_batch_seqs} " + f"val_sliding_seq_len:{args.val_sliding_seq_len} val_sliding_log_every:{args.val_sliding_log_every}" + ) + log0( + f"ema_enabled:{int(args.ema_enabled)} ema_decay:{args.ema_decay:.4f} ema_start:{args.ema_start:.3f} " + f"best_ckpt_k:{args.best_ckpt_k} " + f"best_ckpt_start:{args.best_ckpt_start:.3f}" + ) + + # ----------------------------- + # DATA LOADER & MODEL WARMUP + # ----------------------------- + + train_loader = DistributedTokenLoader(args.train_files, rank, world_size, device) + + def zero_grad_all() -> None: + for opt in optimizers: + opt.zero_grad(set_to_none=True) + + max_wallclock_ms = 1000.0 * args.max_wallclock_seconds if args.max_wallclock_seconds > 0 else None + piecewise_lr_schedule = parse_piecewise_lr_schedule(args.piecewise_matrix_lrs) if args.lr_scheduler == "piecewise" else [] + if not (0.0 <= args.ema_start <= 1.0): + raise ValueError(f"EMA_START must be in [0, 1], got {args.ema_start}") + if not (0.0 <= args.best_ckpt_start <= 1.0): + raise ValueError(f"BEST_CKPT_START must be in [0, 1], got {args.best_ckpt_start}") + if not (0.0 <= args.warmdown_start <= 1.0): + raise ValueError(f"WARMDOWN_START must be in [0, 1], got {args.warmdown_start}") + if args.lr_scheduler == "piecewise" and not piecewise_lr_schedule: + raise ValueError("LR_SCHEDULER=piecewise requires PIECEWISE_MATRIX_LRS") + + def piecewise_matrix_lr(step: int, elapsed_ms: float) -> float: + if not piecewise_lr_schedule: + return args.matrix_lr + if max_wallclock_ms is not None: + progress = min(max(elapsed_ms / max(max_wallclock_ms, 1e-9), 0.0), 1.0) + else: + progress = min(max(step / max(args.iterations, 1), 0.0), 1.0) + running = 0.0 + target_matrix_lr = piecewise_lr_schedule[-1][1] + for frac, stage_lr in piecewise_lr_schedule: + running += frac + target_matrix_lr = stage_lr + if progress < running: + break + return target_matrix_lr + + def training_progress(step: int, elapsed_ms: float) -> float: + if max_wallclock_ms is not None: + return min(max(elapsed_ms / max(max_wallclock_ms, 1e-9), 0.0), 1.0) + return min(max(step / max(args.iterations, 1), 0.0), 1.0) + + def lr_mul(step: int, elapsed_ms: float) -> float: + if args.warmdown_start >= 1.0: + return 1.0 + progress = training_progress(step, elapsed_ms) + if args.warmdown_start <= 0.0: + return max(0.5 * (1.0 - progress), 0.0) + if progress <= args.warmdown_start: + # Smooth pre-warmdown decay: 1.0 at start, 0.5 exactly at WARMDOWN_START. + return float(0.5 ** (progress / max(args.warmdown_start, 1e-9))) + tail_frac = (progress - args.warmdown_start) / max(1.0 - args.warmdown_start, 1e-9) + return max(0.5 * (1.0 - tail_frac), 0.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 + ema_state: dict[str, Tensor] | None = None + best_ckpt_pool: list[dict[str, object]] = [] + log_interval_loss_token_sum = 0.0 + log_interval_token_count = 0.0 + log_interval_byte_count = 0.0 + torch.cuda.synchronize() + t0 = time.perf_counter() + + step = 0 + while True: + last_step = step == args.iterations or (stop_after_step is not None and step >= stop_after_step) + should_validate = last_step or (args.val_loss_every > 0 and step % args.val_loss_every == 0) + if should_validate: + torch.cuda.synchronize() + training_time_ms += 1000.0 * (time.perf_counter() - t0) + val_loss, val_bpb = eval_val( + args, + model, + rank, + world_size, + device, + grad_accum_steps, + val_tokens, + base_bytes_lut, + has_leading_space_lut, + is_boundary_token_lut, + ) + log0( + f"step:{step}/{args.iterations} val_loss:{val_loss:.4f} val_bpb:{val_bpb:.4f} " + f"train_time:{training_time_ms:.0f}ms step_avg:{training_time_ms / max(step, 1):.2f}ms" + ) + torch.cuda.synchronize() + t0 = time.perf_counter() + + if last_step: + if stop_after_step is not None and step < args.iterations: + log0( + f"stopping_early: wallclock_cap train_time:{training_time_ms:.0f}ms " + f"step:{step}/{args.iterations}" + ) + break + + elapsed_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + progress = training_progress(step, elapsed_ms) + scale = lr_mul(step, elapsed_ms) + scheduled_matrix_lr = piecewise_matrix_lr(step, elapsed_ms) if args.lr_scheduler == "piecewise" else args.matrix_lr * scale + + zero_grad_all() + train_loss = torch.zeros((), device=device) + train_token_count = 0.0 + train_byte_count = torch.zeros((), device=device, dtype=torch.float64) + for micro_step in range(grad_accum_steps): + if distributed: + model.require_backward_grad_sync = micro_step == grad_accum_steps - 1 + x, y = train_loader.next_batch(args.train_batch_tokens, args.train_seq_len, grad_accum_steps) + with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=True): + loss = model(x, y) + train_loss += loss.detach() + train_token_count += float(y.numel()) + 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) + train_byte_count += token_bytes.to(torch.float64).sum() + (loss * grad_scale).backward() + train_loss /= grad_accum_steps + train_bits_per_token = train_loss.to(torch.float64) / math.log(2.0) + train_tokens_per_byte = train_token_count / max(train_byte_count.item(), 1.0) + train_bpb = float((train_bits_per_token * train_tokens_per_byte).item()) + log_interval_loss_token_sum += float(train_loss.item()) * train_token_count + log_interval_token_count += train_token_count + log_interval_byte_count += float(train_byte_count.item()) + + 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"] = scheduled_matrix_lr if args.lr_scheduler == "piecewise" else group["base_lr"] * scale + + if args.grad_clip_norm > 0: + torch.nn.utils.clip_grad_norm_(base_model.parameters(), args.grad_clip_norm) + for opt in optimizers: + opt.step() + zero_grad_all() + if args.ema_enabled and progress >= args.ema_start: + if ema_state is None: + ema_state = {name: t.detach().float().cpu().clone() for name, t in base_model.state_dict().items()} + log0(f"ema:start step:{step + 1} progress:{progress:.4f}") + else: + with torch.no_grad(): + for name, t in base_model.state_dict().items(): + ema_state[name].mul_(args.ema_decay).add_(t.detach().float().cpu(), alpha=1.0 - args.ema_decay) + if ( + args.ema_enabled + and args.best_ckpt_k > 0 + and progress >= args.best_ckpt_start + ): + qualifies = len(best_ckpt_pool) < args.best_ckpt_k + worst_idx = None + if not qualifies: + worst_idx = max(range(len(best_ckpt_pool)), key=lambda i: float(best_ckpt_pool[i]["train_bpb"])) + qualifies = train_bpb < float(best_ckpt_pool[worst_idx]["train_bpb"]) + if qualifies: + state_cpu = {name: t.detach().cpu().clone() for name, t in base_model.state_dict().items()} + entry = { + "step": step + 1, + "train_loss": float(train_loss.item()), + "train_bpb": train_bpb, + "state": state_cpu, + } + if len(best_ckpt_pool) < args.best_ckpt_k: + best_ckpt_pool.append(entry) + else: + assert worst_idx is not None + best_ckpt_pool[worst_idx] = entry + best_ckpt_pool.sort(key=lambda item: (float(item["train_bpb"]), int(item["step"]))) + log0( + f"best_ckpt:save step:{step + 1} train_loss:{train_loss.item():.4f} " + f"train_bpb:{train_bpb:.4f} pool:{len(best_ckpt_pool)}/{args.best_ckpt_k}" + ) + + step += 1 + approx_training_time_ms = training_time_ms + 1000.0 * (time.perf_counter() - t0) + is_interval_log = ( + args.train_log_every > 0 + and (step % args.train_log_every == 0 or stop_after_step is not None) + ) + 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: + if is_interval_log and log_interval_token_count > 0 and log_interval_byte_count > 0: + log_train_loss = log_interval_loss_token_sum / log_interval_token_count + log_train_bpb = (log_train_loss / math.log(2.0)) * (log_interval_token_count / log_interval_byte_count) + else: + log_train_loss = float(train_loss.item()) + log_train_bpb = train_bpb + log0( + f"step:{step}/{args.iterations} train_loss:{log_train_loss:.4f} train_bpb:{log_train_bpb:.4f} " + f"train_time:{approx_training_time_ms:.0f}ms step_avg:{approx_training_time_ms / step:.2f}ms " + f"lr_matrix:{scheduled_matrix_lr:.6f}" + ) + if is_interval_log: + log_interval_loss_token_sum = 0.0 + log_interval_token_count = 0.0 + log_interval_byte_count = 0.0 + + # Needed to sync whether we've reached the wallclock cap. + reached_cap = max_wallclock_ms is not None and approx_training_time_ms >= max_wallclock_ms + if distributed and max_wallclock_ms is not None: + reached_cap_tensor = torch.tensor(int(reached_cap), device=device) + 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" + ) + + # ----------------------------- + # SERIALIZATION + ROUNDTRIP VALIDATION + # ----------------------------- + # Save raw fp state, then int6 AWQ (val-tail calibration) + lzma and validate dequantized weights. + + def run_posttrain_eval_standard(eval_model: nn.Module) -> tuple[float, float]: + return eval_val( + args, + eval_model, + rank, + world_size, + device, + grad_accum_steps, + val_tokens, + base_bytes_lut, + has_leading_space_lut, + is_boundary_token_lut, + ) + + def run_posttrain_eval_sliding(eval_model: nn.Module) -> tuple[float, float]: + return sliding_window_evaluation( + args, + eval_model, + rank, + world_size, + device, + val_tokens, + base_bytes_lut, + has_leading_space_lut, + is_boundary_token_lut, + stride=args.val_sliding_stride, + batch_seqs=args.val_sliding_batch_seqs, + eval_seq_len=(args.val_sliding_seq_len or None), + log_fn=log0, + log_prefix="sliding_window", + log_every=args.val_sliding_log_every, + ) + + def eval_candidate_state(tag: str, state_cpu: dict[str, Tensor]) -> tuple[float, float]: + eval_model = copy.deepcopy(base_model) + eval_model.load_state_dict(state_cpu, strict=True) + torch.cuda.synchronize() + t_eval = time.perf_counter() + cand_val_loss, cand_val_bpb = run_posttrain_eval_standard(eval_model) + del eval_model + torch.cuda.synchronize() + log0( + f"candidate_eval:{tag} val_loss:{cand_val_loss:.4f} val_bpb:{cand_val_bpb:.4f} " + f"eval_time:{1000.0 * (time.perf_counter() - t_eval):.0f}ms" + ) + return cand_val_loss, cand_val_bpb + + quant_out = os.environ.get("QUANT_OUT_FILE", "final_model.int6_awq.ptz") + raw_sd_cpu = {k: v.detach().cpu().contiguous() for k, v in base_model.state_dict().items()} + selected_tag = "raw_final" + selected_val_loss, selected_val_bpb = eval_candidate_state(selected_tag, raw_sd_cpu) + selected_state_cpu = raw_sd_cpu + + if ema_state is not None: + ema_val_loss, ema_val_bpb = eval_candidate_state("ema", ema_state) + if ema_val_bpb < selected_val_bpb: + selected_tag = "ema" + selected_val_loss, selected_val_bpb = ema_val_loss, ema_val_bpb + selected_state_cpu = ema_state + + if args.ema_enabled and best_ckpt_pool: + for entry in best_ckpt_pool: + entry_step = int(entry["step"]) + entry_state = entry["state"] + cand_val_loss, cand_val_bpb = eval_candidate_state(f"best_ckpt_step_{entry_step}", entry_state) + if cand_val_bpb < selected_val_bpb: + selected_tag = f"best_ckpt_step_{entry_step}" + selected_val_loss, selected_val_bpb = cand_val_loss, cand_val_bpb + selected_state_cpu = entry_state + if len(best_ckpt_pool) > 1: + avg_state_cpu: dict[str, Tensor] = {} + ref_state = best_ckpt_pool[0]["state"] + for name, ref_tensor in ref_state.items(): + acc = torch.zeros_like(ref_tensor, dtype=torch.float32) + for entry in best_ckpt_pool: + acc.add_(entry["state"][name].float()) + avg_state_cpu[name] = (acc / len(best_ckpt_pool)).to(dtype=ref_tensor.dtype) + cand_val_loss, cand_val_bpb = eval_candidate_state("best_ckpt_avg", avg_state_cpu) + if cand_val_bpb < selected_val_bpb: + selected_tag = "best_ckpt_avg" + selected_val_loss, selected_val_bpb = cand_val_loss, cand_val_bpb + selected_state_cpu = avg_state_cpu + + log0( + f"selected_state:{selected_tag} val_loss:{selected_val_loss:.4f} val_bpb:{selected_val_bpb:.4f}" + ) + base_model.load_state_dict(selected_state_cpu, strict=True) + + if master_process: + torch.save(selected_state_cpu, "final_model.pt") + model_bytes = os.path.getsize("final_model.pt") + code_bytes = len(code.encode("utf-8")) + log0(f"Serialized model: {model_bytes} bytes") + log0(f"Code size: {code_bytes} bytes") + log0(f"Total submission size: {model_bytes + code_bytes} bytes") + + sd_cpu = {k: v.detach().cpu().contiguous() for k, v in selected_state_cpu.items()} + calib_tokens = sample_validation_calibration_sequences_tail(val_tokens, args.train_seq_len, args.quant_calib_seqs) + log0(f"quant_calib:val_tail sequences={len(calib_tokens)} seq_len={args.train_seq_len}") + calib_stats = collect_activation_stats(base_model, calib_tokens, device) + log0(f"quant_calib:activation_stats_tensors={len(calib_stats)}") + quant_obj, quant_stats = quantize_state_dict_int6_awq( + sd_cpu, + calib=calib_stats, + awq_alpha=args.quant_awq_alpha, + col_scale_clamp_min=args.quant_col_scale_clamp_min, + col_scale_clamp_max=args.quant_col_scale_clamp_max, + col_scale_max_ratio=args.quant_col_scale_max_ratio, + ) + quant_buf = io.BytesIO() + torch.save(quant_obj, quant_buf) + quant_raw = quant_buf.getvalue() + quant_blob = lzma.compress(quant_raw, preset=9) + quant_raw_bytes = len(quant_raw) + if master_process: + with open(quant_out, "wb") as f: + f.write(quant_blob) + quant_file_bytes = os.path.getsize(quant_out) + code_bytes = len(code.encode("utf-8")) + ratio = quant_stats["baseline_tensor_bytes"] / max(quant_stats["quant_payload_bytes"], 1) + log0( + f"Serialized model int6_awq+lzma:{quant_out} bytes:{quant_file_bytes} " + f"(payload:{quant_stats['quant_payload_bytes']} raw_torch:{quant_raw_bytes} payload_ratio:{ratio:.2f}x)" + ) + log0(f"Total submission size int6_awq+lzma: {quant_file_bytes + code_bytes} bytes") + + if distributed: + dist.barrier() + with open(quant_out, "rb") as f: + quant_blob_disk = f.read() + quant_state = torch.load(io.BytesIO(lzma.decompress(quant_blob_disk)), map_location="cpu") + base_model.load_state_dict(dequantize_state_dict_int6_awq(quant_state), strict=True) + torch.cuda.synchronize() + t_qeval = time.perf_counter() + q_val_loss, q_val_bpb = run_posttrain_eval_standard(base_model) + torch.cuda.synchronize() + log0( + f"final_int6_awq_lzma_val_tail_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_int6_awq_lzma_val_tail_roundtrip_exact val_loss:{q_val_loss:.8f} val_bpb:{q_val_bpb:.8f}") + if args.val_sliding_stride > 0: + torch.cuda.synchronize() + t_qeval_sliding = time.perf_counter() + q_val_loss_sliding, q_val_bpb_sliding = run_posttrain_eval_sliding(base_model) + torch.cuda.synchronize() + log0( + f"final_int6_awq_lzma_val_tail_sliding_window val_loss:{q_val_loss_sliding:.4f} " + f"val_bpb:{q_val_bpb_sliding:.4f} stride:{args.val_sliding_stride} " + f"eval_time:{1000.0 * (time.perf_counter() - t_qeval_sliding):.0f}ms" + ) + log0( + f"final_int6_awq_lzma_val_tail_sliding_window_exact " + f"val_loss:{q_val_loss_sliding:.8f} val_bpb:{q_val_bpb_sliding:.8f}" + ) + + if distributed: + dist.destroy_process_group() + + +if __name__ == "__main__": + main()