Buckets:

mishig's picture
download
raw
27.3 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;NVIDIA ModelOpt&quot;,&quot;local&quot;:&quot;nvidia-modelopt&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;NVIDIAModelOptConfig&quot;,&quot;local&quot;:&quot;nvidiamodeloptconfig&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Các kiểu lượng hóa được hỗ trợ&quot;,&quot;local&quot;:&quot;supported-quantization-types&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Chuẩn hóa và khử chuẩn hóa các mô hình lượng tử hóa&quot;,&quot;local&quot;:&quot;serializing-and-deserializing-quantized-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/>
<link href="/docs/diffusers/main/en/_app/immutable/entry/start.0S0_iEoG.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/I9eooN77.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/Bupkjr9f.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CyvZ3FS5.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/l0QnnOED.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DnVIJfC5.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DsnmJJEf.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/nodes/0.DteTvmQZ.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/CoSrdQax.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/nodes/354.Da0b8rJZ.js" rel="modulepreload">
<link href="/docs/diffusers/main/en/_app/immutable/chunks/DSPDl1gz.js" rel="modulepreload">
<!--v66wdz--><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;NVIDIA ModelOpt&quot;,&quot;local&quot;:&quot;nvidia-modelopt&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;NVIDIAModelOptConfig&quot;,&quot;local&quot;:&quot;nvidiamodeloptconfig&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Supported quantization types&quot;,&quot;local&quot;:&quot;supported-quantization-types&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Serializing and Deserializing quantized models&quot;,&quot;local&quot;:&quot;serializing-and-deserializing-quantized-models&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2}],&quot;depth&quot;:1}"/><!---->
<link href="/docs/diffusers/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="nvidia-modelopt" 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="#nvidia-modelopt"><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><!---->NVIDIA ModelOpt<!----></span></h1><!--]--><!----> <p><a href="https://github.com/NVIDIA/Model-Optimizer" rel="nofollow">NVIDIA-ModelOpt</a> là thư viện thống nhất các kỹ thuật tối ưu hóa mô hình tiên tiến như lượng tử hóa, cắt tỉa, chưng cất, giải mã suy đoán, v.v. Nó nén các mô hình học sâu cho các khung triển khai hạ nguồn như TensorRT-LLM hoặc TensorRT nhằm tối ưu hóa tốc độ suy luận.</p> <p>Trước khi bắt đầu, hãy đảm bảo bạn đã cài đặt nvidia_modelopt.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-bash "><!---->pip install -U <span class="hljs-string">&quot;nvidia_modelopt[hf]&quot;</span><!----></pre></div><!----> <p>Thực hiện lượng tử hóa một mô hình bằng cách truyền <code>NVIDIAModelOptConfig</code> vào <a href="/docs/diffusers/v0.43.0/vi/api/models/overview#diffusers.ModelMixin.from_pretrained">from_pretrained()</a> (bạn cũng có thể tải các mô hình đã được lượng tử hóa trước). Cách này hoạt động với bất kỳ mô hình nào trong bất kỳ chế độ nào, miễn là nó hỗ trợ tải bằng <a href="https://hf.co/docs/accelerate/index" rel="nofollow">Accelerate</a> và chứa các lớp <code>torch.nn.Linear</code>.</p> <p>Ví dụ bên dưới chỉ lượng tử hóa trọng số xuống FP8.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, SanaPipeline, NVIDIAModelOptConfig
model_id = <span class="hljs-string">&quot;Efficient-Large-Model/Sana_600M_1024px_diffusers&quot;</span>
dtype = torch.bfloat16
quantization_config = NVIDIAModelOptConfig(quant_type=<span class="hljs-string">&quot;FP8&quot;</span>, quant_method=<span class="hljs-string">&quot;modelopt&quot;</span>)
transformer = AutoModel.from_pretrained(
model_id,
subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
quantization_config=quantization_config,
dtype=dtype,
)
pipe = SanaPipeline.from_pretrained(
model_id,
transformer=transformer,
dtype=dtype,
)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
<span class="hljs-built_in">print</span>(<span class="hljs-string">f&quot;Pipeline memory usage: <span class="hljs-subst">{torch.cuda.max_memory_reserved() / <span class="hljs-number">1024</span>**<span class="hljs-number">3</span>:<span class="hljs-number">.3</span>f}</span> GB&quot;</span>)
prompt = <span class="hljs-string">&quot;A cat holding a sign that says hello world&quot;</span>
image = pipe(
prompt, num_inference_steps=<span class="hljs-number">50</span>, guidance_scale=<span class="hljs-number">4.5</span>, max_sequence_length=<span class="hljs-number">512</span>
).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;output.png&quot;</span>)<!----></pre></div><!----> <blockquote><p><strong>Lưu ý:</strong></p> <p>Các phương pháp lượng tử hóa trong NVIDIA-ModelOpt được thiết kế để giảm dung lượng bộ nhớ của trọng số mô hình bằng cách sử dụng các kỹ thuật QAT (Quantization-Aware Training) và PTQ (Post-Training Quantization) khác nhau, đồng thời duy trì hiệu suất của mô hình. Tuy nhiên, mức cải thiện hiệu suất thực tế trong quá trình suy luận phụ thuộc vào khung triển khai (ví dụ: TRT-LLM, TensorRT) và cấu hình phần cứng cụ thể.</p> <p>Chi tiết hơn có thể được tìm thấy <a href="https://github.com/NVIDIA/Model-Optimizer/tree/main/examples" rel="nofollow">tại đây</a>.</p></blockquote> <!--[1--><h2 class="relative group"><a id="nvidiamodeloptconfig" 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="#nvidiamodeloptconfig"><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><!---->NVIDIAModelOptConfig<!----></span></h2><!--]--><!----> <p>Lớp <code>NVIDIAModelOptConfig</code> nhận ba tham số:</p> <ul><li><code>quant_type</code>: Một giá trị chuỗi đề cập đến một trong các kiểu lượng tử hóa bên dưới.</li> <li><code>modules_to_not_convert</code>: Một danh sách các tên module đầy đủ/đầy đủ một phần mà không nên thực hiện lượng tử hóa. Ví dụ, để không thực hiện bất kỳ lượng tử hóa nào cho các khối chiếu pos_embed của <a href="/docs/diffusers/v0.43.0/vi/api/models/sd3_transformer2d#diffusers.SD3Transformer2DModel">SD3Transformer2DModel</a>, bạn sẽ chỉ định: <code>modules_to_not_convert=["pos_embed.proj.weight"]</code>.</li> <li><code>disable_conv_quantization</code>: Một giá trị boolean, khi được đặt thành <code>True</code> sẽ tắt lượng tử hóa cho tất cả các lớp tích chập trong mô hình. Điều này hữu ích vì lượng tử hóa theo kênh và theo khối thường không hoạt động tốt với các lớp tích chập (được sử dụng với INT4, NF4, NVFP4). Nếu bạn muốn tắt lượng tử hóa cho các lớp tích chập cụ thể, hãy sử dụng <code>modules_to_not_convert</code> thay thế.</li> <li><code>algorithm</code>: Thuật toán được sử dụng để xác định scale, mặc định là <code>"max"</code>. Bạn có thể xem tài liệu modelopt để biết thêm về các thuật toán và chi tiết.</li> <li><code>forward_loop</code>: Hàm vòng lặp forward được sử dụng để hiệu chỉnh kích hoạt trong quá trình lượng tử hóa. Nếu không được cung cấp, nó sẽ dựa vào các giá trị scale tĩnh được tính toán chỉ bằng trọng số.</li> <li><code>kwargs</code>: Một dict chứa các đối số theo từ khóa để truyền cho phương pháp lượng tử hóa cơ bản, phương pháp này sẽ được gọi dựa trên <code>quant_type</code>.</li></ul> <!--[1--><h2 class="relative group"><a id="supported-quantization-types" 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="#supported-quantization-types"><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><!---->Các kiểu lượng hóa được hỗ trợ<!----></span></h2><!--]--><!----> <p>ModelOpt hỗ trợ lượng tử hóa int8, fp8, int4, nf4 và nvfp4 ở mức chỉ trọng số, kênh và khối. Các phương pháp lượng tử hóa được thiết kế để giảm dung lượng bộ nhớ của trọng số mô hình trong khi duy trì hiệu suất của mô hình trong quá trình suy luận.</p> <p>Lượng tử hóa chỉ trọng số lưu trữ các trọng số của mô hình trong một kiểu dữ liệu bit thấp cụ thể nhưng thực hiện tính toán bằng một kiểu dữ liệu có độ chính xác cao hơn, chẳng hạn như <code>bfloat16</code>. Điều này làm giảm yêu cầu bộ nhớ từ các trọng số của mô hình nhưng vẫn giữ nguyên các đỉnh bộ nhớ cho việc tính toán kích hoạt.</p> <p>Các phương pháp lượng tử hóa được hỗ trợ như sau:</p> <table><thead><tr><th><strong>Loại lượng tử hóa</strong></th><th><strong>Các Scheme được hỗ trợ</strong></th><th><strong>Tham số bắt buộc</strong></th><th><strong>Ghi chú bổ sung</strong></th></tr></thead><tbody><tr><td><strong>INT8</strong></td><td><code>int8 weight only</code>, <code>int8 channel quantization</code>, <code>int8 block quantization</code></td><td><code>quant_type</code>, <code>quant_type + channel_quantize</code>, <code>quant_type + channel_quantize + block_quantize</code></td><td></td></tr><tr><td><strong>FP8</strong></td><td><code>fp8 weight only</code>, <code>fp8 channel quantization</code>, <code>fp8 block quantization</code></td><td><code>quant_type</code>, <code>quant_type + channel_quantize</code>, <code>quant_type + channel_quantize + block_quantize</code></td><td></td></tr><tr><td><strong>INT4</strong></td><td><code>int4 weight only</code>, <code>int4 block quantization</code></td><td><code>quant_type</code>, <code>quant_type + channel_quantize + block_quantize</code></td><td><code>channel_quantize = -1 is only supported for now</code></td></tr><tr><td><strong>NF4</strong></td><td><code>nf4 weight only</code>, <code>nf4 double block quantization</code></td><td><code>quant_type</code>, <code>quant_type + channel_quantize + block_quantize + scale_channel_quantize</code> + <code>scale_block_quantize</code></td><td><code>channel_quantize = -1 and scale_channel_quantize = -1 are only supported for now</code></td></tr><tr><td><strong>NVFP4</strong></td><td><code>nvfp4 weight only</code>, <code>nvfp4 block quantization</code></td><td><code>quant_type</code>, <code>quant_type + channel_quantize + block_quantize</code></td><td><code>channel_quantize = -1 is only supported for now</code></td></tr></tbody></table> <p>Tham khảo <a href="https://nvidia.github.io/Model-Optimizer/" rel="nofollow">tài liệu chính thức của modelopt</a> để hiểu rõ hơn về các phương pháp lượng tử hóa có sẵn và danh sách đầy đủ các tùy chọn cấu hình.</p> <!--[1--><h2 class="relative group"><a id="serializing-and-deserializing-quantized-models" 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="#serializing-and-deserializing-quantized-models"><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><!---->Chuẩn hóa và khử chuẩn hóa các mô hình lượng tử hóa<!----></span></h2><!--]--><!----> <p>Để serialize một model đã được lượng tử hóa trong một dtype cho trước, trước tiên hãy tải model với dtype lượng tử hóa mong muốn, sau đó lưu nó bằng phương thức <a href="/docs/diffusers/v0.43.0/vi/api/models/overview#diffusers.ModelMixin.save_pretrained">save_pretrained()</a>.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, NVIDIAModelOptConfig
<span class="hljs-keyword">from</span> modelopt.torch.opt <span class="hljs-keyword">import</span> enable_huggingface_checkpointing
enable_huggingface_checkpointing()
model_id = <span class="hljs-string">&quot;Efficient-Large-Model/Sana_600M_1024px_diffusers&quot;</span>
quant_config_fp8 = {<span class="hljs-string">&quot;quant_type&quot;</span>: <span class="hljs-string">&quot;FP8&quot;</span>, <span class="hljs-string">&quot;quant_method&quot;</span>: <span class="hljs-string">&quot;modelopt&quot;</span>}
quant_config_fp8 = NVIDIAModelOptConfig(**quant_config_fp8)
model = AutoModel.from_pretrained(
model_id,
subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
quantization_config=quant_config_fp8,
dtype=torch.bfloat16,
)
model.save_pretrained(<span class="hljs-string">&#x27;path/to/sana_fp8&#x27;</span>, safe_serialization=<span class="hljs-literal">False</span>)<!----></pre></div><!----> <p>Để tải một mô hình lượng tử hóa đã được serialize, hãy sử dụng phương thức <a href="/docs/diffusers/v0.43.0/vi/api/models/overview#diffusers.ModelMixin.from_pretrained">from_pretrained()</a>.</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> Đã sao chép</div><!----></button><!----></div> <pre class="language-python "><!----><span class="hljs-keyword">import</span> torch
<span class="hljs-keyword">from</span> diffusers <span class="hljs-keyword">import</span> AutoModel, NVIDIAModelOptConfig, SanaPipeline
<span class="hljs-keyword">from</span> modelopt.torch.opt <span class="hljs-keyword">import</span> enable_huggingface_checkpointing
enable_huggingface_checkpointing()
quantization_config = NVIDIAModelOptConfig(quant_type=<span class="hljs-string">&quot;FP8&quot;</span>, quant_method=<span class="hljs-string">&quot;modelopt&quot;</span>)
transformer = AutoModel.from_pretrained(
<span class="hljs-string">&quot;path/to/sana_fp8&quot;</span>,
subfolder=<span class="hljs-string">&quot;transformer&quot;</span>,
quantization_config=quantization_config,
dtype=torch.bfloat16,
)
pipe = SanaPipeline.from_pretrained(
<span class="hljs-string">&quot;Efficient-Large-Model/Sana_600M_1024px_diffusers&quot;</span>,
transformer=transformer,
dtype=torch.bfloat16,
)
pipe.to(<span class="hljs-string">&quot;cuda&quot;</span>) <span class="hljs-comment"># or &quot;mps&quot;, &quot;xpu&quot;, &quot;cpu&quot;</span>
prompt = <span class="hljs-string">&quot;A cat holding a sign that says hello world&quot;</span>
image = pipe(
prompt, num_inference_steps=<span class="hljs-number">50</span>, guidance_scale=<span class="hljs-number">4.5</span>, max_sequence_length=<span class="hljs-number">512</span>
).images[<span class="hljs-number">0</span>]
image.save(<span class="hljs-string">&quot;output.png&quot;</span>)<!----></pre></div><!----> <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/modelopt.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_1qsdbwf = {
base: "/docs/diffusers/main/en",
assets: "/docs/diffusers/main/en"
};
const element = document.currentScript.parentElement;
Promise.all([
import("/docs/diffusers/main/en/_app/immutable/entry/start.0S0_iEoG.js"),
import("/docs/diffusers/main/en/_app/immutable/entry/app.CPdOxCbw.js")
]).then(([kit, app]) => {
kit.start(app, element, {
node_ids: [0, 354],
data: [null,null],
form: null,
error: null
});
});
}
</script>

Xet Storage Details

Size:
27.3 kB
·
Xet hash:
e2e9ebcaddcc006a92b10e397b88fc74636d7e8f8d5303930a85a0a529ed3095

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