[training, perf] fix: THD-aware FLOPS via cu_seqlens (Σᵢ sᵢ²) (re-land of #3839) - #4366
Conversation
| cp_use_thd = pg_collection.cp.size() == 1 | ||
| accumulate_flops_metadata( |
There was a problem hiding this comment.
Nit: "the behavior this test passed on before the THD change" references a specific test from this PR context — it'll go stale quickly. Consider rephrasing to something like "so CP > 1 stays on the BSHD fallback (which is correct for the non-THD path)".
Light Code ReviewClean re-land. The refactor centralises FLOPS accumulation into Minor observations
Test coverage Good coverage. Unit tests for Suggested test cases: No perf tests impacted. |
…d of #3839) Re-applies #3839 (reverted in #4363 after an accidental merge) with one fix. #3839's last commit added `cfg.checkpoint.load = None` to the CP+packing functional test (test_sft_example_runs_with_cp_and_packing). With pretrained_checkpoint also None, finetune() then fails its precondition (finetune.py:50) with "Finetuning requires a loading from a pretrained checkpoint or resuming from a checkpoint". This drops that line, restoring the pre-#3839 behavior (inherit the recipe's default load) so the test runs. The `use_distributed_optimizer=False` setting added to that test is kept: it works around an NCCL watchdog hang seen only under the distributed optimizer + context parallelism in this test (root-cause tracked separately; the THD-FLOPS code itself is inert under CP>1, taking the BSHD fallback). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com> Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
Signed-off-by: Chen Cui <chcui@nvidia.com>
a375ef6 to
1b9b525
Compare
|
/ok to test 1b9b525 |
| # (which would force a data-dependent-size sync) is needed. | ||
| sub_seq_lens = _real_subseq_lengths(cu_seqlens, cu_seqlens_argmin, cu_seqlens_unpadded, cu_seqlens_unpadded_argmin) | ||
| if sub_seq_lens is not None and sub_seq_lens.numel() > 0: | ||
| setattr(state, "_flops_requires_global_reduce", True) |
There was a problem hiding this comment.
what does this condition mean and why does it indicate THD layout?
There was a problem hiding this comment.
this mean we only do the DP all-reduce when trining with THD. For fixed seqlen training, we don't want to run allreduce
Re-lands #3839, which was reverted in #4363 after an accidental merge while CI was red.
This branch is no longer a byte-for-byte reland of #3839. It keeps the THD-aware FLOPs accounting and includes the follow-up fixes needed to make the reland correct and CI-stable.
What changed
1. THD-aware FLOPs accounting via
cu_seqlens.For packed/THD batches, attention FLOPs are computed from the real sub-sequence lengths (
Σᵢ sᵢ²) instead of treating the whole pack as one dense sequence (seq_len²). The metadata is accumulated in each forward step and folded intotrain_state.floating_point_operations_so_faronce per completed training step.Throughput logging now uses the cumulative FLOPs delta since the previous log event, so console, WandB, TensorBoard, MLflow, and Comet throughput use the same interval-level FLOPs source as the cumulative counter. This avoids the old current-step-only over/under-reporting behavior for variable-length THD logging intervals.
2. The DP FLOPs all-reduce is conditional.
The earlier implementation reduced FLOPs metadata whenever
data_parallel_size > 1, including dense BSHD training where every DP rank has identical fixed-length sequence stats. The final version adds_flops_requires_global_reduce, which is set only when real THDcu_seqlensmetadata is seen.local * dp_sizefor fixed-shape dense batches and avoids the extra collective.Σᵢ sᵢ²can differ.3. CP+packing functional test is stabilized.
The test now runs a proper pretrain -> SFT flow: it pretrains a tiny CP=2 checkpoint, then finetunes from that checkpoint, satisfying
finetune()'s checkpoint precondition. The pretrain and SFT model shapes are kept identical so checkpoint load validation is meaningful. The test also uses a tiny in-memory SQuAD-shaped dataset to avoid depending on externalrajpurkar/squadcache state in CI.use_distributed_optimizer=Falseremains in this CP+packing test as a test-local stability/memory workaround.4.
test_tensor_inspectmemory footprint is reduced.The tensor-inspect functional test uses smaller model and batch shapes and disables overlap/distributed-optimizer settings that are unnecessary for the behavior under test. This keeps the shared L0 functional-test job below CI runner memory limits.
CI failure context
The original CI symptom looked like a hang/NCCL watchdog timeout, but the evidence pointed to CUDA OOM in the shared single-process L0 functional-test session. Once one rank OOMs, the remaining ranks can block in the next collective and eventually hit the watchdog.
The final code avoids the unnecessary dense-path FLOPs all-reduce and trims the two memory-sensitive functional tests. The CP+packing test does not exercise the THD exact-reduce path in this configuration: it uses BSHD fallback FLOPs metadata and does not issue the FLOPs DP all-reduce.
Validation
L0_Launch_trainingandNemo_CICD_Test.Reverts the revert (#4363).