Merge and project

Output projection

×

Map the merged features back to the model width, with a bias.

Δattn=oWO+bO{\htmlData{light=output output-projection}{\Delta_{\mathrm{attn}}}}={\htmlData{light=merge}{o}}W_{O}+b_{O}
in
oo [B batch, T positions, 4096 heads × d_h]
out
Δattn\Delta_{\mathrm{attn}} [B batch, T positions, 2880 d]

One matrix of 2,880 × 4,096 and a bias of 2,880, bfloat16 (model.py 199–204).

Parameters11,799,360 × 18 layers

11,799,360 parameters per instance.

ComponentCalculationParameters
Weight matrix4,096 × 2,88011,796,480
Output bias2,880
Per-instance total11,799,360

The reference module holds the weight output by input, [out, in].

One in each of the 18 global layers, each with its own weights.

Sources

In codeself.out(t) · 244

Open pinned code ↗

Direct evidence

The attention block: a window on the layers whose index, counted from zero, is even; one sink per query head, bfloat16; an RMSNorm; one fused query–key–value projection with bias to 64·(64 + 2·8) features; an output projection with bias from 64·64 features to the width; the scale 1/√64; its own rotary embedding with the configuration’s base, YaRN factor and NTK α and β.

model.py 176–215 ↗ · openai/gpt-oss @ 5681a6b65526

Excerpt
class AttentionBlock(torch.nn.Module):
    def __init__(
        self,
        config: ModelConfig,
        layer_idx: int = 0,
        device: torch.device | None = None,
    ):
        super().__init__()
        self.head_dim = config.head_dim
        self.num_attention_heads = config.num_attention_heads
        self.num_key_value_heads = config.num_key_value_heads
        # Only apply sliding window to every other layer
        self.sliding_window = config.sliding_window if layer_idx % 2 == 0 else 0
        self.sinks = torch.nn.Parameter(
            torch.empty(config.num_attention_heads, device=device, dtype=torch.bfloat16)
        )
        self.norm = RMSNorm(config.hidden_size, device=device)
        qkv_dim = config.head_dim * (
            config.num_attention_heads + 2 * config.num_key_value_heads
        )
        self.qkv = torch.nn.Linear(
            config.hidden_size, qkv_dim, device=device, dtype=torch.bfloat16
        )
        self.out = torch.nn.Linear(
            config.head_dim * config.num_attention_heads,
            config.hidden_size,
            device=device,
            dtype=torch.bfloat16,
        )
        self.sm_scale = 1 / math.sqrt(config.head_dim)
        self.rope = RotaryEmbedding(
            config.head_dim,
            config.rope_theta,
            torch.float32,
            initial_context_length=config.initial_context_length,
            scaling_factor=config.rope_scaling_factor,
            ntk_alpha=config.rope_ntk_alpha,
            ntk_beta=config.rope_ntk_beta,
            device=device,
        )

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