Buckets:

hf-doc-build/doc / transformers /main /ro /weightconverter.html
HuggingFaceDocBuilder's picture
download
raw
161 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Încărcarea dinamică de weights&quot;,&quot;local&quot;:&quot;încărcarea-dinamică-de-weights&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline-ul complet de încărcare&quot;,&quot;local&quot;:&quot;pipeline-ul-complet-de-încărcare&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Modele dense (e.g., Llama)&quot;,&quot;local&quot;:&quot;modele-dense-eg-llama&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Modele MoE (e.g., Mixtral)&quot;,&quot;local&quot;:&quot;modele-moe-eg-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Modele compuse (e.g., viziune-limbaj)&quot;,&quot;local&quot;:&quot;modele-compuse-eg-viziune-limbaj&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Arhitectură&quot;,&quot;local&quot;:&quot;arhitectură&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;WeightTransform&quot;,&quot;local&quot;:&quot;weighttransform&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;WeightRenaming&quot;,&quot;local&quot;:&quot;weightrenaming&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;WeightConverter&quot;,&quot;local&quot;:&quot;weightconverter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Ordonarea redenumirilor și convertoarelor&quot;,&quot;local&quot;:&quot;ordonarea-redenumirilor-și-convertoarelor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Operații de conversie&quot;,&quot;local&quot;:&quot;operații-de-conversie&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Chunk&quot;,&quot;local&quot;:&quot;chunk&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Concatenate&quot;,&quot;local&quot;:&quot;concatenate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;MergeModulelist&quot;,&quot;local&quot;:&quot;mergemodulelist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;SplitModulelist&quot;,&quot;local&quot;:&quot;splitmodulelist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;PermuteForRope&quot;,&quot;local&quot;:&quot;permuteforrope&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Transpose&quot;,&quot;local&quot;:&quot;transpose&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înlănțuirea operațiilor&quot;,&quot;local&quot;:&quot;înlănțuirea-operațiilor&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Exemplu: Conversia MoE Mixtral&quot;,&quot;local&quot;:&quot;exemplu-conversia-moe-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Reguli de potrivire a pattern-urilor&quot;,&quot;local&quot;:&quot;reguli-de-potrivire-a-pattern-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrarea unei mapări de conversie&quot;,&quot;local&quot;:&quot;înregistrarea-unei-mapări-de-conversie&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Reguli de căutare&quot;,&quot;local&quot;:&quot;reguli-de-căutare&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Mapări bazate pe clasă vs aliasuri model_type&quot;,&quot;local&quot;:&quot;mapări-bazate-pe-clasă-vs-aliasuri-modeltype&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Integrarea cu tensor parallelism&quot;,&quot;local&quot;:&quot;integrarea-cu-tensor-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Stiluri de paralelism disponibile&quot;,&quot;local&quot;:&quot;stiluri-de-paralelism-disponibile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Gestionarea weights împachetate&quot;,&quot;local&quot;:&quot;gestionarea-weights-împachetate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Integrarea cu quantization&quot;,&quot;local&quot;:&quot;integrarea-cu-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Încărcarea rapidă și eficientă a modelelor&quot;,&quot;local&quot;:&quot;încărcarea-rapidă-și-eficientă-a-modelelor&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Încărcare asincronă vs sincronă&quot;,&quot;local&quot;:&quot;încărcare-asincronă-vs-sincronă&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Fluxul de materializare&quot;,&quot;local&quot;:&quot;fluxul-de-materializare&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Eficiența memoriei&quot;,&quot;local&quot;:&quot;eficiența-memoriei&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Reversibilitate&quot;,&quot;local&quot;:&quot;reversibilitate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exemple reale&quot;,&quot;local&quot;:&quot;exemple-reale&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;MoE în stil Mixtral&quot;,&quot;local&quot;:&quot;moe-în-stil-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Model compus viziune-limbaj (Gemma3)&quot;,&quot;local&quot;:&quot;model-compus-viziune-limbaj-gemma3&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Imbricare mai profundă cu suprascrierea specifică capului (DETR)&quot;,&quot;local&quot;:&quot;imbricare-mai-profundă-cu-suprascrierea-specifică-capului-detr&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Operații personalizate (ERNIE 4.5 VL MoE)&quot;,&quot;local&quot;:&quot;operații-personalizate-ernie-45-vl-moe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Aliasuri de tip model&quot;,&quot;local&quot;:&quot;aliasuri-de-tip-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Reutilizarea blocurilor de construcție ale încărcării dinamice&quot;,&quot;local&quot;:&quot;reutilizarea-blocurilor-de-construcție-ale-încărcării-dinamice&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Referință fișiere cheie&quot;,&quot;local&quot;:&quot;referință-fișiere-cheie&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/transformers/main/ro/_app/immutable/entry/start.DwnOcLC_.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/CL87Xr5_.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.Dw7RcaNl.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.DMmsXSdS.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/35.BjLYYn87.js" rel="modulepreload">
<link href="/docs/transformers/main/ro/_app/immutable/chunks/CjTxHwdC.js" rel="modulepreload">
<!--5a0qxf--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Încărcarea dinamică de weights&quot;,&quot;local&quot;:&quot;încărcarea-dinamică-de-weights&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline-ul complet de încărcare&quot;,&quot;local&quot;:&quot;pipeline-ul-complet-de-încărcare&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Modele dense (e.g., Llama)&quot;,&quot;local&quot;:&quot;modele-dense-eg-llama&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Modele MoE (e.g., Mixtral)&quot;,&quot;local&quot;:&quot;modele-moe-eg-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Modele compuse (e.g., viziune-limbaj)&quot;,&quot;local&quot;:&quot;modele-compuse-eg-viziune-limbaj&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Arhitectură&quot;,&quot;local&quot;:&quot;arhitectură&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;WeightTransform&quot;,&quot;local&quot;:&quot;weighttransform&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;WeightRenaming&quot;,&quot;local&quot;:&quot;weightrenaming&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;WeightConverter&quot;,&quot;local&quot;:&quot;weightconverter&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Ordonarea redenumirilor și convertoarelor&quot;,&quot;local&quot;:&quot;ordonarea-redenumirilor-și-convertoarelor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Operații de conversie&quot;,&quot;local&quot;:&quot;operații-de-conversie&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Chunk&quot;,&quot;local&quot;:&quot;chunk&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Concatenate&quot;,&quot;local&quot;:&quot;concatenate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;MergeModulelist&quot;,&quot;local&quot;:&quot;mergemodulelist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;SplitModulelist&quot;,&quot;local&quot;:&quot;splitmodulelist&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;PermuteForRope&quot;,&quot;local&quot;:&quot;permuteforrope&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Transpose&quot;,&quot;local&quot;:&quot;transpose&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înlănțuirea operațiilor&quot;,&quot;local&quot;:&quot;înlănțuirea-operațiilor&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Exemplu: Conversia MoE Mixtral&quot;,&quot;local&quot;:&quot;exemplu-conversia-moe-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Reguli de potrivire a pattern-urilor&quot;,&quot;local&quot;:&quot;reguli-de-potrivire-a-pattern-urilor&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Înregistrarea unei mapări de conversie&quot;,&quot;local&quot;:&quot;înregistrarea-unei-mapări-de-conversie&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Reguli de căutare&quot;,&quot;local&quot;:&quot;reguli-de-căutare&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Mapări bazate pe clasă vs aliasuri model_type&quot;,&quot;local&quot;:&quot;mapări-bazate-pe-clasă-vs-aliasuri-modeltype&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Integrarea cu tensor parallelism&quot;,&quot;local&quot;:&quot;integrarea-cu-tensor-parallelism&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Stiluri de paralelism disponibile&quot;,&quot;local&quot;:&quot;stiluri-de-paralelism-disponibile&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Gestionarea weights împachetate&quot;,&quot;local&quot;:&quot;gestionarea-weights-împachetate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Integrarea cu quantization&quot;,&quot;local&quot;:&quot;integrarea-cu-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Încărcarea rapidă și eficientă a modelelor&quot;,&quot;local&quot;:&quot;încărcarea-rapidă-și-eficientă-a-modelelor&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Încărcare asincronă vs sincronă&quot;,&quot;local&quot;:&quot;încărcare-asincronă-vs-sincronă&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Fluxul de materializare&quot;,&quot;local&quot;:&quot;fluxul-de-materializare&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Eficiența memoriei&quot;,&quot;local&quot;:&quot;eficiența-memoriei&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Reversibilitate&quot;,&quot;local&quot;:&quot;reversibilitate&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Exemple reale&quot;,&quot;local&quot;:&quot;exemple-reale&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;MoE în stil Mixtral&quot;,&quot;local&quot;:&quot;moe-în-stil-mixtral&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Model compus viziune-limbaj (Gemma3)&quot;,&quot;local&quot;:&quot;model-compus-viziune-limbaj-gemma3&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Imbricare mai profundă cu suprascrierea specifică capului (DETR)&quot;,&quot;local&quot;:&quot;imbricare-mai-profundă-cu-suprascrierea-specifică-capului-detr&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Operații personalizate (ERNIE 4.5 VL MoE)&quot;,&quot;local&quot;:&quot;operații-personalizate-ernie-45-vl-moe&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Aliasuri de tip model&quot;,&quot;local&quot;:&quot;aliasuri-de-tip-model&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Reutilizarea blocurilor de construcție ale încărcării dinamice&quot;,&quot;local&quot;:&quot;reutilizarea-blocurilor-de-construcție-ale-încărcării-dinamice&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Referință fișiere cheie&quot;,&quot;local&quot;:&quot;referință-fișiere-cheie&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="încărcarea-dinamică-de-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="#încărcarea-dinamică-de-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>Încărcarea dinamică de weights</span></h1><!--]--><!----> <p>Checkpoint-urile sunt des serializate într-un format diferit de cel pe care un model îl așteaptă la rulare. Scenariile comune includ:</p> <ol><li><strong>Weights fuzionate</strong>: Checkpoint-urile stochează weights separate <code>gate_proj</code> și <code>up_proj</code>, dar modelul utilizează un weight fuzionat <code>gate_up_proj</code> pentru eficiență.</li> <li><strong>Consolidare expertă MoE</strong>: Weights experte individuale (<code>experts.0.weight</code>, <code>experts.1.weight</code>, …) trebuie să fie combinate într-un singur tensor 3D.</li> <li><strong>Denumire veche</strong>: Checkpoint-urile vechi utilizează diferite convenții de numire (e.g., <code>LayerNorm.gamma</code> vs <code>LayerNorm.weight</code>).</li> <li><strong>Modele compuse</strong>: Un model lingvistic vizual conține două sub-module <code>PreTrainedModel</code>, fiecare cu propria convenție pentru checkpoint-uri.</li> <li><strong>Quantization</strong>: Weights ar putea fi stocate în formate quantized care necesită deserializare.</li></ol> <p>Încărcarea dinamică de weights rezolvă această problemă prin aplicarea de operații planificate și reversibile asupra tensorilor din checkpoint pe măsură ce sunt încărcați. Transformers expune aceasta prin <code>WeightConverter</code> și <code>WeightRenaming</code>, care descriu cum una sau mai multe chei de checkpoint se mapează la unul sau mai mulți parametri ai modelului și ce <code>ConversionOps</code> compozabile ar trebui să ruleze pe tensorii potriviți. Această abordare se adaptează la noi layout-uri de weights, suportă mixture-of-experts (MoE) quantized și se integrează cu tensor parallelism.</p> <p>Acest ghid demonstrează cum să utilizezi <code>WeightConverter</code> pentru a converti tensori. Mapările de conversie se află în <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/conversion_mapping.py" rel="nofollow"><code>conversion_mapping.py</code></a>; o mapare înregistrată este indexată fie printr-un string <code>model_type</code> (e.g. <code>"mixtral"</code>) fie printr-un nume de clasă (e.g. <code>"LlavaModel"</code>).</p> <!--[1--><h2 class="relative group"><a id="pipeline-ul-complet-de-încărcare" 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="#pipeline-ul-complet-de-încărcare"><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>Pipeline-ul complet de încărcare</span></h2><!--]--><!----> <p>Toate modelele trec prin sistemul de încărcare dinamică de weights. Maparea de conversie este un <strong>pas opțional în cadrul acelui sistem</strong> care se activează doar atunci când modelul are înregistrări pentru numele clasei sale sau <code>model_type</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=" "><!---->Fișier <span class="hljs-keyword">Checkpoint</span> → from_pretrained() → convert_and_load_state_dict_in_model()
┌───────────────────────────────────────────────────────────┐
│ Pentru fiecare weight din <span class="hljs-keyword">checkpoint</span>: │
<span class="hljs-number">1.</span> Potrivește cheia sursă redenumită/procesată cu │
│ parametrul modelului │
<span class="hljs-number">2.</span> Fragmentează weight-ul și trimite pe dispozitiv │
│ (asincron) │
<span class="hljs-number">3.</span> Colectează tensorii cu același source_pattern │
│ împreună (e.g. experți MoE, gate_up_proj) │
<span class="hljs-number">4.</span> Aplică dequantization/deserializarea (dacă pre-quant) │
<span class="hljs-number">5.</span> Aplică conversia (dacă este definită) │
<span class="hljs-number">6.</span> Aplică quantizarea (dacă activată și pasul <span class="hljs-number">4</span> nu │
│ a fost folosit) │
<span class="hljs-number">7.</span> Setează parametrul pe model │
└───────────────────────────────────────────────────────────┘<!----></pre></div><!----> <table><thead><tr><th>Pas</th><th>Când se activează</th></tr></thead><tbody><tr><td>Încărcare dinamică</td><td>Întotdeauna, pentru toate modelele</td></tr><tr><td>Mapare de conversie</td><td>Doar când clasa modelului sau <code>model_type</code> este înregistrată în <code>_MODEL_TO_CONVERSION_PATTERN</code></td></tr><tr><td>Sharding TP</td><td>Doar când <code>tp_plan="auto"</code> și modelul are <code>base_model_tp_plan</code></td></tr><tr><td>Dequantization/deserializare</td><td>Doar la încărcarea unui checkpoint pre-quantized</td></tr><tr><td>Quantization</td><td>Doar când există o configurație de quantization și weights nu sunt pre-quantized</td></tr></tbody></table> <!--[2--><h3 class="relative group"><a id="modele-dense-eg-llama" 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="#modele-dense-eg-llama"><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>Modele dense (e.g., Llama)</span></h3><!--]--><!----> <p>Pentru majoritatea modelelor dense, formatul checkpoint-ului corespunde direct formatului modelului, așadar nu este necesară nicio mapare de conversie. Unele modele pot necesita, totuși, redenumire (e.g., convenții de numire vechi). Sharding-ul TP se aplică în continuare când este activat.</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=" "><!---->Checkpoint: Model:
model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.self_attn</span><span class="hljs-selector-class">.q_proj</span><span class="hljs-selector-class">.weight</span> → model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.self_attn</span><span class="hljs-selector-class">.q_proj</span><span class="hljs-selector-class">.weight</span>
model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.self_attn</span><span class="hljs-selector-class">.k_proj</span><span class="hljs-selector-class">.weight</span> → model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.self_attn</span><span class="hljs-selector-class">.k_proj</span><span class="hljs-selector-class">.weight</span>
model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.gate_proj</span><span class="hljs-selector-class">.weight</span> → model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.gate_proj</span><span class="hljs-selector-class">.weight</span>
model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.up_proj</span><span class="hljs-selector-class">.weight</span> → model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.up_proj</span><span class="hljs-selector-class">.weight</span>
model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.down_proj</span><span class="hljs-selector-class">.weight</span> → model<span class="hljs-selector-class">.layers</span>.<span class="hljs-number">0</span><span class="hljs-selector-class">.mlp</span><span class="hljs-selector-class">.down_proj</span>.weight<!----></pre></div><!----> <p>Checkpoint-urile vechi pot folosi convenții de numire mai vechi care sunt gestionate de redenumiri integrate aplicate tuturor modelelor:</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=" "><!---->Checkpoint: <span class="hljs-keyword">Model</span>:
LayerNorm.<span class="hljs-built_in">gamma</span> → LayerNorm.weight
LayerNorm.<span class="hljs-built_in">beta</span> → LayerNorm.bias<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="modele-moe-eg-mixtral" 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="#modele-moe-eg-mixtral"><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>Modele MoE (e.g., Mixtral)</span></h3><!--]--><!----> <p>Pentru modelele MoE, formatul checkpoint-ului diferă de formatul modelului. Maparea de conversie transformă weights separate ale experților în tensori 3D fuzionați, iar sharding-ul TP se aplică după conversie.</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=" "><!---->Checkpoint: Model:
experts.<span class="hljs-number">0</span><span class="hljs-selector-class">.w1</span><span class="hljs-selector-class">.weight</span> ─┐
experts.<span class="hljs-number">1</span><span class="hljs-selector-class">.w1</span><span class="hljs-selector-class">.weight</span> │ MergeModulelist
... ├───────────────→ experts<span class="hljs-selector-class">.gate_up_proj</span> (<span class="hljs-number">8</span>, hidden, <span class="hljs-number">2</span>*intermediate)
experts.<span class="hljs-number">0</span><span class="hljs-selector-class">.w3</span><span class="hljs-selector-class">.weight</span> │ + Concatenate
experts.<span class="hljs-number">1</span><span class="hljs-selector-class">.w3</span><span class="hljs-selector-class">.weight</span> ─┘<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="modele-compuse-eg-viziune-limbaj" 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="#modele-compuse-eg-viziune-limbaj"><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>Modele compuse (e.g., viziune-limbaj)</span></h3><!--]--><!----> <p>Un <code>PreTrainedModel</code> poate conține alte sub-module <code>PreTrainedModel</code>. Fiecare sub-model poate avea propria sa mapare de conversie, înregistrată fie împotriva numelui clasei sale, fie împotriva <code>model_type</code>-ului. Când modelul părinte este încărcat, <code>get_model_conversion_mapping</code> parcurge sub-modelele în ordine depth-first, colectează mapările lor și <strong>aplică automat scope-ul</strong> fiecărei transformări la calea sub-modulului său prin <code>scope_prefix</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=" "><!---->Model compus: Mapări per-<span class="hljs-keyword">submodel </span>(auto-<span class="hljs-keyword">scoped):
</span><span class="hljs-keyword">LlavaForConditionalGeneration
</span>├── vision_model: SiglipVisionModel → mapare SiglipVisionModel (<span class="hljs-keyword">scope=&quot;vision_model&quot;)
</span>└── language_model: <span class="hljs-keyword">LlamaForCausalLM </span> → mapare <span class="hljs-keyword">LlamaForCausalLM </span> (<span class="hljs-keyword">scope=&quot;language_model&quot;)</span><!----></pre></div><!----> <p><code>scope_prefix</code> este calea punctată a sub-modulului (<code>"vision_model"</code>, <code>"language_model.model"</code>, etc.). O transformare cu scope se declanșează doar pentru cheile care încep cu <code>f"{scope_prefix}."</code>; prefixul este eliminat înainte de potrivirea pattern-ului și re-atașat după substituire, astfel că maparea fiecărui sub-model este scrisă <em>relativ la sub-model</em>, exact ca și cum ar fi root-ul.</p> <!--[1--><h2 class="relative group"><a id="arhitectură" 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="#arhitectură"><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>Arhitectură</span></h2><!--]--><!----> <p>Sistemul este construit în jurul mai multor componente cheie definite în <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/core_model_loading.py" rel="nofollow"><code>core_model_loading.py</code></a>:</p> <p><strong>Faza 1 — Procesare per-cheie</strong> (iterează peste cheile checkpoint-ului):</p> <ol><li><strong>Parcurge lista de transformări</strong> o singură dată. Fiecare <code>WeightRenaming</code> care se potrivește se declanșează (e.g. <code>block_sparse_moe</code><code>mlp</code>), iar <strong>cel mult un</strong> <code>WeightConverter</code> poate revendica cheia (e.g. <code>experts.*.w1.weight</code>).</li> <li><strong>Sharding (TP) și trimite pe dispozitiv</strong> asincron prin <code>ThreadPoolExecutor</code>.</li> <li><strong>Colectează</strong> tensorii cu același <code>source_pattern</code> împreună (e.g. toate weights ale experților MoE, proiecțiile gate + up).</li></ol> <p><strong>Faza 2 — Procesare per-mapare</strong> (iterează peste mapările colectate):</p> <ol><li><strong>Dequantization/deserializare</strong> (doar pentru checkpoint-uri pre-quantized).</li> <li><strong>Aplică lanțul <code>ConversionOps</code></strong>: <code>Chunk</code>, <code>Concatenate</code>, <code>MergeModulelist</code>, <code>Transpose</code>, etc.</li> <li><strong>Quantization</strong> din mers (dacă nu este pre-quantized).</li> <li><strong>Setează parametrul</strong> pe model.</li></ol> <!--[2--><h3 class="relative group"><a id="weighttransform" 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="#weighttransform"><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>WeightTransform</span></h3><!--]--><!----> <p>Clasa de bază care gestionează potrivirea pattern-urilor și colectarea tensorilor:</p> <ul><li><strong>Compilarea pattern-urilor</strong>: Pattern-urile sursă sunt expresii regulate complete potrivite cu <code>re.search()</code>. Wildcardul <code>*</code> potrivește orice componentă indexabilă și grupează toate potrivirile împreună pentru operații batch.</li> <li><strong>Grupuri captante și referințe inverse</strong>: Grupurile captante din pattern-urile sursă pot fi referențiate ca <code>\1</code>, <code>\2</code>, … în pattern-urile țintă pentru a păstra subșiruri (e.g. indici de straturi) în cadrul redenumirilor.</li> <li><strong>Scoping</strong>: <code>scope_prefix</code> (setat automat per sub-model de <code>get_model_conversion_mapping</code>) restricționează transformarea la cheile de sub acea cale. Prefixul este eliminat înainte de potrivire și re-atașat după substituire.</li> <li><strong>Redenumirea cheilor</strong>: <code>rename_source_key()</code> aplică regex-ul (cu gestionare de scope) și returnează <code>(renamed_key, source_pattern)</code> astfel că loader-ul știe care convertor, dacă există, a revendicat cheia.</li> <li><strong>Colectarea tensorilor</strong>: <code>add_tensor()</code> acumulează tensorii rezolvați (sau <code>Future</code>-uri) sub <code>source_pattern</code>-ul lor astfel că toți tensorii necesari pentru o singură conversie (e.g. toate weights ale experților MoE) sunt grupați împreună înainte de rularea lanțului de operații.</li> <li><strong>Reversibilitate</strong>: <code>reverse_transform()</code> inversează pattern-urile sursă ↔ țintă și inversează fiecare operație, astfel că aceeași listă inversată gestionează salvarea.</li> <li><strong>Urmărire</strong>: <code>was_used()</code> raportează dacă transformarea a potrivit efectiv vreo cheie la încărcare; aceasta este necesară pentru ca redenumirile non-bijective (e.g. <code>PrefixChange</code> adăugând un prefix care poate fi deja prezent) să poată fi re-aplicate simetric la salvare.</li></ul> <!--[2--><h3 class="relative group"><a id="weightrenaming" 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="#weightrenaming"><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>WeightRenaming</span></h3><!--]--><!----> <p><code>WeightRenaming</code> este un <code>WeightTransform</code> specializat pentru redenumiri pure de chei fără operații asupra tensorilor. Spre deosebire de <code>WeightConverter</code>, un <code>WeightRenaming</code> nu <strong>revendică</strong> cheia, astfel că mai multe redenumiri pot fi înlănțuite liber. Pe calea de încărcare, toate redenumirile rulează înainte de convertor; pe calea de salvare (lista inversată), toate redenumirile inversate rulează după convertorul inversat — a se vedea regula de ordonare de mai sus.</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-py "><!----><span class="hljs-comment"># Compatibilitate cu checkpoint-uri vechi</span>
WeightRenaming(<span class="hljs-string">&quot;LayerNorm.gamma&quot;</span>, <span class="hljs-string">&quot;LayerNorm.weight&quot;</span>)
<span class="hljs-comment"># Modificări ale path-ului modulului</span>
WeightRenaming(<span class="hljs-string">&quot;.block_sparse_moe.&quot;</span>, <span class="hljs-string">&quot;.mlp.&quot;</span>)<!----></pre></div><!----> <p><code>PrefixChange</code> este un wrapper de nivel înalt în jurul <code>WeightRenaming</code> pentru cazul comun de eliminare sau adăugare a unei întregi componente de cale. Opționalul <code>model_prefix</code> aplică scope-ul operației la cheile din acel namespace:</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-py "><!----><span class="hljs-comment"># &quot;model.layers.bad_prefix.weight&quot;&quot;model.layers.weight&quot;</span>
PrefixChange(prefix_to_remove=<span class="hljs-string">&quot;bad_prefix&quot;</span>, model_prefix=<span class="hljs-string">&quot;model.layers&quot;</span>)
<span class="hljs-comment"># &quot;layers.0.weight&quot;&quot;model.layers.0.weight&quot;</span>
PrefixChange(prefix_to_add=<span class="hljs-string">&quot;model&quot;</span>)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="weightconverter" 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="#weightconverter"><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>WeightConverter</span></h3><!--]--><!----> <p><code>WeightConverter</code> extinde <code>WeightTransform</code> cu un lanț de <code>ConversionOps</code> care acționează asupra tensorilor colectați. Cele patru cardinalități suportate sunt:</p> <table><thead><tr><th>Cardinalitate</th><th>Pattern-uri sursă</th><th>Pattern-uri țintă</th><th>Operație tipică</th></tr></thead><tbody><tr><td>unu-la-unu</td><td>1</td><td>1</td><td><code>Transpose</code>, <code>PermuteForRope</code></td></tr><tr><td>unu-la-mulți</td><td>1</td><td>>1</td><td><code>Chunk</code> (e.g. despachetare <code>qkv_proj</code>)</td></tr><tr><td>mulți-la-unu</td><td>>1</td><td>1</td><td><code>Concatenate</code>, <code>MergeModulelist</code> (e.g. fuzionarea experților)</td></tr><tr><td>mulți-la-mulți</td><td>>1</td><td>>1</td><td>doar cu operații care îl suportă explicit (e.g. <code>ErnieFuseAndSplitTextVisionExperts</code>)</td></tr></tbody></table> <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 "><!---->WeightConverter(
source_patterns=[<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>, <span class="hljs-string">&quot;.experts.*.w3.weight&quot;</span>],
target_patterns=<span class="hljs-string">&quot;.experts.gate_up_proj&quot;</span>,
operations=[MergeModulelist(dim=<span class="hljs-number">0</span>), Concatenate(dim=<span class="hljs-number">1</span>)],
)<!----></pre></div><!----> <p>Un <code>WeightConverter</code> este de asemenea reversibil: <code>reverse_transform()</code> inversează sursă ↔ țintă și înlocuiește fiecare operație cu <code>reverse_op</code>-ul său, astfel că o mapare de conversie înregistrată este bidirecțională prin construcție (încărcarea folosește lista ca atare, salvarea o folosește inversată).</p> <!--[2--><h3 class="relative group"><a id="ordonarea-redenumirilor-și-convertoarelor" 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="#ordonarea-redenumirilor-și-convertoarelor"><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>Ordonarea redenumirilor și convertoarelor</span></h3><!--]--><!----> <p>Listează intrările <code>WeightRenaming</code> înaintea intrărilor <code>WeightConverter</code> și menține frunzele lor disjuncte: redenumirile normalizează cheile pe care le consumă convertoarele, dar nu țintesc niciodată o frunză pe care un convertor o produce. Calea de salvare se bazează pe aceasta pentru a inversa maparea în două faze (convertoare inverse, apoi redenumiri inverse).</p> <p>Lista de transformări este parcursă în ordine, o singură dată per cheie de checkpoint. Pentru fiecare cheie:</p> <ul><li>Fiecare <code>WeightRenaming</code> care se potrivește se declanșează; mai multe redenumiri pot fi înlănțuite.</li> <li>Primul <code>WeightConverter</code> care se potrivește <strong>revendică</strong> cheia. Convertoarele ulterioare sunt omise — aceasta garantează că tensorul este direcționat către un singur convertor pentru pasul de merge/split și este motivul pentru care două convertoare cu intenție suprapusă ar reprezenta o configurare greșită.</li></ul> <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 "><!---->weight_mapping = [
WeightRenaming(<span class="hljs-string">&quot;^old_prefix&quot;</span>, <span class="hljs-string">&quot;encoder&quot;</span>), <span class="hljs-comment"># redenumirea rulează mereu</span>
WeightConverter( <span class="hljs-comment"># convertorul revendică cheia</span>
<span class="hljs-string">&quot;attn.qkv_proj.weight&quot;</span>,
[<span class="hljs-string">&quot;attn.q_proj.weight&quot;</span>, <span class="hljs-string">&quot;attn.k_proj.weight&quot;</span>, <span class="hljs-string">&quot;attn.v_proj.weight&quot;</span>],
operations=[Chunk(dim=<span class="hljs-number">0</span>)],
),
]
<span class="hljs-comment"># Încărcare: &quot;old_prefix.attn.qkv_proj.weight&quot;</span>
<span class="hljs-comment"># → WeightRenaming → &quot;encoder.attn.qkv_proj.weight&quot;</span>
<span class="hljs-comment"># → WeightConverter → &quot;encoder.attn.{q,k,v}_proj.weight&quot;</span>
<span class="hljs-comment">#</span>
<span class="hljs-comment"># Salvare: lista inversată, fiecare transformare inversată:</span>
<span class="hljs-comment"># → rev(WeightConverter) reîmpachetează QKV → &quot;encoder.attn.qkv_proj.weight&quot;</span>
<span class="hljs-comment"># → rev(WeightRenaming) corectează prefixul → &quot;old_prefix.attn.qkv_proj.weight&quot;</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="operații-de-conversie" 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="#operații-de-conversie"><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>Operații de conversie</span></h2><!--]--><!----> <p>Clasa <code>WeightConverter</code> are mai multe operații care sunt executate când este apelat <code>from_pretrained()</code> pentru transformarea tensorilor sursă din checkpoint în tensorii țintă ai modelului.</p> <p>Operațiile sunt complet reversibile. Salvarea inversează conversiile și returnează checkpoint-ul original, astfel că poți lucra ușor cu diferite framework-uri.</p> <table><thead><tr><th>Operație</th><th>Inversă</th></tr></thead><tbody><tr><td><code>Chunk(dim)</code></td><td><code>Concatenate(dim)</code></td></tr><tr><td><code>Concatenate(dim)</code></td><td><code>Chunk(dim)</code></td></tr><tr><td><code>MergeModulelist(dim)</code></td><td><code>SplitModulelist(dim)</code></td></tr><tr><td><code>SplitModulelist(dim)</code></td><td><code>MergeModulelist(dim)</code></td></tr><tr><td><code>Transpose(d0, d1)</code></td><td><code>Transpose(d1, d0)</code></td></tr><tr><td><code>PermuteForRope()</code></td><td><code>PermuteForRope()</code></td></tr><tr><td><code>Conv3dToLinear(...)</code></td><td><code>LinearToConv3d(...)</code></td></tr></tbody></table> <!--[2--><h3 class="relative group"><a id="chunk" 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="#chunk"><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>Chunk</span></h3><!--]--><!----> <p>Operația <code>Chunk</code> împarte un tensor în părți egale de-a lungul unei dimensiuni. De exemplu, dacă un model se așteaptă ca Q, K și V să fie trei tensori separați în loc de un singur tensor.</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-py "><!---->WeightConverter(
<span class="hljs-string">&quot;self_attn.qkv_proj&quot;</span>,
[<span class="hljs-string">&quot;self_attn.q_proj&quot;</span>, <span class="hljs-string">&quot;self_attn.k_proj&quot;</span>, <span class="hljs-string">&quot;self_attn.v_proj&quot;</span>],
operations=[Chunk(dim=<span class="hljs-number">0</span>)],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="concatenate" 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="#concatenate"><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>Concatenate</span></h3><!--]--><!----> <p>Operația <code>Concatenate</code> fuzionează tensori separați într-un singur tensor. De exemplu, dacă un model se așteaptă ca Q, K și V să fie un singur tensor în loc de tensori separați.</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-py "><!---->WeightConverter(
[<span class="hljs-string">&quot;self_attn.q_proj&quot;</span>, <span class="hljs-string">&quot;self_attn.k_proj&quot;</span>, <span class="hljs-string">&quot;self_attn.v_proj&quot;</span>],
<span class="hljs-string">&quot;self_attn.qkv_proj&quot;</span>,
operations=[Concatenate(dim=<span class="hljs-number">0</span>)],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="mergemodulelist" 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="#mergemodulelist"><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>MergeModulelist</span></h3><!--]--><!----> <p><code>MergeModulelist</code> îmbină o listă de tensori 2D într-un singur tensor 3D. De exemplu, poți compune <code>MergeModulelist</code> cu <code>Concatenate</code> pentru a stiva experții dintr-un MoE și a-i împacheta într-un singur tensor.</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-py "><!---->WeightConverter(
[<span class="hljs-string">&quot;block_sparse_moe.experts.*.w1.weight&quot;</span>, <span class="hljs-string">&quot;block_sparse_moe.experts.*.w3.weight&quot;</span>],
<span class="hljs-string">&quot;mlp.experts.gate_up_proj&quot;</span>,
operations=[MergeModulelist(dim=<span class="hljs-number">0</span>), Concatenate(dim=<span class="hljs-number">1</span>)],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="splitmodulelist" 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="#splitmodulelist"><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>SplitModulelist</span></h3><!--]--><!----> <p><code>SplitModulelist</code> împarte un tensor 3D înapoi într-o listă de tensori 2D. De exemplu, poți împărți o stivă de experți înapoi în experți individuali.</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-py "><!---->WeightConverter(
<span class="hljs-string">&quot;mlp.experts.down_proj&quot;</span>,
<span class="hljs-string">&quot;block_sparse_moe.experts.*.w2.weight&quot;</span>,
operations=[SplitModulelist(dim=<span class="hljs-number">0</span>)],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="permuteforrope" 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="#permuteforrope"><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>PermuteForRope</span></h3><!--]--><!----> <p><code>PermuteForRope</code> convertește weights din formatul intercalat pentru a folosi formatul sin/cos. De exemplu, poți compune <code>Chunk</code> cu <code>PermuteForRope</code> pentru a împărți un tensor QKV fuzionat și a aplica permutarea sin/cos RoPE la Q și K.</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-py "><!---->WeightConverter(
[<span class="hljs-string">&quot;model.layers.*.self_attn.qkv_proj.weight&quot;</span>],
[
<span class="hljs-string">&quot;model.layers.*.self_attn.q_proj.weight&quot;</span>,
<span class="hljs-string">&quot;model.layers.*.self_attn.k_proj.weight&quot;</span>,
<span class="hljs-string">&quot;model.layers.*.self_attn.v_proj.weight&quot;</span>,
],
operations=[Chunk(dim=<span class="hljs-number">0</span>), PermuteForRope()],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="transpose" 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="#transpose"><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>Transpose</span></h3><!--]--><!----> <p><code>Transpose</code> inversează dimensiunile unui tensor. Util pentru conversia layout-urilor de weights între diferite convenții.</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-py "><!---->WeightConverter(
source_patterns=<span class="hljs-string">&quot;mlp.gate.weight&quot;</span>,
target_patterns=<span class="hljs-string">&quot;mlp.text_moe.gate.weight&quot;</span>,
operations=[Transpose(dim0=<span class="hljs-number">0</span>, dim1=<span class="hljs-number">1</span>)],
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="înlănțuirea-operațiilor" 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="#înlănțuirea-operațiilor"><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>Înlănțuirea operațiilor</span></h2><!--]--><!----> <p>Operațiile pot fi înlănțuite pentru a efectua transformări complexe. Operațiile se execută în ordine, ieșirea fiecărei operații devenind intrarea operației următoare.</p> <!--[2--><h3 class="relative group"><a id="exemplu-conversia-moe-mixtral" 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-conversia-moe-mixtral"><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: Conversia MoE Mixtral</span></h3><!--]--><!----> <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 "><!---->WeightConverter(
source_patterns=[
<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>, <span class="hljs-comment"># gate_proj per expert</span>
<span class="hljs-string">&quot;.experts.*.w3.weight&quot;</span>, <span class="hljs-comment"># up_proj per expert</span>
],
target_patterns=<span class="hljs-string">&quot;.experts.gate_up_proj&quot;</span>,
operations=[
MergeModulelist(dim=<span class="hljs-number">0</span>), <span class="hljs-comment"># Stivuiește toți experții: (n_experts, in, out)</span>
Concatenate(dim=<span class="hljs-number">1</span>), <span class="hljs-comment"># Fuzionează gate+up: (n_experts, in, 2*out)</span>
],
)<!----></pre></div><!----> <p><strong>Fluxul de date:</strong></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=" "><!---->Intrare:
<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>: [tensor_0, tensor_1, ..., tensor_7] <span class="hljs-meta"># <span class="hljs-number">8</span> experți</span>
<span class="hljs-string">&quot;.experts.*.w3.weight&quot;</span>: [tensor_0, tensor_1, ..., tensor_7] <span class="hljs-meta"># <span class="hljs-number">8</span> experți</span>
<span class="hljs-keyword">Dup</span>ă MergeModulelist(<span class="hljs-keyword">dim</span>=<span class="hljs-number">0</span>):
<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>: (<span class="hljs-number">8</span>, <span class="hljs-number">4096</span>, <span class="hljs-number">14336</span>) <span class="hljs-meta"># gate stivuit</span>
<span class="hljs-string">&quot;.experts.*.w3.weight&quot;</span>: (<span class="hljs-number">8</span>, <span class="hljs-number">4096</span>, <span class="hljs-number">14336</span>) <span class="hljs-meta"># up stivuit</span>
<span class="hljs-keyword">Dup</span>ă Concatenate(<span class="hljs-keyword">dim</span>=<span class="hljs-number">1</span>):
<span class="hljs-string">&quot;.experts.gate_up_proj&quot;</span>: (<span class="hljs-number">8</span>, <span class="hljs-number">4096</span>, <span class="hljs-number">28672</span>) <span class="hljs-meta"># gate_up fuzionat</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="reguli-de-potrivire-a-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="#reguli-de-potrivire-a-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>Reguli de potrivire a pattern-urilor</span></h3><!--]--><!----> <ul><li>Pattern-urile sursă sunt regex-uri complete evaluate cu <code>re.search()</code>. <code>^</code> ancorează la începutul cheii <strong>cu scope</strong> (după eliminarea <code>scope_prefix</code>).</li> <li><code>*</code> este un wildcard per-index care colectează toți tensorii potriviți sub același pattern sursă (păstrând ordinea checkpoint-ului pentru concatenare corectă).</li> <li>Grupurile captante din pattern-urile sursă pot fi referențiate ca <code>\1</code>, <code>\2</code>, … în pattern-urile țintă pentru a păstra părți ale cheii originale (e.g. indici de straturi).</li> <li>Transformările cu scope (<code>scope_prefix</code> setat) potrivesc doar cheile care încep cu <code>f"{scope_prefix}."</code>.</li></ul> <!--[1--><h2 class="relative group"><a id="înregistrarea-unei-mapări-de-conversie" 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-unei-mapări-de-conversie"><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 unei mapări de conversie</span></h2><!--]--><!----> <p>O listă de conversie este înregistrată împotriva unei chei string — fie un <code>model_type</code> (e.g. <code>"mixtral"</code>) fie un nume de clasă (e.g. <code>"LlavaModel"</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.conversion_mapping <span class="hljs-keyword">import</span> register_checkpoint_conversion_mapping
register_checkpoint_conversion_mapping(
<span class="hljs-string">&quot;my_model_type&quot;</span>,
[
WeightRenaming(<span class="hljs-string">&quot;.old.&quot;</span>, <span class="hljs-string">&quot;.new.&quot;</span>),
WeightConverter(
<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>,
<span class="hljs-string">&quot;.experts.gate_proj&quot;</span>,
operations=[MergeModulelist(dim=<span class="hljs-number">0</span>)],
),
],
)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="reguli-de-căutare" 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="#reguli-de-căutare"><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>Reguli de căutare</span></h3><!--]--><!----> <p>Când <code>get_model_conversion_mapping</code> procesează un <code>PreTrainedModel</code>, fiecare sub-<code>PreTrainedModel</code> este vizitat în ordine DFS (<code>nn.Module.named_modules()</code> filtrat la instanțe <code>PreTrainedModel</code>). Pentru fiecare:</p> <ol><li><strong>Căutarea după numele clasei este încercată prima</strong>, apoi <code>model_type</code>. Dacă ambele sunt înregistrate pentru același modul, <strong>maparea după numele clasei câștigă</strong> și cea <code>model_type</code> este ignorată pentru acel modul — aceasta permite unui cap de sarcină (e.g. <code>LlavaForConditionalGeneration</code>) să suprascrie baseline-ul <code>model_type</code> partajat (<code>"llava"</code>).</li> <li>Maparea selectată are <code>scope_prefix</code> setat la calea punctată a sub-modulului (<code>""</code> pentru root).</li> <li><strong>Deduplicarea bazată pe strămoș</strong> decide dacă se păstrează maparea: <ul><li>Dacă un path <strong>ancestor</strong> a revendicat deja același identificator (numele clasei sau <code>model_type</code>), sub-modulul este <strong>omis</strong> — maparea fără scope sau cu scope mai înalt a strămoșului acoperă deja acest subarbore.</li> <li>Dacă doar un <strong>sibling</strong> l-a revendicat, sub-modulul este <strong>păstrat</strong> cu propriul <code>scope_prefix</code>. Fiecare sibling primește propria mapare cu scope.</li></ul></li></ol> <p>Listele văzute pentru numele clasei și <code>model_type</code> sunt urmărite separat, cu o subtilitate: când un modul este potrivit <strong>prin numele clasei</strong>, <code>model_type</code>-ul său <em>nu</em> este adăugat la lista văzută. Aceasta este pentru ca alte module care partajează același <code>model_type</code> dar fără o mapare specifică clasei (e.g. <code>DetrModel</code> sub <code>DetrForSegmentation</code>) să rămână accesibile prin căutarea <code>model_type</code>.</p> <!--[2--><h3 class="relative group"><a id="mapări-bazate-pe-clasă-vs-aliasuri-modeltype" 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="#mapări-bazate-pe-clasă-vs-aliasuri-modeltype"><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>Mapări bazate pe clasă vs aliasuri model_type</span></h3><!--]--><!----> <p>Ambele stiluri coexistă în <code>_MODEL_TO_CONVERSION_PATTERN</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 "><!---->_MODEL_TO_CONVERSION_PATTERN = {
<span class="hljs-comment"># aliasuri model_type (căutare după config.model_type)</span>
<span class="hljs-string">&quot;minimax&quot;</span>: <span class="hljs-string">&quot;mixtral&quot;</span>,
<span class="hljs-string">&quot;qwen3_moe&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
<span class="hljs-string">&quot;mistral3&quot;</span>: <span class="hljs-string">&quot;llava&quot;</span>,
<span class="hljs-comment"># aliasuri după numele clasei (căutare după type(submodule).__name__)</span>
<span class="hljs-string">&quot;PaliGemmaModel&quot;</span>: <span class="hljs-string">&quot;LlavaModel&quot;</span>,
<span class="hljs-string">&quot;MaskFormerDetrDecoder&quot;</span>: <span class="hljs-string">&quot;DetrModel&quot;</span>,
...
}<!----></pre></div><!----> <p>Cheile după numele clasei sunt preferate când <code>model_type</code>-ul este partajat, dar o clasă specifică necesită comportament diferit.</p> <!--[1--><h2 class="relative group"><a id="integrarea-cu-tensor-parallelism" 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="#integrarea-cu-tensor-parallelism"><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>Integrarea cu tensor parallelism</span></h2><!--]--><!----> <p>Sistemul de încărcare dinamică se integrează cu tensor parallelism (TP) prin ierarhia <code>TensorParallelLayer</code> definită în <code>src/transformers/integrations/tensor_parallel.py</code>.</p> <p>Când TP este activat, tensorii sunt fragmentați <strong>în timpul</strong> materializării, nu după. Aceasta înseamnă că fiecare rang încarcă doar porțiunea de tensor de care are nevoie.</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">def</span> <span class="hljs-title function_">spawn_tp_materialize</span>(<span class="hljs-params">thread_pool, tensor, sharding_method, tensor_idx, device, dtype</span>):
<span class="hljs-keyword">def</span> <span class="hljs-title function_">_job</span>():
<span class="hljs-keyword">return</span> sharding_method.shard_tensor(tensor, tensor_idx=tensor_idx, device=device, dtype=dtype)
<span class="hljs-keyword">return</span> thread_pool.submit(_job)<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="stiluri-de-paralelism-disponibile" 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="#stiluri-de-paralelism-disponibile"><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>Stiluri de paralelism disponibile</span></h3><!--]--><!----> <table><thead><tr><th>Stil</th><th>Dimensiune Fragment Weight</th><th>Descriere</th></tr></thead><tbody><tr><td><code>colwise</code></td><td>-2</td><td>Column-wise: caracteristici de ieșire fragmentate</td></tr><tr><td><code>rowwise</code></td><td>-1</td><td>Row-wise: caracteristici de intrare fragmentate</td></tr><tr><td><code>packed_colwise</code></td><td>-2</td><td>Pentru weights fuzionate (gate_up_proj)</td></tr><tr><td><code>packed_rowwise</code></td><td>-1</td><td>Pentru weights fuzionate</td></tr><tr><td><code>embedding_rowwise</code></td><td>0</td><td>Paralelism vocabular</td></tr><tr><td><code>grouped_gemm</code></td><td>0</td><td>Paralelism expert pentru MoE</td></tr><tr><td><code>sequence_parallel</code></td><td>None</td><td>Fără fragmentare de weights</td></tr></tbody></table> <!--[2--><h3 class="relative group"><a id="gestionarea-weights-împachetate" 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="#gestionarea-weights-împachetate"><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>Gestionarea weights împachetate</span></h3><!--]--><!----> <p>Pentru weights fuzionate precum <code>gate_up_proj</code>, este necesară atenție specială pentru fragmentarea 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-keyword">def</span> <span class="hljs-title function_">get_packed_weights</span>(<span class="hljs-params">param, empty_param, device_mesh, rank, dim</span>):
<span class="hljs-string">&quot;&quot;&quot;
Interleaves gate and up shards correctly.
Packed tensor: [G0 G1 G2 G3 | U0 U1 U2 U3]
With TP=2:
- Rank 0 gets: [G0 G1 | U0 U1]
- Rank 1 gets: [G2 G3 | U2 U3]
&quot;&quot;&quot;</span><!----></pre></div><!----> <p>Operația TP este stocată în <code>WeightTransform</code> și aplicată după operațiile de conversie:</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">if</span> matched_tp_pattern := tp_plan_alt.search(renamed_key):
tp_layer = ALL_PARALLEL_STYLES[model.tp_plan[matched_tp_pattern]]
mapping.distributed_operation = tp_layer(
device_mesh=device_mesh,
rank=device_mesh.get_local_rank(),
empty_param=empty_param.clone()
)<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="integrarea-cu-quantization" 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="#integrarea-cu-quantization"><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>Integrarea cu quantization</span></h2><!--]--><!----> <p>Quantization se integrează în pipeline-ul de încărcare în două moduri, în funcție de dacă checkpoint-ul este deja quantized:</p> <ul><li><strong>Checkpoint-uri pre-quantized</strong>: Quantizer-ul furnizează instanțe <code>WeightConverter</code> (prin <code>get_weight_conversions()</code>) care deserializează tensorii quantizați. Dtype-urile checkpoint-ului sunt păstrate pentru a evita cast-urile nedorite.</li> <li><strong>Quantization din mers</strong>: Quantizer-ul furnizează o operație de quantization care este aplicată după operațiile de conversie, aplicând-o pe weights pe măsură ce sunt încărcate.</li></ul> <p>Quantizer-ul poate, de asemenea, rescrie întreaga listă de conversie la sfârșitul <code>get_model_conversion_mapping</code> prin <code>update_weight_conversions(...)</code> — de exemplu, dequantizer-ul FP8 adaugă o operație <code>Fp8Dequantize</code> la începutul fiecărui convertor existent, astfel că scalele per-bloc sunt aplicate <em>înainte</em> ca orice operații de expert-merge/concat să aplatizeze structura per-expert.</p> <!--[1--><h2 class="relative group"><a id="încărcarea-rapidă-și-eficientă-a-modelelor" 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="#încărcarea-rapidă-și-eficientă-a-modelelor"><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>Încărcarea rapidă și eficientă a modelelor</span></h2><!--]--><!----> <p>Încărcarea unui model este mai rapidă și utilizează mai puțină memorie deoarece loader-ul știe ce tensori sunt necesari pentru operații și le planifică materializarea leneș.</p> <p>Loader-ul scanează checkpoint-ul <em>o singură dată</em> pentru a descoperi potriviri de pattern și a colecta tensori. Îi stochează ca obiecte <code>Future</code> și îi trimite unui pool de thread-uri pentru încărcare asincronă fără a bloca GIL-ul. Un parametru începe să se încarce imediat ce un thread devine disponibil pentru el.</p> <p>Dacă sistemul tău rulează alte procese grele, mai multe thread-uri pot încetini încărcarea în loc să o accelereze. În acest caz, setează variabila de mediu <code>HF_DEACTIVATE_ASYNC_LOAD=1</code> pentru a încărca weights secvențial.</p> <blockquote class="note"><p>Implicit sunt 4 thread-uri pentru încărcarea asincronă a parametrilor. Aceasta oferă cel mai bun compromis pentru scenariile de încărcare și hardware. Munca este în mare parte limitată de I/O, dar în funcție de hardware-ul acceleratorului și de <code>dtype</code>-ul necesar la încărcare, poate deveni limitată de CPU/GPU dacă <code>dtype</code>-ul diferă de cel serializat (aceasta necesită o operație suplimentară de copiere).</p></blockquote> <!--[2--><h3 class="relative group"><a id="încărcare-asincronă-vs-sincronă" 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="#încărcare-asincronă-vs-sincronă"><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>Încărcare asincronă vs sincronă</span></h3><!--]--><!----> <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">def</span> <span class="hljs-title function_">spawn_materialize</span>(<span class="hljs-params">thread_pool, tensor, device, dtype</span>) -&gt; Future | <span class="hljs-type">Callable</span>:
<span class="hljs-keyword">def</span> <span class="hljs-title function_">_job</span>():
<span class="hljs-keyword">return</span> _materialize_copy(tensor, device, dtype)
<span class="hljs-keyword">if</span> thread_pool <span class="hljs-keyword">is</span> <span class="hljs-keyword">not</span> <span class="hljs-literal">None</span>:
<span class="hljs-keyword">return</span> thread_pool.submit(_job) <span class="hljs-comment"># Async: returnează Future</span>
<span class="hljs-keyword">else</span>:
<span class="hljs-keyword">return</span> _job <span class="hljs-comment"># Sync: returnează Callable (execuție amânată)</span><!----></pre></div><!----> <p>Încărcarea sincronă este utilizată când:</p> <ul><li>Variabila de mediu <code>HF_DEACTIVATE_ASYNC_LOAD=1</code> este setată.</li> <li>Descărcarea pe disc este activată (constrângerile de memorie necesită încărcare secvențială).</li> <li>Quantization din mers este activat (evită thread-urile worker care depășesc pasul de quantization).</li></ul> <!--[2--><h3 class="relative group"><a id="fluxul-de-materializare" 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="#fluxul-de-materializare"><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>Fluxul de materializare</span></h3><!--]--><!----> <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=" "><!----><span class="hljs-attribute">1. Iterarea checkpoint-ului (Faza 1)</span><span class="hljs-punctuation">:</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Pentru fiecare cheie, parcurge lista de transformări o singură dată</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Trimite job-ul de materializare la ThreadPoolExecutor</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Job-ul returnează Future (async) sau Callable (sync)</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Colectează în WeightConverter / WeightRenaming potrivit</span>
<span class="hljs-attribute">2. Procesarea per-mapare (Faza 2, câte o mapare pe rând)</span><span class="hljs-punctuation">:</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">materialize_tensors() așteaptă doar Future-urile acestei mapări</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Aplică lanțul de operații de conversie (self.operations)</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Aplică operația de quantizare (dacă din mers)</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Setează parametrii pe model</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Șterge tensorii realizați imediat</span>
<span class="hljs-attribute">3. Curățare</span><span class="hljs-punctuation">:</span>
<span class="hljs-bullet">-</span> <span class="hljs-string">Oprirea pool-ului de thread-uri (cu cancel_futures=True pentru întreruperi)</span><!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="eficiența-memoriei" 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="#eficiența-memoriei"><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>Eficiența memoriei</span></h3><!--]--><!----> <p>La conversia unui weight, convertorul așteaptă ca toți tensorii necesari să se materializeze dacă nu s-au încărcat încă. De exemplu, operația <code>MergeModulelist</code> necesită ca toate weights din <code>ModuleList</code> să fie încărcate înainte de îmbinare.</p> <p>Concatenarea tensorilor necesită o copie temporară, astfel că operații precum <code>MergeModulelist</code> și <code>Concatenate</code> necesită de 2 ori memoria tensorilor de bază în timpul conversiei. Odată îmbinați, în memorie rămâne doar tensorul rezultat. Vârful teoretic maxim de memorie este dimensiunea modelului plus tensorii necesari pentru cea mai mare operație <code>MergeModulelist</code> sau <code>Concatenate</code>.</p> <p>Acest caz cel mai rău apare doar când toți ceilalți parametri s-au încărcat înainte de rularea conversiei exigente. Două scenarii declanșează aceasta.</p> <ol><li>Toți parametrii s-au încărcat asincron înainte de intrarea în conversia exigentă (pool-ul de thread-uri a fost mai rapid decât coada de conversie).</li> <li>Conversia exigentă este ultima.</li></ol> <p>De exemplu, pentru un model MoE care utilizează <code>MergeModulelist</code> pentru experți pe fiecare strat, vârful teoretic maxim de memorie este dimensiunea modelului plus experții de pe un strat.</p> <p>Aceste scenarii de caz cel mai rău sunt neobișnuite. Vârful real de memorie tinde să rămână aproape de dimensiunea modelului.</p> <!--[1--><h2 class="relative group"><a id="reversibilitate" 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="#reversibilitate"><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>Reversibilitate</span></h2><!--]--><!----> <p>Sistemul suportă salvarea modelelor cu transformările inverse, permițând save/load dus-întors. Salvarea rulează în două faze: convertoare inverse mai întâi (fiecare tensor se potrivește cel mult unuia), apoi redenumiri inverse, conform regulii de ordonare de mai sus.</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">def</span> <span class="hljs-title function_">revert_weight_conversion</span>(<span class="hljs-params">model, state_dict</span>):
<span class="hljs-string">&quot;&quot;&quot;Aplică conversiile inverse pentru salvare.&quot;&quot;&quot;</span>
weight_conversions = <span class="hljs-built_in">getattr</span>(model, <span class="hljs-string">&quot;_weight_conversions&quot;</span>, <span class="hljs-literal">None</span>)
<span class="hljs-comment"># Inversează toate transformările</span>
reverse_weight_conversion = [
conversion.reverse_transform() <span class="hljs-keyword">for</span> conversion <span class="hljs-keyword">in</span> weight_conversions
]
<span class="hljs-comment"># Aplică în ordine inversă</span>
<span class="hljs-keyword">for</span> first_param_name, reversed_converter <span class="hljs-keyword">in</span> conversion_mapping.items():
realized_value = reversed_converter.convert(first_param_name, model=model)<!----></pre></div><!----> <p>Lista de transformări utilizate la momentul încărcării este cached pe model ca <code>_weight_conversions</code> (se păstrează doar intrările care s-au declanșat efectiv, astfel că redenumirile non-bijective precum <code>PrefixChange</code> sunt re-aplicate corect simetric). Când modelul a fost instanțiat fără <code>from_pretrained</code> (și deci nu are <code>_weight_conversions</code>), <code>revert_weight_conversion</code> recurge la recalcularea mapării prin <code>get_model_conversion_mapping</code> și elimină orice <code>PrefixChange</code> din aceasta (nu putem determina dacă checkpoint-ul original avea prefixul).</p> <p>Pattern-urile țintă pot conține elemente regex care necesită procesare pentru direcția inversă:</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">def</span> <span class="hljs-title function_">process_target_pattern</span>(<span class="hljs-params">pattern: <span class="hljs-built_in">str</span></span>) -&gt; <span class="hljs-built_in">tuple</span>[<span class="hljs-built_in">str</span>, <span class="hljs-built_in">str</span> | <span class="hljs-literal">None</span>]:
<span class="hljs-string">&quot;&quot;&quot;
- Removes `^` and `$` anchors
- Removes negative lookahead/lookbehind
- Detects capturing groups, replaces with \1
&quot;&quot;&quot;</span><!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="exemple-reale" 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="#exemple-reale"><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>Exemple reale</span></h2><!--]--><!----> <!--[2--><h3 class="relative group"><a id="moe-în-stil-mixtral" 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="#moe-în-stil-mixtral"><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>MoE în stil Mixtral</span></h3><!--]--><!----> <p><strong>Formatul checkpoint-ului:</strong></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=" "><!----><span class="hljs-title">model</span>.layers.<span class="hljs-number">0</span>.block_sparse_moe.experts.<span class="hljs-number">0</span>.w1.weight <span class="hljs-comment"># gate per expert</span>
<span class="hljs-title">model</span>.layers.<span class="hljs-number">0</span>.block_sparse_moe.experts.<span class="hljs-number">0</span>.w2.weight <span class="hljs-comment"># down per expert</span>
<span class="hljs-title">model</span>.layers.<span class="hljs-number">0</span>.block_sparse_moe.experts.<span class="hljs-number">0</span>.w3.weight <span class="hljs-comment"># up per expert</span>
...
<span class="hljs-title">model</span>.layers.<span class="hljs-number">0</span>.block_sparse_moe.experts.<span class="hljs-number">7</span>.w1.weight<!----></pre></div><!----> <p><strong>Formatul modelului:</strong></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=" "><!----><span class="hljs-attribute">model</span>.layers.<span class="hljs-number">0</span>.mlp.experts.gate_up_proj # (<span class="hljs-number">8</span>, <span class="hljs-number">4096</span>, <span class="hljs-number">28672</span>)
<span class="hljs-attribute">model</span>.layers.<span class="hljs-number">0</span>.mlp.experts.down_proj # (<span class="hljs-number">8</span>, <span class="hljs-number">14336</span>, <span class="hljs-number">4096</span>)<!----></pre></div><!----> <p><strong>Maparea de conversie</strong> (din <code>conversion_mapping.py</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-string">&quot;mixtral&quot;</span>: [
WeightRenaming(<span class="hljs-string">&quot;.block_sparse_moe.&quot;</span>, <span class="hljs-string">&quot;.mlp.&quot;</span>),
WeightConverter(
source_patterns=[<span class="hljs-string">&quot;.experts.*.w1.weight&quot;</span>, <span class="hljs-string">&quot;.experts.*.w3.weight&quot;</span>],
target_patterns=<span class="hljs-string">&quot;.experts.gate_up_proj&quot;</span>,
operations=[MergeModulelist(dim=<span class="hljs-number">0</span>), Concatenate(dim=<span class="hljs-number">1</span>)],
),
WeightConverter(
source_patterns=[<span class="hljs-string">&quot;.experts.*.w2.weight&quot;</span>],
target_patterns=<span class="hljs-string">&quot;.experts.down_proj&quot;</span>,
operations=[MergeModulelist(dim=<span class="hljs-number">0</span>)],
),
],<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="model-compus-viziune-limbaj-gemma3" class="header-link block pr-1.5 text-lg no-hover:hidden with-hover:absolute with-hover:p-1.5 with-hover:opacity-0 with-hover:group-hover:opacity-100 with-hover:right-full" href="#model-compus-viziune-limbaj-gemma3"><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>Model compus viziune-limbaj (Gemma3)</span></h3><!--]--><!----> <p>Gemma3 are configurarea canonică “logică de prefix diferită pentru modelul cap față de modelul de bază”. Două intrări sunt înregistrate (aici prin aliasuri care indică listele partajate <code>"llava"</code> / <code>"LlavaModel"</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-comment"># model_type: se aplică la Gemma3ForConditionalGeneration / Gemma3ForSequenceClassification</span>
<span class="hljs-string">&quot;gemma3&quot;</span>: <span class="hljs-string">&quot;llava&quot;</span> <span class="hljs-comment"># → adaugă &quot;model.&quot; în fața language_model / vision_tower / ...</span>
<span class="hljs-comment"># clasă: se aplică la inner Gemma3Model</span>
<span class="hljs-string">&quot;Gemma3Model&quot;</span>: <span class="hljs-string">&quot;LlavaModel&quot;</span> <span class="hljs-comment"># → redenumire minimă în namespace-ul deja prefixat</span><!----></pre></div><!----> <p>Parcurgere DFS pentru <code>Gemma3ForConditionalGeneration</code>:</p> <ol><li>Root — căutarea după clasă eșuează (<code>Gemma3ForConditionalGeneration</code> nu este înregistrată), căutarea după model_type reușește (<code>"gemma3"</code>) → transformările de rescriere a prefixului <code>"llava"</code> sunt adăugate <strong>fără scope</strong>, punând <code>language_model</code>, <code>vision_tower</code>, <code>multi_modal_projector</code> sub <code>model.*</code>.</li> <li><code>model: Gemma3Model</code> interior — <strong>căutarea după clasă reușește</strong> (<code>Gemma3Model</code><code>LlavaModel</code>) → doar maparea <code>LlavaModel</code> este aplicată, cu scope la <code>"model"</code>. Redenumirile de prefix mult mai largi <code>"gemma3"</code> (=<code>"llava"</code>) <strong>nu</strong> sunt re-aplicate aici, ceea ce este exact ce vrei: modelul interior trăiește într-un namespace unde prefixul este deja corect.</li></ol> <p>Aceeași formă de pereche este utilizată de fiecare VLM din familia Llava (<code>PaliGemma</code>, <code>InternVL</code>, <code>Mistral3</code>, …). Intrarea model_type gestionează chirurgia prefixului modelului cap; intrarea de clasă menține maparea modelului de bază interior minimă.</p> <!--[2--><h3 class="relative group"><a id="imbricare-mai-profundă-cu-suprascrierea-specifică-capului-detr" 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="#imbricare-mai-profundă-cu-suprascrierea-specifică-capului-detr"><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>Imbricare mai profundă cu suprascrierea specifică capului (DETR)</span></h3><!--]--><!----> <p><code>DetrForSegmentation</code> arată o suprascrierea specifică capului stratificată pe deasupra a două niveluri imbricate:</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=" "><!---->DetrForSegmentation (clasă înregistrată: redenumiri specifice segmentării)
├── detr: DetrForObjectDetection (fără mapare<span class="hljs-comment">; doar parcurs)</span>
│ └── model: DetrModel (clasă înregistrată: transformări de <span class="hljs-keyword">bază </span>partajate)
│ └── <span class="hljs-keyword">backbone, </span>encoder, ...
└── mask_head, <span class="hljs-keyword">bbox_attention </span> (weights specifice capului)<!----></pre></div><!----> <p>Sunt implicate două mapări (una per cheie î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-string">&quot;DetrModel&quot;</span>: [WeightRenaming(<span class="hljs-string">&quot;backbone.conv_encoder&quot;</span>, <span class="hljs-string">&quot;backbone&quot;</span>), ...] <span class="hljs-comment"># bază partajată</span>
<span class="hljs-string">&quot;DetrForSegmentation&quot;</span>: [WeightRenaming(<span class="hljs-string">&quot;mask_head.lay1&quot;</span>, <span class="hljs-string">&quot;mask_head.conv1.conv&quot;</span>), ...] <span class="hljs-comment"># specific capului</span><!----></pre></div><!----> <p>Parcurgere DFS:</p> <ol><li>Root <code>DetrForSegmentation</code> se potrivește după clasă → redenumiri de segmentare adăugate <strong>fără scope</strong>.</li> <li><code>detr: DetrForObjectDetection</code> nu este înregistrat → nu se adaugă transformări; DFS continuă în interiorul său.</li> <li><code>detr.model: DetrModel</code> se potrivește după clasă → transformări de bază adăugate cu <code>scope_prefix="detr.model"</code>.</li></ol> <p>De aceea <code>DetrForObjectDetection</code> nu necesită propria mapare: singura mapare înregistrată în subarborele său (<code>DetrModel</code>) este automat aplicată cu scope la calea corectă.</p> <p>Aliasurile cheiate după clasă reutilizează maparea de bază fără înregistrare suplimentară: <code>"MaskFormerDetrDecoder": "DetrModel"</code> face ca un decoder <code>MaskFormer</code> să preia aceleași transformări sub propriul nume de clasă.</p> <!--[2--><h3 class="relative group"><a id="operații-personalizate-ernie-45-vl-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="#operații-personalizate-ernie-45-vl-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>Operații personalizate (ERNIE 4.5 VL MoE)</span></h3><!--]--><!----> <p>Când operațiile integrate nu sunt suficiente, poți crea o subclasă <code>ConversionOps</code> personalizată. De exemplu, ERNIE 4.5 VL MoE trebuie să împartă o listă de experți partajată între modalitățile text și viziune — ceva ce nicio operație integrată singulară nu gestionează. Operația personalizată <code>ErnieFuseAndSplitTextVisionExperts</code> împarte și re-stivuiește experții pe două chei țintă:</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-string">&quot;ernie4_5_vl_moe&quot;</span>: [
WeightRenaming(<span class="hljs-string">&quot;vision_model&quot;</span>, <span class="hljs-string">&quot;vision_tower&quot;</span>),
WeightConverter(
source_patterns=[<span class="hljs-string">&quot;experts.*.down_proj.weight&quot;</span>],
target_patterns=[
<span class="hljs-string">&quot;text_moe.experts.down_proj&quot;</span>,
<span class="hljs-string">&quot;vision_moe.experts.down_proj&quot;</span>,
],
operations=[ErnieFuseAndSplitTextVisionExperts(stack_dim=<span class="hljs-number">0</span>, concat_dim=<span class="hljs-number">1</span>)],
),
],<!----></pre></div><!----> <p>Operațiile personalizate trebuie să implementeze <code>convert()</code> și proprietatea <code>reverse_op</code> pentru a suporta save/load dus-întors.</p> <!--[2--><h3 class="relative group"><a id="aliasuri-de-tip-model" 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="#aliasuri-de-tip-model"><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>Aliasuri de tip model</span></h3><!--]--><!----> <p>Multe modele partajează pattern-uri de conversie:</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_TO_CONVERSION_PATTERN = {
<span class="hljs-string">&quot;mixtral&quot;</span>: <span class="hljs-string">&quot;mixtral&quot;</span>,
<span class="hljs-string">&quot;minimax&quot;</span>: <span class="hljs-string">&quot;mixtral&quot;</span>,
<span class="hljs-string">&quot;qwen2_moe&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
<span class="hljs-string">&quot;deepseek_v2&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
<span class="hljs-string">&quot;deepseek_v3&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
<span class="hljs-string">&quot;qwen3_moe&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
<span class="hljs-string">&quot;olmoe&quot;</span>: <span class="hljs-string">&quot;qwen2_moe&quot;</span>,
...
}<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="reutilizarea-blocurilor-de-construcție-ale-încărcării-dinamice" 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="#reutilizarea-blocurilor-de-construcție-ale-încărcării-dinamice"><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>Reutilizarea blocurilor de construcție ale încărcării dinamice</span></h2><!--]--><!----> <p>Încărcarea dinamică de weights nu se limitează la checkpoint-uri complete ale modelului. Aceleași blocuri de construcție îți permit să încarci <em>orice</em> set de weights atâta timp cât poți descrie cum se mapează cheile checkpoint-ului la parametri și te asiguri că modulele țintă există.</p> <p>La un nivel înalt, contractul arată astfel:</p> <ol><li><strong>Pregătește namespace-ul modelului.</strong> Asigură-te că modulele/parametrii pe care dorești să îi încarci sunt prezenți și numiți în modul în care maparea ta îi va targeta. Pentru adaptori, aceasta înseamnă apelarea <code>inject_adapter_in_model(...)</code> pentru ca modulele adaptor să existe înainte de încărcare. Pentru capete personalizate sau module suplimentare, instanțiază-le mai întâi pe model.</li> <li><strong>Descrie cum să mapezi weights.</strong> Construiește o listă de conversie/redenumire (de exemplu, într-un helper precum <code>_build_peft_weight_mapping(...)</code>) utilizând <code>WeightConverter</code> sau <code>WeightRenaming</code>. Acesta este locul unde exprimi cum trebuie convertite, împărțite, îmbinate sau redenumite cheile checkpoint-ului pentru a se potrivi namespace-ului modelului tău.
Poți face în principal 3 lucruri: <ul><li>adaugă operații la lista de convertoare: acestea vor fi aplicate pe toate weights, cu excepția celor colectate în oricare din <code>WeightConverter</code>. În general, acestea ar trebui să fie operații <code>WeightRenaming</code></li> <li>adaugă operații la lista de operații a fiecărui convertor: aceasta este ceea ce se întâmplă pentru <code>Quantization</code>, unde adăugăm o operație de quantization după lista de operații a oricărui <code>WeightConverter</code>.</li> <li>înlocuiește/mapează operații cu operațiile tale personalizate: aceasta este ceea ce se întâmplă cu <code>peft</code>. Înlocuim operația <code>Concatenate</code> din, să zicem, <code>mixtral</code>, cu <code>PeftConcatenate</code> (care este definită în PEFT). În acest fel, când checkpoint-ul adaptor este citit, weights de concatenat sunt colectate și formatate corespunzător pentru <code>peft</code></li></ul></li> <li><strong>Încarcă + finalizează + raportează.</strong> Folosește loader-ul de bază pentru a efectua conversia și a popula tensorii, apoi finalizează și înregistrează rezultatele. Concret, acest flux este: <ul><li><code>LoadStateDictConfig(...)</code> + <code>_load_pretrained_model(...)</code> pentru a încărca și converti.</li> <li><code>_finalize_load_state_dict(...)</code> pentru a muta tensorii lipsă/nepotriviți de pe <code>meta</code>, a-i inițializa și a lega weights.</li> <li><code>log_state_dict_report(...)</code> pentru a raporta cheile lipsă/neașteptate/nepotrivite (și erorile de conversie).</li></ul></li></ol> <p>Aceste API-uri sunt expuse pentru a-ți permite să gestionezi cod personalizat, formate de weights personalizate, dar și pentru a te asigura că beneficiezi de cea mai înaltă și mai eficientă încărcare de weights, fragmentare și calitate a vieții oferite de API-ul <code>transformers</code>!</p> <!--[1--><h2 class="relative group"><a id="referință-fișiere-cheie" 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="#referință-fișiere-cheie"><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ță fișiere cheie</span></h2><!--]--><!----> <table><thead><tr><th>Fișier</th><th>Scop</th></tr></thead><tbody><tr><td><code>src/transformers/core_model_loading.py</code></td><td>Logica de bază de încărcare, <code>WeightConverter</code>, <code>WeightRenaming</code>, <code>ConversionOps</code></td></tr><tr><td><code>src/transformers/conversion_mapping.py</code></td><td>Mapări integrate și compoziție per-submodel (<code>get_model_conversion_mapping</code>)</td></tr><tr><td><code>src/transformers/integrations/tensor_parallel.py</code></td><td>Clase și utilitare pentru sharding TP</td></tr><tr><td><code>src/transformers/quantizers/base.py</code></td><td>Hook-uri de quantization și clasa de bază</td></tr></tbody></table> <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/weightconverter.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_10j3czc = {
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.DwnOcLC_.js"),
import("/docs/transformers/main/ro/_app/immutable/entry/app.Dw7RcaNl.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 35],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
161 kB
·
Xet hash:
eedcea805ebdf3bfc5f8e2e0f5c5703330112c55091267c6c3072a0df99a16a4

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