Buckets:

hf-doc-build/doc-dev / transformers /pr_37396 /en /internal /model_debugging_utils.html
rtrm's picture
download
raw
50.5 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Model debugging toolboxes&quot;,&quot;local&quot;:&quot;model-debugging-toolboxes&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model addition debuggers&quot;,&quot;local&quot;:&quot;model-addition-debuggers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model addition debugger - context manager for model adders&quot;,&quot;local&quot;:&quot;model-addition-debugger---context-manager-for-model-adders&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Rationale&quot;,&quot;local&quot;:&quot;rationale&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Usage&quot;,&quot;local&quot;:&quot;usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Reading results&quot;,&quot;local&quot;:&quot;reading-results&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Comparing between implementations&quot;,&quot;local&quot;:&quot;comparing-between-implementations&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Limitations and scope&quot;,&quot;local&quot;:&quot;transformers.model_addition_debugger_context&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}">
<link href="/docs/transformers/pr_37396/en/_app/immutable/assets/0.e3b0c442.css" rel="modulepreload">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/entry/start.3e00b6da.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/scheduler.18a86fab.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/singletons.240f24f3.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/index.40ab8126.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/paths.2edea311.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/entry/app.e7a7eda8.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/index.98837b22.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/nodes/0.78534817.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/each.e59479a4.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/nodes/39.e82f64ab.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/Docstring.6758ea23.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/globals.7f7f1b26.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/Heading.7e7e0c5b.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/CodeBlock.8d0c2e8a.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/ExampleCodeBlock.0c42463f.js">
<link rel="modulepreload" href="/docs/transformers/pr_37396/en/_app/immutable/chunks/index.f01015d9.js"><!-- HEAD_svelte-u9bgzb_START --><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Model debugging toolboxes&quot;,&quot;local&quot;:&quot;model-debugging-toolboxes&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model addition debuggers&quot;,&quot;local&quot;:&quot;model-addition-debuggers&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Model addition debugger - context manager for model adders&quot;,&quot;local&quot;:&quot;model-addition-debugger---context-manager-for-model-adders&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Rationale&quot;,&quot;local&quot;:&quot;rationale&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Usage&quot;,&quot;local&quot;:&quot;usage&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Reading results&quot;,&quot;local&quot;:&quot;reading-results&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Comparing between implementations&quot;,&quot;local&quot;:&quot;comparing-between-implementations&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Limitations and scope&quot;,&quot;local&quot;:&quot;transformers.model_addition_debugger_context&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;:1}"><!-- HEAD_svelte-u9bgzb_END --> <p></p> <h1 class="relative group"><a id="model-debugging-toolboxes" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-debugging-toolboxes"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model debugging toolboxes</span></h1> <p data-svelte-h="svelte-4sd845">This page lists all the debugging and model adding tools used by the library, as well as the utility functions it provides for it.</p> <p data-svelte-h="svelte-x293w1">Most of those are only useful if you are adding new models in the library.</p> <h2 class="relative group"><a id="model-addition-debuggers" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-addition-debuggers"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model addition debuggers</span></h2> <h3 class="relative group"><a id="model-addition-debugger---context-manager-for-model-adders" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-addition-debugger---context-manager-for-model-adders"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model addition debugger - context manager for model adders</span></h3> <p data-svelte-h="svelte-1d3pgsd">This context manager is a power user tool intended for model adders.
It tracks all forward calls within a model forward and logs a slice of each input and output on a nested Json.
To note, this context manager enforces <code>torch.no_grad()</code>.</p> <h3 class="relative group"><a id="rationale" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#rationale"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Rationale</span></h3> <p data-svelte-h="svelte-fewkz6">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.</p> <h3 class="relative group"><a id="usage" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#usage"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Usage</span></h3> <p data-svelte-h="svelte-jrktnj">Add this context manager as follows to debug a model:</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> LlavaProcessor, LlavaForConditionalGeneration
<span class="hljs-keyword">from</span> transformers.model_debugging_utils <span class="hljs-keyword">import</span> model_addition_debugger_context
torch.random.manual_seed(<span class="hljs-number">673</span>)
<span class="hljs-comment"># load pretrained model and processor</span>
model_id = <span class="hljs-string">&quot;llava-hf/llava-1.5-7b-hf&quot;</span>
processor = LlavaProcessor.from_pretrained(model_id)
model = LlavaForConditionalGeneration.from_pretrained(model_id, low_cpu_mem_usage=<span class="hljs-literal">True</span>)
<span class="hljs-comment"># create random image input</span>
random_image = Image.fromarray(torch.randint(<span class="hljs-number">0</span>, <span class="hljs-number">256</span>, (<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>), dtype=torch.uint8).numpy())
<span class="hljs-comment"># prompt</span>
prompt = <span class="hljs-string">&quot;&lt;image&gt;Describe this image.&quot;</span>
<span class="hljs-comment"># process inputs</span>
inputs = processor(text=prompt, images=random_image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
<span class="hljs-comment"># call forward method (not .generate!)</span>
<span class="hljs-keyword">with</span> model_addition_debugger_context(
model,
debug_path=<span class="hljs-string">&quot;optional_path_to_your_directory&quot;</span>,
do_prune_layers=<span class="hljs-literal">False</span> <span class="hljs-comment"># This will output ALL the layers of a model.</span>
):
output = model.forward(**inputs)
<!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="reading-results" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#reading-results"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Reading results</span></h3> <p data-svelte-h="svelte-1p13r1j">The debugger generates two files from the forward call, both with the same base name,
but ending either with <code>_SUMMARY.json</code> or with <code>_FULL_TENSORS.json</code>.</p> <p data-svelte-h="svelte-a5x8y4">The first one will contain a summary of each module’s <em>input</em> and <em>output</em> tensor values and shapes.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;module_path&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;MolmoForConditionalGeneration&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;inputs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;args&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span><span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;kwargs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;input_ids&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 589])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.int64&quot;</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;attention_mask&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 589])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.int64&quot;</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;pixel_values&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 5, 576, 588])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.float32&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;mean&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-8.9514e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;std&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(9.2586e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;min&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-1.7923e+00, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;max&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(1.8899e+00, device=&#x27;cuda:0&#x27;)&quot;</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;module_path&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;MolmoForConditionalGeneration.language_model.model.embed_tokens&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;inputs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;args&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 589])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.int64&quot;</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;outputs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 589, 3584])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.float32&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;mean&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(6.5460e-06, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;std&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(2.3807e-02, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;min&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-3.3398e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;max&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(3.9453e-01, device=&#x27;cuda:0&#x27;)&quot;</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;module_path&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;MolmoForConditionalGeneration.vision_tower&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;inputs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;args&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([5, 1, 576, 588])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.float32&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;mean&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-8.9514e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;std&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(9.2586e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;min&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-1.7923e+00, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;max&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(1.8899e+00, device=&#x27;cuda:0&#x27;)&quot;</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;kwargs&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;output_hidden_states&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;True&quot;</span>
<span class="hljs-punctuation">}</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;children&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-punctuation">{</span> ... and so on<!-- HTML_TAG_END --></pre></div> <p data-svelte-h="svelte-1bk1mhr">The <code>_FULL_TENSORS.json</code> file will display a full view of all tensors, which is useful
for comparing two files.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --> <span class="hljs-attr">&quot;pixel_values&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">{</span>
<span class="hljs-attr">&quot;shape&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.Size([1, 5, 576, 588])&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;dtype&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;torch.float32&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;value&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-punctuation">[</span>
<span class="hljs-string">&quot;tensor([[[[-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; ...,&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00]],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [[-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; ...,&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.4857e+00, -1.4820e+00, -1.2100e+00, ..., -6.0979e-01, -5.9650e-01, -3.8527e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.6755e+00, -1.7221e+00, -1.4518e+00, ..., -7.5577e-01, -7.4658e-01, -5.5592e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-7.9957e-01, -8.2162e-01, -5.7014e-01, ..., -1.3689e+00, -1.3169e+00, -1.0678e+00]],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [[-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; ...,&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-3.0322e-01, -5.0645e-01, -5.8436e-01, ..., -6.2439e-01, -7.9160e-01, -8.1188e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-4.4921e-01, -6.5653e-01, -7.2656e-01, ..., -3.4702e-01, -5.2146e-01, -5.1326e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-3.4702e-01, -5.3647e-01, -5.4170e-01, ..., -1.0915e+00, -1.1968e+00, -1.0252e+00]],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [[-1.1207e+00, -1.2718e+00, -1.0678e+00, ..., 1.2013e-01, -1.3126e-01, -1.7197e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-6.9738e-01, -9.1166e-01, -8.5454e-01, ..., -5.5050e-02, -2.8134e-01, -4.2793e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-3.4702e-01, -5.5148e-01, -5.8436e-01, ..., 1.9312e-01, -8.6235e-02, -2.1463e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; ...,&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00]],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot;&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [[-1.0039e+00, -9.5669e-01, -6.5546e-01, ..., -1.4711e+00, -1.4219e+00, -1.1389e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.0039e+00, -9.5669e-01, -6.5546e-01, ..., -1.7193e+00, -1.6771e+00, -1.4091e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.6317e+00, -1.6020e+00, -1.2669e+00, ..., -1.2667e+00, -1.2268e+00, -8.9720e-01],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; ...,&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00],&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-string">&quot; [-1.7923e+00, -1.7521e+00, -1.4802e+00, ..., -1.7923e+00, -1.7521e+00, -1.4802e+00]]]], device=&#x27;cuda:0&#x27;)&quot;</span>
<span class="hljs-punctuation">]</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;mean&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-8.9514e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;std&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(9.2586e-01, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;min&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(-1.7923e+00, device=&#x27;cuda:0&#x27;)&quot;</span><span class="hljs-punctuation">,</span>
<span class="hljs-attr">&quot;max&quot;</span><span class="hljs-punctuation">:</span> <span class="hljs-string">&quot;tensor(1.8899e+00, device=&#x27;cuda:0&#x27;)&quot;</span>
<span class="hljs-punctuation">}</span><span class="hljs-punctuation">,</span><!-- HTML_TAG_END --></pre></div> <h3 class="relative group"><a id="comparing-between-implementations" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#comparing-between-implementations"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Comparing between implementations</span></h3> <p data-svelte-h="svelte-xwf6bx">Once the forward passes of two models have been traced by the debugger, one can compare the <code>json</code> output files. See below: we can see slight differences between these two implementations’ key projection layer. Inputs are mostly identical, but not quite. Looking through the file differences makes it easier to pinpoint which layer is wrong.</p> <p data-svelte-h="svelte-9wdzxi"><img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/files_difference_debugging.png" alt="download-icon"></p> <h3 class="relative group"><a id="transformers.model_addition_debugger_context" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.model_addition_debugger_context"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Limitations and scope</span></h3> <p data-svelte-h="svelte-1ug7o4n">This feature will only work for torch-based models, and would require more work and case-by-case approach for say <code>jax</code>-based models that are usually compiled. Models relying heavily on external kernel calls may work, but trace will probably miss some things. Regardless, any python implementation that aims at mimicking another implementation can be traced once instead of reran N times with breakpoints.</p> <p data-svelte-h="svelte-1f729uy">If you pass <code>do_prune_layers=False</code> to your model debugger, ALL the layers will be outputted to <code>json</code>. Else, only the first and last layer will be shown. This is useful when some layers (typically cross-attention) appear only after N layers.</p> <div class="docstring border-l-2 border-t-2 pl-4 pt-3.5 border-gray-100 rounded-tl-xl mb-6 mt-8"> <div><span class="group flex space-x-1.5 items-center text-gray-800 bg-gradient-to-r rounded-tr-lg -mt-4 -ml-4 pt-3 px-2.5" id="transformers.model_addition_debugger_context"><!-- HTML_TAG_START --><h4 class="!m-0"><span class="flex-1 rounded-xl py-0.5 break-all bg-gradient-to-r from-blue-50/60 to-white dark:from-gray-900 dark:to-gray-950 text-blue-700 dark:text-blue-300 font-medium px-2"><svg width="1em" height="1em" viewBox="0 0 32 33" class="mr-1 inline-block -mt-0.5" xmlns="http://www.w3.org/2000/svg"><path d="M5.80566 18.3545C4.90766 17.4565 4.90766 16.0005 5.80566 15.1025L14.3768 6.53142C15.2748 5.63342 16.7307 5.63342 17.6287 6.53142L26.1999 15.1025C27.0979 16.0005 27.0979 17.4565 26.1999 18.3545L17.6287 26.9256C16.7307 27.8236 15.2748 27.8236 14.3768 26.9256L5.80566 18.3545Z" fill="currentColor" fill-opacity="0.25"/><path fill-rule="evenodd" clip-rule="evenodd" d="M16.4801 13.9619C16.4801 12.9761 16.7467 12.5436 16.9443 12.3296C17.1764 12.078 17.5731 11.8517 18.2275 11.707C18.8821 11.5623 19.638 11.5342 20.4038 11.5582C20.7804 11.57 21.1341 11.5932 21.4719 11.6156L21.5263 11.6193C21.8195 11.6389 22.1626 11.6618 22.4429 11.6618V7.40825C22.3209 7.40825 22.1219 7.39596 21.7544 7.37149C21.4202 7.34925 20.9976 7.32115 20.5371 7.30672C19.6286 7.27824 18.4672 7.29779 17.3093 7.55377C16.1512 7.8098 14.8404 8.33724 13.8181 9.4452C12.7612 10.5907 12.2266 12.1236 12.2266 13.9619V15.0127H10.6836V19.2662H12.2266V26.6332H16.4801V19.2662H20.3394V15.0127H16.4801V13.9619Z" fill="currentColor"/></svg>transformers.model_addition_debugger_context</span></h4><!-- HTML_TAG_END --> <a id="transformers.model_addition_debugger_context" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.model_addition_debugger_context"><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></a> <a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/vr_37396/src/transformers/model_debugging_utils.py#L336" target="_blank"><span data-svelte-h="svelte-1kd6by1">&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline" data-svelte-h="svelte-122apf4">source</span> <span data-svelte-h="svelte-x0xyl0">&gt;</span></a></span> <p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span data-svelte-h="svelte-8mvn6a">(</span> <span class="comma cursor-default"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">model<span class="opacity-60"></span></span> </span><span class="comma cursor-default"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">debug_path<span class="opacity-60">: typing.Optional[str] = None</span></span> </span><span class="comma cursor-default"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">do_prune_layers<span class="opacity-60">: typing.Optional[bool] = True</span></span> </span> <span data-svelte-h="svelte-1jq0pl7">)</span> </p> <div class="!mb-10 relative docstring-details "> </div></div> <h1 class="relative group"><a id="model-addition-debugger---context-manager-for-model-adders" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-addition-debugger---context-manager-for-model-adders"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Model addition debugger - context manager for model adders</span></h1> <p data-svelte-h="svelte-1d3pgsd">This context manager is a power user tool intended for model adders.
It tracks all forward calls within a model forward and logs a slice of each input and output on a nested Json.
To note, this context manager enforces <code>torch.no_grad()</code>.</p> <h2 class="relative group"><a id="usage" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#usage"><span><svg class="" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <span>Usage</span></h2> <p data-svelte-h="svelte-chd0p3">add the context manager to a model to debug</p> <div class="relative group rounded-md"><a id="transformers.model_addition_debugger_context.example" class="header-link block pr-0.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#transformers.model_addition_debugger_context.example"><span><svg class="text-smd" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 256"><path d="M167.594 88.393a8.001 8.001 0 0 1 0 11.314l-67.882 67.882a8 8 0 1 1-11.314-11.315l67.882-67.881a8.003 8.003 0 0 1 11.314 0zm-28.287 84.86l-28.284 28.284a40 40 0 0 1-56.567-56.567l28.284-28.284a8 8 0 0 0-11.315-11.315l-28.284 28.284a56 56 0 0 0 79.196 79.197l28.285-28.285a8 8 0 1 0-11.315-11.314zM212.852 43.14a56.002 56.002 0 0 0-79.196 0l-28.284 28.284a8 8 0 1 0 11.314 11.314l28.284-28.284a40 40 0 0 1 56.568 56.567l-28.285 28.285a8 8 0 0 0 11.315 11.314l28.284-28.284a56.065 56.065 0 0 0 0-79.196z" fill="currentColor"></path></svg></span></a> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg class="" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg> <div class="absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0"><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent; "></div> Copied</div></button></div> <pre class=""><!-- HTML_TAG_START --><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> PIL <span class="hljs-keyword">import</span> Image
<span class="hljs-keyword">import</span> requests
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> LlavaProcessor, LlavaForConditionalGeneration
<span class="hljs-keyword">from</span> transformers.model_debugging_utils <span class="hljs-keyword">import</span> model_addition_debugger_context
torch.random.manual_seed(<span class="hljs-number">673</span>)
<span class="hljs-comment"># load pretrained model and processor</span>
model_id = <span class="hljs-string">&quot;llava-hf/llava-1.5-7b-hf&quot;</span>
processor = LlavaProcessor.from_pretrained(model_id)
model = LlavaForConditionalGeneration.from_pretrained(model_id, low_cpu_mem_usage=<span class="hljs-literal">True</span>)
<span class="hljs-comment"># create random image input</span>
random_image = Image.fromarray(torch.randint(<span class="hljs-number">0</span>, <span class="hljs-number">256</span>, (<span class="hljs-number">224</span>, <span class="hljs-number">224</span>, <span class="hljs-number">3</span>), dtype=torch.uint8).numpy())
<span class="hljs-comment"># prompt</span>
prompt = <span class="hljs-string">&quot;&lt;image&gt;Describe this image.&quot;</span>
<span class="hljs-comment"># process inputs</span>
inputs = processor(text=prompt, images=random_image, return_tensors=<span class="hljs-string">&quot;pt&quot;</span>)
<span class="hljs-comment"># call forward method (not .generate!)</span>
<span class="hljs-keyword">with</span> model_addition_debugger_context(model, debug_path=<span class="hljs-string">&quot;Your_debug_path&quot;</span>, do_prune_layers=<span class="hljs-literal">False</span>):
output = model.forward(**inputs)<!-- HTML_TAG_END --></pre></div></div></div> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/internal/model_debugging_utils.md" target="_blank"><span data-svelte-h="svelte-1kd6by1">&lt;</span> <span data-svelte-h="svelte-x0xyl0">&gt;</span> <span data-svelte-h="svelte-1dajgef"><span class="underline ml-1.5">Update</span> on GitHub</span></a> <p></p>
<script>
{
__sveltekit_1s09dg7 = {
assets: "/docs/transformers/pr_37396/en",
base: "/docs/transformers/pr_37396/en",
env: {}
};
const element = document.currentScript.parentElement;
const data = [null,null];
Promise.all([
import("/docs/transformers/pr_37396/en/_app/immutable/entry/start.3e00b6da.js"),
import("/docs/transformers/pr_37396/en/_app/immutable/entry/app.e7a7eda8.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 39],
data,
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
50.5 kB
·
Xet hash:
be174ea79eb636c96effd9fe059ac10ad8b23004f1a559494f9cc103d8c10c6e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.