| Vector | Role |
|---|---|
| Query, Q | The current position's representation used to match available positions. |
| Key, K | A position's representation used to calculate compatibility with a query. |
| Value, V | A position's information contributing to the weighted sum. |
Attention(Q,K,V) = softmax(QKᵀ / √d_k)V. With q·k = 12 and d_k = 9, the logit is 4. After softmax, weights 0.4 and 0.6 over scalar values 1 and 3 give 0.4 × 1 + 0.6 × 3 = 2.2.
Multi-head attention performs several such transformations. A standard split of d_model = 384 across 6 heads gives a width of 64 per head.
| Decoder training | Generation |
|---|---|
| Teacher forcing supplies correct preceding tokens. | The next token is chosen from the model's output and appended to its input. |
| A causal mask blocks future tokens; known positions can be computed in parallel. | The next unknown token depends on previous choices; ordinary decoding is sequential. |
KV-cache bytes for one sequence: 2 × layers × tokens × KV_heads × head_dim × bytes_per_number. The 2 counts K and V. A batch of B equally long sequences adds a factor B.
Example: 1 layer × 50 tokens × 2 KV heads × 4 numbers × 2 bytes × 2 = 1600 bytes before metadata.
| Method | Rule |
|---|---|
| Greedy | Select the highest-probability token. |
| Top-k | Keep the k highest-probability tokens, renormalize and sample. |
| Top-p | Keep the smallest sorted prefix whose sum is ≥ p, then renormalize and sample. |
| Temperature T | Divide logits by T before softmax. A lower positive T strengthens preference for leading tokens. |
| Beam search | Retain a limited number of partial continuations. A pruned path may eventually have been best. |
For [0.4, 0.35, 0.25] with top-p = 0.7, keep the first two tokens: 0.4 is insufficient; 0.75 is sufficient. max_new_tokens limits new tokens only, not prompt length.
If quantization hurts rare long queries, compare original and quantized models on identical queries, broken down by length and type. A single overall average can conceal the loss.
Calibration data are representative examples used to set quantization parameters, such as activation ranges in methods that use such calibration. A set containing only short, simple queries may poorly represent production workloads; include long and rare cases in the checks. This is separate from calibrating a classifier’s predicted probabilities.