<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
    <channel>
        <title>Long Context on KnightLi Blog</title>
        <link>https://www.knightli.com/en/tags/long-context/</link>
        <description>Recent content in Long Context on KnightLi Blog</description>
        <generator>Hugo -- gohugo.io</generator>
        <language>en</language>
        <lastBuildDate>Mon, 18 May 2026 18:38:26 +0800</lastBuildDate><atom:link href="https://www.knightli.com/en/tags/long-context/index.xml" rel="self" type="application/rss+xml" /><item>
        <title>DeepSeek-V4 KV Cache Explained: Why 1M Context Uses Less VRAM</title>
        <link>https://www.knightli.com/en/2026/05/18/deepseek-v4-kv-cache-compressed-attention/</link>
        <pubDate>Mon, 18 May 2026 18:38:26 +0800</pubDate>
        
        <guid>https://www.knightli.com/en/2026/05/18/deepseek-v4-kv-cache-compressed-attention/</guid>
        <description>&lt;p&gt;The real cost of long-context models is often not whether they can accept one million tokens, but how much VRAM the KV Cache consumes during inference.&lt;/p&gt;
&lt;p&gt;During Transformer decoding, every newly generated token needs access to the Key and Value states of previous tokens. The longer the context, the larger the KV Cache. A larger KV Cache puts pressure on VRAM, memory bandwidth, time to first token, and throughput.&lt;/p&gt;
&lt;p&gt;DeepSeek-V4 is interesting because it does not only reduce cache along the attention-head dimension. It pushes compression into the sequence-length dimension. According to Hugging Face&amp;rsquo;s discussion of DeepSeek-V4, in a 1M-token setting, DeepSeek-V4-Pro&amp;rsquo;s KV Cache is about 10% of DeepSeek-V3.2, and about 2% of a common bf16 GQA architecture.&lt;/p&gt;
&lt;p&gt;That is the key difference: DeepSeek-V4 does not merely store each KV entry in a smaller format. It reduces the number of KV entries that must be kept and searched over long history.&lt;/p&gt;
&lt;h2 id=&#34;several-generations-of-kv-cache-optimization&#34;&gt;Several generations of KV Cache optimization
&lt;/h2&gt;&lt;p&gt;KV Cache optimization has evolved through several routes.&lt;/p&gt;
&lt;p&gt;The first is traditional MHA, or Multi-Head Attention. Each Query head typically has its own Key/Value heads. The structure is direct, but under long context the cache grows linearly with sequence length, making VRAM pressure heavy.&lt;/p&gt;
&lt;p&gt;The second is GQA, or Grouped Query Attention. Multiple Query heads share fewer Key/Value heads. Many modern models such as LLaMA, Mistral, and Qwen use similar ideas. It significantly reduces KV head count and is now a common long-context optimization.&lt;/p&gt;
&lt;p&gt;The third is MLA, or Multi-head Latent Attention. DeepSeek-V2 and DeepSeek-V3 use this route, compressing Key/Value into low-rank latent representations and further reducing cache along the attention-head dimension.&lt;/p&gt;
&lt;p&gt;The fourth is DeepSeek-V4&amp;rsquo;s hybrid compressed attention. It focuses on sequence length: instead of only reducing how much KV each token stores, it compresses multiple historical tokens into fewer KV entries and retrieves them through sparse or dense attention.&lt;/p&gt;
&lt;p&gt;Roughly:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;MHA: every head remembers separately.&lt;/li&gt;
&lt;li&gt;GQA: multiple Query heads share memory.&lt;/li&gt;
&lt;li&gt;MLA: each token&amp;rsquo;s KV representation is compressed into a latent vector.&lt;/li&gt;
&lt;li&gt;DeepSeek-V4: many historical tokens are aggregated into fewer compressed memory blocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;key-change-from-head-compression-to-sequence-compression&#34;&gt;Key change: from head compression to sequence compression
&lt;/h2&gt;&lt;p&gt;GQA and MLA mainly optimize how much KV each token stores. That works well, but when context reaches 1M tokens, the token count itself becomes the problem.&lt;/p&gt;
&lt;p&gt;DeepSeek-V4 compresses old context into blocks. The model does not necessarily preserve full KV for every distant token. Instead, multiple tokens form compressed entries.&lt;/p&gt;
&lt;p&gt;It is a bit like reading a very long book: you remember recent pages in detail, while earlier chapters are stored more as summaries, themes, and key clues. DeepSeek-V4&amp;rsquo;s attention design follows a similar split: keep detail nearby, use compressed representation farther away.&lt;/p&gt;
&lt;h2 id=&#34;csa-4x-compression-plus-sparse-retrieval&#34;&gt;CSA: 4x compression plus sparse retrieval
&lt;/h2&gt;&lt;p&gt;CSA stands for Compressed Sparse Attention. It is the finer-grained long-context compression mechanism.&lt;/p&gt;
&lt;p&gt;In CSA, the model compresses neighboring tokens into fewer KV entries. The Hugging Face Transformers documentation gives a default compression ratio of &lt;code&gt;m=4&lt;/code&gt;, meaning roughly every four tokens become one compressed entry.&lt;/p&gt;
&lt;p&gt;But it is not simple averaging. CSA uses a learned compression pool and overlapping windows so the model can preserve more useful information. After compression, the query does not attend to all compressed blocks directly. It first uses a Lightning Indexer to score them, selects the most relevant top-k compressed blocks, and then performs the core attention computation.&lt;/p&gt;
&lt;p&gt;This gives two benefits:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The number of historical KV entries becomes smaller.&lt;/li&gt;
&lt;li&gt;Each query only looks at a relevant subset of compressed blocks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;CSA is suitable for long-range context where details still matter, such as codebases, long documents, and tool-call histories.&lt;/p&gt;
&lt;h2 id=&#34;hca-128x-compression-plus-dense-attention&#34;&gt;HCA: 128x compression plus dense attention
&lt;/h2&gt;&lt;p&gt;HCA stands for Heavily Compressed Attention, and it is more aggressive.&lt;/p&gt;
&lt;p&gt;The Transformers documentation gives a default compression ratio of &lt;code&gt;m&#39;=128&lt;/code&gt;. HCA compresses a much longer context span into one compressed entry. Because the compressed sequence becomes very short, it does not need sparse top-k retrieval like CSA. The query can simply perform dense attention over all HCA compressed entries.&lt;/p&gt;
&lt;p&gt;HCA acts more like a global summary. It does not try to preserve every detail. Instead, it covers very long history at extremely low cost, helping the model stay aware of global context, long-range topics, and far-away information.&lt;/p&gt;
&lt;p&gt;If CSA is &amp;ldquo;searchable compressed notes,&amp;rdquo; HCA is closer to a &amp;ldquo;global table of contents and summary.&amp;rdquo;&lt;/p&gt;
&lt;h2 id=&#34;sliding-window-recent-context-keeps-details&#34;&gt;Sliding window: recent context keeps details
&lt;/h2&gt;&lt;p&gt;DeepSeek-V4 does not compress everything.&lt;/p&gt;
&lt;p&gt;In addition to CSA and HCA, it keeps a sliding-window branch for the most recent uncompressed context. The Transformers documentation notes that DeepSeek-V4 attention blocks concatenate long-range compressed branches with sliding-window K/V.&lt;/p&gt;
&lt;p&gt;This matters. When generating the next token, the nearest context is often the most important: variable names, function signatures, the current sentence, fresh tool outputs, or the user&amp;rsquo;s latest instruction. If recent context were over-compressed, output quality would suffer.&lt;/p&gt;
&lt;p&gt;So the design is:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Nearby context: preserve uncompressed details.&lt;/li&gt;
&lt;li&gt;Mid-to-long context: use CSA for searchable compression.&lt;/li&gt;
&lt;li&gt;Farther context: use HCA for heavily compressed global summary.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&#34;hybrid-layer-stack-different-layers-use-different-attention&#34;&gt;Hybrid layer stack: different layers use different attention
&lt;/h2&gt;&lt;p&gt;DeepSeek-V4 does not use one attention mechanism in every layer.&lt;/p&gt;
&lt;p&gt;The Hugging Face DeepSeek-V4 article notes that V4-Pro&amp;rsquo;s 61-layer structure uses HCA in the first two layers, alternates CSA and HCA afterward, and uses a sliding-window MTP block at the end. The Transformers documentation also describes V4-Pro as using two HCA bootstrap layers followed by alternating CSA/HCA layers.&lt;/p&gt;
&lt;p&gt;This shows that DeepSeek-V4 treats attention as a layered system. Different layers handle different information roles: some favor global compression, some favor sparse retrieval, and some preserve local windows.&lt;/p&gt;
&lt;p&gt;Compared with using one attention type everywhere, this hybrid structure is more complex but better suited to 1M-token context.&lt;/p&gt;
&lt;h2 id=&#34;fp8-and-fp4-further-reduce-cache-cost&#34;&gt;FP8 and FP4 further reduce cache cost
&lt;/h2&gt;&lt;p&gt;DeepSeek-V4&amp;rsquo;s savings do not come only from compression ratio.&lt;/p&gt;
&lt;p&gt;The Hugging Face article notes that most KV entries in V4 use FP8 storage, RoPE-related dimensions remain BF16, and the Lightning Indexer in CSA uses FP4. Compression ratio, low-precision storage, and sparse retrieval together create very low KV Cache usage.&lt;/p&gt;
&lt;p&gt;This is a reminder: do not only look at the headline context length. Deployment feasibility is determined by VRAM usage, bandwidth pressure, latency, and implementation quality under long context.&lt;/p&gt;
&lt;h2 id=&#34;differences-from-other-models&#34;&gt;Differences from other models
&lt;/h2&gt;&lt;p&gt;Compared with traditional MHA, DeepSeek-V4 no longer keeps full attention memory for every token in long history, so cache pressure drops sharply.&lt;/p&gt;
&lt;p&gt;Compared with GQA, DeepSeek-V4 does not merely reduce the number of KV heads. It also reduces the number of KV entries for long history. GQA still accumulates cache linearly with sequence length; V4 compresses distant context into blocks.&lt;/p&gt;
&lt;p&gt;Compared with DeepSeek-V3&amp;rsquo;s MLA, V4 extends optimization from &amp;ldquo;making each token representation more compact&amp;rdquo; to &amp;ldquo;compressing the number of historical token entries.&amp;rdquo; MLA already lowers per-token KV cost significantly, but under million-token context, sequence length remains a bottleneck.&lt;/p&gt;
&lt;p&gt;Compared with ordinary sparse attention, CSA compresses first and then performs sparse retrieval over a shorter compressed sequence. HCA goes further, using 128x compression so dense attention becomes cheap.&lt;/p&gt;
&lt;h2 id=&#34;what-it-means-for-agents-and-long-tasks&#34;&gt;What it means for agents and long tasks
&lt;/h2&gt;&lt;p&gt;Agent workflows are especially hungry for long context. They read files, call tools, receive tool results, generate plans, revise plans, and call tools again. The longer the context, the more likely KV Cache becomes the bottleneck.&lt;/p&gt;
&lt;p&gt;DeepSeek-V4&amp;rsquo;s cache design may help in several ways:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Easier handling of long codebases, long documents, and multi-round tool histories.&lt;/li&gt;
&lt;li&gt;Less pressure on time to first token and throughput from KV Cache.&lt;/li&gt;
&lt;li&gt;Longer context or more concurrent requests on the same hardware.&lt;/li&gt;
&lt;li&gt;Million-token context becomes closer to practical deployment, not just a benchmark number.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;But compressed attention is not free. Compressing historical tokens into blocks involves information trade-offs. The model must balance saving VRAM with preserving retrievable details. Real performance depends on the task: code navigation, legal documents, long-form QA, and agent toolchains all have different detail-recall needs.&lt;/p&gt;
&lt;h2 id=&#34;do-not-read-2-as-2-of-all-cost&#34;&gt;Do not read 2% as 2% of all cost
&lt;/h2&gt;&lt;p&gt;&amp;ldquo;KV Cache is about 2% of GQA&amp;rdquo; is easy to misread.&lt;/p&gt;
&lt;p&gt;It mainly refers to KV Cache memory size. It does not mean total inference cost drops to 2%, or that every scenario becomes 50x faster. Inference still includes model weight reads, MoE routing, feed-forward networks, attention computation, scheduling, and communication overhead.&lt;/p&gt;
&lt;p&gt;The Hugging Face article separates two numbers: in 1M-token context, DeepSeek-V4-Pro&amp;rsquo;s per-token inference FLOPs are 27% of DeepSeek-V3.2, while KV Cache is 10%. Cache and compute are different dimensions.&lt;/p&gt;
&lt;p&gt;The safer statement is: DeepSeek-V4 greatly reduces KV Cache pressure for ultra-long context, improving deployment feasibility for million-token scenarios. Actual latency and throughput still depend on implementation, hardware, batching, quantization, and inference framework.&lt;/p&gt;
&lt;h2 id=&#34;summary&#34;&gt;Summary
&lt;/h2&gt;&lt;p&gt;The biggest difference between DeepSeek-V4 and other large models is that it moves KV Cache optimization from the attention-head dimension into the sequence-length dimension.&lt;/p&gt;
&lt;p&gt;GQA stores fewer KV heads. MLA makes each token&amp;rsquo;s KV representation more compact. DeepSeek-V4 further aggregates distant tokens into compressed blocks and combines CSA, HCA, sliding windows, and low-precision storage so million-token context is not immediately blocked by KV Cache.&lt;/p&gt;
&lt;p&gt;This is not a single trick. It is a long-context inference architecture: preserve details nearby, compress distant context, retrieve details when needed, and summarize globally when possible.&lt;/p&gt;
&lt;p&gt;For developers and agent applications, the meaning is direct: long context is not just about accepting more input. It must be runnable, stable, and affordable. That is what DeepSeek-V4 changes.&lt;/p&gt;
&lt;h2 id=&#34;references&#34;&gt;References
&lt;/h2&gt;&lt;ul&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://huggingface.co/blog/deepseekv4&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;Hugging Face: DeepSeek-V4: a million-token context that agents can actually use&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://huggingface.co/docs/transformers/model_doc/deepseek_v4&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;Hugging Face Transformers: DeepSeek-V4 model documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a class=&#34;link&#34; href=&#34;https://arxiv.org/abs/2412.19437&#34;  target=&#34;_blank&#34; rel=&#34;noopener&#34;
    &gt;DeepSeek-V3 Technical Report&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
        </item>
        
    </channel>
</rss>
