Commit
•
e4d374d
1
Parent(s):
a27ef1b
Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,298 @@
|
|
1 |
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
library_name: transformers
|
3 |
-
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
|
|
|
10 |
|
|
|
11 |
|
12 |
-
|
13 |
|
14 |
-
|
15 |
|
16 |
-
|
|
|
17 |
|
18 |
-
|
19 |
|
20 |
-
|
21 |
-
- **Funded by [optional]:** [More Information Needed]
|
22 |
-
- **Shared by [optional]:** [More Information Needed]
|
23 |
-
- **Model type:** [More Information Needed]
|
24 |
-
- **Language(s) (NLP):** [More Information Needed]
|
25 |
-
- **License:** [More Information Needed]
|
26 |
-
- **Finetuned from model [optional]:** [More Information Needed]
|
27 |
|
28 |
-
|
29 |
|
30 |
-
|
|
|
|
|
31 |
|
32 |
-
|
33 |
-
- **Paper [optional]:** [More Information Needed]
|
34 |
-
- **Demo [optional]:** [More Information Needed]
|
35 |
|
36 |
-
|
|
|
|
|
37 |
|
38 |
-
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
|
101 |
-
|
102 |
-
|
103 |
-
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
-
|
115 |
-
|
116 |
-
|
117 |
-
|
118 |
-
|
119 |
-
|
120 |
-
|
121 |
-
|
122 |
-
|
123 |
-
|
124 |
-
|
125 |
-
|
126 |
-
|
127 |
-
|
128 |
-
|
129 |
-
|
130 |
-
|
131 |
-
|
132 |
-
|
133 |
-
|
134 |
-
|
135 |
-
|
136 |
-
|
137 |
-
|
138 |
-
|
139 |
-
|
140 |
-
|
141 |
-
|
142 |
-
|
143 |
-
|
144 |
-
|
145 |
-
|
146 |
-
|
147 |
-
|
148 |
-
|
149 |
-
|
150 |
-
|
151 |
-
|
152 |
-
|
153 |
-
|
154 |
-
|
155 |
-
|
156 |
-
|
157 |
-
|
158 |
-
|
159 |
-
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
|
178 |
-
|
179 |
-
|
180 |
-
|
181 |
-
|
182 |
-
|
183 |
-
|
184 |
-
|
185 |
-
|
186 |
-
|
187 |
-
|
188 |
-
|
189 |
-
|
190 |
-
|
191 |
-
|
192 |
-
|
193 |
-
|
194 |
-
|
195 |
-
|
196 |
-
|
197 |
-
|
198 |
-
|
199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
---
|
2 |
+
language:
|
3 |
+
- fr
|
4 |
+
- it
|
5 |
+
- de
|
6 |
+
- es
|
7 |
+
- en
|
8 |
+
license: apache-2.0
|
9 |
library_name: transformers
|
10 |
+
pipeline_tag: text-generation
|
11 |
+
tags:
|
12 |
+
- mixtral
|
13 |
+
- autoawq
|
14 |
---
|
15 |
|
16 |
+
> [!IMPORTANT]
|
17 |
+
> This repository is a community-driven quantized version of the original model [`mistralai/Mixtral-8x7B-Instruct-v0.1`](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) which is the BF16 half-precision official version released by Mistral AI.
|
18 |
|
19 |
+
## Model Information
|
20 |
|
21 |
+
The Mixtral-8x7B Large Language Model (LLM) is a pretrained generative Sparse Mixture of Experts. The Mixtral-8x7B outperforms Llama 2 70B on most benchmarks they tested.
|
22 |
|
23 |
+
For full details of this model please read our [release blog post](https://mistral.ai/news/mixtral-of-experts/).
|
24 |
|
25 |
+
This repository contains [`mistralai/Mixtral-8x7B-Instruct-v0.1`](https://huggingface.co/mistralai/Mixtral-8x7B-Instruct-v0.1) quantized using [AutoAWQ](https://github.com/casper-hansen/AutoAWQ) from FP16 down to INT4 using the GEMM kernels performing zero-point quantization with a group size of 128.
|
26 |
|
27 |
+
## Model Usage
|
28 |
|
29 |
+
> [!NOTE]
|
30 |
+
> In order to run the inference with Mixtral 8x7B Instruct AWQ in INT4, around 24 GiB of VRAM are needed only for loading the model checkpoint, excluding the KV cache and/or the CUDA graphs, meaning that there should be a bit over that VRAM available.
|
31 |
|
32 |
+
In order to use the current quantized model, support is offered for different solutions as `transformers`, `autoawq`, or `text-generation-inference`.
|
33 |
|
34 |
+
### 🤗 Transformers
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
In order to run the inference with Mixtral 8x7B Instruct AWQ in INT4, you need to install the following packages:
|
37 |
|
38 |
+
```bash
|
39 |
+
pip install -q --upgrade transformers autoawq accelerate
|
40 |
+
```
|
41 |
|
42 |
+
To run the inference on top of Mixtral 8x7B Instruct AWQ in INT4 precision, the AWQ model can be instantiated as any other causal language modeling model via `AutoModelForCausalLM` and run the inference normally.
|
|
|
|
|
43 |
|
44 |
+
```python
|
45 |
+
import torch
|
46 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
|
47 |
|
48 |
+
model_id = "hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4"
|
49 |
|
50 |
+
quantization_config = AwqConfig(
|
51 |
+
bits=4,
|
52 |
+
fuse_max_seq_len=512, # Note: Update this as per your use-case
|
53 |
+
do_fuse=True,
|
54 |
+
)
|
55 |
|
56 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
57 |
+
model = AutoModelForCausalLM.from_pretrained(
|
58 |
+
model_id,
|
59 |
+
torch_dtype=torch.float16,
|
60 |
+
low_cpu_mem_usage=True,
|
61 |
+
device_map="auto",
|
62 |
+
quantization_config=quantization_config
|
63 |
+
)
|
64 |
+
|
65 |
+
prompt = [
|
66 |
+
{"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
|
67 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
68 |
+
]
|
69 |
+
inputs = tokenizer.apply_chat_template(
|
70 |
+
prompt,
|
71 |
+
tokenize=True,
|
72 |
+
add_generation_prompt=True,
|
73 |
+
return_tensors="pt",
|
74 |
+
return_dict=True,
|
75 |
+
).to("cuda")
|
76 |
+
|
77 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
78 |
+
print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
|
79 |
+
```
|
80 |
+
|
81 |
+
### AutoAWQ
|
82 |
+
|
83 |
+
In order to run the inference with Mixtral 8x7B Instruct AWQ in INT4, you need to install the following packages:
|
84 |
+
|
85 |
+
```bash
|
86 |
+
pip install -q --upgrade transformers autoawq accelerate
|
87 |
+
```
|
88 |
+
|
89 |
+
Alternatively, one may want to run that via `AutoAWQ` even though it's built on top of 🤗 `transformers`, which is the recommended approach instead as described above.
|
90 |
+
|
91 |
+
```python
|
92 |
+
import torch
|
93 |
+
from awq import AutoAWQForCausalLM
|
94 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
95 |
+
|
96 |
+
model_id = "hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4"
|
97 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
98 |
+
model = AutoAWQForCausalLM.from_pretrained(
|
99 |
+
model_id,
|
100 |
+
torch_dtype=torch.float16,
|
101 |
+
low_cpu_mem_usage=True,
|
102 |
+
device_map="auto",
|
103 |
+
)
|
104 |
+
|
105 |
+
prompt = [
|
106 |
+
{"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
|
107 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
108 |
+
]
|
109 |
+
inputs = tokenizer.apply_chat_template(
|
110 |
+
prompt,
|
111 |
+
tokenize=True,
|
112 |
+
add_generation_prompt=True,
|
113 |
+
return_tensors="pt",
|
114 |
+
return_dict=True,
|
115 |
+
).to("cuda")
|
116 |
+
|
117 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
118 |
+
print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
|
119 |
+
```
|
120 |
+
|
121 |
+
The AutoAWQ script has been adapted from [`AutoAWQ/examples/generate.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/generate.py).
|
122 |
+
|
123 |
+
### 🤗 Text Generation Inference (TGI)
|
124 |
+
|
125 |
+
To run the `text-generation-launcher` with Mixtral 8x7B Instruct AWQ in INT4 with Marlin kernels for optimized inference speed, you will need to have Docker installed (see [installation notes](https://docs.docker.com/engine/install/)) and the `huggingface_hub` Python package as you need to login to the Hugging Face Hub.
|
126 |
+
|
127 |
+
```bash
|
128 |
+
pip install -q --upgrade huggingface_hub
|
129 |
+
huggingface-cli login
|
130 |
+
```
|
131 |
+
|
132 |
+
Then you just need to run the TGI v2.0.3 (or higher) Docker container as follows:
|
133 |
+
|
134 |
+
```bash
|
135 |
+
docker run --gpus all --shm-size 1g -ti -p 8080:80 \
|
136 |
+
-v hf_cache:/data \
|
137 |
+
-e MODEL_ID=hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4 \
|
138 |
+
-e QUANTIZE=awq \
|
139 |
+
-e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
|
140 |
+
-e MAX_INPUT_LENGTH=4000 \
|
141 |
+
-e MAX_TOTAL_TOKENS=4096 \
|
142 |
+
ghcr.io/huggingface/text-generation-inference:2.0.3
|
143 |
+
```
|
144 |
+
|
145 |
+
> [!NOTE]
|
146 |
+
> TGI will expose different endpoints, to see all the endpoints available check [TGI OpenAPI Specification](https://huggingface.github.io/text-generation-inference/#/).
|
147 |
+
|
148 |
+
To send request to the deployed TGI endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
149 |
+
|
150 |
+
```bash
|
151 |
+
curl 0.0.0.0:8080/v1/chat/completions \
|
152 |
+
-X POST \
|
153 |
+
-H 'Content-Type: application/json' \
|
154 |
+
-d '{
|
155 |
+
"model": "tgi",
|
156 |
+
"messages": [
|
157 |
+
{
|
158 |
+
"role": "system",
|
159 |
+
"content": "You are a helpful assistant."
|
160 |
+
},
|
161 |
+
{
|
162 |
+
"role": "user",
|
163 |
+
"content": "What is Deep Learning?"
|
164 |
+
}
|
165 |
+
],
|
166 |
+
"max_tokens": 128
|
167 |
+
}'
|
168 |
+
```
|
169 |
+
|
170 |
+
Or programatically via the `huggingface_hub` Python client as follows:
|
171 |
+
|
172 |
+
```python
|
173 |
+
import os
|
174 |
+
from huggingface_hub import InferenceClient
|
175 |
+
|
176 |
+
client = InferenceClient(base_url="http://0.0.0.0:8080", api_key=os.getenv("HF_TOKEN", "-"))
|
177 |
+
|
178 |
+
chat_completion = client.chat.completions.create(
|
179 |
+
model="hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4",
|
180 |
+
messages=[
|
181 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
182 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
183 |
+
],
|
184 |
+
max_tokens=128,
|
185 |
+
)
|
186 |
+
```
|
187 |
+
|
188 |
+
Alternatively, the OpenAI Python client can also be used (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
|
189 |
+
|
190 |
+
```python
|
191 |
+
import os
|
192 |
+
from openai import OpenAI
|
193 |
+
|
194 |
+
client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key=os.getenv("OPENAI_API_KEY", "-"))
|
195 |
+
|
196 |
+
chat_completion = client.chat.completions.create(
|
197 |
+
model="tgi",
|
198 |
+
messages=[
|
199 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
200 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
201 |
+
],
|
202 |
+
max_tokens=128,
|
203 |
+
)
|
204 |
+
```
|
205 |
+
|
206 |
+
### vLLM
|
207 |
+
|
208 |
+
To run vLLM with Mixtral 8x7B Instruct AWQ in INT4, you will need to have Docker installed (see [installation notes](https://docs.docker.com/engine/install/)) and run the latest vLLM Docker container as follows:
|
209 |
+
|
210 |
+
```bash
|
211 |
+
docker run --runtime nvidia --gpus all --ipc=host -p 8000:8000 \
|
212 |
+
-v hf_cache:/root/.cache/huggingface \
|
213 |
+
vllm/vllm-openai:latest \
|
214 |
+
--model hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4 \
|
215 |
+
--max-model-len 4096
|
216 |
+
```
|
217 |
+
|
218 |
+
To send request to the deployed vLLM endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
219 |
+
|
220 |
+
```bash
|
221 |
+
curl 0.0.0.0:8000/v1/chat/completions \
|
222 |
+
-X POST \
|
223 |
+
-H 'Content-Type: application/json' \
|
224 |
+
-d '{
|
225 |
+
"model": "hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4",
|
226 |
+
"messages": [
|
227 |
+
{
|
228 |
+
"role": "system",
|
229 |
+
"content": "You are a helpful assistant."
|
230 |
+
},
|
231 |
+
{
|
232 |
+
"role": "user",
|
233 |
+
"content": "What is Deep Learning?"
|
234 |
+
}
|
235 |
+
],
|
236 |
+
"max_tokens": 128
|
237 |
+
}'
|
238 |
+
```
|
239 |
+
|
240 |
+
Or programatically via the `openai` Python client (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
|
241 |
+
|
242 |
+
```python
|
243 |
+
import os
|
244 |
+
from openai import OpenAI
|
245 |
+
|
246 |
+
client = OpenAI(base_url="http://0.0.0.0:8000/v1", api_key=os.getenv("VLLM_API_KEY", "-"))
|
247 |
+
|
248 |
+
chat_completion = client.chat.completions.create(
|
249 |
+
model="hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4",
|
250 |
+
messages=[
|
251 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
252 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
253 |
+
],
|
254 |
+
max_tokens=128,
|
255 |
+
)
|
256 |
+
```
|
257 |
+
|
258 |
+
## Quantization Reproduction
|
259 |
+
|
260 |
+
> [!NOTE]
|
261 |
+
> In order to quantize Mixtral 8x7B Instruct using AutoAWQ, you will need to use an instance with at least enough CPU RAM to fit the whole model in half-precision i.e. ~90GiB, and an NVIDIA GPU with at least 16GiB of VRAM to quantize it.
|
262 |
+
|
263 |
+
In order to quantize Mixtral 8x7B Instruct, first install the following packages:
|
264 |
+
|
265 |
+
```bash
|
266 |
+
pip install -q --upgrade transformers autoawq accelerate
|
267 |
+
```
|
268 |
+
|
269 |
+
Then run the following script, adapted from [`AutoAWQ/examples/quantize.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/quantize.py):
|
270 |
+
|
271 |
+
```python
|
272 |
+
from awq import AutoAWQForCausalLM
|
273 |
+
from transformers import AutoTokenizer
|
274 |
+
|
275 |
+
model_path = "mistralai/Mixtral-8x7B-Instruct-v0.1"
|
276 |
+
quant_path = "hugging-quants/Mixtral-8x7B-Instruct-v0.1-AWQ-INT4"
|
277 |
+
quant_config = {
|
278 |
+
"zero_point": True,
|
279 |
+
"q_group_size": 128,
|
280 |
+
"w_bit": 4,
|
281 |
+
"version": "GEMM",
|
282 |
+
}
|
283 |
+
|
284 |
+
# Load model
|
285 |
+
model = AutoAWQForCausalLM.from_pretrained(
|
286 |
+
model_path, low_cpu_mem_usage=True, use_cache=False,
|
287 |
+
)
|
288 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
289 |
+
|
290 |
+
# Quantize
|
291 |
+
model.quantize(tokenizer, quant_config=quant_config)
|
292 |
+
|
293 |
+
# Save quantized model
|
294 |
+
model.save_quantized(quant_path)
|
295 |
+
tokenizer.save_pretrained(quant_path)
|
296 |
+
|
297 |
+
print(f'Model is quantized and saved at "{quant_path}"')
|
298 |
+
```
|