Put the pieces together: implement the forward pass of one gpt-oss transformer block on a sequence, following OpenAI's reference implementation. gptoss_block(x, W, cfg, layer_idx) takes x of shape (T, d) and returns the block's output, also (T, d).
Weights in W follow PyTorch's Linear convention, :
attn_norm (d,), qkv_w ((Hq+2Hkv)*h, d), qkv_b, out_w (d, Hq*h), out_b (d,), sinks (Hq,), mlp_norm (d,), gate_w (E, d), gate_b (E,), mlp1_w (E, 2I, d), mlp1_b (E, 2I), mlp2_w (E, d, I), mlp2_b (E, d).
cfg has num_attention_heads, num_key_value_heads, head_dim, sliding_window, rope_theta, experts_per_token, swiglu_limit and eps.
Attention sub-block. RMSNorm ( times the scale) → qkv projection; the first outputs are the queries, the next the keys, the last the values, each reshaped to heads of size → RoPE on queries and keys with frequencies at positions , using the half-split convention (rotate the first half of each head against the second half: , ) → causal grouped-query attention with scale , a learned sink logit per query head, and the sliding window applied only when layer_idx is even (query sees keys with ) → output projection → add to .
MoE sub-block. RMSNorm → router logits → keep the top experts per token and softmax over those logits only → for each selected expert, mlp1, then gpt-oss's clamped SwiGLU on the interleaved halves (gate = even outputs clamped above at the limit, linear = odd outputs clamped to limit, gate (linear + 1)), then mlp2 → weighted sum of the expert outputs → add to the attention sub-block's output.
Work in float64.