core.ssm.context_parallel.chunkwise#
Common chunkwise context-parallel interfaces for linear attention.
Module Contents#
Classes#
One shard’s unpacked |
|
One shard’s packed |
|
One shard’s unpacked |
|
One shard’s packed |
|
Backend state saved from forward for backward. |
|
Rank-local metadata for chunkwise CP over packed sequences. |
|
Local kernel interface for one contiguous chunkwise-CP shard. |
|
Results retained by a variant-specific autograd adapter after CP forward. |
Functions#
Gather local summaries and apply the selected causal prefix. |
|
Gather local adjoint summaries and apply the selected reverse-causal suffix. |
|
Build rank-local packed-sequence metadata for chunkwise CP. |
|
All-gather an unpacked state summary without packing its tensors. |
|
Validate a tensor communicated as part of a CP summary. |
Data#
API#
- class core.ssm.context_parallel.chunkwise.CPForwardUnpackedSummary#
One shard’s unpacked
S_out = M S_in + delta_Ssummary.- transition: torch.Tensor#
None
- state_update: torch.Tensor#
None
- class core.ssm.context_parallel.chunkwise.CPForwardPackedSummary#
One shard’s packed
S_out = M S_in + delta_Ssummary.- packed: torch.Tensor#
None
- core.ssm.context_parallel.chunkwise.CPForwardSummary#
None
- class core.ssm.context_parallel.chunkwise.CPBackwardUnpackedSummary#
One shard’s unpacked
dS_in = M^T dS_out + gammasummary.- transition: torch.Tensor#
None
- local_state_grad: torch.Tensor#
None
- class core.ssm.context_parallel.chunkwise.CPBackwardPackedSummary#
One shard’s packed
dS_in = M^T dS_out + gammasummary.- packed: torch.Tensor#
None
- core.ssm.context_parallel.chunkwise.CPBackwardSummary#
None
- class core.ssm.context_parallel.chunkwise.CPSavedContext#
Backend state saved from forward for backward.
metadatamust not contain tensors; tensor state belongs intensorsso that the variant’s autograd adapter can save it withctx.save_for_backward.- tensors: tuple[torch.Tensor, ...]#
None
- metadata: object | None#
None
- class core.ssm.context_parallel.chunkwise.PackedSequenceCPMetadata#
Rank-local metadata for chunkwise CP over packed sequences.
- Parameters:
local_seq_idx – Per-token sequence IDs for the local contiguous shard.
local_cu_seqlens – Sequence boundaries within the local contiguous shard.
preceding_rank_start – Inclusive start of the forward-summary prefix.
following_rank_stop – Exclusive end of the backward-summary suffix.
- local_seq_idx: torch.Tensor#
None
- local_cu_seqlens: torch.Tensor#
None
- preceding_rank_start: int#
None
- following_rank_stop: int#
None
- core.ssm.context_parallel.chunkwise.CPInputT#
‘TypeVar(…)’
- core.ssm.context_parallel.chunkwise.LocalContextT#
‘TypeVar(…)’
- core.ssm.context_parallel.chunkwise.BackwardContextT#
‘TypeVar(…)’
- core.ssm.context_parallel.chunkwise.CPGradientsT#
‘TypeVar(…)’
- class core.ssm.context_parallel.chunkwise.LinearAttentionCPBackend#
Bases:
typing.Protocol[core.ssm.context_parallel.chunkwise.CPInputT,core.ssm.context_parallel.chunkwise.LocalContextT,core.ssm.context_parallel.chunkwise.BackwardContextT,core.ssm.context_parallel.chunkwise.CPGradientsT]Local kernel interface for one contiguous chunkwise-CP shard.
Megatron owns all process-group operations and passes only selected summary slices to these methods.
- cp_forward_prepare( ) tuple[core.ssm.context_parallel.chunkwise.CPForwardSummary, core.ssm.context_parallel.chunkwise.LocalContextT]#
Compute the local state summary with zero incoming state.
- cp_forward_apply(
- local_context: core.ssm.context_parallel.chunkwise.LocalContextT,
- preceding_summaries: core.ssm.context_parallel.chunkwise.CPForwardSummary,
Compose the preceding summaries and compute the local output.
- cp_backward_prepare(
- output_grad: torch.Tensor,
- saved_context: core.ssm.context_parallel.chunkwise.CPSavedContext,
Compute local
gammawith zero outgoing-state gradient.
- cp_backward_apply(
- backward_context: core.ssm.context_parallel.chunkwise.BackwardContextT,
- following_summaries: core.ssm.context_parallel.chunkwise.CPBackwardSummary,
Compose the reverse-causal suffix and finish the local input gradients.
- class core.ssm.context_parallel.chunkwise.CPForwardResult#
Results retained by a variant-specific autograd adapter after CP forward.
- output: torch.Tensor#
None
- saved_context: core.ssm.context_parallel.chunkwise.CPSavedContext#
None
- core.ssm.context_parallel.chunkwise.chunkwise_cp_forward(
- backend: core.ssm.context_parallel.chunkwise.LinearAttentionCPBackend[core.ssm.context_parallel.chunkwise.CPInputT, core.ssm.context_parallel.chunkwise.LocalContextT, core.ssm.context_parallel.chunkwise.BackwardContextT, core.ssm.context_parallel.chunkwise.CPGradientsT],
- inputs: core.ssm.context_parallel.chunkwise.CPInputT,
- cp_group: torch.distributed.ProcessGroup,
- preceding_slice: slice,
Gather local summaries and apply the selected causal prefix.
- core.ssm.context_parallel.chunkwise.chunkwise_cp_backward(
- backend: core.ssm.context_parallel.chunkwise.LinearAttentionCPBackend[core.ssm.context_parallel.chunkwise.CPInputT, core.ssm.context_parallel.chunkwise.LocalContextT, core.ssm.context_parallel.chunkwise.BackwardContextT, core.ssm.context_parallel.chunkwise.CPGradientsT],
- output_grad: torch.Tensor,
- saved_context: core.ssm.context_parallel.chunkwise.CPSavedContext,
- cp_group: torch.distributed.ProcessGroup,
- following_slice: slice,
Gather local adjoint summaries and apply the selected reverse-causal suffix.
- core.ssm.context_parallel.chunkwise._all_gather_forward_summary(
- local_summary: core.ssm.context_parallel.chunkwise.CPForwardSummary,
- cp_group: torch.distributed.ProcessGroup,
- core.ssm.context_parallel.chunkwise._all_gather_backward_summary(
- local_summary: core.ssm.context_parallel.chunkwise.CPBackwardSummary,
- cp_group: torch.distributed.ProcessGroup,
- core.ssm.context_parallel.chunkwise._slice_forward_summary(
- summary: core.ssm.context_parallel.chunkwise.CPForwardSummary,
- rank_slice: slice,
- core.ssm.context_parallel.chunkwise._slice_backward_summary(
- summary: core.ssm.context_parallel.chunkwise.CPBackwardSummary,
- rank_slice: slice,
- core.ssm.context_parallel.chunkwise.build_packed_sequence_cp_metadata(
- global_seq_idx: torch.Tensor,
- cp_rank: int,
- cp_size: int,
Build rank-local packed-sequence metadata for chunkwise CP.
- Parameters:
global_seq_idx – Nondecreasing global per-token sequence IDs in
[1, T]layout.cp_rank – This rank’s position in causal order.
cp_size – Number of contiguous CP shards.
- Returns:
The local sequence IDs, local sequence boundaries, and summary slice bounds for this rank.
- core.ssm.context_parallel.chunkwise._all_gather_unpacked_summary(
- transition: torch.Tensor,
- state_update: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
All-gather an unpacked state summary without packing its tensors.
- core.ssm.context_parallel.chunkwise._all_gather_tensor(
- local_tensor: torch.Tensor,
- cp_group: torch.distributed.ProcessGroup,
- core.ssm.context_parallel.chunkwise._validate_summary_tensor(tensor: torch.Tensor) None#
Validate a tensor communicated as part of a CP summary.