SYNTOLOGY HomeExplorerAtlasCodeMethodologyAboutDevelopersFeedPricing
Paper · 2403.09054 · 2024

Keyformer: KV Cache Reduction through Key Tokens Selection for Efficient Generative Inference

arXiv · PDF · Open in the Atlas

Code that ran

3 of 8 samples (functions and classes) lifted from this paper's linked repositories executed on a synthesized input in Syntology's jail (0 of them constructed an object rather than computing a result; 1 honoured the contract we drafted, 0 violated it and 0 have no contract verdict; 2 failures of our instrument; 5 no recorded run), measured 2026-11-22; repository links from the Papers with Code archive attributed under CC BY-SA, no affiliation; bodies inline only under a permissive licence as GitHub detects it. Not a reproduction of the paper's results.

"Ran" means the function executed on a synthesized input and returned a value. It is not a reproduction of the paper's results. This record, dated, as JSON: /api/ran/2403.09054.json · badge: /api/ran/2403.09054.svg

Each repository's role is a label: a classifier's output recorded on the link, or, where no role is recorded, what the Papers with Code archive marked. The line under it names its basis. Neither says who wrote the code.

RepositoryRoleRan
d-matrix-ai/keyformer-llm canonical (classifier label)
classifier basis: census
3 ran · 5 no recorded run
FunctionStatusWhere it lives
create_sinusoidal_positions Ran
ran, honoured the contract we drafted
d-matrix-ai/keyformer-llm/models/gptj-keyformer-lib/modeling_gptj.py
code served (permissive licence) · get_code("c2217cd556fe5607")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 2, dtype family float, shape (2, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

ARGS = {
    "num_pos": 2,
    "dim": 4,
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 2, dtype family float, shape (2, 4) — shape (2, 4) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
get_embed_positions Ran
ran; our contract draft was wrong, not the code
d-matrix-ai/keyformer-llm/models/gptj-keyformer-lib/modeling_gptj.py
code served (permissive licence) · get_code("22d927bdb866159f")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 3, dtype family float, shape (2, 5, 4) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch
import torch.nn as nn

torch.manual_seed(0)

# embed_positions: a tensor representing embedding positions
# The function calls .to(position_ids.device) and .repeat(position_ids.shape[0], 1, 1)
# So it needs to support these operations
embed_positions = torch.randn(1, 5, 4)  # shape (1, seq_len, embed_dim)

# position_ids: a tensor with shape[0] that determines batch size and device
# The function uses position_ids.device and position_ids.shape[0]
position_ids = torch.tensor([[0, 1, 2, 3, 4], [0, 1, 2, 3, 4]])  # shape (2, 5) for batch_size=2

ARGS = {
    "embed_positions": embed_positions,
    "position_ids": position_ids,
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 3, dtype family float, shape (2, 5, 4) — shape (2, 5, 4) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
rotate_every_two Ran
ran; the contract check could not run (RAISES, DEP_MISSING or TIMEOUT)
d-matrix-ai/keyformer-llm/models/gptj-keyformer-lib/modeling_gptj.py
code served (permissive licence) · get_code("2e3bec9f0c9b1973")
How it ran

Ran in Syntology's sandbox on this synthesized input (drafted by a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API) and returned Tensor, ndim 4, dtype family float, shape (2, 3, 4, 6) as a later run recorded it. The image it ran in was not recorded. If your copy returns the same type, ndim and shape on these inputs, it matched what we observed; nothing here checks that. It does NOT mean the output is correct: nothing has compared it to the paper.

import torch

torch.manual_seed(0)

# The function rotate_every_two takes a single tensor argument x
# It expects a 4D tensor based on the indexing: x[:, :, :, ::2] and x[:, :, :, 1::2]
# The last dimension must be even so it can be split into pairs
# Let's create a small tensor with shape (batch=2, dim1=3, dim2=4, dim3=6)
# where dim3=6 is even so it can be split into pairs

x = torch.randn(2, 3, 4, 6)

ARGS = {
    "x": x
}
Setup block drafted by
a language model, Claude Haiku 4.5, prompt python_fixture_v1, through the batch API
Imports
torch — import names as written, not package names to install; a name the repository itself provides is not a package
Returned
type Tensor, ndim 4, dtype family float, shape (2, 3, 4, 6) — shape (2, 3, 4, 6) as a later run recorded it
dtype family
int means an integer dtype; float means any other dtype the instrument saw, bool and complex included (fixture_check.py records only these two families)
Environment
image not recorded; re-verify against syntology-harvest-sandbox:v5-deps; not recorded for this sample: the harness installs a cuda-to-cpu shim (cuda_shim.py) before every sample and no run records whether a sample needed it; seed: none by the harness; any seed is in the setup block
flash_attn_fn Not yet run
unverified (harvested, no recorded run)
d-matrix-ai/keyformer-llm/models/mpt-keyformer-lib/attention_streaming_llm.py
code served (permissive licence) · get_code("69a085d27f83db2a")
How it ran

no invocation recorded.

keyformer_mask Not yet run
unverified (harvested, no recorded run)
d-matrix-ai/keyformer-llm/models/mpt-keyformer-lib/attention_lm_eval_harness.py
code served (permissive licence) · get_code("5b88d9d6199110e1")
How it ran

no invocation recorded.

load_tf_weights_in_gpt2 Not yet run
unverified (harvested, no recorded run)
d-matrix-ai/keyformer-llm/models/cerebras-keyformer-lib/modeling_gpt2.py
code served (permissive licence) · get_code("bc9fb74e483fb901")
How it ran

no invocation recorded.

scaled_multihead_dot_product_attention Not yet run
unverified (harvested, no recorded run)
d-matrix-ai/keyformer-llm/models/mpt-keyformer-lib/attention_streaming_llm.py
code served (permissive licence) · get_code("aa98cb8ec8ec9683")
How it ran

no invocation recorded.

triton_flash_attn_fn Not yet run
unverified (harvested, no recorded run)
d-matrix-ai/keyformer-llm/models/mpt-keyformer-lib/attention_streaming_llm.py
code served (permissive licence) · get_code("a27f1a3818430d86")
How it ran

no invocation recorded.

Also present in other papers' code

Of 8 samples lifted from this paper's linked repositories, 8 carry a function identity and 0 do not. Of the 8 distinct functions with an identity, 6 are also present in other papers whose code we hold (6 with at least one paper dated earlier, 3 the same month, 3 later) and 2 are only here among the 52,977 papers whose code Syntology holds (as of 2026-09-26).

Only here means: among the 52,977 papers whose code Syntology holds (51,887 with lifted samples, 1,090 through their assemblies only), as of 2026-09-26, no other paper's lifted samples or current assembly bricks contain an identical function body after normalisation.

FunctionAlso present inEarliest-datedOthers named
load_tf_weights_in_gpt2 11 other papers
8 dated earlier, 3 later
2206.11871 (2022-06)
Offline RL for Natural Language Generation with Implicit Language Q Learning
2211.09817 (2022-11)
On the Effect of Pre-training for Transformer in Different Modality on Offline Reinforcement Learning
2211.11694 (2022-11)
Exploring Discrete Diffusion Models for Image Captioning
and 8 more not named here
create_sinusoidal_positions 4 other papers
2 dated earlier, 1 the same month, 1 later
2203.13474 (2022-03)
CodeGen: An Open Large Language Model for Code with Multi-Turn Program Synthesis
2311.13230 (2023-11)
Enhancing Uncertainty-Based Hallucination Detection with Stronger Focus
2403.04945 (2024-03)
MEIT: Multimodal Electrocardiogram Instruction Tuning on Large Language Models for Report Generation
and 1 more not named here
rotate_every_two 4 other papers
2 dated earlier, 1 the same month, 1 later
2203.13474 (2022-03)
CodeGen: An Open Large Language Model for Code with Multi-Turn Program Synthesis
2311.13230 (2023-11)
Enhancing Uncertainty-Based Hallucination Detection with Stronger Focus
2403.04945 (2024-03)
MEIT: Multimodal Electrocardiogram Instruction Tuning on Large Language Models for Report Generation
and 1 more not named here
flash_attn_fn 2 other papers
2 dated earlier
2402.13561 (2024-02)
Cognitive Visual-Language Mapper: Advancing Multimodal Comprehension with Enhanced Visual Knowledge Alignment
2402.18376 (2024-02)
Tokenization Is More Than Compression
get_embed_positions 2 other papers
1 dated earlier, 1 the same month
2311.13230 (2023-11)
Enhancing Uncertainty-Based Hallucination Detection with Stronger Focus
2403.04945 (2024-03)
MEIT: Multimodal Electrocardiogram Instruction Tuning on Large Language Models for Report Generation
triton_flash_attn_fn 2 other papers
2 dated earlier
2402.13561 (2024-02)
Cognitive Visual-Language Mapper: Advancing Multimodal Comprehension with Enhanced Visual Knowledge Alignment
2402.18376 (2024-02)
Tokenization Is More Than Compression

Presence, not direction: an identical function body in two papers' repositories does not say who wrote it, which came from which, or that either paper's method rests on it. Matching is exact after normalisation, so a renamed or edited function is not matched. Dates are when papers appeared, not when code was written. Only .py files the harvester lifted are covered; notebooks are not read. A new-style arXiv id YYMM.NNNN dates a paper to the month 20YY-MM; otherwise Paper.year dates it to the year; otherwise it is undated. Dates are when the paper appeared, not when the code was written. Read 2026-11-22.

Repositories linked to this paper

Some links come from the archived Papers with Code dataset (CC BY-SA 4.0): attribution and licence.

Abstract

Transformers have emerged as the underpinning architecture for Large Language Models (LLMs). In generative language models, the inference process involves two primary phases: prompt processing and token generation. Token generation, which constitutes the majority of the computational workload, primarily entails vector-matrix multiplications and interactions with the Key-Value (KV) Cache. This phase is constrained by memory bandwidth due to the overhead of transferring weights and KV cache values from the memory system to the computing units. This memory bottleneck becomes particularly pronounced in applications that require long-context and extensive text generation, both of which are increasingly crucial for LLMs. This paper introduces "Keyformer", an innovative inference-time approach, to mitigate the challenges associated with KV cache size and memory bandwidth utilization. Keyformer leverages the observation that approximately 90% of the attention weight in generative inference focuses on a specific subset of tokens, referred to as "key" tokens. Keyformer retains only the key tokens in the KV cache by identifying these crucial tokens using a novel score function. This approach effectively reduces both the KV cache size and memory bandwidth usage without compromising model accuracy. We evaluate Keyformer's performance across three foundational models: GPT-J, Cerebras-GPT, and MPT, which employ various positional embedding algorithms. Our assessment encompasses a variety of tasks, with a particular emphasis on summarization and conversation tasks involving extended contexts. Keyformer's reduction of KV cache reduces inference latency by 2.1x and improves token generation throughput by 2.4x, while preserving the model's accuracy.

For agents

The same record, over MCP at https://syntology.ai/mcp:

get_harvested_code_for_paper("2403.09054")
get_code_for_paper("2403.09054")
have("2403.09054")

The run record, dated, one paper per request, free:

curl https://syntology.ai/api/ran/2403.09054.json

A badge for a README (the split and the date, never a ratio):

[![Syntology run record](https://syntology.ai/api/ran/2403.09054.svg)](https://syntology.ai/paper/2403.09054)

Connect an agent — have() is free.