alexmarques commited on
Commit
25992a4
1 Parent(s): a35502e

Create README.md

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