Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/transformers/utils/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,6 +1010,21 @@ def wrapper(self, *args, **kwargs):
collected_outputs = defaultdict(tuple)
monkey_patched_layers = []

# Check attention implementation is properly set for capturing attention outputs
if recordable_keys.get("output_attentions", False):
supported_attn = ["eager", "eager_paged", "flex_attention"]

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.

Flex attention returns the lse (like flash attention) - not really the same as what you would get with eager

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.

yeah, I noticed that, but that would be captured as attentions and returned in ModelOutput, if I'm no missing anything. Do you have an idea of a better warning?

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.

Don't have an immediate solution, but 2 suggestions:

  • Properly change what output_attentions does in the docs and enable outputting the lse (would include flash attn then for example)
  • Another separate kwarg/attr output_lse which works for flash, flex etc and not on eager for example.

Currently, we have a weird situation with flex 😅 doesnt need immediate change but maybe put it up for discussion internally?

config_attn = getattr(self.config, "_attn_implementation", None)
sub_configs = [getattr(self.config, key, None) for key in self.config.sub_configs]
sub_configs_attn = [
getattr(config, "_attn_implementation", None) for config in sub_configs if config is not None
]
if config_attn not in supported_attn or any(attn not in supported_attn for attn in sub_configs_attn):
warnings.warn(
f"`output_attentions=True` is not supported with `attn_implementation` other than {supported_attn}. "
"Please use `model.set_attn_implementation('eager')` to enable capturing attention outputs.",
UserWarning,
)

def make_capture_wrapper(module, orig_forward, key, index):
@wraps(orig_forward)
def wrapped_forward(*args, **kwargs):
Expand Down