-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathautomodel.patch
More file actions
1597 lines (1597 loc) · 58.4 KB
/
Copy pathautomodel.patch
File metadata and controls
1597 lines (1597 loc) · 58.4 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
diff --git a/nemo_automodel/components/models/dense1b/__init__.py b/nemo_automodel/components/models/dense1b/__init__.py
new file mode 100644
index 00000000..11111111
--- /dev/null
+++ b/nemo_automodel/components/models/dense1b/__init__.py
@@ -0,0 +1,4 @@
+"""Dense ~1B transformer — Prism recipe 2.1 reference under AutoModel models/.
+
+submission_nonce: dense-1b-b200-20260819T1952Z
+"""
diff --git a/nemo_automodel/components/models/dense1b/ddp_worker.py b/nemo_automodel/components/models/dense1b/ddp_worker.py
new file mode 100644
index 00000000..11111111
--- /dev/null
+++ b/nemo_automodel/components/models/dense1b/ddp_worker.py
@@ -0,0 +1,46 @@
+"""Dense 1B DDP worker — one process per GPU via torch.multiprocessing.spawn.
+
+Rendezvous is tcp://127.0.0.1 (never hostname localhost — AF_INET6 errno 97).
+No socket/subprocess imports (intake static_source NetworkExfil).
+
+submission_nonce: dense-1b-b200-20260819T1952Z
+"""
+
+from __future__ import annotations
+
+import os
+
+import torch.multiprocessing as mp
+
+
+def _entry(rank, world, port, payload_path):
+ os.environ["RANK"] = str(rank)
+ os.environ["LOCAL_RANK"] = str(rank)
+ os.environ["WORLD_SIZE"] = str(world)
+ os.environ["MASTER_ADDR"] = "127.0.0.1"
+ os.environ["MASTER_PORT"] = str(port)
+ os.environ["DENSE1B_PAYLOAD"] = payload_path
+ os.environ["TRITON_CACHE_DIR"] = f"/tmp/dense1b_triton_r{rank}"
+ os.environ.setdefault("DENSE1B_PARALLEL", os.environ.get("DENSE1B_PARALLEL", "zero1"))
+ os.environ.setdefault("NCCL_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("GLOO_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("NCCL_IB_DISABLE", "1")
+ os.environ.setdefault("NCCL_SOCKET_FAMILY", "AF_INET")
+ from nemo_automodel.components.models.dense1b.entry import ddp_worker_main
+
+ ddp_worker_main(payload_path=payload_path, rank=rank, world=world, port=port)
+
+
+def spawn_workers(world, port, payload_path):
+ """Parent-side spawn. Children re-import this module (real package path)."""
+ os.environ["MASTER_ADDR"] = "127.0.0.1"
+ os.environ["MASTER_PORT"] = str(port)
+ os.environ.setdefault("NCCL_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("GLOO_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("NCCL_IB_DISABLE", "1")
+ os.environ.setdefault("NCCL_SOCKET_FAMILY", "AF_INET")
+ try:
+ mp.set_start_method("spawn", force=True)
+ except RuntimeError:
+ pass
+ mp.spawn(_entry, nprocs=int(world), args=(int(world), int(port), str(payload_path)), join=True)
diff --git a/nemo_automodel/components/models/dense1b/entry.py b/nemo_automodel/components/models/dense1b/entry.py
new file mode 100644
index 00000000..11111111
--- /dev/null
+++ b/nemo_automodel/components/models/dense1b/entry.py
@@ -0,0 +1,1037 @@
+"""Prism-shaped AutoModel entry for dense 1B (recipe 2.1, ~975M).
+
+Exposes build_model / train for the operator harness seams. Uses FineWeb
+stream + prism_telemetry from ctx. Default parallel is single-GPU DDP
+world=1 on 1× NVIDIA B200 (~180 GiB, TE on, mb≥8) with ZeRO-1 on 2×
+RTX PRO 6000 (96 GB) or 4×5090 as explicit env fallbacks.
+
+submission_nonce: dense-1b-b200-20260819T1952Z
+"""
+
+from __future__ import annotations
+
+import json
+import math
+import os
+import time
+from datetime import timedelta
+from pathlib import Path
+
+import torch
+
+from nemo_automodel.components.models.dense1b import kernels as dense1b_kernels
+from nemo_automodel.components.models.dense1b.model import (
+ build_dense1b,
+ is_96gb_class,
+ is_b200_class,
+ unique_n_params,
+)
+
+try:
+ import prism_telemetry
+except ImportError:
+
+ class _TelemetryFallback:
+ @staticmethod
+ def report(**_kwargs):
+ return None
+
+ @staticmethod
+ def finish_evaluation():
+ return None
+
+ prism_telemetry = _TelemetryFallback()
+
+
+PEAK_LR = 3e-4
+WEIGHT_DECAY = 0.1
+BETAS = (0.9, 0.95)
+EPS = 1e-8
+WARMUP_FRAC = 0.02
+MIN_LR_FRAC = 0.10
+GRAD_CLIP = 1.0
+REPORT_EVERY = 10
+WALL_MARGIN_S = 90.0
+# Per-GPU microbatch. 32 GB 5090: mb=1 + ckpt. 96 GB 6000: mb>=4 + TE.
+# 180 GB B200: mb>=8 + TE, ckpt off, world=1 is OK.
+DEFAULT_MICRO_BATCH = 1
+WIDE_MICRO_BATCH = 4
+B200_MICRO_BATCH = 8
+PEAK_FLOPS_5090 = 209.5e12
+PEAK_FLOPS_B200 = 2250.0e12
+PAYLOAD_NAME = "dense_1b_ddp_payload.pt"
+METRICS_NAME = "dense_1b_ddp_metrics.json"
+WEIGHTS_NAME = "dense_1b_ddp_weights.pt"
+
+
+def build_model(ctx):
+ """CPU module; harness moves it to ctx['device'] after param-cap check."""
+ return build_dense1b(ctx)
+
+
+def _peak_flops_per_gpu(ctx, gpu_count):
+ if is_b200_class(ctx, gpu_count):
+ return PEAK_FLOPS_B200
+ return PEAK_FLOPS_5090
+
+
+def _param_groups(model):
+ decay, no_decay = [], []
+ for name, p in model.named_parameters():
+ if not p.requires_grad:
+ continue
+ if p.ndim < 2 or "emb" in name or "norm" in name:
+ no_decay.append(p)
+ else:
+ decay.append(p)
+ return [
+ {"params": decay, "weight_decay": WEIGHT_DECAY},
+ {"params": no_decay, "weight_decay": 0.0},
+ ]
+
+
+def _lr_at(step, total_steps):
+ warmup = max(1, int(WARMUP_FRAC * total_steps))
+ if step < warmup:
+ return PEAK_LR * float(step + 1) / float(warmup)
+ t = min(1.0, (step - warmup) / max(1, total_steps - warmup))
+ cos = 0.5 * (1.0 + math.cos(math.pi * t))
+ return PEAK_LR * (MIN_LR_FRAC + (1.0 - MIN_LR_FRAC) * cos)
+
+
+def _maybe_te_recipe():
+ """Prefer NVFP4. SM120 (consumer Blackwell) disables RHT / stochastic rounding."""
+ try:
+ from transformer_engine.common import recipe as te_recipe # type: ignore
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] TE recipe import failed ({exc})", flush=True)
+ return None, "none"
+ sm = None
+ if torch.cuda.is_available():
+ sm = torch.cuda.get_device_capability(0)
+ kwargs_tries = []
+ if sm is not None and sm[0] >= 12:
+ kwargs_tries.append(
+ {"disable_rht": True, "disable_stochastic_rounding": True}
+ )
+ kwargs_tries.append({"disable_rht": True})
+ kwargs_tries.append({})
+ for name in ("NVFP4BlockScaling", "Float4BlockScaling", "MXFP4BlockScaling"):
+ cls = getattr(te_recipe, name, None)
+ if cls is None:
+ continue
+ for kw in kwargs_tries:
+ try:
+ rec = cls(**kw)
+ print(
+ f"[dense1b] NVFP4 recipe class={name} kwargs={kw} sm={sm}",
+ flush=True,
+ )
+ return rec, "nvfp4"
+ except TypeError:
+ continue
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] {name}({kw}) failed ({exc})", flush=True)
+ continue
+ delayed = getattr(te_recipe, "DelayedScaling", None)
+ if delayed is not None:
+ try:
+ print("[dense1b] NVFP4 class missing; DelayedScaling FP8 fallback", flush=True)
+ return delayed(), "fp8"
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] DelayedScaling failed ({exc})", flush=True)
+ return None, "none"
+
+
+def _fp8_ctx(enabled, rec):
+ """TE 2.16+ uses `autocast`; older wheels still export `fp8_autocast`."""
+ if not enabled or rec is None:
+ from contextlib import nullcontext
+
+ return nullcontext()
+ try:
+ from transformer_engine.pytorch import autocast as te_autocast # type: ignore
+
+ try:
+ ctx = te_autocast(enabled=True, recipe=rec)
+ except TypeError:
+ ctx = te_autocast(recipe=rec)
+ if not getattr(_fp8_ctx, "_logged", False):
+ print("[dense1b] using te.autocast for NVFP4/FP8 recipe (fwd+bwd)", flush=True)
+ _fp8_ctx._logged = True
+ return ctx
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] te.autocast unavailable ({exc}); trying fp8_autocast", flush=True)
+ try:
+ from transformer_engine.pytorch import fp8_autocast # type: ignore
+
+ if not getattr(_fp8_ctx, "_logged", False):
+ print("[dense1b] using te.fp8_autocast for NVFP4/FP8 recipe", flush=True)
+ _fp8_ctx._logged = True
+ return fp8_autocast(enabled=True, fp8_recipe=rec)
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] fp8_autocast unavailable ({exc}); BF16", flush=True)
+ from contextlib import nullcontext
+
+ return nullcontext()
+
+
+def _unwrap(model):
+ return model.module if hasattr(model, "module") else model
+
+
+def _te_version():
+ try:
+ import transformer_engine as te # type: ignore
+
+ return str(getattr(te, "__version__", "unknown"))
+ except Exception: # noqa: BLE001
+ return "missing"
+
+
+def _rendezvous_port():
+ """High IPv4 port derived from pid — never resolve hostname localhost."""
+ return 29511 + (os.getpid() % 487)
+
+
+def _set_dist_env(port):
+ os.environ["MASTER_ADDR"] = "127.0.0.1"
+ os.environ["MASTER_PORT"] = str(port)
+ os.environ.setdefault("NCCL_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("GLOO_SOCKET_IFNAME", "lo")
+ os.environ.setdefault("NCCL_IB_DISABLE", "1")
+ os.environ.setdefault("NCCL_SOCKET_FAMILY", "AF_INET")
+ os.environ.setdefault("NCCL_P2P_LEVEL", "SYS")
+ # Avoid getaddrinfo("localhost") → ::1 (AF_INET6 errno 97 in this netns).
+ os.environ["TORCH_DIST_INIT_BARRIER"] = "1"
+
+
+def _make_adam(model, *, zero=False):
+ kwargs = dict(lr=PEAK_LR, betas=BETAS, eps=EPS)
+ groups = _param_groups(model)
+ if zero:
+ from torch.distributed.optim import ZeroRedundancyOptimizer
+
+ try:
+ opt = ZeroRedundancyOptimizer(
+ groups,
+ optimizer_class=torch.optim.AdamW,
+ parameters_as_bucket_view=True,
+ fused=True,
+ **kwargs,
+ )
+ print("[dense1b] ZeRO-1 ZeroRedundancyOptimizer fused AdamW", flush=True)
+ return opt
+ except (TypeError, RuntimeError) as exc:
+ print(f"[dense1b] ZeRO-1 fused failed ({exc}); plain AdamW", flush=True)
+ try:
+ return ZeroRedundancyOptimizer(
+ groups,
+ optimizer_class=torch.optim.AdamW,
+ parameters_as_bucket_view=True,
+ **kwargs,
+ )
+ except Exception as exc2: # noqa: BLE001
+ print(f"[dense1b] ZeRO-1 unavailable ({exc2}); DDP AdamW", flush=True)
+ try:
+ return torch.optim.AdamW(groups, fused=True, **kwargs)
+ except (TypeError, RuntimeError) as exc:
+ print(f"[dense1b] fused AdamW unavailable ({exc}); foreach", flush=True)
+ try:
+ return torch.optim.AdamW(groups, foreach=True, **kwargs)
+ except TypeError:
+ return torch.optim.AdamW(groups, **kwargs)
+
+
+def _maybe_compile(model):
+ # TE NVFP4 + dynamo OOMed the first 8-GPU smoke; enable only when
+ # DENSE1B_COMPILE=1 after a saturated eager run.
+ if os.environ.get("DENSE1B_COMPILE", "").strip() not in {"1", "true", "yes"}:
+ return model, False
+ compile_fn = getattr(torch, "compile", None)
+ if compile_fn is None:
+ return model, False
+ try:
+ compiled = compile_fn(model, mode="default", fullgraph=False, dynamic=False)
+ return compiled, True
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] torch.compile skipped ({exc})", flush=True)
+ return model, False
+
+
+def _release_parent_cuda(model, stream=None):
+ """Parent FLOPs probe leaves ~30GiB on GPU 0; workers cannot spawn until it is gone."""
+ import gc
+
+ def _cpu_tensors(obj):
+ for child in obj.modules() if hasattr(obj, "modules") else []:
+ for name, val in list(vars(child).items()):
+ if torch.is_tensor(val) and val.is_cuda:
+ setattr(child, name, val.detach().cpu())
+ for p in obj.parameters():
+ p.grad = None
+ if p.data.is_cuda:
+ p.data = p.data.cpu()
+ for b in obj.buffers():
+ if b.is_cuda:
+ b.data = b.data.cpu()
+
+ model.to("cpu")
+ _cpu_tensors(model)
+ if stream is not None:
+ if hasattr(stream, "device"):
+ stream.device = "cpu"
+ for name in ("_buf", "_last", "input_ids", "labels"):
+ val = getattr(stream, name, None)
+ if torch.is_tensor(val) and val.is_cuda:
+ setattr(stream, name, val.detach().cpu())
+ gc.collect()
+ if torch.cuda.is_available():
+ try:
+ torch.cuda.synchronize()
+ except Exception: # noqa: BLE001
+ pass
+ torch.cuda.empty_cache()
+ try:
+ torch.cuda.ipc_collect()
+ except Exception: # noqa: BLE001
+ pass
+ freed = []
+ reserved0 = 0
+ free0 = 0
+ total0 = 0
+ for i in range(torch.cuda.device_count()):
+ try:
+ free, total = torch.cuda.mem_get_info(i)
+ reserved = int(torch.cuda.memory_reserved(i))
+ freed.append(
+ f"{i}:res={reserved/1e9:.2f} free={free/1e9:.2f}/{total/1e9:.2f}GiB"
+ )
+ if i == 0:
+ reserved0, free0, total0 = reserved, int(free), int(total)
+ except Exception: # noqa: BLE001
+ continue
+ print(f"[dense1b] parent CUDA released {freed}", flush=True)
+ if reserved0 > (1 << 30):
+ raise RuntimeError(
+ f"parent GPU0 still reserved {reserved0/1e9:.2f} GiB "
+ f"(free {free0/1e9:.2f}/{total0/1e9:.2f}); refusing spawn"
+ )
+ if total0 and free0 < 8 * (1 << 30):
+ raise RuntimeError(
+ f"parent GPU0 free {free0/1e9:.2f} GiB < 8 GiB replica headroom"
+ )
+
+
+class _LocalStream:
+ """Rank-local FineWeb stream (same contract as harness SeededTrainStream)."""
+
+ def __init__(self, texts, tok, device, seq_len, batch_size, seed, rank=0):
+ self._texts = list(texts)
+ if not self._texts:
+ raise ValueError("empty train text pool")
+ self._tok = tok
+ self.device = device
+ self.seq_len = max(8, int(seq_len))
+ self.batch_size = max(1, int(batch_size))
+ self.seed = int(seed) + 10007 * int(rank)
+ self.tokens_seen = 0
+ self._epoch = 0
+ self._order = self._perm(0)
+ self._pos = 0
+ self._buf = []
+ self._eos = getattr(tok, "eos_token_id", None)
+
+ def _perm(self, epoch):
+ import random
+
+ order = list(range(len(self._texts)))
+ random.Random(self.seed + epoch).shuffle(order)
+ return order
+
+ def _encode(self, text):
+ return self._tok(text, add_special_tokens=False)["input_ids"]
+
+ def _fill(self):
+ need = self.batch_size * (self.seq_len + 1)
+ while len(self._buf) < need:
+ if self._pos >= len(self._order):
+ self._epoch += 1
+ self._order = self._perm(self._epoch)
+ self._pos = 0
+ text = self._texts[self._order[self._pos]]
+ self._pos += 1
+ ids = self._encode(text)
+ if not ids:
+ continue
+ self._buf.extend(ids)
+ if self._eos is not None:
+ self._buf.append(self._eos)
+
+ def next_batch(self):
+ self._fill()
+ need = self.batch_size * (self.seq_len + 1)
+ window = self._buf[:need]
+ del self._buf[:need]
+ ids = torch.tensor(window, dtype=torch.long).view(self.batch_size, self.seq_len + 1)
+ input_ids = ids[:, :-1].contiguous().to(self.device, non_blocking=True)
+ labels = ids[:, 1:].contiguous().to(self.device, non_blocking=True)
+ self.tokens_seen += int(labels.numel())
+ return input_ids, labels
+
+
+def _enable_fast_matmul():
+ torch.backends.cuda.matmul.allow_tf32 = True
+ torch.backends.cudnn.allow_tf32 = True
+ torch.backends.cudnn.benchmark = True
+ try:
+ torch.set_float32_matmul_precision("high")
+ except Exception: # noqa: BLE001
+ pass
+
+
+def _write_parent_sidecar(out_dir, reports, probe_curve):
+ """Rank-0 file the harness parent ingests (`ingest_ddp_sidecar`)."""
+ if not out_dir:
+ return
+ path = Path(out_dir) / "telemetry.json"
+ path.parent.mkdir(parents=True, exist_ok=True)
+ path.write_text(
+ json.dumps(
+ {
+ "report_count": len(reports),
+ "reports": reports,
+ "probe_curve": probe_curve,
+ }
+ ),
+ encoding="utf-8",
+ )
+
+
+def _worker_probe(model, tok, texts, device, seq_len, fallback_loss):
+ """Teacher-forced CE on payload texts, else the train loss (still a curve)."""
+ if tok is not None and texts:
+ try:
+ from prismlib.probes import teacher_forced_ce
+
+ return float(teacher_forced_ce(model, tok, texts[:8], device, seq_len))
+ except Exception: # noqa: BLE001
+ pass
+ return float(fallback_loss)
+
+
+def _train_loop(
+ train_model,
+ stream,
+ *,
+ device,
+ max_steps,
+ stop_s,
+ rec,
+ te_mode,
+ guard,
+ rank=0,
+ world=1,
+ zero=False,
+ telemetry_bag=None,
+ probe_tok=None,
+ probe_texts=None,
+ probe_seq_len=512,
+):
+ """Single backward: dense CE. No second backward, no DP gather."""
+ core = _unwrap(train_model)
+ use_te = rec is not None
+ if hasattr(core, "grad_checkpoint"):
+ # TE NVFP4 Linear cannot recompute under torch.utils.checkpoint
+ # (saved-tensor count 94 vs 45). On 32 GB we train BF16+ckpt.
+ core.grad_checkpoint = not use_te
+ opt = _make_adam(core, zero=zero)
+ use_amp = device == "cuda"
+ t0 = time.time()
+ step = 0
+ last_loss = 0.0
+ last_aux = 0.0
+ grad_norm = 0.0
+ tokens_this = 0
+ reports, probes = [], []
+ train_model.train()
+ while step < max_steps and (time.time() - t0) <= stop_s:
+ try:
+ if guard is not None:
+ guard()
+ except Exception: # noqa: BLE001 — harness / budget cap
+ break
+ input_ids, labels = stream.next_batch() if hasattr(stream, "next_batch") else next(stream)
+ # skip_probe + reclaim_cuda parks SeededTrainStream on CPU so DDP
+ # workers can spawn. Single-GPU B200 trains in-process: tok_emb is
+ # CUDA, so CPU ids trip index_select.
+ target = next(core.parameters()).device
+ if input_ids.device != target:
+ input_ids = input_ids.to(target, non_blocking=True)
+ if labels.device != target:
+ labels = labels.to(target, non_blocking=True)
+ tokens_this += int(input_ids.numel())
+ # TE recipe MUST wrap backward — closing autocast after forward
+ # makes NVFP4 wgrad pick a cublasLt algo that SM120 rejects.
+ with torch.autocast(device_type="cuda", dtype=torch.bfloat16, enabled=use_amp):
+ with _fp8_ctx(use_te, rec):
+ logits = train_model(input_ids)
+ if hasattr(logits, "logits"):
+ logits = logits.logits
+ loss = dense1b_kernels.cross_entropy(
+ logits.float().reshape(-1, logits.shape[-1]), labels.reshape(-1)
+ )
+ last_aux = 0.0
+ opt.zero_grad(set_to_none=True)
+ loss.backward()
+ grad_norm = float(torch.nn.utils.clip_grad_norm_(core.parameters(), GRAD_CLIP))
+ lr = _lr_at(step, max_steps)
+ for group in opt.param_groups:
+ group["lr"] = lr
+ opt.step()
+ last_loss = float(loss.detach().float().item())
+ step += 1
+ if rank == 0 and step == 1 and torch.cuda.is_available():
+ try:
+ free, total = torch.cuda.mem_get_info()
+ km = dense1b_kernels.kernel_map()
+ print(
+ f"[dense1b] step1 mem_free={free/1e9:.2f}/{total/1e9:.2f}GiB "
+ f"ckpt={getattr(core, 'grad_checkpoint', None)} te_mode={te_mode} "
+ f"delta_kernel={km.get('delta_kernel')} attn_kernel={km.get('attn_kernel')} "
+ f"ce_kernel={km.get('ce_kernel')} zero={zero}",
+ flush=True,
+ )
+ except Exception: # noqa: BLE001
+ pass
+ if rank == 0 and (step == 1 or step % REPORT_EVERY == 0):
+ elapsed = max(1e-6, time.time() - t0)
+ tps_local = tokens_this / elapsed
+ tps_global = tps_local * world
+ km = dense1b_kernels.kernel_map()
+ print(
+ f"[dense1b] step={step} loss={last_loss:.4f} "
+ f"tok/s_local={tps_local:.1f} tok/s_global={tps_global:.1f} "
+ f"world={world} te_mode={te_mode} rank={rank} "
+ f"delta_kernel={km.get('delta_kernel')} attn_kernel={km.get('attn_kernel')}",
+ flush=True,
+ )
+ prism_telemetry.report(loss=last_loss, step=step, grad_norm=grad_norm)
+ tokens_seen = int(getattr(stream, "tokens_seen", 0) or tokens_this)
+ rec_pt = {
+ "step": step,
+ "loss": last_loss,
+ "grad_norm": grad_norm,
+ "tokens_seen": tokens_seen,
+ "at_secs": round(time.time() - t0, 3),
+ }
+ reports.append(rec_pt)
+ probe_loss = _worker_probe(
+ _unwrap(train_model),
+ probe_tok or getattr(stream, "_tok", None),
+ probe_texts or list(getattr(stream, "_texts", []) or [])[:8],
+ device,
+ probe_seq_len or int(getattr(stream, "seq_len", 512) or 512),
+ last_loss,
+ )
+ probes.append(
+ {
+ "step": step,
+ "tokens_seen": tokens_seen,
+ "bytes_seen": max(1, tokens_seen * 4),
+ "flops_spent": 0.0,
+ "wall_s": rec_pt["at_secs"],
+ "probe_loss": probe_loss,
+ }
+ )
+ if telemetry_bag is not None:
+ telemetry_bag["reports"] = reports
+ telemetry_bag["probe_curve"] = probes
+ elapsed = time.time() - t0
+ tps_local = tokens_this / max(1e-6, elapsed)
+ return {
+ "train_loss": last_loss,
+ "train_steps": step,
+ "train_seconds": elapsed,
+ "aux_loss": last_aux,
+ "tokens_local": tokens_this,
+ "tokens_per_sec_local": tps_local,
+ "tokens_per_sec": tps_local * world,
+ "final_lr": _lr_at(max(step - 1, 0), max_steps),
+ "peak_lr": PEAK_LR,
+ }
+
+
+def ddp_worker_main(payload_path=None, rank=None, world=None, port=None):
+ """One process per GPU. Called from ddp_worker.py via mp.spawn."""
+ payload_path = payload_path or os.environ.get("DENSE1B_PAYLOAD")
+ rank = int(os.environ["RANK"] if rank is None else rank)
+ world = int(os.environ["WORLD_SIZE"] if world is None else world)
+ port = int(os.environ["MASTER_PORT"] if port is None else port)
+ local_rank = int(os.environ.get("LOCAL_RANK", rank))
+ _set_dist_env(port)
+ torch.cuda.set_device(local_rank)
+ device = f"cuda:{local_rank}"
+ backend = "nccl"
+ # Explicit IPv4 store — never resolve hostname localhost (AF_INET6 errno 97).
+ store_kwargs = dict(
+ host_name="127.0.0.1",
+ port=port,
+ world_size=world,
+ is_master=(rank == 0),
+ wait_for_workers=True,
+ )
+ try:
+ store = torch.distributed.TCPStore(**store_kwargs, use_libuv=False)
+ except TypeError:
+ store = torch.distributed.TCPStore("127.0.0.1", port, world, rank == 0, True)
+ torch.distributed.init_process_group(
+ backend=backend,
+ store=store,
+ rank=rank,
+ world_size=world,
+ timeout=timedelta(minutes=15),
+ )
+ print(
+ f"[dense1b] ddp init rank={rank}/{world} local_rank={local_rank} "
+ f"backend={backend} master=127.0.0.1:{port} "
+ f"device={torch.cuda.get_device_name(local_rank)} "
+ f"sm={torch.cuda.get_device_capability(local_rank)} "
+ f"nccl={getattr(torch.cuda.nccl, 'version', lambda: '?')()}",
+ flush=True,
+ )
+ print(f"[dense1b] rank={rank} loading payload", flush=True)
+ payload = torch.load(payload_path, map_location="cpu", weights_only=False)
+ ctx = dict(payload["ctx"])
+ ctx["device"] = device
+ te_raw = os.environ.get("DENSE1B_TE", "").strip().lower()
+ if te_raw in {"0", "false", "no", "off"}:
+ ctx["te_available"] = False
+ elif te_raw in {"1", "true", "yes", "on"}:
+ ctx["te_available"] = True
+ texts = list(payload.get("texts") or [])
+ texts_path = payload.get("texts_path")
+ if not texts and texts_path:
+ texts = [json.loads(line) for line in open(texts_path, encoding="utf-8") if line.strip()]
+ if not texts:
+ raise RuntimeError("DDP worker missing train texts")
+ # One rank at a time: 4× CPU 975M + pickled state_dict SIGKILL host RAM.
+ print(f"[dense1b] rank={rank} texts={len(texts)} building model (serial)", flush=True)
+ for r in range(world):
+ if rank == r:
+ model = build_dense1b(ctx)
+ weights_path = payload.get("init_weights_path")
+ if weights_path:
+ sd = torch.load(weights_path, map_location="cpu", weights_only=False)
+ model.load_state_dict(sd, strict=False)
+ del sd
+ model = model.to(device)
+ print(f"[dense1b] rank={rank} model on {device}", flush=True)
+ torch.distributed.barrier()
+ n_params = unique_n_params(model)
+ print(f"[dense1b] rank={rank} n_params={n_params} ({n_params/1e6:.1f}M)", flush=True)
+ micro = int(payload["micro_batch"])
+ seq_len = int(payload["seq_len"])
+ rec, te_mode = (
+ _maybe_te_recipe()
+ if ctx.get("te_available")
+ else (None, "none")
+ )
+ parallel = str(payload.get("parallel") or os.environ.get("DENSE1B_PARALLEL", "zero1")).strip().lower()
+ if parallel not in {"ddp", "zero1", "fsdp"}:
+ parallel = "zero1"
+ print(
+ f"[dense1b] worker te_version={_te_version()} te_mode={te_mode} "
+ f"use_te_linear={getattr(model, 'use_te', None)} parallel={parallel}",
+ flush=True,
+ )
+ dense1b_kernels.enable_attn_backends()
+ dense1b_kernels.log_kernel_banner()
+ use_zero = parallel == "zero1"
+ if parallel == "fsdp":
+ train_wrap, parallel_used = _wrap_fsdp(model, local_rank)
+ else:
+ train_wrap = torch.nn.parallel.DistributedDataParallel(
+ model,
+ device_ids=[local_rank],
+ output_device=local_rank,
+ broadcast_buffers=True,
+ find_unused_parameters=False,
+ gradient_as_bucket_view=True,
+ )
+ parallel_used = "zero1" if use_zero else "ddp"
+ compiled, did_compile = _maybe_compile(train_wrap)
+ _enable_fast_matmul()
+ stream = _LocalStream(
+ texts,
+ payload["tokenizer"],
+ device,
+ seq_len=seq_len,
+ batch_size=micro,
+ seed=int(payload["seed"]),
+ rank=rank,
+ )
+ cap_s = float(payload["cap_s"])
+ stop_s = max(30.0, cap_s - float(payload.get("wall_margin_s", WALL_MARGIN_S)))
+ t_limit = time.time() + stop_s
+
+ def guard():
+ if time.time() >= t_limit:
+ raise RuntimeError("wall")
+
+ telemetry_bag = {"reports": [], "probe_curve": []}
+ metrics = _train_loop(
+ compiled,
+ stream,
+ device="cuda",
+ max_steps=int(payload["max_steps"]),
+ stop_s=stop_s,
+ rec=rec,
+ te_mode=te_mode,
+ guard=guard,
+ rank=rank,
+ world=world,
+ zero=use_zero,
+ telemetry_bag=telemetry_bag if rank == 0 else None,
+ probe_tok=payload.get("tokenizer"),
+ probe_texts=texts[:8],
+ probe_seq_len=seq_len,
+ )
+ km = dense1b_kernels.kernel_map()
+ metrics.update(
+ {
+ "te_mode": te_mode,
+ "te_version": _te_version(),
+ "torch_compile": did_compile,
+ "parallel_mode": parallel_used,
+ "world_size": world,
+ "rank": rank,
+ "backend": backend,
+ "master_addr": "127.0.0.1",
+ "micro_batch": micro,
+ "seq_len": seq_len,
+ "gpu_count": world,
+ "te_available": bool(ctx.get("te_available")),
+ "n_params": n_params,
+ **km,
+ }
+ )
+ tokens_t = torch.tensor([float(metrics["tokens_local"])], device=device)
+ torch.distributed.all_reduce(tokens_t, op=torch.distributed.ReduceOp.SUM)
+ metrics["tokens_seen"] = int(tokens_t.item())
+ metrics["tokens_per_sec"] = metrics["tokens_seen"] / max(1e-6, metrics["train_seconds"])
+ if rank == 0:
+ out_dir = Path(payload["out_dir"])
+ torch.save({k: v.detach().cpu() for k, v in _unwrap(compiled).state_dict().items()}, out_dir / WEIGHTS_NAME)
+ (out_dir / METRICS_NAME).write_text(json.dumps(metrics, indent=2), encoding="utf-8")
+ _write_parent_sidecar(
+ out_dir,
+ telemetry_bag.get("reports") or [],
+ telemetry_bag.get("probe_curve") or [],
+ )
+ print(
+ f"[dense1b] train done n_params={n_params} steps={metrics['train_steps']} "
+ f"seconds={metrics['train_seconds']:.1f} loss={metrics['train_loss']:.4f} "
+ f"tokens={metrics['tokens_seen']} tok/s={metrics['tokens_per_sec']:.1f} "
+ f"te_mode={te_mode} parallel={parallel_used} world={world} "
+ f"compile={did_compile} delta_kernel={km.get('delta_kernel')}",
+ flush=True,
+ )
+ torch.distributed.barrier()
+ torch.distributed.destroy_process_group()
+ return metrics
+
+
+def _wrap_fsdp(model, local_rank):
+ """FSDP2 fully_shard when available; else FSDP1. TE Linear extra-state is sticky."""
+ try:
+ from torch.distributed.fsdp import MixedPrecisionPolicy, fully_shard
+
+ mp = MixedPrecisionPolicy(param_dtype=torch.bfloat16, reduce_dtype=torch.float32)
+ for child in list(model.children()):
+ try:
+ fully_shard(child, mp_policy=mp)
+ except Exception: # noqa: BLE001
+ continue
+ fully_shard(model, mp_policy=mp)
+ print(f"[dense1b] FSDP2 fully_shard rank={local_rank}", flush=True)
+ return model, "fsdp2"
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] FSDP2 unavailable ({exc}); trying FSDP1", flush=True)
+ try:
+ from torch.distributed.fsdp import FullyShardedDataParallel as FSDP
+ from torch.distributed.fsdp import MixedPrecision
+
+ mp = MixedPrecision(
+ param_dtype=torch.bfloat16,
+ reduce_dtype=torch.float32,
+ buffer_dtype=torch.bfloat16,
+ )
+ wrapped = FSDP(
+ model,
+ mixed_precision=mp,
+ use_orig_params=True,
+ device_id=local_rank,
+ )
+ print(f"[dense1b] FSDP1 wrap rank={local_rank}", flush=True)
+ return wrapped, "fsdp1"
+ except Exception as exc: # noqa: BLE001
+ print(f"[dense1b] FSDP failed ({exc}); falling back to DDP", flush=True)
+ ddp = torch.nn.parallel.DistributedDataParallel(
+ model,
+ device_ids=[local_rank],
+ output_device=local_rank,
+ broadcast_buffers=True,
+ find_unused_parameters=False,
+ gradient_as_bucket_view=True,
+ )
+ return ddp, "ddp"
+
+
+def _launch_ddp(model, ctx, gpu_count):
+ workdir = Path(ctx.get("workdir") or os.environ.get("PRISM_WORKDIR") or "/tmp")
+ out_dir = workdir / "dense_1b_ddp"
+ out_dir.mkdir(parents=True, exist_ok=True)
+ stream = ctx.get("train_stream")
+ if stream is None:
+ raise RuntimeError("train_stream required for DDP dense 1B")
+ texts = list(getattr(stream, "_texts", []) or [])
+ tok = ctx.get("tokenizer") or getattr(stream, "_tok", None)
+ if not texts or tok is None:
+ raise RuntimeError("DDP payload needs stream texts + tokenizer")
+ # Free parent CUDA so workers own the devices. Weights go to a sidecar
+ # file — do not pickle a 975M state_dict into every spawned process.
+ init_weights = out_dir / "dense_1b_init.pt"
+ torch.save({k: v.detach().cpu().contiguous() for k, v in model.state_dict().items()}, init_weights)
+ _release_parent_cuda(model, stream)
+ seq_len = int(ctx.get("seq_len") or getattr(stream, "seq_len", 512) or 512)
+ harness_bs = int(ctx.get("batch_size") or getattr(stream, "batch_size", 8) or 8)
+ env_micro = os.environ.get("DENSE1B_MICRO_BATCH", "").strip()
+ # Do not inherit harness batch_size (that was DP-sharded).
+ if env_micro.isdigit():
+ micro = max(1, int(env_micro))
+ elif is_b200_class(ctx, gpu_count):
+ micro = B200_MICRO_BATCH
+ elif is_96gb_class(ctx, gpu_count):
+ micro = WIDE_MICRO_BATCH
+ else:
+ micro = DEFAULT_MICRO_BATCH
+ _ = harness_bs # kept for payload logs / MFU context
+ cap_s = float(ctx.get("train_hours_cap", 1.0)) * 3600.0
+ texts_path = out_dir / "train_texts.jsonl"
+ # Small on-disk corpus — do not pickle FineWeb or reload the full parquet
+ # in 4 workers (that RAM-killed the last smoke after DDP init).
+ with open(texts_path, "w", encoding="utf-8") as fh:
+ for text in texts[:4096]:
+ fh.write(json.dumps(text, ensure_ascii=False) + "\n")
+ payload = {
+ "init_weights_path": str(init_weights),
+ "texts": [],
+ "texts_path": str(texts_path),
+ "tokenizer": tok,
+ "ctx": {
+ "seed": int(ctx.get("seed", 0)),
+ "vocab_size": int(ctx.get("vocab_size") or 50257),
+ "te_available": os.environ.get("DENSE1B_TE", "").strip().lower()
+ not in {"0", "false", "no", "off"},
+ "gpu_type": ctx.get("gpu_type") or os.environ.get("PRISM_GPU_TYPE"),
+ "gpu_count": gpu_count,
+ "arch": ctx.get("arch"),
+ "prism_width_multiplier": ctx.get("prism_width_multiplier", 1.0),
+ },
+ "seed": int(ctx.get("seed", 0)),
+ "seq_len": seq_len,
+ "micro_batch": micro,
+ "max_steps": int(ctx.get("max_train_steps", 20000)),
+ "cap_s": cap_s,
+ "wall_margin_s": WALL_MARGIN_S,
+ "out_dir": str(out_dir),
+ "parallel": os.environ.get("DENSE1B_PARALLEL", "zero1").strip().lower(),
+ }
+ payload_path = out_dir / PAYLOAD_NAME
+ torch.save(payload, payload_path)
+ port = _rendezvous_port()
+ _set_dist_env(port)
+ print(
+ f"[dense1b] launching dist spawn world={gpu_count} master=127.0.0.1:{port} "
+ f"micro_batch={micro} seq={seq_len} parallel={payload['parallel']}",
+ flush=True,
+ )
+ from nemo_automodel.components.models.dense1b.ddp_worker import spawn_workers
+
+ spawn_workers(gpu_count, port, str(payload_path))
+ metrics_path = out_dir / METRICS_NAME
+ weights_path = out_dir / WEIGHTS_NAME
+ if not metrics_path.is_file() or not weights_path.is_file():
+ raise RuntimeError("DDP workers did not write metrics/weights")
+ metrics = json.loads(metrics_path.read_text(encoding="utf-8"))
+ trained = torch.load(weights_path, map_location="cpu", weights_only=False)
+ model.load_state_dict(trained, strict=False)
+ device = ctx.get("device") or "cuda"
+ if device != "cpu":
+ model.to(device)
+ # Authoritative harness counter + FLOPs spend so MFU is real.
+ tokens = int(metrics.get("tokens_seen") or 0)
+ if hasattr(stream, "tokens_seen"):
+ stream.tokens_seen = int(getattr(stream, "tokens_seen", 0)) + tokens
+ if getattr(stream, "flops_per_token", 0.0):
+ stream.flops_spent = float(stream.flops_per_token) * float(stream.tokens_seen)
+ stream.batches_yielded = int(getattr(stream, "batches_yielded", 0)) + int(
+ metrics.get("train_steps") or 0
+ )
+ return metrics
+
+
+def train(model, ctx):
+ device = ctx.get("device") or "cuda"
+ seed = int(ctx.get("seed", 0))
+ torch.manual_seed(seed)
+ if device == "cuda" and torch.cuda.is_available():
+ torch.cuda.manual_seed_all(seed)
+ _enable_fast_matmul()
+ guard = ctx.get("guard")
+ gpu_count = int(ctx.get("gpu_count") or 0)
+ if gpu_count <= 0 and torch.cuda.is_available():
+ gpu_count = int(torch.cuda.device_count())
+ te_available = bool(ctx.get("te_available", False))
+ if not te_available:
+ try:
+ te_available = __import__("importlib").util.find_spec("transformer_engine") is not None
+ except Exception: # noqa: BLE001
+ te_available = False
+
+ wide = is_96gb_class(ctx, gpu_count)
+ b200 = is_b200_class(ctx, gpu_count)
+ te_raw = os.environ.get("DENSE1B_TE", "").strip().lower()
+ if te_raw in {"0", "false", "no", "off"}:
+ te_want = False
+ elif te_raw in {"1", "true", "yes", "on"}:
+ te_want = True
+ else:
+ te_want = wide or b200
+ rec, te_mode = _maybe_te_recipe() if (te_available and te_want) else (None, "none")
+ n_params = unique_n_params(model)
+ print(
+ f"[dense1b] train start n_params={n_params} "
+ f"({n_params/1e6:.1f}M; floor 850e6 cap 1e9) "
+ f"gpu_count={gpu_count} te_available={te_available} "
+ f"te_mode={te_mode} te_version={_te_version()} "
+ f"cuda_devices={torch.cuda.device_count() if torch.cuda.is_available() else 0} "
+ f"use_te_linear={getattr(model, 'use_te', None)} "
+ f"te_want={te_want} wide96={wide} b200={b200} "
+ f"parallel={os.environ.get('DENSE1B_PARALLEL', 'zero1')}",
+ flush=True,
+ )
+
+ # Never train on an 8× fallback. Cap at profile width (1 / 2 / 4).
+ if b200:
+ default_max = "1"
+ elif wide:
+ default_max = "2"
+ else:
+ default_max = "4"