Gated-attention layer

×

Gated attention and a mixture of experts, each reading the normalized residual stream and adding to it; every fourth layer: layers 4, 8, 12, …, 92.

h′=h+Δattnh'=h+\Delta_{\mathrm{attn}}
hnext=h′+Δffnh_{\mathrm{next}}=h'+\Delta_{\mathrm{ffn}}
All 42 steps
xattn=RMSNorm⁡(h)x_{\mathrm{attn}}=\operatorname{RMSNorm}(h)
Gated attention:\text{Gated attention:}
[ q  g ]=xattnWQ[\,{\htmlClass{vl-stream-q}{q}}\;g\,]=x_{\mathrm{attn}}W_{{\htmlClass{vl-stream-q}{Q}}}
qhh=[ q  g ]h, 1:256{\htmlClass{vl-stream-q}{q^{\mathrm h}_h}}=[\,{\htmlClass{vl-stream-q}{q}}\;g\,]_{h,\,1:256}
gh=[ q  g ]h, 257:512g_h=[\,{\htmlClass{vl-stream-q}{q}}\;g\,]_{h,\,257:512}
qn=(1+wq)⊙qhmean⁡((qh)2)+10−6{\htmlClass{vl-stream-q}{q^{\mathrm n}}}=(1+w_{{\htmlClass{vl-stream-q}{q}}})\odot\frac{{\htmlClass{vl-stream-q}{q^{\mathrm h}}}}{\sqrt{\operatorname{mean}(({\htmlClass{vl-stream-q}{q^{\mathrm h}}})^2)+10^{-6}}}
Q=[ RoPE⁡t(q1:64n)    q65:256n ]{\htmlClass{vl-stream-q}{Q}}=\big[\,\operatorname{RoPE}_{t}({\htmlClass{vl-stream-q}{q^{\mathrm n}_{1:64}}})\;\;{\htmlClass{vl-stream-q}{q^{\mathrm n}_{65:256}}}\,\big]
k=xattnWK{\htmlClass{vl-stream-k}{k}}=x_{\mathrm{attn}}W_{{\htmlClass{vl-stream-k}{K}}}
kn=(1+wk)⊙khmean⁡((kh)2)+10−6{\htmlClass{vl-stream-k}{k^{\mathrm n}}}=(1+w_{{\htmlClass{vl-stream-k}{k}}})\odot\frac{{\htmlClass{vl-stream-k}{k^{\mathrm h}}}}{\sqrt{\operatorname{mean}(({\htmlClass{vl-stream-k}{k^{\mathrm h}}})^2)+10^{-6}}}
K=[ RoPE⁡t(k1:64n)    k65:256n ]{\htmlClass{vl-stream-k}{K}}=\big[\,\operatorname{RoPE}_{t}({\htmlClass{vl-stream-k}{k^{\mathrm n}_{1:64}}})\;\;{\htmlClass{vl-stream-k}{k^{\mathrm n}_{65:256}}}\,\big]
v=xattnWV{\htmlClass{vl-stream-v}{v}}=x_{\mathrm{attn}}W_{{\htmlClass{vl-stream-v}{V}}}
KG[h]=K[⌊h/16⌋]{\htmlClass{vl-stream-k}{K_{\mathrm G}}}[h]={\htmlClass{vl-stream-k}{K}}\big[\lfloor h/16\rfloor\big]
VG[h]=V[⌊h/16⌋]{\htmlClass{vl-stream-v}{V_{\mathrm G}}}[h]={\htmlClass{vl-stream-v}{V}}\big[\lfloor h/16\rfloor\big]
S=QKGTS=QK_{\mathrm G}^{\mathsf T}
S′=S/256S'=S/\sqrt{256}
s≤ts\le t
A[b,i,t,s]=exp⁡(S′[b,i,t,s])∑u=0T−1exp⁡(S′[b,i,t,u])A[b,i,t,s]=\frac{\exp(S'[b,i,t,s])}{\sum_{u=0}^{T-1}\exp(S'[b,i,t,u])}
H=AVGH=AV_{\mathrm G}
o~=reshape⁡(H)\tilde o=\operatorname{reshape}(H)
o=σ(g)⊙o~o=\sigma(g)\odot\tilde o
Δattn=oWO\Delta_{\mathrm{attn}}=oW_{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=xffnWrz=x_{\mathrm{ffn}}W_{r}
p=softmax⁡(z)p=\operatorname{softmax}(z)
Eisel=[ i∈top⁡10(p) ]E^{\mathrm{sel}}_i=\big[\,i\in\operatorname{top}_{10}(p)\,\big]
Gi=pi Eisel∑jpj EjselG_{i}=\frac{p_i\,E^{\mathrm{sel}}_i}{\sum_{j}p_j\,E^{\mathrm{sel}}_j}
g=xffnWgate,ig=x_{\mathrm{ffn}}W_{\mathrm{gate},i}
u=xffnWup,iu=x_{\mathrm{ffn}}W_{\mathrm{up},i}
a=g σ(g)a=g\,\sigma(g)
h=a⊙uh=a\odot u
Ei=hWdown,iE_i=hW_{\mathrm{down},i}
yrouted=∑i=1512Gi Eiy^{\mathrm{routed}}=\sum_{i=1}^{512}G_i\,E_i
g=xffnWgateg=x_{\mathrm{ffn}}W_{\mathrm{gate}}
u=xffnWupu=x_{\mathrm{ffn}}W_{\mathrm{up}}
a=g σ(g)a=g\,\sigma(g)
h=a⊙uh=a\odot u
yshared=hWdowny^{\mathrm{shared}}=hW_{\mathrm{down}}
s=xffnwss=x_{\mathrm{ffn}}w_{s}
y^shared=σ(s) yshared\hat y^{\mathrm{shared}}=\sigma(s)\,y^{\mathrm{shared}}
Δffn=yrouted+y^shared\Delta_{\mathrm{ffn}}=y^{\mathrm{routed}}+\hat y^{\mathrm{shared}}
hnext=h′+Δffnh_{\mathrm{next}}=h'+\Delta_{\mathrm{ffn}}
in
hh [B batch, T positions, 8192 d]RR [B batch, T positions, 64 rotary features]
out
hnexth_{\mathrm{next}} [B batch, T positions, 8192 d]

Between forward calls a gated-attention layer keeps the keys and values of every position so far, 4 heads of 256 each: 2,048 values per position (modeling_qwen3_5_moe.py 789–790; cache_utils.py 1265).

Parameters26,243,785,216 × 23 layers

26,243,785,216 parameters per attention layer, across its learned components

ComponentParameters
Attention RMSNorm8,192
Gated attention419,430,912
MoE RMSNorm8,192
Mixture of experts25,824,337,920
Per-instance total26,243,785,216

One of 23 independent attention layers.

Explanation

Layers 4, 8, 12, …, 92: layer_types names them full_attention (config.json).

Sources

In codeQwen3_5MoeDecoderLayer(config, layer_idx)

Open pinned code ↗

Direct evidence

A decoder layer has Gated DeltaNet or gated attention by its layer type, the MoE block, and two RMSNorms.

modeling_qwen3_5_moe.py 933–944 ↗ · huggingface/transformers @ 18329f106538

Excerpt
class Qwen3_5MoeDecoderLayer(GradientCheckpointingLayer):
    def __init__(self, config: Qwen3_5MoeTextConfig, layer_idx: int):
        super().__init__()
        self.hidden_size = config.hidden_size
        self.block_type = config.layer_types[layer_idx]
        if self.block_type == "linear_attention":
            self.linear_attn = Qwen3_5MoeGatedDeltaNet(config, layer_idx)
        elif self.block_type == "full_attention":
            self.self_attn = Qwen3_5MoeAttention(config, layer_idx)
        self.mlp = Qwen3_5MoeSparseMoeBlock(config)
        self.input_layernorm = Qwen3_5MoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)
        self.post_attention_layernorm = Qwen3_5MoeRMSNorm(config.hidden_size, eps=config.rms_norm_eps)

A layer is pre-norm: the token mixer reads the normalised input and its output is added to the residual; the MoE reads the normalised sum and its output is added again.

modeling_qwen3_5_moe.py 946–989 ↗ · huggingface/transformers @ 18329f106538

Excerpt
    def forward(
        self,
        hidden_states: torch.Tensor,
        position_embeddings: tuple[torch.Tensor, torch.Tensor],
        attention_mask: torch.Tensor | None = None,
        position_ids: torch.LongTensor | None = None,
        past_key_values: Cache | None = None,
        **kwargs: Unpack[FlashAttentionKwargs],
    ) -> torch.FloatTensor:
        residual = hidden_states

        hidden_states = self.input_layernorm(hidden_states)

        # Token Mixer
        if self.block_type == "linear_attention":
            hidden_states = self.linear_attn(
                hidden_states=hidden_states,
                cache_params=past_key_values,
                attention_mask=attention_mask,
                **kwargs,
            )
        elif self.block_type == "full_attention":
            # Self Attention
            hidden_states, _ = self.self_attn(
                hidden_states=hidden_states,
                attention_mask=attention_mask,
                position_ids=position_ids,
                past_key_values=past_key_values,
                position_embeddings=position_embeddings,
                **kwargs,
            )

        hidden_states = residual + hidden_states

        # Fully Connected
        residual = hidden_states
        hidden_states = self.post_attention_layernorm(hidden_states)
        hidden_states = self.mlp(hidden_states)
        # For the MoE layers, we need to unpack
        if isinstance(hidden_states, tuple):
            hidden_states, _ = hidden_states
        hidden_states = residual + hidden_states

        return hidden_states

When layer_types is not given, a layer is full_attention when its 1-based index is divisible by full_attention_interval and linear_attention otherwise; the checkpoint’s configuration lists them explicitly in that pattern.

configuration_qwen3_5_moe.py 115–129 ↗ · huggingface/transformers @ 18329f106538

Excerpt
    layer_types: list[str] | None = None

… lines 116–121 omitted …

    def __post_init__(self, **kwargs):
        kwargs.setdefault("partial_rotary_factor", 0.25)  # assign default for BC
        if self.layer_types is None:
            interval_pattern = kwargs.pop("full_attention_interval", 4)
            self.layer_types = [
                "linear_attention" if bool((i + 1) % interval_pattern) else "full_attention"
                for i in range(self.num_hidden_layers)
            ]