Skip to content

Commit 1d3f35f

Browse files
authored
Add model visual debugger (#36798)
* draft of model tracer visualiser * add context manager in addition to decorator * add debug utils to init * move model debugging utils to dedicated file * add documentation * protect some imports * format * move and protect imports * format * doc: improve errors in case of broken dummy imports. * format * use automatic torch backend * update doc * fix backend * (TEMP) move to dummies while backend wait * update documentation * doc
1 parent 6515c25 commit 1d3f35f

File tree

5 files changed

+432
-2
lines changed

5 files changed

+432
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<!--Copyright 2025 The HuggingFace Team. All rights reserved.
2+
3+
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
9+
an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
10+
specific language governing permissions and limitations under the License.
11+
12+
⚠️ Note that this file is in Markdown but contain specific syntax for our doc-builder (similar to MDX) that may not be
13+
rendered properly in your Markdown viewer.
14+
15+
-->
16+
17+
# Model debugging toolboxes
18+
19+
This page lists all the debugging and model adding tools used by the library, as well as the utility functions it provides for it.
20+
21+
Most of those are only useful if you are adding new models in the library.
22+
23+
24+
## Model addition debuggers
25+
26+
27+
### Model addition debugger - context manager for model adders
28+
29+
This context manager is a power user tool intended for model adders.
30+
It tracks all forward calls within a model forward and logs a slice of each input and output on a nested Json.
31+
To note, this context manager enforces `torch.inference_mode()`.
32+
33+
### Rationale
34+
35+
Because when porting models to transformers, even from python to python, model adders often have to do a lot of manual operations, involving saving and loading tensors, comparing dtypes, etc. This small tool can hopefully shave off some time.
36+
37+
### Usage
38+
39+
Add this context manager as follows to debug a model:
40+
41+
```python
42+
import torch
43+
from PIL import Image
44+
import requests
45+
from transformers import LlavaProcessor, LlavaForConditionalGeneration
46+
torch.random.manual_seed(673)
47+
48+
# load pretrained model and processor
49+
model_id = "llava-hf/llava-1.5-7b-hf"
50+
processor = LlavaProcessor.from_pretrained(model_id)
51+
model = LlavaForConditionalGeneration.from_pretrained(model_id, low_cpu_mem_usage=True)
52+
53+
# create random image input
54+
random_image = Image.fromarray(torch.randint(0, 256, (224, 224, 3), dtype=torch.uint8).numpy())
55+
56+
# prompt
57+
prompt = "<image>Describe this image."
58+
59+
# process inputs
60+
inputs = processor(text=prompt, images=random_image, return_tensors="pt")
61+
62+
# call forward method (not .generate!)
63+
with model_addition_debugger_context(model, "optional_path_to_your_output_file.json"):
64+
output = model.forward(**inputs)
65+
66+
```
67+
68+
69+
[[autodoc]] utils.model_addition_debugger
70+
71+
[[autodoc]] utils.model_addition_debugger_context

src/transformers/__init__.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,6 +1376,10 @@
13761376

13771377
_import_structure["utils.dummy_pt_objects"] = [name for name in dir(dummy_pt_objects) if not name.startswith("_")]
13781378
else:
1379+
_import_structure["model_debugging_utils"] = [
1380+
"model_addition_debugger",
1381+
"model_addition_debugger_context",
1382+
]
13791383
_import_structure["activations"] = []
13801384
_import_structure["cache_utils"] = [
13811385
"Cache",
@@ -6605,6 +6609,7 @@
66056609
except OptionalDependencyNotAvailable:
66066610
from .utils.dummy_pt_objects import *
66076611
else:
6612+
# Debugging
66086613
from .cache_utils import (
66096614
Cache,
66106615
CacheConfig,
@@ -6690,6 +6695,10 @@
66906695
TorchExportableModuleWithStaticCache,
66916696
convert_and_export_with_cache,
66926697
)
6698+
from .model_debugging_utils import (
6699+
model_addition_debugger,
6700+
model_addition_debugger_context,
6701+
)
66936702
from .modeling_rope_utils import ROPE_INIT_FUNCTIONS
66946703
from .modeling_utils import PreTrainedModel
66956704
from .models.albert import (

0 commit comments

Comments
 (0)