Lin-K76 commited on
Commit
9cc89ac
1 Parent(s): 9a4e5cd

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +253 -0
README.md ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ tags:
3
+ - fp8
4
+ - vllm
5
+ license: gemma
6
+ ---
7
+
8
+ # gemma-2-2b-it-FP8
9
+
10
+ ## Model Overview
11
+ - **Model Architecture:** Gemma 2
12
+ - **Input:** Text
13
+ - **Output:** Text
14
+ - **Model Optimizations:**
15
+ - **Weight quantization:** FP8
16
+ - **Activation quantization:** FP8
17
+ - **Intended Use Cases:** Intended for commercial and research use in English. Similarly to [Meta-Llama-3-8B-Instruct](https://huggingface.co/meta-llama/Meta-Llama-3-8B-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/7/2024
20
+ - **Version:** 1.0
21
+ - **License(s):** [gemma](https://ai.google.dev/gemma/terms)
22
+ - **Model Developers:** Neural Magic
23
+
24
+ Quantized version of [gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it).
25
+ It achieves an average score of 58.41 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 58.80.
26
+
27
+ ### Model Optimizations
28
+
29
+ This model was obtained by quantizing the weights and activations of [gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it) to FP8 data type, ready for inference with vLLM built from source.
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%. In particular, this model can now be loaded and evaluated with a single node of 8xH100 GPUs, as opposed to multiple nodes.
31
+
32
+ Only the weights and activations of the linear operators within transformers blocks are quantized. Symmetric per-tensor quantization is applied, in which a single linear scaling maps the FP8 representations of the quantized weights and activations.
33
+ [LLM Compressor](https://github.com/vllm-project/llm-compressor) is used for quantization with 512 sequences of UltraChat.
34
+
35
+ ## Deployment
36
+
37
+ ### Use with vLLM
38
+
39
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
40
+
41
+ ```python
42
+ from vllm import LLM, SamplingParams
43
+ from transformers import AutoTokenizer
44
+
45
+ model_id = "neuralmagic/gemma-2-2b-it-FP8"
46
+
47
+ sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
48
+
49
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
50
+
51
+ messages = [
52
+ {"role": "user", "content": "Who are you? Please respond in pirate speak!"},
53
+ ]
54
+
55
+ prompts = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
56
+
57
+ llm = LLM(model=model_id)
58
+
59
+ outputs = llm.generate(prompts, sampling_params)
60
+
61
+ generated_text = outputs[0].outputs[0].text
62
+ print(generated_text)
63
+ ```
64
+
65
+ vLLM also supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
66
+
67
+ ## Creation
68
+
69
+ This model was created by applying [LLM Compressor with calibration samples from UltraChat](https://github.com/vllm-project/llm-compressor/blob/sa/big_model_support/examples/big_model_offloading/big_model_w8a8_calibrate.py), as presented in the code snipet below.
70
+
71
+ ```python
72
+ import torch
73
+ from datasets import load_dataset
74
+ from transformers import AutoTokenizer
75
+
76
+ from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
77
+ from llmcompressor.transformers.compression.helpers import (
78
+ calculate_offload_device_map,
79
+ custom_offload_device_map,
80
+ )
81
+
82
+ recipe = """
83
+ quant_stage:
84
+ quant_modifiers:
85
+ QuantizationModifier:
86
+ ignore: ["lm_head"]
87
+ config_groups:
88
+ group_0:
89
+ weights:
90
+ num_bits: 8
91
+ type: float
92
+ strategy: tensor
93
+ dynamic: false
94
+ symmetric: true
95
+ input_activations:
96
+ num_bits: 8
97
+ type: float
98
+ strategy: tensor
99
+ dynamic: false
100
+ symmetric: true
101
+ targets: ["Linear"]
102
+ """
103
+
104
+ model_stub = "neuralmagic/gemma-2-2b-it-FP8"
105
+ model_name = model_stub.split("/")[-1]
106
+
107
+ device_map = calculate_offload_device_map(
108
+ model_stub, reserve_for_hessians=False, num_gpus=1, torch_dtype=torch.float16
109
+ )
110
+
111
+ model = SparseAutoModelForCausalLM.from_pretrained(
112
+ model_stub, torch_dtype=torch.float16, device_map=device_map
113
+ )
114
+ tokenizer = AutoTokenizer.from_pretrained(model_stub)
115
+
116
+ output_dir = f"./{model_name}-FP8"
117
+
118
+ DATASET_ID = "HuggingFaceH4/ultrachat_200k"
119
+ DATASET_SPLIT = "train_sft"
120
+ NUM_CALIBRATION_SAMPLES = 512
121
+ MAX_SEQUENCE_LENGTH = 4096
122
+
123
+ ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
124
+ ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
125
+
126
+ def preprocess(example):
127
+ return {
128
+ "text": tokenizer.apply_chat_template(
129
+ example["messages"],
130
+ tokenize=False,
131
+ )
132
+ }
133
+
134
+ ds = ds.map(preprocess)
135
+
136
+ def tokenize(sample):
137
+ return tokenizer(
138
+ sample["text"],
139
+ padding=False,
140
+ max_length=MAX_SEQUENCE_LENGTH,
141
+ truncation=True,
142
+ add_special_tokens=False,
143
+ )
144
+
145
+ ds = ds.map(tokenize, remove_columns=ds.column_names)
146
+
147
+ oneshot(
148
+ model=model,
149
+ output_dir=output_dir,
150
+ dataset=ds,
151
+ recipe=recipe,
152
+ max_seq_length=MAX_SEQUENCE_LENGTH,
153
+ num_calibration_samples=NUM_CALIBRATION_SAMPLES,
154
+ save_compressed=True,
155
+ )
156
+ ```
157
+
158
+ ## Evaluation
159
+
160
+ 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:
161
+ ```
162
+ lm_eval \
163
+ --model vllm \
164
+ --model_args pretrained="neuralmagic/gemma-2-2b-it-FP8",dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096 \
165
+ --tasks openllm \
166
+ --batch_size auto
167
+ ```
168
+
169
+ ### Accuracy
170
+
171
+ #### Open LLM Leaderboard evaluation scores
172
+ <table>
173
+ <tr>
174
+ <td><strong>Benchmark</strong>
175
+ </td>
176
+ <td><strong>gemma-2-2b-it</strong>
177
+ </td>
178
+ <td><strong>gemma-2-2b-it-FP8(this model)</strong>
179
+ </td>
180
+ <td><strong>Recovery</strong>
181
+ </td>
182
+ </tr>
183
+ <tr>
184
+ <td>MMLU (5-shot)
185
+ </td>
186
+ <td>56.93
187
+ </td>
188
+ <td>56.87
189
+ </td>
190
+ <td>99.89%
191
+ </td>
192
+ </tr>
193
+ <tr>
194
+ <td>ARC Challenge (25-shot)
195
+ </td>
196
+ <td>57.76
197
+ </td>
198
+ <td>57.25
199
+ </td>
200
+ <td>99.12%
201
+ </td>
202
+ </tr>
203
+ <tr>
204
+ <td>GSM-8K (5-shot, strict-match)
205
+ </td>
206
+ <td>45.11
207
+ </td>
208
+ <td>44.35
209
+ </td>
210
+ <td>98.32%
211
+ </td>
212
+ </tr>
213
+ <tr>
214
+ <td>Hellaswag (10-shot)
215
+ </td>
216
+ <td>71.22
217
+ </td>
218
+ <td>70.87
219
+ </td>
220
+ <td>99.51%
221
+ </td>
222
+ </tr>
223
+ <tr>
224
+ <td>Winogrande (5-shot)
225
+ </td>
226
+ <td>68.67
227
+ </td>
228
+ <td>68.11
229
+ </td>
230
+ <td>99.18%
231
+ </td>
232
+ </tr>
233
+ <tr>
234
+ <td>TruthfulQA (0-shot)
235
+ </td>
236
+ <td>53.11
237
+ </td>
238
+ <td>52.99
239
+ </td>
240
+ <td>99.77%
241
+ </td>
242
+ </tr>
243
+ <tr>
244
+ <td><strong>Average</strong>
245
+ </td>
246
+ <td><strong>58.80</strong>
247
+ </td>
248
+ <td><strong>58.41</strong>
249
+ </td>
250
+ <td><strong>99.33%</strong>
251
+ </td>
252
+ </tr>
253
+ </table>