Skip to content

[training, perf] feat: Sync sequence packing (THD) E2E support from MLM PR 3386 - #3634

Closed
cuichenx wants to merge 1 commit into
mainfrom
chcui/sync-mlm-pr3386-thd-e2e
Closed

[training, perf] feat: Sync sequence packing (THD) E2E support from MLM PR 3386#3634
cuichenx wants to merge 1 commit into
mainfrom
chcui/sync-mlm-pr3386-thd-e2e

Conversation

@cuichenx

@cuichenx cuichenx commented May 1, 2026

Copy link
Copy Markdown
Contributor

Summary

Phase 1 port of the Bridge-training-side counterpart of NVIDIA/Megatron-LM#3386 ("Add E2E support for THD format"). Wires sequence-packing (THD) end-to-end through the Bridge training loop and FLOPs accounting. The new code paths are only entered when model_config.sequence_packing / model_config.sequence_packing_scheduler are set, so existing BSHD recipes are unaffected.

⚠️ Dependency

This PR depends on NVIDIA/Megatron-LM#3386 and the Megatron-Core submodule bump that ships:

  • megatron.core.datasets.data_schedule.wrap_data_iterator
  • megatron.core.datasets.data_schedule.get_batch_on_this_rank_for_sequence_packing
  • ModelParallelConfig.sequence_packing and TransformerConfig.sequence_packing_scheduler / max_seqlen_per_dp_cp_rank / hybrid_context_parallel

Until that lands and is bumped here, the new branches will fail at runtime when enabled. Imports are guarded (lazy) so the test suite and existing recipes still work without it.

Marked as draft to make this dependency explicit. Will undraft after the mcore bump.

Changes

File What
training/utils/flop_utils.py num_floating_point_operations accepts optional seqlen_sum_this_global_batch / seqlen_squared_sum_this_global_batch — both default to the BSHD geometry so existing callers and tests are exactly equivalent. Internal helpers (mlp/moe/attn/mamba/gdn/hybrid_layer_flops) refactored in lockstep. transformer_flops decomposes self-attention into a token-linear (× seqlen_sum) part and a sequence-quadratic (× seqlen_squared_sum) part across MLA, MHA/GQA, SWA, and GDN-override paths.
training/train.py train_step calls wrap_data_iterator when packing is enabled, returns 2 extra metrics (seqlen_sum_this_global_batch, seqlen_squared_sum_this_global_batch); train unpacks them and uses them for per-step FLOPs in the packed case while keeping the cached num_floating_point_operations_model * batch_size fast path for BSHD.
training/gpt_step.py get_batch dispatches to get_batch_on_this_rank_for_sequence_packing when cfg.model.sequence_packing_scheduler is set; the helper handles TP/PP broadcast and CP partitioning internally, so dispatch happens before the standard PP-stage gating. PackedSeqParams is projected into Bridge's flat 10-tuple.

Out of scope (deferred)

The MLM PR also adds:

  • MockSFTLowLevelDataset / MockSFTDataset (depends on pandas, only used for variable-length benchmarking)
  • --sft-mock-dataset-config-json validation logic and default-config plumbing in validate_args

These are skipped intentionally for Phase 1 and can be ported in a follow-up if the SFT mock-data flow is needed downstream.

Test plan

  • After mcore bump: enable sequence_packing_scheduler on a small Llama recipe, confirm packed iterator integration works end-to-end.
  • Verify existing BSHD recipes are bit-exactly unchanged (FLOPs accounting math is preserved when seqlen_sum / seqlen_squared_sum are derived from batch_size).
  • Run tests/unit_tests/training/utils/test_flop_utils.py — should pass without modification (BSHD path).
  • Run uv run pre-commit run --all-files (auto-formatter has already run via local pre-commit hook on this commit).

…LM PR 3386

Mirror the training-side changes from NVIDIA/Megatron-LM PR #3386 ("Add E2E
support for THD format") into the Bridge training loop. The mcore-side data
scheduler that this code depends on (``wrap_data_iterator``,
``get_batch_on_this_rank_for_sequence_packing``) ships as part of that PR; this
change is intentionally ahead of the submodule bump and the new code paths are
only entered when ``model_config.sequence_packing`` /
``model_config.sequence_packing_scheduler`` are set.

Changes:

- ``flop_utils.num_floating_point_operations``: refactor signature to accept
  ``seqlen_sum_this_global_batch`` and ``seqlen_squared_sum_this_global_batch``
  for THD packed accounting. When unset, both default to the BSHD geometry
  (``batch_size * seq_length`` and ``batch_size * seq_length**2``), preserving
  exact numerical equivalence for existing fixed-length callers and tests.
  Helpers ``mlp_layer_flops``, ``moe_layer_flops``, ``attn_layer_flops``,
  ``mamba_layer_flops``, ``gdn_layer_flops``, and ``hybrid_flops`` are updated
  in lockstep. The standard ``transformer_flops`` body decomposes
  self-attention into a token-linear part (* seqlen_sum) and a
  sequence-quadratic part (* seqlen_squared_sum); MLA, MHA/GQA, SWA, and
  GDN-override paths are all migrated.

- ``train.train_step``: when ``model_config.sequence_packing`` is enabled,
  delegate to ``wrap_data_iterator`` to obtain the packed iterator, the
  scheduler-determined microbatch count, and the seqlen sums needed for
  accurate FLOPs. The function now returns those two metrics as additional
  fields; ``train`` consumes them and uses them for per-step FLOPs accounting
  in the packed case while keeping the cached
  ``num_floating_point_operations_model * batch_size`` fast path for BSHD.

- ``gpt_step.get_batch``: when ``cfg.model.sequence_packing_scheduler`` is set,
  call ``get_batch_on_this_rank_for_sequence_packing`` (which handles TP/PP
  broadcast and CP partitioning internally) and project the resulting
  ``PackedSeqParams`` into Bridge's flat 10-tuple return shape. Dispatch
  happens before the standard PP-stage gating so middle PP stages still
  receive the cu_seqlens metadata the helper broadcasts.

Out of scope for this PR (Phase 1 only): mock SFT dataset support and the
``--sft-mock-dataset-config-json`` validation logic from MLM PR 3386 are not
ported. Those will come in a follow-up if needed.

Depends on: NVIDIA/Megatron-LM#3386 landing and the Megatron-Core submodule
being bumped to a commit that includes ``megatron.core.datasets.data_schedule``
and the new ``ModelParallelConfig`` / ``TransformerConfig`` fields.

Signed-off-by: Chen Cui <chcui@nvidia.com>
@copy-pr-bot

copy-pr-bot Bot commented May 1, 2026

Copy link
Copy Markdown

Auto-sync is disabled for draft pull requests in this repository. Workflows must be run manually.

Contributors can view more details about this message here.

@yaoyu-33 yaoyu-33 added the area:training Training loop, callbacks, and runtime integration label May 7, 2026
@cuichenx cuichenx closed this Jul 1, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:training Training loop, callbacks, and runtime integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants