Buckets:

hf-doc-build/doc / transformers /main /ro /monkey_patching.html
HuggingFaceDocBuilder's picture
download
raw
122 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Monkey patching (funcție experimentală)&quot;,&quot;local&quot;:&quot;monkey-patching-funcție-experimentală&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pornire rapidă&quot;,&quot;local&quot;:&quot;pornire-rapidă&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cum funcționează&quot;,&quot;local&quot;:&quot;cum-funcționează&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrare globală&quot;,&quot;local&quot;:&quot;înregistrare-globală&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Potrivirea pattern-urilor&quot;,&quot;local&quot;:&quot;potrivirea-pattern-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Construcția manuală a modelului&quot;,&quot;local&quot;:&quot;construcția-manuală-a-modelului&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Note importante&quot;,&quot;local&quot;:&quot;note-importante&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Depanare&quot;,&quot;local&quot;:&quot;depanare&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Patch-ul meu nu se aplică&quot;,&quot;local&quot;:&quot;patch-ul-meu-nu-se-aplică&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Cum știu dacă patch-ul meu funcționează?&quot;,&quot;local&quot;:&quot;cum-știu-dacă-patch-ul-meu-funcționează&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Erori de nepotrivire a dimensiunilor weights&quot;,&quot;local&quot;:&quot;erori-de-nepotrivire-a-dimensiunilor-weights&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Curățarea patch-urilor&quot;,&quot;local&quot;:&quot;curățarea-patch-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exemplu complet&quot;,&quot;local&quot;:&quot;exemplu-complet&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrarea și reluarea rutării experților MoE&quot;,&quot;local&quot;:&quot;înregistrarea-și-reluarea-rutării-experților-moe&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Interoperabilitate cu vLLM&quot;,&quot;local&quot;:&quot;interoperabilitate-cu-vllm&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Referință API&quot;,&quot;local&quot;:&quot;transformers.monkey_patching.register_patch_mapping&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/ro/_app/immutable/entry/start.CIgIKwFw.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/BcwjRHJP.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/Cl6kX2N0.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/entry/app.eNKDC4Vf.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/BzzeShVL.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/DWwSmaYb.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/DM0RASQS.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/nodes/0.P_YGbUHv.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/C8aKV6he.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/nodes/23.BDzBDurZ.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/CjTxHwdC.js" rel="modulepreload">
<!--xt7dkv--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Monkey patching (funcție experimentală)&quot;,&quot;local&quot;:&quot;monkey-patching-funcție-experimentală&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pornire rapidă&quot;,&quot;local&quot;:&quot;pornire-rapidă&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Cum funcționează&quot;,&quot;local&quot;:&quot;cum-funcționează&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrare globală&quot;,&quot;local&quot;:&quot;înregistrare-globală&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Potrivirea pattern-urilor&quot;,&quot;local&quot;:&quot;potrivirea-pattern-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Construcția manuală a modelului&quot;,&quot;local&quot;:&quot;construcția-manuală-a-modelului&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Note importante&quot;,&quot;local&quot;:&quot;note-importante&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Depanare&quot;,&quot;local&quot;:&quot;depanare&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Patch-ul meu nu se aplică&quot;,&quot;local&quot;:&quot;patch-ul-meu-nu-se-aplică&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Cum știu dacă patch-ul meu funcționează?&quot;,&quot;local&quot;:&quot;cum-știu-dacă-patch-ul-meu-funcționează&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Erori de nepotrivire a dimensiunilor weights&quot;,&quot;local&quot;:&quot;erori-de-nepotrivire-a-dimensiunilor-weights&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Curățarea patch-urilor&quot;,&quot;local&quot;:&quot;curățarea-patch-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exemplu complet&quot;,&quot;local&quot;:&quot;exemplu-complet&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrarea și reluarea rutării experților MoE&quot;,&quot;local&quot;:&quot;înregistrarea-și-reluarea-rutării-experților-moe&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Interoperabilitate cu vLLM&quot;,&quot;local&quot;:&quot;interoperabilitate-cu-vllm&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Referință API&quot;,&quot;local&quot;:&quot;transformers.monkey_patching.register_patch_mapping&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/transformers/main/ro/_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: right; margin-left: 10px; display: inline-flex; position: relative; z-index: 10;"><div 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>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="monkey-patching-funcție-experimentală" 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="#monkey-patching-funcție-experimentală"><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>Monkey patching (funcție experimentală)</span></h1><!--]--><!----> <p>Monkey patching îți permite să înlocuiești componente ale modelului la nivel global fără a modifica codul original al modelului. Odată înregistrate, patch-urile sunt aplicate automat la încărcarea oricărui model cu <code>from_pretrained()</code> sau <code>~PreTrainedModel.from_config</code>. Aceasta îți permite să restructurezi modele pentru cerințe specifice precum compatibilitatea cu quantization, să aplici optimizări sau să experimentezi cu variante arhitecturale.</p> <blockquote class="warning"><p><strong>Monkey patching ar trebui folosit ca ultimă soluție</strong> atunci când trebuie să schimbi layout-ul și structura unui modul și/sau weights asociate acestuia. Pentru nevoile de personalizare și optimizare, încearcă să folosești în schimb [interfața Attention], [interfața Experts] sau [registrul Kernels]. Folosește monkey patching doar când ai nevoie de schimbări structurale care nu pot fi realizate doar prin implementări forward personalizate (e.g., pentru compatibilitatea cu biblioteci de quantization, fuzionarea layers, sau experimente arhitecturale).</p></blockquote> <!--[1--><h2 class="relative group"><a id="pornire-rapidă" 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="#pornire-rapidă"><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>Pornire rapidă</span></h2><!--]--><!----> <p>Iată un exemplu simplu care arată cum să înlocuiești o componentă a modelului:</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 <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.models.llama.modeling_llama <span class="hljs-keyword">import</span> LlamaAttention
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
<span class="hljs-comment"># Definește clasa ta de înlocuire (trebuie să moștenească din nn.Module)</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">CustomLlamaAttention</span>(<span class="hljs-title class_ inherited__">LlamaAttention</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, *args, **kwargs</span>):
<span class="hljs-comment"># Implementarea ta personalizată</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">&quot;Using custom attention!&quot;</span>)
<span class="hljs-keyword">return</span> <span class="hljs-built_in">super</span>().forward(*args, **kwargs)
<span class="hljs-comment"># Înregistrează patch-ul global (se aplică doar modulelor de modelare transformers)</span>
register_patch_mapping(mapping={<span class="hljs-string">&quot;LlamaAttention&quot;</span>: CustomLlamaAttention})
<span class="hljs-comment"># Încarcă un model - patch-ul este aplicat automat în timpul inițializării</span>
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-3.2-1B&quot;</span>)
<span class="hljs-comment"># Toate layers LlamaAttention din model sunt acum instanțe CustomLlamaAttention</span>
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(model.model.layers[<span class="hljs-number">0</span>].self_attn)) <span class="hljs-comment"># &lt;class &#x27;__main__.CustomLlamaAttention&#x27;&gt;</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="cum-funcționează" 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="#cum-funcționează"><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>Cum funcționează</span></h2><!--]--><!----> <p>Monkey patches funcționează printr-un proces în două etape:</p> <ol><li><p><strong>Înregistrare</strong>: Apelează <code>register_patch_mapping</code> pentru a adăuga mapări la un registru global.</p></li> <li><p><strong>Aplicare</strong>: Patch-urile sunt aplicate automat în timpul inițializării modelului:</p> <ul><li><strong><code>from_pretrained</code> / <code>from_config</code></strong>: Patch-urile sunt aplicate <strong>automat</strong> printr-un context manager intern. Nu este necesară nicio acțiune suplimentară!</li> <li><strong>Construcție manuală</strong> (e.g., <code>Model(config)</code>): Trebuie să utilizezi manual context manager-ul <code>apply_patches</code>.</li></ul></li></ol> <p>Odată ce patch-urile sunt înregistrate, persistă și afectează toate încărcările ulterioare de modele până când le ștergi cu <code>clear_patch_mapping</code>.</p> <p><strong>Limitări importante</strong>:</p> <ul><li>Doar clasele din modulele de modelare <code>transformers</code> pot fi patched (e.g., <code>LlamaAttention</code>, <code>LlamaMLP</code>).</li> <li>Cheile mapării pot fi fie nume exacte de clase, fie pattern-uri de expresii regulate (vezi <a href="#potrivirea-pattern-urilor">Potrivirea pattern-urilor</a> mai jos).</li></ul> <!--[1--><h2 class="relative group"><a id="înregistrare-globală" 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="#înregistrare-globală"><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>Înregistrare globală</span></h2><!--]--><!----> <p>Folosește <code>register_patch_mapping</code> pentru a înregistra mapări global:</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.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
<span class="hljs-comment"># Înregistrează un singur patch</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: SequentialExperts}
)
<span class="hljs-comment"># Înregistrează mai multe patch-uri simultan</span>
register_patch_mapping(
mapping={
<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: SequentialExperts,
<span class="hljs-string">&quot;Qwen2MoeAttention&quot;</span>: CustomAttention,
},
<span class="hljs-comment"># Suprascrie patch-urile existente dacă există</span>
overwrite=<span class="hljs-literal">True</span>,
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="potrivirea-pattern-urilor" 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="#potrivirea-pattern-urilor"><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>Potrivirea pattern-urilor</span></h2><!--]--><!----> <p>Poți folosi expresii regulate pentru a potrivi mai multe clase cu un singur pattern:</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.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
<span class="hljs-comment"># Potrivește toate clasele care conțin &quot;Attention&quot;</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;.*Attention&quot;</span>: CustomAttention}
)
<span class="hljs-comment"># Mai multe exemple</span>
register_patch_mapping(
mapping={
<span class="hljs-string">&quot;.*MoeExperts$&quot;</span>: CustomExperts, <span class="hljs-comment"># Se termină cu &quot;MoeExperts&quot;</span>
<span class="hljs-string">&quot;^Llama\\d+Attention$&quot;</span>: CustomAttention, <span class="hljs-comment"># Llama2Attention, Llama3Attention, etc.</span>
}
)<!----></pre></div><!----> <p><strong>Important</strong>: Potrivirile exacte au prioritate față de pattern-uri. Dacă înregistrezi atât <code>"LlamaAttention"</code> cât și <code>".*Attention"</code>, clasele numite <code>LlamaAttention</code> vor folosi înlocuirea prin potrivire exactă, în timp ce alte clase potrivite vor folosi înlocuirea prin potrivire după pattern.</p> <blockquote class="warning"><p><strong>Pattern-urile regex pot strica modelele în tăcere.</strong> Un pattern larg precum <code>".*Attention"</code> va potrivi <em>fiecare</em> clasă al cărei nume conține “Attention” — inclusiv clasele container care învelesc attention-ul pe care vrei să îl înlocuiești. De exemplu, BERT are trei clase legate de attention: <code>BertSelfAttention</code> și <code>BertCrossAttention</code> (implementările interioare de attention) și <code>BertAttention</code> (un modul exterior care <em>conține</em> una dintre acele clase interioare). Patch-uirea tuturor celor trei cu același layer de attention personalizat produce un model stricat deoarece <code>BertAttention</code>-ul exterior nu mai învelește cel interior — <em>este</em> unul, eliminând sub-modulele așteptate precum <code>self</code> și <code>output</code>. Preferă pattern-uri înguste (e.g., <code>".*SelfAttention$"</code>) sau nume exacte de clase pentru a evita potrivirile neintenționate.</p></blockquote> <p>Pentru a dezînregistra patch-uri, folosește <code>unregister_patch_mapping</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> transformers.monkey_patching <span class="hljs-keyword">import</span> unregister_patch_mapping
<span class="hljs-comment"># Dezînregistrează un singur patch (folosește numele exact sau pattern-ul din înregistrare)</span>
unregister_patch_mapping(keys=[<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>])
<span class="hljs-comment"># Dezînregistrează mai multe patch-uri simultan</span>
unregister_patch_mapping(keys=[<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>, <span class="hljs-string">&quot;Qwen2MoeAttention&quot;</span>])
<span class="hljs-comment"># Dezînregistrează un pattern</span>
unregister_patch_mapping(keys=[<span class="hljs-string">&quot;.*Attention&quot;</span>])<!----></pre></div><!----> <p>Pentru a șterge toate patch-urile înregistrate, folosește <code>clear_patch_mapping</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> transformers.monkey_patching <span class="hljs-keyword">import</span> clear_patch_mapping
clear_patch_mapping()<!----></pre></div><!----> <p>Pentru a vizualiza patch-urile înregistrate curent, folosește <code>get_patch_mapping</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> transformers.monkey_patching <span class="hljs-keyword">import</span> get_patch_mapping
current_patches = get_patch_mapping()
<span class="hljs-built_in">print</span>(current_patches)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="construcția-manuală-a-modelului" 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="#construcția-manuală-a-modelului"><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>Construcția manuală a modelului</span></h2><!--]--><!----> <p>Context manager-ul <code>apply_patches</code> este necesar doar atunci când construiești modele <strong>manual</strong> (e.g., <code>Model(config)</code>) fără a folosi <code>from_pretrained</code> sau <code>from_config</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> transformers <span class="hljs-keyword">import</span> LlamaModel, LlamaConfig
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping, apply_patches
<span class="hljs-comment"># Înregistrează patch-ul global</span>
register_patch_mapping(mapping={<span class="hljs-string">&quot;LlamaAttention&quot;</span>: CustomAttention})
<span class="hljs-comment"># Pentru construcția manuală, ai nevoie de context manager</span>
<span class="hljs-keyword">with</span> apply_patches():
model = LlamaModel(LlamaConfig()) <span class="hljs-comment"># Utilizează CustomAttention</span>
<span class="hljs-comment"># Fără context manager, construcția manuală utilizează clasele originale</span>
model = LlamaModel(LlamaConfig()) <span class="hljs-comment"># Utilizează LlamaAttention</span>
<span class="hljs-comment"># Dar from_pretrained și from_config vor aplica întotdeauna patch-urile înregistrate</span>
model = LlamaModel.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-3.2-1B&quot;</span>) <span class="hljs-comment"># Utilizează CustomAttention</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="note-importante" 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="#note-importante"><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>Note importante</span></h2><!--]--><!----> <ul><li><p><strong>Gestionarea weights</strong>: Monkey patching înlocuiește doar clasele, nu și weights. Dacă clasa ta patched are un layout de weights diferit, va trebui să gestionezi <a href="./weightconverter">conversiile de weights</a> separat pentru a asigura compatibilitatea cu weights pre-antrenate. Vezi <a href="#exemplu-complet">Exemplul complet</a> de mai jos pentru a combina monkey patches cu mapări de conversie a weights.</p></li> <li><p><strong>Efect global</strong>: Patch-urile înregistrate cu <code>register_patch_mapping</code> sunt aplicate global tuturor modelelor încărcate după înregistrare. Folosește întotdeauna <code>clear_patch_mapping</code> pentru a face curățenie când termini, mai ales în teste, notebooks sau aplicații de lungă durată.</p></li> <li><p><strong>Validarea claselor</strong>: API-ul validează automat că clasele de înlocuire sunt subclase <code>nn.Module</code>. Dacă pasezi o clasă invalidă, vei primi un mesaj de eroare clar.</p></li> <li><p><strong>Thread safety</strong>: Toate operațiile de patching sunt thread-safe. Poți înregistra, dezînregistra și aplica patch-uri în siguranță din mai multe thread-uri.</p></li> <li><p><strong>Comportamentul de potrivire</strong>: Când folosești nume exacte de clase, acestea trebuie să corespundă exact cu numele claselor originale din codul sursă al modelului (cu distincție între majuscule și minuscule). Când folosești pattern-uri regex, acestea sunt potrivite față de numele claselor folosind <code>re.search()</code>.</p></li></ul> <!--[1--><h2 class="relative group"><a id="depanare" 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="#depanare"><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>Depanare</span></h2><!--]--><!----> <!--[2--><h3 class="relative group"><a id="patch-ul-meu-nu-se-aplică" 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="#patch-ul-meu-nu-se-aplică"><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>Patch-ul meu nu se aplică</span></h3><!--]--><!----> <p><strong>Verifică numele clasei sau pattern-ul</strong>: Asigură-te că numele clasei sau pattern-ul din maparea ta este corect:</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-comment"># Pentru nume exacte - trebuie să corespundă exact (cu distincție între majuscule și minuscule)</span>
register_patch_mapping(mapping={<span class="hljs-string">&quot;LlamaAttention&quot;</span>: CustomAttention})
<span class="hljs-comment"># Pentru pattern-uri - folosește regex valid</span>
register_patch_mapping(mapping={<span class="hljs-string">&quot;.*Attention&quot;</span>: CustomAttention})<!----></pre></div><!----> <p><strong>Verifică înregistrarea</strong>: Folosește <code>get_patch_mapping</code> pentru a confirma că maparea ta este înregistrată:</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-built_in">print</span>(get_patch_mapping())
<span class="hljs-comment"># Afișează toate mapările înregistrate: {&#x27;LlamaAttention&#x27;: &lt;class &#x27;CustomAttention&#x27;&gt;, &#x27;.*MLP&#x27;: &lt;class &#x27;CustomMLP&#x27;&gt;}</span><!----></pre></div><!----> <p><strong>Verifică sursa modelului</strong>: Găsește numele exact al clasei în sursa modelului:</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.models.llama <span class="hljs-keyword">import</span> modeling_llama
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">dir</span>(modeling_llama)) <span class="hljs-comment"># Caută numele clasei</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="cum-știu-dacă-patch-ul-meu-funcționează" 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="#cum-știu-dacă-patch-ul-meu-funcționează"><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>Cum știu dacă patch-ul meu funcționează?</span></h3><!--]--><!----> <p>Inspectează modelul încărcat pentru a verifica patch-ul:</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 "><!---->model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-3.2-1B&quot;</span>)
<span class="hljs-comment"># Verifică tipul unui modul specific</span>
<span class="hljs-built_in">print</span>(<span class="hljs-built_in">type</span>(model.model.layers[<span class="hljs-number">0</span>].self_attn)) <span class="hljs-comment"># Ar trebui să afișeze clasa ta personalizată</span>
<span class="hljs-comment"># Sau iterează prin toate modulele</span>
<span class="hljs-keyword">for</span> name, module <span class="hljs-keyword">in</span> model.named_modules():
<span class="hljs-keyword">if</span> <span class="hljs-string">&#x27;attention&#x27;</span> <span class="hljs-keyword">in</span> name.lower():
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;<span class="hljs-subst">{name}</span>: <span class="hljs-subst">{<span class="hljs-built_in">type</span>(module)}</span>&quot;</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="erori-de-nepotrivire-a-dimensiunilor-weights" 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="#erori-de-nepotrivire-a-dimensiunilor-weights"><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>Erori de nepotrivire a dimensiunilor weights</span></h3><!--]--><!----> <p>Dacă clasa ta patch-uită are dimensiuni de weights diferite, înregistrează o conversie de weights:</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.conversion_mapping <span class="hljs-keyword">import</span> register_checkpoint_conversion_mapping, WeightConverter
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
register_patch_mapping(
mapping={
<span class="hljs-string">&quot;LlamaAttention&quot;</span>: LlamaFusedAttention,
}
)
register_checkpoint_conversion_mapping(
model_type=<span class="hljs-string">&quot;llama&quot;</span>,
mapping=[
WeightConverter(
source_patterns=[<span class="hljs-string">&quot;q_proj&quot;</span>, <span class="hljs-string">&quot;k_proj&quot;</span>, <span class="hljs-string">&quot;v_proj&quot;</span>],
target_patterns=[<span class="hljs-string">&quot;qkv_proj&quot;</span>],
operations=[
Concatenate(dim=<span class="hljs-number">0</span>),
],
)
],
overwrite=<span class="hljs-literal">True</span>,
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="curățarea-patch-urilor" 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="#curățarea-patch-urilor"><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>Curățarea patch-urilor</span></h3><!--]--><!----> <p>Curăță întotdeauna patch-urile când termini pentru a evita afectarea altui cod:</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.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping, clear_patch_mapping
<span class="hljs-keyword">try</span>:
register_patch_mapping(mapping={<span class="hljs-string">&quot;LlamaAttention&quot;</span>: CustomAttention})
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-2-7b-chat-hf&quot;</span>)
<span class="hljs-comment"># ... folosește modelul ...</span>
<span class="hljs-keyword">finally</span>:
clear_patch_mapping() <span class="hljs-comment"># Curăță întotdeauna</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="exemplu-complet" 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="#exemplu-complet"><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>Exemplu complet</span></h2><!--]--><!----> <p>Iată un exemplu cuprinzător care arată cum să restructurezi atât modulele experts cât și cele de attention dintr-un model Mixture-of-Experts (<code>qwen2_moe</code>) pentru optimizare și compatibilitate cu quantization. Acesta demonstrează:</p> <ol><li>Crearea de clase de înlocuire personalizate care mențin aceeași interfață</li> <li>Înregistrarea de monkey patches pentru mai multe componente</li> <li>Gestionarea conversiilor de weights pentru noua structură</li></ol> <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> typing <span class="hljs-keyword">import</span> Unpack
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.nn <span class="hljs-keyword">as</span> nn
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> AutoModelForCausalLM, Concatenate, WeightConverter
<span class="hljs-keyword">from</span> transformers.activations <span class="hljs-keyword">import</span> ACT2FN
<span class="hljs-keyword">from</span> transformers.cache_utils <span class="hljs-keyword">import</span> Cache
<span class="hljs-keyword">from</span> transformers.conversion_mapping <span class="hljs-keyword">import</span> register_checkpoint_conversion_mapping
<span class="hljs-keyword">from</span> transformers.integrations.sdpa_attention <span class="hljs-keyword">import</span> sdpa_attention_forward
<span class="hljs-keyword">from</span> transformers.models.qwen2_moe.modeling_qwen2_moe <span class="hljs-keyword">import</span> apply_rotary_pos_emb
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
<span class="hljs-keyword">from</span> transformers.utils.generic <span class="hljs-keyword">import</span> TransformersKwargs
<span class="hljs-keyword">class</span> <span class="hljs-title class_">MoeMLP</span>(nn.Module):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>):
<span class="hljs-built_in">super</span>().__init__()
<span class="hljs-variable language_">self</span>.config = config
<span class="hljs-variable language_">self</span>.num_experts = config.num_experts
<span class="hljs-variable language_">self</span>.hidden_size = config.hidden_size
<span class="hljs-variable language_">self</span>.intermediate_size = config.moe_intermediate_size
<span class="hljs-variable language_">self</span>.gate_proj = nn.Linear(<span class="hljs-variable language_">self</span>.hidden_size, <span class="hljs-variable language_">self</span>.intermediate_size, bias=<span class="hljs-literal">False</span>)
<span class="hljs-variable language_">self</span>.up_proj = nn.Linear(<span class="hljs-variable language_">self</span>.hidden_size, <span class="hljs-variable language_">self</span>.intermediate_size, bias=<span class="hljs-literal">False</span>)
<span class="hljs-variable language_">self</span>.down_proj = nn.Linear(<span class="hljs-variable language_">self</span>.intermediate_size, <span class="hljs-variable language_">self</span>.hidden_size, bias=<span class="hljs-literal">False</span>)
<span class="hljs-variable language_">self</span>.act_fn = ACT2FN[config.hidden_act]
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, x</span>):
down_proj = <span class="hljs-variable language_">self</span>.down_proj(<span class="hljs-variable language_">self</span>.act_fn(<span class="hljs-variable language_">self</span>.gate_proj(x)) * <span class="hljs-variable language_">self</span>.up_proj(x))
<span class="hljs-keyword">return</span> down_proj
<span class="hljs-comment"># Adaptat din Qwen2MoeExperts original</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">ModuleListExperts</span>(nn.ModuleList):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config</span>):
<span class="hljs-built_in">super</span>().__init__()
<span class="hljs-variable language_">self</span>.config = config
<span class="hljs-variable language_">self</span>.num_experts = config.num_experts
<span class="hljs-keyword">for</span> _ <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-variable language_">self</span>.num_experts):
<span class="hljs-variable language_">self</span>.append(MoeMLP(config))
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">
self, hidden_states: torch.Tensor, top_k_index: torch.Tensor, top_k_weights: torch.Tensor
</span>) -&gt; torch.Tensor:
final_hidden_states = torch.zeros_like(hidden_states)
<span class="hljs-keyword">with</span> torch.no_grad():
expert_mask = torch.nn.functional.one_hot(top_k_index, num_classes=<span class="hljs-variable language_">self</span>.num_experts)
expert_mask = expert_mask.permute(<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">0</span>)
<span class="hljs-keyword">for</span> expert_idx <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(<span class="hljs-variable language_">self</span>.num_experts):
top_k_pos, token_idx = torch.where(expert_mask[expert_idx])
current_state = hidden_states[token_idx]
current_hidden_states = <span class="hljs-variable language_">self</span>[expert_idx](current_state)
current_hidden_states = current_hidden_states * top_k_weights[token_idx, top_k_pos, <span class="hljs-literal">None</span>]
final_hidden_states.index_add_(<span class="hljs-number">0</span>, token_idx, current_hidden_states.to(final_hidden_states.dtype))
<span class="hljs-keyword">return</span> final_hidden_states
<span class="hljs-comment"># Adaptat din Qwen2MoeAttention original</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">FusedQKVAttention</span>(nn.Module):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, config, layer_idx: <span class="hljs-built_in">int</span></span>):
<span class="hljs-built_in">super</span>().__init__()
<span class="hljs-variable language_">self</span>.config = config
<span class="hljs-variable language_">self</span>.layer_idx = layer_idx
<span class="hljs-variable language_">self</span>.head_dim = <span class="hljs-built_in">getattr</span>(config, <span class="hljs-string">&quot;head_dim&quot;</span>, config.hidden_size // config.num_attention_heads)
<span class="hljs-variable language_">self</span>.num_key_value_groups = config.num_attention_heads // config.num_key_value_heads
<span class="hljs-variable language_">self</span>.scaling = <span class="hljs-variable language_">self</span>.head_dim**-<span class="hljs-number">0.5</span>
<span class="hljs-variable language_">self</span>.attention_dropout = config.attention_dropout
<span class="hljs-variable language_">self</span>.is_causal = <span class="hljs-literal">True</span>
<span class="hljs-variable language_">self</span>.qkv_proj = nn.Linear(config.hidden_size, <span class="hljs-number">3</span> * config.num_attention_heads * <span class="hljs-variable language_">self</span>.head_dim, bias=<span class="hljs-literal">True</span>)
<span class="hljs-variable language_">self</span>.o_proj = nn.Linear(config.num_attention_heads * <span class="hljs-variable language_">self</span>.head_dim, config.hidden_size, bias=<span class="hljs-literal">False</span>)
<span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.config.layer_types[layer_idx] == <span class="hljs-string">&quot;sliding_attention&quot;</span>:
<span class="hljs-variable language_">self</span>.sliding_window = config.sliding_window
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">
self,
hidden_states: torch.Tensor,
position_embeddings: <span class="hljs-built_in">tuple</span>[torch.Tensor, torch.Tensor] | <span class="hljs-literal">None</span> = <span class="hljs-literal">None</span>,
attention_mask: torch.Tensor | <span class="hljs-literal">None</span> = <span class="hljs-literal">None</span>,
past_key_values: Cache | <span class="hljs-literal">None</span> = <span class="hljs-literal">None</span>,
**kwargs: Unpack[TransformersKwargs],
</span>) -&gt; <span class="hljs-built_in">tuple</span>[torch.Tensor, torch.Tensor]:
input_shape = hidden_states.shape[:-<span class="hljs-number">1</span>]
hidden_shape = (*input_shape, -<span class="hljs-number">1</span>, <span class="hljs-variable language_">self</span>.head_dim)
query_states, key_states, value_states = <span class="hljs-variable language_">self</span>.qkv_proj(hidden_states).chunk(<span class="hljs-number">3</span>, dim=-<span class="hljs-number">1</span>)
query_states = query_states.view(hidden_shape).transpose(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
key_states = key_states.view(hidden_shape).transpose(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
value_states = value_states.view(hidden_shape).transpose(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>)
cos, sin = position_embeddings
query_states, key_states = apply_rotary_pos_emb(query_states, key_states, cos, sin)
<span class="hljs-keyword">if</span> past_key_values <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
key_states, value_states = past_key_values.update(key_states, value_states, <span class="hljs-variable language_">self</span>.layer_idx)
attn_output, attn_weights = sdpa_attention_forward(
<span class="hljs-variable language_">self</span>,
query_states,
key_states,
value_states,
attention_mask,
dropout=<span class="hljs-number">0.0</span> <span class="hljs-keyword">if</span> <span class="hljs-keyword">not</span> <span class="hljs-variable language_">self</span>.training <span class="hljs-keyword">else</span> <span class="hljs-variable language_">self</span>.attention_dropout,
scaling=<span class="hljs-variable language_">self</span>.scaling,
**kwargs,
)
attn_output = attn_output.reshape(*input_shape, -<span class="hljs-number">1</span>).contiguous()
attn_output = <span class="hljs-variable language_">self</span>.o_proj(attn_output)
<span class="hljs-keyword">return</span> attn_output, attn_weights
<span class="hljs-comment"># Înregistrează monkey patches pentru noile module de attention și experts.</span>
register_patch_mapping(
mapping={
<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: ModuleListExperts,
<span class="hljs-string">&quot;Qwen2MoeAttention&quot;</span>: FusedQKVAttention,
}
)
<span class="hljs-comment"># Înregistrează mapările de conversie a weights adaptate pentru noile module. Această înregistrare va:</span>
<span class="hljs-comment"># - Suprascrie maparea de conversie originală pentru qwen2_moe care concatena experții într-un format de parametru unic.</span>
<span class="hljs-comment"># - Concatenează weights/biases q_proj, k_proj, v_proj într-un singur weight/bias qkv_proj pentru noul modul de attention fuzionat.</span>
register_checkpoint_conversion_mapping(
model_type=<span class="hljs-string">&quot;qwen2_moe&quot;</span>,
mapping=[
WeightConverter(
source_patterns=[<span class="hljs-string">&quot;q_proj&quot;</span>, <span class="hljs-string">&quot;k_proj&quot;</span>, <span class="hljs-string">&quot;v_proj&quot;</span>],
target_patterns=[<span class="hljs-string">&quot;qkv_proj&quot;</span>],
operations=[Concatenate(dim=<span class="hljs-number">0</span>)],
),
],
overwrite=<span class="hljs-literal">True</span>,
)
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="înregistrarea-și-reluarea-rutării-experților-moe" 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="#înregistrarea-și-reluarea-rutării-experților-moe"><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>Înregistrarea și reluarea rutării experților MoE</span></h2><!--]--><!----> <p>Fluxurile de antrenare Mixture-of-Experts precum RLHF trebuie să înregistreze la ce experți a fost dirijat fiecare token în timpul generării, apoi să repete exact acea rutare într-un forward pass de antrenare separat. Poți construi aceasta end-to-end cu mecanismele existente de monkey patching și captare a output-urilor — nu sunt necesare modificări ale fișierelor de modelare.</p> <p>Pattern-ul are trei componente:</p> <ol><li>O <strong>subclasă de router reluabilă</strong> care poate citi opțional indici de experți forțați dintr-un atribut de instanță.</li> <li>Un <strong>context manager</strong> care setează acele atribute pe fiecare router înainte de un forward pass și le șterge după.</li> <li>O intrare în registrul de captare a output-urilor modelului astfel că <code>output_&lt;name>=True</code> expune indicii prin calea standard <code>@capture_outputs</code>.</li></ol> <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> contextlib <span class="hljs-keyword">import</span> contextmanager
<span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">import</span> torch.nn.functional <span class="hljs-keyword">as</span> F
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> Qwen3MoeConfig, Qwen3MoeForCausalLM
<span class="hljs-keyword">from</span> transformers.models.qwen3_moe.modeling_qwen3_moe <span class="hljs-keyword">import</span> Qwen3MoeTopKRouter
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> apply_patches, register_patch_mapping
<span class="hljs-keyword">from</span> transformers.utils.output_capturing <span class="hljs-keyword">import</span> _CAN_RECORD_REGISTRY, OutputRecorder
<span class="hljs-keyword">class</span> <span class="hljs-title class_">ReplayableQwen3MoeTopKRouter</span>(<span class="hljs-title class_ inherited__">Qwen3MoeTopKRouter</span>):
_forced_indices: torch.Tensor | <span class="hljs-literal">None</span> = <span class="hljs-literal">None</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, hidden_states</span>):
hidden_states = hidden_states.reshape(-<span class="hljs-number">1</span>, <span class="hljs-variable language_">self</span>.hidden_dim)
router_logits = F.linear(hidden_states, <span class="hljs-variable language_">self</span>.weight)
router_logits = F.softmax(router_logits, dtype=torch.<span class="hljs-built_in">float</span>, dim=-<span class="hljs-number">1</span>)
<span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>._forced_indices <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
router_indices = <span class="hljs-variable language_">self</span>._forced_indices.to(router_logits.device).long()
<span class="hljs-comment"># Replay în stil Megatron: păstrează calea expertului, recalculează scorurile curente</span>
router_top_value = router_logits.gather(-<span class="hljs-number">1</span>, router_indices)
<span class="hljs-keyword">else</span>:
router_top_value, router_indices = torch.topk(router_logits, <span class="hljs-variable language_">self</span>.top_k, dim=-<span class="hljs-number">1</span>)
<span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.norm_topk_prob:
router_top_value = router_top_value / router_top_value.<span class="hljs-built_in">sum</span>(dim=-<span class="hljs-number">1</span>, keepdim=<span class="hljs-literal">True</span>)
<span class="hljs-keyword">return</span> router_logits, router_top_value.to(router_logits.dtype), router_indices
<span class="hljs-meta">@contextmanager</span>
<span class="hljs-keyword">def</span> <span class="hljs-title function_">replay_moe_routing</span>(<span class="hljs-params">model, selected_experts_per_layer</span>):
routers = [m <span class="hljs-keyword">for</span> m <span class="hljs-keyword">in</span> model.modules() <span class="hljs-keyword">if</span> <span class="hljs-built_in">isinstance</span>(m, ReplayableQwen3MoeTopKRouter)]
<span class="hljs-keyword">if</span> <span class="hljs-built_in">len</span>(routers) != <span class="hljs-built_in">len</span>(selected_experts_per_layer):
<span class="hljs-keyword">raise</span> ValueError(<span class="hljs-string">f&quot;Got <span class="hljs-subst">{<span class="hljs-built_in">len</span>(routers)}</span> routers but <span class="hljs-subst">{<span class="hljs-built_in">len</span>(selected_experts_per_layer)}</span> tensors&quot;</span>)
<span class="hljs-keyword">for</span> r, t <span class="hljs-keyword">in</span> <span class="hljs-built_in">zip</span>(routers, selected_experts_per_layer):
r._forced_indices = t
<span class="hljs-keyword">try</span>:
<span class="hljs-keyword">yield</span>
<span class="hljs-keyword">finally</span>:
<span class="hljs-keyword">for</span> r <span class="hljs-keyword">in</span> routers:
r._forced_indices = <span class="hljs-literal">None</span>
<span class="hljs-comment"># Înlocuiește clasa router și construiește modelul</span>
register_patch_mapping({<span class="hljs-string">&quot;Qwen3MoeTopKRouter&quot;</span>: ReplayableQwen3MoeTopKRouter})
<span class="hljs-keyword">with</span> apply_patches():
model = Qwen3MoeForCausalLM(Qwen3MoeConfig(...)).<span class="hljs-built_in">eval</span>()
<span class="hljs-comment"># Expune `output_selected_experts=True` pe modelul de bază adăugând un OutputRecorder</span>
<span class="hljs-comment"># la runtime. Indexul 2 din output-ul tuple al router-ului reprezintă indicii experților.</span>
inner = model.model
existing = _CAN_RECORD_REGISTRY.get(<span class="hljs-built_in">str</span>(inner.__class__), {}) <span class="hljs-keyword">or</span> {}
_CAN_RECORD_REGISTRY[<span class="hljs-built_in">str</span>(inner.__class__)] = {
**existing,
<span class="hljs-string">&quot;selected_experts&quot;</span>: OutputRecorder(ReplayableQwen3MoeTopKRouter, index=<span class="hljs-number">2</span>),
}
<span class="hljs-comment"># Înregistrează</span>
captured = inner(input_ids=input_ids, output_selected_experts=<span class="hljs-literal">True</span>)
selected_experts = captured.selected_experts <span class="hljs-comment"># tuple de LongTensors (num_tokens, top_k)</span>
<span class="hljs-comment"># Replay — aceeași cale a expertului indiferent de weights-urile curente ale router-ului</span>
<span class="hljs-keyword">with</span> replay_moe_routing(inner, <span class="hljs-built_in">list</span>(selected_experts)):
outputs = inner(input_ids=input_ids)<!----></pre></div><!----> <p>Replay-ul păstrează indicii exacte ai experților și recalculează scorurile de rutare cu weights curente ale router-ului, astfel că gradients curg prin parametrii activi în timp ce selecția experților rămâne fixă. Acesta este contractul minimal de replay utilizat în antrenarea MoE în stil Megatron.</p> <!--[2--><h3 class="relative group"><a id="interoperabilitate-cu-vllm" 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="#interoperabilitate-cu-vllm"><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>Interoperabilitate cu vLLM</span></h3><!--]--><!----> <p>Opțiunea <code>enable_return_routed_experts=True</code> din vLLM populează <code>CompletionOutput.routed_experts</code> ca un array <code>np.int32</code> de forma <code>(seq_len, num_layers, top_k)</code>. Convertește-l în lista per-layer pe care o utilizează acest pattern cu o singură expresie:</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 "><!---->selected = [
torch.from_numpy(routed_experts[:, layer, :].copy()).long()
<span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> <span class="hljs-built_in">range</span>(routed_experts.shape[<span class="hljs-number">1</span>])
]
<span class="hljs-keyword">with</span> replay_moe_routing(model, selected):
loss = model(input_ids=input_ids, labels=labels).loss<!----></pre></div><!----> <p>Aceeași rețetă se aplică și altor familii MoE — subclasează <code>*TopKRouter</code>-ul familiei, potrivește contractul de return original (de obicei <code>(router_logits, router_scores, router_indices)</code>) și înregistrează patch-ul. Consultă clasa router a fiecărui model pentru semnătura exactă.</p> <!--[1--><h2 class="relative group"><a id="transformers.monkey_patching.register_patch_mapping" 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.monkey_patching.register_patch_mapping"><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>Referință API</span></h2><!--]--><!----> <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.monkey_patching.register_patch_mapping"><!----><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.monkey_patching.register_patch_mapping</span></h4><!----> <a id="transformers.monkey_patching.register_patch_mapping" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.monkey_patching.register_patch_mapping"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/monkey_patching.py#L85" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">mapping<span class="opacity-60">: dict</span></span></span><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">overwrite<span class="opacity-60">: bool = False</span></span></span><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[0--><p class="flex items-center font-semibold !mt-2 !mb-2 text-gray-800">Parameters <span class="flex-auto border-t-2 border-gray-100 dark:border-gray-700 ml-3"></span></p> <ul class="px-2"><!--[--><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.monkey_patching.register_patch_mapping.mapping" 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.monkey_patching.register_patch_mapping.mapping"><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> <span><!----><strong>mapping</strong> (<code>Dict[str, type[nn.Module]]</code>) &#x2014;
Mapping from original class names (or regex patterns) to replacement classes. Supports:</p>
<ul>
<li>Exact class names: <code>&quot;Qwen2MoeExperts&quot;</code> &#x2192; <code>CustomExperts</code></li>
<li>Regex patterns: <code>&quot;.*Attention&quot;</code> matches <code>LlamaAttention</code>, <code>MistralAttention</code>, etc.,
or <code>&quot;^Llama\d+Attention$&quot;</code> matches <code>Llama2Attention</code>, <code>Llama3Attention</code>, etc.</li>
</ul>
<p>Exact matches take precedence over patterns. Patterns are matched using <code>re.search()</code>,
so they can match anywhere in the class name unless you use anchors (<code>^</code> for start, <code>$</code> for end).<!----></span></span></li><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.monkey_patching.register_patch_mapping.overwrite" 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.monkey_patching.register_patch_mapping.overwrite"><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> <span><!----><strong>overwrite</strong> (<code>bool</code>, <em>optional</em>, defaults to <code>False</code>) &#x2014;
Whether to overwrite existing mappings for class names that are already registered.<!----></span></span></li><!--]--></ul><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Register patch mappings to enable automatic patching during model creation using <code>from_pretrained</code>, <code>from_config</code> or within the <code>apply_patches</code> context manager.</p> <p>Use this to register class replacements that will be automatically applied when loading any model.
This is useful for quantization library compatibility, structural optimizations, and architectural
experimentation. The mapping is global, can grow with multiple calls, and can be cleared entirely.</p> <div class="relative group rounded-md"><a id="transformers.monkey_patching.register_patch_mapping.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.monkey_patching.register_patch_mapping.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> <p>Example:</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 <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping
<span class="hljs-comment"># Define custom expert implementation</span>
<span class="hljs-keyword">class</span> <span class="hljs-title class_">SequentialExperts</span>(nn.Module):
...
<span class="hljs-comment"># Register exact class name</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: SequentialExperts}
)
<span class="hljs-comment"># Register with regex pattern to match multiple classes</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;.*Attention&quot;</span>: CustomAttention} <span class="hljs-comment"># Matches LlamaAttention, MistralAttention, etc.</span>
)
<span class="hljs-comment"># Match specific model versions</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;^Llama\d+Attention$&quot;</span>: CustomLlamaAttention} <span class="hljs-comment"># Matches Llama2Attention, Llama3Attention</span>
)
<span class="hljs-comment"># The patch will be automatically applied during loading</span>
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;meta-llama/Llama-3.2-1B&quot;</span>)<!----></pre></div><!----><!----></div><!----> <p>Note:
For weight conversions, use <code>~transformers.register_checkpoint_conversion_mapping</code> instead.</p></div> <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.monkey_patching.unregister_patch_mapping"><!----><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.monkey_patching.unregister_patch_mapping</span></h4><!----> <a id="transformers.monkey_patching.unregister_patch_mapping" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.monkey_patching.unregister_patch_mapping"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/monkey_patching.py#L158" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><span class="comma cursor-pointer"><span class="rounded hover:bg-black hover:text-white dark:hover:bg-white dark:hover:text-black">keys<span class="opacity-60">: list</span></span></span><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[0--><p class="flex items-center font-semibold !mt-2 !mb-2 text-gray-800">Parameters <span class="flex-auto border-t-2 border-gray-100 dark:border-gray-700 ml-3"></span></p> <ul class="px-2"><!--[--><li class="text-base !pl-4 my-3 rounded "><span class="group flex space-x-1.5 items-start"><a id="transformers.monkey_patching.unregister_patch_mapping.keys" 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.monkey_patching.unregister_patch_mapping.keys"><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> <span><!----><strong>keys</strong> (<code>List[str]</code>) &#x2014;
List of mapping keys (class names or regex patterns) to remove from the patch mapping
(e.g., <code>[&quot;Qwen2MoeExperts&quot;]</code> or <code>[&quot;.*Attention&quot;]</code>).<!----></span></span></li><!--]--></ul><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Unregister patch mappings to disable automatic patching.</p> <p>This removes specified mappings from the global registry, preventing them from being applied
during model loading. You must provide the exact same name or pattern that was used during registration.</p> <div class="relative group rounded-md"><a id="transformers.monkey_patching.unregister_patch_mapping.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.monkey_patching.unregister_patch_mapping.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> <p>Example:</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 <span class="hljs-keyword">import</span> AutoModelForCausalLM
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping, unregister_patch_mapping
<span class="hljs-comment"># Register a patch</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: CustomExperts}
)
<span class="hljs-comment"># Unregister the patch</span>
unregister_patch_mapping([<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>])
<span class="hljs-comment"># The patch will no longer be applied during loading</span>
model = AutoModelForCausalLM.from_pretrained(<span class="hljs-string">&quot;Qwen/Qwen1.5-MoE-A2.7B&quot;</span>)<!----></pre></div><!----><!----></div><!----></div> <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.monkey_patching.clear_patch_mapping"><!----><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.monkey_patching.clear_patch_mapping</span></h4><!----> <a id="transformers.monkey_patching.clear_patch_mapping" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.monkey_patching.clear_patch_mapping"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/monkey_patching.py#L209" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Clear all registered patch mappings.</p> <p>This removes all registered mappings from the global registry.</p> <div class="relative group rounded-md"><a id="transformers.monkey_patching.clear_patch_mapping.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.monkey_patching.clear_patch_mapping.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> <p>Example:</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.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping, clear_patch_mapping
<span class="hljs-comment"># Register some patches</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: CustomExperts}
)
<span class="hljs-comment"># Clear all patches</span>
clear_patch_mapping()<!----></pre></div><!----><!----></div><!----></div> <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.monkey_patching.get_patch_mapping"><!----><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.monkey_patching.get_patch_mapping</span></h4><!----> <a id="transformers.monkey_patching.get_patch_mapping" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.monkey_patching.get_patch_mapping"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/monkey_patching.py#L198" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><!--]--> <span>)</span> <!--[0--><span class="font-bold"></span> <span class="rounded hover:bg-gray-400 cursor-pointer"><!----><script context="module">export const metadata = 'undefined';</script><span><code>Dict[str, type[nn.Module]]</code></span><!----></span><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[0--><div id="transformers.monkey_patching.get_patch_mapping.returns" class="flex items-center font-semibold space-x-3 text-base !mt-0 !mb-0 text-gray-800 rounded "><p class="text-base">Returns</p> <!--[0--><!----><script context="module">export const metadata = 'undefined';</script>
<p><code>Dict[str, type[nn.Module]]</code></p>
<!----><!--]--> <span class="flex-auto border-t-2 border-gray-100 dark:border-gray-700"></span></div> <p class="text-base"><!----><script context="module">export const metadata = 'undefined';</script>
<p>Dictionary mapping class names or patterns to replacement classes.</p>
<!----></p><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Get all registered patch mappings.</p></div> <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.monkey_patching.apply_patches"><!----><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.monkey_patching.apply_patches</span></h4><!----> <a id="transformers.monkey_patching.apply_patches" class="header-link invisible with-hover:group-hover:visible pr-2" href="#transformers.monkey_patching.apply_patches"><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><!----></a> <!--[0--><a class="!ml-auto !text-gray-400 !no-underline text-sm flex items-center" href="https://github.com/huggingface/transformers/blob/main/src/transformers/monkey_patching.py#L233" target="_blank"><span>&lt;</span> <span class="hidden md:block mx-0.5 hover:!underline">source</span> <span>></span></a><!--]--></span> <!--[0--><p class="font-mono text-xs md:text-sm !leading-relaxed !my-6"><span>(</span> <!--[--><!--]--> <span>)</span> <!--[-1--><!--]--></p><!--]--> <div class="!mb-10 relative docstring-details "><!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--> <!--[-1--><!--]--></div></div><!----> <p>Context manager to apply registered monkey patches within a block of code.</p> <p>This temporarily replaces original classes with their registered replacements during the execution of the block, and restores the original classes afterward.</p> <div class="relative group rounded-md"><a id="transformers.monkey_patching.apply_patches.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.monkey_patching.apply_patches.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> <p>Example:</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 <span class="hljs-keyword">import</span> Qwen2MoeModel, Qwen2MoeConfig
<span class="hljs-keyword">from</span> transformers.monkey_patching <span class="hljs-keyword">import</span> register_patch_mapping, apply_patches
<span class="hljs-comment"># Register a patch</span>
register_patch_mapping(
mapping={<span class="hljs-string">&quot;Qwen2MoeExperts&quot;</span>: CustomExperts}
)
<span class="hljs-comment"># Apply patches within the context</span>
<span class="hljs-keyword">with</span> apply_patches():
<span class="hljs-comment"># The model will use CustomExperts instead of Qwen2MoeExperts</span>
model = Qwen2MoeModel(Qwen2MoeConfig())
<span class="hljs-comment"># Outside the context, original classes are restored</span>
<span class="hljs-comment"># The model will use Qwen2MoeExperts again</span>
model = Qwen2MoeModel(Qwen2MoeConfig())<!----></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/ro/monkey_patching.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_1pz8pps = {
base: "/docs/transformers/main/ro",
assets: "/docs/transformers/main/ro"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/transformers/main/ro/_app/immutable/entry/start.CIgIKwFw.js"),
import("/docs/transformers/main/ro/_app/immutable/entry/app.eNKDC4Vf.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 23],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
122 kB
·
Xet hash:
846f952192053a39f8626ed5a1629e1ea5e17c9ebe4cb90828788c0c4608769e

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