fix: MTP CP-aware roll, segment-aware roll, and synthetic packing support - #4623
Open
chiaotung97 wants to merge 1 commit into
Open
fix: MTP CP-aware roll, segment-aware roll, and synthetic packing support#4623chiaotung97 wants to merge 1 commit into
chiaotung97 wants to merge 1 commit into
Conversation
- Add CP-aware left shift (_shift_left_one_cp_aware) using ppermute - Add segment-aware roll (roll_and_mask_by_segment) to prevent cross-document target leakage in MTP - Generate packed segment IDs for synthetic data when packing=True - Remove guard against synthetic+packing+CP in train_utils.py - Add 17 unit tests covering segment roll, CP shift, and CP+segment
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
chiaotung97
requested review from
A9isha,
NuojCheng,
RissyRan,
SurbhiJainUSC,
abhinavclemson,
aireenmei,
bvandermoon,
darisoy,
dipannita08,
gagika,
gobbleturk,
hengtaoguo,
huytransformer,
igorts-git,
jacoguzo,
jiangjy1982,
khatwanimohit,
parambole,
richjames0,
shralex,
suexu1025,
vipannalla and
xibinliu
as code owners
July 27, 2026 09:51
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
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.
Summary
Fixes 3 issues when combining Multi-Token Prediction (MTP) with
All-Gather Context Parallelism (AG-CP) and Packing.
Checklist
_shift_left_one_cp_aware) with ppermute backward ringroll_and_mask_by_segment) to prevent cross-document leakage_make_packed_segment_ids)Problem
jnp.rolldoes not cross CP rank boundariesjnp.rollonly operates within the local shardroll_and_maskis segment-unawaresegment_idsis always all-ones even thoughbase.ymldefaults topacking: truetrain_utils.pyalso blocks the combination outrightSolution
1. CP-Aware Left Shift (
_shift_left_one_cp_aware)Uses
jax.lax.ppermutein a backward ring: rank r sends its first token torank r−1, receives rank r+1's first token, and places it in its last
position. Degrades to
jnp.rollwhen no CP axis is in scope — zero overhead.2. Segment-Aware Roll (
roll_and_mask_by_segment)Shifts both
xandsegment_idsvia_shift_left_one_cp_aware, then maskspositions where
seg_current != seg_next(document boundary) orseg_current == 0(padding). Falls back toroll_and_maskwhensegment_ids=None. All rolling variables inMultiTokenPredictionBlock.__call__now use this function.3. Synthetic Data with Packed Segment IDs
_make_packed_segment_idssplits each row into 2..N randomly-sized segmentswith sequential integer IDs starting from 1. Removed the
train_utils.pyguard that rejected
synthetic + packing + CP.Files Changed
layers/multi_token_prediction.py_shift_left_one_cp_aware,roll_and_mask_by_segment, wiringinput_pipeline/synthetic_data_processing.py_make_packed_segment_idsutils/train_utils.pytests/unit/multi_token_prediction_test.pydocs/design/ag_cp_mtp_packing_fix.mdUnit Tests
30 tests, all passing (TPU v6e-4, 4 devices):
TestRollAndMaskTestRollAndMaskBySegmentTestMakePackedSegmentIdsTestShiftLeftOneCpAwareTestRollAndMaskBySegmentWithCpBackward Compatibility
_shift_left_one_cp_aware: degrades tojnp.rollwhen CP=1 or no"context"axisroll_and_mask_by_segment: degrades toroll_and_maskwhensegment_ids=Noneroll_and_mask(shift=-1): equivalent to original path when CP is offsynthetic_data_processing: segment IDs remainjnp.oneswhenpacking=FalseVerification