Skip to content

Fix assisted decoding crash when the model returns no attentions - #51244

Open
ranmori wants to merge 1 commit into
huggingface:mainfrom
ranmori:fix-assisted-decoding-empty-attentions
Open

ranmori wants to merge 1 commit into
huggingface:mainfrom
ranmori:fix-assisted-decoding-empty-attentions

Conversation

@ranmori

@ranmori ranmori commented Nov 25, 2026 •

Copy link
Copy Markdown

🤗 Transformers CI

What this fixes

generate() raises IndexError: tuple index out of range when assisted decoding (assistant_model=... or
prompt_lookup_num_tokens=...) is combined with output_attentions=True, for any model whose attention
implementation does not return attentions — including sdpa, the default for most models.

Every other decoding strategy only emits the usual
sdpa` attention does not support `output_attentions=True warning and returns empty attentions, so this is
specific to assisted decoding.

Reproducer (fails on main, CPU only, no checkpoint download)

import torch
from transformers import AutoConfig, AutoModelForCausalLM

config = AutoConfig.for_model(
    "llama", hidden_size=16, intermediate_size=32, num_hidden_layers=2,
    num_attention_heads=2, num_key_value_heads=2, head_dim=8,
    vocab_size=32, max_position_embeddings=64,
)
model = AutoModelForCausalLM.from_config(config).eval()  # sdpa by default
input_ids = torch.randint(1, 30, (1, 10))

model.generate(
    input_ids, max_new_tokens=4, do_sample=False, eos_token_id=-1,
    prompt_lookup_num_tokens=2, return_dict_in_generate=True, output_attentions=True,
)
File "src/transformers/generation/utils.py", line 4220, in _assisted_decoding
    elif outputs.attentions[0] is not None:
         ~~~~~~~~~~~~~~~~~~^^^
IndexError: tuple index out of range

Cause

The guard at generation/utils.py:4220 already intends to cover this case — its comment reads
"some (V)LLMs have hard requirement on SDPA and thus never return attn" — but it only handles an
attentions tuple populated with Nones. sdpa instead returns an empty tuple, so [0] raises
before the is not None check can run.

Fix

Check that the tuple is non-empty before indexing it:

elif outputs.attentions and outputs.attentions[0] is not None:

With the fix, attention collection is skipped (as the guard intended) and generation completes normally.

Why CI did not catch it

test_prompt_lookup_decoding_matches_greedy_search and test_assisted_decoding_matches_greedy_search both set
config._attn_implementation = "eager" so that output_attentions is supported, so the no-attentions path was
never exercised in assisted decoding.

Test

Adds test_prompt_lookup_decoding_output_attentions_without_attentions to
tests/generation/test_utils.py, built from a tiny in-process llama config (no checkpoint download, runs in
~1s). It fails on main with the IndexError above and passes with the fix.

Assisted decoding (`assistant_model=...` or `prompt_lookup_num_tokens=...`)
with `output_attentions=True` raised `IndexError: tuple index out of range`
for any attention implementation that does not return attentions, including
`sdpa`, the default for most models.

The guard already intended to cover this case -- its comment reads "some
(V)LLMs have hard requirement on SDPA and thus never return attn" -- but it
only handled an `attentions` tuple populated with `None`s. `sdpa` returns an
empty tuple instead, so indexing it raised before the `is not None` check
could run. Every other decoding strategy only warns and returns empty
attentions.

The existing assisted-decoding tests force `_attn_implementation = "eager"`
so that attentions are available, which is why the no-attentions path was
never exercised.
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution 🤗!

CI Security Gate — waiting for maintainer approval

CI is approved automatically only for authors who already have a contribution merged in this repository. A maintainer will review this PR and approve CI to run on it.

This branch has not been deployed

No deployments
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.

1 participant