How to do int4 quantization
- User
How to do int4 quantization
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
How to do int4 quantization
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
INT4 quantization reduces neural network model size and accelerates inference by using 4-bit integers for weights and/or activations. Here's a structured guide based on current methods:
Weight-Only Quantization (W4A16)
from llmcompressor.transformers import oneshot
from llmcompressor.modifiers.quantization import GPTQModifier
recipe = GPTQModifier(
targets="Linear",
scheme="W4A16",
group_size=128,
actorder="weight"
)
oneshot(model, dataset, recipe)
Weight+Activation Quantization (W4A4)
Model Preparation
# Hugging Face base
from transformers import AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained("meta-llama/Meta-Llama-3-8B")
# Intel optimized
from intel_extension_for_transformers import AutoModelForCausalLM
q4_model = AutoModelForCausalLM.from_pretrained(model_name, load_in_4bit=True) [3]
Calibration Data
from datasets import load_dataset
ds = load_dataset("HuggingFaceH4/ultrachat_200k", split="train_sft")
ds = ds.map(tokenize, batched=True) [1][4]
Quantization Algorithms
| Technique | Benefit | Implementation Example |
|---|---|---|
| Grouped Quantization | Reduces per-tensor error | group_size=128 in GPTQ [1][4] |
| Activation Ordering | Preserves important channels | actorder="weight" [1][4] |
| Knowledge Distillation | Maintains accuracy | Layer-wise feature alignment [4][9] |
| SIMD Optimizations | Accelerates INT4 ops | ARM SVE2/NEON instructions [5][9] |
Accuracy Drops
dampening_frac (0.01-0.1) [1]First Layer Handling
# First layer config
quant_config = {"num_bits": 8}
# Other layers
quant_config[1:] = {"num_bits": 4} [6][9]
Decoder Model Issues
INT4 quantization enables efficient deployment of large models while maintaining >95% of original accuracy in most encoder architectures [1][3][4]. For decoder models like GPT, W4A16 or hybrid quantization approaches are currently more practical [4][9].