Short convolution
×Mix each of the 20,480 channels with its own last 3 positions, then apply SiLU.
- in
- [B batch, 20480 channels, 4 kept inputs]
- out
- [B batch, 1 position, 20480 q, k, v features]
Parameters81,920 × 69 layers
81,920 parameters owned by this component
| Component | Calculation | Parameters |
|---|---|---|
| weights, no bias | 4 × 20,480 | 81,920 |
One in each of the 69 GDN layers, each with its own weights.
A depthwise convolution: 4 weights for each of the 20,480 channels, no bias.
Explanation
The kernel reads the current position and the ones before it, never later ones, so the convolution is causal.
Sources
In codecausal_conv1d_fn(mixed_qkv, self.conv1d.weight.squeeze(1), …) · 578–584
Direct evidence
The causal convolution: over a whole sequence a depthwise conv1d padded by kernel − 1 on the left and cut to the sequence; for one new position with a cache, the kept inputs move up by one, the new input goes last, and the convolution is taken over them; SiLU after either.
modeling_qwen3_5_moe.py 238–277 ↗ · huggingface/transformers @ 18329f106538
Excerpt
def causal_conv1d_update(
hidden_states: torch.Tensor,
conv_state: torch.Tensor,
weight: nn.Parameter,
bias: nn.Parameter | None = None,
activation: str | None = None,
):
_, hidden_size, seq_len = hidden_states.shape
state_len = conv_state.shape[-1]
hidden_states_new = torch.cat([conv_state, hidden_states], dim=-1).to(weight.dtype)
conv_state.copy_(hidden_states_new[:, :, -state_len:])
out = F.conv1d(hidden_states_new, weight.unsqueeze(1), bias, padding=0, groups=hidden_size)
out = out[:, :, -seq_len:]
if activation is not None:
out = ACT2FN[activation](out)
return out.to(hidden_states.dtype)
@use_kernel_func_from_hub_with_fallback("causal_conv1d_fn", "causal_conv1d")
def causal_conv1d_fn(
hidden_states: torch.Tensor,
weight: nn.Parameter,
bias: nn.Parameter | None = None,
activation: str | None = None,
**kwargs,
):
_, hidden_size, seq_len = hidden_states.shape
padding = weight.shape[-1] - 1
out = F.conv1d(
hidden_states.to(weight.dtype),
weight=weight.unsqueeze(1),
bias=bias,
padding=padding,
groups=hidden_size,
)[:, :, :seq_len]
if activation is not None:
out = ACT2FN[activation](out)
return out.to(hidden_states.dtype)Gated DeltaNet owns one projection to queries, keys and values (2 × 16 × 128 + 128 × 128 = 20,480 channels), one depthwise causal convolution over them without bias, a projection to the output gate z as wide as the values, one projection each to the write strength b and the decay a, one per value head, a per-head A_log and dt_bias, a gated RMSNorm over each value head and an output projection.
modeling_qwen3_5_moe.py 492–535 ↗ · huggingface/transformers @ 18329f106538
Excerpt
class Qwen3_5MoeGatedDeltaNet(nn.Module):
def __init__(self, config: Qwen3_5MoeConfig, layer_idx: int):
super().__init__()
self.hidden_size = config.hidden_size
self.num_v_heads = config.linear_num_value_heads
self.num_k_heads = config.linear_num_key_heads
self.head_k_dim = config.linear_key_head_dim
self.head_v_dim = config.linear_value_head_dim
self.key_dim = self.head_k_dim * self.num_k_heads
self.value_dim = self.head_v_dim * self.num_v_heads
self.conv_kernel_size = config.linear_conv_kernel_dim
self.layer_idx = layer_idx
self.activation = config.hidden_act
self.layer_norm_epsilon = config.rms_norm_eps
# QKV
self.conv_dim = self.key_dim * 2 + self.value_dim
self.conv1d = nn.Conv1d(
in_channels=self.conv_dim,
out_channels=self.conv_dim,
bias=False,
kernel_size=self.conv_kernel_size,
groups=self.conv_dim,
padding=self.conv_kernel_size - 1,
)
# time step projection (discretization)
# instantiate once and copy inv_dt in init_weights of PretrainedModel
self.dt_bias = nn.Parameter(torch.ones(self.num_v_heads))
# Lower bound kept away from 0 so log(A) never becomes -inf
A = torch.empty(self.num_v_heads).uniform_(0.01, 16)
self.A_log = nn.Parameter(torch.log(A))
self.norm = Qwen3_5MoeRMSNormGated(self.head_v_dim, eps=self.layer_norm_epsilon)
self.out_proj = nn.Linear(self.value_dim, self.hidden_size, bias=False)
self.layer_type = config.layer_types[layer_idx]
self.in_proj_qkv = nn.Linear(self.hidden_size, self.key_dim * 2 + self.value_dim, bias=False)
self.in_proj_z = nn.Linear(self.hidden_size, self.value_dim, bias=False)
self.in_proj_b = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)
self.in_proj_a = nn.Linear(self.hidden_size, self.num_v_heads, bias=False)The input is masked for padding, projected to the mixed queries, keys and values, to z, b and a; the mixed channels are convolved (with the kept inputs when decoding) and split into queries, keys and values, read as heads; β = sigmoid(b), g = −exp(A_log) · softplus(a + dt_bias) in float32; queries and keys are repeated to the value heads; the kernel runs recurrently for one new position with a cache and chunked otherwise, with the L2 normalisation of queries and keys inside it; the recurrent state is kept.
modeling_qwen3_5_moe.py 538–642 ↗ · huggingface/transformers @ 18329f106538
Excerpt
def forward(
self,
hidden_states: torch.Tensor,
cache_params: Cache | None = None,
attention_mask: torch.Tensor | None = None,
**kwargs: Unpack[TransformersKwargs],
):
hidden_states = apply_mask_to_padding_states(hidden_states, attention_mask)
# Set up dimensions for reshapes later
batch_size, seq_len, _ = hidden_states.shape
use_precomputed_states = cache_params is not None and cache_params.has_previous_state(
self.layer_idx, state_idx=0
)
mixed_qkv = self.in_proj_qkv(hidden_states)
mixed_qkv = mixed_qkv.transpose(1, 2)
z = self.in_proj_z(hidden_states)
z = z.reshape(batch_size, seq_len, -1, self.head_v_dim)
b = self.in_proj_b(hidden_states)
a = self.in_proj_a(hidden_states)
if use_precomputed_states and seq_len == 1 and not cache_params.layers[self.layer_idx].record_past:
conv_state = cache_params.layers[self.layer_idx].conv_states[0]
# Single-token cached decode: the fused per-step kernel updates the conv state in-place.
mixed_qkv = causal_conv1d_update(
mixed_qkv,
conv_state,
self.conv1d.weight.squeeze(1),
self.conv1d.bias,
self.activation,
)
else:
if cache_params is not None:
mixed_qkv = cache_params.update_conv_state(
mixed_qkv, self.layer_idx, conv_kernel_size=self.conv_kernel_size
)
mixed_qkv = causal_conv1d_fn(
mixed_qkv,
self.conv1d.weight.squeeze(1),
self.conv1d.bias,
activation=self.activation,
**kwargs,
)
# Drop the additional previous states
if cache_params is not None:
mixed_qkv = mixed_qkv[:, :, -seq_len:]
mixed_qkv = mixed_qkv.transpose(1, 2)
query, key, value = torch.split(
mixed_qkv,
[
self.key_dim,
self.key_dim,
self.value_dim,
],
dim=-1,
)
query = query.reshape(batch_size, seq_len, -1, self.head_k_dim)
key = key.reshape(batch_size, seq_len, -1, self.head_k_dim)
value = value.reshape(batch_size, seq_len, -1, self.head_v_dim)
beta = b.sigmoid()
# If the model is loaded in fp16, without the .float() here, A might be -inf
g = -self.A_log.float().exp() * F.softplus(a.float() + self.dt_bias)
if self.num_v_heads // self.num_k_heads > 1:
query = query.repeat_interleave(self.num_v_heads // self.num_k_heads, dim=2)
key = key.repeat_interleave(self.num_v_heads // self.num_k_heads, dim=2)
recurrent_state = cache_params.layers[self.layer_idx].recurrent_states[0] if use_precomputed_states else None
if use_precomputed_states and seq_len == 1:
core_attn_out, last_recurrent_state = torch_recurrent_gated_delta_rule(
query,
key,
value,
g=g,
beta=beta,
initial_state=recurrent_state,
output_final_state=cache_params is not None,
use_qk_l2norm_in_kernel=True,
cu_seqlens=kwargs.pop("cu_seq_lens_q", None),
**kwargs,
)
else:
core_attn_out, last_recurrent_state = torch_chunk_gated_delta_rule(
query,
key,
value,
g=g,
beta=beta,
initial_state=recurrent_state,
output_final_state=cache_params is not None,
use_qk_l2norm_in_kernel=True,
cu_seqlens=kwargs.pop("cu_seq_lens_q", None),
**kwargs,
)
# Update cache
if cache_params is not None:
cache_params.update_recurrent_state(last_recurrent_state, self.layer_idx)