abhinavnmagic commited on
Commit
cbab0bc
1 Parent(s): 72f987e

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +262 -0
README.md ADDED
@@ -0,0 +1,262 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ pipeline_tag: text-generation
5
+ license: llama2
6
+ ---
7
+
8
+ # gemma-2-2b-it-quantized.w4a16
9
+
10
+ ## Model Overview
11
+ - **Model Architecture:** Gemma-2
12
+ - **Input:** Text
13
+ - **Output:** Text
14
+ - **Model Optimizations:**
15
+ - **Weight quantization:** INT4
16
+ - **Intended Use Cases:** Intended for commercial and research use in English. Similarly to [gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it), 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:** 8/16/2024
19
+ - **Version:** 1.0
20
+ - **License(s)**: [LLama2](https://huggingface.co/google/gemma-2-2b-it/blob/main/LICENSE.txt)
21
+ - **Model Developers:** Neural Magic
22
+
23
+ Quantized version of [gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it).
24
+ It achieves an average score of 57.75 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 58.8.
25
+
26
+ ### Model Optimizations
27
+
28
+ This model was obtained by quantizing the weights of [gemma-2-2b-it](https://huggingface.co/google/gemma-2-2b-it) to INT4 data type.
29
+ This optimization reduces the number of bits per parameter from 16 to 4, reducing the disk size and GPU memory requirements by approximately 75%.
30
+
31
+ Only the weights of the linear operators within transformers blocks are quantized. Symmetric group-wise quantization is applied, in which a linear scaling per group maps the INT4 and floating point representations of the quantized weights.
32
+ 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. Quantization is performed with 10% damping factor, group-size as 128 and 512 sequences sampled from [Open-Platypus](https://huggingface.co/datasets/garage-bAInd/Open-Platypus).
33
+
34
+ ## Deployment
35
+
36
+ ### Use with vLLM
37
+
38
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
39
+
40
+ ```python
41
+ from vllm import LLM, SamplingParams
42
+ from transformers import AutoTokenizer
43
+
44
+ model_id = "neuralmagic/gemma-2-2b-it-quantized.w4a16"
45
+
46
+ sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
47
+
48
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
49
+
50
+ messages = [
51
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
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, tensor_parallel_size=2)
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 aslo supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
66
+
67
+ ### Use with transformers
68
+
69
+ The following example contemplates how the model can be deployed in Transformers using the `generate()` function.
70
+
71
+ ```python
72
+ from transformers import AutoTokenizer, AutoModelForCausalLM
73
+
74
+ model_id = "neuralmagic/gemma-2-2b-it-quantized.w4a16"
75
+
76
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
77
+ model = AutoModelForCausalLM.from_pretrained(
78
+ model_id,
79
+ torch_dtype="auto",
80
+ device_map="auto",
81
+ )
82
+
83
+ messages = [
84
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
85
+ {"role": "user", "content": "Who are you? Please respond in pirate speak"},
86
+ ]
87
+
88
+ input_ids = tokenizer.apply_chat_template(
89
+ messages,
90
+ add_generation_prompt=True,
91
+ return_tensors="pt"
92
+ ).to(model.device)
93
+
94
+ terminators = [
95
+ tokenizer.eos_token_id,
96
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
97
+ ]
98
+
99
+ outputs = model.generate(
100
+ input_ids,
101
+ max_new_tokens=256,
102
+ eos_token_id=terminators,
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 llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
118
+ from llmcompressor.modifiers.quantization import GPTQModifier
119
+ from datasets import load_dataset
120
+ import random
121
+
122
+ model_id = "google/gemma-2-2b-it"
123
+
124
+ num_samples = 512
125
+ max_seq_len = 4096
126
+
127
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
128
+
129
+ preprocess_fn = lambda example: {"text": "Below is an instruction that describes a task. Write a response that appropriately completes the request.\n\n{text}".format_map(example)}
130
+
131
+ dataset_name = "neuralmagic/LLM_compression_calibration"
132
+ dataset = load_dataset(dataset_name, split="train")
133
+ ds = dataset.shuffle().select(range(num_samples))
134
+ ds = ds.map(preprocess_fn)
135
+
136
+ examples = [
137
+ tokenizer(
138
+ example["text"], padding=False, max_length=max_seq_len, truncation=True,
139
+ ) for example in ds
140
+ ]
141
+
142
+ recipe = GPTQModifier(
143
+ targets="Linear",
144
+ scheme="W4A16",
145
+ ignore=["lm_head"],
146
+ dampening_frac=0.1,
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("gemma-2-2b-it-quantized.w4a16")
164
+ ```
165
+
166
+
167
+ ## Evaluation
168
+
169
+ 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:
170
+ ```
171
+ lm_eval \
172
+ --model vllm \
173
+ --model_args pretrained="neuralmagic/gemma-2-2b-it-quantized.w4a16",dtype=auto,tensor_parallel_size=2,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096,trust_remote_code=True \
174
+ --tasks openllm \
175
+ --batch_size auto
176
+ ```
177
+
178
+ ### Accuracy
179
+
180
+ #### Open LLM Leaderboard evaluation scores
181
+ <table>
182
+ <tr>
183
+ <td><strong>Benchmark</strong>
184
+ </td>
185
+ <td><strong>gemma-2-2b-it </strong>
186
+ </td>
187
+ <td><strong>gemma-2-2b-it-quantized.w4a16(this model)</strong>
188
+ </td>
189
+ <td><strong>Recovery</strong>
190
+ </td>
191
+ </tr>
192
+ <tr>
193
+ <td>MMLU (5-shot)
194
+ </td>
195
+ <td>56.93
196
+ </td>
197
+ <td>55.98
198
+ </td>
199
+ <td>98.33%
200
+ </td>
201
+ </tr>
202
+ <tr>
203
+ <td>ARC Challenge (25-shot)
204
+ </td>
205
+ <td>57.76
206
+ </td>
207
+ <td>57.08
208
+ </td>
209
+ <td>98.82%
210
+ </td>
211
+ </tr>
212
+ <tr>
213
+ <td>GSM-8K (5-shot, strict-match)
214
+ </td>
215
+ <td>45.11
216
+ </td>
217
+ <td>42.0
218
+ </td>
219
+ <td>93.11%
220
+ </td>
221
+ </tr>
222
+ <tr>
223
+ <td>Hellaswag (10-shot)
224
+ </td>
225
+ <td>71.22
226
+ </td>
227
+ <td>70.53
228
+ </td>
229
+ <td>99.03%
230
+ </td>
231
+ </tr>
232
+ <tr>
233
+ <td>Winogrande (5-shot)
234
+ </td>
235
+ <td>68.67
236
+ </td>
237
+ <td>68.35
238
+ </td>
239
+ <td>99.53%
240
+ </td>
241
+ </tr>
242
+ <tr>
243
+ <td>TruthfulQA (0-shot)
244
+ </td>
245
+ <td>53.11
246
+ </td>
247
+ <td>52.57
248
+ </td>
249
+ <td>98.98%
250
+ </td>
251
+ </tr>
252
+ <tr>
253
+ <td><strong>Average</strong>
254
+ </td>
255
+ <td><strong>58.8</strong>
256
+ </td>
257
+ <td><strong>57.75</strong>
258
+ </td>
259
+ <td><strong>98.21%</strong>
260
+ </td>
261
+ </tr>
262
+ </table>