Gated Multi-head Latent Attention compresses each hidden token once and reconstructs keys and values when attention runs. Let hidden states X have shape (B,S,D), let H be the number of heads, let Dh=D/H, and let L be the latent width. The projections produce
Q=XWqT∈RB×S×D. C=XWcT∈RB×S×L. K=CWkT,V=CWvT,where both K and V have shape (B,S,D). Split Q, K, and V into H heads of width Dh. For each head, compute
A=softmax(DhQKT+M),where M masks later sequence positions when causal mode is enabled and is zero otherwise. These layers use no positional encoding. Concatenate the head contexts into O∈RB×S×D, then apply the channel gate and output projection:
Y=[sigmoid(XWgT)⊙O]WoT.Return a dictionary with output and latent_cache. The output has shape (B,S,D), and the cache has shape (B,S,L); both preserve the input floating-point dtype and device.
Examples
Input: hidden_states = [[[2]]], query_projection = [[1]], latent_down_projection = [[1]], key_up_projection = [[1]], value_up_projection = [[1]], output_gate_projection = [[0]], output_projection = [[1]], num_heads = 1, causal = true
Output: {"output": tensor([[[1.0]]]), "latent_cache": tensor([[[2.0]]])}
Explanation: The token is cached as the scalar latent value 2. Single-token attention reads that value, and the output gate scales it by 0.5.
Input: hidden states shape (1, 2, 2), latent width = 1, number of heads = 1, causal = True
Output: {"output": tensor of shape (1, 2, 2), "latent_cache": tensor of shape (1, 2, 1)}
Input: hidden states shape (1, 3, 2), latent width = 1, number of heads = 2, causal = False
Output: {"output": tensor of shape (1, 3, 2), "latent_cache": tensor of shape (1, 3, 1)}
Hint 1
Reshape projected tensors to batch, heads, sequence, head width before computing attention.
Hint 2
A causal mask excludes positions above the main diagonal before softmax.
Hint 3
Merge the head outputs back to model width before applying the channel gate and output projection.
Requirements
- Return a dictionary with exactly output and latent_cache.
- The output has the same shape, dtype, and device as the hidden states.
- The cache shape is (batch, sequence, latent width).
- Do not mutate any input tensor.
Constraints
- Model width is divisible by the positive head count.
- Projection matrices have compatible input and output widths.
- The causal flag is a Boolean value.
- All tensor inputs share a floating-point dtype and device.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number
Gated MLA
Gated Multi-head Latent Attention compresses each hidden token once and reconstructs keys and values when attention runs. Let hidden states X have shape (B,S,D), let H be the number of heads, let Dh=D/H, and let L be the latent width. The projections produce
Q=XWqT∈RB×S×D. C=XWcT∈RB×S×L. K=CWkT,V=CWvT,where both K and V have shape (B,S,D). Split Q, K, and V into H heads of width Dh. For each head, compute
A=softmax(DhQKT+M),where M masks later sequence positions when causal mode is enabled and is zero otherwise. These layers use no positional encoding. Concatenate the head contexts into O∈RB×S×D, then apply the channel gate and output projection:
Y=[sigmoid(XWgT)⊙O]WoT.Return a dictionary with output and latent_cache. The output has shape (B,S,D), and the cache has shape (B,S,L); both preserve the input floating-point dtype and device.
Examples
Input: hidden_states = [[[2]]], query_projection = [[1]], latent_down_projection = [[1]], key_up_projection = [[1]], value_up_projection = [[1]], output_gate_projection = [[0]], output_projection = [[1]], num_heads = 1, causal = true
Output: {"output": tensor([[[1.0]]]), "latent_cache": tensor([[[2.0]]])}
Explanation: The token is cached as the scalar latent value 2. Single-token attention reads that value, and the output gate scales it by 0.5.
Input: hidden states shape (1, 2, 2), latent width = 1, number of heads = 1, causal = True
Output: {"output": tensor of shape (1, 2, 2), "latent_cache": tensor of shape (1, 2, 1)}
Input: hidden states shape (1, 3, 2), latent width = 1, number of heads = 2, causal = False
Output: {"output": tensor of shape (1, 3, 2), "latent_cache": tensor of shape (1, 3, 1)}
Hint 1
Reshape projected tensors to batch, heads, sequence, head width before computing attention.
Hint 2
A causal mask excludes positions above the main diagonal before softmax.
Hint 3
Merge the head outputs back to model width before applying the channel gate and output projection.
Requirements
- Return a dictionary with exactly output and latent_cache.
- The output has the same shape, dtype, and device as the hidden states.
- The cache shape is (batch, sequence, latent width).
- Do not mutate any input tensor.
Constraints
- Model width is divisible by the positive head count.
- Projection matrices have compatible input and output widths.
- The causal flag is a Boolean value.
- All tensor inputs share a floating-point dtype and device.
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: array
Accepts: number