CommunityKV: Efficient Long-Context Decoding via Graph Partitioning

Joe McKenna, Anastasios Alexandridis, Nathan Susanj, Jing LiuNeurIPS 2026

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:

Figure 1: Overview of the CommunityKV framework. (1) Graph Construction. During prefill, we induce a sparse graph from the attention matrix by connecting each query to its top-κ keys (gray cells, κ = 3 shown). Sink tokens (hatched) are excluded from the top-κ pool. (2) Initial Partitioning. We partition the graph into semantic communities (colored regions) using the Leiden algorithm and compute a centroid (star) per community. (3) Incremental Update. At each decoding step (token 16 shown), we score the query against community centroids to retrieve a candidate subset, run exact attention on this subset together with sink tokens, and use the resulting top-κ keys to update the new token’s edges and assign it to a community in constant time. For clarity, only direct attention w(1) relations are depicted; this corresponds to λ = 1 in the combined graph.
  1. 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.
  2. 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).
  3. 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.
Table 1: Computational complexity comparison. n is sequence length, d is hidden dimension. Note that our method achieves sub-linear retrieval O(√nd) without expensive clustering or update costs.

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:

Table 3: Accuracy on LongBench v2 across token budgets (128–4096) at cluster size 16 for Qwen3 models (4B, 8B, 14B). CommunityKV accuracy improves with budget and reaches near dense parity at 4096 tokens across all model scales.
  • 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

Abstract

Scaling Transformers to long contexts is constrained by the quadratic cost of self-attention and the linear growth of key-value cache memory transfer. Sparse attention mitigates this by retrieving only relevant tokens, but current approaches either require large-scale training or, within the training-free regime, rely on semantically coarse heuristics or expensive clustering that is difficult to update efficiently during decoding. We introduce CommunityKV, a framework that formulates sparse attention as a community detection problem. CommunityKV constructs a token graph from the $QK^T$ scores already computed during standard prefill, and partitions the graph into communities to enable retrieval of semantically coherent token groups. A local update rule assigns newly generated tokens to communities in constant time, enabling sparse retrieval throughout streaming decoding without global re-partitioning. We evaluate CommunityKV on three Qwen3 model scales across three long-context benchmarks. With one graph per query head, CommunityKV delivers up to $1.25\times$ the end-to-end generation throughput of dense attention, while query-group graph aggregation yields up to $1.71\times$ with comparable accuracy.
BibTeX
@inproceedings{mckenna2026communitykv,
  author = {Joe McKenna and Anastasios Alexandridis and Nathan Susanj and Jing Liu},
  title = {{CommunityKV: Efficient Long-Context Decoding via Graph Partitioning}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2026},
  url = {https://openreview.net/forum?id=BNHDGnaga1},
}
Figure