core.transformer.residual_recompute#

Shared replay contexts, block planning, and finalization.

Connections choose which operations can be replayed and which state stays live. Callers register operations in forward order and finalize each block at its live boundary. Only immutable boundary plans may be cached; managers belong to one forward invocation.

Module Contents#

Classes#

ResidualStreamRecomputeContext

One layer’s immutable view of a shared residual-stream replay block.

Functions#

build_recompute_block_end_plan

Mark block ends without splitting disjoint adjacent atomic layer pairs.

build_recompute_layer_managers

Allocate fresh managers for an active plan, sharing one within each block.

finalize_recompute_block

Discard registered outputs and attach replay at the block’s live boundary.

build_residual_stream_recompute_plan

Partition local physical layers into independent ordered replay blocks.

Data#

_R

API#

core.transformer.residual_recompute._R#

‘TypeVar(…)’

core.transformer.residual_recompute.build_recompute_block_end_plan(
num_layers: int,
block_size: int | None,
*,
atomic_layer_pairs: collections.abc.Sequence[tuple[int, int]] = (),
) → tuple[bool, ...]#

Mark block ends without splitting disjoint adjacent atomic layer pairs.

Indices use the caller’s local layer order. A pair may exceed a block size of one; otherwise blocks contain at most block_size layers. None uses the entire local stack. Empty stacks return an empty plan.

core.transformer.residual_recompute.build_recompute_layer_managers(
block_ends: collections.abc.Sequence[bool],
) → list[megatron.core.tensor_parallel.random.CheckpointWithoutOutputManager]#

Allocate fresh managers for an active plan, sharing one within each block.

core.transformer.residual_recompute.finalize_recompute_block(
manager: megatron.core.tensor_parallel.random.CheckpointWithoutOutputManager | None,
hidden_states: torch.Tensor,
is_block_end: bool,
) → None#

Discard registered outputs and attach replay at the block’s live boundary.

class core.transformer.residual_recompute.ResidualStreamRecomputeContext#

One layer’s immutable view of a shared residual-stream replay block.

manager: megatron.core.tensor_parallel.random.CheckpointWithoutOutputManager#

None

is_block_end: bool#

None

checkpoint(
function: collections.abc.Callable[..., core.transformer.residual_recompute._R],
*args: Any,
fp8: bool = False,
) → core.transformer.residual_recompute._R#

Run one cheap operation and register it for ordered replay.

finalize(hidden_states: torch.Tensor) → None#

Discard this block’s registered outputs once its live boundary exists.

core.transformer.residual_recompute.build_residual_stream_recompute_plan(
num_layers: int,
block_size: int | None,
*,
atomic_layer_pairs: collections.abc.Sequence[tuple[int, int]] = (),
) → list[core.transformer.residual_recompute.ResidualStreamRecomputeContext]#

Partition local physical layers into independent ordered replay blocks.

Shortcut MoE’s predecessor (e.g. attention or Mamba) and paired MoE layer share replay-owned intermediates, so a replay block must not end between them.

Parameters:
  • num_layers – Local physical layer count, before shortcut wrapper grouping.

  • block_size – Target maximum physical layers per block; None uses the entire local stack. A pair stays intact even when block_size=1, forming a two-layer block.

  • atomic_layer_pairs – Disjoint adjacent pairs of zero-based local physical layer indices that must share one replay manager.

Returns:

One context per physical layer, sharing a manager within each block. Only the block’s final layer has is_block_end=True.

For example, four layers with block_size=3 and pairs (0, 1), (2, 3) form blocks [0, 1] and [2, 3], not [0, 1, 2] and [3].