Buckets:
| <meta charset="utf-8" /><meta name="hf:doc:metadata" content="{"title":"贡献","local":"contribute","sections":[{"title":"要求","local":"requirements","sections":[],"depth":2},{"title":"创建新的 HFQuantizer 类","local":"create-new-hfquantizer-class","sections":[],"depth":2},{"title":"文件概览","local":"files-overview","sections":[],"depth":2},{"title":"理解 get_quantize_ops 与 get_weight_conversions","local":"understanding-getquantizeops-vs-getweightconversions","sections":[{"title":"get_quantize_ops — 即时量化","local":"getquantizeops--quantize-on-the-fly","sections":[],"depth":3},{"title":"get_weight_conversions — 加载预量化检查点","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><!---->贡献<!----></span></h1><!--]--><!----> <p>Transformers 支持多种量化方法,例如 QLoRA、GPTQ、LLM.int8 和 AWQ。不过,目前仍有许多量化方案尚未集成。为了更便捷地在 Transformers 中添加和使用这些量化方法,请使用 <a href="/docs/transformers/main/zh/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a> 类。<a href="/docs/transformers/main/zh/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a> 被设计为用于添加量化方法的内部辅助类,而不是应用于每个 PyTorch 模块的组件。</p> <p>本指南将向您展示如何将新的量化方法与 <a href="/docs/transformers/main/zh/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><!---->要求<!----></span></h2><!--]--><!----> <p>在将新的量化方法集成到 Transformers 之前,请确保该方法满足以下要求。仅支持与 PyTorch 模块兼容的量化方法。</p> <ul><li><p>量化方法可通过一个支持 pip 安装的 Python 包提供(如果只能从源码安装该包,也可以)。理想情况下,pip 包中应包含预编译的 kernel。</p></li> <li><p>该方法可以在常用硬件(CPU、GPU 等)上运行。</p></li> <li><p>该方法被封装在一个 <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>)中,量化后的线性层应具有如下定义。</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> 已复制</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>通过这种方式,只需将 <a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a> 的实例替换为目标类,即可轻松对 Transformers 模型进行量化。</p></li> <li><p>量化方法应当是可序列化的。您可以将量化后的权重保存到本地,或推送到 Hub。</p></li> <li><p>确保包含量化内核/原语的包是稳定的(没有频繁的破坏性变更)。</p></li></ul> <p>某些量化方法可能需要通过数据校准对模型进行“预量化”(AWQ)。在这种情况下,我们倾向于在 Transformers 中仅支持推理,而将模型量化本身交由由机器学习社区维护的第三方库处理。</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><!---->创建新的 HFQuantizer 类<!----></span></h2><!--]--><!----> <ol start="0"><li><p>最好的起点是参考另一种量化方法,例如 Finegrained Fp8。你总共需要更新或创建三个文件:<a href="https://github.com/huggingface/transformers/blob/main/src/transformers/utils/quantization_config.py" rel="nofollow">配置文件</a>、<a href="https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/finegrained_fp8.py" rel="nofollow">集成文件</a>和 <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/quantizers/quantizer_finegrained_fp8.py" rel="nofollow">量化器文件</a>。</p></li> <li><p>在 <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/utils/quantization_config.py" rel="nofollow">src/transformers/utils/quantization_config.py</a> 中创建一个新的量化配置类。将新的量化配置添加到 Transformers 的 <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py" rel="nofollow">src/transformers/__init__.py</a> 文件中的 <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py#L1088" rel="nofollow">_import_structure</a> 里。</p></li> <li><p>在 <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers" rel="nofollow">src/transformers/quantizers/</a> 目录下创建一个名为 <code>quantizer_your_method.py</code> 的新文件,并使其继承自 [`~quantizers.HfQuantizer`]。请确保在 <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>为您的量化方法定义以下类属性和属性方法:</p> <ul><li><code>requires_calibration</code>:量化方法是否需要数据校准过程。如果设置为 <code>True</code>,则仅支持推理(使用量化后的权重),而不支持推理和量化。</li> <li><code>is_serializable</code>:一个属性方法,用于判断该方法是否可序列化。</li> <li><code>is_trainable</code>:一个属性方法,用于确定是否可以在量化方法之上微调模型(无论是否使用 PEFT 方法)。</li></ul></li> <li><p>编写 <code>validate_environment</code> 和 <code>update_dtype</code> 方法。这些方法在创建量化模型之前被调用,以确保用户使用了正确的配置。请参考其他量化器了解其实现方式。</p></li> <li><p>编写 <code>_process_model_before_weight_loading</code> 方法。在 Transformers 中,量化模型会先在 <code>"meta"</code> 设备上初始化,然后再加载权重。这意味着 <code>_process_model_before_weight_loading</code> 方法负责操作模型骨架,将某些模块(<a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a>)替换为目标模块(量化模块)。</p></li></ol> <p>你可以通过在 <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/integrations" rel="nofollow">transformers/src/integrations/</a> 中创建新文件,并在该文件夹的 <code>__init__.py</code> 文件中暴露相关方法,来定义模块替换逻辑或其他任何实用方法。</p> <ol start="6"><li><p>如果量化支持即时量化,请向量化器类添加 <code>get_quantize_ops</code> 方法。在 transformers 中,我们会实例化每个张量,并对其进行一系列不同的操作。在我们的场景中,量化操作发生在最后。你需要创建一个 <code>XXXQuantize</code>,即 <code>ConversionOps</code> 的子类,并添加一个 <code>convert</code> 方法。在 <code>convert</code> 方法中,你需要对权重进行量化,并返回一个包含量化后参数的字典。</p></li> <li><p>如果量化支持加载预量化权重,请在量化器类中添加 <code>get_weight_conversions</code> 方法。在 transformers 中,我们可以收集多个张量并对它们执行操作。当检查点中的张量需要重新分组以重建量化张量时,这一点尤其有用。</p></li> <li><p>如有需要,请编写 <code>_process_model_after_weight_loading</code> 方法。此方法可用于实现需要在加载权重后对模型进行额外操作的功能。</p></li> <li><p>记录一切!请通过在 <code>docs/source/en/quantization</code> 下添加新文件,确保你的量化方法已被记录。</p></li> <li><p>您应在 <code>docker/transformers-quantization-latest-gpu</code> 中的 nightly Dockerfile 里添加该包,然后在 <code>tests/quantization/xxx</code> 中添加一个新的测试文件。您可以参考现有的量化方法,了解其实现方式。</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><!---->文件概览<!----></span></h2><!--]--><!----> <table><thead><tr><th>文件</th><th>目的</th></tr></thead><tbody><tr><td><code>utils/quantization_config.py</code></td><td>定义继承自 <code>QuantizationConfigMixin</code> 的 <code>YourMethodConfig</code></td></tr><tr><td><code>quantizers/quantizer_your_method.py</code></td><td>实现继承自 <code>HfQuantizer</code> 的 <code>YourMethodHfQuantizer</code></td></tr><tr><td><code>integrations/your_method.py</code></td><td>实现 <code>ConversionOps</code> 的子类和辅助函数</td></tr><tr><td><code>quantizers/auto.py</code></td><td>在 <code>AUTO_QUANTIZER_MAPPING</code> 和 <code>AUTO_QUANTIZATION_CONFIG_MAPPING</code> 中注册量化器和配置</td></tr><tr><td><code>docs/source/en/quantization/your_method.md</code></td><td>为用户记录使用情况</td></tr><tr><td><code>tests/quantization/your_method/</code></td><td>添加集成测试</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><!---->理解 get_quantize_ops 与 get_weight_conversions<!----></span></h2><!--]--><!----> <p>这两种方法用于处理不同的权重加载场景。了解何时使用每种方法至关重要。</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 — 即时量化<!----></span></h3><!--]--><!----> <p>在加载<strong>未量化的检查点</strong>(例如 float16/bfloat16 权重)并在加载过程中进行量化时,请使用此方法。</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> 已复制</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><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> 已复制</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 — 加载预量化检查点<!----></span></h3><!--]--><!----> <p>在加载<strong>预量化检查点</strong>时使用此功能,其中量化权重以多个独立组件(例如 data、scale 和 zero point)的形式保存,并且需要在加载过程中将这些组件合并为一个张量。并非所有量化方法都需要此重建步骤:例如,FP8 等某些方法会直接加载权重和缩放因子,而无需进行合并。其他方法(如 torchao)则需要从多个已保存的组件中重新组装量化张量。</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> 已复制</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><code>WeightConverter</code> 会根据 <code>source_patterns</code> 收集相关的张量,然后将它们传递给你的 <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> 已复制</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:
- 33.8 kB
- Xet hash:
- c9727a4414496ca2f8bd1407d06f591b708cfd46db20ecb6cccdbe8792705750
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.