Layers

Sliding-window layer

×

Sliding-window attention over the last 128 positions and a mixture of experts: 18 layers, the odd ones, 1 to 35.

h′=h+Δattnh'=h+\Delta_{\mathrm{attn}}
hnext=h′+Δffnh_{\mathrm{next}}=h'+\Delta_{\mathrm{ffn}}
All 32 steps
xattn=RMSNorm⁡(h)x_{\mathrm{attn}}=\operatorname{RMSNorm}(h)
Sliding-window attention:\text{Sliding-window attention:}
[ q  k  v ]=xattnWQKV+bQKV[\,{\htmlClass{vl-stream-q}{q}}\;{\htmlClass{vl-stream-k}{k}}\;{\htmlClass{vl-stream-v}{v}}\,]=x_{\mathrm{attn}}W_{QKV}+b_{QKV}
qh=[ q  k  v ]1:4096{\htmlClass{vl-stream-q}{q^{\mathrm h}}}=[\,{\htmlClass{vl-stream-q}{q}}\;{\htmlClass{vl-stream-k}{k}}\;{\htmlClass{vl-stream-v}{v}}\,]_{1:4096}
kh=[ q  k  v ]4097:4608{\htmlClass{vl-stream-k}{k^{\mathrm h}}}=[\,{\htmlClass{vl-stream-q}{q}}\;{\htmlClass{vl-stream-k}{k}}\;{\htmlClass{vl-stream-v}{v}}\,]_{4097:4608}
V=[ q  k  v ]4609:5120{\htmlClass{vl-stream-v}{V}}=[\,{\htmlClass{vl-stream-q}{q}}\;{\htmlClass{vl-stream-k}{k}}\;{\htmlClass{vl-stream-v}{v}}\,]_{4609:5120}
Q=RoPE⁡t(qh){\htmlClass{vl-stream-q}{Q}}=\operatorname{RoPE}_{t}({\htmlClass{vl-stream-q}{q^{\mathrm h}}})
K=RoPE⁡t(kh){\htmlClass{vl-stream-k}{K}}=\operatorname{RoPE}_{t}({\htmlClass{vl-stream-k}{k^{\mathrm h}}})
KG[h]=K[⌊h/8⌋]{\htmlClass{vl-stream-k}{K_{\mathrm G}}}[h]={\htmlClass{vl-stream-k}{K}}\big[\lfloor h/8\rfloor\big]
VG[h]=V[⌊h/8⌋]{\htmlClass{vl-stream-v}{V_{\mathrm G}}}[h]={\htmlClass{vl-stream-v}{V}}\big[\lfloor h/8\rfloor\big]
S=QKGTS=QK_{\mathrm G}^{\mathsf T}
S′=S/64S'=S/\sqrt{64}
t−128<s≤tt-128<s\le t
Ah,t,s=eSh,t,s′eb+∑s′eSh,t,s′′A_{h,t,s}=\frac{e^{S'_{h,t,s}}}{e^{b}+\sum_{s'}e^{S'_{h,t,s'}}}
H=AVGH=AV_{\mathrm G}
o=reshape⁡(H)o=\operatorname{reshape}(H)
Δattn=oWO+bO\Delta_{\mathrm{attn}}=oW_{O}+b_{O}
h′=h+Δattnh'=h+\Delta_{\mathrm{attn}}
xffn=RMSNorm⁡(h′)x_{\mathrm{ffn}}=\operatorname{RMSNorm}(h')
Mixture of experts:\text{Mixture of experts:}
z=xffnWr+brz=x_{\mathrm{ffn}}W_{r}+b_{r}
Eisel=[ i∈top⁡4(z) ]E^{\mathrm{sel}}_i=\big[\,i\in\operatorname{top}_{4}(z)\,\big]
Gi=ezi Eisel∑jezj EjselG_{i}=\frac{e^{z_i}\,E^{\mathrm{sel}}_i}{\sum_{j}e^{z_j}\,E^{\mathrm{sel}}_j}
[ g  u ]=xffnW1,i+b1,i[\,g\;u\,]=x_{\mathrm{ffn}}W_{1,i}+b_{1,i}
g=[ g  u ]0,2,4,…g=[\,g\;u\,]_{0,2,4,\ldots}
u=[ g  u ]1,3,5,…u=[\,g\;u\,]_{1,3,5,\ldots}
gˉ=min⁡(g, 7)\bar g=\min(g,\,7)
uˉ=clip⁡(u, −7, 7)\bar u=\operatorname{clip}(u,\,-7,\,7)
a=gˉ σ(1.702 gˉ)a=\bar g\,\sigma(1.702\,\bar g)
y=a⊙(uˉ+1)y=a\odot(\bar u+1)
Ei=yW2,i+b2,iE_i=yW_{2,i}+b_{2,i}
Δffn=∑i=1128Gi Ei\Delta_{\mathrm{ffn}}=\sum_{i=1}^{128}G_i\,E_i
hnext=h′+Δffnh_{\mathrm{next}}=h'+\Delta_{\mathrm{ffn}}
in
h0h_0 [B batch, T positions, 2880 d]RR [B batch, T positions, 64 rotary features]
out
hnexth_{\mathrm{next}} [B batch, T positions, 2880 d]
Parameters3,213,080,192 × 18 layers

3,213,080,192 parameters per window layer, across its learned components

ComponentParameters
Attention RMSNorm2,880
Sliding-window attention26,550,144
MoE RMSNorm2,880
Mixture of experts3,186,524,288
Per-instance total3,213,080,192

One of 18 independent window layers.

Explanation

Layers 1, 3, …, 35: counted from zero, the even ones (model.py 187–188); the Hugging Face configuration lists them as sliding_attention.

Each block’s norm belongs to the block: the attention block normalizes what enters it and adds its output to what entered, and so does the MLP block (model.py 218, 245, 313, 336).

Sources

In codeTransformerBlock(config, layer_idx) · 339–354

Open pinned code ↗

Direct evidence

A layer is the attention block then the MLP block, each adding its output to what entered it.

model.py 339–354 ↗ · openai/gpt-oss @ 5681a6b65526

Excerpt
class TransformerBlock(torch.nn.Module):
    def __init__(
        self,
        config: ModelConfig,
        layer_idx: int,
        device: torch.device | None = None,
    ):
        super().__init__()
        self.layer_idx = layer_idx
        self.attn = AttentionBlock(config, layer_idx, device)
        self.mlp = MLPBlock(config, device)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        x = self.attn(x)
        x = self.mlp(x)
        return x

The attention block’s forward: normalise; project; take the query, key and value features in that order; read queries as 8 groups of 8 heads of 64, keys and values as 8 heads of 64; rotate queries and keys; attend; project; add to the block’s input.

model.py 217–246 ↗ · openai/gpt-oss @ 5681a6b65526

Excerpt
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        t = self.norm(x)
        qkv = self.qkv(t)
        q = qkv[:, : self.num_attention_heads * self.head_dim].contiguous()
        k = qkv[
            :,
            self.num_attention_heads
            * self.head_dim : (self.num_attention_heads + self.num_key_value_heads)
            * self.head_dim,
        ].contiguous()
        v = qkv[
            :,
            (self.num_attention_heads + self.num_key_value_heads)
            * self.head_dim : (self.num_attention_heads + 2 * self.num_key_value_heads)
            * self.head_dim,
        ].contiguous()

        q = q.view(
            -1,
            self.num_key_value_heads,
            self.num_attention_heads // self.num_key_value_heads,
            self.head_dim,
        )
        k = k.view(-1, self.num_key_value_heads, self.head_dim)
        v = v.view(-1, self.num_key_value_heads, self.head_dim)
        q, k = self.rope(q, k)
        t = sdpa(q, k, v, self.sinks, self.sm_scale, self.sliding_window)
        t = self.out(t)
        t = x + t
        return t

The MLP block’s forward: normalise; score the experts; take the 4 highest scores and their experts; softmax over those 4; run each chosen expert’s first matrix and bias, the activation, its second matrix and bias; sum the 4 outputs by their weights; add to the block’s input.

model.py 312–336 ↗ · openai/gpt-oss @ 5681a6b65526

Excerpt
    def forward(self, x: torch.Tensor) -> torch.Tensor:
        t = self.norm(x)
        g = self.gate(t)
        experts = torch.topk(g, k=self.experts_per_token, dim=-1, sorted=True)
        expert_weights = torch.nn.functional.softmax(experts.values, dim=1)
        expert_indices = experts.indices

        # MLP #1
        mlp1_weight = self.mlp1_weight[expert_indices, ...]
        mlp1_bias = self.mlp1_bias[expert_indices, ...]
        t = torch.einsum("beck,bk->bec", mlp1_weight, t) + mlp1_bias
        t = swiglu(t, limit=self.swiglu_limit)

        # MLP #2
        mlp2_weight = self.mlp2_weight[expert_indices, ...]
        mlp2_bias = self.mlp2_bias[expert_indices, ...]
        t = torch.einsum("beck,bek->bec", mlp2_weight, t)
        if self.world_size > 1:
            dist.all_reduce(t, op=dist.ReduceOp.SUM)
        t += mlp2_bias

        # Weighted sum of experts
        t = torch.einsum("bec,be->bc", t, expert_weights)

        return x + t