Skip to navigation

nemo_automodel.components.models.kimi_k3.situ

View as Markdown

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

NameDescription
_AttnResTritonFunctionAttention-residual mix on the fused Triton kernels; saves only the inputs and 3 x (k+1) fp32 stats per token.
_DenseSiTUFunctionDense SiTU (no routing weights) on the Triton kernels, saving only the low-precision input.
_WeightedSiTUFunctionChunked fp32 weighted-SiTU that saves only the low-precision inputs.

Functions

NameDescription
_apply_attn_resMix [tokens, hidden] with prior [tokens, blocks, hidden] residuals.
_attn_res_corefp32 attention-residual mixing chain.
_attn_res_triton_appliesReturn True when the fused Triton kernels handle this attention-residual mix call.
_compile_norm_coreWrap the RMSNorm core with torch.compile (BackendConfig.compile_norm).
_compile_situ_coresWrap the SiTU cores and the attn-res core with torch.compile.
_dense_situ_coreSiTU gated activation for dense / shared-expert MLPs (SituAndMul).
_enable_attn_res_tritonRoute the attention-residual mix through the fused Triton kernels (KimiK3TextConfig.attn_res_triton).
_enable_situ_tritonRoute SiTU through the hand-written Triton kernels (situ_backend = triton / triton_fast_math).
_rms_normDispatch RMSNorm through the current (eager or compiled) core.
_rms_norm_corefp32 RMSNorm chain shared by every KimiRMSNorm instance.
_situ_bwd_coreCompute analytic fp32 SiTU gradients for one chunk of rows.
_situ_fwd_coreCompute the fp32 SiTU chain for one chunk of rows.
_situ_rw_is_row_alignedReturn True when routing_weights carries one entry per gate_up row.
_situ_triton_appliesReturn True when the Triton SiTU kernels handle this [rows, 2 * intermediate] call.
_weighted_situApply SiTU and routing weights to [tokens, 2 * intermediate] projections.
_weighted_situ_bwd_fusedWhole-tensor weighted-SiTU backward for the compiled path.
_weighted_situ_fwd_fusedWhole-tensor weighted-SiTU forward for the compiled path.
dense_situApply the dense SiTU activation through the Triton kernels or the (possibly compiled) core.

Data

_ATTN_RES_TRITON_ENABLED

_RMS_NORM_COMPILED

_SITU_BWD_MATH

_SITU_CHUNK_ROWS

_SITU_CHUNK_THRESHOLD

_SITU_CORES_COMPILED

_SITU_FAST_MATH

_SITU_FWD_MATH

_SITU_TRITON_ENABLED

_dense_situ_dispatch

_rms_norm_dispatch

_weighted_situ_bwd_fused_dispatch

_weighted_situ_fwd_fused_dispatch

API

class nemo_automodel.components.models.kimi_k3.situ._AttnResTritonFunction()

Bases: Function

Attention-residual mix on the fused Triton kernels; saves only the inputs and 3 x (k+1) fp32 stats per token.

nemo_automodel.components.models.kimi_k3.situ._AttnResTritonFunction.backward(
ctx: typing.Any,
grad_out: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None, torch.Tensor | None, None]
staticmethod
nemo_automodel.components.models.kimi_k3.situ._AttnResTritonFunction.forward(
ctx: typing.Any,
prefix_sum: torch.Tensor,
block_residual: torch.Tensor,
norm_weight: torch.Tensor,
proj_weight: torch.Tensor,
eps: float
) -> torch.Tensor
staticmethod
class nemo_automodel.components.models.kimi_k3.situ._DenseSiTUFunction()

Bases: Function

Dense SiTU (no routing weights) on the Triton kernels, saving only the low-precision input.

nemo_automodel.components.models.kimi_k3.situ._DenseSiTUFunction.backward(
ctx: typing.Any,
grad_out: torch.Tensor
) -> tuple[torch.Tensor, None, None]
staticmethod
nemo_automodel.components.models.kimi_k3.situ._DenseSiTUFunction.forward(
ctx: typing.Any,
x: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor
staticmethod
class nemo_automodel.components.models.kimi_k3.situ._WeightedSiTUFunction()

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.

nemo_automodel.components.models.kimi_k3.situ._WeightedSiTUFunction.backward(
ctx: typing.Any,
grad_out: torch.Tensor
) -> tuple[torch.Tensor, torch.Tensor | None, None, None]
staticmethod

Recompute fp32 per chunk and return analytic gradients.

Parameters:

ctx
Any

Autograd context holding the saved low-precision inputs.

grad_out
torch.Tensor

Upstream gradient with the forward’s output shape […, intermediate].

Returns: torch.Tensor

Tuple (d_gate_up, d_routing_weights, None, None) where

nemo_automodel.components.models.kimi_k3.situ._WeightedSiTUFunction.forward(
ctx: typing.Any,
gate_up: torch.Tensor,
routing_weights: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor
staticmethod

Apply SiTU and routing weights chunk by chunk.

Parameters:

ctx
Any

Autograd context; saves gate_up and routing_weights in their original (typically bf16 / fp32) dtypes.

gate_up
torch.Tensor

Gate+up projections of shape […, 2 * intermediate], gate in the first half of the last axis, up in the second.

routing_weights
torch.Tensor

Routing weights, either row-aligned with shape […, k] matching gate_up’s leading dimensions (typically [tokens, 1]) or broadcastable against them.

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

Returns: torch.Tensor

Tensor of shape “broadcast(gate_up.shape[:-1] + [intermediate],

nemo_automodel.components.models.kimi_k3.situ._apply_attn_res(
prefix_sum: torch.Tensor,
block_residual: torch.Tensor,
projection: torch.nn.Linear,
) -> torch.Tensor

Mix [tokens, hidden] with prior [tokens, blocks, hidden] residuals.

nemo_automodel.components.models.kimi_k3.situ._attn_res_core(
values: torch.Tensor,
norm_weight: torch.Tensor,
proj_weight: torch.Tensor,
variance_epsilon: float,
out_dtype: torch.dtype
) -> torch.Tensor

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:

values
torch.Tensor

Stacked residuals of shape [tokens, blocks+1, hidden] (block residuals concatenated with the current prefix sum along axis 1).

norm_weight
torch.Tensor

RMSNorm weight of shape [hidden].

proj_weight
torch.Tensor

Squeezed attn-res projection weight of shape [hidden].

variance_epsilon
float

RMSNorm epsilon.

out_dtype
torch.dtype

dtype of the returned mixed tensor.

Returns: torch.Tensor

Mixed residual of shape [tokens, hidden] in out_dtype.

nemo_automodel.components.models.kimi_k3.situ._attn_res_triton_applies(
prefix_sum: torch.Tensor,
block_residual: torch.Tensor
) -> bool

Return True when the fused Triton kernels handle this attention-residual mix call.

Parameters:

prefix_sum
torch.Tensor

Current residual stream of shape [tokens, hidden].

block_residual
torch.Tensor

Prior block starts of shape [tokens, k, hidden].

nemo_automodel.components.models.kimi_k3.situ._compile_norm_core() -> None

Wrap the RMSNorm core with torch.compile (BackendConfig.compile_norm).

Runs once per process, same lazy pattern as _compile_situ_cores: the compiled function replaces the module-level eager core, so every KimiRMSNorm instance shares one compiled kernel. Compiled numerics are allclose to eager, not bitwise-identical.

nemo_automodel.components.models.kimi_k3.situ._compile_situ_cores() -> None

Wrap the SiTU 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). On this path _WeightedSiTUFunction runs each pass as one fused whole-tensor kernel (the eager row-chunk loop, its casts and slice copies are skipped) and SituAndMul uses the compiled dense core. Compiled numerics are allclose to eager, not bitwise-identical.

nemo_automodel.components.models.kimi_k3.situ._dense_situ_core(
x: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor

SiTU gated activation for dense / shared-expert MLPs (SituAndMul).

Parameters:

x
torch.Tensor

Gate+up projections of shape […, 2 * intermediate].

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

Returns: torch.Tensor

Tensor of shape […, intermediate] in x’s dtype.

nemo_automodel.components.models.kimi_k3.situ._enable_attn_res_triton() -> None

Route the attention-residual mix through the fused Triton kernels (KimiK3TextConfig.attn_res_triton).

Runs once per process. _apply_attn_res then launches attn_res_triton.attn_res_fwd_triton / attn_res_bwd_triton (no torch.cat, no fp32 copy of the stacked entries) for CUDA inputs with at most attn_res_triton.MAX_ENTRIES block entries; every other call keeps the eager or compile_situ chain. Without Triton the flag is a no-op. fp32 math matches _attn_res_core up to fp32 accumulation order.

nemo_automodel.components.models.kimi_k3.situ._enable_situ_triton(
fast_math: bool = False
) -> None

Route SiTU through the hand-written Triton kernels (situ_backend = triton / triton_fast_math).

Runs once per process. _WeightedSiTUFunction (row-aligned [rows, 1] weights on a CUDA device) and SituAndMul then launch situ_triton.situ_fwd_triton / situ_bwd_triton instead of the eager chunk loop or the compile_situ inductor kernels; every other shape keeps its existing path. Without Triton the flag is a no-op (the eager or compiled path stays in place). fp32 math and operation order match the eager cores; only the routing-weight gradient’s fp32 accumulation order differs. fast_math (situ_backend = "triton_fast_math") selects the SFU variants of the kernels; once any layer of the process asks for them every SiTU call uses them.

nemo_automodel.components.models.kimi_k3.situ._rms_norm(
hidden_states: torch.Tensor,
weight: torch.Tensor,
variance_epsilon: float
) -> torch.Tensor

Dispatch RMSNorm through the current (eager or compiled) core.

Indirection so KimiRMSNorm.forward picks up the compiled core even when _compile_norm_core runs after modules were constructed.

Parameters:

hidden_states
torch.Tensor

Tensor of shape […, hidden], with arbitrary leading dimensions.

weight
torch.Tensor

RMSNorm weight of shape [hidden].

variance_epsilon
float

RMSNorm epsilon.

Returns: torch.Tensor

Normalized tensor with the same shape and dtype as hidden_states.

nemo_automodel.components.models.kimi_k3.situ._rms_norm_core(
hidden_states: torch.Tensor,
weight: torch.Tensor,
variance_epsilon: float
) -> torch.Tensor

fp32 RMSNorm chain shared by every KimiRMSNorm instance.

Parameters:

hidden_states
torch.Tensor

Tensor of shape […, hidden], with arbitrary leading dimensions.

weight
torch.Tensor

RMSNorm weight of shape [hidden].

variance_epsilon
float

RMSNorm epsilon.

Returns: torch.Tensor

Normalized tensor with the same shape and dtype as hidden_states.

nemo_automodel.components.models.kimi_k3.situ._situ_bwd_core(
g: torch.Tensor,
u0: torch.Tensor,
w: torch.Tensor,
go: torch.Tensor,
beta: float,
linear_beta: float | None,
want_drw: bool
) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor | None]

Compute analytic fp32 SiTU gradients for one chunk of rows.

Parameters:

g
torch.Tensor

fp32 gate projections of shape [rows, intermediate].

u0
torch.Tensor

fp32 up projections of shape [rows, intermediate].

w
torch.Tensor

fp32 routing weights broadcastable to [rows, intermediate], typically of shape [rows, 1].

go
torch.Tensor

fp32 upstream gradient of shape [rows, intermediate].

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

want_drw
bool

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

nemo_automodel.components.models.kimi_k3.situ._situ_fwd_core(
g: torch.Tensor,
u0: torch.Tensor,
w: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor

Compute the fp32 SiTU chain for one chunk of rows.

Parameters:

g
torch.Tensor

fp32 gate projections of shape [rows, intermediate].

u0
torch.Tensor

fp32 up projections of shape [rows, intermediate].

w
torch.Tensor

fp32 routing weights broadcastable to [rows, intermediate], typically of shape [rows, 1].

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

Returns: torch.Tensor

fp32 tensor of shape [rows, intermediate]: situ(g) * up(u0) * w.

nemo_automodel.components.models.kimi_k3.situ._situ_rw_is_row_aligned(
gate_up: torch.Tensor,
routing_weights: torch.Tensor
) -> bool

Return True when routing_weights carries one entry per gate_up row.

Parameters:

gate_up
torch.Tensor

Gate+up projections of shape […, 2 * intermediate].

routing_weights
torch.Tensor

Routing weights; row-aligned when its shape is […, k] with the same leading dimensions as gate_up.

nemo_automodel.components.models.kimi_k3.situ._situ_triton_applies(
gate_up2: torch.Tensor,
routing_weights2: torch.Tensor | None
) -> bool

Return True when the Triton SiTU kernels handle this [rows, 2 * intermediate] call.

Parameters:

gate_up2
torch.Tensor

Flattened gate+up projections of shape [rows, 2 * intermediate].

routing_weights2
torch.Tensor | None

Row-aligned weights of shape [rows, k], or None for the dense activation. Only k == 1 is routed to Triton.

nemo_automodel.components.models.kimi_k3.situ._weighted_situ(
gate_up: torch.Tensor,
routing_weights: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor

Apply SiTU and routing weights to [tokens, 2 * intermediate] projections.

nemo_automodel.components.models.kimi_k3.situ._weighted_situ_bwd_fused(
gate_up2: torch.Tensor,
rw: torch.Tensor,
go2: torch.Tensor,
beta: float,
linear_beta: float | None,
want_drw: bool
) -> tuple[torch.Tensor, torch.Tensor | None]

Whole-tensor weighted-SiTU backward for the compiled path.

Parameters:

gate_up2
torch.Tensor

Saved gate+up projections of shape [rows, 2 * intermediate].

rw
torch.Tensor

Saved routing weights (see _weighted_situ_fwd_fused).

go2
torch.Tensor

Upstream gradient of shape [rows, intermediate].

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

want_drw
bool

Whether the routing-weight gradient is needed.

Returns: tuple[torch.Tensor, torch.Tensor | None]

(d_gate_up2, d_rw) in the inputs’ dtypes; d_rw is None when not wanted.

nemo_automodel.components.models.kimi_k3.situ._weighted_situ_fwd_fused(
gate_up2: torch.Tensor,
rw: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor

Whole-tensor weighted-SiTU forward for the compiled path.

Low-precision in, low-precision out, fp32 math inside; under torch.compile the casts fuse into the elementwise kernel, so no fp32 intermediate exists and the eager row-chunk loop is unnecessary.

Parameters:

gate_up2
torch.Tensor

Gate+up projections of shape [rows, 2 * intermediate].

rw
torch.Tensor

Routing weights, [rows, k] row-aligned or broadcastable to [rows, intermediate].

beta
float

SiTU beta applied to the gate branch.

linear_beta
float | None

Optional bounded-linear beta applied to the up branch.

Returns: torch.Tensor

Tensor of shape [rows, intermediate] in gate_up2’s dtype.

nemo_automodel.components.models.kimi_k3.situ.dense_situ(
x: torch.Tensor,
beta: float,
linear_beta: float | None
) -> torch.Tensor

Apply the dense SiTU activation through the Triton kernels or the (possibly compiled) core.

nemo_automodel.components.models.kimi_k3.situ._ATTN_RES_TRITON_ENABLED = False
nemo_automodel.components.models.kimi_k3.situ._RMS_NORM_COMPILED = False
nemo_automodel.components.models.kimi_k3.situ._SITU_BWD_MATH = _situ_bwd_core
nemo_automodel.components.models.kimi_k3.situ._SITU_CHUNK_ROWS = 32768
nemo_automodel.components.models.kimi_k3.situ._SITU_CHUNK_THRESHOLD = 12288
nemo_automodel.components.models.kimi_k3.situ._SITU_CORES_COMPILED = False
nemo_automodel.components.models.kimi_k3.situ._SITU_FAST_MATH = False
nemo_automodel.components.models.kimi_k3.situ._SITU_FWD_MATH = _situ_fwd_core
nemo_automodel.components.models.kimi_k3.situ._SITU_TRITON_ENABLED = False
nemo_automodel.components.models.kimi_k3.situ._dense_situ_dispatch = _dense_situ_core
nemo_automodel.components.models.kimi_k3.situ._rms_norm_dispatch = _rms_norm_core
nemo_automodel.components.models.kimi_k3.situ._weighted_situ_bwd_fused_dispatch = _weighted_situ_bwd_fused
nemo_automodel.components.models.kimi_k3.situ._weighted_situ_fwd_fused_dispatch = _weighted_situ_fwd_fused