feat: MTP CP-aware roll, segment-aware roll, and synthetic packing support#4600
Open
chiaotung97 wants to merge 1 commit into
Open
feat: MTP CP-aware roll, segment-aware roll, and synthetic packing support#4600chiaotung97 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 jax.lax.ppermute backward ring to handle cross-rank token transfer under context parallelism, and segment-aware roll (roll_and_mask_by_segment) to prevent cross-document target leakage under packing. Generate packed segment IDs in synthetic data. - layers/multi_token_prediction.py: +107/−4 - input_pipeline/synthetic_data_processing.py: +43/−2 - utils/train_utils.py: −5 - tests/unit/multi_token_prediction_test.py: +296 - docs/design/ag_cp_mtp_packing_fix.md: +141 Co-Authored-By: Claude <noreply@anthropic.com>
chiaotung97
requested review from
A9isha,
RissyRan,
SurbhiJainUSC,
bvandermoon,
gagika,
gobbleturk,
jacoguzo,
richjames0 and
shralex
as code owners
July 24, 2026 10:52
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
chiaotung97
requested review from
NuojCheng,
abhinavclemson,
aireenmei,
darisoy,
dipannita08,
hengtaoguo,
huytransformer,
igorts-git,
jiangjy1982,
khatwanimohit,
parambole,
suexu1025,
vipannalla and
xibinliu
as code owners
July 24, 2026 10:52
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
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