bridge.data.megatron_mimo.sequence_pack#
Sequence-packing assembly for MegatronMIMO language shards.
Concatenates the real (unpadded) tokens of a group of examples into a single
packed sequence — the Transformer Engine thd layout (total-tokens × heads × head-dim) —
with cu_seqlens boundaries. Downstream, the cu_seqlens make attention block-diagonal
per example (the qwen3-vl attention / flash-attn path already honors this), so packing does
not leak attention across examples.
This module packs the whole shard as one group — there is no per-pack length budget here, so the assembled pack is the entire shard.
Module Contents#
Functions#
Concatenate one group’s real tokens into a single packed sequence (THD layout). |
|
Pack a per-DP-shard language batch |
API#
- bridge.data.megatron_mimo.sequence_pack.assemble_packed_sequence(
- group: List[int],
- tokens: Optional[torch.Tensor],
- lengths: List[int],
- *,
- labels: Optional[torch.Tensor] = None,
- loss_mask: Optional[torch.Tensor] = None,
- position_ids: Optional[torch.Tensor] = None,
Concatenate one group’s real tokens into a single packed sequence (THD layout).
tokensmay beNoneon a non-first PP stage that has noinput_idsbut still must packlabels/loss_maskto the same[1, T]shape (F5, PP-consistent packing); in that caseinput_idsisNonein the result and the device is taken from the first available tensor.- Parameters:
group – Row indices (into the source tensors) of the examples in this pack, in pack order.
tokens – Padded
[B, S]token ids, orNone(last-stage label-only pack).lengths – Real (unpadded) length per row.
labels – Optional padded
[B, S]labels (pad value-100).loss_mask – Optional padded
[B, S]loss mask (pad value0).position_ids – Optional padded position ids. Both 2-D
[B, S]and Qwen-VL MRoPE 3-D[3, B, S]are handled; each sample’s real slice is concatenated, so the packed positions are per-sample-reset (each sample’s positions already start at 0).
- Returns:
Dict with
input_ids/labels/loss_maskas packed[1, T]tensors (T = sum of the group’s real lengths;input_idsisNonewhentokensisNone),position_idsas[1, T](2-D input) or[3, 1, T](MRoPE input),cu_seqlens(int32, lengthlen(group)+1), andmax_seqlen(int).- Raises:
ValueError – If
position_idsis neither 2-D[B, S]nor 3-D MRoPE[3, B, S]; if none oftokens/labels/loss_mask/position_idsis a tensor (no device or shape to pack to); or if the group’s segment lengths sum to zero (an empty packed shard cannot be built).
- bridge.data.megatron_mimo.sequence_pack.pack_language_shard(
- data_batch: Dict[str, Any],
- *,
- lengths: torch.Tensor | None = None,
Pack a per-DP-shard language batch
[bs, S]into a single packed sequence[1, T].Concatenates the shard’s real (unpadded) tokens — including image-placeholder tokens, so the downstream MIMO modality splice still fills them in order — into one packed sequence with
cu_seqlensblock-diagonal boundaries. This removes the per-sample padding compute (the LM no longer processesS-padded sequences) and makes attention cost ∝ real tokens.The returned
packing_kwargsfeedsMimoModel.forward(packing_kwargs=...), which builds a THDPackedSeqParams(qkv_format='thd') and threadscu_seqlensto the GPT decoder for block-diagonal attention. Non-LM tensors (modality_inputs, etc.) are carried through unchanged.The per-sample real length comes only from the caller-supplied
lengths(forward_stepderives it from the batch’sattention_maskon every language stage, so all stages pack to an identical[1, T]).Nonereturns the batch unchanged.loss_maskis not used for length (it is a supervision mask, not a padding mask).- Parameters:
data_batch – This rank’s sliced language batch with
input_ids[bs, S]and/orlabels/loss_mask[bs, S], plus optionalposition_ids([bs, S]or MRoPE[3, bs, S]).lengths – Per-sample real token lengths
[bs]from the caller. WhenNonethe batch is returned unchanged (no supported length source).
- Returns:
(packed_batch, packing_kwargs).packing_kwargsisNone(and the batch is returned unchanged) whenlengthsisNone(no length source).- Raises:
ValueError – If
lengthscannot be derived to a consistent[bs]shape (a mis-shaped pack would otherwise be silent).