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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
tags:
|
| 3 |
+
- w4a16
|
| 4 |
+
- int4
|
| 5 |
+
- vllm
|
| 6 |
+
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-instruct
|
| 11 |
+
library_name: transformers
|
| 12 |
+
---
|
| 13 |
+
|
| 14 |
+
# granite-3.1-2b-instruct-quantized.w4a16
|
| 15 |
+
|
| 16 |
+
## Model Overview
|
| 17 |
+
- **Model Architecture:** granite-3.1-2b-instruct
|
| 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-instruct](https://huggingface.co/ibm-granite/granite-3.1-2b-instruct).
|
| 28 |
+
It achieves an average score of xxxx on the [OpenLLM](https://huggingface.co/spaces/open-llm-leaderboard/open_llm_leaderboard) benchmark (version 1), whereas the unquantized model achieves xxxx.
|
| 29 |
+
|
| 30 |
+
### Model Optimizations
|
| 31 |
+
|
| 32 |
+
This model was obtained by quantizing the weights of [ibm-granite/granite-3.1-2b-instruct](https://huggingface.co/ibm-granite/granite-3.1-2b-instruct) 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-instruct-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-instruct --quant_path "output_dir/granite-3.1-2b-instruct-quantized.w4a16" --calib_size 2048 --dampening_frac 0.01 --observer mse
|
| 72 |
+
```
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
```python
|
| 76 |
+
from datasets import load_dataset
|
| 77 |
+
from transformers import AutoTokenizer
|
| 78 |
+
from llmcompressor.modifiers.quantization import GPTQModifier
|
| 79 |
+
from llmcompressor.transformers import SparseAutoModelForCausalLM, 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 |
+
args = parser.parse_args()
|
| 91 |
+
|
| 92 |
+
model = SparseAutoModelForCausalLM.from_pretrained(
|
| 93 |
+
args.model_path,
|
| 94 |
+
device_map="auto",
|
| 95 |
+
torch_dtype="auto",
|
| 96 |
+
use_cache=False,
|
| 97 |
+
trust_remote_code=True,
|
| 98 |
+
)
|
| 99 |
+
tokenizer = AutoTokenizer.from_pretrained(args.model_path)
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
NUM_CALIBRATION_SAMPLES = args.calib_size
|
| 103 |
+
DATASET_ID = "neuralmagic/LLM_compression_calibration"
|
| 104 |
+
DATASET_SPLIT = "train"
|
| 105 |
+
ds = load_dataset(DATASET_ID, split=DATASET_SPLIT)
|
| 106 |
+
ds = ds.shuffle(seed=42).select(range(NUM_CALIBRATION_SAMPLES))
|
| 107 |
+
|
| 108 |
+
def preprocess(example):
|
| 109 |
+
concat_txt = example["instruction"] + "\n" + example["output"]
|
| 110 |
+
return {"text": concat_txt}
|
| 111 |
+
|
| 112 |
+
ds = ds.map(preprocess)
|
| 113 |
+
|
| 114 |
+
def tokenize(sample):
|
| 115 |
+
return tokenizer(
|
| 116 |
+
sample["text"],
|
| 117 |
+
padding=False,
|
| 118 |
+
truncation=False,
|
| 119 |
+
add_special_tokens=True,
|
| 120 |
+
)
|
| 121 |
+
|
| 122 |
+
|
| 123 |
+
ds = ds.map(tokenize, remove_columns=ds.column_names)
|
| 124 |
+
|
| 125 |
+
recipe = [
|
| 126 |
+
GPTQModifier(
|
| 127 |
+
targets=["Linear"],
|
| 128 |
+
ignore=["lm_head"],
|
| 129 |
+
scheme="w4a16",
|
| 130 |
+
dampening_frac=args.dampening_frac,
|
| 131 |
+
observer=args.observer,
|
| 132 |
+
)
|
| 133 |
+
]
|
| 134 |
+
oneshot(
|
| 135 |
+
model=model,
|
| 136 |
+
dataset=ds,
|
| 137 |
+
recipe=recipe,
|
| 138 |
+
num_calibration_samples=args.calib_size,
|
| 139 |
+
max_seq_length=8196,
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
# Save to disk compressed.
|
| 143 |
+
model.save_pretrained(SAVE_DIR, save_compressed=True)
|
| 144 |
+
tokenizer.save_pretrained(SAVE_DIR)
|
| 145 |
+
```
|
| 146 |
+
|
| 147 |
+
## Evaluation
|
| 148 |
+
|
| 149 |
+
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:
|
| 150 |
+
|
| 151 |
+
OpenLLM Leaderboard V1:
|
| 152 |
+
```
|
| 153 |
+
lm_eval \
|
| 154 |
+
--model vllm \
|
| 155 |
+
--model_args pretrained="neuralmagic-ent/granite-3.1-2b-instruct-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 \
|
| 156 |
+
--tasks openllm \
|
| 157 |
+
--write_out \
|
| 158 |
+
--batch_size auto \
|
| 159 |
+
--output_path output_dir \
|
| 160 |
+
--show_config
|
| 161 |
+
```
|
| 162 |
+
|
| 163 |
+
#### HumanEval
|
| 164 |
+
##### Generation
|
| 165 |
+
```
|
| 166 |
+
python3 codegen/generate.py \
|
| 167 |
+
--model neuralmagic-ent/granite-3.1-2b-instruct-quantized.w4a16 \
|
| 168 |
+
--bs 16 \
|
| 169 |
+
--temperature 0.2 \
|
| 170 |
+
--n_samples 50 \
|
| 171 |
+
--root "." \
|
| 172 |
+
--dataset humaneval
|
| 173 |
+
```
|
| 174 |
+
##### Sanitization
|
| 175 |
+
```
|
| 176 |
+
python3 evalplus/sanitize.py \
|
| 177 |
+
humaneval/neuralmagic-ent--granite-3.1-2b-instruct-quantized.w4a16_vllm_temp_0.2
|
| 178 |
+
```
|
| 179 |
+
##### Evaluation
|
| 180 |
+
```
|
| 181 |
+
evalplus.evaluate \
|
| 182 |
+
--dataset humaneval \
|
| 183 |
+
--samples humaneval/neuralmagic-ent--granite-3.1-2b-instruct-quantized.w4a16_vllm_temp_0.2-sanitized
|
| 184 |
+
```
|
| 185 |
+
|
| 186 |
+
### Accuracy
|
| 187 |
+
|
| 188 |
+
#### OpenLLM Leaderboard V1 evaluation scores
|
| 189 |
+
|
| 190 |
+
| Metric | ibm-granite/granite-3.1-2b-instruct | neuralmagic-ent/granite-3.1-2b-instruct-quantized.w4a16 |
|
| 191 |
+
|-----------------------------------------|:---------------------------------:|:-------------------------------------------:|
|
| 192 |
+
| ARC-Challenge (Acc-Norm, 25-shot) | 55.63 | 55.72 |
|
| 193 |
+
| GSM8K (Strict-Match, 5-shot) | 60.96 | 54.51 |
|
| 194 |
+
| HellaSwag (Acc-Norm, 10-shot) | 75.21 | 71.92 |
|
| 195 |
+
| MMLU (Acc, 5-shot) | 54.38 | 51.96 |
|
| 196 |
+
| TruthfulQA (MC2, 0-shot) | 55.93 | 52.90 |
|
| 197 |
+
| Winogrande (Acc, 5-shot) | 69.67 | 67.56 |
|
| 198 |
+
| **Average Score** | **61.98** | **59.09** |
|
| 199 |
+
| **Recovery** | **100.00** | **95.34** |
|
| 200 |
+
|
| 201 |
+
#### HumanEval pass@1 scores
|
| 202 |
+
| Metric | ibm-granite/granite-3.1-2b-instruct | neuralmagic-ent/granite-3.1-2b-instruct-quantized.w4a16 |
|
| 203 |
+
|-----------------------------------------|:---------------------------------:|:-------------------------------------------:|
|
| 204 |
+
| HumanEval Pass@1 | 53.40 | 50.70 |
|
| 205 |
+
|
| 206 |
+
|