Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion atom/distributed/pp_comm.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

# Keys carried between pipeline stages. `sparse_kv_indices` is optional: present
# only when a PP boundary splits a DSA IndexShare group (see model_runner).
# `block_residual` is Kimi-K3's AttnRes block window [T, k, H]. `PP_AUX_KEY`
# `block_residual` is Kimi-K3's AttnRes block bank [T, blocks, H]. `PP_AUX_KEY`
# relays a drafter's target aux hidden states (`PPAuxRelay`, set/popped by
# model_runner). Keys missing here are silently dropped by the send.
PP_AUX_KEY = "pp_aux"
Expand Down
20 changes: 17 additions & 3 deletions atom/model_ops/attention_residual.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ def __init__(
self.eps = 1e-6 if norm is None else _rms_eps(norm)
self.out_eps = 1e-6 if out_norm is None else _rms_eps(out_norm)
self.score_weight: torch.Tensor | None = None
# Set only on the site that closes out blocks (see maybe_close_block).
# Set on a site handed the whole bank: it mixes the slots closed before
# layer `layer_idx`, which `maybe_close_block` writes.
self.block_size = block_size
self.layer_idx = layer_idx

Expand Down Expand Up @@ -170,6 +171,7 @@ def forward(
if prefix_sum is None:
prefix_sum, add_hidden, add_hidden2 = add_hidden, add_hidden2, None
assert prefix_sum is not None
block_residual = self.window(block_residual)

if self.enabled and block_residual is not None and block_residual.shape[1] > 0:
score_weight = self.score_weight
Expand Down Expand Up @@ -199,12 +201,19 @@ def forward(
mixed = prefix_sum if self.out_norm is None else self.out_norm(prefix_sum)
return mixed, prefix_sum

def window(self, block_residual: torch.Tensor | None) -> torch.Tensor | None:
"""The bank's slots this site mixes. Sliced here, not by the caller: in
a compiled forward a view must not cross a graph split."""
if self.block_size is None or block_residual is None:
return block_residual
return block_residual[:, : -(-self.layer_idx // self.block_size)]

def maybe_close_block(
self,
prefix_sum: torch.Tensor,
block_residual: torch.Tensor | None,
) -> tuple[torch.Tensor | None, torch.Tensor | None]:
"""Append ``prefix_sum`` as a candidate every ``block_size`` layers.
"""Copy ``prefix_sum`` into its slot of the bank every ``block_size`` layers.

Returns the new ``(block_residual, prefix_sum)``. A closed-out block
leaves prefix_sum None: the running sum has been banked as a candidate
Expand All @@ -216,5 +225,10 @@ def maybe_close_block(
if self.layer_idx % self.block_size != 0:
return block_residual, prefix_sum
assert block_residual is not None
block_residual = torch.cat([block_residual, prefix_sum.unsqueeze(1)], dim=1)
# as_strided, not select: Inductor reinplaces it even into a bank allocated
# in the same graph, where a select's scatter rewrites every slot. The bank
# starts its storage (storage_offset() would break the Dynamo graph).
row, slot, col = block_residual.stride()
start = self.layer_idx // self.block_size * slot
block_residual.as_strided(prefix_sum.shape, (row, col), start).copy_(prefix_sum)
return block_residual, None
50 changes: 50 additions & 0 deletions atom/model_ops/module_dispatch_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
Currently registered:
- torch.ops.aiter.maybe_dual_stream_forward — V2/V3.2/V4 MoE (summed)
- torch.ops.aiter.maybe_dual_stream_split_forward — K3 MoE (routed/shared unsummed)
- torch.ops.aiter.kimi_k3_post_attention_forward — K3 MoE layer after its attention
- torch.ops.aiter.indexer_score_topk — V4 sparse indexer
"""

Expand Down Expand Up @@ -149,6 +150,55 @@ def _maybe_dual_stream_split_forward_fake(
)


# ---------------------------------------------------------------------------
# K3 MoE layer after its attention: o_proj's reduce, mlp_attn_res and the MoE
# ---------------------------------------------------------------------------
#
# Caller contract (the decoder layer looked up by `layer_name`):
# - `post_attention_forward(attn_partial, prefix_sum, block_residual)
# -> (prefix_sum, routed, shared)`, three `[T, H]` tensors
#
# One op, so that whether a step reduces o_proj's partial sum by all-reduce or
# runs the attention residual on this rank's rows (reduce-scatter, then an
# all-gather of the MoE input) is decided at run time by T: no traced tensor
# changes shape between the two. On the row-sharded path the residual comes
# back whole as `prefix_sum`, and `routed` / `shared` are real zero tensors.


def kimi_k3_post_attention_forward(
attn_partial: torch.Tensor,
prefix_sum: torch.Tensor | None,
block_residual: torch.Tensor | None,
layer_name: str,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
self = get_current_atom_config().compilation_config.static_forward_context[
layer_name
]
return self.post_attention_forward(attn_partial, prefix_sum, block_residual)


def _kimi_k3_post_attention_forward_fake(
attn_partial: torch.Tensor,
prefix_sum: torch.Tensor | None,
block_residual: torch.Tensor | None,
layer_name: str,
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
return (
torch.empty_like(attn_partial),
torch.empty_like(attn_partial),
torch.empty_like(attn_partial),
)


direct_register_custom_op(
op_name="kimi_k3_post_attention_forward",
op_func=kimi_k3_post_attention_forward,
mutates_args=(),
fake_impl=_kimi_k3_post_attention_forward_fake,
tags=(torch.Tag.needs_fixed_stride_order,),
)


# ---------------------------------------------------------------------------
# Sparse indexer score + top-k (V2/V3.2/V4)
# ---------------------------------------------------------------------------
Expand Down
Loading
Loading