TL;DR
Reading a massive document shouldn't require re-reading every single word just to write the next sentence, yet that is essentially what standard Transformer models do with their memory cache. CommunityKV speeds up long-context generation—without any retraining—by turning a model's initial attention scores into a social network of tokens and grouping them into semantic "communities." When generating new text, the model simply queries community representatives to fetch only the relevant memories in sub-linear time, achieving up to 1.71× faster throughput with virtually zero loss in accuracy.
What Problem Does This Paper Address?
When Large Language Models (LLMs) process long contexts—like entire codebases or multi-document histories—they store past token representations in a Key-Value (KV) cache. As generation continues, transferring this growing KV cache from memory at every step becomes a massive bottleneck.
Empirically, predicting the next token only relies on a tiny fraction of past tokens. Sparse attention tries to exploit this by fetching only the relevant bits. While recent architectures bake sparsity directly into pre-training, training-free methods—which you can drop onto existing models tonight—force an unpleasant choice:
- Permanent eviction: Delete "unimportant" tokens to save memory, which inevitably loses details you might unexpectedly need later.
- Coarse blocks: Group adjacent tokens together (fast, but pulls in irrelevant neighbors).
- Vector clustering: Run $k$-means on token embeddings (accurate, but computationally brutal to build and keep updated as new tokens stream in).
The authors ask: Can we get the semantic precision of clustering with fast, lightweight updates during streaming generation—all without retraining the model?
Methods
CommunityKV reframes sparse attention as a graph community detection problem across three phases:
- Graph Construction (Prefill): While processing the initial prompt, the model already computes attention scores ($QK^\top$). CommunityKV piggybacks on FlashAttention to grab each query's top-$\kappa$ strongest connections, building a sparse graph at almost no extra cost. Crucially, edges combine direct attention (Token A looks at Token B) and co-attention (Tokens B and C are both looked at by Token A). Initial "attention sink" tokens—which blindly attract high attention—are excluded so they don't act as giant hubs that blur community lines.
- Initial Partitioning: Using the standard Leiden algorithm, the graph is sliced into densely connected semantic communities asynchronously while the forward pass finishes. Each community gets a representative vector (a centroid).
- Incremental Update & Retrieval (Decode): For each new token generated, the model compares its query against the community centroids, retrieves tokens from the winning communities (plus the excluded sink tokens), and runs exact attention on that small budget. Finally, using a greedy local update rule, the newly generated token joins the neighboring community that maximizes modularity—taking constant $\mathcal{O}(d)$ time without re-clustering the graph.
Because the number of communities naturally scales with the square root of context length ($\sqrt{n}$), retrieval per step drops from linear $\mathcal{O}(nd)$ to sub-linear $\mathcal{O}(\sqrt{n}\,d)$.
Datasets
Because CommunityKV is completely training-free, no training datasets are used. The authors evaluate it on three established long-context benchmarks (no new benchmarks are introduced):
- LongBench v2: Challenging multiple-choice tasks covering code repositories, long dialogues, and multi-document QA.
- BABILong (QA1–QA5): A needle-in-a-haystack style multi-hop reasoning test requiring 1 to 5 supporting facts scattered across 64k-token contexts.
- RULER: A configurable suite testing retrieval, aggregation, and multi-hop tracing.
- (PG-19 is also used in an appendix stability test for ultra-long generation).
Key Insights and Ideas
- Don't delete memories; index them: Comparing CommunityKV to GraphKV (which builds a similar attention graph to permanently evict redundant tokens) shows a night-and-day difference. On Qwen3-8B, CommunityKV hits 31.1% accuracy with a 4k token budget, whereas static eviction only reaches 22.5% even when given an 8× larger (32k) budget.
- Direct + Co-attention is essential: Using only direct query-key links (28.5%) or only co-occurrence links (28.8%) degrades performance compared to blending them equally (32.0%). Two keys attended by the same query share semantic roles even if they never directly attend to one another.
- Shared graphs are a sweet spot for GQA: Modern models use Grouped-Query Attention (GQA), where multiple query heads share one KV head. Aggregating one graph per query group instead of per individual head slashes graph memory overhead from 52–82% of the KV cache down to just 12–15%, while boosting speed and keeping accuracy virtually identical (31.8% vs. 32.0%).
Key Results
Evaluated across Qwen3 (4B, 8B, and 14B), CommunityKV hits dense-attention accuracy while noticeably accelerating long generation:
- Accuracy Parity: At a 4,096-token retrieval budget on LongBench v2, CommunityKV matches exact FlashAttention-2 on Qwen3-4B (27.4%) and Qwen3-14B (32.0%), outperforming $k$-means clustering (Multipole, which plateaus at 28.8% on 14B) and block-heuristics (Quest, 30.6%).
- Multi-Hop Reasoning: On 64k-context BABILong with Qwen3-8B, CommunityKV leads all direct retrieval baselines with a 45.4% average (vs. 48.6% dense), trailing only SnapKV (49.2%).
- End-to-End Speedup: Generating 32,768 tokens from 64k and 128k contexts on an NVIDIA H200, per-head graphs yield 1.08–1.25× end-to-end throughput speedups. Switching to query-group aggregation pushes speedups to 1.39–1.71× while adding only 0.5–8.5% to peak GPU memory.
Limitations and Future Work
- Partition Drift: Because the fast update rule freezes old token assignments, communities slowly drift out of optimal shape over massive generation horizons (e.g., 16k+ generated tokens). Periodically re-running Leiden every $\sqrt{n}$ steps fixes this, though it introduces latency spikes in practice.
- Myopic Edge Updates: New tokens only form graph edges with the subset of tokens retrieved at that step, meaning true top-$\kappa$ connections hiding in unretrieved communities are missed.
- Future Directions: The authors suggest exploring local multi-node Leiden refinements instead of single-node greedy updates, leveraging Leiden's natural hierarchy to achieve $\mathcal{O}(d \log n)$ tree-like retrieval for million-token contexts, and sharing community structures across adjacent Transformer layers to save even more memory.
Summary updated