Skip to content

fix(generation): Enforce the auto-compile cache check for encoder-decoder models - #48364

Merged
vasqu merged 4 commits into
huggingface:mainfrom
harshaljanjani:fix/enc-dec-cache-compile-criteria
Aug 31, 2026
Merged

vasqu merged 4 commits into
huggingface:mainfrom
harshaljanjani:fix/enc-dec-cache-compile-criteria

Conversation

@harshaljanjani

@harshaljanjani harshaljanjani commented Aug 27, 2026 •

Copy link
Copy Markdown
Contributor

CPU CI GPU run-slow

What does this PR do?

→ Follow-up to this comment on the Canary PR; the encoder-decoder cache holds two subcaches and the auto-compile check isn't propagated through them.
→ No behaviour change on main

cc: @ebezzam @vasqu

Code Agent Policy

  • I confirm that this is not a pure code agent PR.

Before submitting

  • Did you read the contributor guideline, Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? Please add a link to it if that's the case.
  • Did you fix any necessary existing tests?

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also cc @Cyrilvallez and @zucchini-nlp for generation in general

Comment thread src/transformers/generation/utils.py Outdated
Comment on lines +2136 to +2139
# layers are, but we don't want to ALWAYS compile when calling `generate`, so we check the type. Encoder-decoder
# models hold that cache in a subcache, so we unwrap it to check the cache the decoder actually generates with
decoder_cache = cache.self_attention_cache if isinstance(cache, EncoderDecoderCache) else cache
using_compilable_cache = cache is not None and cache.is_compileable and type(decoder_cache) is not DynamicCache

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# layers are, but we don't want to ALWAYS compile when calling `generate`, so we check the type. Encoder-decoder
# models hold that cache in a subcache, so we unwrap it to check the cache the decoder actually generates with
decoder_cache = cache.self_attention_cache if isinstance(cache, EncoderDecoderCache) else cache
using_compilable_cache = cache is not None and cache.is_compileable and type(decoder_cache) is not DynamicCache
# Encoder-decoder models hold that cache in a subcache, so we unwrap it to check the cache the decoder actually generates with
decoder_cache = cache.self_attention_cache if isinstance(cache, EncoderDecoderCache) else cache
# Note: for some models that only use linear attention (e.g. Mamba), even a DynamicCache is compilable since all
# layers are, but we don't want to ALWAYS compile when calling `generate`, so we check the type
using_compilable_cache = cache is not None and cache.is_compileable and type(decoder_cache) is not DynamicCache

let's split the comments per line more readable imo

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't checking cache.is_compileable enough in general, i don't think we have compileable Dynamic Cache 🤔

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check the comment above (lin attn caches)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved, thank you both :)

Co-authored-by: Anton Vlasjuk <73884904+vasqu@users.noreply.github.com>
@harshaljanjani
harshaljanjani requested a review from vasqu August 27, 2026 10:50

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small last comments from my side

Comment thread tests/generation/test_utils.py Outdated
def test_compileable_default_cache_doesnt_compile_encoder_decoder(self):
"""Test that a compileable default cache doesn't trigger compilation on encoder-decoder models either"""
model = AutoModelForSeq2SeqLM.from_pretrained("hf-internal-testing/tiny-random-bart")
decoder_config = copy.deepcopy(model.config.get_text_config(decoder=True))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be overkill since it only lives for this test

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, dropped it :)

Comment thread tests/generation/test_utils.py Outdated
)
generation_config = GenerationConfig()
generation_config.compile_config = CompileConfig()
generation_config.compile_config._compile_all_devices = True

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just wondering, could we set it at construction time?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't unfortunately, it's a normal attr not a dataclass field in CompileConfig. Set it like the other usages.

generation_config = GenerationConfig()
generation_config.compile_config = CompileConfig()
generation_config.compile_config._compile_all_devices = True
self.assertFalse(model._valid_auto_compile_criteria({"past_key_values": cache}, generation_config))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess it needs the static cache to be true?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup because otherwise the test passes on CPU regardless. Static cache alone doesn't repro the issue either (both main and the PR return True). The actual diff in behavior is with a compileable DynamicCache (main returns True but the fix returns False) so that's the behavior we're trying to catch.

@harshaljanjani
harshaljanjani requested a review from vasqu August 31, 2026 13:25

@vasqu vasqu left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice thanks for the explanations, let's merge

@vasqu
vasqu enabled auto-merge August 31, 2026 14:04
@HuggingFaceDocBuilderDev

Copy link
Copy Markdown

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@vasqu

vasqu commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Hmm the failing test seems unrelated to me lemme try to rerun CI later today again

@github-actions

Copy link
Copy Markdown
Contributor

CI recap

Dashboard: View test results in Grafana
Latest run: 33395774991:4
Result: success | Jobs: 1 | Tests: 29 | Failures: 0 | Duration: 6m 20s

@vasqu

vasqu commented Aug 31, 2026

Copy link
Copy Markdown
Collaborator

Syncing with main worked 🤷 should merge soon

@vasqu
vasqu added this pull request to the merge queue Aug 31, 2026
Merged via the queue into huggingface:main with commit e37b269 Aug 31, 2026
115 checks passed
sbucaille pushed a commit to sbucaille/transformers that referenced this pull request Sep 16, 2026
…oder models (huggingface#48364)

* fix: Enforce the auto-compile cache check for encoder-decoder models

* change: Update generation/utils.py

Co-authored-by: Anton Vlasjuk <73884904+vasqu@users.noreply.github.com>

* fix: Changes

---------

Co-authored-by: Anton Vlasjuk <73884904+vasqu@users.noreply.github.com>
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.

4 participants