Inference pipeline#
One decode step#
Core softmax update#
new_m = max(m, *scores)running max
if m == float("-inf"):
exp_old = 0.0
else:
exp_old = math.exp(m - new_m)rescale old sums
exp_new = [math.exp(sc - new_m) for sc in scores]
s = s * exp_old + sum(exp_new)running sum
Task reference#
| Task | Code |
|---|
| Count KV cache bytes | kv_cache_bytes(N, n_layers, n_heads_kv, d_head, dtype=2) |
|---|
| Tiled attention output | tiled_softmax_dot(q, Ks, Vs, tile=4) |
|---|
| Simulate batching | simulate_continuous_batching(requests, batch_size) |
|---|
| Cache a shared prefix | PrefixCache(max_entries=1000) |
|---|
| Speculate tokens | speculative_decode(draft_model, target_model, context, num_speculative=5) |
|---|
| Check memory fit | memory_budget(config, gpu_memory_gb) |
|---|
Defaults#
| Setting | Default | Note |
|---|
dtype in kv_cache_bytes | 2 | Bytes per value. fp16 or bf16 |
|---|
tile in tiled_softmax_dot | 4 | Keys per tile |
|---|
max_entries in PrefixCache | 1000 | Trie node limit |
|---|
num_speculative | 5 | Draft tokens per round |
|---|
acceptance_rate in DraftModel | 0.8 | Simulated accept rate |
|---|
CHUNK_SIZE | 512 | Prefill tokens per slice |
|---|
KV_BLOCK_SIZE | 16 | Tokens per KV block |
|---|
KV_BLOCKS_AVAILABLE | 1800 | Toy block budget |
|---|
Glossary#
- KV cache
- Stored keys and values for past tokens, so they are not recomputed.
- Prefill
- Processing the whole prompt before the first output token.
- Decode
- Producing one output token per step.
- TTFT
- Time to first token. Set when prefill ends.
- ITL
- Inter-token latency. Time between output tokens.