Repository navigation
Conversation
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.
1 task done
5 of 7 tasks
This was referenced Nov 24, 2026
4 of 5 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
transformers 5.24.0 (released 2026-11-21, huggingface/transformers#50585) renamed
past_key_valuesandcache_paramstocache: decoder layers now pass the cache to attention modules and recurrent mixers ascache, and the SSM models'forwardtakescache(withcache_paramskept as a deprecated alias until 5.40). The bridges that read only the old names never see the cache on 5.24:AttentionBridge._update_kv_cache, which readspast_key_valuesandlayer_past;MLAAttentionBridgeandGlmMoeDsaAttentionBridge, which poppast_key_values) never update it, so a forward withpast_key_values=DynamicCache(), use_cache=Trueleaves the cache empty, andTransformerBridge.generate()with the defaultuse_past_kv_cache=Truefeeds one token per step with no past;past_key_values, so T5 cached generation breaks the same way;GatedDeltaNetBridge(Qwen3-Next, Qwen3.5) takes HF's cached path only whencache_paramsis set, so during decoding it runs the hooked full-sequence path on one token and ignores the recurrent state;past_key_valueswhenforwardhas nocache_paramsparameter, which the 5.24 SSM models silently ignore, so every step starts from an empty state;eager_scanpath of the Mamba mixers is meant for prefill only and is skipped whencache_paramsis set; on 5.24 it also runs on cached forwards.pyproject.tomlasks fortransformers>=5.9.0with 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
cachefirst and falls back to the old names, so it works on both sides of the rename. The T5 block takescacheas well and passes the cache to each sublayer under whichever name itsforwardaccepts, and the stateful loop passes the cache ascachewhenforwardtakes it. Passingpast_key_valuesat 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
devwith transformers 5.24.0 (downloads GPT-2):On
devwith 5.24.0:With this change (and on
devwith 5.23.0), both lines readLondon, and is the tallest building in the world. Itand 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_ARCHITECTURESthat transformers can build from its own tiny tester config. Ondevwith 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(tinyfrom_configmodels, no download): for GPT-2 (joint QKV), Llama (rotary, GQA) and DeepSeek V3 (MLA), one forward fills aDynamicCache, andgenerate()gives the same tokens with the cache on, with it off, and from Hugging Face's owngenerate(); for Mamba-2 (stateful loop) and Qwen3-Next (gated delta-net next to attention), cachedgenerate()matches Hugging Face's; the same comparison for T5 through the patched blocks.TestUpdateKVCache.test_cache_kwarg_is_updatedintests/unit/model_bridge/generalized_components/test_kv_cache.py.On
devwith 5.24.0 all 10 new cases fail; with 5.13.0 (the lock) only the unit test fails, since it passescachedirectly. 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.pyand the Mamba, Mamba-2, FalconMamba, Bamba, Jamba, LFM2 and NemotronH adapter tests). Under 5.24.0, 28 tests that fail ondevpass with this change (among themtest_token_by_token_matches_full_forward, the Mamba and Mamba-2test_greedy_matches_hf_exactly, andtest_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: ondevwith 5.24.0, two Qwen3.5 tests fail (test_requires_use_cache_falseinTestQwen3_5GatedDeltaNetEffectiveAttentionandTestQwen3_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 ondevand come from other changes in 5.24; this PR does not touch them.Type of change
Checklist: