Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attention visualization tool #36630

Merged
merged 30 commits into from
Mar 19, 2025
Merged

Add attention visualization tool #36630

merged 30 commits into from
Mar 19, 2025

Conversation

ArthurZucker
Copy link
Collaborator

@ArthurZucker ArthurZucker commented Mar 10, 2025

What does this PR do?

TODOS

  • Add some tests
  • make it work on all models

cc @yonigozlan and @zucchini-nlp @molbap

This will be available as:

from transformers.utils.attention_visualizer import AttentionMaskVisualizer
visualizer = AttentionMaskVisualizer("meta-llama/Llama-3.2-3B-Instruct")
visualizer("A normal attention mask")

visualizer = AttentionMaskVisualizer("mistralai/Mistral-Small-24B-Instruct-2501")
visualizer("A normal attention mask with a long text to see how it is displayed, and if it is displayed correctly")

visualizer = AttentionMaskVisualizer("google/paligemma2-3b-mix-224")
visualizer("<img> You are an assistant.", suffix = "What is on the image?")

visualizer = AttentionMaskVisualizer("google/gemma-2b")
visualizer("You are an assistant. Make sure you print me") # we should have slidiing on non sliding side by side

visualizer = AttentionMaskVisualizer("google/gemma-3-27b-it")
visualizer("<img>You are an assistant. Make sure you print me") # we should have slidiing on non sliding side by side

image

@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@jmayank23
Copy link

Hi, I was trying to visualize the attention mask for Gemma 3 but I get

AttributeError: 'Gemma3ForConditionalGeneration' object has no attribute 'visualize_attention_mask'

For reference, sharing relevant excerpts of the code:

from transformers import AutoProcessor, Gemma3ForConditionalGeneration, AutoTokenizer
from PIL import Image
import json
import torch

model_id = "google/gemma-3-27b-it"

model = Gemma3ForConditionalGeneration.from_pretrained(
    model_id, device_map="auto"
).eval()

processor = AutoProcessor.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
.
.
.
inputs = processor.apply_chat_template(
        messages,
        add_generation_prompt=True,
        tokenize=True,
        return_dict=True,
        return_tensors="pt"
    ).to(model.device, dtype=torch.bfloat16)

model.visualize_attention_mask(tokenizer, inputs)

@ArthurZucker
Copy link
Collaborator Author

Hey ! Will work a bit to make it more easy to use! THanks 🤗

@ArthurZucker ArthurZucker marked this pull request as ready for review March 18, 2025 15:49
@github-actions github-actions bot requested a review from Rocketknight1 March 18, 2025 15:50
@ArthurZucker ArthurZucker requested a review from molbap March 18, 2025 15:51
@molbap
Copy link
Contributor

molbap commented Mar 18, 2025

Doing a first review, so happy about this

Copy link
Contributor

@molbap molbap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Amazing

@ArthurZucker ArthurZucker merged commit fef8b7f into main Mar 19, 2025
19 of 24 checks passed
@ArthurZucker ArthurZucker deleted the attention-utilities branch March 19, 2025 12:58
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.

4 participants