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 |
+
license: apache-2.0
|
7 |
+
license_link: https://huggingface.co/datasets/choosealicense/licenses/blob/main/markdown/apache-2.0.md
|
8 |
+
language:
|
9 |
+
- en
|
10 |
+
base_model: ibm-granite/granite-3.1-2b-base
|
11 |
+
library_name: transformers
|
12 |
+
---
|
13 |
+
|
14 |
+
# granite-3.1-2b-base-quantized.w4a16
|
15 |
+
|
16 |
+
## Model Overview
|
17 |
+
- **Model Architecture:** granite-3.1-2b-base
|
18 |
+
- **Input:** Text
|
19 |
+
- **Output:** Text
|
20 |
+
- **Model Optimizations:**
|
21 |
+
- **Weight quantization:** INT4
|
22 |
+
- **Activation quantization:** INT4
|
23 |
+
- **Release Date:** 1/8/2025
|
24 |
+
- **Version:** 1.0
|
25 |
+
- **Model Developers:** Neural Magic
|
26 |
+
|
27 |
+
Quantized version of [ibm-granite/granite-3.1-2b-base](https://huggingface.co/ibm-granite/granite-3.1-2b-base).
|
28 |
+
It achieves an average score of 61.54 on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves 61.98.
|
29 |
+
|
30 |
+
### Model Optimizations
|
31 |
+
|
32 |
+
This model was obtained by quantizing the weights of [ibm-granite/granite-3.1-2b-base](https://huggingface.co/ibm-granite/granite-3.1-2b-base) to INT4 data type, ready for inference with vLLM >= 0.5.2.
|
33 |
+
This optimization reduces the number of bits per parameter from 16 to 4, reducing the disk size and GPU memory requirements by approximately 75%. Only the weights of the linear operators within transformers blocks are quantized.
|
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 transformers import AutoTokenizer
|
43 |
+
from vllm import LLM, SamplingParams
|
44 |
+
|
45 |
+
max_model_len, tp_size = 4096, 1
|
46 |
+
model_name = "neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16"
|
47 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
48 |
+
llm = LLM(model=model_name, tensor_parallel_size=tp_size, max_model_len=max_model_len, trust_remote_code=True)
|
49 |
+
sampling_params = SamplingParams(temperature=0.3, max_tokens=256, stop_token_ids=[tokenizer.eos_token_id])
|
50 |
+
|
51 |
+
messages_list = [
|
52 |
+
[{"role": "user", "content": "Who are you? Please respond in pirate speak!"}],
|
53 |
+
]
|
54 |
+
|
55 |
+
prompt_token_ids = [tokenizer.apply_chat_template(messages, add_generation_prompt=True) for messages in messages_list]
|
56 |
+
|
57 |
+
outputs = llm.generate(prompt_token_ids=prompt_token_ids, sampling_params=sampling_params)
|
58 |
+
|
59 |
+
generated_text = [output.outputs[0].text for output in outputs]
|
60 |
+
print(generated_text)
|
61 |
+
```
|
62 |
+
|
63 |
+
vLLM also supports OpenAI-compatible serving. See the [documentation](https://docs.vllm.ai/en/latest/) for more details.
|
64 |
+
|
65 |
+
## Creation
|
66 |
+
|
67 |
+
This model was created with [llm-compressor](https://github.com/vllm-project/llm-compressor) by running the code snippet below.
|
68 |
+
|
69 |
+
|
70 |
+
```bash
|
71 |
+
python quantize.py --model_path ibm-granite/granite-3.1-2b-base --quant_path "output_dir/granite-3.1-2b-base-quantized.w4a16" --calib_size 1024 --dampening_frac 0.01 --observer mse
|
72 |
+
```
|
73 |
+
|
74 |
+
|
75 |
+
```python
|
76 |
+
from datasets import load_dataset
|
77 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
78 |
+
from llmcompressor.modifiers.quantization import GPTQModifier
|
79 |
+
from llmcompressor.transformers import oneshot, apply
|
80 |
+
import argparse
|
81 |
+
from compressed_tensors.quantization import QuantizationScheme, QuantizationArgs, QuantizationType, QuantizationStrategy
|
82 |
+
|
83 |
+
|
84 |
+
parser = argparse.ArgumentParser()
|
85 |
+
parser.add_argument('--model_path', type=str)
|
86 |
+
parser.add_argument('--quant_path', type=str)
|
87 |
+
parser.add_argument('--calib_size', type=int, default=256)
|
88 |
+
parser.add_argument('--dampening_frac', type=float, default=0.1)
|
89 |
+
parser.add_argument('--observer', type=str, default="minmax")
|
90 |
+
parser.add_argument('--group_size', type=int, default="128")
|
91 |
+
args = parser.parse_args()
|
92 |
+
|
93 |
+
model = AutoModelForCausalLM.from_pretrained(
|
94 |
+
args.model_path,
|
95 |
+
device_map="auto",
|
96 |
+
torch_dtype="auto",
|
97 |
+
use_cache=False,
|
98 |
+
trust_remote_code=True,
|
99 |
+
)
|
100 |
+
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
|
101 |
+
|
102 |
+
|
103 |
+
NUM_CALIBRATION_SAMPLES = args.calib_size
|
104 |
+
DATASET_ID = "neuralmagic/LLM_compression_calibration"
|
105 |
+
DATASET_SPLIT = "train"
|
106 |
+
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
|
107 |
+
ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
|
108 |
+
|
109 |
+
def preprocess(example):
|
110 |
+
concat_txt = example["baseion"] + "\n" + example["output"]
|
111 |
+
return {"text": concat_txt}
|
112 |
+
|
113 |
+
ds = ds.map(preprocess)
|
114 |
+
|
115 |
+
def tokenize(sample):
|
116 |
+
return tokenizer(
|
117 |
+
sample["text"],
|
118 |
+
padding=False,
|
119 |
+
truncation=False,
|
120 |
+
add_special_tokens=True,
|
121 |
+
)
|
122 |
+
|
123 |
+
|
124 |
+
ds = ds.map(tokenize, remove_columns=ds.column_names)
|
125 |
+
|
126 |
+
recipe = [
|
127 |
+
GPTQModifier(
|
128 |
+
targets=["Linear"],
|
129 |
+
ignore=["lm_head"],
|
130 |
+
scheme="w4a16",
|
131 |
+
dampening_frac=args.dampening_frac,
|
132 |
+
observer=args.observer,
|
133 |
+
)
|
134 |
+
]
|
135 |
+
oneshot(
|
136 |
+
model=model,
|
137 |
+
dataset=ds,
|
138 |
+
recipe=recipe,
|
139 |
+
num_calibration_samples=args.calib_size,
|
140 |
+
max_seq_length=8196,
|
141 |
+
)
|
142 |
+
|
143 |
+
# Save to disk compressed.
|
144 |
+
model.save_pretrained(SAVE_DIR, save_compressed=True)
|
145 |
+
tokenizer.save_pretrained(SAVE_DIR)
|
146 |
+
```
|
147 |
+
|
148 |
+
## Evaluation
|
149 |
+
|
150 |
+
The model was evaluated on OpenLLM Leaderboard [V1](https://huggingface.co/spaces/open-llm-leaderboard-old/open_llm_leaderboard) and on [HumanEval](https://github.com/neuralmagic/evalplus), using the following commands:
|
151 |
+
|
152 |
+
OpenLLM Leaderboard V1:
|
153 |
+
```
|
154 |
+
lm_eval \
|
155 |
+
--model vllm \
|
156 |
+
--model_args pretrained="neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16",dtype=auto,add_bos_token=True,max_model_len=4096,tensor_parallel_size=1,gpu_memory_utilization=0.8,enable_chunked_prefill=True,trust_remote_code=True \
|
157 |
+
--tasks openllm \
|
158 |
+
--write_out \
|
159 |
+
--batch_size auto \
|
160 |
+
--output_path output_dir \
|
161 |
+
--show_config
|
162 |
+
```
|
163 |
+
|
164 |
+
#### HumanEval
|
165 |
+
##### Generation
|
166 |
+
```
|
167 |
+
python3 codegen/generate.py \
|
168 |
+
--model neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16 \
|
169 |
+
--bs 16 \
|
170 |
+
--temperature 0.2 \
|
171 |
+
--n_samples 50 \
|
172 |
+
--root "." \
|
173 |
+
--dataset humaneval
|
174 |
+
```
|
175 |
+
##### Sanitization
|
176 |
+
```
|
177 |
+
python3 evalplus/sanitize.py \
|
178 |
+
humaneval/neuralmagic-ent--granite-3.1-2b-base-quantized.w4a16_vllm_temp_0.2
|
179 |
+
```
|
180 |
+
##### Evaluation
|
181 |
+
```
|
182 |
+
evalplus.evaluate \
|
183 |
+
--dataset humaneval \
|
184 |
+
--samples humaneval/neuralmagic-ent--granite-3.1-2b-base-quantized.w4a16_vllm_temp_0.2-sanitized
|
185 |
+
```
|
186 |
+
|
187 |
+
### Accuracy
|
188 |
+
|
189 |
+
#### OpenLLM Leaderboard V1 evaluation scores
|
190 |
+
|
191 |
+
Here is the table with all values for metrics removed:
|
192 |
+
|
193 |
+
---
|
194 |
+
Here is the table filled with the base values provided:
|
195 |
+
|
196 |
+
---
|
197 |
+
|
198 |
+
| Metric | ibm-granite/granite-3.1-2b-base | neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16 |
|
199 |
+
|-----------------------------------------|:---------------------------------:|:-------------------------------------------:|
|
200 |
+
| ARC-Challenge (Acc-Norm, 25-shot) | 53.75 | 51.96 |
|
201 |
+
| GSM8K (Strict-Match, 5-shot) | 47.84 | 42.53 |
|
202 |
+
| HellaSwag (Acc-Norm, 10-shot) | 77.94 | 75.38 |
|
203 |
+
| MMLU (Acc, 5-shot) | 52.88 | 51.09 |
|
204 |
+
| TruthfulQA (MC2, 0-shot) | 39.04 | 41.35 |
|
205 |
+
| Winogrande (Acc, 5-shot) | 74.43 | 74.27 |
|
206 |
+
| **Average Score** | **57.65** | **56.10** |
|
207 |
+
| **Recovery** | **100.00** | **97.31** |
|
208 |
+
|
209 |
+
---
|
210 |
+
|
211 |
+
#### OpenLLM Leaderboard V2 evaluation scores
|
212 |
+
|
213 |
+
| Metric | ibm-granite/granite-3.1-2b-base | neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16 |
|
214 |
+
|-----------------------------------------|:---------------------------------:|:-------------------------------------------:|
|
215 |
+
| IFEval (Inst Level Strict Acc, 0-shot) | 41.01 | |
|
216 |
+
| BBH (Acc-Norm, 3-shot) | 40.19 | |
|
217 |
+
| Math-Hard (Exact-Match, 4-shot) | 4.86 | |
|
218 |
+
| GPQA (Acc-Norm, 0-shot) | 27.11 | |
|
219 |
+
| MUSR (Acc-Norm, 0-shot) | 34.85 | |
|
220 |
+
| MMLU-Pro (Acc, 5-shot) | 22.49 | |
|
221 |
+
| **Average Score** | **28.42** | |
|
222 |
+
| **Recovery** | **100.00** | |
|
223 |
+
|
224 |
+
---
|
225 |
+
|
226 |
+
#### HumanEval pass@1 scores
|
227 |
+
|
228 |
+
| Metric | ibm-granite/granite-3.1-2b-base | neuralmagic-ent/granite-3.1-2b-base-quantized.w4a16 |
|
229 |
+
|-----------------------------------------|:---------------------------------:|:-------------------------------------------:|
|
230 |
+
| HumanEval Pass@1 | 30.00 | 0.298 |
|