Search

Search IconIcon to open search

Transformers Caching

Last updatedUpdated: by Jakub Žovák · 1 min read

Properties
created 11.11.2024, 10:38
modified 15.02.2026, 10:16
published Empty
topics Transformers
authors Empty
ai-assisted No

The key-value (KV) vectors are used to calculate attention scores. For autoregressive models, KV scores are calculated every time because the model predicts one token at a time. Each prediction depends on the previous tokens, which means the model performs the same computations each time.

A KV cache stores these calculations so they can be reused without recomputing them. Efficient caching is crucial for optimizing model performance because it reduces computation time and improves response rates. Refer to the  Caching doc for a more detailed explanation about how a cache works.

Transformers offers several  Cache classes that implement different caching mechanisms. Some of these  Cache classes are optimized to save memory while others are designed to maximize generation speed. Refer to the table below to compare cache types and use it to help you select the best cache for your use case.

Cache TypeSupports sliding layersSupports offloadingSupports torch.compile()Expected memory usage
Dynamic CacheYesYesNoMedium
Static CacheYesYesYesHigh
Quantized CacheNoNoNoLow

This guide introduces you to the different  Cache classes and shows you how to use them for generation.