Commit
•
f6fc7c3
1
Parent(s):
028abac
Update README.md
Browse files
README.md
CHANGED
@@ -1,199 +1,281 @@
|
|
1 |
---
|
|
|
|
|
|
|
2 |
library_name: transformers
|
3 |
-
|
|
|
|
|
|
|
|
|
4 |
---
|
5 |
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
|
|
|
10 |
|
|
|
11 |
|
12 |
-
## Model
|
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 |
-
|
34 |
-
|
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 |
+
license: gemma
|
3 |
+
language:
|
4 |
+
- en
|
5 |
library_name: transformers
|
6 |
+
pipeline_tag: text-generation
|
7 |
+
tags:
|
8 |
+
- gemma2
|
9 |
+
- google
|
10 |
+
- autoawq
|
11 |
---
|
12 |
|
13 |
+
> [!IMPORTANT]
|
14 |
+
> This repository is a community-driven quantized version of the original model [`google/gemma-2-9b-it`](https://huggingface.co/google/gemma-2-9b-it) which is the BF16 half-precision official version released by Meta AI.
|
15 |
|
16 |
+
## Model Information
|
17 |
|
18 |
+
Gemma is a family of lightweight, state-of-the-art open models from Google, built from the same research and technology used to create the Gemini models. They are text-to-text, decoder-only large language models, available in English, with open weights for both pre-trained variants and instruction-tuned variants. Gemma models are well-suited for a variety of text generation tasks, including question answering, summarization, and reasoning. Their relatively small size makes it possible to deploy them in environments with limited resources such as a laptop, desktop or your own cloud infrastructure, democratizing access to state of the art AI models and helping foster innovation for everyone.
|
19 |
|
20 |
+
This repository contains [`google/gemma-2-9b-it`](https://huggingface.co/google/gemma-2-9b-it) 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.
|
21 |
|
22 |
+
## Model Usage
|
23 |
|
24 |
+
> [!NOTE]
|
25 |
+
> In order to run the inference with Gemma2 9B Instruct AWQ in INT4, around 6 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.
|
26 |
|
27 |
+
In order to use the current quantized model, support is offered for different solutions as `transformers`, `autoawq`, or `text-generation-inference`.
|
28 |
|
29 |
+
### 🤗 Transformers
|
30 |
|
31 |
+
In order to run the inference with Gemma2 9B Instruct AWQ in INT4, you need to install the following packages:
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
+
```bash
|
34 |
+
pip install -q --upgrade transformers autoawq accelerate
|
35 |
+
```
|
36 |
|
37 |
+
To run the inference on top of Gemma2 9B 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.
|
38 |
|
39 |
+
```python
|
40 |
+
import torch
|
41 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer, AwqConfig
|
42 |
|
43 |
+
model_id = "hugging-quants/gemma-2-9b-it-AWQ-INT4"
|
44 |
|
45 |
+
quantization_config = AwqConfig(
|
46 |
+
bits=4,
|
47 |
+
fuse_max_seq_len=512, # Note: Update this as per your use-case
|
48 |
+
do_fuse=True,
|
49 |
+
)
|
50 |
|
51 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
52 |
+
model = AutoModelForCausalLM.from_pretrained(
|
53 |
+
model_id,
|
54 |
+
torch_dtype=torch.float16,
|
55 |
+
low_cpu_mem_usage=True,
|
56 |
+
device_map="auto",
|
57 |
+
quantization_config=quantization_config
|
58 |
+
)
|
59 |
+
|
60 |
+
prompt = [
|
61 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
62 |
+
]
|
63 |
+
inputs = tokenizer.apply_chat_template(
|
64 |
+
prompt,
|
65 |
+
tokenize=True,
|
66 |
+
add_generation_prompt=True,
|
67 |
+
return_tensors="pt",
|
68 |
+
return_dict=True,
|
69 |
+
).to("cuda")
|
70 |
+
|
71 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
72 |
+
print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
|
73 |
+
```
|
74 |
+
|
75 |
+
### AutoAWQ
|
76 |
+
|
77 |
+
In order to run the inference with Gemma2 9B Instruct AWQ in INT4, you need to install the following packages:
|
78 |
+
|
79 |
+
```bash
|
80 |
+
pip install -q --upgrade transformers autoawq accelerate
|
81 |
+
```
|
82 |
+
|
83 |
+
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.
|
84 |
+
|
85 |
+
```python
|
86 |
+
import torch
|
87 |
+
from awq import AutoAWQForCausalLM
|
88 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
89 |
+
|
90 |
+
model_id = "hugging-quants/gemma-2-9b-it-AWQ-INT4"
|
91 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
92 |
+
model = AutoAWQForCausalLM.from_pretrained(
|
93 |
+
model_id,
|
94 |
+
torch_dtype=torch.float16,
|
95 |
+
low_cpu_mem_usage=True,
|
96 |
+
device_map="auto",
|
97 |
+
)
|
98 |
+
|
99 |
+
prompt = [
|
100 |
+
{"role": "user", "content": "What's Deep Learning?"},
|
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 |
+
outputs = model.generate(**inputs, do_sample=True, max_new_tokens=256)
|
111 |
+
print(tokenizer.batch_decode(outputs[:, inputs['input_ids'].shape[1]:], skip_special_tokens=True)[0])
|
112 |
+
```
|
113 |
+
|
114 |
+
The AutoAWQ script has been adapted from [`AutoAWQ/examples/generate.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/generate.py).
|
115 |
+
|
116 |
+
### 🤗 Text Generation Inference (TGI)
|
117 |
+
|
118 |
+
To run the `text-generation-launcher` with Gemma2 9B 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.
|
119 |
+
|
120 |
+
```bash
|
121 |
+
pip install -q --upgrade huggingface_hub
|
122 |
+
huggingface-cli login
|
123 |
+
```
|
124 |
+
|
125 |
+
Then you just need to run the TGI v2.2.0 (or higher) Docker container as follows:
|
126 |
+
|
127 |
+
```bash
|
128 |
+
docker run --gpus all --shm-size 1g -ti -p 8080:80 \
|
129 |
+
-v hf_cache:/data \
|
130 |
+
-e MODEL_ID=hugging-quants/gemma-2-9b-it-AWQ-INT4 \
|
131 |
+
-e QUANTIZE=awq \
|
132 |
+
-e HF_TOKEN=$(cat ~/.cache/huggingface/token) \
|
133 |
+
-e MAX_INPUT_LENGTH=4000 \
|
134 |
+
-e MAX_TOTAL_TOKENS=4096 \
|
135 |
+
ghcr.io/huggingface/text-generation-inference:2.2.0
|
136 |
+
```
|
137 |
+
|
138 |
+
> [!NOTE]
|
139 |
+
> TGI will expose different endpoints, to see all the endpoints available check [TGI OpenAPI Specification](https://huggingface.github.io/text-generation-inference/#/).
|
140 |
+
|
141 |
+
To send request to the deployed TGI endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
142 |
+
|
143 |
+
```bash
|
144 |
+
curl 0.0.0.0:8080/v1/chat/completions \
|
145 |
+
-X POST \
|
146 |
+
-H 'Content-Type: application/json' \
|
147 |
+
-d '{
|
148 |
+
"model": "tgi",
|
149 |
+
"messages": [
|
150 |
+
{
|
151 |
+
"role": "user",
|
152 |
+
"content": "What is Deep Learning?"
|
153 |
+
}
|
154 |
+
],
|
155 |
+
"max_tokens": 128
|
156 |
+
}'
|
157 |
+
```
|
158 |
+
|
159 |
+
Or programatically via the `huggingface_hub` Python client as follows:
|
160 |
+
|
161 |
+
```python
|
162 |
+
import os
|
163 |
+
from huggingface_hub import InferenceClient
|
164 |
+
|
165 |
+
client = InferenceClient(base_url="http://0.0.0.0:8080", api_key=os.getenv("HF_TOKEN", "-"))
|
166 |
+
|
167 |
+
chat_completion = client.chat.completions.create(
|
168 |
+
model="hugging-quants/gemma-2-9b-it-AWQ-INT4",
|
169 |
+
messages=[
|
170 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
171 |
+
],
|
172 |
+
max_tokens=128,
|
173 |
+
)
|
174 |
+
```
|
175 |
+
|
176 |
+
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:
|
177 |
+
|
178 |
+
```python
|
179 |
+
import os
|
180 |
+
from openai import OpenAI
|
181 |
+
|
182 |
+
client = OpenAI(base_url="http://0.0.0.0:8080/v1", api_key=os.getenv("OPENAI_API_KEY", "-"))
|
183 |
+
|
184 |
+
chat_completion = client.chat.completions.create(
|
185 |
+
model="tgi",
|
186 |
+
messages=[
|
187 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
188 |
+
],
|
189 |
+
max_tokens=128,
|
190 |
+
)
|
191 |
+
```
|
192 |
+
|
193 |
+
### vLLM
|
194 |
+
|
195 |
+
To run vLLM with Gemma2 9B 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:
|
196 |
+
|
197 |
+
```bash
|
198 |
+
docker run --runtime nvidia --gpus all --ipc=host -p 8000:8000 \
|
199 |
+
-v hf_cache:/root/.cache/huggingface \
|
200 |
+
vllm/vllm-openai:latest \
|
201 |
+
--model hugging-quants/gemma-2-9b-it-AWQ-INT4 \
|
202 |
+
--max-model-len 4096
|
203 |
+
```
|
204 |
+
|
205 |
+
To send request to the deployed vLLM endpoint compatible with [OpenAI OpenAPI specification](https://github.com/openai/openai-openapi) i.e. `/v1/chat/completions`:
|
206 |
+
|
207 |
+
```bash
|
208 |
+
curl 0.0.0.0:8000/v1/chat/completions \
|
209 |
+
-X POST \
|
210 |
+
-H 'Content-Type: application/json' \
|
211 |
+
-d '{
|
212 |
+
"model": "hugging-quants/gemma-2-9b-it-AWQ-INT4",
|
213 |
+
"messages": [
|
214 |
+
{
|
215 |
+
"role": "user",
|
216 |
+
"content": "What is Deep Learning?"
|
217 |
+
}
|
218 |
+
],
|
219 |
+
"max_tokens": 128
|
220 |
+
}'
|
221 |
+
```
|
222 |
+
|
223 |
+
Or programatically via the `openai` Python client (see [installation notes](https://github.com/openai/openai-python?tab=readme-ov-file#installation)) as follows:
|
224 |
+
|
225 |
+
```python
|
226 |
+
import os
|
227 |
+
from openai import OpenAI
|
228 |
+
|
229 |
+
client = OpenAI(base_url="http://0.0.0.0:8000/v1", api_key=os.getenv("VLLM_API_KEY", "-"))
|
230 |
+
|
231 |
+
chat_completion = client.chat.completions.create(
|
232 |
+
model="hugging-quants/gemma-2-9b-it-AWQ-INT4",
|
233 |
+
messages=[
|
234 |
+
{"role": "user", "content": "What is Deep Learning?"},
|
235 |
+
],
|
236 |
+
max_tokens=128,
|
237 |
+
)
|
238 |
+
```
|
239 |
+
|
240 |
+
## Quantization Reproduction
|
241 |
+
|
242 |
+
> [!NOTE]
|
243 |
+
> In order to quantize Gemma2 9B Instruct using AutoAWQ, you will need to use an instance with at least enough CPU RAM to fit the whole model i.e. ~20GiB, and an NVIDIA GPU with 16GiB of VRAM to quantize it.
|
244 |
+
|
245 |
+
In order to quantize Gemma2 9B Instruct, first install the following packages:
|
246 |
+
|
247 |
+
```bash
|
248 |
+
pip install -q --upgrade "torch==2.3.0" transformers accelerate
|
249 |
+
INSTALL_KERNELS=1 pip install -q git+https://github.com/casper-hansen/AutoAWQ.git@79547665bdb27768a9b392ef375776b020acbf0c
|
250 |
+
```
|
251 |
+
|
252 |
+
Then run the following script, adapted from [`AutoAWQ/examples/quantize.py`](https://github.com/casper-hansen/AutoAWQ/blob/main/examples/quantize.py):
|
253 |
+
|
254 |
+
```python
|
255 |
+
from awq import AutoAWQForCausalLM
|
256 |
+
from transformers import AutoTokenizer
|
257 |
+
|
258 |
+
model_path = "google/gemma-2-9b-it"
|
259 |
+
quant_path = "hugging-quants/gemma-2-9b-it-AWQ-INT4"
|
260 |
+
quant_config = {
|
261 |
+
"zero_point": True,
|
262 |
+
"q_group_size": 128,
|
263 |
+
"w_bit": 4,
|
264 |
+
"version": "GEMM",
|
265 |
+
}
|
266 |
+
|
267 |
+
# Load model
|
268 |
+
model = AutoAWQForCausalLM.from_pretrained(
|
269 |
+
model_path, low_cpu_mem_usage=True, use_cache=False,
|
270 |
+
)
|
271 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
272 |
+
|
273 |
+
# Quantize
|
274 |
+
model.quantize(tokenizer, quant_config=quant_config)
|
275 |
+
|
276 |
+
# Save quantized model
|
277 |
+
model.save_quantized(quant_path)
|
278 |
+
tokenizer.save_pretrained(quant_path)
|
279 |
+
|
280 |
+
print(f'Model is quantized and saved at "{quant_path}"')
|
281 |
+
```
|