Skip to content

perf(sequence-packing): keep packed-sequence boundaries on CPU - #3933

Open
CarlosGomes98 wants to merge 7 commits into
NVIDIA-NeMo:mainfrom
CarlosGomes98:cgomes/optimize-logprob-prepare-input
Open

perf(sequence-packing): keep packed-sequence boundaries on CPU#3933
CarlosGomes98 wants to merge 7 commits into
NVIDIA-NeMo:mainfrom
CarlosGomes98:cgomes/optimize-logprob-prepare-input

Conversation

@CarlosGomes98

Copy link
Copy Markdown
Contributor

What does this PR do ?

Depends on #3932 . Currently contains the full diff but should only be merged after #3932

Captures packed-sequence boundaries before the batch moves to CUDA and carries them through the Megatron forward pass. Loss and logprob post-processing can then reuse the CPU metadata without synchronizing after forward.

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:22
@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 CarlosGomes98 changed the title Cgomes/optimize logprob prepare input perf(sequence-packing): keep packed-sequence boundaries on CPU Aug 31, 2026
@CarlosGomes98

Copy link
Copy Markdown
Contributor Author

/ok to test 580c579

Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
@CarlosGomes98
CarlosGomes98 force-pushed the cgomes/optimize-logprob-prepare-input branch from 580c579 to 240914c 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. Clean, value-preserving perf refactor keeping packed-sequence boundaries on CPU; the retyping is applied consistently across every call site.

Verified:

  • _build_packed_sequence_metadata reproduces _pack_sequences_for_megatron's cu_seqlens/cu_seqlens_padded for all three configs (non-model-slices, model-slices, VLM), including the needs_padding=False, pad_packed_seq_to_multiple_of, and pad_full_seq_to (PP>1) edge cases.
  • The rewritten loss loop is value-preserving (seq_end == seq_start + padded_seq_lengths[seq_idx]), and all downstream consumers use integer indexing / len() compatible with tuples.
  • Every production caller of the retyped APIs passes a to_cpu_int_tuple(...) — no stragglers handing in raw CUDA tensors.

Optional (non-blocking) suggestion: add a parametrized test that runs _pack_sequences_for_megatron and asserts its returned cu_seqlens/cu_seqlens_padded equal _build_packed_sequence_metadata(...) for the same inputs (esp. CP>1 with pad_packed_seq_to_multiple_of>1), so the reimplemented padding math can't silently diverge from the packer.

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/optimize-logprob-prepare-input branch from 240914c to 057f7f4 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 057f7f4

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 94fb900

Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
@CarlosGomes98
CarlosGomes98 force-pushed the cgomes/optimize-logprob-prepare-input branch from 94fb900 to ce480cf Compare September 2, 2026 11:42
@CarlosGomes98

Copy link
Copy Markdown
Contributor Author

/ok to test ce480cf

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