Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Urmărirea output-urilor intermediare ale modelului","local":"urmărirea-output-urilor-intermediare-ale-modelului","sections":[{"title":"Declară ce submodule să captezi","local":"declară-ce-submodule-să-captezi","sections":[],"depth":2},{"title":"Mapează câmpurile de ieșire la submodule","local":"mapează-câmpurile-de-ieșire-la-submodule","sections":[],"depth":2},{"title":"Control detaliat cu OutputRecorder","local":"control-detaliat-cu-outputrecorder","sections":[],"depth":2},{"title":"Patch-uiește clasele de layers","local":"patch-uiește-clasele-de-layers","sections":[],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/ro/_app/immutable/entry/start.BKl8VbaW.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/chunks/DND-eWVw.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/chunks/wvPyzM3A.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/entry/app.BftD3nJ7.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/chunks/T1rQByR4.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/chunks/BeYo49jn.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/nodes/0.B4JK_tsn.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/nodes/2.CPcJ2QJY.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/ro/_app/immutable/chunks/C2oiMm1z.js" rel="modulepreload"> | |
| <!--aeq4qj--><meta name="hf:doc:metadata" content="{"title":"Urmărirea output-urilor intermediare ale modelului","local":"urmărirea-output-urilor-intermediare-ale-modelului","sections":[{"title":"Declară ce submodule să captezi","local":"declară-ce-submodule-să-captezi","sections":[],"depth":2},{"title":"Mapează câmpurile de ieșire la submodule","local":"mapează-câmpurile-de-ieșire-la-submodule","sections":[],"depth":2},{"title":"Control detaliat cu OutputRecorder","local":"control-detaliat-cu-outputrecorder","sections":[],"depth":2},{"title":"Patch-uiește clasele de layers","local":"patch-uiește-clasele-de-layers","sections":[],"depth":2}],"depth":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="urmărirea-output-urilor-intermediare-ale-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="#urmărirea-output-urilor-intermediare-ale-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>Urmărirea output-urilor intermediare ale modelului</span></h1><!--]--><!----> <p>Metoda <code>forward()</code> a fiecărui model obișnuia să rezolve manual flag-uri de tip <code>None</code> precum <code>output_attentions</code> din valorile implicite din config, să acumuleze attention weights și hidden states per layer în tuples și să convertească dataclasses <code>ModelOutput</code> în tuple-uri plain când <code>return_dict=False</code>. Doi decoratori înlocuiesc tot acel cod boilerplate.</p> <ul><li><code>@capture_outputs</code> rezolvă flag-urile de ieșire, colectează valorile intermediare și gestionează conversia <code>return_dict</code>.</li> <li><code>@merge_with_config_defaults</code> rezolvă <code>use_cache</code> din config. Omite-l pentru modelele care nu fac cache, precum <code>CLIPModel</code>.</li></ul> <p>Vei întâlni acești decoratori mai ales când integrezi un model nou. Vezi <a href="./modular_transformers">adăugarea unui model în 🤗 Transformers</a> pentru un ghid pas cu pas.</p> <!--[1--><h2 class="relative group"><a id="declară-ce-submodule-să-captezi" 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="#declară-ce-submodule-să-captezi"><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>Declară ce submodule să captezi</span></h2><!--]--><!----> <p>Aplică <code>@capture_outputs</code> pe metoda <code>forward()</code> a modelului de bază. Atașează forward hooks care:</p> <ul><li>Interceptează ieșirile din clasele de submodule specificate în timpul forward pass-ului, fără ca acele submodule să știe că sunt observate.</li> <li>Colectează attention weights și hidden states per layer în tuple-uri.</li> <li>Injectează valorile colectate în dataclass-ul <code>ModelOutput</code> returnat.</li> <li>Convertesc dataclass-ul într-un tuple plain când <code>return_dict=False</code>.</li> <li>Rezolvă <code>output_attentions</code> și <code>output_hidden_states</code> din kwargs sau <code>self.config</code> când sunt <code>None</code>.</li></ul> <!--[1--><h2 class="relative group"><a id="mapează-câmpurile-de-ieșire-la-submodule" 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="#mapează-câmpurile-de-ieșire-la-submodule"><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>Mapează câmpurile de ieșire la submodule</span></h2><!--]--><!----> <p><code>@capture_outputs</code> trebuie să știe ce submodul produce ce ieșire. Declară un dicționar <code>_can_record_outputs</code> la nivel de clasă pe subclasa ta <code>PreTrainedModel</code>. Fiecare cheie este un nume de câmp de ieșire (<code>"hidden_states"</code>, <code>"attentions"</code>, <code>"cross_attentions"</code>), iar fiecare valoare este o clasă de modul, un șir cu numele clasei, o instanță <code>OutputRecorder</code> sau o listă cu acestea pentru a înregistra mai multe tipuri de module sub aceeași cheie.</p> <!--[1--><h2 class="relative group"><a id="control-detaliat-cu-outputrecorder" 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="#control-detaliat-cu-outputrecorder"><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>Control detaliat cu OutputRecorder</span></h2><!--]--><!----> <p><code>OutputRecorder</code> acceptă un <code>target_class</code> (o subclasă <code>nn.Module</code> ale cărei output-uri să le colectezi) și un <code>index</code> opțional pentru a selecta ce element din tuple-ul de ieșire al modulului să ia. Pasează <code>layer_name</code> ca să atașezi hook-uri doar la modulele cu un nume de atribut specific. Folosește <code>layer_name</code> când două layers partajează aceeași clasă, de exemplu self-attention vs. cross-attention.</p> <p>Exemplul de mai jos arată ambii decoratori în practică cu diferite niveluri de control al ieșirii. Vezi <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/models/llama/modeling_llama.py" rel="nofollow">LlamaModel</a> pentru o referință din lumea reală.</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> ...processing_utils <span class="hljs-keyword">import</span> Unpack | |
| <span class="hljs-keyword">from</span> ...utils <span class="hljs-keyword">import</span> TransformersKwargs | |
| <span class="hljs-keyword">from</span> ...utils.generic <span class="hljs-keyword">import</span> merge_with_config_defaults | |
| <span class="hljs-keyword">from</span> ...utils.output_capturing <span class="hljs-keyword">import</span> capture_outputs, OutputRecorder | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyPreTrainedModel</span>(<span class="hljs-title class_ inherited__">PreTrainedModel</span>): | |
| _can_record_outputs = { | |
| <span class="hljs-comment"># Capturează hidden_states: hook-ul se declanșează după fiecare forward MyDecoderBlock,</span> | |
| <span class="hljs-comment"># luând prima ieșire (index 0 implicit).</span> | |
| <span class="hljs-string">"hidden_states"</span>: MyDecoderBlock, | |
| <span class="hljs-comment"># Capturează self-attention weights: hook-ul se declanșează după fiecare forward MyAttention,</span> | |
| <span class="hljs-comment"># luând a doua ieșire (index=1 implicit).</span> | |
| <span class="hljs-string">"attentions"</span>: MyAttention, | |
| <span class="hljs-comment"># Capturează cross-attention weights: aceeași clasă, submodul diferit.</span> | |
| <span class="hljs-comment"># layer_name țintește atributul `self.crossattention` din interiorul block-ului.</span> | |
| <span class="hljs-comment"># Capturează a doua ieșire cum s-a cerut (index=1)</span> | |
| <span class="hljs-string">"cross_attentions"</span>: OutputRecorder( | |
| MyAttention, layer_name=<span class="hljs-string">"crossattention"</span>, index=<span class="hljs-number">1</span> | |
| ), | |
| } | |
| <span class="hljs-comment"># Acum în forward-ul modelului de bază avem nevoie de decoratori și `Unpack` `kwargs`</span> | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">MyModel</span>(<span class="hljs-title class_ inherited__">MyPreTrainedModel</span>): | |
| <span class="hljs-meta"> @merge_with_config_defaults </span><span class="hljs-comment"># ← rezolvă use_cache</span> | |
| <span class="hljs-meta"> @capture_outputs </span><span class="hljs-comment"># ← gestionează colectarea ieșirilor + return_dict</span> | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params"> | |
| self, | |
| input_ids: torch.LongTensor | <span class="hljs-literal">None</span> = <span class="hljs-literal">None</span>, | |
| past_key_values: Cache = <span class="hljs-literal">None</span>, | |
| **kwargs: Unpack[TransformersKwargs], | |
| </span>) -> BaseModelOutputWithPast: | |
| <span class="hljs-comment"># Nu e nevoie de colectare manuală. Rulează layers normal.</span> | |
| hidden_states = <span class="hljs-variable language_">self</span>.embed_tokens(input_ids) | |
| <span class="hljs-keyword">for</span> layer <span class="hljs-keyword">in</span> <span class="hljs-variable language_">self</span>.layers: | |
| hidden_states = layer(hidden_states, **kwargs) | |
| <span class="hljs-comment"># Returnează ieșirile primare. Decoratorul va completa automat</span> | |
| <span class="hljs-comment"># hidden_states/attentions/cross_attentions.</span> | |
| <span class="hljs-keyword">return</span> BaseModelOutputWithPast( | |
| last_hidden_state=hidden_states, | |
| past_key_values=past_key_values | |
| )<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="patch-uiește-clasele-de-layers" 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-uiește-clasele-de-layers"><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-uiește clasele de layers</span></h2><!--]--><!----> <p>Urmărirea output-urilor depinde de <code>_can_record_outputs</code> care pointează la clasele <em>exacte</em> pe care layer-urile modelului le instanțiază. Dacă înlocuiești o implementare de layer cu un kernel de attention personalizat, un layer de expert quantized sau o variantă arhitecturală, acei pointeri trebuie să rămână sincronizați. <a href="./monkey_patching">API-ul de patching</a> oferă un registru global curat pentru a menține <code>_can_record_outputs</code> consistent.</p> <p><code>register_patch_mapping</code> mapează numele originale ale claselor la subclase <code>nn.Module</code> de înlocuire. Cheile pot fi nume exacte de clase sau pattern-uri regex. Potrivirile exacte au prioritate. Pattern-urile sunt testate cu <code>re.search()</code>, deci pattern-urile neancorate se potrivesc oriunde în numele clasei. Înregistrarea aceleiași chei de două ori ridică <code>ValueError</code> dacă nu pasezi <code>overwrite=True</code>.</p> <p>Elimină intrările cu <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> register_patch_mapping, unregister_patch_mapping | |
| <span class="hljs-comment"># Nume exact – înlocuiește doar Qwen2MoeExperts</span> | |
| register_patch_mapping({<span class="hljs-string">"Qwen2MoeExperts"</span>: SequentialExperts}) | |
| <span class="hljs-comment"># Regex – înlocuiește orice clasă al cărei nume se termină cu "Attention"</span> | |
| register_patch_mapping({<span class="hljs-string">".*Attention$"</span>: FusedAttention}) | |
| <span class="hljs-comment"># Versiunea ancorată – se potrivește doar cu Llama2Attention, Llama3Attention, …</span> | |
| register_patch_mapping({<span class="hljs-string">"^Llama\\d+Attention$"</span>: CustomLlamaAttention}) | |
| <span class="hljs-comment"># La fel, cheile personalizate pot fi eliminate din registru pasând numele care a fost înregistrat</span> | |
| unregister_patch_mapping([<span class="hljs-string">"Qwen2MoeExperts"</span>, <span class="hljs-string">".*Attention$"</span>])<!----></pre></div><!----> <p>Odată ce mapările sunt înregistrate, <code>patch_output_recorders</code> parcurge fiecare submodul și actualizează fiecare <code>OutputRecorder.target_class</code> la înlocuitorul înregistrat.</p> <blockquote class="tip"><p>Metoda <code>from_pretrained()</code> apelează <code>patch_output_recorders</code> automat. Trebuie să o apelezi tu doar când construiești un model direct.</p></blockquote> <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> patch_output_recorders | |
| <span class="hljs-comment"># Construit manual, în afara from_pretrained</span> | |
| model = Qwen2MoeModel(config) | |
| <span class="hljs-comment"># Fără asta, _can_record_outputs tot pointează la clasa originală Qwen2MoeExperts</span> | |
| <span class="hljs-comment"># și hook-urile nu se vor declanșa niciodată pe instanțele CustomExperts.</span> | |
| patch_output_recorders(model)<!----></pre></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/model_output_tracing.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_1wlbmk2 = { | |
| 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.BKl8VbaW.js"), | |
| import("/docs/transformers/main/ro/_app/immutable/entry/app.BftD3nJ7.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 2], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 25.6 kB
- Xet hash:
- 68724666661754ec0f0e5e29a37066f6aa1c7c9b341e0b5ff54151024de16395
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.