Buckets:
| # AWQ | |
| [Activation-aware Weight Quantization (AWQ)](https://hf.co/papers/2306.00978) mempertahankan sebagian kecil bobot yang penting untuk kinerja LLM guna mengompres model menjadi 4-bit dengan degradasi kinerja yang minimal. | |
| Terdapat beberapa pustaka untuk mengkuantisasi model dengan algoritma AWQ, seperti [llm-awq](https://github.com/mit-han-lab/llm-awq), [autoawq](https://github.com/casper-hansen/AutoAWQ), atau [optimum-intel](https://huggingface.co/docs/optimum/main/en/intel/optimization_inc). Transformers mendukung pemuatan model yang telah dikuantisasi dengan pustaka llm-awq dan autoawq. Panduan ini akan menunjukkan cara memuat model yang dikuantisasi dengan autoawq, namun prosesnya serupa untuk model yang dikuantisasi dengan llm-awq. | |
| Jalankan perintah di bawah ini untuk menginstal autoawq | |
| ```bash | |
| pip install autoawq | |
| ``` | |
| > [!WARNING] | |
| > AutoAWQ menurunkan versi Transformers ke 4.47.1. Jika Anda ingin melakukan inferensi dengan AutoAWQ, Anda mungkin perlu menginstal ulang versi Transformers Anda setelah menginstal AutoAWQ. | |
| Identifikasi model yang dikuantisasi AWQ dengan memeriksa kunci `quant_method` di file [config.json](https://huggingface.co/TheBloke/zephyr-7B-alpha-AWQ/blob/main/config.json) model. | |
| ```json | |
| { | |
| "_name_or_path": "/workspace/process/huggingfaceh4_zephyr-7b-alpha/source", | |
| "architectures": [ | |
| "MistralForCausalLM" | |
| ], | |
| ... | |
| ... | |
| ... | |
| "quantization_config": { | |
| "quant_method": "awq", | |
| "zero_point": true, | |
| "group_size": 128, | |
| "bits": 4, | |
| "version": "gemm" | |
| } | |
| } | |
| ``` | |
| Muat model yang dikuantisasi AWQ dengan [from_pretrained()](/docs/transformers/main/id/main_classes/model#transformers.PreTrainedModel.from_pretrained). Secara default, bobot lainnya secara otomatis diatur ke fp16 demi alasan performa. Gunakan parameter `dtype` untuk memuat bobot lainnya dalam format yang berbeda. | |
| Jika model dimuat di CPU, gunakan parameter `device_map` untuk memindahkannya ke akselerator. | |
| ```py | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from accelerate import Accelerator | |
| import torch | |
| device = Accelerator().device | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "TheBloke/zephyr-7B-alpha-AWQ", | |
| dtype=torch.float32, | |
| device_map=device | |
| ) | |
| ``` | |
| Gunakan `attn_implementation` untuk mengaktifkan [FlashAttention2](../kernel_doc/loading_kernels#attention-kernels) guna mempercepat inferensi lebih lanjut. | |
| ```py | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| from accelerate import Accelerator | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "TheBloke/zephyr-7B-alpha-AWQ", | |
| attn_implementation="flash_attention_2", | |
| device_map=Accelerator().device | |
| ) | |
| ``` | |
| ## Modul yang digabungkan | |
| Modul fused menawarkan akurasi dan performa yang lebih baik. Modul AWQ untuk arsitektur [Llama](https://huggingface.co/meta-llama) dan [Mistral](https://huggingface.co/mistralai/Mistral-7B-v0.1) didukung secara bawaan, tetapi Anda juga dapat mem-fuse modul AWQ untuk arsitektur yang tidak didukung. | |
| > [!WARNING] | |
| > Modul yang digabungkan (fused) tidak dapat dikombinasikan dengan teknik optimasi lainnya seperti FlashAttention2. | |
| Buat [AwqConfig](/docs/transformers/main/id/main_classes/quantization#transformers.AwqConfig) dan atur parameter `fuse_max_seq_len` serta `do_fuse=True` untuk mengaktifkan modul yang difusi. Parameter `fuse_max_seq_len` adalah panjang sekuens total, yang harus mencakup panjang konteks dan panjang generasi yang diharapkan. Atur ke nilai yang lebih besar untuk kepastian. | |
| Contoh di bawah ini menggabungkan modul AWQ dari model [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ). | |
| ```python | |
| import torch | |
| from transformers import AwqConfig, AutoModelForCausalLM | |
| quantization_config = AwqConfig( | |
| bits=4, | |
| fuse_max_seq_len=512, | |
| do_fuse=True, | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "TheBloke/Mistral-7B-OpenOrca-AWQ", | |
| quantization_config=quantization_config | |
| ).to(0) | |
| ``` | |
| Model [TheBloke/Mistral-7B-OpenOrca-AWQ](https://huggingface.co/TheBloke/Mistral-7B-OpenOrca-AWQ) diuji dengan `batch_size=1` dengan dan tanpa modul yang digabungkan. | |
| Modul yang tidak digabungkan | |
| | Ukuran Batch | Panjang Prefill | Panjang Dekode | Token prefill/detik | Token/s saat decode | Memori (VRAM) | | |
| |-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| | |
| | 1 | 32 | 32 | 60.0984 | 38.4537 | 4,50 GB (5,68%) | | |
| | 1 | 64 | 64 | 1333.67 | 31.6604 | 4,50 GB (5,68%) | | |
| | 1 | 128 | 128 | 2434.06 | 31.6272 | 4,50 GB (5,68%) | | |
| | 1 | 256 | 256 | 3072.26 | 38.1731 | 4,50 GB (5,68%) | | |
| | 1 | 512 | 512 | 3184.74 | 31.6819 | 4,59 GB (5,80%) | | |
| | 1 | 1024 | 1024 | 3148.18 | 36.8031 | 4,81 GB (6,07%) | | |
| | 1 | 2048 | 2048 | 2927.33 | 35.2676 | 5,73 GB (7,23%) | | |
| Modul terpadu | |
| | Ukuran Batch | Panjang Prefill | Panjang Dekode | Token prefill/detik | Token/s saat decode | Memori (VRAM) | | |
| |-------------:|-----------------:|----------------:|-------------------:|------------------:|:----------------| | |
| | 1 | 32 | 32 | 81.4899 | 80.2569 | 4,00 GB (5,05%) | | |
| | 1 | 64 | 64 | 1756.1 | 106.26 | 4,00 GB (5,05%) | | |
| | 1 | 128 | 128 | 2479.32 | 105.631 | 4,00 GB (5,06%) | | |
| | 1 | 256 | 256 | 1813.6 | 85.7485 | 4,01 GB (5,06%) | | |
| | 1 | 512 | 512 | 2848.9 | 97.701 | 4,11 GB (5,19%) | | |
| | 1 | 1024 | 1024 | 3044.35 | 87.7323 | 4,41 GB (5,57%) | | |
| | 1 | 2048 | 2048 | 2715.11 | 89.4709 | 5,57 GB (7,04%) | | |
| Kecepatan dan throughput modul yang difusi dan tidak difusi juga diuji menggunakan pustaka [optimum-benchmark](https://github.com/huggingface/optimum-benchmark). | |
| memori puncak forward/ukuran batch | |
| generate throughput/ukuran batch | |
| Untuk arsitektur yang tidak mendukung modul terfusi, buat [AwqConfig](/docs/transformers/main/id/main_classes/quantization#transformers.AwqConfig) dan definisikan pemetaan fusing kustom di `modules_to_fuse` untuk menentukan modul mana yang perlu difusi. | |
| Contoh di bawah ini menggabungkan modul AWQ dari model [TheBloke/Yi-34B-AWQ](https://huggingface.co/TheBloke/Yi-34B-AWQ). | |
| ```python | |
| import torch | |
| from transformers import AwqConfig, AutoModelForCausalLM | |
| quantization_config = AwqConfig( | |
| bits=4, | |
| fuse_max_seq_len=512, | |
| modules_to_fuse={ | |
| "attention": ["q_proj", "k_proj", "v_proj", "o_proj"], | |
| "layernorm": ["ln1", "ln2", "norm"], | |
| "mlp": ["gate_proj", "up_proj", "down_proj"], | |
| "use_alibi": False, | |
| "num_attention_heads": 56, | |
| "num_key_value_heads": 8, | |
| "hidden_size": 7168 | |
| } | |
| ) | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "TheBloke/Yi-34B-AWQ", | |
| quantization_config=quantization_config | |
| ).to(0) | |
| ``` | |
| Parameter `modules_to_fuse` harus menyertakan kunci-kunci berikut. | |
| - `"attention"`: Nama-nama lapisan perhatian yang akan digabungkan dalam urutan berikut: lapisan proyeksi query, key, value, dan output. Jika Anda tidak ingin menggabungkan lapisan-lapisan ini, berikan daftar kosong. | |
| - `"layernorm"`: Nama dari semua lapisan LayerNorm yang ingin Anda ganti dengan LayerNorm fused kustom. Jika Anda tidak ingin menggabungkan lapisan-lapisan ini, berikan daftar kosong. | |
| - `"mlp"`: Nama-nama lapisan MLP yang ingin Anda gabungkan menjadi satu lapisan MLP dalam urutan: (lapisan gate (dense, layer, post-attention) / up / down). | |
| - `"use_alibi"`: Jika model Anda menggunakan positional embedding ALiBi. | |
| - `"num_attention_heads"`: Jumlah attention heads. | |
| - `"num_key_value_heads"`: Jumlah key value heads yang harus digunakan untuk mengimplementasikan Grouped Query Attention (GQA). | |
| | nilai parameter | attention | | |
| |---|---| | |
| | `num_key_value_heads=num_attention_heads` | Perhatian Multi-Head | | |
| | `num_key_value_heads=1` | Perhatian Multi-Query | | |
| | `num_key_value_heads=...` | Grouped Query Attention | | |
| - `"hidden_size"`: Dimensi dari representasi tersembunyi. | |
| ## ExLlamaV2 | |
| Kernel [ExLlamaV2](https://github.com/turboderp/exllamav2) mendukung prefill dan decoding yang lebih cepat. Jalankan perintah di bawah ini untuk menginstal versi terbaru autoawq dengan dukungan ExLlamaV2. | |
| ```bash | |
| pip install git+https://github.com/casper-hansen/AutoAWQ.git | |
| ``` | |
| Setel `version="exllama"` di [AwqConfig](/docs/transformers/main/id/main_classes/quantization#transformers.AwqConfig) untuk mengaktifkan kernel ExLlamaV2. | |
| > [!TIP] | |
| > ExLlamaV2 didukung pada GPU AMD. | |
| ```py | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig | |
| quantization_config = AwqConfig(version="exllama") | |
| model = AutoModelForCausalLM.from_pretrained( | |
| "TheBloke/Mistral-7B-Instruct-v0.1-AWQ", | |
| quantization_config=quantization_config, | |
| device_map="auto", | |
| ) | |
| ``` | |
| ## Sumber daya | |
| Jalankan [notebook](https://colab.research.google.com/drive/1HzZH89yAXJaZgwJDhQj9LqSBux932BvY#scrollTo=Wwsg6nCwoThm) demo AWQ untuk melihat lebih banyak contoh cara mengkuantisasi model, mendorong model terkuantisasi ke Hub, dan lainnya. | |
Xet Storage Details
- Size:
- 9.68 kB
- Xet hash:
- 8929010a5ca18e16c3c8ba3a5ca37e4fb588fb507745fa4fcb9e3a2b72d2d7cc
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.