Read
×Read each head’s memory with its query.
- in
- [B batch, 128 heads, 128 key d_h, 128 value d_h] [B batch, 128 heads, 1 position, 128 key d_h]
- out
- [B batch, 128 heads, 1 position, 128 value d_h]
Explanation
The read uses the memory after the current write, so a position can read what it has just stored.
Sources
In codecore_attn_out · 456, 479
Direct evidence
The gated delta rule per head, one position at a time: queries and keys optionally L2-normalised, queries divided by √d_k; the state is multiplied by exp(g), the write is β times the key times the difference between the value and what the state returns for the key, and the output reads the updated state with the query; in float32.
modeling_qwen3_5_moe.py 426–485 ↗ · huggingface/transformers @ 18329f106538
Excerpt
def torch_recurrent_gated_delta_rule(
query: torch.Tensor,
key: torch.Tensor,
value: torch.Tensor,
g: torch.Tensor,
beta: torch.Tensor,
initial_state: torch.Tensor | None = None,
output_final_state: bool = False,
use_qk_l2norm_in_kernel: bool = False,
**kwargs,
) -> tuple[torch.Tensor, torch.Tensor | None]:
"""Computes linear attention using the gated delta rule, by iterating over each token in the sequence dimension.
Same args and return value as torch_chunk_gated_delta_rule, except for `chunk_size` because the sequence dim is not
chunked."""
initial_dtype = query.dtype
batch_size, sequence_length, _, k_head_dim = key.shape
num_v_heads, v_head_dim = value.shape[-2:]
decay = g # rename for clarity: argument name must stay "g" to match flash_linear_attention's API
# Make sure all tensors are fp32 and reshape them to [batch_size, num_*_heads, seqlen, ...]
query, key, value, beta, decay = [
x.transpose(1, 2).to(torch.float32, memory_format=torch.contiguous_format)
for x in (query, key, value, beta, decay)
]
# If enabled, normalize query and key vectors (done once in fp32 for better accuracy)
if use_qk_l2norm_in_kernel:
query = l2norm(query, dim=-1, eps=1e-6)
key = l2norm(key, dim=-1, eps=1e-6)
# And always normalize queries by the head dimension
query = query / (query.shape[-1] ** 0.5)
# Create the storage for the last recurrent state, which will be updated in place. If a previous state is provided,
# it is the starting point, otherwise start with a zeroed buffer.
if initial_state is None:
recurrent_state_shape = (batch_size, num_v_heads, k_head_dim, v_head_dim)
last_recurrent_state = torch.zeros(recurrent_state_shape, dtype=value.dtype, device=value.device)
else:
last_recurrent_state = initial_state.to(value)
core_attn_out = torch.zeros_like(value)
# Loop over each token and update the recurrent state
for i in range(sequence_length):
q_t, k_t, v_t = query[:, :, i], key[:, :, i], value[:, :, i]
# Decay the recurrent state
decay_t = decay[:, :, i].exp()[..., None, None]
last_recurrent_state = last_recurrent_state * decay_t
# Update the recurrent state with the current token
beta_t = beta[:, :, i].unsqueeze(-1)
kv_mem = (last_recurrent_state * k_t.unsqueeze(-1)).sum(dim=-2)
delta = (v_t - kv_mem) * beta_t
last_recurrent_state = last_recurrent_state + k_t.unsqueeze(-1) * delta.unsqueeze(-2)
# And use it to compute the attention output for the current token
core_attn_out[:, :, i] = (last_recurrent_state * q_t.unsqueeze(-1)).sum(dim=-2)
# Discard the final state if not requested
last_recurrent_state = None if not output_final_state else last_recurrent_state
# Convert back to the original shape [batch_size, sequence_length, num_v_heads, v_head_dim] and dtype
core_attn_out = core_attn_out.transpose(1, 2).contiguous().to(initial_dtype)
return core_attn_out, last_recurrent_state