Buckets:

mishig's picture
download
raw
33.5 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;快速入门&quot;,&quot;local&quot;:&quot;getting-started&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline 级别的量化&quot;,&quot;local&quot;:&quot;pipeline-level-quantization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;基础量化&quot;,&quot;local&quot;:&quot;basic-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;高级量化&quot;,&quot;local&quot;:&quot;advanced-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;加载预量化检查点&quot;,&quot;local&quot;:&quot;loading-prequantized-checkpoints&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;资源&quot;,&quot;local&quot;:&quot;resources&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/entry/start.1gaXe03k.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/BObs6oQO.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DPnyVmXs.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/entry/app.BwdYRbCn.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DSGUT_OW.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/C045MWWx.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/6tuJ3nyi.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/nodes/0.CornNeQR.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/IaDvMxLQ.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/nodes/312._S_peb4w.js" rel="modulepreload">
<link href="/docs/diffusers/v0.43.0/en/_app/immutable/chunks/bKdwVYpU.js" rel="modulepreload">
<!--cubdu--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Getting started&quot;,&quot;local&quot;:&quot;getting-started&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Pipeline-level quantization&quot;,&quot;local&quot;:&quot;pipeline-level-quantization&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Basic quantization&quot;,&quot;local&quot;:&quot;basic-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;Advanced quantization&quot;,&quot;local&quot;:&quot;advanced-quantization&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Loading prequantized checkpoints&quot;,&quot;local&quot;:&quot;loading-prequantized-checkpoints&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Resources&quot;,&quot;local&quot;:&quot;resources&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/diffusers/v0.43.0/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="getting-started" 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="#getting-started"><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>量化旨在用更少的位数来表示数据,同时尽量保留原始数据的精度。这通常意味着将数据类型转换为用更少的位数表示相同的信息。例如,如果你的模型权重以 32 位浮点数存储,并将其量化为 16 位浮点数,模型大小将减半,从而更易于存储并减少内存占用。较低的精度还可以加速推理,因为使用更少的位数进行计算所需的时间更短。</p> <p>Diffusers 支持多种量化后端,使 <a href="../api/pipelines/flux">Flux</a> 等大型扩散模型更易用。本指南介绍如何使用 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.PipelineQuantizationConfig">PipelineQuantizationConfig</a> 类,在从预训练或非量化检查点初始化流水线时对其进行量化。</p> <!--[1--><h2 class="relative group"><a id="pipeline-level-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="#pipeline-level-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><!---->Pipeline 级别的量化<!----></span></h2><!--]--><!----> <p>根据您希望对量化配置进行自定义的程度,有两种方式使用 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.PipelineQuantizationConfig">PipelineQuantizationConfig</a>。</p> <ul><li>对于基本使用场景,请定义 <code>quant_backend</code>、<code>quant_kwargs</code> 和 <code>components_to_quantize</code> 参数</li> <li>如需进行细粒度的量化控制,请定义一个 <code>quant_mapping</code>,为各个模型组件提供量化配置。</li></ul> <!--[2--><h3 class="relative group"><a id="basic-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="#basic-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><!---->基础量化<!----></span></h3><!--]--><!----> <p>使用以下参数初始化 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.PipelineQuantizationConfig">PipelineQuantizationConfig</a>。</p> <ul><li><code>quant_backend</code> 指定要使用的量化后端。目前支持的后端包括:<code>bitsandbytes_4bit</code>、<code>bitsandbytes_8bit</code>、<code>gguf</code>、<code>quanto</code> 和 <code>torchao</code>。</li> <li><code>quant_kwargs</code> 指定要使用的量化参数。</li></ul> <blockquote class="tip"><p>这些 <code>quant_kwargs</code> 参数因后端而异。请参阅 <a href="../api/quantization">量化 API</a> 文档以查看各后端的参数。</p></blockquote> <ul><li><p><code>components_to_quantize</code> 指定要量化的流水线组件。通常,您应该量化计算量最大的组件,例如 transformer。如果流水线包含多个文本编码器(例如 <a href="/docs/diffusers/v0.43.0/zh/api/pipelines/flux#diffusers.FluxPipeline">FluxPipeline</a>),则文本编码器是另一个需要考虑量化的组件。下面的示例对 <a href="/docs/diffusers/v0.43.0/zh/api/pipelines/flux#diffusers.FluxPipeline">FluxPipeline</a> 中的 T5 文本编码器进行量化,同时保持 CLIP 模型不变。</p> <p><code>components_to_quantize</code> 可以接受一个列表(用于多个模型)或一个字符串(用于单个模型)。</p></li></ul> <p>以下示例使用 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.BitsAndBytesConfig">BitsAndBytesConfig</a> 中的 <code>load_in_4bit</code>、<code>bnb_4bit_quant_type</code> 和 <code>bnb_4bit_compute_dtype</code> 参数加载 bitsandbytes 后端。</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">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">from</span> diffusers.quantizers <span class="hljs-keyword">import</span> PipelineQuantizationConfig
pipeline_quant_config = PipelineQuantizationConfig(
quant_backend=<span class="hljs-string">&quot;bitsandbytes_4bit&quot;</span>,
quant_kwargs={<span class="hljs-string">&quot;load_in_4bit&quot;</span>: <span class="hljs-literal">True</span>, <span class="hljs-string">&quot;bnb_4bit_quant_type&quot;</span>: <span class="hljs-string">&quot;nf4&quot;</span>, <span class="hljs-string">&quot;bnb_4bit_compute_dtype&quot;</span>: torch.bfloat16},
components_to_quantize=[<span class="hljs-string">&quot;transformer&quot;</span>, <span class="hljs-string">&quot;text_encoder_2&quot;</span>],
)<!----></pre></div><!----> <p>将 <code>pipeline_quant_config</code> 传递给 <a href="/docs/diffusers/v0.43.0/zh/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">from_pretrained()</a> 以量化该 pipeline。</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 "><!---->pipe = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
quantization_config=pipeline_quant_config,
dtype=torch.bfloat16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
image = pipe(<span class="hljs-string">&quot;photo of a cute dog&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[2--><h3 class="relative group"><a id="advanced-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="#advanced-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><!---->高级量化<!----></span></h3><!--]--><!----> <p><code>quant_mapping</code> 参数提供了更多选项,用于指定流水线中各个组件的量化方式,例如组合使用不同的量化后端。</p> <p>初始化 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.PipelineQuantizationConfig">PipelineQuantizationConfig</a> 并向其传入 <code>quant_mapping</code>。<code>quant_mapping</code> 允许你为流水线中的每个组件(例如 transformer 和 text encoder)指定量化选项。</p> <p>下面的示例使用两个量化后端 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.QuantoConfig">QuantoConfig</a> 和 <a href="https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig" rel="nofollow">transformers.BitsAndBytesConfig</a>,分别用于 transformer 和文本编码器。</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">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> BitsAndBytesConfig <span class="hljs-keyword">as</span> DiffusersBitsAndBytesConfig
<span class="hljs-keyword">from</span> diffusers.quantizers.quantization_config <span class="hljs-keyword">import</span> QuantoConfig
<span class="hljs-keyword">from</span> diffusers.quantizers <span class="hljs-keyword">import</span> PipelineQuantizationConfig
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BitsAndBytesConfig <span class="hljs-keyword">as</span> TransformersBitsAndBytesConfig
pipeline_quant_config = PipelineQuantizationConfig(
quant_mapping={
<span class="hljs-string">&quot;transformer&quot;</span>: QuantoConfig(weights_dtype=<span class="hljs-string">&quot;int8&quot;</span>),
<span class="hljs-string">&quot;text_encoder_2&quot;</span>: TransformersBitsAndBytesConfig(
load_in_4bit=<span class="hljs-literal">True</span>, compute_dtype=torch.bfloat16
),
}
)<!----></pre></div><!----> <p><a href="https://huggingface.co/docs/transformers/main_classes/quantization#transformers.BitsAndBytesConfig" rel="nofollow">Transformers</a> 中有一个独立的 bitsandbytes 后端。对于来自 Transformers 的组件,您需要导入并使用 <a href="https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig" rel="nofollow">transformers.BitsAndBytesConfig</a>。例如,<a href="/docs/diffusers/v0.43.0/zh/api/pipelines/flux#diffusers.FluxPipeline">FluxPipeline</a> 中的 <code>text_encoder_2</code> 是 Transformers 中的 <a href="https://huggingface.co/docs/transformers/v5.22.2/en/model_doc/t5#transformers.T5EncoderModel" rel="nofollow">T5EncoderModel</a>,因此您需要使用 <a href="https://huggingface.co/docs/transformers/v5.22.2/en/main_classes/quantization#transformers.BitsAndBytesConfig" rel="nofollow">transformers.BitsAndBytesConfig</a> 而不是 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.BitsAndBytesConfig">diffusers.BitsAndBytesConfig</a>。</p> <blockquote class="tip"><p>如果您不想管理这些不同的导入,或者不确定每个流水线组件的来源,请使用上述的<a href="#basic-quantization">基础量化</a>方法。</p></blockquote> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> BitsAndBytesConfig <span class="hljs-keyword">as</span> DiffusersBitsAndBytesConfig
<span class="hljs-keyword">from</span> diffusers.quantizers <span class="hljs-keyword">import</span> PipelineQuantizationConfig
<span class="hljs-keyword">from</span> transformers <span class="hljs-keyword">import</span> BitsAndBytesConfig <span class="hljs-keyword">as</span> TransformersBitsAndBytesConfig
pipeline_quant_config = PipelineQuantizationConfig(
quant_mapping={
<span class="hljs-string">&quot;transformer&quot;</span>: DiffusersBitsAndBytesConfig(load_in_4bit=<span class="hljs-literal">True</span>, bnb_4bit_compute_dtype=torch.bfloat16),
<span class="hljs-string">&quot;text_encoder_2&quot;</span>: TransformersBitsAndBytesConfig(
load_in_4bit=<span class="hljs-literal">True</span>, compute_dtype=torch.bfloat16
),
}
)<!----></pre></div><!----> <p>将 <code>pipeline_quant_config</code> 传递给 <a href="/docs/diffusers/v0.43.0/zh/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained">from_pretrained()</a> 以量化该 pipeline。</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 "><!---->pipe = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;black-forest-labs/FLUX.1-dev&quot;</span>,
quantization_config=pipeline_quant_config,
dtype=torch.bfloat16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
image = pipe(<span class="hljs-string">&quot;photo of a cute dog&quot;</span>).images[<span class="hljs-number">0</span>]<!----></pre></div><!----> <!--[1--><h2 class="relative group"><a id="loading-prequantized-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="#loading-prequantized-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><!---->加载预量化检查点<!----></span></h2><!--]--><!----> <p>某些量化后端支持加载已经量化并上传到 Hub 的检查点。在这种情况下,量化配置存储在模型的 <code>config.json</code> 中并会被自动读取——您无需创建 <a href="/docs/diffusers/v0.43.0/zh/api/quantization#diffusers.PipelineQuantizationConfig">PipelineQuantizationConfig</a> 或传入 <code>quantization_config</code> 参数。</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> 已复制</div><!----></button><!----></div> <pre class="language-py "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> DiffusionPipeline
pipe = DiffusionPipeline.from_pretrained(
<span class="hljs-string">&quot;rootonchair/ERNIE-Image-Turbo-nunchaku-lite-nvfp4&quot;</span>,
dtype=torch.bfloat16,
).to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span><!----></pre></div><!----> <p>以下后端开箱即支持加载预量化检查点。</p> <table><thead><tr><th>后端</th><th>备注</th></tr></thead><tbody><tr><td><a href="./bitsandbytes">bitsandbytes</a></td><td>Config 保存在 <code>config.json</code> 中,无需额外参数。支持即时量化以及加载预量化检查点。</td></tr><tr><td><a href="./torchao">torchao</a></td><td>同上。</td></tr><tr><td><a href="./gguf">GGUF</a></td><td>使用 <code>from_single_file</code> 与 Model 类;不支持 pipeline 级别的加载。</td></tr><tr><td><a href="./bfl">BFL</a></td><td>使用 <code>from_single_file</code> 加载 Black Forest Labs 的 <code>fp8r</code> 和 <code>nvfp4</code> 单文件检查点;不支持流水线级别的加载。</td></tr><tr><td><a href="./autoround">AutoRound</a></td><td>仅支持加载;请先使用 AutoRound CLI 或 Python API 进行量化。</td></tr><tr><td><a href="./nunchaku">Nunchaku Lite</a></td><td>Config 保存在 <code>config.json</code> 中;需要 <code>kernels</code> 包。仅支持加载。</td></tr><tr><td><a href="./modelopt">ModelOpt</a></td><td>仅支持加载预量化模型。</td></tr><tr><td><a href="./sdnq">SDNQ</a></td><td>支持在运行时进行量化以及加载预量化模型;需要 <code>sdnq</code> 包。</td></tr></tbody></table> <!--[1--><h2 class="relative group"><a id="resources" 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="#resources"><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>请查看以下资源,以了解更多有关量化的信息。</p> <ul><li><p>如果您是量化新手,我们推荐查看以下与 DeepLearning.AI 合作推出的入门友好课程。</p> <ul><li><a href="https://www.deeplearning.ai/short-courses/quantization-fundamentals-with-hugging-face/" rel="nofollow">使用 Hugging Face 了解量化基础</a></li> <li><a href="https://www.deeplearning.ai/short-courses/quantization-in-depth/" rel="nofollow">深入量化</a></li></ul></li> <li><p>如果您有兴趣添加新的量化方法,请参阅<a href="https://huggingface.co/docs/transformers/main/en/quantization/contribute" rel="nofollow">贡献新量化方法指南</a>。</p></li> <li><p>Transformers 量化<a href="https://huggingface.co/docs/transformers/quantization/overview#when-to-use-what" rel="nofollow">概述</a>介绍了不同量化后端的优缺点。</p></li> <li><p>阅读<a href="https://huggingface.co/blog/diffusers-quantization" rel="nofollow">探索 Diffusers 中的量化后端</a>博客文章,了解每个量化后端的简要介绍、如何选择合适的后端,以及如何将量化与其他内存优化技术结合使用。</p></li></ul> <a class="!text-gray-400 !no-underline text-sm flex items-center not-prose mt-4" href="https://github.com/huggingface/diffusers/blob/main/docs/source/en/quantization/overview.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_ywq9g6 = {
base: "/docs/diffusers/v0.43.0/en",
assets: "/docs/diffusers/v0.43.0/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/diffusers/v0.43.0/en/_app/immutable/entry/start.1gaXe03k.js"),
import("/docs/diffusers/v0.43.0/en/_app/immutable/entry/app.BwdYRbCn.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 312],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
33.5 kB
·
Xet hash:
731b7103668732870b7ec27ebb5826c4771cc527be8dcc2fe2f5626c65ee9bab

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