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 subconfigs

View on GitHub (pinned to a597f97485)

Solutions

  1. Set attn_implementation='eager' (before or after enabling output_attentions) when you need attention weights
  2. Or set output_attentions first while _attn_implementation is None so eager is auto-selected
  3. 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

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


AI-assisted analysis of huggingface/transformers@a597f97485 (2026-08-14). Data as JSON: /api/errors/56ebb130c8ccaf43. Report an issue: GitHub.