---
title: "KV Cache from scratch in nanoVLM"
description: "KV caching avoids recomputing keys and values for earlier tokens during autoregressive generation, and implementing it from scratch in the pure-PyTorch nanoVLM codebase gave a 38% speedup in generation."
url: "https://swe.quest/articles/kv-cache-from-scratch-in-nanovlm-f1d92b90631a1366"
canonical: "https://huggingface.co/blog/kv-cache"
source: "Hugging Face"
published: "2025-06-04"
difficulty: "medium"
kind: "How-to"
reading_minutes: 9
tags: ["inference","llms","performance","python"]
---

# KV Cache from scratch in nanoVLM

A post on [Hugging Face](https://swe.quest/sources/hugging-face.md), published 2025-06-04, 9 min read, How-to. Original: [https://huggingface.co/blog/kv-cache](https://huggingface.co/blog/kv-cache)

## Summary

KV caching avoids recomputing keys and values for earlier tokens during autoregressive generation, and implementing it from scratch in the pure-PyTorch nanoVLM codebase gave a 38% speedup in generation. The walkthrough shows the redundancy in self-attention with small PyTorch examples. It then makes three changes: the grouped attention block appends new K/V to a per-layer cache, the language model tracks the cache per layer and uses start\_pos to keep rotary position embeddings correct, and generation is split into a prefill phase and a token-by-token decode phase. The tradeoff is memory for speed, plus more complex code that makes schemes like beam search harder.

Written by swe.quest, not by the author.

**Difficulty:** Medium: assumes a working developer; shows how it is built or done

## Topics

- [Inference & serving](https://swe.quest/tags/inference.md) (AI & ML)
- [LLMs](https://swe.quest/tags/llms.md) (AI & ML)
- [Performance engineering](https://swe.quest/tags/performance.md) (Reliability & operations)
- [Python](https://swe.quest/tags/python.md) (Languages)

## Prerequisites

- transformer self-attention
- PyTorch basics
- autoregressive decoding

## Keywords

KV cache, nanoVLM, PyTorch, rotary positional embeddings, Hugging Face

## Related posts

- [Unlocking Longer Generation with Key-Value Cache Quantization](https://swe.quest/articles/unlocking-longer-generation-with-key-value-cache-quantization-c7f36c464d98f5fa.md): Hugging Face · 2024-05-16 · Medium · 11 min read · Inference & serving, LLMs, Python
  Hugging Face Transformers adds KV cache quantization, inspired by the KIVI paper, that stores keys and values per-token in int2/int4/int8 via quanto or HQQ backends.
- [Continuous batching from first principles](https://swe.quest/articles/continuous-batching-from-first-principles-b2a5a3d9c00e56be.md): Hugging Face · 2025-11-25 · Medium · 17 min read · Inference & serving, LLMs, Performance engineering
  Continuous batching falls out naturally once you optimize LLM serving for throughput, and the post derives it step by step from attention, causal masking, KV caching and chunked prefill.
- [Mastering Long Contexts in LLMs with KVPress](https://swe.quest/articles/mastering-long-contexts-in-llms-with-kvpress-778c6dc9287ed60d.md): Hugging Face · 2025-01-23 · Medium · 7 min read · Inference & serving, LLMs, Python, Performance engineering
  KV cache memory grows linearly with context length: Llama 3-70B at 1M tokens in bfloat16 needs about 330GB of KV cache, roughly 70% of total memory.
- [Prefill and Decode for Concurrent Requests - Optimizing LLM Performance](https://swe.quest/articles/prefill-and-decode-for-concurrent-requests-optimizing-llm-performance-66a6db57e54903ca.md): Hugging Face · 2025-04-16 · Medium · 10 min read · Inference & serving, LLMs, Performance engineering, Hardware
  LLM token generation splits into a compute-bound prefill phase, which processes all prompt tokens in parallel to produce the first token, and a memory-bandwidth-bound decode phase, which produces later tokens one at a time.
- [180B Parameters, 32GB of RAM: Pushing MoE Inference to the Limit with Transformers](https://swe.quest/articles/180b-parameters-32gb-of-ram-pushing-moe-inference-to-the-limit-with-transformers-63cd3cf21432f7c3.md): Hugging Face · 2026-11-18 · Hard · 11 min read · Inference & serving, LLMs, Performance engineering, Caching, Hardware
  Streaming MoE experts from SSD through a bounded device-side slot pool lets Transformers run Qwen3.8-Flash-Next (121 GB of experts) on a 32 GB MacBook M5 at ~13.9 tok/s, within 6% of the specialized Strata engine on decode and slightly faster on prefill.

Browse: [Latest posts](https://swe.quest/index.md) · [Topics](https://swe.quest/tags.md) · [Sources](https://swe.quest/sources.md) · [Topic map](https://swe.quest/map.md) · [Site guide (llms.txt)](https://swe.quest/llms.txt)
