perf(sequence-packing): use CPU sequence boundaries - #3932
Open
CarlosGomes98 wants to merge 5 commits into
Open
Conversation
4 tasks
Contributor
Author
|
/ok to test 462d7e2 |
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
CarlosGomes98
force-pushed
the
cgomes/sequence-packing-cpu-boundaries
branch
from
September 1, 2026 22:17
462d7e2 to
5534092
Compare
CarlosGomes98
commented
Sep 1, 2026
Contributor
Author
There was a problem hiding this comment.
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_sizeequals the old(seq_start + padded_seq_lengths[seq_idx]) // cp_sizesinceseq_end = padded_cu_seqlens[i+1]andpadded_seq_lengths[i] = padded_cu_seqlens[i+1] - padded_cu_seqlens[i]. Same holds for the fused-pathmin(...)branch. No off-by-one. - Every production caller either converts via
to_cpu_int_tupleat 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_boundariesis correct.
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
force-pushed
the
cgomes/sequence-packing-cpu-boundaries
branch
from
September 1, 2026 22:20
5534092 to
ff3be49
Compare
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>
Contributor
Author
|
/ok to test 6cb9b58 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What does this PR do ?
A more detailed breakdown
sequence_lengths.pyintroduces the helper conversion function and typetrain.pyhandle the conversion before passing to host-side loss and logprob codemodel_utils.py,algorithms/loss/utils.pyandalgorithms/loss/wrapper.pyexplicitly 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]models/megatron/data.pynormalizes inputs once at entry, and then use CPU codeIssues
List issues that this PR closes (syntax):
Usage
# Add a code snippet demonstrating how to use thisBefore your PR is "Ready for review"
Pre checks:
Additional Information