nemo_automodel.components.models.kimi_k3.situ
nemo_automodel.components.models.kimi_k3.situ
Kimi-K3 SiTU activation and attention-residual compute cores.
Memory- and dispatch-optimized fp32 chains extracted from model.py:
the chunked bf16-saving weighted-SiTU autograd Function, the attn-res
mixing chain, and the opt-in torch.compile wrapper
(BackendConfig.compile_situ) shared by all of them.
Module Contents
Classes
Functions
Data
API
Bases: Function
Chunked fp32 weighted-SiTU that saves only the low-precision inputs.
The forward computes the same fp32 chain as the eager _weighted_situ
path in row chunks (bitwise-identical result); the backward recomputes
the fp32 intermediates per chunk with analytic gradients that match
autograd’s fp32 chain, so autograd never stores full-size fp32 copies of
the [tokens, 2 * intermediate] projections.
Recompute fp32 per chunk and return analytic gradients.
Parameters:
Autograd context holding the saved low-precision inputs.
Upstream gradient with the forward’s output shape […, intermediate].
Returns: torch.Tensor
Tuple (d_gate_up, d_routing_weights, None, None) where
Apply SiTU and routing weights chunk by chunk.
Parameters:
Autograd context; saves gate_up and routing_weights
in their original (typically bf16 / fp32) dtypes.
Gate+up projections of shape […, 2 * intermediate], gate in the first half of the last axis, up in the second.
Routing weights, either row-aligned with shape
[…, k] matching gate_up’s leading dimensions (typically
[tokens, 1]) or broadcastable against them.
SiTU beta applied to the gate branch.
Optional bounded-linear beta applied to the up branch.
Returns: torch.Tensor
Tensor of shape “broadcast(gate_up.shape[:-1] + [intermediate],
Mix [tokens, hidden] with prior [tokens, blocks, hidden] residuals.
fp32 attention-residual mixing chain.
The weighted combine is multiply+sum rather than
torch.matmul(prob[T, 1, B], values[T, B, H]): cuBLAS dispatches that
degenerate batched-GEMM shape to non-tensor-core fp32 kernels
(magma_sgemmEx / gemv2 — 4.3% of busy GPU time on a 256×GB200 Kimi-K3
profile). multiply+sum runs the same fp32 math on the elementwise/reduce
path (identical up to fp32 accumulation order, which is below bf16
resolution for typical shapes) and fuses cleanly under torch.compile
when BackendConfig.compile_situ is set.
Parameters:
Stacked residuals of shape [tokens, blocks+1, hidden] (block residuals concatenated with the current prefix sum along axis 1).
RMSNorm weight of shape [hidden].
Squeezed attn-res projection weight of shape [hidden].
RMSNorm epsilon.
dtype of the returned mixed tensor.
Returns: torch.Tensor
Mixed residual of shape [tokens, hidden] in out_dtype.
Wrap the SiTU chunk cores and the attn-res core with torch.compile.
Runs once per process: the compiled functions replace the module-level eager cores, so every layer shares the same compiled kernels and repeated model construction does not recompile. Compilation itself is lazy (at first call). Compiled numerics are allclose to eager, not bitwise-identical.
Compute analytic fp32 SiTU gradients for one chunk of rows.
Parameters:
fp32 gate projections of shape [rows, intermediate].
fp32 up projections of shape [rows, intermediate].
fp32 routing weights broadcastable to [rows, intermediate], typically of shape [rows, 1].
fp32 upstream gradient of shape [rows, intermediate].
SiTU beta applied to the gate branch.
Optional bounded-linear beta applied to the up branch.
Whether the routing-weight gradient reduction is needed.
Returns: torch.Tensor
Tuple of fp32 tensors (d_g, d_u, red) where d_g and d_u
Compute the fp32 SiTU chain for one chunk of rows.
Parameters:
fp32 gate projections of shape [rows, intermediate].
fp32 up projections of shape [rows, intermediate].
fp32 routing weights broadcastable to [rows, intermediate], typically of shape [rows, 1].
SiTU beta applied to the gate branch.
Optional bounded-linear beta applied to the up branch.
Returns: torch.Tensor
fp32 tensor of shape [rows, intermediate]: situ(g) * up(u0) * w.
Return True when routing_weights carries one entry per gate_up row.
Parameters:
Gate+up projections of shape […, 2 * intermediate].
Routing weights; row-aligned when its shape is
[…, k] with the same leading dimensions as gate_up.
Apply SiTU and routing weights to [tokens, 2 * intermediate] projections.