First layer

Dense MLP

×

A dense MLP with the SiTU-GLU activation, 33,792 hidden features, used in the first layer only.

h=a⊙uˉh=a\odot\bar u
Δffn=hWdown\Delta_{\mathrm{ffn}}=hW_{\mathrm{down}}
All 6 steps
g=xffnWgateg=x_{\mathrm{ffn}}W_{\mathrm{gate}}
u=xffnWupu=x_{\mathrm{ffn}}W_{\mathrm{up}}
a=4tanh⁡ ⁣(g/4) σ(g)a=4\tanh\!\big(g/4\big)\,\sigma(g)
uˉ=25tanh⁡ ⁣(u/25)\bar u=25\tanh\!\big(u/25\big)
h=a⊙uˉh=a\odot\bar u
Δffn=hWdown\Delta_{\mathrm{ffn}}=hW_{\mathrm{down}}
in
xffnx_{\mathrm{ffn}} [B batch, 1 position, 7168 d]
out
Δffn\Delta_{\mathrm{ffn}} [B batch, 1 position, 7168 d]
Parameters726,663,168

726,663,168 parameters per instance, across its learned components

ComponentCalculationParameters
Gate projection7,168 × 33,792242,221,056
Up projection7,168 × 33,792242,221,056
Down projection33,792 × 7,168242,221,056
Per-instance total726,663,168
Explanation

The first layer uses this dense MLP; the other 92 use the mixture of experts (first_k_dense_replace is 1; modeling_kimi_linear.py 893–900).

Sources

In codeself.mlp · KimiMLP

Open pinned code ↗

Direct evidence

The dense feed-forward network: down(SiTU(gate x, up x)).

modeling_kimi_linear.py 273–301 ↗ · moonshotai/Kimi-K3 @ f831ab668142

Excerpt
class KimiMLP(nn.Module):
    def __init__(self, config: KimiLinearConfig, hidden_size=None, intermediate_size=None):
        super().__init__()
        self.config = config
        self.hidden_size = config.hidden_size if hidden_size is None else hidden_size
        self.intermediate_size = config.intermediate_size if intermediate_size is None else intermediate_size
        self.gate_proj = nn.Linear(
            self.hidden_size, self.intermediate_size, bias=False)
        self.up_proj = nn.Linear(
            self.hidden_size, self.intermediate_size, bias=False)
        self.down_proj = nn.Linear(
            self.intermediate_size, self.hidden_size, bias=False)
        if config.hidden_act == "situ":
            beta, linear_beta = _get_situ_activation_params(config)
            self.act_fn = SituAndMul(
                beta=beta,
                linear_beta=linear_beta,
            )
        else:
            self.act_fn = ACT2FN[config.hidden_act]

    def forward(self, x):
        if self.config.hidden_act == "situ":
            gate_up = torch.cat([self.gate_proj(x), self.up_proj(x)], dim=-1)
            down_proj = self.down_proj(self.act_fn(gate_up))
        else:
            down_proj = self.down_proj(self.act_fn(
                self.gate_proj(x)) * self.up_proj(x))
        return down_proj

A layer has Kimi Delta Attention or latent attention by its index, a dense MLP in the first first_k_dense_replace layers and a MoE in the rest, two pre-norms, and for each of its two attention-residual mixes an RMSNorm and a one-row projection.

modeling_kimi_linear.py 878–917 ↗ · moonshotai/Kimi-K3 @ f831ab668142

Excerpt
    def __init__(self, config: KimiLinearConfig, layer_idx: int):
        super().__init__()
        self.hidden_size = config.hidden_size
        self.config = config
        self.layer_idx = layer_idx
        if config.is_kda_layer(layer_idx):
            self.is_linear_attn = True
            self.self_attn = KimiDeltaAttention(
                config=config, layer_idx=layer_idx)
        elif config.is_mla:
            self.is_linear_attn = False
            self.self_attn = KimiMLAAttention(
                config=config, layer_idx=layer_idx)
        else:
            raise NotImplementedError
        if (
            config.num_experts is not None
            and layer_idx >= config.first_k_dense_replace
            and layer_idx % getattr(config, "moe_layer_freq", 1) == 0
        ):
            self.block_sparse_moe = KimiSparseMoeBlock(config)
        else:
            self.mlp = KimiMLP(config)
        self.input_layernorm = KimiRMSNorm(
            config.hidden_size, eps=config.rms_norm_eps)
        self.post_attention_layernorm = KimiRMSNorm(
            config.hidden_size, eps=config.rms_norm_eps)

        # Attention residual
        self.use_attn_residuals = getattr(config, "attn_res_block_size", None) is not None
        if self.use_attn_residuals:
            self.attn_res_block_size = config.attn_res_block_size
            self.self_attention_res_norm = KimiRMSNorm(
                config.hidden_size, eps=config.rms_norm_eps)
            self.mlp_res_norm = KimiRMSNorm(
                config.hidden_size, eps=config.rms_norm_eps)
            self.self_attention_res_proj = nn.Linear(
                config.hidden_size, 1, bias=False)
            self.mlp_res_proj = nn.Linear(
                config.hidden_size, 1, bias=False)