|
| 1 | +# Copyright 2025 The HuggingFace Inc. team. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import builtins |
| 16 | +import io |
| 17 | +import re |
| 18 | +import unittest |
| 19 | + |
| 20 | +from transformers.testing_utils import require_read_token, require_torch |
| 21 | +from transformers.utils.attention_visualizer import AttentionMaskVisualizer |
| 22 | + |
| 23 | + |
| 24 | +ANSI_RE = re.compile(r"\x1b\[[0-9;]*m") |
| 25 | + |
| 26 | + |
| 27 | +def _normalize(s: str) -> str: |
| 28 | + # drop ANSI (colors may be disabled on CI), normalize line endings, |
| 29 | + # and strip trailing spaces without touching alignment inside lines |
| 30 | + s = ANSI_RE.sub("", s) |
| 31 | + s = s.replace("\r\n", "\n").replace("\r", "\n") |
| 32 | + return "\n".join(line.rstrip() for line in s.split("\n")).strip() |
| 33 | + |
| 34 | + |
| 35 | +@require_torch |
| 36 | +class AttentionMaskVisualizerTester(unittest.TestCase): |
| 37 | + """Test suite for AttentionMaskVisualizer""" |
| 38 | + |
| 39 | + @require_read_token |
| 40 | + def test_paligemma_multimodal_visualization(self): |
| 41 | + """Test AttentionMaskVisualizer with PaliGemma multimodal model""" |
| 42 | + model_name = "hf-internal-testing/namespace_google_repo_name_paligemma-3b-pt-224" |
| 43 | + input_text = "<img> What is in this image?" |
| 44 | + |
| 45 | + buf = io.StringIO() |
| 46 | + orig_print = builtins.print |
| 47 | + |
| 48 | + def _print(*args, **kwargs): |
| 49 | + kwargs.setdefault("file", buf) |
| 50 | + orig_print(*args, **kwargs) |
| 51 | + |
| 52 | + try: |
| 53 | + builtins.print = _print |
| 54 | + visualizer = AttentionMaskVisualizer(model_name) |
| 55 | + visualizer(input_text) |
| 56 | + finally: |
| 57 | + builtins.print = orig_print |
| 58 | + output = buf.getvalue() |
| 59 | + |
| 60 | + expected_output = """ |
| 61 | +########################################################################################################################################################################################################################################## |
| 62 | +## Attention visualization for \033[1mpaligemma:hf-internal-testing/namespace_google_repo_name_paligemma-3b-pt-224\033[0m PaliGemmaModel ## |
| 63 | +########################################################################################################################################################################################################################################## |
| 64 | + \033[92m■\033[0m: i == j (diagonal) \033[93m■\033[0m: token_type_ids |
| 65 | + Attention Matrix |
| 66 | +
|
| 67 | +
|
| 68 | +\033[93m'<image>'\033[0m: 0 \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 69 | +\033[93m'<image>'\033[0m: 1 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 70 | +\033[93m'<image>'\033[0m: 2 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 71 | +\033[93m'<image>'\033[0m: 3 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 72 | +\033[93m'<image>'\033[0m: 4 \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m \033[93m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 73 | +'<bos>' : 5 ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 74 | +'▁What' : 6 ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 75 | +'▁is' : 7 ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 76 | +'▁in' : 8 ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 77 | +'▁this' : 9 ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ | |
| 78 | +'▁image' : 10 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ | |
| 79 | +'?' : 11 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ | |
| 80 | +'\\n' : 12 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ | |
| 81 | +'<eos>' : 13 ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m | |
| 82 | +########################################################################################################################################################################################################################################## |
| 83 | +""" # noqa |
| 84 | + |
| 85 | + self.assertEqual(_normalize(output), _normalize(expected_output)) |
| 86 | + |
| 87 | + @require_read_token |
| 88 | + def test_llama_text_only_visualization(self): |
| 89 | + """Test AttentionMaskVisualizer with Llama text-only model""" |
| 90 | + model_name = "hf-internal-testing/namespace_meta-llama_repo_name_Llama-2-7b-hf" |
| 91 | + input_text = "Plants create energy through a process known as" |
| 92 | + |
| 93 | + buf = io.StringIO() |
| 94 | + orig_print = builtins.print |
| 95 | + |
| 96 | + def _print(*args, **kwargs): |
| 97 | + kwargs.setdefault("file", buf) |
| 98 | + orig_print(*args, **kwargs) |
| 99 | + |
| 100 | + try: |
| 101 | + builtins.print = _print |
| 102 | + visualizer = AttentionMaskVisualizer(model_name) |
| 103 | + visualizer(input_text) |
| 104 | + finally: |
| 105 | + builtins.print = orig_print |
| 106 | + output = buf.getvalue() |
| 107 | + |
| 108 | + expected_output = """ |
| 109 | +########################################################################################################################################################################################################## |
| 110 | +## Attention visualization for \033[1mllama:hf-internal-testing/namespace_meta-llama_repo_name_Llama-2-7b-hf\033[0m LlamaModel ## |
| 111 | +########################################################################################################################################################################################################## |
| 112 | + \033[92m■\033[0m: i == j (diagonal) \033[93m■\033[0m: token_type_ids |
| 113 | + Attention Matrix |
| 114 | +
|
| 115 | +'▁Pl' : 0 \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 116 | +'ants' : 1 ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 117 | +'▁create' : 2 ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 118 | +'▁energy' : 3 ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ ⬚ | |
| 119 | +'▁through': 4 ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ ⬚ | |
| 120 | +'▁a' : 5 ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ ⬚ | |
| 121 | +'▁process': 6 ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ ⬚ | |
| 122 | +'▁known' : 7 ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m ⬚ | |
| 123 | +'▁as' : 8 ■ ■ ■ ■ ■ ■ ■ ■ \033[92m■\033[0m | |
| 124 | +########################################################################################################################################################################################################## |
| 125 | +""" # noqa |
| 126 | + |
| 127 | + self.assertEqual(_normalize(output), _normalize(expected_output)) |
0 commit comments