Repository navigation
Transformer Concepts KV Cache
The KV Cache (Key-Value Cache) is an engineering optimization that makes autoregressive generation practical. Instead of recomputing the Keys and Values for every previous token at every single generation step, the model saves them in memory (the cache).
Without a KV cache, generating the 100th token would take 100 times longer than generating the 1st token, leading to quadratic slowdowns. Understanding the KV cache is essential for inference engineers dealing with memory limits and batch sizing.
TokenPrint explicitly visualizes the state of the KV cache during Live Inference:
-
The Phase Readout: The Bottom Bar shows exactly what phase the cache is in.
-
Prefill: Step 0. The model processes the entire prompt in parallel.
cache_len = 0. -
Decode: Step 1+. The model processes 1 token and appends its K and V to the cache.
cache_len = prompt_len + steps.
-
Prefill: Step 0. The model processes the entire prompt in parallel.
- Visual Cache Volume: When active, the 3D UI displays the KV cache as a spatial block alongside the Transformer Stack. During Prefill, you see a wide band of computation. During Decode, you see a single narrow slice of computation alongside a growing, dimmer block of cached memory.
Note TokenPrint relies on the backend
model.pyactively threading the PyTorchpast_key_valuesobject to determine these states. It is a reflection of real backend behavior, not a hardcoded frontend assumption.
sequenceDiagram
participant Input
participant Model
participant KVCache
Note over Input, KVCache: Step 0: PREFILL
Input->>Model: "The cat sat" (3 tokens)
Model->>KVCache: Save K, V for all 3
Model-->>Input: "on" (Token 4)
Note over Input, KVCache: Step 1: DECODE
Input->>Model: "on" (1 token)
Model->>KVCache: Read K, V for [1,2,3]
Model->>KVCache: Append K, V for [4]
Model-->>Input: "the" (Token 5)
| Previous | Home | Next |
|---|---|---|
| Multi-Head Attention | Home | LayerNorm |
TokenPrint Wiki | Home | Getting Started | User Guide | Concepts | Architecture | API | GitHub
TokenPrint is an interactive 3D visualization platform for exploring transformer architectures, tensors, and real-time LLM inference.
- User Guide
- Architecture Explorer
- Live Inference
- Tensor Inspector
- Camera Controls
- HUD
- Timeline
- Scene Navigation
- Transformer Concepts
- Tokenization
- Embeddings
- Positional Encoding
- RoPE
- Multi-Head Attention
- KV Cache
- LayerNorm
- Feed Forward Network
- Residual Connections
- Logits
- Softmax
- Sampling
- Autoregressive Generation
- Visualization System
- Scene Graph
- Geometry
- Materials
- Animation System
- Color Mapping
- Camera System
- Interaction Model
- Architecture
- Overall Architecture
- Frontend
- Backend
- WebSocket Protocol
- Data Pipeline
- Renderer
- Event System