alexmarques commited on
Commit
37336cc
·
verified ·
1 Parent(s): e36a076

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
+ license: mit
6
+ base_model: microsoft/Phi-3-small-128k-instruct
7
+ ---
8
+
9
+ # Phi-3-small-128k-instruct-quantized.w8a16
10
+
11
+ ## Model Overview
12
+ - **Model Architecture:** Phi-3
13
+ - **Input:** Text
14
+ - **Output:** Text
15
+ - **Model Optimizations:**
16
+ - **Weight quantization:** INT8
17
+ - **Intended Use Cases:** Intended for commercial and research use in English. Similarly to [Phi-3-small-128k-instruct](https://huggingface.co/microsoft/Phi-3-small-128k-instruct), this models is intended for assistant-like chat.
18
+ - **Out-of-scope:** Use in any manner that violates applicable laws or regulations (including trade compliance laws). Use in languages other than English.
19
+ - **Release Date:** 8/14/2024
20
+ - **Version:** 1.0
21
+ - **License(s):** [MIT](https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/mit.md)
22
+ - **Model Developers:** Neural Magic
23
+
24
+ Quantized version of [Phi-3-small-128k-instruct](https://huggingface.co/microsoft/Phi-3-small-128k-instruct), a 3.8 billion-parameter open model trained using the Phi-3 datasets.
25
+ It achieves an average score of 75.85 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 75.80.
26
+
27
+ ### Model Optimizations
28
+
29
+ This model was obtained by quantizing the weights of [Phi-3-small-128k-instruct](https://huggingface.co/microsoft/Phi-3-small-128k-instruct) to INT8 data type.
30
+ This optimization reduces the number of bits per parameter from 16 to 8, reducing the disk size and GPU memory requirements by approximately 50%.
31
+
32
+ Only the weights of the linear operators within transformers blocks are quantized. Symmetric per-channel quantization is applied, in which a linear scaling per output dimension maps the INT8 and floating point representations of the quantized weights.
33
+ 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. GPTQ used a 1% damping factor and 256 sequences of 8,192 random tokens.
34
+
35
+
36
+ ## Deployment
37
+
38
+ ### Use with vLLM
39
+
40
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
41
+
42
+ ```python
43
+ from vllm import LLM, SamplingParams
44
+ from transformers import AutoTokenizer
45
+
46
+ model_id = "neuralmagic/Phi-3-small-128k-instruct-quantized.w8a16"
47
+ number_gpus = 1
48
+
49
+ sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
50
+
51
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
52
+
53
+ messages = [
54
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
55
+ {"role": "user", "content": "Who are you?"},
56
+ ]
57
+
58
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
59
+
60
+ llm = LLM(model=model_id, trust_remote_code=True, max_model_len=8196, tensor_parallel_size=number_gpus)
61
+
62
+ outputs = llm.generate(prompts, sampling_params)
63
+
64
+ generated_text = outputs[0].outputs[0].text
65
+ print(generated_text)
66
+ ```
67
+
68
+
69
+ vLLM aslo supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
70
+
71
+ ### Use with transformers
72
+
73
+ The following example contemplates how the model can be deployed in Transformers using the `generate()` function.
74
+
75
+ ```python
76
+ from transformers import AutoTokenizer, AutoModelForCausalLM
77
+
78
+ model_id = "neuralmagic/Phi-3-small-128k-instruct-quantized.w8a16"
79
+
80
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
81
+ model = AutoModelForCausalLM.from_pretrained(
82
+ model_id,
83
+ torch_dtype="auto",
84
+ device_map="auto",
85
+ trust_remote_code=True,
86
+ )
87
+
88
+ messages = [
89
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
90
+ {"role": "user", "content": "Who are you?"},
91
+ ]
92
+
93
+ input_ids = tokenizer.apply_chat_template(
94
+ messages,
95
+ add_generation_prompt=True,
96
+ return_tensors="pt"
97
+ ).to(model.device)
98
+
99
+ outputs = model.generate(
100
+ input_ids,
101
+ max_new_tokens=256,
102
+ do_sample=True,
103
+ temperature=0.6,
104
+ top_p=0.9,
105
+ )
106
+ response = outputs[0][input_ids.shape[-1]:]
107
+ print(tokenizer.decode(response, skip_special_tokens=True))
108
+ ```
109
+
110
+ ## Creation
111
+
112
+ This model was created by using the [llm-compressor](https://github.com/vllm-project/llm-compressor) library as presented in the code snipet below.
113
+
114
+ ```python
115
+ from transformers import AutoTokenizer
116
+ from datasets import Dataset
117
+ from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
118
+ from llmcompressor.modifiers.quantization import GPTQModifier
119
+ import random
120
+
121
+ model_id = "microsoft/Phi-3-small-128k-instruct"
122
+
123
+ num_samples = 256
124
+ max_seq_len = 8192
125
+
126
+ tokenizer = AutoTokenizer.from_pretrained(model_id, trust_remote_code=True)
127
+
128
+ max_token_id = len(tokenizer.get_vocab()) - 1
129
+ input_ids = [[random.randint(0, max_token_id) for _ in range(max_seq_len)] for _ in range(num_samples)]
130
+ attention_mask = num_samples * [max_seq_len * [1]]
131
+ ds = Dataset.from_dict({"input_ids": input_ids, "attention_mask": attention_mask})
132
+
133
+ recipe = GPTQModifier(
134
+ targets="Linear",
135
+ scheme="W8A16",
136
+ ignore=["lm_head"],
137
+ dampening_frac=0.01,
138
+ )
139
+
140
+ model = SparseAutoModelForCausalLM.from_pretrained(
141
+ model_id,
142
+ device_map="auto",
143
+ trust_remote_code=True,
144
+ )
145
+
146
+ oneshot(
147
+ model=model,
148
+ dataset=ds,
149
+ recipe=recipe,
150
+ max_seq_length=max_seq_len,
151
+ num_calibration_samples=num_samples,
152
+ tokenizer=tokenizer,
153
+ )
154
+
155
+ model.save_pretrained("Phi-3-small-128k-instruct-quantized.w8a16")
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-small-128k-instruct-quantized.w8a16",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-small-128k-instruct </strong>
179
+ </td>
180
+ <td><strong>Phi-3-small-128k-instruct-quantized.w8a16 (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>75.25
189
+ </td>
190
+ <td>75.30
191
+ </td>
192
+ <td>100.1%
193
+ </td>
194
+ </tr>
195
+ <tr>
196
+ <td>ARC Challenge (25-shot)
197
+ </td>
198
+ <td>70.31
199
+ </td>
200
+ <td>70.05
201
+ </td>
202
+ <td>99.6%
203
+ </td>
204
+ </tr>
205
+ <tr>
206
+ <td>GSM-8K (5-shot, strict-match)
207
+ </td>
208
+ <td>83.40
209
+ </td>
210
+ <td>83.62
211
+ </td>
212
+ <td>100.3%
213
+ </td>
214
+ </tr>
215
+ <tr>
216
+ <td>Hellaswag (10-shot)
217
+ </td>
218
+ <td>85.02
219
+ </td>
220
+ <td>85.06
221
+ </td>
222
+ <td>100.0%
223
+ </td>
224
+ </tr>
225
+ <tr>
226
+ <td>Winogrande (5-shot)
227
+ </td>
228
+ <td>77.82
229
+ </td>
230
+ <td>78.06
231
+ </td>
232
+ <td>100.3%
233
+ </td>
234
+ </tr>
235
+ <tr>
236
+ <td>TruthfulQA (0-shot)
237
+ </td>
238
+ <td>63.02
239
+ </td>
240
+ <td>63.00
241
+ </td>
242
+ <td>100.0%
243
+ </td>
244
+ </tr>
245
+ <tr>
246
+ <td><strong>Average</strong>
247
+ </td>
248
+ <td><strong>75.80</strong>
249
+ </td>
250
+ <td><strong>75.85</strong>
251
+ </td>
252
+ <td><strong>100.1%</strong>
253
+ </td>
254
+ </tr>
255
+ </table>