perf(sequence-packing): keep packed-sequence boundaries on CPU - #3933
Open
CarlosGomes98 wants to merge 7 commits into
Open
perf(sequence-packing): keep packed-sequence boundaries on CPU#3933CarlosGomes98 wants to merge 7 commits into
CarlosGomes98 wants to merge 7 commits into
Conversation
Contributor
Author
|
/ok to test 580c579 |
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
CarlosGomes98
force-pushed
the
cgomes/optimize-logprob-prepare-input
branch
from
September 1, 2026 22:17
580c579 to
240914c
Compare
CarlosGomes98
commented
Sep 1, 2026
Contributor
Author
There was a problem hiding this comment.
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_metadatareproduces_pack_sequences_for_megatron'scu_seqlens/cu_seqlens_paddedfor all three configs (non-model-slices, model-slices, VLM), including theneeds_padding=False,pad_packed_seq_to_multiple_of, andpad_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.
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/optimize-logprob-prepare-input
branch
from
September 1, 2026 22:20
240914c to
057f7f4
Compare
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>
Contributor
Author
|
/ok to test 94fb900 |
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
Signed-off-by: Carlos Gomes <cgomes@nvidia.com>
CarlosGomes98
force-pushed
the
cgomes/optimize-logprob-prepare-input
branch
from
September 2, 2026 11:42
94fb900 to
ce480cf
Compare
Contributor
Author
|
/ok to test ce480cf |
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 ?
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
# Add a code snippet demonstrating how to use thisBefore your PR is "Ready for review"
Pre checks:
Additional Information