Repository navigation
Expand file tree
/
Copy pathmoe_train.py
More file actions
1463 lines (1223 loc) · 63.7 KB
/
Copy pathmoe_train.py
File metadata and controls
1463 lines (1223 loc) · 63.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
moe_train.py — MoE pretraining on DCLM-edu with wandb logging + DDP
===============================================================
Three real MoE architectures (OLMoE / Qwen3-MoE / DeepSeekMoE),
full bf16 training scalable across multiple GPUs (if model fits single GPU, with grad checkpointing).
Uses:
- HuggingFace transformers v5 with grouped_mm (fused expert GEMM)
- torch.optim.Muon (official PyTorch) or AdamW
- DCLM-edu dataset via your data.py module
- wandb logging
┌─────────────────────────┬──────────┬──────────┬──────────┬──────────┐
│ Config │ Total │ Active │ Adam GB │ Muon GB │
├─────────────────────────┼──────────┼──────────┼──────────┼──────────┤
│ A) OLMoE-1B-7B │ 6.8B │ 1.2B │ ~109 │ ~82 │
│ B) Qwen3-MoE-style │ 6.2B │ 0.6B │ ~98 │ ~74 │
│ C) DeepSeekMoE-style │ 7.0B │ 1.1B │ ~112 │ ~84 │
└─────────────────────────┴──────────┴──────────┴──────────┴──────────┘
Launch (1 GPU):
python moe_train.py --arch olmoe --optimizer muon
python moe_train.py --arch qwen3moe --optimizer muon --compile
python moe_train.py --arch deepseek --optimizer adam --no-wandb
python moe_train.py --arch olmoe --dry-run # 100 steps
Launch (4 GPUs DDP):
torchrun --standalone --nproc_per_node=4 moe_train.py --arch deepseek --optimizer muon
"""
from __future__ import annotations
import argparse
import json
import math
import os
import time
from pathlib import Path
from contextlib import nullcontext
import torch
import torch.nn as nn
import torch.distributed as dist
from torch.nn.parallel import DistributedDataParallel as DDP
from data import get_dataloader, get_eval_batches
try:
import wandb
HAS_WANDB = True
except ImportError:
HAS_WANDB = False
print("wandb not installed — logging to console only")
try:
from tqdm import tqdm
HAS_TQDM = True
except ImportError:
HAS_TQDM = False
# Enable TF32 for faster fp32 matmuls on Ampere/Hopper
torch.set_float32_matmul_precision('high')
# ──────────────────────────────────────────────────────────────────────
# 1. Architecture Configs
# ──────────────────────────────────────────────────────────────────────
def get_olmoe_config(scale="regular"):
"""OLMoE-1B-7B (Allen AI, Sep 2024) - arxiv.org/abs/2409.02060"""
from transformers import OlmoeConfig
is_small = scale == "small"
is_tiny = scale == "tiny"
return OlmoeConfig(
vocab_size=100277,
hidden_size=512 if is_tiny else (1024 if is_small else 2048),
intermediate_size=256 if is_tiny else (512 if is_small else 1024),
num_hidden_layers=16,
num_attention_heads=4 if is_tiny else (8 if is_small else 16),
num_key_value_heads=4 if is_tiny else (8 if is_small else 16),
num_experts=64,
num_experts_per_tok=8,
router_aux_loss_coef=0.01,
max_position_embeddings=4096,
hidden_act="silu",
rms_norm_eps=1e-5,
clip_qkv=8.0,
tie_word_embeddings=False,
use_cache=False,
output_router_logits=True,
)
def get_qwen3moe_config(scale="regular"):
"""Qwen3-MoE-style (Qwen Team, Apr 2025) - arxiv.org/abs/2505.09388"""
from transformers import Qwen3MoeConfig
is_small = scale == "small"
is_tiny = scale == "tiny"
return Qwen3MoeConfig(
vocab_size=100277,
hidden_size=384 if is_tiny else (768 if is_small else 1536),
intermediate_size=1024 if is_tiny else (2048 if is_small else 4096),
moe_intermediate_size=192 if is_tiny else (384 if is_small else 768),
num_hidden_layers=28,
num_attention_heads=6 if is_tiny else (12 if is_small else 24),
num_key_value_heads=1 if is_tiny else (2 if is_small else 4),
num_experts=60,
num_experts_per_tok=4,
decoder_sparse_step=1,
norm_topk_prob=True,
# The HF default of 0.001 is calibrated for the bias-based balancer used in
# production Qwen3-MoE; the HF implementation does not include that mechanism,
# so we use 0.01 to match the standard auxiliary-loss-only setting (as in OLMoE).
router_aux_loss_coef=0.01,
max_position_embeddings=4096,
rope_theta=10000.0,
hidden_act="silu",
rms_norm_eps=1e-6,
tie_word_embeddings=False,
use_cache=False,
output_router_logits=True,
)
def get_deepseek_config(scale="regular"):
"""DeepSeekMoE-style (DeepSeek-AI, 2024)"""
from transformers import OlmoeConfig
is_small = scale == "small"
is_tiny = scale == "tiny"
return OlmoeConfig(
vocab_size=100277,
hidden_size=512 if is_tiny else (1024 if is_small else 2048),
intermediate_size=256 if is_tiny else (512 if is_small else 1024),
num_hidden_layers=16,
num_attention_heads=4 if is_tiny else (8 if is_small else 16),
num_key_value_heads=4 if is_tiny else (8 if is_small else 16),
num_experts=64,
num_experts_per_tok=6,
# Using 0.01 in place of the HF default of 0.001, which is calibrated for
# bias-based load balancing not implemented in the HF DeepSeekMoE class.
router_aux_loss_coef=0.01,
max_position_embeddings=4096,
hidden_act="silu",
rms_norm_eps=1e-5,
clip_qkv=8.0,
tie_word_embeddings=False,
use_cache=False,
output_router_logits=True,
)
ARCH_REGISTRY = {
"olmoe": ("OLMoE-1B-7B", get_olmoe_config),
"qwen3moe": ("Qwen3-MoE-style", get_qwen3moe_config),
"deepseek": ("DeepSeekMoE-style", get_deepseek_config),
}
# Train config presets (matching your train.py structure)
TRAIN_CONFIGS = {
"default": dict(
batch_size=16, grad_accum=4, seq_len=2048,
n_steps=20_000, lr=2e-2, min_lr=2e-3,
warmup_steps=100, weight_decay=0.1, grad_clip=1.0, # weight decay is set per param group below; this value is unused
eval_every=500, eval_batches=10, save_every=500,
log_every=10, compile_model=True,
),
}
# ──────────────────────────────────────────────────────────────────────
# 2. Expert Weight Tying
# ──────────────────────────────────────────────────────────────────────
def tie_expert_layers(model, group_size: int, skip_first: int = 2, skip_last: int = 2, master_process: bool = True):
"""
Tie expert parameters across consecutive layers to save memory.
Within each group, all layers share the same expert weight tensors
(gate_up_proj and down_proj). Router weights (gate.weight) and all
attention/norm params remain independent per layer.
Gradients accumulate correctly through all tied layers because they
reference the same nn.Parameter object — autograd sums gradients
from every use site.
Args:
model: HF transformers MoE model (OlmoeForCausalLM or Qwen3MoeForCausalLM)
group_size: number of consecutive layers that share expert weights
skip_first: layers at the start left untied (default: 2)
skip_last: layers at the end left untied (default: 2)
Returns:
dict with tying info for logging
Example (16 layers, group_size=5, skip 2+2):
layers 0,1: untied (independent experts)
layers 2-6: share experts from layer 2
layers 7-11: share experts from layer 7
layers 12,13: share experts from layer 12
layers 14,15: untied (independent experts)
"""
raw = model._orig_mod if hasattr(model, "_orig_mod") else model
# Navigate to the layer list — handle both OLMoE and Qwen3MoE structures
if hasattr(raw, "model") and hasattr(raw.model, "layers"):
layers = raw.model.layers
elif hasattr(raw, "layers"):
layers = raw.layers
else:
raise ValueError("Cannot find model layers for expert tying")
n_layers = len(layers)
middle_start = skip_first
middle_end = n_layers - skip_last
if middle_end <= middle_start:
if master_process:
print(f" Expert tying: nothing to tie (only {n_layers} layers, "
f"skipping {skip_first}+{skip_last})")
return {"tied": False}
middle_indices = list(range(middle_start, middle_end))
# Chunk into groups
groups = []
for i in range(0, len(middle_indices), group_size):
groups.append(middle_indices[i:i + group_size])
# Count params before tying
params_before = sum(p.numel() for p in raw.parameters())
# Tie within each group: all layers point to the first layer's expert params
total_tied = 0
for group in groups:
leader_idx = group[0]
leader = layers[leader_idx]
for follower_idx in group[1:]:
follower = layers[follower_idx]
# Tie expert weights (gate_up_proj, down_proj)
follower.mlp.experts.gate_up_proj = leader.mlp.experts.gate_up_proj
follower.mlp.experts.down_proj = leader.mlp.experts.down_proj
# Router (gate) stays INDEPENDENT — each layer routes differently
total_tied += 1
params_after = sum(p.numel() for p in raw.parameters())
saved = params_before - params_after
info = {
"tied": True,
"group_size": group_size,
"groups": groups,
"skip_first": skip_first,
"skip_last": skip_last,
"layers_tied": total_tied,
"params_before": params_before,
"params_after": params_after,
"params_saved": saved,
}
if master_process:
print(f" Expert tying: group_size={group_size}, skip {skip_first}+{skip_last}")
print(f" Groups: {groups}")
print(f" Layers sharing experts: {total_tied} "
f"(each reuses leader's gate_up_proj + down_proj)")
print(f" Params: {params_before/1e9:.2f}B → {params_after/1e9:.2f}B "
f"(saved {saved/1e9:.2f}B, {saved/params_before*100:.1f}%)")
# Verify: check that tied layers actually share the same object
for group in groups:
leader = layers[group[0]]
for idx in group[1:]:
assert layers[idx].mlp.experts.gate_up_proj is leader.mlp.experts.gate_up_proj, \
f"Tying failed: layer {idx} gate_up_proj not shared with layer {group[0]}"
assert layers[idx].mlp.experts.down_proj is leader.mlp.experts.down_proj, \
f"Tying failed: layer {idx} down_proj not shared with layer {group[0]}"
# Router must NOT be tied
assert layers[idx].mlp.gate.weight is not leader.mlp.gate.weight, \
f"Bug: layer {idx} router is tied with layer {group[0]} — should be independent"
if master_process:
print(f" Verification: all ties correct, routers independent")
return info
def build_optimizer(model, optimizer_type, lr, weight_decay, args, config, master_process: bool = True):
"""Build optimizer. Muon for 2D hidden params, AdamW for embeddings/head/1D/Routers.
For 3D expert weight tensors [E, out, in] (from HF transformers v5 grouped_mm),
we create 2D proxy parameters that share storage with the 3D tensor. Each expert's
[out, in] slice becomes a separate 2D leaf param passed to Muon. A post-backward
hook copies the 3D grad into the proxy grads so Muon can step on them.
"""
if optimizer_type == "adamw":
standard_params = []
tied_expert_params = []
for name, p in model.named_parameters():
if not p.requires_grad: continue
is_tied_expert = False
if p.dim() == 3 and getattr(args, "tie_group_size", 0) > 1:
try:
parts = name.split('.')
if "layers" in parts:
layer_idx = int(parts[parts.index("layers") + 1])
if args.tie_skip_first <= layer_idx < (config.num_hidden_layers - args.tie_skip_last):
is_tied_expert = True
except Exception:
pass
if is_tied_expert: tied_expert_params.append(p)
else: standard_params.append(p)
# AdamW WD = 0.01 (Keller Jordan / nanoGPT convention).
# Override the `weight_decay` arg passed in (which comes from train_config dict).
adamw_wd = 0.01
return torch.optim.AdamW([
{"params": standard_params, "weight_decay": adamw_wd},
{
"params": tied_expert_params,
"is_tied_expert": True,
# Standard uncompensated weight decay
"weight_decay": adamw_wd,
}
], lr=lr, weight_decay=adamw_wd, betas=(0.9, 0.95))
elif optimizer_type == "muon":
muon_standard = []
muon_tied_experts = []
adamw_params = []
expert_proxies = []
raw = model.module if isinstance(model, DDP) else model
raw = raw._orig_mod if hasattr(raw, '_orig_mod') else raw
embed_head_keywords = {"embed_tokens", "lm_head", "wte", "wpe", "embed", "head"}
for name, p in model.named_parameters():
if not p.requires_grad:
continue
is_embed_head = any(kw in name for kw in embed_head_keywords)
# Identify Router
is_router = "gate" in name and "weight" in name and p.dim() == 2
if is_embed_head or is_router:
adamw_params.append(p)
elif p.dim() == 2:
muon_standard.append(p)
elif p.dim() == 3:
# Isolate ONLY the experts that fall within the explicitly tied layers
is_tied_expert = False
if getattr(args, "tie_group_size", 0) > 1:
try:
parts = name.split('.')
if "layers" in parts:
layer_idx = int(parts[parts.index("layers") + 1])
if args.tie_skip_first <= layer_idx < (config.num_hidden_layers - args.tie_skip_last):
is_tied_expert = True
except Exception:
pass
E = p.shape[0]
proxies = []
for i in range(E):
proxy = torch.nn.Parameter(p.data[i], requires_grad=False)
proxies.append(proxy)
if is_tied_expert:
muon_tied_experts.append(proxy)
else:
muon_standard.append(proxy)
expert_proxies.append((p, proxies))
else:
adamw_params.append(p)
if master_process:
n_muon = sum(p.numel() for p in muon_standard + muon_tied_experts)
n_adamw = sum(p.numel() for p in adamw_params)
print(f" Muon params: {n_muon:,} | AdamW params: {n_adamw:,}")
from torch.optim import Muon as TorchMuon
# Decoupled weight decay (Loshchilov & Hutter, 2019).
# Muon body weights use weight_decay=0.1, following common production MoE recipes
# (Llama, Qwen, DeepSeek). AdamW handles embeddings, output head, routers, and 1D
# parameters; weight_decay=0.01 follows Keller Jordan's reference Muon recipe.
muon_wd = 0.1
adamw_wd = 0.01
muon_opt = TorchMuon([
{"params": muon_standard},
{
"params": muon_tied_experts,
"is_tied_expert": True,
# Standard uncompensated weight decay
"weight_decay": muon_wd,
}
], lr=lr, momentum=0.95, nesterov=True, weight_decay=muon_wd)
adamw_opt = torch.optim.AdamW(
adamw_params,
lr=lr * 0.1,
weight_decay=adamw_wd,
betas=(0.9, 0.95),
)
class DualOptimizer:
def __init__(self, muon, adamw, expert_proxies):
self.muon = muon
self.adamw = adamw
self.expert_proxies = expert_proxies
self.param_groups = muon.param_groups + adamw.param_groups
def zero_grad(self, set_to_none=False):
# Zero the parameters registered to the standard optimisers.
self.muon.zero_grad(set_to_none=set_to_none)
self.adamw.zero_grad(set_to_none=set_to_none)
# The 3D expert tensors are not registered with either optimiser
# (they are mirrored through 2D proxies); zero their gradients explicitly.
for param_3d, _ in self.expert_proxies:
if param_3d.grad is not None:
if set_to_none:
param_3d.grad = None
else:
param_3d.grad.zero_()
def _sync_expert_grads(self):
# Copy the per-expert slices of the 3D parameter gradient into the proxy
# parameters that Muon optimises. The assignment is unconditional: experts
# that received no tokens still need a (zero) gradient slot so that decoupled
# weight decay is applied uniformly across all expert weights.
for param_3d, proxies in self.expert_proxies:
if param_3d.grad is None:
continue
for i, proxy in enumerate(proxies):
proxy.grad = param_3d.grad[i]
def step(self):
self._sync_expert_grads()
self.muon.step()
self.adamw.step()
def state_dict(self):
return {"muon": self.muon.state_dict(), "adamw": self.adamw.state_dict()}
def load_state_dict(self, state_dict):
self.muon.load_state_dict(state_dict["muon"])
self.adamw.load_state_dict(state_dict["adamw"])
return DualOptimizer(muon_opt, adamw_opt, expert_proxies)
else:
raise ValueError(f"Unknown optimizer: {optimizer_type}")
# ──────────────────────────────────────────────────────────────────────
# 3. LR Schedule
# ──────────────────────────────────────────────────────────────────────
def get_lr(step, warmup_steps, n_steps, lr, min_lr):
if step < warmup_steps:
return lr * (step + 1) / warmup_steps
progress = (step - warmup_steps) / max(1, n_steps - warmup_steps)
return min_lr + 0.5 * (lr - min_lr) * (1 + math.cos(math.pi * progress))
# ──────────────────────────────────────────────────────────────────────
# 4. Eval
# ──────────────────────────────────────────────────────────────────────
@torch.no_grad()
def evaluate(model, eval_batches, device):
model.eval()
total_loss = 0.0
total_tokens = 0
use_amp = device.type == "cuda"
for input_ids, targets in eval_batches:
input_ids = input_ids.to(device)
targets = targets.to(device)
with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=use_amp):
# Disable router logits during evaluation so HF skips the aux-loss computation.
out = model(input_ids=input_ids, labels=input_ids, output_router_logits=False)
total_loss += out.loss.item() * targets.numel()
total_tokens += targets.numel()
model.train()
avg_loss = total_loss / total_tokens
return avg_loss, math.exp(min(avg_loss, 20))
# ──────────────────────────────────────────────────────────────────────
# 5. Verification
# ──────────────────────────────────────────────────────────────────────
def verify_grouped_mm(model, config, arch, master_process: bool = True):
impl = getattr(config, "_experts_implementation", "unknown")
if not master_process:
return
print(f" experts_implementation: {impl}")
has_gmm = hasattr(torch.nn.functional, "grouped_mm")
has_gmm_priv = hasattr(torch, "_grouped_mm")
print(f" torch.nn.functional.grouped_mm: {has_gmm}")
try:
raw = model._orig_mod if hasattr(model, "_orig_mod") else model
# HF 5.x: don't use .base_model — it's a property returning the inner
# OlmoeModel directly, breaking the old `raw.base_model.model.layers`
# pattern that assumed PEFT-style wrapping.
if hasattr(raw, "model") and hasattr(raw.model, "layers"):
layers = raw.model.layers
elif hasattr(raw, "layers"):
layers = raw.layers
else:
raise AttributeError("Cannot locate layer list on model")
experts = layers[0].mlp.experts
print(f" gate_up_proj: {tuple(experts.gate_up_proj.shape)} "
f"(3D: {experts.gate_up_proj.ndim == 3})")
except Exception as e:
print(f" Could not inspect expert weights: {e}")
if impl == "grouped_mm" and (has_gmm or has_gmm_priv):
print(f" → Fused grouped GEMM active ✓")
elif impl == "batched_mm":
print(f" → Batched BMM (faster than loop, slower than grouped)")
else:
print(f" → Eager for-loop fallback")
# ──────────────────────────────────────────────────────────────────────
# 5b. Depth-scaled Xavier init
# ──────────────────────────────────────────────────────────────────────
def _reinit_weights_depth_scaled(model, config, master_process: bool = True):
"""Re-initialize weights with depth-scaled Xavier, matching custom model.py.
HF default is truncated_normal(σ=0.02) which ignores depth.
Custom model.py uses xavier_uniform with gain = 1/√(3·depth).
"""
eff_depth = config.num_hidden_layers
gain = 1.0 / math.sqrt(3 * eff_depth)
reinit_count = 0
for name, p in model.named_parameters():
if p.dim() == 2:
nn.init.xavier_uniform_(p, gain=gain)
reinit_count += 1
elif p.dim() == 3:
# Stacked expert weights (n_experts, out, in) — init each slice
for i in range(p.shape[0]):
nn.init.xavier_uniform_(p[i], gain=gain)
reinit_count += 1
# 1D params (norms, biases) keep their default init
if master_process:
print(f" Depth-scaled Xavier init: gain=1/√(3×{eff_depth})={gain:.4f}, "
f"re-initialized {reinit_count} tensors")
# ──────────────────────────────────────────────────────────────────────
# 6. Training
# ──────────────────────────────────────────────────────────────────────
def train(args):
# --- DDP SETUP ---
is_ddp = int(os.environ.get('RANK', -1)) != -1
if is_ddp:
dist.init_process_group("nccl")
ddp_rank = int(os.environ['RANK'])
ddp_local_rank = int(os.environ['LOCAL_RANK'])
ddp_world_size = int(os.environ['WORLD_SIZE'])
device = torch.device(f"cuda:{ddp_local_rank}")
torch.cuda.set_device(device)
master_process = ddp_rank == 0
else:
ddp_rank = 0
ddp_local_rank = 0
ddp_world_size = 1
master_process = True
device = torch.device(args.device) if args.device else (
torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu"))
# Force disable wandb on all worker processes
if not master_process:
os.environ["WANDB_MODE"] = "disabled"
tcfg = dict(TRAIN_CONFIGS.get(args.train_config, TRAIN_CONFIGS["default"]))
# CLI overrides
if args.n_steps is not None:
tcfg["n_steps"] = args.n_steps
if args.batch_size is not None:
tcfg["batch_size"] = args.batch_size
if args.grad_accum is not None:
tcfg["grad_accum"] = args.grad_accum
if args.seq_len is not None:
tcfg["seq_len"] = args.seq_len
if args.dry_run:
tcfg["n_steps"] = 100
tcfg["eval_every"] = 50
tcfg["log_every"] = 10
if master_process:
print("DRY RUN MODE: 100 steps")
use_amp = device.type == "cuda"
amp_ctx = lambda: torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=use_amp)
arch_name, config_fn = ARCH_REGISTRY[args.arch]
# --- Define unified run name for logs and checkpoints ---
tie_gs = args.tie_group_size if args.tie_group_size and args.tie_group_size > 1 else 0
run_name = f"{args.arch}-g{tie_gs}" if tie_gs else f"{args.arch}-notie"
# Append width expansion to run name so checkpoints don't overwrite!
if getattr(args, "expand_tied_experts", None) is not None:
run_name += f"-we{args.expand_tied_experts}"
if getattr(args, "scale", "regular") == "small":
run_name += "-small"
elif getattr(args, "scale", "regular") == "tiny":
run_name += "-tiny"
if master_process:
print(f"\n{'='*70}")
print(f" MoE TRAINING: {arch_name}")
print(f"{'='*70}")
print(f"Device: {device}")
if device.type == "cuda":
print(f" GPU: {torch.cuda.get_device_name()}")
print(f" VRAM: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB")
print(f"Device: {device} | DDP World Size: {ddp_world_size}")
# ── Build model ──
if master_process:
print(f"\nBuilding model ... (Scale: {args.scale})")
config = config_fn(scale=getattr(args, "scale", "regular"))
config._experts_implementation = "grouped_mm"
if args.arch == "qwen3moe":
from transformers import Qwen3MoeForCausalLM
model = Qwen3MoeForCausalLM(config)
else:
from transformers import OlmoeForCausalLM
model = OlmoeForCausalLM(config)
# ─── Router load-balancing + z-loss ──────
# HF 5.8's OlmoeTopKRouter / Qwen3MoeTopKRouter classes return raw logits as the
# first element of their output tuple, which propagates unchanged to
# out.router_logits. We can therefore consume gate_logits directly.
def heterogeneous_load_balancing_loss_func(gate_logits, num_experts, top_k, attention_mask=None):
import torch.nn.functional as F
if gate_logits is None or len(gate_logits) == 0:
return 0.0
# HF post-multiplies the return value by router_aux_loss_coef before
# adding it to the language-model loss. Pre-divide the z-loss term so
# the effective z-loss coefficient stays equal to args.z_loss_coef
# regardless of router_aux_loss_coef.
hf_aux_coef = getattr(config, "router_aux_loss_coef", 0.01)
actual_z_loss_coef = args.z_loss_coef / hf_aux_coef
aux_loss = 0.0
valid_layers = 0
for layer_logits in gate_logits:
if layer_logits is None:
continue
if isinstance(layer_logits, tuple):
layer_logits = layer_logits[0]
n_exp = layer_logits.size(-1)
flat_logits = layer_logits.view(-1, n_exp)
# Upcast to float32 before softmax/logsumexp to avoid bf16 overflow.
float_logits = flat_logits.float()
routing_probs = F.softmax(float_logits, dim=-1).type_as(flat_logits)
# Router z-loss (Zoph et al., 2022).
z_loss = torch.mean(torch.logsumexp(float_logits, dim=-1) ** 2) * actual_z_loss_coef
_, selected = torch.topk(routing_probs, top_k, dim=-1)
expert_mask = F.one_hot(selected, n_exp)
# The /top_k is required so frequencies sum to 1 (cf. HF #43688).
tokens_per_expert = expert_mask.sum(dim=1).float().mean(dim=0) / top_k
router_prob_per_expert = routing_probs.mean(dim=0)
# Per-layer load-balancing loss (Shazeer 2017; Fedus 2022).
layer_aux_loss = (tokens_per_expert * router_prob_per_expert).sum() * n_exp
aux_loss += layer_aux_loss + z_loss
valid_layers += 1
return aux_loss / valid_layers if valid_layers > 0 else 0.0
# ─── Robust patch injection across HF 5.3+ ────────────────────────────
def _install_router_loss_patch(fn, master_process):
"""Patch every known location of HF's load_balancing_loss_func.
Asserts at least one site was patched. If HF moves the symbol again,
this fails loudly at startup rather than silently letting HF's stock
implementation run (which would drop the z-loss term).
"""
targets = []
candidates = [
"transformers.models.olmoe.modeling_olmoe",
"transformers.models.qwen3_moe.modeling_qwen3_moe",
"transformers.loss.loss_utils",
"transformers.modeling_utils",
]
for modpath in candidates:
try:
mod = __import__(modpath, fromlist=["load_balancing_loss_func"])
except ImportError:
continue
if hasattr(mod, "load_balancing_loss_func"):
mod.load_balancing_loss_func = fn
targets.append(modpath)
assert targets, (
"load_balancing_loss_func not found in any known HF location. "
"HF transformers API has changed — patch is dead."
)
if master_process:
print(f" Patched load_balancing_loss_func in: {targets}")
_install_router_loss_patch(heterogeneous_load_balancing_loss_func, master_process)
# ── Heterogeneous Expert Transplant (For strict Iso-Parameter matching) ──
if getattr(args, "expand_tied_experts", None) is not None:
import copy
if master_process:
print(f"\n [Transplant] Building heterogeneous model: Middle layers expanded to {args.expand_tied_experts} experts...")
# 1. Create a config for the expanded middle layers
config_expanded = copy.deepcopy(config)
config_expanded.num_experts = args.expand_tied_experts
# 2. Build a temporary dummy model to generate the larger expert matrices
if args.arch == "qwen3moe":
dummy_model = Qwen3MoeForCausalLM(config_expanded)
else:
dummy_model = OlmoeForCausalLM(config_expanded)
# 3. Transplant the expanded MLPs into the middle layers of our real model
raw = model._orig_mod if hasattr(model, "_orig_mod") else model
dummy_raw = dummy_model._orig_mod if hasattr(dummy_model, "_orig_mod") else dummy_model
layers = raw.model.layers if hasattr(raw, "model") else raw.layers
dummy_layers = dummy_raw.model.layers if hasattr(dummy_raw, "model") else dummy_raw.layers
middle_start = args.tie_skip_first
middle_end = len(layers) - args.tie_skip_last
for i in range(middle_start, middle_end):
layers[i].mlp = dummy_layers[i].mlp
# 4. Free the dummy model from RAM
del dummy_model
if master_process:
print(f" [Transplant] Complete! Layers {middle_start} to {middle_end-1} now have {args.expand_tied_experts} experts.")
# ── Depth-scaled Xavier init (matching custom model.py) ──
_reinit_weights_depth_scaled(model, config, master_process)
# Gradient checkpointing
if args.gradient_checkpointing:
model.gradient_checkpointing_enable(
gradient_checkpointing_kwargs={"use_reentrant": False}
)
# ── Expert weight tying (before .to(device) to save GPU memory) ──
tie_info = {"tied": False}
if args.tie_group_size and args.tie_group_size > 1:
if master_process:
print(f"\nExpert weight tying:")
tie_info = tie_expert_layers(
model,
group_size=args.tie_group_size,
skip_first=args.tie_skip_first,
skip_last=args.tie_skip_last,
master_process=master_process,
)
model = model.to(device)
# Wrap in DDP
if is_ddp:
model = DDP(
model,
device_ids=[ddp_local_rank],
find_unused_parameters=False,
broadcast_buffers=False, # Transformers don't use BatchNorm, saves memory/time
gradient_as_bucket_view=True, # <--- THE VRAM SAVER
)
total_params = sum(p.numel() for p in model.parameters())
trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)
if master_process:
print(f" Total params: {total_params:>14,} ({total_params/1e9:.2f}B)")
print(f" Trainable: {trainable_params:>14,} ({trainable_params/1e9:.2f}B)")
print(f" Est. Adam VRAM: {trainable_params * 16 / 1e9:.1f} GB")
print(f" Est. Muon VRAM: {trainable_params * 12 / 1e9:.1f} GB")
if device.type == "cuda":
print(f" Model loaded: {torch.cuda.memory_allocated()/1e9:.1f} GB")
print(f"\nExpert backend:")
verify_grouped_mm(model, config, args.arch, master_process)
# ── Optimizer (BEFORE compile, so param ids match) ──
opt_type = args.optimizer
# If passed via CLI, use it. Otherwise, use the config dictionary.
if args.lr is not None:
opt_lr = args.lr
else:
opt_lr = tcfg["lr"]
if master_process:
print(f"\nOptimizer: {opt_type}, LR: {opt_lr}")
# Passing args and config through to safely parse the exact layer strings
optimizer = build_optimizer(model, opt_type, opt_lr, tcfg["weight_decay"], args, config, master_process)
# ── Checkpoint Resumption ──
start_step = 0
best_eval_loss = float("inf")
wandb_run_id = None
wandb_resume_step = None # step to resume wandb logging from (overwrites later data)
# --- Auto-Resume Logic ---
if args.auto_resume and not args.resume:
import glob
import re
save_dir_path = Path(args.save_dir)
# Look for checkpoints matching this exact run name
search_pattern = str(save_dir_path / f"{run_name}_step*.pt")
step_ckpts = glob.glob(search_pattern)
latest_ckpt = None
max_step = -1
for ckpt_str in step_ckpts:
# Extract the step number from the filename
match = re.search(r'_step(\d+)\.pt$', ckpt_str)
if match:
s = int(match.group(1))
if s > max_step:
max_step = s
latest_ckpt = ckpt_str
if latest_ckpt:
args.resume = latest_ckpt
if master_process:
print(f"\n [Auto-Resume] Detected crash/restart. Resuming from latest step: {latest_ckpt}")
else:
# Fallback to best if no step checkpoints exist yet
best_ckpt = save_dir_path / f"{run_name}_best.pt"
if best_ckpt.exists():
args.resume = str(best_ckpt)
if master_process:
print(f"\n [Auto-Resume] No step checkpoints found. Falling back to best: {best_ckpt}")
# --- Standard Resume Loading ---
if args.resume:
ckpt_path = Path(args.resume)
if ckpt_path.exists():
if master_process:
print(f"\n Resuming from checkpoint: {ckpt_path}")
# Load to CPU first to avoid a transient VRAM spike from duplicated tensors
# during deserialisation; load_state_dict moves tensors to the appropriate
# device afterwards.
ckpt = torch.load(ckpt_path, map_location="cpu", weights_only=False)
raw_model = model.module if is_ddp else model
raw_model = raw_model._orig_mod if hasattr(raw_model, "_orig_mod") else raw_model
raw_model.load_state_dict(ckpt["model_state_dict"])
if "optimizer_state_dict" in ckpt:
optimizer.load_state_dict(ckpt["optimizer_state_dict"])
# --- Move custom optimizer states (Muon) to the GPU ---
if hasattr(optimizer, "muon") and hasattr(optimizer, "adamw"):
for opt in [optimizer.muon, optimizer.adamw]:
for state in opt.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device)
else:
for state in optimizer.state.values():
for k, v in state.items():
if isinstance(v, torch.Tensor):
state[k] = v.to(device)
elif master_process:
print(" Warning: No optimizer state found in checkpoint.")
# --- Restore RNG states to prevent Dataloader scrambling ---
if "rng_state" in ckpt and ckpt["rng_state"] is not None:
torch.set_rng_state(ckpt["rng_state"])
if "cuda_rng_state" in ckpt and ckpt["cuda_rng_state"] is not None and torch.cuda.is_available():
torch.cuda.set_rng_state(ckpt["cuda_rng_state"])
# The checkpoint saved the step that was JUST COMPLETED (weights already updated).
# To avoid a double-update on the exact same data, we start at the NEXT step.
saved_step = ckpt.get("step", -1)
start_step = saved_step + 1 if saved_step >= 0 else 0
# Safely load best_eval_loss to prevent NoneType crash
best_eval_loss = ckpt.get("best_eval_loss", float("inf"))
if best_eval_loss is None:
best_eval_loss = float("inf")
wandb_run_id = ckpt.get("wandb_run_id")
wandb_resume_step = saved_step # used for resume_from to overwrite stale data
if master_process:
print(f" → Resumed at step {start_step} (Best loss: {best_eval_loss:.4f})")
else:
if master_process:
print(f"\n Checkpoint {ckpt_path} not found. Starting fresh.")
# ── Compile ──
if tcfg["compile_model"] and args.compile and hasattr(torch, "compile"):
try:
model = torch.compile(model)
if master_process:
print(f" torch.compile: enabled")
except Exception as e:
if master_process:
print(f" torch.compile: failed ({e})")
# ── Data (your DCLM-edu data module) ──
if master_process:
print(f"\nData:")
train_loader = get_dataloader(
seq_len=tcfg["seq_len"],
batch_size=tcfg["batch_size"],
num_workers=4,
)
eval_batches = get_eval_batches(
seq_len=tcfg["seq_len"],
batch_size=tcfg["batch_size"],
n_batches=tcfg["eval_batches"],
)
grad_accum = tcfg["grad_accum"]
eff_batch_tokens = tcfg["batch_size"] * grad_accum * tcfg["seq_len"] * ddp_world_size
if master_process:
print(f" Eval batches: {len(eval_batches)}")
print(f" Effective batch: {tcfg['batch_size']}×{grad_accum}×{tcfg['seq_len']}×{ddp_world_size}gpu = {eff_batch_tokens:,} tok/step")
# ── wandb ──
run = None
if HAS_WANDB and not args.no_wandb and master_process:
print(f"\nInitializing wandb ...", flush=True)
wandb_kwargs = {
"project": args.wandb_project,
"group": args.run_group,
"config": {
"arch": args.arch,
"arch_name": arch_name,
"optimizer": opt_type,
"lr": opt_lr,
"z_loss_coef": args.z_loss_coef,
"tied_lr_divisor": getattr(args, "tied_lr_divisor", 1.0),
"train_config": tcfg,
"total_params": total_params,
"trainable_params": trainable_params,
"experts_impl": getattr(config, "_experts_implementation", "unknown"),
"num_experts": config.num_experts,
"expand_tied_experts": args.expand_tied_experts,
"num_experts_per_tok": config.num_experts_per_tok,
"hidden_size": config.hidden_size,
"num_hidden_layers": config.num_hidden_layers,
"gradient_checkpointing": args.gradient_checkpointing,
"compile": args.compile,
"tie_info": tie_info,
# Explicit top-level fields for easy filtering in wandb
"batch_size": tcfg["batch_size"],
"grad_accum": grad_accum,
"seq_len": tcfg["seq_len"],
"ddp_world_size": ddp_world_size,
"eff_batch_tokens": eff_batch_tokens,
}
}
if wandb_run_id and args.resume:
# Standard append mode (WandB rewind is not working properly or not available)
wandb_kwargs["id"] = wandb_run_id
wandb_kwargs["resume"] = "must"
print(f" wandb: Resuming run {wandb_run_id} (append mode)", flush=True)
else:
wandb_kwargs["name"] = run_name
print(f" wandb: {run_name}", flush=True)