forked from pytorch/torchtitan
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeatures.py
More file actions
executable file
·623 lines (609 loc) · 21.4 KB
/
Copy pathfeatures.py
File metadata and controls
executable file
·623 lines (609 loc) · 21.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
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
# This source code is licensed under the BSD-style license found in the
# LICENSE file in the root directory of this source tree.
import dataclasses
import os
from tests.integration_tests import OverrideDefinitions
def _is_pp_only(variant: tuple[str, ...], ngpu: int) -> bool:
"""True when the variant has PP > 1 and no other SPMD parallelism > 1.
full_dtensor requires at least one SPMD axis > 1; PP-only runs collapse
every dense SPMD axis to size 1 and trip DTensor's reshape / flatten
rejection of Shard-on-degenerate-axis. Detected by parsing the explicit
``--parallelism.*_degree`` flags and back-computing ``dp_shard`` (default
-1, "fill remaining").
"""
degrees = {
"pipeline_parallel_degree": 1,
"data_parallel_replicate_degree": 1,
"data_parallel_shard_degree": -1,
"tensor_parallel_degree": 1,
"context_parallel_degree": 1,
"expert_parallel_degree": 1,
}
for arg in variant:
for key in degrees:
if key in arg:
try:
degrees[key] = int(arg.split()[-1])
except ValueError:
pass
break
if degrees["data_parallel_shard_degree"] == -1:
denom = (
degrees["pipeline_parallel_degree"]
* degrees["data_parallel_replicate_degree"]
* degrees["context_parallel_degree"]
* degrees["tensor_parallel_degree"]
)
degrees["data_parallel_shard_degree"] = max(1, ngpu // denom)
return degrees["pipeline_parallel_degree"] > 1 and all(
v <= 1 for k, v in degrees.items() if k != "pipeline_parallel_degree"
)
def _enable_full_dtensor(t: OverrideDefinitions) -> OverrideDefinitions:
"""Inject ``--parallelism.spmd_backend full_dtensor`` into every variant.
All features.py tests run under full_dtensor except PP-only variants
(see ``_is_pp_only``) and CP + compile variants (upstream symint
limitation); legacy non-full_dtensor coverage lives in models.py.
"""
new_args = []
for variant in t.override_args:
prefix: list[str] = []
has_cp = any("context_parallel_degree" in arg for arg in variant)
has_compile = any("compile.enable" in arg for arg in variant)
if not _is_pp_only(variant, t.ngpu) and not (has_cp and has_compile):
prefix.append("--parallelism.spmd_backend full_dtensor")
new_args.append(tuple(prefix) + tuple(variant))
return dataclasses.replace(t, override_args=tuple(new_args))
# Use RUNNER_TEMP if defined (GitHub Actions variable), else fallback to old path
runner_temp = os.getenv("RUNNER_TEMP")
if runner_temp:
checkpoint_path = os.path.join(
runner_temp,
"artifacts-to-be-uploaded/model_only_hf_checkpoint/hf_checkpoint/step-10/",
)
else:
checkpoint_path = (
"artifacts-to-be-uploaded/model_only_hf_checkpoint/hf_checkpoint/step-10/"
)
def build_features_test_list() -> list[OverrideDefinitions]:
"""
key is the config file name and value is a list of OverrideDefinitions
that is used to generate variations of integration tests based on the
same root config file.
"""
integration_tests_flavors = [
OverrideDefinitions(
[
[
"--profiler.enable_profiling",
"--metrics.enable_tensorboard",
],
],
"default",
"default",
),
OverrideDefinitions(
[
[
"--compile.enable",
],
],
"1D compile",
"1d_compile",
),
OverrideDefinitions(
[
[
"--compile.enable",
"activation-checkpoint:selective",
],
],
"1D compile with selective op AC",
"1d_compile_sac_op",
),
OverrideDefinitions(
[
[
"--parallelism.tensor_parallel_degree 2",
],
[
"--module llama3 --config llama3_debugmodel_ce_loss",
"--parallelism.tensor_parallel_degree 2",
],
],
"2D eager (ChunkedCELoss + standard CE loss with TP+loss_parallel)",
"2d_eager",
),
OverrideDefinitions(
[
[
"--parallelism.tensor_parallel_degree 2",
"--parallelism.no-enable-sequence-parallel",
],
],
"2D eager (SP disabled)",
"2d_eager_no_sp",
),
OverrideDefinitions(
[
[
"--compile.enable",
"--parallelism.tensor_parallel_degree 2",
],
],
"2D compile",
"2d_compile",
),
# TODO: re-enable this test once the async TP CI issue is fixed
OverrideDefinitions(
[
[
"--compile.enable",
"--parallelism.tensor_parallel_degree 2",
"--parallelism.enable_async_tensor_parallel",
],
],
"2D async TP compile",
"2d_asynctp_compile",
disabled=True,
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
],
[
"--checkpoint.enable",
"--training.steps 20",
],
],
"Checkpoint Integration Test - Save Load Full Checkpoint",
"full_checkpoint",
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
"--checkpoint.folder hf_checkpoint",
"--checkpoint.last_save_model_only",
"--checkpoint.last_save_in_hf",
],
[
"--checkpoint.enable",
f"--checkpoint.initial_load_path {checkpoint_path}",
"--checkpoint.initial_load_model_only",
"--checkpoint.initial_load_in_hf",
],
],
"Checkpoint Integration Test - save load model only checkpoint in HF definition and format",
"model_only_hf_checkpoint",
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
"--checkpoint.last_save_model_only",
"--checkpoint.export_dtype bfloat16",
],
],
"Checkpoint Integration Test - Save Model Only bf16",
"last_save_model_only_bf16",
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule 1F1B",
"--parallelism.data_parallel_shard_degree 1",
],
],
"PP 1D test 1F1B",
"pp_1f1b",
ngpu=2,
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule 1F1B",
"--parallelism.data_parallel_shard_degree 2",
],
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule 1F1B",
"--parallelism.pipeline_parallel_layers_per_stage 4",
"--parallelism.data_parallel_shard_degree 2",
],
],
"PP+DP 1F1B 2D test",
"pp_dp_1f1b",
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule GPipe",
"--parallelism.tensor_parallel_degree 2",
],
],
"PP+TP GPipe 2D test",
"pp_tp_gpipe",
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.data_parallel_shard_degree 2",
"--parallelism.tensor_parallel_degree 2",
],
[
"--training.steps 20",
"--checkpoint.enable",
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.data_parallel_shard_degree 2",
"--parallelism.tensor_parallel_degree 2",
],
],
"PP+DP+TP 3D test with save/load resume ckpt",
"pp_dp_tp",
ngpu=8,
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.data_parallel_shard_degree 2",
"--parallelism.tensor_parallel_degree 2",
"--compile.enable",
],
],
"PP+DP+TP 3D test with torch.compile",
"3d_compile",
ngpu=8,
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 4",
"--parallelism.pipeline_parallel_schedule Interleaved1F1B",
],
[
"--parallelism.pipeline_parallel_degree 4",
"--parallelism.pipeline_parallel_schedule Interleaved1F1B",
"--parallelism.pipeline_parallel_layers_per_stage 1",
],
],
"PP looped 1F1B test",
"pp_looped_1f1b",
ngpu=4,
),
# TODO: Disabled with the FlexAttention default (SDPA is no longer a
# language-model backend). Zero-bubble / multi schedules split backward
# and call torch's stage_backward_input, which runs
# _get_grad_fn_or_grad_acc (t.requires_grad) over every stage input —
# including the forwarded FlexAttention BlockMask, which is not a Tensor
# ("'BlockMask' object has no attribute 'requires_grad'"). Full-backward
# schedules (1F1B/GPipe/Interleaved1F1B) are unaffected. Re-enable once
# stage_backward_input skips non-tensor stage inputs upstream.
# (VarlenAttention's tensor-based metadata would sidestep this, but
# varlen requires flash_attn_interface/FA3, which the core integration
# CI does not install; SDPA is no longer a core LM backend. So the
# upstream stage_backward_input fix is the path here.)
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 4",
"--parallelism.pipeline_parallel_schedule InterleavedZeroBubble",
"activation-checkpoint:full",
],
],
"PP looped zero bubble test",
"pp_looped_zero_bubble",
ngpu=4,
disabled=True,
),
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule ZBVZeroBubble",
"activation-checkpoint:full",
],
],
"PP zero bubble test (v shaped)",
"pp_zbv",
ngpu=2,
disabled=True,
),
# TODO: Disabled for the same reason as the zero-bubble PP tests above:
# the custom CSV schedule splits backward (separate input-grad step),
# so stage_backward_input chokes on the forwarded FlexAttention
# BlockMask. Re-enable once stage_backward_input skips non-tensor inputs.
OverrideDefinitions(
[
[
"--parallelism.pipeline_parallel_degree 2",
"--parallelism.pipeline_parallel_schedule PipelineScheduleMulti",
"--parallelism.pipeline_parallel_schedule_csv ./tests/assets/custom_schedule.csv",
"activation-checkpoint:full",
],
],
"PP with custom pipeline schedule loaded from CSV file",
"pp_custom_csv",
ngpu=2,
disabled=True,
),
OverrideDefinitions(
[
[
"--optimizer.implementation fused_opt_states_bf16",
]
],
"BF16 Optimizer States Test",
"optimizer_bf16_states",
ngpu=2,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=1",
"--parallelism.data_parallel_replicate_degree=4",
]
],
"DDP",
"ddp",
ngpu=4,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=2",
"--parallelism.data_parallel_replicate_degree=2",
]
],
"HSDP",
"hsdp",
ngpu=4,
),
OverrideDefinitions(
[
[
"--parallelism.context_parallel_degree=4",
]
],
"CP",
"cp",
ngpu=4,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=2",
"--parallelism.data_parallel_replicate_degree=2",
"--parallelism.tensor_parallel_degree=2",
]
],
"HSDP+TP",
"hsdp+tp",
ngpu=8,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=2",
"--parallelism.context_parallel_degree=2",
]
],
"FSDP+CP",
"fsdp+cp",
ngpu=4,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=1",
"--parallelism.data_parallel_replicate_degree=2",
"--parallelism.context_parallel_degree=2",
]
],
"HSDP+CP (without dp_shard)",
"hsdp+cp_without_dp_shard",
ngpu=4,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=2",
"--parallelism.data_parallel_replicate_degree=2",
"--parallelism.context_parallel_degree=2",
]
],
"HSDP+CP (with dp_shard)",
"hsdp+cp_with_dp_shard",
ngpu=8,
),
OverrideDefinitions(
[
[
"--parallelism.data_parallel_shard_degree=2",
"--parallelism.tensor_parallel_degree=2",
"--parallelism.context_parallel_degree=2",
]
],
"FSDP+TP+CP",
"fsdp+tp+cp",
ngpu=8,
),
OverrideDefinitions(
[
[
"--parallelism.fsdp_reshard_after_forward always",
],
],
"Test always resharding after forward pass",
"fsdp_reshard_always",
ngpu=2,
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
"--training.steps 10",
],
# Save at [dp:4] and load at [dp:2, tp:2]. Note that the dataloader should be
# excluded during loading to avoid errors caused by mismatched dp_degree.
[
"--checkpoint.enable",
"--checkpoint.exclude_from_loading lr_scheduler,dataloader,optimizer",
"--parallelism.tensor_parallel_degree 2",
"--training.steps 20",
],
],
"Optional checkpoint",
"optional_checkpoint",
),
OverrideDefinitions(
[
[
# Local batch size = 8, and `ngpu=2`, so default
# global batch size = 8 * 2 = 16.
# To achieve 2 gradient accumulation steps, multiply
# default global batch size by 2. 16 * 2 = 32.
"--training.local_batch_size 8",
"--training.global_batch_size 32",
],
],
"Gradient accumulation",
"gradient_accumulation",
ngpu=2,
),
OverrideDefinitions(
[
[
"--validator.enable",
"--validator.dataloader.dataset c4_test",
"--parallelism.tensor_parallel_degree=2",
"--parallelism.context_parallel_degree=2",
"--parallelism.pipeline_parallel_degree=2",
"--parallelism.pipeline_parallel_schedule Interleaved1F1B",
],
],
"Validation test with tp, cp, pp",
"validation_tp_cp_pp",
ngpu=8,
),
OverrideDefinitions(
[
[
"--override.imports torchtitan.overrides.fused_swiglu",
"--parallelism.tensor_parallel_degree 2",
],
],
"Override: swap FeedForward with fused SwiGLU (FSDP2 + TP2)",
"override_fused_swiglu",
ngpu=4,
),
OverrideDefinitions(
[
[
"--module deepseek_v3 --config deepseek_v3_debugmodel",
"--override.imports torchtitan.overrides.fused_swiglu",
"--parallelism.tensor_parallel_degree 2",
"--parallelism.expert_parallel_degree 4",
],
],
"Override: fuse grouped experts + FFNs on deepseek_v3 "
"(FSDP2 + TP2 dense, EP4 sparse)",
"override_fused_grouped_experts",
ngpu=4,
),
# NOTE: below are tests which require config change that cannot be done
# via CLI overrides, so remain llama3 specific
OverrideDefinitions(
[
[
"--module llama3 --config llama3_debugmodel_varlen_attn",
"--parallelism.data_parallel_shard_degree=4",
"activation-checkpoint:selective",
]
],
"FSDP+VARLEN_ATTN + per op SAC",
"fsdp+varlen_attn+per_op_sac",
ngpu=4,
skip_rocm_test=True,
),
OverrideDefinitions(
[
[
"--module llama3 --config llama3_debugmodel_float8_emulate_lora",
"--parallelism.tensor_parallel_degree 2",
"--parallelism.pipeline_parallel_degree 2",
],
],
"Float8 emulate + LoRA training test",
"float8_emulate_lora",
ngpu=8,
),
OverrideDefinitions(
[
[
"--comm.mode torchcomms",
"--parallelism.context_parallel_degree 2",
"--parallelism.pipeline_parallel_degree 2",
"--compile.enable",
],
],
"FSDP+CP+PP+compile with torchcomms",
"torchcomms_3d_dp+cp+pp+compile",
ngpu=8,
skip_rocm_test=True,
# NotImplementedError: new_group cannot delegate to split_group
# with use_local_synchronization=True; split_group requires all
# ranks in the parent group to participate.
disabled=True,
),
OverrideDefinitions(
[
[
"--module llama3 --config llama3_debugmodel_ce_loss",
"--comm.mode torchcomms",
"--parallelism.tensor_parallel_degree 2",
"--parallelism.pipeline_parallel_degree 2",
"--compile.enable",
],
],
"FSDP+TP+PP+compile with torchcomms",
"torchcomms_3d_dp+tp+pp+compile",
ngpu=8,
skip_rocm_test=True,
# torchcomms-managed TP PG not registered in c10d;
# resolve fails under compile
disabled=True,
),
OverrideDefinitions(
[
[
"--module llama3 --config sft_debugmodel",
],
],
"SFT ChatDataset integration test",
"sft",
ngpu=2,
),
OverrideDefinitions(
[
[
"--checkpoint.enable",
"--checkpoint.create_seed_checkpoint",
],
],
"Seed checkpoint creation",
"seed_checkpoint",
ngpu=1,
timeout=30,
),
]
return [_enable_full_dtensor(t) for t in integration_tests_flavors]