Skip to content

Commit 47dd4db

Browse files
committed
Fix assisted decoding crash when the model returns no attentions
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.
1 parent c9b611d commit 47dd4db

2 files changed

Lines changed: 35 additions & 1 deletion

File tree

‎src/transformers/generation/utils.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4217,7 +4217,7 @@ def _assisted_decoding(
42174217
is_decoder_attention=True,
42184218
)
42194219
# some (V)LLMs have hard requirement on SDPA and thus never return attn
4220-
elif outputs.attentions[0] is not None:
4220+
elif outputs.attentions and outputs.attentions[0] is not None:
42214221
decoder_attentions = _split_model_outputs(
42224222
decoder_attentions,
42234223
outputs.attentions,

‎tests/generation/test_utils.py‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4811,6 +4811,40 @@ def test_prompt_lookup_decoding_no_eos_token(self):
48114811
# With no EOS to stop at, PLD proposes all num_output_tokens continuation tokens (10 + 4)
48124812
self.assertTrue(output_prompt_lookup.shape[-1] == 14)
48134813

4814+
def test_prompt_lookup_decoding_output_attentions_without_attentions(self):
4815+
# Assisted decoding collects the attentions of each generated token when `output_attentions=True`. Attention
4816+
# implementations that cannot return attentions (`sdpa`, the default for most models) return an empty
4817+
# `attentions` tuple, which used to be indexed unconditionally and raised
4818+
# `IndexError: tuple index out of range`. Every other decoding strategy only warns in this case.
4819+
config = AutoConfig.for_model(
4820+
"llama",
4821+
hidden_size=16,
4822+
intermediate_size=32,
4823+
num_hidden_layers=2,
4824+
num_attention_heads=2,
4825+
num_key_value_heads=2,
4826+
head_dim=8,
4827+
vocab_size=32,
4828+
max_position_embeddings=64,
4829+
)
4830+
config._attn_implementation = "sdpa"
4831+
model = AutoModelForCausalLM.from_config(config).to(torch_device).eval()
4832+
4833+
input_ids = torch.randint(1, 30, (1, 10), device=torch_device)
4834+
output = model.generate(
4835+
input_ids,
4836+
max_new_tokens=4,
4837+
do_sample=False,
4838+
eos_token_id=-1,
4839+
prompt_lookup_num_tokens=2,
4840+
return_dict_in_generate=True,
4841+
output_attentions=True,
4842+
)
4843+
4844+
# The model returns no attentions, so none are collected -- but generation must still succeed.
4845+
self.assertEqual(len(output.attentions), 0)
4846+
self.assertEqual(output.sequences.shape[-1], input_ids.shape[-1] + 4)
4847+
48144848
# TODO (ydshieh): Find a better way to handle flakyness, or find a working seed number.
48154849
@unittest.skip(reason="The seed 42 is no longer working after we switch to torch 2.13.")
48164850
def test_speculative_decoding_equals_regular_decoding(self):

0 commit comments

Comments
 (0)