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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: llama3.1
|
3 |
+
language:
|
4 |
+
- en
|
5 |
+
- de
|
6 |
+
- fr
|
7 |
+
- it
|
8 |
+
- pt
|
9 |
+
- hi
|
10 |
+
- es
|
11 |
+
- th
|
12 |
+
library_name: transformers
|
13 |
+
pipeline_tag: text-generation
|
14 |
+
tags:
|
15 |
+
- llama-3.1
|
16 |
+
- meta
|
17 |
+
- autogptq
|
18 |
+
---
|
19 |
+
|
20 |
+
> [!IMPORTANT]
|
21 |
+
> This repository is a community-driven quantized version of the original model [`meta-llama/Meta-Llama-3.1-8B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) which is the FP16 half-precision official version released by Meta AI.
|
22 |
+
|
23 |
+
## Model Information
|
24 |
+
|
25 |
+
The Meta Llama 3.1 collection of multilingual large language models (LLMs) is a collection of pretrained and instruction tuned generative models in 8B, 70B and 405B sizes (text in/text out). The Llama 3.1 instruction tuned text only models (8B, 70B, 405B) are optimized for multilingual dialogue use cases and outperform many of the available open source and closed chat models on common industry benchmarks.
|
26 |
+
|
27 |
+
This repository contains [`meta-llama/Meta-Llama-3.1-8B-Instruct`](https://huggingface.co/meta-llama/Meta-Llama-3.1-8B-Instruct) quantized using [AutoGPTQ](https://github.com/AutoGPTQ/AutoGPTQ) from FP16 down to INT4 using the GPTQ kernels performing zero-point quantization with a group size of 128.
|
28 |
+
|
29 |
+
## Model Usage
|
30 |
+
|
31 |
+
> [!NOTE]
|
32 |
+
> In order to run the inference with Llama 3.1 8B Instruct GPTQ in INT4, around 4 GiB of VRAM are needed only for loading the model checkpoint, without including the KV cache or the CUDA graphs, meaning that there should be a bit over that VRAM available.
|
33 |
+
|
34 |
+
In order to use the current quantized model, support is offered for different solutions as `transformers`, `autogptq`, or `text-generation-inference`.
|
35 |
+
|
36 |
+
### 🤗 transformers
|
37 |
+
|
38 |
+
In order to run the inference with Llama 3.1 8B Instruct GPTQ in INT4, you need to install the following packages:
|
39 |
+
|
40 |
+
```bash
|
41 |
+
pip install -q --upgrade transformers accelerate optimum
|
42 |
+
pip install -q --no-build-isolation auto-gptq
|
43 |
+
```
|
44 |
+
|
45 |
+
To run the inference on top of Llama 3.1 8B Instruct GPTQ in INT4 precision, the GPTQ model can be instantiated as any other causal language modeling model via `AutoModelForCausalLM` and run the inference normally.
|
46 |
+
|
47 |
+
```python
|
48 |
+
import torch
|
49 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
50 |
+
|
51 |
+
model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4"
|
52 |
+
prompt = [
|
53 |
+
{"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
|
54 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
55 |
+
]
|
56 |
+
|
57 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
58 |
+
|
59 |
+
inputs = tokenizer.apply_chat_template(
|
60 |
+
prompt,
|
61 |
+
tokenize=True,
|
62 |
+
add_generation_prompt=True,
|
63 |
+
return_tensors="pt",
|
64 |
+
return_dict=True,
|
65 |
+
).to("cuda")
|
66 |
+
|
67 |
+
model = AutoModelForCausalLM.from_pretrained(
|
68 |
+
model_id,
|
69 |
+
torch_dtype=torch.float16,
|
70 |
+
low_cpu_mem_usage=True,
|
71 |
+
device_map="auto",
|
72 |
+
)
|
73 |
+
|
74 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
75 |
+
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
|
76 |
+
```
|
77 |
+
|
78 |
+
### AutoGPTQ
|
79 |
+
|
80 |
+
In order to run the inference with Llama 3.1 8B Instruct GPTQ in INT4, you need to install the following packages:
|
81 |
+
|
82 |
+
```bash
|
83 |
+
pip install -q --upgrade transformers accelerate optimum
|
84 |
+
pip install -q --no-build-isolation auto-gptq
|
85 |
+
```
|
86 |
+
|
87 |
+
Alternatively, one may want to run that via `AutoGPTQ` even though it's built on top of 🤗 `transformers`, which is the recommended approach instead as described above.
|
88 |
+
|
89 |
+
```python
|
90 |
+
import torch
|
91 |
+
from auto_gptq import AutoGPTQForCausalLM
|
92 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
93 |
+
|
94 |
+
model_id = "hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4"
|
95 |
+
prompt = [
|
96 |
+
{"role": "system", "content": "You are a helpful assistant, that responds as a pirate."},
|
97 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
98 |
+
]
|
99 |
+
|
100 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
101 |
+
|
102 |
+
inputs = tokenizer.apply_chat_template(
|
103 |
+
prompt,
|
104 |
+
tokenize=True,
|
105 |
+
add_generation_prompt=True,
|
106 |
+
return_tensors="pt",
|
107 |
+
return_dict=True,
|
108 |
+
).to("cuda")
|
109 |
+
|
110 |
+
model = AutoGPTQForCausalLM.from_pretrained(
|
111 |
+
model_id,
|
112 |
+
torch_dtype=torch.float16,
|
113 |
+
low_cpu_mem_usage=True,
|
114 |
+
device_map="auto",
|
115 |
+
)
|
116 |
+
|
117 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
118 |
+
print(tokenizer.batch_decode(outputs, skip_special_tokens=True))
|
119 |
+
```
|
120 |
+
|
121 |
+
The AutoGPTQ script has been adapted from [`AutoGPTQ/examples/quantization/basic_usage.py`](https://github.com/AutoGPTQ/AutoGPTQ/blob/main/examples/quantization/basic_usage.py).
|
122 |
+
|
123 |
+
### 🤗 Text Generation Inference (TGI)
|
124 |
+
|
125 |
+
To run the `text-generation-launcher` with Llama 3.1 8B Instruct GPTQ 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.2.0 (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/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4 \
|
138 |
+
-e NUM_SHARD=4 \
|
139 |
+
-e QUANTIZE=gptq \
|
140 |
+
-e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
|
141 |
+
-e MAX_INPUT_LENGTH=4000 \
|
142 |
+
-e MAX_TOTAL_TOKENS=4096 \
|
143 |
+
ghcr.io/huggingface/text-generation-inference:2.2.0
|
144 |
+
```
|
145 |
+
|
146 |
+
> [!NOTE]
|
147 |
+
> TGI will expose different endpoints, to see all the endpoints available check [TGI OpenAPI Specification](https://huggingface.github.io/text-generation-inference/#/).
|
148 |
+
|
149 |
+
To send request to the deployed TGI endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
150 |
+
|
151 |
+
```bash
|
152 |
+
curl 0.0.0.0:8080/v1/chat/completions \
|
153 |
+
-X POST \
|
154 |
+
-H 'Content-Type: application/json' \
|
155 |
+
-d '{
|
156 |
+
"model": "tgi",
|
157 |
+
"messages": [
|
158 |
+
{
|
159 |
+
"role": "system",
|
160 |
+
"content": "You are a helpful assistant."
|
161 |
+
},
|
162 |
+
{
|
163 |
+
"role": "user",
|
164 |
+
"content": "What is Deep Learning?"
|
165 |
+
}
|
166 |
+
],
|
167 |
+
"max_tokens": 128
|
168 |
+
}'
|
169 |
+
```
|
170 |
+
|
171 |
+
Or programatically via the `huggingface_hub` Python client as follows:
|
172 |
+
|
173 |
+
```python
|
174 |
+
import os
|
175 |
+
from huggingface_hub import InferenceClient
|
176 |
+
|
177 |
+
client = InferenceClient(base_url="http://0.0.0.0:8080", api_key=os.getenv("HF_TOKEN", "-"))
|
178 |
+
|
179 |
+
chat_completion = client.chat.completions.create(
|
180 |
+
model="hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4",
|
181 |
+
messages=[
|
182 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
183 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
184 |
+
],
|
185 |
+
max_tokens=128,
|
186 |
+
)
|
187 |
+
```
|
188 |
+
|
189 |
+
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:
|
190 |
+
|
191 |
+
```python
|
192 |
+
import os
|
193 |
+
from openai import OpenAI
|
194 |
+
|
195 |
+
client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key=os.getenv("OPENAI_API_KEY", "-"))
|
196 |
+
|
197 |
+
chat_completion = client.chat.completions.create(
|
198 |
+
model="tgi",
|
199 |
+
messages=[
|
200 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
201 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
202 |
+
],
|
203 |
+
max_tokens=128,
|
204 |
+
)
|
205 |
+
```
|
206 |
+
|
207 |
+
### vLLM
|
208 |
+
|
209 |
+
To run vLLM with Llama 3.1 8B Instruct GPTQ 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:
|
210 |
+
|
211 |
+
```bash
|
212 |
+
docker run --runtime nvidia --gpus all --ipc=host -p 8000:8000 \
|
213 |
+
-v hf_cache:/root/.cache/huggingface \
|
214 |
+
vllm/vllm-openai:latest \
|
215 |
+
--model hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4 \
|
216 |
+
--quantization gptq_marlin \
|
217 |
+
--tensor-parallel-size 4 \
|
218 |
+
--max-model-len 4096
|
219 |
+
```
|
220 |
+
|
221 |
+
To send request to the deployed vLLM endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
222 |
+
|
223 |
+
```bash
|
224 |
+
curl 0.0.0.0:8000/v1/chat/completions \
|
225 |
+
-X POST \
|
226 |
+
-H 'Content-Type: application/json' \
|
227 |
+
-d '{
|
228 |
+
"model": "hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4",
|
229 |
+
"messages": [
|
230 |
+
{
|
231 |
+
"role": "system",
|
232 |
+
"content": "You are a helpful assistant."
|
233 |
+
},
|
234 |
+
{
|
235 |
+
"role": "user",
|
236 |
+
"content": "What is Deep Learning?"
|
237 |
+
}
|
238 |
+
],
|
239 |
+
"max_tokens": 128
|
240 |
+
}'
|
241 |
+
```
|
242 |
+
|
243 |
+
Or programatically via the `openai` Python client (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
|
244 |
+
|
245 |
+
```python
|
246 |
+
import os
|
247 |
+
from openai import OpenAI
|
248 |
+
|
249 |
+
client = OpenAI(base_url="http://0.0.0.0:8000/v1", api_key=os.getenv("VLLM_API_KEY", "-"))
|
250 |
+
|
251 |
+
chat_completion = client.chat.completions.create(
|
252 |
+
model="hugging-quants/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4",
|
253 |
+
messages=[
|
254 |
+
{"role": "system", "content": "You are a helpful assistant."},
|
255 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
256 |
+
],
|
257 |
+
max_tokens=128,
|
258 |
+
)
|
259 |
+
```
|
260 |
+
|
261 |
+
## Quantization Reproduction
|
262 |
+
|
263 |
+
> [!NOTE]
|
264 |
+
> In order to quantize Llama 3.1 8B Instruct using AutoGPTQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~8GiB, and an NVIDIA GPU with 16GiB of VRAM to quantize it.
|
265 |
+
|
266 |
+
In order to quantize Llama 3.1 8B Instruct with GPTQ in INT4, you need to install the following packages:
|
267 |
+
|
268 |
+
```bash
|
269 |
+
pip install -q --upgrade transformers accelerate optimum
|
270 |
+
pip install -q --no-build-isolation auto-gptq
|
271 |
+
```
|
272 |
+
|
273 |
+
Then run the following script, adapted from [`AutoGPTQ/examples/quantization/basic_usage.py`](https://github.com/AutoGPTQ/AutoGPTQ/blob/main/examples/quantization/basic_usage.py).
|
274 |
+
|
275 |
+
```python
|
276 |
+
import random
|
277 |
+
|
278 |
+
import numpy as np
|
279 |
+
import torch
|
280 |
+
|
281 |
+
from auto_gptq import AutoGPTQForCausalLM, BaseQuantizeConfig
|
282 |
+
from datasets import load_dataset
|
283 |
+
from transformers import AutoTokenizer
|
284 |
+
|
285 |
+
pretrained_model_dir = "meta-llama/Meta-Llama-3.1-8B-Instruct"
|
286 |
+
quantized_model_dir = "meta-llama/Meta-Llama-3.1-8B-Instruct-GPTQ-INT4"
|
287 |
+
|
288 |
+
print("Loading tokenizer, dataset, and tokenizing the dataset...")
|
289 |
+
tokenizer = AutoTokenizer.from_pretrained(pretrained_model_dir, use_fast=True)
|
290 |
+
dataset = load_dataset("Salesforce/wikitext", "wikitext-2-raw-v1", split="train")
|
291 |
+
encodings = tokenizer("\n\n".join(dataset["text"]), return_tensors="pt")
|
292 |
+
|
293 |
+
print("Setting random seeds...")
|
294 |
+
random.seed(0)
|
295 |
+
np.random.seed(0)
|
296 |
+
torch.random.manual_seed(0)
|
297 |
+
|
298 |
+
print("Setting calibration samples...")
|
299 |
+
nsamples = 128
|
300 |
+
seqlen = 2048
|
301 |
+
calibration_samples = []
|
302 |
+
for _ in range(nsamples):
|
303 |
+
i = random.randint(0, encodings.input_ids.shape[1] - seqlen - 1)
|
304 |
+
j = i + seqlen
|
305 |
+
input_ids = encodings.input_ids[:, i:j]
|
306 |
+
attention_mask = torch.ones_like(input_ids)
|
307 |
+
calibration_samples.append({"input_ids": input_ids, "attention_mask": attention_mask})
|
308 |
+
|
309 |
+
quantize_config = BaseQuantizeConfig(
|
310 |
+
bits=4, # quantize model to 4-bit
|
311 |
+
group_size=128, # it is recommended to set the value to 128
|
312 |
+
desc_act=True, # set to False can significantly speed up inference but the perplexity may slightly bad
|
313 |
+
sym=True, # using symmetric quantization so that the range is symmetric allowing the value 0 to be precisely represented (can provide speedups)
|
314 |
+
damp_percent=0.1, # see https://github.com/AutoGPTQ/AutoGPTQ/issues/196
|
315 |
+
)
|
316 |
+
|
317 |
+
# load un-quantized model, by default, the model will always be loaded into CPU memory
|
318 |
+
print("Load unquantized model...")
|
319 |
+
model = AutoGPTQForCausalLM.from_pretrained(pretrained_model_dir, quantize_config)
|
320 |
+
|
321 |
+
# quantize model, the examples should be list of dict whose keys can only be "input_ids" and "attention_mask"
|
322 |
+
print("Quantize model with calibration samples...")
|
323 |
+
model.quantize(calibration_samples)
|
324 |
+
|
325 |
+
# save quantized model using safetensors
|
326 |
+
model.save_quantized(quantized_model_dir, use_safetensors=True)
|
327 |
+
```
|