Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"Contribuer","local":"contribute","sections":[{"title":"Prérequis","local":"requirements","sections":[],"depth":2},{"title":"Créer la nouvelle classe HFQuantizer","local":"create-new-hfquantizer-class","sections":[],"depth":2},{"title":"Aperçu des fichiers","local":"files-overview","sections":[],"depth":2},{"title":"Comprendre get_quantize_ops et get_weight_conversions","local":"understanding-getquantizeops-vs-getweightconversions","sections":[{"title":"get_quantize_ops — Quantification à la volée","local":"getquantizeops--quantize-on-the-fly","sections":[],"depth":3},{"title":"get_weight_conversions — Charger des points de contrôle pré-quantifiés","local":"getweightconversions--load-pre-quantized-checkpoints","sections":[],"depth":3}],"depth":2}],"depth":1}"/> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DSYYoqAA.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/BovLmb3z.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/WyYpyc-L.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DYBfcveH.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DOLBYqcr.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/0.ByO_lNbj.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/ZtBhN-Ei.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/nodes/703.CAsO0Fiz.js" rel="modulepreload"> | |
| <link href="/docs/transformers/main/en/_app/immutable/chunks/D-dvW--N.js" rel="modulepreload"> | |
| <!--a24xey--><meta name="hf:doc:metadata" content="{"title":"Contribute","local":"contribute","sections":[{"title":"Requirements","local":"requirements","sections":[],"depth":2},{"title":"Create new HFQuantizer class","local":"create-new-hfquantizer-class","sections":[],"depth":2},{"title":"Files overview","local":"files-overview","sections":[],"depth":2},{"title":"Understanding get_quantize_ops vs get_weight_conversions","local":"understanding-getquantizeops-vs-getweightconversions","sections":[{"title":"get_quantize_ops — Quantize on the fly","local":"getquantizeops--quantize-on-the-fly","sections":[],"depth":3},{"title":"get_weight_conversions — Load pre-quantized checkpoints","local":"getweightconversions--load-pre-quantized-checkpoints","sections":[],"depth":3}],"depth":2}],"depth":1}"/><!----> | |
| <link href="/docs/transformers/main/en/_app/immutable/assets/0.tn0RQdqM.css" rel="modulepreload"> <!--[--><!--[0--><!--[--><!--[0--><!--[--><p></p> <div class="items-center shrink-0 min-w-[100px] max-sm:min-w-[50px] justify-end ml-auto flex" style="float: inline-end; margin-inline-start: 10px; display: inline-flex; position: relative; z-index: 10;"><div dir="ltr" class="inline-flex rounded-md max-sm:rounded-sm"><button class="inline-flex items-center gap-1 h-7 max-sm:h-7 px-2 max-sm:px-1.5 text-sm font-medium text-gray-800 border border-r-0 rounded-l-md max-sm:rounded-l-sm border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-live="polite"><span class="inline-flex items-center justify-center rounded-md p-0.5 max-sm:p-0 hover:text-gray-800 dark:hover:text-gray-200"><svg class="sm:size-3.5 size-3" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----></span> <span dir="auto">Copy page</span></button> <button class="inline-flex items-center justify-center w-6 max-sm:w-5 h-7 max-sm:h-7 disabled:pointer-events-none text-sm text-gray-500 hover:text-gray-700 dark:hover:text-white rounded-r-md max-sm:rounded-r-sm border border-l transition border-gray-200 bg-white hover:shadow-inner dark:border-gray-850 dark:bg-gray-950 dark:text-gray-200 dark:hover:bg-gray-800" aria-haspopup="menu" aria-expanded="false" aria-label="Open copy menu"><svg class="transition-transform text-gray-400 overflow-visible sm:size-3.5 size-3 rotate-0" width="1em" height="1em" viewBox="0 0 12 7" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M1 1L6 6L11 1" stroke="currentColor"></path></svg><!----></button></div> <!--[-1--><!--]--></div><!----> <!--[0--><h1 class="relative group"><a id="contribute" 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="#contribute"><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><!---->Contribuer<!----></span></h1><!--]--><!----> <p>Transformers prend en charge de nombreuses méthodes de quantification, telles que QLoRA, GPTQ, LLM.int8 et AWQ. Cependant, il existe encore de nombreuses autres approches de quantification qui n’ont pas encore été intégrées. Pour faciliter l’ajout et l’utilisation de ces méthodes de quantification avec Transformers, utilisez la classe <a href="/docs/transformers/main/fr/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a>. <a href="/docs/transformers/main/fr/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a> est conçue comme une classe d’assistance interne pour ajouter une méthode de quantification, plutôt que comme un élément appliqué à chaque module PyTorch.</p> <p>Ce guide vous montre comment intégrer une nouvelle méthode de quantification avec <a href="/docs/transformers/main/fr/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a>.</p> <!--[1--><h2 class="relative group"><a id="requirements" 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="#requirements"><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><!---->Prérequis<!----></span></h2><!--]--><!----> <p>Avant d’intégrer une nouvelle méthode de quantification dans Transformers, assurez-vous que la méthode répond aux exigences suivantes. Seules les méthodes de quantification qui peuvent être exécutées avec des modules PyTorch sont prises en charge.</p> <ul><li><p>La méthode de quantification est disponible via un paquet Python installable avec pip (il est également acceptable que le paquet ne soit installable qu’à partir du code source). Idéalement, des noyaux précompilés sont inclus dans le paquet pip.</p></li> <li><p>La méthode peut s’exécuter sur du matériel couramment utilisé (CPU, GPU, etc.).</p></li> <li><p>La méthode est encapsulée dans un <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html" rel="nofollow">nn.Module</a> (<code>~bitsandbytes.nn.Linear8bitLt</code>, <code>~bitsandbytes.nn.Linear4bit</code>), et la couche linéaire quantifiée doit avoir la définition suivante.</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">Linear4bit</span>(nn.Module): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">__init__</span>(<span class="hljs-params">self, ...</span>): | |
| ... | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">forward</span>(<span class="hljs-params">self, x</span>): | |
| <span class="hljs-keyword">return</span> my_4bit_kernel(x, <span class="hljs-variable language_">self</span>.weight, <span class="hljs-variable language_">self</span>.bias)<!----></pre></div><!----> <p>De cette manière, les modèles Transformers sont facilement quantisés en remplaçant les instances de <a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a> par une classe cible.</p></li> <li><p>La méthode de quantification doit être sérialisable. Vous pouvez enregistrer les poids quantifiés localement ou les pousser sur le Hub.</p></li> <li><p>Vérifiez que le package contenant les noyaux/primitives de quantisation est stable (sans modifications cassantes fréquentes).</p></li></ul> <p>Certaines méthodes de quantification peuvent nécessiter une « pré-quantification » du modèle par calibration sur des données (AWQ). Dans ce cas, nous préférons ne prendre en charge que l’inférence dans Transformers et laisser la bibliothèque tierce maintenue par la communauté ML gérer elle-même la quantification du modèle.</p> <!--[1--><h2 class="relative group"><a id="create-new-hfquantizer-class" 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="#create-new-hfquantizer-class"><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><!---->Créer la nouvelle classe HFQuantizer<!----></span></h2><!--]--><!----> <ol start="0"><li><p>Le meilleur point de départ serait de consulter une autre méthode de quantification, telle que Finegrained Fp8. Vous devrez mettre à jour ou créer trois fichiers au total : le <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/utils/quantization_config.py" rel="nofollow">fichier de configuration</a>, le <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/finegrained_fp8.py" rel="nofollow">fichier d’intégration</a> et le <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/quantizers/quantizer_finegrained_fp8.py" rel="nofollow">fichier de quantiseur</a>.</p></li> <li><p>Créez une nouvelle classe de configuration de quantification dans <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/utils/quantization_config.py" rel="nofollow">src/transformers/utils/quantization_config.py</a>. Ajoutez la nouvelle configuration de quantification à <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py#L1088" rel="nofollow">_import_structure</a> dans le fichier <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py" rel="nofollow">src/transformers/__init__.py</a> de Transformers.</p></li> <li><p>Créez un nouveau fichier dans <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers" rel="nofollow">src/transformers/quantizers/</a> nommé <code>quantizer_your_method.py</code>, et faites-le hériter de [`~quantizers.HfQuantizer]. Pensez à ajouter le nouveau quantizer et la configuration de quantification dans le mappage automatique de quantification dans <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers/auto.py" rel="nofollow">src/transformers/quantizers/auto.py</a>.</p></li> <li><p>Définissez les attributs de classe et les méthodes de propriété suivants pour votre méthode de quantification :</p> <ul><li><code>requires_calibration</code> : Indique si la méthode de quantification nécessite un processus de calibration des données. Si défini sur <code>True</code>, vous ne pouvez prendre en charge que l'inférence (avec des poids quantifiés) et non l'inférence et la quantification.</li> <li><code>is_serializable</code> : une méthode de propriété permettant de déterminer si la méthode est sérialisable ou non.</li> <li><code>is_trainable</code> : une méthode de propriété qui détermine si vous pouvez effectuer un fine-tuning de modèles au-dessus de la méthode de quantisation (avec ou sans approches PEFT).</li></ul></li> <li><p>Écrivez les méthodes <code>validate_environment</code> et <code>update_dtype</code>. Ces méthodes sont appelées avant la création du modèle quantifié afin de garantir que les utilisateurs utilisent la bonne configuration. Reportez-vous à d'autres quantiseurs pour un exemple de leur implémentation.</p></li> <li><p>Écrivez la méthode <code>_process_model_before_weight_loading</code>. Dans Transformers, les modèles quantifiés sont d'abord initialisés sur l'appareil <code>"meta"</code> avant le chargement des poids. Cela signifie que la méthode <code>_process_model_before_weight_loading</code> s'occupe de manipuler le squelette du modèle pour remplacer certains modules (<a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a>) par les modules cibles (modules de quantification).</p></li></ol> <p>Vous pouvez définir la logique de remplacement de module ou toute autre méthode utilitaire en créant un nouveau fichier dans <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/integrations" rel="nofollow">transformers/src/integrations/</a> et en exposant les méthodes pertinentes dans le fichier <code>__init__.py</code> de ce dossier.</p> <ol start="6"><li><p>Ajoutez la méthode <code>get_quantize_ops</code> à la classe de quantiseur si la quantisation prend en charge la quantisation à la volée. Dans transformers, nous matérialisons chaque tenseur et y appliquons une séquence d’opérations différentes. Dans notre cas, l’opération de quantisation a lieu à la fin. Vous devez créer un <code>XXXQuantize</code>, une sous-classe de <code>ConversionOps</code>, et ajouter une méthode <code>convert</code>. Dans la méthode <code>convert</code>, vous devez quantiser les poids et retourner un dictionnaire de paramètres quantifiés.</p></li> <li><p>Ajoutez la méthode <code>get_weight_conversions</code> à la classe de quantiseur si la quantisation prend en charge le chargement de poids pré-quantifiés. Dans transformers, il est possible de regrouper plusieurs tenseurs et d’appliquer des opérations dessus. C’est particulièrement utile lorsque les tenseurs présents dans le checkpoint doivent être regroupés pour recréer les tenseurs quantifiés.</p></li> <li><p>Écrivez la méthode <code>_process_model_after_weight_loading</code> si nécessaire. Cette méthode permet de mettre en œuvre des fonctionnalités supplémentaires nécessitant de manipuler le modèle après le chargement des poids.</p></li> <li><p>Documentez tout ! Assurez-vous que votre méthode de quantification est documentée en ajoutant un nouveau fichier sous <code>docs/source/en/quantization</code>.</p></li> <li><p>Vous devez ajouter des tests en ajoutant le paquet dans notre Dockerfile de nuit à l’intérieur de <code>docker/transformers-quantization-latest-gpu</code>, puis en ajoutant un nouveau fichier de test dans <code>tests/quantization/xxx</code>. N’hésitez pas à consulter les méthodes de quantification existantes pour voir comment elles sont implémentées.</p></li></ol> <!--[1--><h2 class="relative group"><a id="files-overview" 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="#files-overview"><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><!---->Aperçu des fichiers<!----></span></h2><!--]--><!----> <table><thead><tr><th>Fichier</th><th>Objectif</th></tr></thead><tbody><tr><td><code>utils/quantization_config.py</code></td><td>Définissez <code>YourMethodConfig</code> en héritant de <code>QuantizationConfigMixin</code></td></tr><tr><td><code>quantizers/quantizer_your_method.py</code></td><td>Implémenter <code>YourMethodHfQuantizer</code> en héritant de <code>HfQuantizer</code></td></tr><tr><td><code>integrations/your_method.py</code></td><td>Implémenter des sous-classes de <code>ConversionOps</code> et des fonctions utilitaires</td></tr><tr><td><code>quantizers/auto.py</code></td><td>Enregistrer le quantizer et la config dans <code>AUTO_QUANTIZER_MAPPING</code> et <code>AUTO_QUANTIZATION_CONFIG_MAPPING</code></td></tr><tr><td><code>docs/source/en/quantization/your_method.md</code></td><td>Utilisation de la documentation pour les utilisateurs</td></tr><tr><td><code>tests/quantization/your_method/</code></td><td>Ajouter des tests d'intégration</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="understanding-getquantizeops-vs-getweightconversions" 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="#understanding-getquantizeops-vs-getweightconversions"><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><!---->Comprendre get_quantize_ops et get_weight_conversions<!----></span></h2><!--]--><!----> <p>Ces deux méthodes gèrent des scénarios différents pour le chargement des poids. Il est essentiel de comprendre quand utiliser chacune d'elles.</p> <!--[2--><h3 class="relative group"><a id="getquantizeops--quantize-on-the-fly" 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="#getquantizeops--quantize-on-the-fly"><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><!---->get_quantize_ops — Quantification à la volée<!----></span></h3><!--]--><!----> <p>Utilisez ceci lors du chargement d'un <strong>point de contrôle non quantifié</strong> (par exemple, des poids en float16/bfloat16) et de la quantification pendant le chargement.</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> Copié</div><!----></button><!----></div> <pre class="language-text "><!---->Checkpoint: model.safetensors (float16 weights for example) | |
| ↓ | |
| get_quantize_ops → YourQuantize.convert() | |
| ↓ | |
| Result: Quantized weights in memory<!----></pre></div><!----> <p>La méthode <code>convert</code> reçoit un tenseur à la fois, le quantifie et peut renvoyer un dictionnaire de paramètres quantifiés, par exemple :</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">class</span> <span class="hljs-title class_">YourQuantize</span>(<span class="hljs-title class_ inherited__">ConversionOps</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">convert</span>(<span class="hljs-params">self, input_dict, model, full_layer_name, missing_keys, **kwargs</span>): | |
| <span class="hljs-comment"># input_dict = {"layer.weight": <float16 tensor>}</span> | |
| value = <span class="hljs-built_in">list</span>(input_dict.values())[<span class="hljs-number">0</span>] | |
| module, tensor_name = get_module_from_name(model, full_layer_name) | |
| <span class="hljs-comment"># Quantize and assign</span> | |
| quantized, scale, zero_point = your_quantize_fn(value) | |
| <span class="hljs-keyword">return</span> {full_layer_name: quantized, full_layer_name + <span class="hljs-string">".scale"</span>: scale, full_layer_name + <span class="hljs-string">".zero_point"</span>: zero_point}<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="getweightconversions--load-pre-quantized-checkpoints" 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="#getweightconversions--load-pre-quantized-checkpoints"><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><!---->get_weight_conversions — Charger des points de contrôle pré-quantifiés<!----></span></h3><!--]--><!----> <p>Utilisez cette option lors du chargement d'un <strong>point de contrôle pré-quantifié</strong> dont les poids quantifiés sont enregistrés sous forme de plusieurs composants distincts (tels que les données, l'échelle et le point zéro) et qui doivent être fusionnés en un seul tenseur au moment du chargement. Toutes les méthodes de quantification n'exigent pas cette étape de reconstruction : par exemple, certaines méthodes comme FP8 chargent simplement les poids et les échelles tels quels, sans les combiner. D'autres, comme torchao, nécessitent en revanche de reconstituer le tenseur quantifié à partir de ses multiples composants enregistrés.</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> Copié</div><!----></button><!----></div> <pre class="language-text "><!---->Checkpoint: model.safetensors (quantized components) | |
| - layer._weight_qdata | |
| - layer._weight_scale | |
| - layer._weight_zero_point | |
| ↓ | |
| get_weight_conversions → WeightConverter + YourDeserialize.convert() | |
| ↓ | |
| Result: Reconstructed quantized tensor → layer.weight<!----></pre></div><!----> <p>Le <code>WeightConverter</code> collecte les tenseurs liés en fonction de <code>source_patterns</code>, puis les transmet à votre méthode <code>convert</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> Copié</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">def</span> <span class="hljs-title function_">get_weight_conversions</span>(<span class="hljs-params">self</span>): | |
| <span class="hljs-keyword">if</span> <span class="hljs-variable language_">self</span>.pre_quantized: | |
| <span class="hljs-keyword">return</span> [ | |
| WeightConverter( | |
| source_patterns=[<span class="hljs-string">"_weight_qdata"</span>, <span class="hljs-string">"_weight_scale"</span>, <span class="hljs-string">"_weight_zero_point"</span>], | |
| target_patterns=<span class="hljs-string">"weight"</span>, | |
| operations=[YourDeserialize(<span class="hljs-variable language_">self</span>)], | |
| ), | |
| ] | |
| <span class="hljs-keyword">return</span> [] | |
| <span class="hljs-keyword">class</span> <span class="hljs-title class_">YourDeserialize</span>(<span class="hljs-title class_ inherited__">ConversionOps</span>): | |
| <span class="hljs-keyword">def</span> <span class="hljs-title function_">convert</span>(<span class="hljs-params">self, input_dict, model, full_layer_name, **kwargs</span>): | |
| <span class="hljs-comment"># input_dict contains all collected tensors</span> | |
| <span class="hljs-comment"># Reconstruct the quantized tensor from components</span> | |
| reconstructed_tensor = reconstruct_from_components(input_dict) | |
| <span class="hljs-keyword">return</span> {full_layer_name: reconstructed_tensor}<!----></pre></div><!----> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/transformers/blob/main/docs/source/en/quantization/contribute.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_1m6yd5j = { | |
| base: "/docs/transformers/main/en", | |
| assets: "/docs/transformers/main/en" | |
| }; | |
| const element = document.currentScript.parentElement; | |
| Promise.all([ | |
| import("/docs/transformers/main/en/_app/immutable/entry/start.DYIO-UII.js"), | |
| import("/docs/transformers/main/en/_app/immutable/entry/app.D2_KizbC.js") | |
| ]).then(([kit, app]) => { | |
| kit.start(app, element, { | |
| node_ids: [0, 703], | |
| data: [null,null], | |
| form: null, | |
| error: null | |
| }); | |
| }); | |
| } | |
| </script> | |
Xet Storage Details
- Size:
- 36 kB
- Xet hash:
- 7a2479b461299f52729bec64bd3f0d36c10b07875459ca4f0d96635c69698759
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.