alexmarques commited on
Commit
b441592
·
verified ·
1 Parent(s): d0faa57

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +250 -0
README.md ADDED
@@ -0,0 +1,250 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: mit
3
+ license_link: https://huggingface.co/microsoft/phi-4/resolve/main/LICENSE
4
+ language:
5
+ - en
6
+ pipeline_tag: text-generation
7
+ base_model: microsoft/phi-4
8
+ tags:
9
+ - phi
10
+ - nlp
11
+ - math
12
+ - code
13
+ - chat
14
+ - conversational
15
+ - neuralmagic
16
+ - redhat
17
+ - llmcompressor
18
+ - quantized
19
+ - int8
20
+ ---
21
+
22
+ # phi-4-quantized.w8a8
23
+
24
+ ## Model Overview
25
+ - **Model Architecture:** Phi3ForCausalLM
26
+ - **Input:** Text
27
+ - **Output:** Text
28
+ - **Model Optimizations:**
29
+ - **Activation quantization:** INT8
30
+ - **Weight quantization:** INT8
31
+ - **Intended Use Cases:** This model is designed to accelerate research on language models, for use as a building block for generative AI powered features. It provides uses for general purpose AI systems and applications (primarily in English) which require:
32
+ 1. Memory/compute constrained environments.
33
+ 2. Latency bound scenarios.
34
+ 3. Reasoning and logic.
35
+ - **Out-of-scope:** This model is not specifically designed or evaluated for all downstream purposes, thus:
36
+ 1. Developers should consider common limitations of language models as they select use cases, and evaluate and mitigate for accuracy, safety, and fairness before using within a specific downstream use case, particularly for high-risk scenarios.
37
+ 2. Developers should be aware of and adhere to applicable laws or regulations (including privacy, trade compliance laws, etc.) that are relevant to their use case, including the model’s focus on English.
38
+ 3. Nothing contained in this Model Card should be interpreted as or deemed a restriction or modification to the license the model is released under.
39
+ - **Release Date:** 03/03/2025
40
+ - **Version:** 1.0
41
+ - **Model Developers:** RedHat (Neural Magic)
42
+
43
+
44
+ ### Model Optimizations
45
+
46
+ This model was obtained by quantizing activations and weights of [phi-4](https://huggingface.co/microsoft/phi-4) to INT8 data type.
47
+ 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).
48
+ Weight quantization also reduces disk size requirements by approximately 50%.
49
+
50
+ Only weights and activations of the linear operators within transformers blocks are quantized.
51
+ Weights are quantized with a symmetric static per-channel scheme, whereas activations are quantized with a symmetric dynamic per-token scheme.
52
+ A combination of the [SmoothQuant](https://arxiv.org/abs/2211.10438) and [GPTQ](https://arxiv.org/abs/2210.17323) algorithms is applied for quantization, as implemented in the [llm-compressor](https://github.com/vllm-project/llm-compressor) library.
53
+
54
+
55
+ ## Deployment
56
+
57
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
58
+
59
+ ```python
60
+ from vllm import LLM, SamplingParams
61
+ from transformers import AutoTokenizer
62
+
63
+ model_id = "neuralmagic-ent/phi-4-quantized.w8a8"
64
+ number_gpus = 1
65
+
66
+ sampling_params = SamplingParams(temperature=0.7, top_p=0.8, max_tokens=256)
67
+
68
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
69
+
70
+ prompt = "Give me a short introduction to large language model."
71
+
72
+ llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
73
+
74
+ outputs = llm.generate(prompt, sampling_params)
75
+
76
+ generated_text = outputs[0].outputs[0].text
77
+ print(generated_text)
78
+ ```
79
+
80
+ vLLM aslo supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
81
+
82
+ ## Creation
83
+
84
+ <details>
85
+ <summary>Creation details</summary>
86
+ This model was created with [llm-compressor](https://github.com/vllm-project/llm-compressor) by running the code snippet below.
87
+
88
+
89
+ ```python
90
+ from transformers import AutoModelForCausalLM, AutoTokenizer
91
+ from llmcompressor.modifiers.quantization import GPTQModifier
92
+ from llmcompressor.modifiers.smoothquant import SmoothQuantModifier
93
+ from llmcompressor.transformers import oneshot
94
+ from datasets import load_dataset
95
+
96
+ # Load model
97
+ model_stub = "microsoft/phi-4"
98
+ model_name = model_stub.split("/")[-1]
99
+
100
+ num_samples = 1024
101
+ max_seq_len = 8192
102
+
103
+ tokenizer = AutoTokenizer.from_pretrained(model_stub)
104
+
105
+ model = AutoModelForCausalLM.from_pretrained(
106
+ model_stub,
107
+ device_map="auto",
108
+ torch_dtype="auto",
109
+ )
110
+
111
+ def preprocess_fn(example):
112
+ return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
113
+
114
+ ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
115
+ ds = ds.map(preprocess_fn)
116
+
117
+ # Configure the quantization algorithm and scheme
118
+ recipe = [
119
+ SmoothQuantModifier(
120
+ smoothing_strength=0.7,
121
+ mappings=[
122
+ [["re:.*qkv_proj"], "re:.*input_layernorm"],
123
+ [["re:.*gate_up_proj"], "re:.*post_attention_layernorm"],
124
+ ],
125
+ ),
126
+ GPTQModifier(
127
+ ignore=["lm_head"],
128
+ sequential_targets=["Phi3DecoderLayer"],
129
+ dampening_frac=0.01,
130
+ targets="Linear",
131
+ scheme="W8A8",
132
+ ),
133
+ ]
134
+
135
+ # Apply quantization
136
+ oneshot(
137
+ model=model,
138
+ dataset=ds,
139
+ recipe=recipe,
140
+ max_seq_length=max_seq_len,
141
+ num_calibration_samples=num_samples,
142
+ )
143
+
144
+ # Save to disk in compressed-tensors format
145
+ save_path = model_name + "-quantized.w8a8"
146
+ model.save_pretrained(save_path)
147
+ tokenizer.save_pretrained(save_path)
148
+ print(f"Model and tokenizer saved to: {save_path}")
149
+ ```
150
+ </details>
151
+
152
+
153
+
154
+ ## Evaluation
155
+
156
+ The model was evaluated on the OpenLLM leaderboard tasks (version 1) with the [lm-evaluation-harness](https://github.com/EleutherAI/lm-evaluation-harness) and the [vLLM](https://docs.vllm.ai/en/stable/) engine, using the following command:
157
+ ```
158
+ lm_eval \
159
+ --model vllm \
160
+ --model_args pretrained="neuralmagic-ent/phi-4-quantized.w8a8",dtype=auto,gpu_memory_utilization=0.6,max_model_len=4096,enable_chunk_prefill=True,tensor_parallel_size=1 \
161
+ --tasks openllm \
162
+ --batch_size auto
163
+ ```
164
+
165
+ ### Accuracy
166
+
167
+ #### Open LLM Leaderboard evaluation scores
168
+ <table>
169
+ <tr>
170
+ <td><strong>Benchmark</strong>
171
+ </td>
172
+ <td><strong>phi-4</strong>
173
+ </td>
174
+ <td><strong>phi-4-quantized.w8a8<br>(this model)</strong>
175
+ </td>
176
+ <td><strong>Recovery</strong>
177
+ </td>
178
+ </tr>
179
+ <tr>
180
+ <td>MMLU (5-shot)
181
+ </td>
182
+ <td>80.30
183
+ </td>
184
+ <td>80.39
185
+ </td>
186
+ <td>100.1%
187
+ </td>
188
+ </tr>
189
+ <tr>
190
+ <td>ARC Challenge (25-shot)
191
+ </td>
192
+ <td>64.42
193
+ </td>
194
+ <td>64.33
195
+ </td>
196
+ <td>99.9%
197
+ </td>
198
+ </tr>
199
+ <tr>
200
+ <td>GSM-8K (5-shot, strict-match)
201
+ </td>
202
+ <td>90.07
203
+ </td>
204
+ <td>90.30
205
+ </td>
206
+ <td>100.3%
207
+ </td>
208
+ </tr>
209
+ <tr>
210
+ <td>Hellaswag (10-shot)
211
+ </td>
212
+ <td>84.37
213
+ </td>
214
+ <td>84.30
215
+ </td>
216
+ <td>99.9%
217
+ </td>
218
+ </tr>
219
+ <tr>
220
+ <td>Winogrande (5-shot)
221
+ </td>
222
+ <td>80.58
223
+ </td>
224
+ <td>79.95
225
+ </td>
226
+ <td>99.2%
227
+ </td>
228
+ </tr>
229
+ <tr>
230
+ <td>TruthfulQA (0-shot, mc2)
231
+ </td>
232
+ <td>59.37
233
+ </td>
234
+ <td>58.82
235
+ </td>
236
+ <td>99.1%
237
+ </td>
238
+ </tr>
239
+ <tr>
240
+ <td><strong>Average</strong>
241
+ </td>
242
+ <td><strong>76.52</strong>
243
+ </td>
244
+ <td><strong>76.35</strong>
245
+ </td>
246
+ <td><strong>99.8%</strong>
247
+ </td>
248
+ </tr>
249
+ </table>
250
+