Skip to content

fix: read the renamed cache kwarg on transformers >= 5.24 - #2047

Merged
jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
XiaoyuanWang111:fix/kv-cache-kwarg-transformers-5-24
Nov 24, 2026
Merged

jlarson4 merged 2 commits into
TransformerLensOrg:devfrom
XiaoyuanWang111:fix/kv-cache-kwarg-transformers-5-24

Conversation

@XiaoyuanWang111

@XiaoyuanWang111 XiaoyuanWang111 commented Nov 22, 2026 •

Copy link
Copy Markdown

Description

transformers 5.24.0 (released 2026-11-21, huggingface/transformers#50585) renamed past_key_values and cache_params to cache: decoder layers now pass the cache to attention modules and recurrent mixers as cache, and the SSM models' forward takes cache (with cache_params kept as a deprecated alias until 5.40). The bridges that read only the old names never see the cache on 5.24:

  • the bridges that rebuild attention (AttentionBridge._update_kv_cache, which reads past_key_values and layer_past; MLAAttentionBridge and GlmMoeDsaAttentionBridge, which pop past_key_values) never update it, so a forward with past_key_values=DynamicCache(), use_cache=True leaves the cache empty, and TransformerBridge.generate() with the default use_past_kv_cache=True feeds one token per step with no past;
  • the patched T5 block passes the cache to its self- and cross-attention sublayers only as past_key_values, so T5 cached generation breaks the same way;
  • GatedDeltaNetBridge (Qwen3-Next, Qwen3.5) takes HF's cached path only when cache_params is set, so during decoding it runs the hooked full-sequence path on one token and ignores the recurrent state;
  • the stateful generation loop (Mamba, Mamba-2, FalconMamba) passes the cache as past_key_values when forward has no cache_params parameter, which the 5.24 SSM models silently ignore, so every step starts from an empty state;
  • the opt-in eager_scan path of the Mamba mixers is meant for prefill only and is skipped when cache_params is set; on 5.24 it also runs on cached forwards.

pyproject.toml asks for transformers>=5.9.0 with no upper bound, so a fresh install gets 5.24.0. CI runs the lock's 5.13.0, which is why it does not show there.

The change reads cache first and falls back to the old names, so it works on both sides of the rename. The T5 block takes cache as well and passes the cache to each sublayer under whichever name its forward accepts, and the stateful loop passes the cache as cache when forward takes it. Passing past_key_values at the model level still works on 5.24 through transformers' deprecation alias, so the bridge's other model calls are left as they are.

To reproduce on dev with transformers 5.24.0 (downloads GPT-2):

import torch
from transformers import DynamicCache

from transformer_lens.model_bridge import TransformerBridge

bridge = TransformerBridge.boot_transformers("gpt2", device="cpu")
prompt = "The Eiffel Tower is located in the city of"
for use_cache in (False, True):
    text = bridge.generate(prompt, max_new_tokens=12, do_sample=False, use_past_kv_cache=use_cache, verbose=False)
    print(f"use_past_kv_cache={use_cache}: {text!r}")
cache = DynamicCache()
with torch.no_grad():
    bridge(bridge.to_tokens(prompt), past_key_values=cache, use_cache=True)
print("cache length after one forward:", cache.get_seq_length())

On dev with 5.24.0:

use_past_kv_cache=False: 'The Eiffel Tower is located in the city of London, and is the tallest building in the world. It'
use_past_kv_cache=True: 'The Eiffel Tower is located in the city of London London London London London London London London London London London London'
cache length after one forward: 0

With this change (and on dev with 5.23.0), both lines read London, and is the tallest building in the world. It and the cache length is 12.

Across architectures: I ran cached, uncached and Hugging Face greedy generation (6 new tokens, float32) for every decoder-only key of SUPPORTED_ARCHITECTURES that transformers can build from its own tiny tester config. On dev with 5.24.0, 69 architectures give different tokens with the cache on, among them GPT-2, Llama, Mistral, Mixtral, Qwen2/3, Qwen3-Next, Qwen3.5, Gemma 1/2/3, Llava, Mistral3, Phi-3, Cohere, DeepSeek V2/V3, GLM, GPT-OSS, OLMo, Granite, LFM2 and StableLM, and the cached tokens of Mamba-2 and FalconMamba no longer match Hugging Face's; with 5.23.0, none of this happens. With this change all of them agree again under 5.24.0, and nothing changes under 5.23.0.

Tests:

  • tests/integration/model_bridge/test_bridge_cache_kwarg.py (tiny from_config models, no download): for GPT-2 (joint QKV), Llama (rotary, GQA) and DeepSeek V3 (MLA), one forward fills a DynamicCache, and generate() gives the same tokens with the cache on, with it off, and from Hugging Face's own generate(); for Mamba-2 (stateful loop) and Qwen3-Next (gated delta-net next to attention), cached generate() matches Hugging Face's; the same comparison for T5 through the patched blocks.
  • TestUpdateKVCache.test_cache_kwarg_is_updated in tests/unit/model_bridge/generalized_components/test_kv_cache.py.

On dev with 5.24.0 all 10 new cases fail; with 5.13.0 (the lock) only the unit test fails, since it passes cache directly. With this change they pass under 5.24.0 and 5.13.0.

Existing tests: I ran the bridge's cache, generation, T5 and SSM/hybrid test files (26 files, among them test_bridge_manual_kv_cache.py, test_generate_attention_mask.py, test_bridge_generate_return_cache.py and the Mamba, Mamba-2, FalconMamba, Bamba, Jamba, LFM2 and NemotronH adapter tests). Under 5.24.0, 28 tests that fail on dev pass with this change (among them test_token_by_token_matches_full_forward, the Mamba and Mamba-2 test_greedy_matches_hf_exactly, and test_eager_requires_use_cache_false), and no test starts failing. Under 5.13.0 all of them pass with this change. I also ran the 21 test files of the multimodal adapters (Gemma 3n, GLM-4V, Qwen2.5-VL, Qwen3-VL, Qwen3.5 and their MoE variants) with timm and torchvision installed: on dev with 5.24.0, two Qwen3.5 tests fail (test_requires_use_cache_false in TestQwen3_5GatedDeltaNetEffectiveAttention and TestQwen3_5GatedDeltaNetState), and with this change all 387 pass under 5.24.0 and 5.13.0. Other failures under 5.24.0 (in the Mamba, Mamba-2, FalconMamba, Jamba, LFM2, NemotronH and SSM dispatch files) are the same on dev and come from other changes in 5.24; this PR does not touch them.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Checklist:

  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • I have not rewritten tests relating to key interfaces which would affect backward compatibility

transformers 5.24 passes the KV cache to attention modules, and to T5
blocks, as `cache` instead of `past_key_values` / `layer_past`. The bridges
that rebuild attention read only the old names, so the cache stayed empty:
cached generation (generate()'s default) diverged from uncached and from
Hugging Face's own generation on GPT-2, Llama, Mistral, Qwen2, Gemma, MLA
models, T5 and most other decoders. Read `cache` first in
_update_kv_cache, the MLA and GLM DSA attention bridges and the patched T5
block, which also hands it to sublayers that take `cache`.
transformers 5.24 also renamed `cache_params` to `cache`. GatedDeltaNetBridge
took HF's cached path only on `cache_params`, the stateful generation loop
passed the cache as `past_key_values` when forward had no `cache_params`
(ignored by the 5.24 SSM models), and the mixers' eager scan ran on cached
forwards. Read `cache` as well, and pass the cache as `cache` when forward
takes it. Adds Mamba-2 and Qwen3-Next cases to the cache tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants