alexmarques commited on
Commit
ce7108c
·
verified ·
1 Parent(s): 9ef720f

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +265 -0
README.md ADDED
@@ -0,0 +1,265 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ language:
3
+ - en
4
+ pipeline_tag: text-generation
5
+ ---
6
+
7
+ # Llama-2-7b-chat-quantized.w8a8
8
+
9
+ ## Model Overview
10
+ - **Model Architecture:** Llama-2
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 [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf), 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/2/2024
19
+ - **Version:** 1.0
20
+ - **Model Developers:** Neural Magic
21
+
22
+ Quantized version of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf).
23
+ It achieves an average score of 53.38 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 53.41.
24
+
25
+ ### Model Optimizations
26
+
27
+ This model was obtained by quantizing the weights of [Llama-2-7b-chat](https://huggingface.co/meta-llama/Llama-2-7b-chat-hf) 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 taken from Neural Magic's [LLM compression calibration dataset](https://huggingface.co/datasets/neuralmagic/LLM_compression_calibration).
36
+
37
+
38
+ ## Deployment
39
+
40
+ ### Use with vLLM
41
+
42
+ This model can be deployed efficiently using the [vLLM](https://docs.vllm.ai/en/latest/) backend, as shown in the example below.
43
+
44
+ ```python
45
+ from vllm import LLM, SamplingParams
46
+ from transformers import AutoTokenizer
47
+
48
+ model_id = "neuralmagic/Llama-2-7b-chat-quantized.w8a8"
49
+ number_gpus = 1
50
+
51
+ sampling_params = SamplingParams(temperature=0.6, top_p=0.9, max_tokens=256)
52
+
53
+ tokenizer = AutoTokenizer.from_pretrained(model_id, tensor_parallel_size=number_gpus)
54
+
55
+ messages = [
56
+ {"role": "system", "content": "You are a pirate chatbot who always responds in pirate speak!"},
57
+ {"role": "user", "content": "Who are you?"},
58
+ ]
59
+
60
+ prompts = tokenizer.apply_chat_template(messages, add_generation_prompt=True, tokenize=False)
61
+
62
+ llm = LLM(model=model_id, tensor_parallel_size=number_gpus)
63
+
64
+ outputs = llm.generate(prompts, sampling_params)
65
+
66
+ generated_text = outputs[0].outputs[0].text
67
+ print(generated_text)
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
+
77
+ ```python
78
+ from transformers import AutoTokenizer, AutoModelForCausalLM
79
+
80
+ model_id = "neuralmagic/Llama-2-7b-chat-quantized.w8a8"
81
+
82
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
83
+ model = AutoModelForCausalLM.from_pretrained(
84
+ model_id,
85
+ torch_dtype="auto",
86
+ device_map="auto",
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
+ terminators = [
101
+ tokenizer.eos_token_id,
102
+ tokenizer.convert_tokens_to_ids("<|eot_id|>")
103
+ ]
104
+
105
+ outputs = model.generate(
106
+ input_ids,
107
+ max_new_tokens=256,
108
+ eos_token_id=terminators,
109
+ do_sample=True,
110
+ temperature=0.6,
111
+ top_p=0.9,
112
+ )
113
+ response = outputs[0][input_ids.shape[-1]:]
114
+ print(tokenizer.decode(response, skip_special_tokens=True))
115
+ ```
116
+
117
+ ## Creation
118
+
119
+ This model was created by using the [llm-compressor](https://github.com/vllm-project/llm-compressor) library as presented in the code snipet below.
120
+
121
+ ```python
122
+ This model was created by using the [llm-compressor](https://github.com/vllm-project/llm-compressor) library as presented in the code snipet below.
123
+
124
+ ```python
125
+ from transformers import AutoTokenizer
126
+ from datasets import load_dataset
127
+ from llmcompressor.transformers import SparseAutoModelForCausalLM, oneshot
128
+ from llmcompressor.modifiers.quantization import GPTQModifier
129
+
130
+ model_id = "meta-llama/Llama-2-7b-chat-hf"
131
+
132
+ num_samples = 256
133
+ max_seq_len = 8192
134
+
135
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
136
+
137
+ def preprocess_fn(example):
138
+ return {"text": tokenizer.apply_chat_template(example["messages"], add_generation_prompt=False, tokenize=False)}
139
+
140
+ ds = load_dataset("neuralmagic/LLM_compression_calibration", split="train")
141
+ ds = ds.shuffle().select(range(num_samples))
142
+ ds = ds.map(preprocess_fn)
143
+
144
+ recipe = GPTQModifier(
145
+ targets="Linear",
146
+ scheme="W8A8",
147
+ ignore=["lm_head"],
148
+ dampening_frac=0.01,
149
+ )
150
+
151
+ model = SparseAutoModelForCausalLM.from_pretrained(
152
+ model_id,
153
+ device_map="auto",
154
+ trust_remote_code=True,
155
+ )
156
+
157
+ oneshot(
158
+ model=model,
159
+ dataset=ds,
160
+ recipe=recipe,
161
+ max_seq_length=max_seq_len,
162
+ num_calibration_samples=num_samples,
163
+ )
164
+
165
+ model.save_pretrained("Llama-2-7b-chat-quantized.w8a8")
166
+ ```
167
+
168
+
169
+
170
+ ## Evaluation
171
+
172
+ 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:
173
+ ```
174
+ lm_eval \
175
+ --model vllm \
176
+ --model_args pretrained="neuralmagic/Llama-2-7b-chat-quantized.w8a8",dtype=auto,gpu_memory_utilization=0.4,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1 \
177
+ --tasks openllm \
178
+ --batch_size auto
179
+ ```
180
+
181
+ ### Accuracy
182
+
183
+ #### Open LLM Leaderboard evaluation scores
184
+ <table>
185
+ <tr>
186
+ <td><strong>Benchmark</strong>
187
+ </td>
188
+ <td><strong>Llama-2-7b-chat </strong>
189
+ </td>
190
+ <td><strong>Llama-2-7b-chat-quantized.w8a8 (this model)</strong>
191
+ </td>
192
+ <td><strong>Recovery</strong>
193
+ </td>
194
+ </tr>
195
+ <tr>
196
+ <td>MMLU (5-shot)
197
+ </td>
198
+ <td>47.34
199
+ </td>
200
+ <td>47.04
201
+ </td>
202
+ <td>99.4%
203
+ </td>
204
+ </tr>
205
+ <tr>
206
+ <td>ARC Challenge (25-shot)
207
+ </td>
208
+ <td>53.24
209
+ </td>
210
+ <td>54.52
211
+ </td>
212
+ <td>102.4%
213
+ </td>
214
+ </tr>
215
+ <tr>
216
+ <td>GSM-8K (5-shot, strict-match)
217
+ </td>
218
+ <td>23.20
219
+ </td>
220
+ <td>22.21
221
+ </td>
222
+ <td>95.8%
223
+ </td>
224
+ </tr>
225
+ <tr>
226
+ <td>Hellaswag (10-shot)
227
+ </td>
228
+ <td>78.65
229
+ </td>
230
+ <td>78.17
231
+ </td>
232
+ <td>99.4%
233
+ </td>
234
+ </tr>
235
+ <tr>
236
+ <td>Winogrande (5-shot)
237
+ </td>
238
+ <td>72.45
239
+ </td>
240
+ <td>72.85
241
+ </td>
242
+ <td>100.5%
243
+ </td>
244
+ </tr>
245
+ <tr>
246
+ <td>TruthfulQA (0-shot)
247
+ </td>
248
+ <td>45.58
249
+ </td>
250
+ <td>45.50
251
+ </td>
252
+ <td>99.8%
253
+ </td>
254
+ </tr>
255
+ <tr>
256
+ <td><strong>Average</strong>
257
+ </td>
258
+ <td><strong>53.41</strong>
259
+ </td>
260
+ <td><strong>53.38</strong>
261
+ </td>
262
+ <td><strong>99.9%</strong>
263
+ </td>
264
+ </tr>
265
+ </table>