core.context_parallel.layout#
Context-parallel sequence-layout conversion.
Module Contents#
Classes#
Rank-local all-to-all plan expressed in sequence-segment counts. |
|
Rank-local all-to-all-v plan for packed sequence layout conversion. |
|
Process-group coordinates used by a layout redistribution. |
|
Manage CP layout transitions across a sequence of layers. |
|
Per-forward state for CP layout conversion. |
Functions#
Return two segments for CP-only conversion and one for even-TP SP conversion. |
|
Return the atomic sequence segments owned by one TP x CP rank. |
|
Map logical |
|
Resolve the rank coordinates and process group for a layout redistribution. |
|
Build the all-to-all-v plan for one rank of a CP layout conversion. |
|
Build a packed layout plan from source indices grouped by target logical rank. |
|
Build padded dual-chunk attention order without Transformer Engine helpers. |
|
Build a reusable packed sequence layout plan for attention. |
|
Redistribute local sequence segments with a differentiable all-to-all-v. |
|
Apply a packed sequence layout plan with a differentiable all-to-all-v. |
|
Convert contiguous CP sequence shards to Megatron’s zigzag attention layout. |
|
Convert Megatron’s zigzag attention shards back to contiguous CP shards. |
Data#
API#
- core.context_parallel.layout.CPLayout#
None
- class core.context_parallel.layout._LayoutRedistributionPlan#
Rank-local all-to-all plan expressed in sequence-segment counts.
- send_slots: tuple[int, ...]#
None
- input_segment_counts: tuple[int, ...]#
None
- output_segment_counts: tuple[int, ...]#
None
- receive_permutation: tuple[int, ...]#
None
- class core.context_parallel.layout.THDCPLayoutPlan#
Rank-local all-to-all-v plan for packed sequence layout conversion.
Forward fields describe contiguous-to-zigzag conversion. Reverse fields describe the inverse conversion. The plan is built once per packed batch and reused around every attention layer.
- contiguous_local_token_count: int#
None
- zigzag_local_token_count: int#
None
- cu_seqlens_padded: torch.Tensor#
None
- max_seqlen_padded: int#
None
- pad_between_seqs: bool#
None
- forward_send_indices: torch.Tensor#
None
- forward_receive_positions: torch.Tensor#
None
- forward_input_split_sizes: tuple[int, ...]#
None
- forward_output_split_sizes: tuple[int, ...]#
None
- reverse_send_indices: torch.Tensor#
None
- reverse_receive_indices: torch.Tensor#
None
- class core.context_parallel.layout._LayoutParallelContext#
Process-group coordinates used by a layout redistribution.
- cp_size: int#
None
- cp_rank: int#
None
- tp_size: int#
None
- tp_rank: int#
None
- communication_group: torch.distributed.ProcessGroup#
None
- group_rank_by_logical_rank: tuple[int, ...]#
None
- property group_size: int#
Return the number of ranks participating in the redistribution.
- core.context_parallel.layout._segments_per_rank(tp_size: int) int#
Return two segments for CP-only conversion and one for even-TP SP conversion.
- core.context_parallel.layout._local_segment_ids(
- layout: core.context_parallel.layout.CPLayout,
- cp_size: int,
- cp_rank: int,
- tp_size: int = 1,
- tp_rank: int = 0,
Return the atomic sequence segments owned by one TP x CP rank.
- core.context_parallel.layout._segment_owner(
- segment_id: int,
- layout: core.context_parallel.layout.CPLayout,
- cp_size: int,
- tp_size: int,
- core.context_parallel.layout._build_group_rank_by_logical_rank(
- cp_global_ranks: tuple[int, ...],
- tp_global_ranks: tuple[int, ...],
- tp_cp_global_ranks: tuple[int, ...],
- current_global_rank: int,
Map logical
cp_rank * tp_size + tp_rankcoordinates to group ranks.
- core.context_parallel.layout._get_group_rank_by_logical_rank(
- cp_group: torch.distributed.ProcessGroup,
- tp_group: torch.distributed.ProcessGroup,
- tp_cp_group: torch.distributed.ProcessGroup,
- core.context_parallel.layout._get_layout_parallel_context(
- cp_group: torch.distributed.ProcessGroup,
- sequence_parallel: bool,
- tp_group: torch.distributed.ProcessGroup | None,
- tp_cp_group: torch.distributed.ProcessGroup | None,
Resolve the rank coordinates and process group for a layout redistribution.
- core.context_parallel.layout._build_layout_redistribution_plan(
- source_layout: core.context_parallel.layout.CPLayout,
- target_layout: core.context_parallel.layout.CPLayout,
- cp_size: int,
- cp_rank: int,
- tp_size: int = 1,
- tp_rank: int = 0,
- group_rank_by_logical_rank: tuple[int, ...] | None = None,
Build the all-to-all-v plan for one rank of a CP layout conversion.
- core.context_parallel.layout._build_thd_cp_layout_plan_from_rank_order_indices(
- rank_order_indices: torch.Tensor,
- source_token_count: int,
- cu_seqlens_padded: torch.Tensor,
- cp_size: int,
- cp_rank: int,
- tp_size: int = 1,
- tp_rank: int = 0,
- group_rank_by_logical_rank: tuple[int, ...] | None = None,
- pad_between_seqs: bool | torch.Tensor = False,
Build a packed layout plan from source indices grouped by target logical rank.
rank_order_indicescontains one entry for every padded attention position. Non-negative entries identify positions in the contiguous input;-1entries are temporary padding.
- core.context_parallel.layout._build_thd_rank_order_indices(
- cu_seqlens: torch.Tensor,
- cu_seqlens_padded: torch.Tensor | None,
- cp_size: int,
- tp_size: int,
- expected_source_token_count: int | None = None,
Build padded dual-chunk attention order without Transformer Engine helpers.
- core.context_parallel.layout.build_thd_cp_layout_plan(
- cu_seqlens: torch.Tensor,
- total_tokens: int,
- cp_group: torch.distributed.ProcessGroup,
- cu_seqlens_padded: torch.Tensor | None = None,
- sequence_parallel: bool = False,
- tp_group: torch.distributed.ProcessGroup | None = None,
- tp_cp_group: torch.distributed.ProcessGroup | None = None,
Build a reusable packed sequence layout plan for attention.
- Parameters:
cu_seqlens – Global cumulative actual sequence lengths in
torch.int32.total_tokens – Global token count in the contiguous residual stream.
cp_group – Context-parallel process group.
cu_seqlens_padded – Optional physical offsets already present in the input.
sequence_parallel – Whether the residual stream is also sharded over TP ranks.
tp_group – Tensor-parallel process group, required with sequence parallelism.
tp_cp_group – Combined TP x CP process group, required when TP size is greater than one.
- Returns:
A rank-local plan reusable for both directions of the layout conversion.
- core.context_parallel.layout._redistribute_layout(
- input_: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
- source_layout: core.context_parallel.layout.CPLayout,
- target_layout: core.context_parallel.layout.CPLayout,
- sequence_parallel: bool,
- tp_group: torch.distributed.ProcessGroup | None,
- tp_cp_group: torch.distributed.ProcessGroup | None,
Redistribute local sequence segments with a differentiable all-to-all-v.
- core.context_parallel.layout._redistribute_thd_layout(
- input_: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
- plan: core.context_parallel.layout.THDCPLayoutPlan,
- source_layout: core.context_parallel.layout.CPLayout,
- target_layout: core.context_parallel.layout.CPLayout,
- sequence_parallel: bool,
- tp_group: torch.distributed.ProcessGroup | None,
- tp_cp_group: torch.distributed.ProcessGroup | None,
Apply a packed sequence layout plan with a differentiable all-to-all-v.
- core.context_parallel.layout.contiguous_to_zigzag(
- input_: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
- sequence_parallel: bool = False,
- tp_group: torch.distributed.ProcessGroup | None = None,
- tp_cp_group: torch.distributed.ProcessGroup | None = None,
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None = None,
Convert contiguous CP sequence shards to Megatron’s zigzag attention layout.
- core.context_parallel.layout.zigzag_to_contiguous(
- input_: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
- sequence_parallel: bool = False,
- tp_group: torch.distributed.ProcessGroup | None = None,
- tp_cp_group: torch.distributed.ProcessGroup | None = None,
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None = None,
Convert Megatron’s zigzag attention shards back to contiguous CP shards.
- class core.context_parallel.layout.ContextParallelLayoutManager#
Manage CP layout transitions across a sequence of layers.
- layer_layouts: tuple[core.context_parallel.layout.CPLayout, ...]#
None
- boundary_layout: core.context_parallel.layout.CPLayout#
None
- sequence_parallel: bool#
None
- cp_group: torch.distributed.ProcessGroup#
None
- tp_group: torch.distributed.ProcessGroup | None#
None
- tp_cp_group: torch.distributed.ProcessGroup | None#
None
- requires_conversion: bool#
‘field(…)’
- __post_init__() None#
Determine whether the layer sequence needs layout conversion.
- _convert_cp_layout(
- hidden_states: torch.Tensor,
- source_layout: core.context_parallel.layout.CPLayout,
- target_layout: core.context_parallel.layout.CPLayout,
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None = None,
- prepare_layer_input(
- layer_index: int,
- hidden_states: torch.Tensor,
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None = None,
Convert when a layer requires a different layout than its predecessor.
- finalize_layer_output(
- layer_index: int,
- hidden_states: torch.Tensor,
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None = None,
Restore the boundary layout after the final layer.
- build_packed_zigzag_layout(
- packed_seq_params: megatron.core.packed_seq_params.PackedSeqParams,
Build one THD conversion plan and its zigzag-layout metadata.
- build_forward_state(
- packed_seq_params: megatron.core.packed_seq_params.PackedSeqParams | None,
Build the layout state for one forward pass.
- class core.context_parallel.layout.ContextParallelLayoutState#
Per-forward state for CP layout conversion.
- thd_plan: core.context_parallel.layout.THDCPLayoutPlan | None#
None
- contiguous_packed_seq_params: megatron.core.packed_seq_params.PackedSeqParams | None#
None
- zigzag_packed_seq_params: megatron.core.packed_seq_params.PackedSeqParams | None#
None
- prepare_layer(
- layer_index: int,
- hidden_states: torch.Tensor,
Prepare a layer’s input and matching packed metadata.
- finalize_layer(
- layer_index: int,
- hidden_states: torch.Tensor,
Finalize a layer’s output layout.