Skip to content

Add data-schedule utilities and GPTDatasetConfig fields for sequence packing - #5903

Merged
ilml merged 6 commits into
NVIDIA:mainfrom
ilml:split/3386-03-packing-foundation
Aug 18, 2026
Merged

Add data-schedule utilities and GPTDatasetConfig fields for sequence packing#5903
ilml merged 6 commits into
NVIDIA:mainfrom
ilml:split/3386-03-packing-foundation

Conversation

@ilml

@ilml ilml commented Jul 20, 2026

Copy link
Copy Markdown
Contributor
  • I, the PR author, have personally reviewed every line of this PR.

What does this PR do?

Adds the new megatron/core/datasets/data_schedule_utils.py module (the unpack/gather/pack/reroute/broadcast pipeline used by the sequence-packing schedulers) and four inert GPTDatasetConfig fields consumed by later PRs in the series.

Per review feedback (@parthmannan), this PR also dedupes HybridCPDataLoaderWrapper against the new utils where provably behavior-preserving: the DP-group seqlen gather now delegates to _get_global_seqlens_and_ids (verified equivalent — same collectives, dtypes, and results; get_global_seqlens kept as a compat shim). The two remaining near-duplicates are deliberately NOT folded and carry NOTE comments explaining why:

  • reroute_samples_to_hdp_ranks: the utils variant uses a torch.empty(1) empty-send fallback that fails c10d split-size validation in a reachable hybrid-CP case where the existing torch.empty(0) works, plus PP-modulo rank mapping and original/padded_seq_len key special-casing.
  • unpack_batch: the utils variant copies a fixed key list, injects synthesized length keys (changing the __next__ output contract consumed by hybrid_cp_schedule.py and adding two collectives per batch), and squeezes in place.

Open question for @xiaoyao0115: the utils reroute_samples_to_dcp_ranks torch.empty(1) empty-send fallback looks incorrect if ever reached — if it moves to torch.empty(0), the reroute duplication (~105 lines) becomes foldable in a follow-up.

This PR now bases directly on main, so the diff shows exactly its own 3 files.

Part 03/10 of splitting #3386 (Add E2E support for THD format; dev-branch PR #2924). Original changes by @xiaoyao0115 in #3386 — split into functionally self-contained PRs to ease review. Hard dependencies (must merge first): none — independently mergeable.

Split series (#3386)

Part PR Hard deps (merge first)
01 #5901
02 #5902
03 #5903
04 #6589 #5903
05 #5905 #6589
06 #5906 #5902, #5903
07 #5907 #5902, #6589, #5905
08 #5908
09 #5909 #5906, #5908
10 #5910 #5907, #5909

Branches are stacked linearly (each on the previous) so every PR shows a clean own-diff once its base is retargeted to the copy-pr-bot pull-request/<parent> ref; until then the Files-changed view of a stacked PR includes its ancestors — its own change is the last commit.
Stacked on #5902.

Review notes

  • data_schedule_utils direct unit tests (test_unpack_batch_*) arrive with the varlen test file later in the series (they need varlen_dataset.py to collect); the module is also exercised by test_sequence_packing.py in the two follow-up PRs.

Issue tracking

Linked issue: Related to #3386

Contribution process

Pre-checks

  • I have added relevant unit tests
  • I have added relevant functional tests
  • I have added proper typing to my code Typing guidelines
  • I have added relevant documentation
  • I have run the autoformatter.sh on my PR

🤖 Generated with Claude Code

@copy-pr-bot

copy-pr-bot Bot commented Jul 20, 2026

Copy link
Copy Markdown

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@ilml
ilml marked this pull request as ready for review July 22, 2026 01:02
@ilml
ilml requested review from a team as code owners July 22, 2026 01:02
…packing

Split 3/10 from NVIDIA#3386 (sequence packing / THD E2E support). Adds the
data_schedule_utils module (unpack/gather/pack/reroute/broadcast
pipeline) and four inert GPTDatasetConfig fields consumed by later
split PRs.

Per review feedback, HybridCPDataLoaderWrapper now delegates its
DP-group seqlen gather to data_schedule_utils (proven equivalent);
the two near-duplicates that differ semantically (reroute empty-send
fallback, unpack output contract) are kept with NOTE comments
explaining why.

Original changes by @xiaoyao0115 in NVIDIA#3386.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Signed-off-by: ilml <tolong@nvidia.com>
@ilml
ilml force-pushed the split/3386-03-packing-foundation branch from bce5ecd to a8835e8 Compare July 27, 2026 20:04
@Connor-XY

Copy link
Copy Markdown
Contributor

/claude review

@claude claude Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM — clean extraction of shared data-schedule helpers into data_schedule_utils.py. The offsets-semantics change is behavior-preserving (get_global_seqlens slices offsets[:-1] and bucketize(gid, offsets[1:]-1) is unaffected by the extra trailing boundary since gid <= total-1), and the NOTE: comments documenting why the sequence-packing variants are intentionally not reused on the hybrid-CP path are very helpful. One minor docstring nit left inline.

Comment thread megatron/core/datasets/data_schedule_utils.py Outdated
Comment thread megatron/core/datasets/gpt_dataset.py Outdated
Comment thread megatron/core/datasets/gpt_dataset.py
Comment thread megatron/core/datasets/data_schedule_utils.py Outdated
Comment thread megatron/core/datasets/data_schedule_utils.py Outdated
Comment thread megatron/core/datasets/data_schedule_utils.py Outdated
Comment thread megatron/core/datasets/data_schedule_utils.py
…oadcast

Review feedback on NVIDIA#5903:

- get_batch_and_global_seqlens: document all five returned values; the
  Returns block only listed three (offsets and seqlens_gathered were
  missing).
- _unpack_batch: return early on an empty batch instead of raising
  IndexError; hoist the collate-dimension normalization out of both
  branches and assert the leading dimension is 1 before squeezing, since
  squeeze(0) is a silent no-op otherwise; decide the input shape once for
  the whole batch and assert every sample agrees and carries exactly one
  of padded_seq_len (already unpacked) or cu_seqlens (pre-packed).
- _unpack_batch: rename the local dev to device, and replace the stale
  micro_batch_size comment -- the packing path installs an identity
  collate_fn, so the limitation is specific to the default collate_fn.
- broadcast_to_pp_group: document who needs which tensors, why the last
  PP rank stays in the collective but skips the unpacking, and what the
  pp_group.size() > 2 guard actually tests. No behavior change.
- GPTDatasetConfig: clarify that sequence_packing_scheduler mirrors the
  ModelParallelConfig knob and name the BasePackingScheduler extension
  point; mark varlen_sbhd_validation as a verification-only knob with a
  TODO to drop it once a THD-vs-SBHD parity test exists.

Also ran black on data_schedule_utils.py, which was not formatted.

Original changes by @xiaoyao0115 in NVIDIA#3386.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: ilml <tolong@nvidia.com>
Comment thread megatron/core/datasets/gpt_dataset.py Outdated
Per review feedback: nothing reads this field. The scheduler is selected
from ModelParallelConfig.sequence_packing_scheduler -- wrap_data_iterator
reads it off the model config alongside max_seqlen_per_dp_cp_rank and
microbatch_group_size_per_vp_stage -- and the dataloader/collate choice
reads args.sequence_packing_scheduler directly. The dev branch, which
carries the other three dataset-config fields added here, does not mirror
this one either.

Original changes by @xiaoyao0115 in NVIDIA#3386.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Signed-off-by: ilml <tolong@nvidia.com>
@svcnvidia-nemo-ci svcnvidia-nemo-ci added the Final Review PR is in the "final review" stage label Aug 4, 2026
@ilml

ilml commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a15b5ab

@svcnvidia-nemo-ci svcnvidia-nemo-ci added Approved All necessary approvals have been made and removed Final Review PR is in the "final review" stage labels Aug 17, 2026
@ilml
ilml enabled auto-merge August 17, 2026 22:18
@ilml

ilml commented Aug 17, 2026

Copy link
Copy Markdown
Contributor Author

/ok to test a15b5ab

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Approved All necessary approvals have been made complexity: medium deepseekv4 DeepSeek V4 PRs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

10 participants