Urgent.News

What's breaking now, across thousands of outlets.

Tech

The KV Cache From Scratch

In every part of this series, one thing kept showing up: the notebook . In Part 1 it grew by 72 KB with every word. In Part 3 it was the part of the work that batching couldn't share. In Part 4 it stayed stuck on the memory side of the roof, however many users we added. Engineers call it the KV cache . Today we build one by hand, check that it really gives the same answer, weigh it in bytes, and…

In this report, we delve into the KV cache—a crucial component of language models that we will build manually. The KV cache works by storing question (Q), label (K), and value (V) cards for each word in a text. When generating the next word, the model compares the new word's Q card with the labels of previously generated words. The better the match, the more the content (V) card of the new word is mixed in.

Importantly, a word only looks backward—once a word has created its cards, they cannot be altered by later words. This leads to the creation of a notebook, which stores the K and V cards for all earlier words, enabling the model to read from this storage instead of regenerating the entire text.

To illustrate the efficiency of the KV cache, we compared two versions of a 12-layer GPT-2 small model (12 floors, width 768, 12 heads, random weights) while generating 32 words. The first version ran without the KV cache, with the time per new word ranging from 231.7 to 330.2 ms. The second version, with the KV cache enabled, showed significantly faster performance—20.4 to 21.4 ms per new word.

This represents a notable improvement, with the notebook making the typing process around 15 times faster, especially for longer answers where the difference becomes even more pronounced.

The KV cache's memory footprint is substantial. For each word, the notebook saves two cards (K and V) on every floor and for every head, resulting in a memory cost of approximately 73,728 bytes per token. This amounts to 74 MB for 1,000 tokens, 590 MB for 8,000 tokens, and 2359 MB for 32,000 tokens. Interestingly, once the notebook grows beyond approximately 4,600 words, it surpasses the size of the entire model itself.

Even with real-world models like Llama 3.1 8B, the notebook can consume a significant portion of the GPU memory. For instance, a single 8K-word chat on Llama 3.1 8B would require about 1 GB of notebook memory, which is more than the 16 GB the model's tables occupy. Consequently, this highlights the need for careful management of notebook memory, especially when dealing with long contexts in language models.

Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.

Read the original at dev.to →

More in Tech

Submitting URLs to IndexNow from PHP: getting Bing and Yandex to notice new pages in hours

Google has Search Console for submitting new pages, but Bing and Yandex have something simpler: IndexNow. You make one HTTP request naming a URL, and both search engines pick it up — usually within…

  • Submit URLs to IndexNow via PHP using a POST request with a key.
  • Generate a 32-character hex string key file at site root for proof of ownership.
  • PHP function indexnowsubmit handles key, host, and key location for API submission.

I told readers to date a Vercel deploy by reading two headers, and both of them moved without a deploy

On 31 October I published a post about the three caches sitting in front of PubTrivia's 72 public pages, and I ended one section with a tip: if you want to know when a Vercel site last shipped and you…

  • Author suggested dating Vercel deploys by reading two headers
  • Age header reported 12.2 days since last deploy, but deployment log showed 18 October
  • ETag changed while content remained same, causing fresh responses for returning readers

More from Wednesday 4 November →