Documentation Index
Fetch the complete documentation index at: https://mintlify.com/itsubaki/gpt/llms.txt
Use this file to discover all available pages before exploring further.
MultiHeadAttention is the core attention layer implementing scaled dot-product multi-head causal self-attention with optional KV caching. It contains four linear projection layers (Wq, Wk, Wv, Wo) and applies RoPE (Rotary Position Embedding) to both Q and K before computing attention scores.
MultiHeadAttention()
*MultiHeadAttentionT with four initialized Linear weight matrices.
Input and output embedding dimension
E. The Wq/Wk/Wv projections read from this dimension, and the Wo output projection writes back to it.Number of attention heads
H. The full hidden dimension H*D is split evenly across heads after projection.Per-head key/query/value dimension
D. Typically set to embedDim / numOfHeads. The scaling factor 1/sqrt(D) is applied to the raw attention scores.The rotary position embedding function. Construct it with
function.RoPE(theta, embedDim, contextLen) and share the same instance across all blocks in a model.Optional variadic flag. Pass
true to enable KV caching for autoregressive (token-by-token) generation. Defaults to false (no caching; used during training).Forward()
First() is a convenience wrapper that calls Forward and returns only the first (and only) output variable.
Tensor shape flow
| Step | Operation | Shape |
|---|---|---|
| Input | x | (B, C, E) |
| After Wq / Wk / Wv | linear projection | (B, C, H*D) |
| After reshape | split into heads | (B, C, H, D) |
| After transpose | head-first layout | (B, H, C, D) |
| After RoPE | rotary embeddings on Q, K | (B, H, C, D) |
QKᵀ / sqrt(D) | raw attention scores | (B, H, C, C) |
| With KV cache | scores against full cache | (B, H, C, C+offset) |
| After causal mask + softmax | attention weights | (B, H, C, C) or (B, H, C, C+offset) with cache |
After weights @ V | attended values | (B, H, C, D) |
| After transpose + reshape | concatenate heads | (B, C, H*D) |
| After Wo | output projection | (B, C, E) |
B = batch size, C = sequence (context) length, E = embedDim, H = numOfHeads, D = headDim.
When caching is active and this is not the first call, the attention scores have shape
(B, H, C, C+offset) because K and V are concatenated with previously cached values. The causal mask is only applied on the first call.Causal masking
The lower-triangular causal mask is applied whenuseCache is false, or when caching is enabled but this is the very first call (i.e., kCache == nil). The mask fills upper-triangle positions with -inf before softmax, preventing each position from attending to future tokens.
KV Cache behavior
WhenuseCache = true, MultiHeadAttentionT maintains an internal KV cache across successive Forward calls. This is the standard approach for efficient autoregressive text generation.
| Call | kCache before | Action | offset after |
|---|---|---|---|
1st (kCache == nil) | nil | initialize cache with K, V; apply causal mask | C |
| 2nd+ | (B, H, prev, D) | concat new K/V onto cache; skip causal mask | offset + C |
ClearCache()
kCache→nilvCache→niloffset→0
useCache = true.