Skip to content

perf(sequence-packing): use CPU sequence boundaries - #3932

Open
CarlosGomes98 wants to merge 5 commits into
NVIDIA-NeMo:mainfrom
CarlosGomes98:cgomes/sequence-packing-cpu-boundaries
Open

perf(sequence-packing): use CPU sequence boundaries#3932
CarlosGomes98 wants to merge 5 commits into
NVIDIA-NeMo:mainfrom
CarlosGomes98:cgomes/sequence-packing-cpu-boundaries

Conversation

@CarlosGomes98

@CarlosGomes98 CarlosGomes98 commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

What does this PR do ?

  • Convert many separate CPU-GPU syncs to 1-time bulk syncs
  • Move all required GPU tensors to host before entering the loss and logprob code. This eliminates repeated D2H copies
  • Introduce the to_cpu_int_tuple helper and CpuIntTuple type to establish a clear contract regarding when data is expected to be on host already.

A more detailed breakdown

  1. sequence_lengths.py introduces the helper conversion function and type
  2. automodel and megatrontrain.py handle the conversion before passing to host-side loss and logprob code
  3. model_utils.py, algorithms/loss/utils.py and algorithms/loss/wrapper.py explicitly accept CpuIntTuple and don't require .item() calls anymore. Also simplified some code that was recomputing seq_end as seq_start + padded_seq_lengths[seq_idx]
  4. models/megatron/data.py normalizes inputs once at entry, and then use CPU code

Issues

List issues that this PR closes (syntax):

Usage

  • You can potentially add a usage example below
# Add a code snippet demonstrating how to use this

Before your PR is "Ready for review"

Pre checks:

  • Make sure you read and followed Contributor guidelines
  • Did you write any new necessary tests?
  • Did you run the unit tests and functional tests locally? Visit our Testing Guide for how to run tests
  • Did you add or update any necessary documentation? Visit our Document Development Guide for how to write, build and test the docs.

Additional Information

  • ...

@CarlosGomes98
CarlosGomes98 requested review from a team as code owners August 31, 2026 14:19
@copy-pr-bot

copy-pr-bot Bot commented Aug 31, 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.

@CarlosGomes98

Copy link
Copy Markdown
Contributor Author

/ok to test 462d7e2

Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
@CarlosGomes98
CarlosGomes98 force-pushed the cgomes/sequence-packing-cpu-boundaries branch from 462d7e2 to 5534092 Compare September 1, 2026 22:17

@CarlosGomes98 CarlosGomes98 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

From the /review-pr skill, as requested:

LGTM. Well-scoped refactor converting per-.item() D2H syncs into a single bulk .tolist() conversion at the host/device boundary via the new to_cpu_int_tuple/CpuIntTuple contract.

Verified:

  • The wrapper simplification is exactly value-preserving: logit_end = seq_end // cp_size equals the old (seq_start + padded_seq_lengths[seq_idx]) // cp_size since seq_end = padded_cu_seqlens[i+1] and padded_seq_lengths[i] = padded_cu_seqlens[i+1] - padded_cu_seqlens[i]. Same holds for the fused-path min(...) branch. No off-by-one.
  • Every production caller either converts via to_cpu_int_tuple at the boundary or normalizes internally at function entry; tuples are compatible with all downstream ops (len(), indexing, slicing, //).
  • No circular-import risk (the new module imports only torch / collections.abc).
  • The added test_pack_input_ids_with_cpu_boundaries is correct.

CarlosGomes98 and others added 2 commits September 2, 2026 00:19
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
…dary

pack_rolled_draft_token_mask receives torch.Tensor cu_seqlens from the
draft-loss wrapper but passes them to _pack_input_ids, which is typed to
accept CpuIntTuple. Convert at the boundary via to_cpu_int_tuple, matching
the module's stated contract, clearing the pyrefly bad-argument-type errors.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
@CarlosGomes98
CarlosGomes98 force-pushed the cgomes/sequence-packing-cpu-boundaries branch from 5534092 to ff3be49 Compare September 1, 2026 22:20
@CarlosGomes98 CarlosGomes98 added the CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version) label Sep 1, 2026
@CarlosGomes98

Copy link
Copy Markdown
Contributor Author

/ok to test ff3be49

The new module was type-clean but absent from pyrefly.toml project-includes,
so its annotations were never checked. Add it to the whitelist.

Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
@CarlosGomes98

Copy link
Copy Markdown
Contributor Author

/ok to test 6cb9b58

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

Labels

CI:Lfast Runs a fast test suite and re-use nightly `main` container (but sync dependencies to PRs version)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant