This repository implements a KV Cache mechanism in autoregressive transformer models and a KV cache compression algorithm that dynamically reduce the size of the cache to decrease its memory footprint.
To explore these concepts, I implemented a decoder-only transformer model using transformer blocks written in Equinox that are built on a MultiHeadAttention layer I implemented from scratch using JAX. The final model includes:
- Two Embedding layers covering token and positional embeddings
- Two pre-normalized Transformer Blocks
- A final two-layer MultiLayer Perceptron (MLP) with GELU activations
- Dropout layers applied after transformer blocks and the first layer of the final MLP
- A "vanilla" forward pass used during training and a
decodemethod that leverages the KV cache mechanism during inference
The model is trained on the Lichess chess games dataset to perform autoregressive next-move prediction on sequences of chess moves, where each move is treated as a discrete token.
Moreover, the trained model is evaluated with classic performance metrics (i. e., perplexity and top-1, 3, and 5 accuracy) along with an analysis of cache performance measured with per-token latency, Time To First Token (TTFT), and cumulative latency.
Afterwards, I implemented a
├── preprocess.py # Functions to preprocess the data and to apply tokenization
├── layers.py # Implementation of the layers of the model
├── model.py # Architecture of the final model
├── kv_cache.py # Implementation of the KV Cache and the cache compression method
├── train_evaluate.py # Functions to train and evaluate the model's performance, along with the functions used to evaluate the KV Cache mechanism and the compression method
└── analysis.ipynb # Final notebook containing the results relative to the model's training and evaluation and the assessment of the behavior of the KV Cache and the compression mechanism
https://jax-ml.github.io/scaling-book/inference/
https://magazine.sebastianraschka.com/p/coding-the-kv-cache-in-llms
Devoto, A., Zhao, Y., Scardapane, S., & Minervini, P. (2024). A Simple and Effective