huggingface/transformers · error · ValueError
The `output_attentions` attribute is not supported when usin
Error message
The `output_attentions` attribute is not supported when using the `attn_implementation` set to {self._attn_implementation}. Please set it to 'eager' instead. What it means
ValueError raised by the output_attentions setter. Attention weights are only returned by the eager attention implementation, so setting output_attentions=True while a non-eager implementation (sdpa, flash_attention_2, flex) is selected is rejected. Convenience: setting output_attentions=True before any implementation is chosen auto-dispatches eager.
Source
Thrown at src/transformers/configuration_utils.py:409
# compute it based on the length of the `id2label` map
if self.id2label is None or self.num_labels != num_labels:
self.id2label = {i: f"LABEL_{i}" for i in range(num_labels)}
self.label2id = dict(zip(self.id2label.values(), self.id2label.keys()))
@property
def output_attentions(self):
"""
`bool`: Whether or not the model should returns all attentions.
"""
return self._output_attentions
@output_attentions.setter
def output_attentions(self, value: bool):
# If we set `output_attentions` explicitly before the attn implementation, dispatch eager
if value and self._attn_implementation is None:
self._attn_implementation = "eager"
if value and self._attn_implementation != "eager":
raise ValueError(
"The `output_attentions` attribute is not supported when using the `attn_implementation` set to "
f"{self._attn_implementation}. Please set it to 'eager' instead."
)
self._output_attentions = value
@property
def _attn_implementation(self):
return self._attn_implementation_internal
@_attn_implementation.setter
def _attn_implementation(self, value: str | dict | None):
"""We set it recursively on the sub-configs as well"""
# Set if for current config
current_attn = getattr(self, "_attn_implementation", None)
attn_implementation = value if not isinstance(value, dict) else value.get("", current_attn)
self._attn_implementation_internal = attn_implementation
# Set it recursively on the subconfigsView on GitHub (pinned to a597f97485)
Solutions
- Set attn_implementation='eager' (before or after enabling output_attentions) when you need attention weights
- Or set output_attentions first while _attn_implementation is None so eager is auto-selected
- Only enable attentions for the inspection run; keep sdpa/FA2 for training and inference
Example fix
# before cfg.attn_implementation = 'sdpa' cfg.output_attentions = True # ValueError # after cfg.attn_implementation = 'eager' cfg.output_attentions = True
Defensive patterns
Strategy: validation
Validate before calling
if output_attentions:
assert cfg._attn_implementation in (None, 'eager'), 'output_attentions requires eager attention'
cfg.attn_implementation = 'eager'
cfg.output_attentions = output_attentions Type guard
def attentions_supported(cfg) -> bool:
return cfg._attn_implementation in (None, 'eager') Try / catch
try:
cfg.output_attentions = True
except ValueError as e:
if 'not supported' in str(e):
cfg.attn_implementation = 'eager'
cfg.output_attentions = True
else:
raise Prevention
- Decide up front: eager for inspection runs, sdpa/FA2 for throughput — never both flags
- Set output_attentions before any attn_implementation to get eager auto-dispatch
When it happens
Trigger: config.attn_implementation = 'sdpa' followed by config.output_attentions = True; or from_pretrained(..., attn_implementation='flash_attention_2', output_attentions=True). The setter order matters: attentions first (with _attn_implementation None) silently selects eager.
Common situations: Attention-map visualization or interpretability tooling on models loaded with SDPA/FA2 for speed; config dicts from_pretrained that set both keys; fine-tuning scripts enabling attentions globally.
Related errors
- out_indices must be a list, got {type(self._out_indices)}
- out_indices must be valid indices for stage_names {self.stag
- out_indices must not contain any duplicates, got {self._out_
- out_indices must be in the same order as stage_names, expect
- out_features and out_indices should have the same length if
AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14).
Data as JSON: /api/errors/56ebb130c8ccaf43.
Report an issue: GitHub.