Buckets:

mishig's picture
download
raw
37.1 kB
<meta charset="utf-8" /><meta name="hf:doc:metadata" content="{&quot;title&quot;:&quot;Đóng góp&quot;,&quot;local&quot;:&quot;contribute&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Yêu cầu&quot;,&quot;local&quot;:&quot;requirements&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tạo lớp HFQuantizer mới&quot;,&quot;local&quot;:&quot;create-new-hfquantizer-class&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Tổng quan về các tệp&quot;,&quot;local&quot;:&quot;files-overview&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Hiểu về get_quantize_ops so với get_weight_conversions&quot;,&quot;local&quot;:&quot;understanding-getquantizeops-vs-getweightconversions&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;get_quantize_ops — Lượng tử hóa theo thời gian thực&quot;,&quot;local&quot;:&quot;getquantizeops--quantize-on-the-fly&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_weight_conversions — Nạp các checkpoint đã lượng tử hóa trước&quot;,&quot;local&quot;:&quot;getweightconversions--load-pre-quantized-checkpoints&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;: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="{&quot;title&quot;:&quot;Contribute&quot;,&quot;local&quot;:&quot;contribute&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;Requirements&quot;,&quot;local&quot;:&quot;requirements&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Create new HFQuantizer class&quot;,&quot;local&quot;:&quot;create-new-hfquantizer-class&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Files overview&quot;,&quot;local&quot;:&quot;files-overview&quot;,&quot;sections&quot;:[],&quot;depth&quot;:2},{&quot;title&quot;:&quot;Understanding get_quantize_ops vs get_weight_conversions&quot;,&quot;local&quot;:&quot;understanding-getquantizeops-vs-getweightconversions&quot;,&quot;sections&quot;:[{&quot;title&quot;:&quot;get_quantize_ops — Quantize on the fly&quot;,&quot;local&quot;:&quot;getquantizeops--quantize-on-the-fly&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3},{&quot;title&quot;:&quot;get_weight_conversions — Load pre-quantized checkpoints&quot;,&quot;local&quot;:&quot;getweightconversions--load-pre-quantized-checkpoints&quot;,&quot;sections&quot;:[],&quot;depth&quot;:3}],&quot;depth&quot;:2}],&quot;depth&quot;: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><!---->Đóng góp<!----></span></h1><!--]--><!----> <p>Transformers hỗ trợ nhiều phương pháp lượng tử hóa như QLoRA, GPTQ, LLM.int8 và AWQ. Tuy nhiên, vẫn còn nhiều cách tiếp cận lượng tử hóa khác chưa được tích hợp. Để việc thêm và sử dụng các phương pháp lượng tử hóa này với Transformers trở nên dễ dàng hơn, hãy sử dụng lớp <a href="/docs/transformers/main/vi/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a>. <a href="/docs/transformers/main/vi/main_classes/quantization#transformers.quantizers.HfQuantizer">HfQuantizer</a> được thiết kế như một lớp trợ giúp nội bộ để thêm một phương pháp lượng tử hóa, thay vì là thứ được áp dụng cho mọi module PyTorch.</p> <p>Hướng dẫn này sẽ cho bạn thấy cách tích hợp một phương pháp lượng tử hóa mới với <a href="/docs/transformers/main/vi/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><!---->Yêu cầu<!----></span></h2><!--]--><!----> <p>Trước khi tích hợp một phương pháp lượng tử hóa mới vào Transformers, hãy đảm bảo phương pháp đó đáp ứng các yêu cầu sau. Chỉ các phương pháp lượng tử hóa có thể chạy với các module PyTorch mới được hỗ trợ.</p> <ul><li><p>Phương pháp lượng tử hóa có sẵn thông qua một gói Python có thể cài đặt bằng pip (việc chỉ có thể cài đặt gói từ mã nguồn cũng hoàn toàn ổn). Về lý tưởng, các kernel đã biên dịch sẵn được bao gồm trong gói pip.</p></li> <li><p>Phương pháp này có thể chạy trên phần cứng phổ biến (CPU, GPU, v.v.).</p></li> <li><p>Phương thức được bọc trong một <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>), và lớp tuyến tính đã lượng tử hóa nên có định nghĩa như sau.</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-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>Nhờ đó, các mô hình Transformers có thể được lượng tử hóa một cách dễ dàng bằng cách thay thế các thể hiện của <a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a> bằng một lớp đích.</p></li> <li><p>Phương pháp lượng tử hóa phải có thể được phân chuỗi. Bạn có thể lưu các trọng số đã lượng tử hóa cục bộ hoặc đẩy chúng lên Hub.</p></li> <li><p>Hãy đảm bảo rằng gói chứa các kernel/primitive lượng tử hóa ổn định (không có thay đổi gây lỗi thường xuyên).</p></li></ul> <p>Một số phương pháp lượng tử hóa có thể yêu cầu “tiền lượng tử hóa” mô hình thông qua hiệu chuẩn dữ liệu (AWQ). Trong trường hợp này, chúng tôi ưu tiên chỉ hỗ trợ suy luận trong Transformers và để thư viện bên thứ ba do cộng đồng ML duy trì tự xử lý việc lượng tử hóa mô hình.</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><!---->Tạo lớp HFQuantizer mới<!----></span></h2><!--]--><!----> <ol start="0"><li><p>Điểm khởi đầu tốt nhất là xem xét một phương pháp lượng tử hóa khác, chẳng hạn như Finegrained Fp8. Bạn sẽ phải cập nhật hoặc tạo tổng cộng ba tệp: <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/utils/quantization_config.py" rel="nofollow">tệp cấu hình</a>, <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/integrations/finegrained_fp8.py" rel="nofollow">tệp tích hợp</a> và <a href="https://github.com/huggingface/transformers/blob/main/src/transformers/quantizers/quantizer_finegrained_fp8.py" rel="nofollow">tệp bộ lượng tử hóa</a>.</p></li> <li><p>Tạo một lớp cấu hình lượng tử hóa mới bên trong <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/utils/quantization_config.py" rel="nofollow">src/transformers/utils/quantization_config.py</a>. Thêm cấu hình lượng tử hóa mới vào <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py#L1088" rel="nofollow">_import_structure</a> bên trong tệp <a href="https://github.com/huggingface/transformers/blob/abbffc4525566a48a9733639797c812301218b83/src/transformers/__init__.py" rel="nofollow">src/transformers/__init__.py</a> của Transformers.</p></li> <li><p>Tạo một tệp mới bên trong <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/quantizers" rel="nofollow">src/transformers/quantizers/</a> có tên là <code>quantizer_your_method.py</code>, và cho nó kế thừa từ [`~quantizers.HfQuantizer`]. Hãy đảm bảo thêm quantizer mới và cấu hình lượng tử hóa vào bản ánh xạ tự động cho lượng tử hóa trong <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>Định nghĩa các thuộc tính lớp và phương thức thuộc tính sau cho phương pháp lượng tử hóa của bạn:</p> <ul><li><code>requires_calibration</code>: Xác định xem phương pháp lượng tử hóa có yêu cầu quy trình hiệu chuẩn dữ liệu hay không. Nếu đặt thành <code>True</code>, bạn chỉ có thể hỗ trợ suy luận (với trọng số đã lượng tử hóa) mà không thể thực hiện cả suy luận lẫn lượng tử hóa.</li> <li><code>is_serializable</code>: Một phương thức thuộc tính để xác định xem phương thức có thể được phân chuỗi (serializable) hay không.</li> <li><code>is_trainable</code>: Một phương thức thuộc tính để xác định xem bạn có thể tinh chỉnh (fine-tune) các mô hình trên phương pháp lượng tử hóa (có hoặc không có các phương pháp PEFT) hay không.</li></ul></li> <li><p>Viết các phương thức <code>validate_environment</code> và <code>update_dtype</code>. Các phương thức này được gọi trước khi tạo mô hình lượng tử hóa để đảm bảo người dùng sử dụng cấu hình đúng. Tham khảo các bộ lượng tử hóa khác để xem cách chúng được triển khai.</p></li> <li><p>Viết phương thức <code>_process_model_before_weight_loading</code>. Trong Transformers, các mô hình lượng tử hóa được khởi tạo trước trên thiết bị <code>"meta"</code> trước khi tải trọng số. Điều này có nghĩa là phương thức <code>_process_model_before_weight_loading</code> chịu trách nhiệm thao tác trên khung mô hình để thay thế một số module (<a href="https://pytorch.org/docs/stable/generated/torch.nn.Linear.html" rel="nofollow">nn.Linear</a>) bằng các module đích (các module lượng tử hóa).</p></li></ol> <p>Bạn có thể định nghĩa logic thay thế module hoặc bất kỳ phương tiện tiện ích nào khác bằng cách tạo một tệp mới trong <a href="https://github.com/huggingface/transformers/tree/abbffc4525566a48a9733639797c812301218b83/src/transformers/integrations" rel="nofollow">transformers/src/integrations/</a> và phơi bày các phương thức liên quan trong tệp <code>__init__.py</code> của thư mục đó.</p> <ol start="6"><li><p>Thêm phương thức <code>get_quantize_ops</code> vào lớp quantizer nếu phép lượng tử hóa hỗ trợ lượng tử hóa trực tiếp. Trong transformers, chúng ta vật chất hóa từng tensor và áp dụng một chuỗi các thao tác khác nhau lên nó. Trong trường hợp của chúng ta, thao tác lượng tử hóa diễn ra ở cuối. Bạn cần tạo một <code>XXXQuantize</code>, một lớp con của <code>ConversionOps</code>, và thêm một phương thức <code>convert</code>. Trong phương thức <code>convert</code>, bạn cần lượng tử hóa các trọng số và trả về một từ điển chứa các tham số đã được lượng tử hóa.</p></li> <li><p>Thêm phương thức <code>get_weight_conversions</code> vào lớp quantizer nếu quá trình lượng tử hóa hỗ trợ nạp trọng số đã được lượng tử hóa trước. Trong transformers, chúng ta có thể thu thập nhiều tensor và áp dụng các thao tác lên chúng. Điều này đặc biệt hữu ích khi chúng ta có các tensor trong checkpoint cần được nhóm lại để tái tạo các tensor đã lượng tử hóa.</p></li> <li><p>Viết phương thức <code>_process_model_after_weight_loading</code> nếu cần. Phương thức này cho phép triển khai các tính năng bổ sung yêu cầu thao tác trên mô hình sau khi tải trọng số.</p></li> <li><p>Tài liệu hóa mọi thứ! Đảm bảo phương pháp lượng tử hóa của bạn được tài liệu hóa bằng cách thêm một tệp mới dưới <code>docs/source/en/quantization</code>.</p></li> <li><p>Bạn nên thêm các bài kiểm thử bằng cách thêm gói vào Dockerfile chạy hàng đêm của chúng tôi trong <code>docker/transformers-quantization-latest-gpu</code> và sau đó thêm một tệp kiểm thử mới trong <code>tests/quantization/xxx</code>. Bạn có thể xem các phương pháp lượng tử hóa hiện có để biết cách triển khai.</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><!---->Tổng quan về các tệp<!----></span></h2><!--]--><!----> <table><thead><tr><th>File</th><th>Mục đích</th></tr></thead><tbody><tr><td><code>utils/quantization_config.py</code></td><td>Định nghĩa <code>YourMethodConfig</code> kế thừa từ <code>QuantizationConfigMixin</code></td></tr><tr><td><code>quantizers/quantizer_your_method.py</code></td><td>Thực hiện <code>YourMethodHfQuantizer</code> kế thừa từ <code>HfQuantizer</code></td></tr><tr><td><code>integrations/your_method.py</code></td><td>Thực hiện các lớp con của <code>ConversionOps</code> và các hàm hỗ trợ</td></tr><tr><td><code>quantizers/auto.py</code></td><td>Đăng ký quantizer và config trong <code>AUTO_QUANTIZER_MAPPING</code> và <code>AUTO_QUANTIZATION_CONFIG_MAPPING</code></td></tr><tr><td><code>docs/source/en/quantization/your_method.md</code></td><td>Tài liệu sử dụng cho người dùng</td></tr><tr><td><code>tests/quantization/your_method/</code></td><td>Thêm các bài kiểm thử tích hợp</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><!---->Hiểu về get_quantize_ops so với get_weight_conversions<!----></span></h2><!--]--><!----> <p>Hai phương pháp này xử lý các kịch bản khác nhau khi nạp trọng số. Việc hiểu rõ khi nào nên sử dụng từng phương pháp là điều cần thiết.</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 — Lượng tử hóa theo thời gian thực<!----></span></h3><!--]--><!----> <p>Sử dụng điều này khi tải một <strong>checkpoint không lượng tử hóa</strong> (ví dụ: trọng số float16/bfloat16) và lượng tử hóa trong quá trình tải.</p> <div class="code-block relative "><div class="absolute top-2.5 right-4"><button class="inline-flex items-center relative text-sm focus:text-green-500 cursor-pointer focus:outline-none transition duration-200 ease-in-out opacity-0 mx-0.5 text-gray-600 " title="code excerpt" type="button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" fill="currentColor" focusable="false" role="img" width="1em" height="1em" preserveAspectRatio="xMidYMid meet" viewBox="0 0 32 32"><path d="M28,10V28H10V10H28m0-2H10a2,2,0,0,0-2,2V28a2,2,0,0,0,2,2H28a2,2,0,0,0,2-2V10a2,2,0,0,0-2-2Z" transform="translate(0)"></path><path d="M4,18H2V4A2,2,0,0,1,4,2H18V4H4Z" transform="translate(0)"></path><rect fill="none" width="32" height="32"></rect></svg><!----> <div class=" absolute pointer-events-none transition-opacity bg-black text-white py-1 px-2 leading-tight rounded font-normal shadow left-1/2 top-full transform -translate-x-1/2 translate-y-2 opacity-0 "><div class="absolute bottom-full left-1/2 transform -translate-x-1/2 w-0 h-0 border-black border-4 border-t-0" style="border-left-color: transparent; border-right-color: transparent;"></div> Đã sao chép</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>Phương thức <code>convert</code> nhận một tensor mỗi lần, lượng tử hóa nó và có thể trả về một từ điển các tham số đã lượng tử hóa, ví dụ:</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-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 = {&quot;layer.weight&quot;: &lt;float16 tensor&gt;}</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">&quot;.scale&quot;</span>: scale, full_layer_name + <span class="hljs-string">&quot;.zero_point&quot;</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 — Nạp các checkpoint đã lượng tử hóa trước<!----></span></h3><!--]--><!----> <p>Sử dụng tùy chọn này khi nạp một <strong>checkpoint đã lượng tử hóa trước</strong>, trong đó các trọng số đã lượng tử hóa được lưu dưới dạng nhiều thành phần riêng biệt (chẳng hạn như data, scale và zero point), và các thành phần này cần được kết hợp thành một tensor duy nhất trong quá trình nạp. Không phải phương pháp lượng tử hóa nào cũng yêu cầu bước tái tạo này: ví dụ, một số phương pháp như FP8 chỉ đơn giản nạp trọng số và scale như chúng được lưu, mà không cần kết hợp chúng. Ngược lại, các phương pháp khác, chẳng hạn như torchao, lại yêu cầu lắp ráp lại tensor đã lượng tử hóa từ nhiều thành phần đã được lưu.</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-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> thu thập các tensor liên quan dựa trên <code>source_patterns</code>, sau đó truyền chúng đến phương thức <code>convert</code> của bạn:</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-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">&quot;_weight_qdata&quot;</span>, <span class="hljs-string">&quot;_weight_scale&quot;</span>, <span class="hljs-string">&quot;_weight_zero_point&quot;</span>],
target_patterns=<span class="hljs-string">&quot;weight&quot;</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:
37.1 kB
·
Xet hash:
0ab1b48dedf122b413a3703a53e2e40f25f11f3c192ddae76dc9e781024bb266

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