alexmarques commited on
Commit
3712ba0
1 Parent(s): f05e0ea

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +263 -0
README.md ADDED
@@ -0,0 +1,263 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ pipeline_tag: text-generation
5
+ license: apache-2.0
6
+ license_link: https://www.apache.org/licenses/LICENSE-2.0
7
+ ---
8
+
9
+ # Qwen2-0.5B-Instruct-quantized.w8a8
10
+
11
+ ## Model Overview
12
+ - **Model Architecture:** Qwen2
13
+ - **Input:** Text
14
+ - **Output:** Text
15
+ - **Model Optimizations:**
16
+ - **Activation quantization:** INT8
17
+ - **Weight quantization:** INT8
18
+ - **Intended Use Cases:** Intended for commercial and research use in English. Similarly to [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct), this models is intended for assistant-like chat.
19
+ - **Out-of-scope:** Use in any manner that violates applicable laws or regulations (including trade compliance laws). Use in languages other than English.
20
+ - **Release Date:** 7/12/2024
21
+ - **Version:** 1.0
22
+ - **License(s):** [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)
23
+ - **Model Developers:** Neural Magic
24
+
25
+ Quantized version of [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct).
26
+ It achieves an average score of 69.09 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 69.64.
27
+
28
+ ### Model Optimizations
29
+
30
+ This model was obtained by quantizing the weights of [Qwen2-7B-Instruct](https://huggingface.co/Qwen/Qwen2-7B-Instruct) to INT8 data type.
31
+ This optimization reduces the number of bits used to represent weights and activations from 16 to 8, reducing GPU memory requirements (by approximately 50%) and increasing matrix-multiply compute throughput (by approximately 2x).
32
+ Weight quantization also reduces disk size requirements by approximately 50%.
33
+
34
+ Only weights and activations of the linear operators within transformers blocks are quantized.
35
+ Weights are quantized with a symmetric static per-channel scheme, where a fixed linear scaling factor is applied between INT8 and floating point representations for each output channel dimension.
36
+ Activations are quantized with a symmetric dynamic per-token scheme, computing a linear scaling factor at runtime for each token between INT8 and floating point representations.
37
+ The [GPTQ](https://arxiv.org/abs/2210.17323) algorithm is applied for quantization, as implemented in the [llm-compressor](https://github.com/vllm-project/llm-compressor) library.
38
+ GPTQ used a 1% damping factor and 256 sequences of 8,192 random tokens.
39
+
40
+
41
+ ## Deployment
42
+
43
+ ### Use with vLLM
44
+
45
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
46
+
47
+ ```python
48
+ from vllm import LLM, SamplingParams
49
+ from transformers import AutoTokenizer
50
+
51
+ model_id = "neuralmagic/Qwen2-7B-Instruct-quantized.w8a8"
52
+ number_gpus = 1
53
+
54
+ sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
55
+
56
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
57
+
58
+ messages = [
59
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
60
+ {"role": "user", "content": "Who are you?"},
61
+ ]
62
+
63
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
64
+
65
+ llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
66
+
67
+ outputs = llm.generate(prompts, sampling_params)
68
+
69
+ generated_text = outputs[0].outputs[0].text
70
+ print(generated_text)
71
+ ```
72
+
73
+ vLLM aslo supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
74
+
75
+ ### Use with transformers
76
+
77
+ The following example contemplates how the model can be deployed in Transformers using the `generate()` function.
78
+
79
+ ```python
80
+ from transformers import AutoTokenizer, AutoModelForCausalLM
81
+
82
+ model_id = "neuralmagic/Qwen2-7B-Instruct-quantized.w8a8"
83
+
84
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
85
+ model = AutoModelForCausalLM.from_pretrained(
86
+ model_id,
87
+ torch_dtype="auto",
88
+ device_map="auto",
89
+ )
90
+
91
+ messages = [
92
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
93
+ {"role": "user", "content": "Who are you?"},
94
+ ]
95
+
96
+ input_ids = tokenizer.apply_chat_template(
97
+ messages,
98
+ add_generation_prompt=True,
99
+ return_tensors="pt"
100
+ ).to(model.device)
101
+
102
+ terminators = [
103
+ tokenizer.eos_token_id,
104
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
105
+ ]
106
+
107
+ outputs = model.generate(
108
+ input_ids,
109
+ max_new_tokens=256,
110
+ eos_token_id=terminators,
111
+ do_sample=True,
112
+ temperature=0.7,
113
+ top_p=0.8,
114
+ )
115
+ response = outputs[0][input_ids.shape[-1]:]
116
+ print(tokenizer.decode(response, skip_special_tokens=True))
117
+ ```
118
+
119
+ ## Creation
120
+
121
+ This model was created by using the [llm-compressor](https://github.com/vllm-project/llm-compressor) library as presented in the code snipet below.
122
+
123
+ ```python
124
+ from transformers import AutoTokenizer
125
+ from datasets import Dataset
126
+ from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
127
+ from llmcompressor.modifiers.quantization import GPTQModifier
128
+ import random
129
+
130
+ model_id = "Qwen/Qwen2-7B-Instruct"
131
+
132
+ num_samples = 256
133
+ max_seq_len = 8192
134
+
135
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
136
+
137
+ max_token_id = len(tokenizer.get_vocab()) - 1
138
+ input_ids = [[random.randint(0, max_token_id) for _ in range(max_seq_len)] for _ in range(num_samples)]
139
+ attention_mask = num_samples * [max_seq_len * [1]]
140
+ ds = Dataset.from_dict({"input_ids": input_ids, "attention_mask": attention_mask})
141
+
142
+ recipe = GPTQModifier(
143
+ targets="Linear",
144
+ scheme="W8A8",
145
+ ignore=["lm_head"],
146
+ dampening_frac=0.01,
147
+ )
148
+
149
+ model = SparseAutoModelForCausalLM.from_pretrained(
150
+ model_id,
151
+ device_map="auto",
152
+ trust_remote_code=True,
153
+ )
154
+
155
+ oneshot(
156
+ model=model,
157
+ dataset=ds,
158
+ recipe=recipe,
159
+ max_seq_length=max_seq_len,
160
+ num_calibration_samples=num_samples,
161
+ )
162
+
163
+ model.save_pretrained("Qwen2-7B-Instruct-quantized.w8a18)
164
+ ```
165
+
166
+
167
+
168
+ ## Evaluation
169
+
170
+ The model was evaluated on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) leaderboard tasks (version 1) with the [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness/tree/383bbd54bc621086e05aa1b030d8d4d5635b25e6) (commit 383bbd54bc621086e05aa1b030d8d4d5635b25e6) and the [vLLM](https://docs.vllm.ai/en/stable/) engine, using the following command:
171
+ ```
172
+ lm_eval \
173
+ --model vllm \
174
+ --model_args pretrained="neuralmagic/Qwen2-7B-Instruct-quantized.w8a8",dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
175
+ --tasks openllm \
176
+ --batch_size auto
177
+ ```
178
+
179
+ ### Accuracy
180
+
181
+ #### Open LLM Leaderboard evaluation scores
182
+ <table>
183
+ <tr>
184
+ <td><strong>Benchmark</strong>
185
+ </td>
186
+ <td><strong>Qwen2-7B-Instruct</strong>
187
+ </td>
188
+ <td><strong>Qwen2-7B-Instruct-quantized.w8a8 (this model)</strong>
189
+ </td>
190
+ <td><strong>Recovery</strong>
191
+ </td>
192
+ </tr>
193
+ <tr>
194
+ <td>MMLU (5-shot)
195
+ </td>
196
+ <td>70.78
197
+ </td>
198
+ <td>70.27
199
+ </td>
200
+ <td>99.3%
201
+ </td>
202
+ </tr>
203
+ <tr>
204
+ <td>ARC Challenge (25-shot)
205
+ </td>
206
+ <td>62.29
207
+ </td>
208
+ <td>62.71
209
+ </td>
210
+ <td>100.7%
211
+ </td>
212
+ </tr>
213
+ <tr>
214
+ <td>GSM-8K (5-shot, strict-match)
215
+ </td>
216
+ <td>69.14
217
+ </td>
218
+ <td>66.94
219
+ </td>
220
+ <td>96.8%
221
+ </td>
222
+ </tr>
223
+ <tr>
224
+ <td>Hellaswag (10-shot)
225
+ </td>
226
+ <td>81.76
227
+ </td>
228
+ <td>81.31
229
+ </td>
230
+ <td>99.5%
231
+ </td>
232
+ </tr>
233
+ <tr>
234
+ <td>Winogrande (5-shot)
235
+ </td>
236
+ <td>76.56
237
+ </td>
238
+ <td>75.77
239
+ </td>
240
+ <td>99.0%
241
+ </td>
242
+ </tr>
243
+ <tr>
244
+ <td>TruthfulQA (0-shot)
245
+ </td>
246
+ <td>57.31
247
+ </td>
248
+ <td>57.55
249
+ </td>
250
+ <td>100.4%
251
+ </td>
252
+ </tr>
253
+ <tr>
254
+ <td><strong>Average</strong>
255
+ </td>
256
+ <td><strong>69.64</strong>
257
+ </td>
258
+ <td><strong>69.09</strong>
259
+ </td>
260
+ <td><strong>99.2%</strong>
261
+ </td>
262
+ </tr>
263
+ </table>