Buckets:

hf-doc-build/doc-dev / transformers /pr_51549 /en /exporters_extend.html
download
raw
35.1 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Extending the exporters&quot;,&quot;local&quot;:&quot;extending-the-exporters&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Patches and fixes&quot;,&quot;local&quot;:&quot;patches-and-fixes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Add a patch&quot;,&quot;local&quot;:&quot;add-a-patch&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Stage reference&quot;,&quot;local&quot;:&quot;stage-reference&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DynamoExporter&quot;,&quot;local&quot;:&quot;dynamoexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;OnnxExporter&quot;,&quot;local&quot;:&quot;onnxexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;ExecutorchExporter&quot;,&quot;local&quot;:&quot;executorchexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Add a backend&quot;,&quot;local&quot;:&quot;add-a-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Known upstream workarounds&quot;,&quot;local&quot;:&quot;known-upstream-workarounds&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/en/_app/immutable/entry/start.DVtWCojw.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/U9oJsqGv.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CfyCAb-k.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/entry/app.BfqWnvlk.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DKAe5MDQ.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/D3TbGwj-.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/0.BtskVwaq.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/CCOHVGcd.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/nodes/3.CnWIg4MF.js" rel="modulepreload">
<link href="/docs/transformers/main/en/_app/immutable/chunks/BgU8G3tw.js" rel="modulepreload">
<!--1fld0js--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Extending the exporters&quot;,&quot;local&quot;:&quot;extending-the-exporters&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Patches and fixes&quot;,&quot;local&quot;:&quot;patches-and-fixes&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Add a patch&quot;,&quot;local&quot;:&quot;add-a-patch&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Stage reference&quot;,&quot;local&quot;:&quot;stage-reference&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;DynamoExporter&quot;,&quot;local&quot;:&quot;dynamoexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;OnnxExporter&quot;,&quot;local&quot;:&quot;onnxexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;ExecutorchExporter&quot;,&quot;local&quot;:&quot;executorchexporter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Add a backend&quot;,&quot;local&quot;:&quot;add-a-backend&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Known upstream workarounds&quot;,&quot;local&quot;:&quot;known-upstream-workarounds&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" 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><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="extending-the-exporters" 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="#extending-the-exporters"><span><svg 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><!---->Extending the exporters<!----></span></h1><!--]--><!----> <p><code>torch.export</code> traces the model into a graph, later stages transform
that graph, and a final stage lowers or emits it for the target runtime. Most models pass through
untouched. When there’s a PyTorch pattern the backend can’t handle, the exporter applies a
small workaround at that stage rather than editing the model.</p> <p>Add a workaround by writing one function and registering it with a decorator. Each workaround belongs at the lowest stage that can express it cleanly.</p> <!--[1--><h2 class="relative group"><a id="patches-and-fixes" 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="#patches-and-fixes"><span><svg 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><!---->Patches and fixes<!----></span></h2><!--]--><!----> <p>A workaround is either a patch or a fix. The two differ in whether they can be reverted.</p> <table><thead><tr><th></th><th>Patch</th><th>Fix</th></tr></thead><tbody><tr><td>What it does</td><td>Swaps out an attribute (a <code>torch</code> op, an ExecuTorch internal, or a model method) for the duration of the export</td><td>Rewrites the traced graph or program</td></tr><tr><td>Reverted</td><td>Yes, the original is restored afterward</td><td>No, it repairs the artifact before the next stage runs</td></tr><tr><td>Register with</td><td><code>@register_patch(backend, *paths)</code></td><td><code>@register_fx_node_fix(backend)</code> or <code>@register_fx_program_fix(backend)</code></td></tr></tbody></table> <p>Both live in a registry in <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/exporters/utils.py" rel="nofollow">exporters/utils.py</a>,
and the exporter installs everything registered for its backend at the right stage.</p> <p>Reach for a patch when the issue is a single backend’s lowering bug: a missing ONNX
translation, an ORT validation quirk, or an FX decomposition that emits a dead op. The workaround
stays in the exporter, and the modeling code stays clean.</p> <p>When the pattern blocks export across multiple backends, such as data-dependent loops, stateful
caches outside <code>Cache</code>, or hand-written split-loop attention, fix the model instead. Fixing it
once in the model helps every exporter.</p> <!--[1--><h2 class="relative group"><a id="add-a-patch" 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="#add-a-patch"><span><svg 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><!---->Add a patch<!----></span></h2><!--]--><!----> <p>Suppose a model method does something <code>torch.export</code> can’t trace. NLLB-MoE’s <code>NllbMoeTop2Router._cast_classifier</code> casts the classifier weights to another dtype,
which isn’t traceable. Replace it with a no-op for the duration of the export.</p> <p>Write a factory that takes the original method and returns its replacement, then register the
factory against the method’s dotted path:</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 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="language-python "><!----><span class="hljs-keyword">from</span> transformers.exporters.utils <span class="hljs-keyword">import</span> register_patch
<span class="hljs-meta">@register_patch(<span class="hljs-params"><span class="hljs-string">&quot;dynamo&quot;</span>, <span class="hljs-string">&quot;transformers.models.nllb_moe.modeling_nllb_moe.NllbMoeTop2Router._cast_classifier&quot;</span></span>)</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">_patch_classifier_cast</span>(<span class="hljs-params">_original</span>):
<span class="hljs-comment"># Replace the untraceable dtype cast with a no-op during export.</span>
<span class="hljs-keyword">return</span> <span class="hljs-keyword">lambda</span> <span class="hljs-variable language_">self</span>, *args, **kwargs: <span class="hljs-literal">None</span><!----></pre></div><!----> <p>The exporter swaps the method in before tracing and restores it afterward,
so the patch only affects export. A few variations:</p> <ul><li>Pass extra paths to share one factory across call sites, for example <code>@register_patch("dynamo", path_a, path_b)</code>.</li> <li>Patch a <code>torch</code> op by pointing the path at it, for example <code>@register_patch("onnx", "torch.where")</code>.
The factory receives the real op as its argument, so the replacement can call through to it.</li> <li>Write a fix instead of a patch when you need to rewrite the graph after tracing. The mechanism is
the same, a decorated function in the matching registry.</li></ul> <!--[1--><h2 class="relative group"><a id="stage-reference" 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="#stage-reference"><span><svg 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><!---->Stage reference<!----></span></h2><!--]--><!----> <p>Each exporter’s source labels its stages as <code># ── Stage N: … ──</code> comment blocks, so the file and
this reference line up. Look there for the exact ops and classes each stage handles.</p> <!--[2--><h3 class="relative group"><a id="dynamoexporter" 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="#dynamoexporter"><span><svg 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><!---->DynamoExporter<!----></span></h3><!--]--><!----> <p>The base exporter runs one patch stage and four helpers, in order, inside <code>DynamoExporter.export_program</code> (see <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/exporters/exporter_dynamo.py" rel="nofollow">exporter_dynamo.py</a>).</p> <ol><li>Forward-signature patch: gives <code>model.forward</code> a flat argument signature so <code>torch.export</code> doesn’t bundle inputs into one <code>**kwargs</code> tuple. This is internal and not an extension point.</li> <li>Model patches: swap untraceable model methods for export-safe equivalents during tracing. Extend
with <code>@register_patch("dynamo", ...)</code>.</li> <li>Pytree registration: register each <code>Cache</code> and <code>ModelOutput</code> so <code>torch.export</code> can flatten and
rebuild it (usually happens automatically). Add a branch to <code>_flatten_to_context</code> / <code>_unflatten_from_context</code> for a type the attribute walk can’t reach.</li> <li>Dynamic shapes: assign <code>Dim.AUTO</code> to every tensor and cache leaf when <code>dynamic=True</code>. Override
with <code>DynamoConfig.dynamic_shapes</code>.</li> <li>State cleanup: reset tensor attributes a model sets inside <code>forward</code> that <code>torch.export</code> leaves
as fake tensors. Extend by adding the attribute name to <code>_STATEFUL_CACHE_ATTRS</code>.</li></ol> <!--[2--><h3 class="relative group"><a id="onnxexporter" 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="#onnxexporter"><span><svg 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><!---->OnnxExporter<!----></span></h3><!--]--><!----> <p><code>OnnxExporter</code> adds five stages around <code>torch.onnx.export</code> (see <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/exporters/exporter_onnx.py" rel="nofollow">exporter_onnx.py</a>).
Grep the file for the full list of patches:</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 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="language-bash "><!---->grep -nE <span class="hljs-string">&quot;^def (_patch_|_fix_|_aten_)&quot;</span> src/transformers/exporters/exporter_onnx.py<!----></pre></div><!----> <ol><li>Torch patches: swap <code>torch</code> ops the ONNX exporter can’t translate as-is. Extend with <code>@register_patch("onnx", ...)</code>.</li> <li>ONNX patches: re-run the node fixes after <code>run_decompositions</code> so newly introduced shape-guard
nodes get repaired before lowering. Uses the same <code>@register_patch("onnx", ...)</code> registry.</li> <li>FX node fixes: rewrite graph nodes the ONNX exporter can’t lower, such as alias ops, in-place
views, and dead asserts. Extend with <code>@register_fx_node_fix("onnx")</code>.</li> <li>ONNX translations: supply a custom lowering for an aten op where the default is missing or buggy
(for example <code>aten.index_put</code> or <code>aten._grouped_mm</code>). Add an <code>_aten_*</code> function to <code>_ONNX_TRANSLATION_TABLE</code>.</li> <li>ONNX IR fixes: rewrite the finished ONNX program to work around ONNX Runtime bugs (for example
forcing <code>TopK(sorted=True)</code>). Add a <code>_fix_ir_*</code> function to <code>_IR_FIXES</code>.</li></ol> <!--[2--><h3 class="relative group"><a id="executorchexporter" 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="#executorchexporter"><span><svg 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><!---->ExecutorchExporter<!----></span></h3><!--]--><!----> <p><code>ExecutorchExporter</code> adds five stages around <code>to_edge_transform_and_lower</code> and <code>to_executorch</code>,
starting with backend preparation (see <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/exporters/exporter_executorch.py" rel="nofollow">exporter_executorch.py</a>).</p> <ol><li>Backend preparation: move the model to the target device and dtype and pick its partitioner
(<code>prepare_for_xnnpack</code>, <code>prepare_for_cuda</code>). Add a backend by registering <code>prepare_for_&lt;name></code> in <code>_BACKEND_PREPARE</code>.</li> <li>Torch patches: replace <code>torch</code> ops the ExecuTorch backends can’t accept, such as <code>split_copy</code>, <code>chunk</code>, and <code>topk(k>dim)</code>. Extend with <code>@register_patch("executorch", ...)</code>.</li> <li>ExecuTorch patches: swap ExecuTorch internals that crash on valid dynamic-shape graphs. Uses the
same <code>@register_patch("executorch", ...)</code> registry.</li> <li>FX program fixes: repair the exported program where the fix needs whole-program context, such as
widening range constraints or filling missing placeholder metadata. Extend with <code>@register_fx_program_fix("executorch")</code>.</li> <li>FX node fixes: rewrite individual nodes, such as mapping Python sym ops to <code>executorch_prim.*</code> or
rewriting <code>pow</code> as a <code>mul</code> chain. Extend with <code>@register_fx_node_fix("executorch")</code>.</li></ol> <!--[1--><h2 class="relative group"><a id="add-a-backend" 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="#add-a-backend"><span><svg 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><!---->Add a backend<!----></span></h2><!--]--><!----> <p>A backend is an <code>HfExporter</code> subclass, plus a config. <code>export_program</code> traces one graph and returns the
backend’s program with the metadata recorded while tracing it, <code>save_program</code> writes one program to disk, and <code>load_program</code> reads it back. <code>~HfExporter.export</code> and <code>~HfExporter.export_for_generation</code> build the <code>ExportedModel</code> on top of
them. Subclassing <code>DynamoExporter</code> reuses its trace and its metadata, so a backend only converts the <code>ExportedProgram</code>:</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 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="language-python "><!----><span class="hljs-keyword">from</span> dataclasses <span class="hljs-keyword">import</span> dataclass
<span class="hljs-keyword">from</span> transformers.exporters <span class="hljs-keyword">import</span> DynamoConfig, DynamoExporter, register_backend
<span class="hljs-meta">@dataclass</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyConfig</span>(<span class="hljs-title class_ inherited__">DynamoConfig</span>):
export_format: <span class="hljs-built_in">str</span> = <span class="hljs-string">&quot;my_format&quot;</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MyExporter</span>(<span class="hljs-title class_ inherited__">DynamoExporter</span>):
export_format = <span class="hljs-string">&quot;my_format&quot;</span>
config_class = MyConfig
program_suffix = <span class="hljs-string">&quot;.bin&quot;</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">export_program</span>(<span class="hljs-params">self, model, sample_inputs, config</span>):
exported_program, metadata = <span class="hljs-built_in">super</span>().export_program(model, sample_inputs, config)
<span class="hljs-keyword">return</span> convert(exported_program), metadata
<span class="hljs-meta"> @classmethod</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">save_program</span>(<span class="hljs-params">cls, program, path</span>):
program.save(path)
<span class="hljs-meta"> @classmethod</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">load_program</span>(<span class="hljs-params">cls, path</span>):
<span class="hljs-keyword">return</span> load(path)
register_backend(MyExporter)<!----></pre></div><!----> <p>To call its exports, register a <code>ModelRunner</code> with it: <code>register_backend(MyExporter, MyRunner)</code>.</p> <p>The auto classes then dispatch <code>"my_format"</code> to them, and <code>ExportedModel.from_pretrained</code> loads its saved exports:</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 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="language-python "><!----><span class="hljs-keyword">from</span> transformers.exporters <span class="hljs-keyword">import</span> AutoExportConfig, AutoHfExporter, ExportedModel
config = AutoExportConfig.from_dict({<span class="hljs-string">&quot;export_format&quot;</span>: <span class="hljs-string">&quot;my_format&quot;</span>, <span class="hljs-string">&quot;dynamic&quot;</span>: <span class="hljs-literal">True</span>})
exported = AutoHfExporter.from_config(config).export(model, inputs, config)
exported.save_pretrained(<span class="hljs-string">&quot;my-model-my_format&quot;</span>) <span class="hljs-comment"># model.bin, export.json, config.json</span>
exported.push_to_hub(<span class="hljs-string">&quot;my-org/my-model-my_format&quot;</span>)
loaded = ExportedModel.from_pretrained(<span class="hljs-string">&quot;my-org/my-model-my_format&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="known-upstream-workarounds" 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="#known-upstream-workarounds"><span><svg 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><!---->Known upstream workarounds<!----></span></h2><!--]--><!----> <p>A few model classes hit confirmed bugs in the <code>onnxscript</code> graph optimizer (constant folding crashing
on <code>SplitToSequence</code>, FPN initializers being dropped). <a href="https://github.com/huggingface/transformers/blob/main/tests/exporters/test_export.py" rel="nofollow">ONNX_DISABLE_OPTIMIZE</a> disables <code>onnxscript</code> optimization for those models. Each entry records the
upstream issue next to the model name. The list is expected to shrink as upstream bugs land, so a
new entry must reference a specific upstream bug rather than disable optimization arbitrarily.</p> <p><a href="https://github.com/huggingface/transformers/blob/main/tests/exporters/test_export.py" rel="nofollow">EXPORT_SKIPS</a>,
opts a handful of model classes out of the export sweep entirely when the model is
fundamentally non-exportable as-is (data-dependent control flow that can’t be vectorized, or modules
treated as forward arguments). Each entry carries a reason naming the model-side change needed. This
list is also expected to shrink, not grow.</p> <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/exporters_extend.md" target="_blank"><svg class="mr-1" 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="M31,16l-7,7l-1.41-1.41L28.17,16l-5.58-5.59L24,9l7,7z"></path><path d="M1,16l7-7l1.41,1.41L3.83,16l5.58,5.59L8,23l-7-7z"></path><path d="M12.419,25.484L17.639,6.552l1.932,0.518L14.351,26.002z"></path></svg><!----> <span><span class="underline">Update</span> on GitHub</span></a><!----> <p></p><!--]--><!----><!--]--><!--]--><!--]--> <!--[-1--><!--]--><!--]-->
<script>
{
__sveltekit_yo02r9 = {
base: "/docs/transformers/main/en",
assets: "/docs/transformers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/en/_app/immutable/entry/start.DVtWCojw.js"),
import("/docs/transformers/main/en/_app/immutable/entry/app.BfqWnvlk.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 3],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
35.1 kB
·
Xet hash:
aeb72e8e0ed85ad480ba31668c4dba0f7fb9fd175efb6da7a2183dae3441bdda

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