2626from megatron .core .process_groups_config import ProcessGroupCollection
2727from megatron .core .tensor_observation import is_observing_tensor , observe_tensor
2828from megatron .core .tensor_parallel import (
29+ gather_from_sequence_parallel_region ,
2930 gather_from_tensor_model_parallel_region ,
3031 scatter_to_sequence_parallel_region ,
3132)
@@ -1585,6 +1586,8 @@ def _get_embeddings(
15851586 hidden_states (torch.Tensor): hidden states tensor of shape [s, b, h] where s is the
15861587 sequence length, b is the batch size, and h is the hidden size.
15871588 packed_seq_params (PackedSeqParams): Parameters for packed sequence processing.
1589+ padding_mask (torch.Tensor, optional): Padding flags of shape [b, s/tp] with
1590+ sequence parallelism, otherwise [b, s]. True marks padding.
15881591 mtp_input_mask (torch.Tensor, optional): Mask of conditioning tokens backed by
15891592 regular token embeddings. Shape: [b, s].
15901593 """
@@ -1593,6 +1596,14 @@ def _get_embeddings(
15931596 f"mtp_input_mask shape { mtp_input_mask .shape } must match "
15941597 f"input_ids shape { input_ids .shape } "
15951598 )
1599+ if padding_mask is not None and self .config .sequence_parallel :
1600+ # The mask is SP-sharded, while IDs and CP metadata cover the full
1601+ # CP-local sequence. Restore that sequence before either layout's roll.
1602+ padding_mask = gather_from_sequence_parallel_region (
1603+ padding_mask .transpose (0 , 1 ).contiguous (),
1604+ tensor_parallel_output_grad = False ,
1605+ group = self .tp_group ,
1606+ ).transpose (0 , 1 )
15961607 if self .config .attention_cp_layout == "contiguous" :
15971608 input_ids , position_ids , padding_mask , mtp_input_mask = roll_contiguous_fields (
15981609 (input_ids , position_ids , padding_mask , mtp_input_mask ),
@@ -1636,14 +1647,24 @@ def _get_embeddings(
16361647 return_sum = False ,
16371648 )
16381649 if padding_mask is not None :
1639- padding_mask , _ = roll_tensor (
1640- padding_mask ,
1650+ # Roll validity so zero-filled sequence ends remain padding.
1651+ valid_mask , _ = roll_tensor (
1652+ ~ padding_mask ,
16411653 shifts = - 1 ,
16421654 dims = - 1 ,
16431655 cp_group = self .cp_group ,
16441656 packed_seq_params = packed_seq_params ,
16451657 return_sum = False ,
16461658 )
1659+ padding_mask = ~ valid_mask
1660+ if padding_mask is not None and self .config .sequence_parallel :
1661+ padding_mask = (
1662+ scatter_to_sequence_parallel_region (
1663+ padding_mask .transpose (0 , 1 ).contiguous (), group = self .tp_group
1664+ )
1665+ .transpose (0 , 1 )
1666+ .contiguous ()
1667+ )
16471668 # embedding
16481669 decoder_input = embedding (input_ids = input_ids , position_ids = position_ids )
16491670
@@ -2241,6 +2262,7 @@ class MultiTokenPredictionInputs:
22412262 loss_mask : Optional [Tensor ]
22422263 mtp_input_mask : Optional [Tensor ]
22432264 packed_seq_params : Optional [PackedSeqParams ]
2265+ padding_mask : Optional [Tensor ] = None
22442266
22452267
22462268def _get_mtp_block_submodules (
@@ -2404,8 +2426,9 @@ def prepare_cp_layout(
24042426 mtp_input_mask : Optional [Tensor ],
24052427 packed_seq_params : Optional [PackedSeqParams ],
24062428 cp_batch : Optional [ContextParallelBatch ],
2429+ padding_mask : Optional [Tensor ] = None ,
24072430 ) -> MultiTokenPredictionInputs :
2408- """Prepare activations and token-aligned inputs for the MTP block's CP layout ."""
2431+ """Prepare MTP inputs, including the batch-major, optionally SP-sharded padding mask ."""
24092432 source_layout = (
24102433 cp_batch .boundary_layout if cp_batch is not None else self .config .linear_cp_layout
24112434 )
@@ -2449,6 +2472,23 @@ def prepare_cp_layout(
24492472 self .tp_cp_group ,
24502473 cp_batch .thd_plan ,
24512474 )
2475+ if padding_mask is not None :
2476+ # Convert validity so any new THD padding slots (zero-filled by
2477+ # layout conversion) remain excluded from routing.
2478+ padding_mask = (
2479+ ~ convert_cp_layout (
2480+ (~ padding_mask ).transpose (0 , 1 ).contiguous (),
2481+ source_layout ,
2482+ target_layout ,
2483+ self .cp_group ,
2484+ self .sequence_parallel ,
2485+ self .tp_group ,
2486+ self .tp_cp_group ,
2487+ cp_batch .thd_plan ,
2488+ )
2489+ .transpose (0 , 1 )
2490+ .contiguous ()
2491+ )
24522492 packed_seq_params = cp_batch .get_packed_seq_params (target_layout )
24532493 layout_batch = cp_batch .get_batch (target_layout )
24542494 input_ids = layout_batch ["tokens" ]
@@ -2466,6 +2506,7 @@ def prepare_cp_layout(
24662506 loss_mask = loss_mask ,
24672507 mtp_input_mask = mtp_input_mask ,
24682508 packed_seq_params = packed_seq_params ,
2509+ padding_mask = padding_mask ,
24692510 )
24702511
24712512 def _build_layers (self , pg_collection ):
@@ -2638,6 +2679,19 @@ def forward(
26382679 packed_seq_params = packed_seq_params ,
26392680 return_sum = False ,
26402681 )
2682+ if padding_mask is not None :
2683+ # Precomputed embeddings bypass the layer's _get_embeddings,
2684+ # so shift validity here alongside those embeddings.
2685+ valid_mask , _ = roll_tensor_precomputed_embeddings (
2686+ (~ padding_mask ).transpose (0 , 1 ).contiguous (),
2687+ shifts = - 1 ,
2688+ dims = 0 ,
2689+ sp_group = self .tp_group if self .sequence_parallel else None ,
2690+ cp_group = self .cp_group ,
2691+ packed_seq_params = packed_seq_params ,
2692+ return_sum = False ,
2693+ )
2694+ padding_mask = ~ valid_mask .transpose (0 , 1 ).contiguous ()
26412695
26422696 # Older HSM entries predict earlier targets than the newest entry. Roll
26432697 # them once per depth so all candidates correspond to the same target.
0 commit comments