Optimizing Llama 3 Inference Speed Using INT8 Quantization via AutoGPTQ
The biggest hurdle most people hit is the calibration dataset. If you just quantize without a representative sample, your perplexity spikes and the model starts hallucinating gibberish. I found that using a slice of the wikitext2 dataset usually keeps the logic intact while slashing the VRAM footprint.
Here is the core implementation flow I used to get Llama 3 quantized and running. I'm using a Python environment with optimum and auto-gptq installed.
from transformers import AutoModelForCausalLM, AutoTokenizer
from auto_gptq import AutoGPTQQuantizer
model_id = "meta-llama/Meta-Llama-3-8B"
save_dir = "llama3-8b-int8"
# Load tokenizer and model in half precision first
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id, torch_dtype="auto", device_map="auto")
# Setup the quantizer for INT8
# bits=8 is the sweet spot for maintaining near-FP16 accuracy
quantizer = AutoGPTQQuantizer(bits=8, dataset="wikitext2", model=model)
# This is where the heavy lifting happens
quantizer.quantize_model()
quantizer.save_quantized_model(save_dir, tokenizer=tokenizer)One massive productivity gain I noticed is that moving to INT8 allows me to crank up the batch_size during inference without hitting Out-of-Memory (OOM) errors. In my tests, I saw a roughly 40% increase in tokens per second on a consumer RTX 3090 compared to the standard half-precision load.
Crucial Config Tips for Performance:
- Group Size: Stick to
group_size=128. Going lower increases accuracy slightly but kills the speed gains. - Dampening: If you notice the model becoming "repetitive" after quantization, check your dampening factor; keeping it at the default is usually fine for Llama 3, but some custom finetunes need tweaking.
- KV Cache: Since you're saving VRAM on weights, use that headroom to increase your
max_position_embeddingsor context window in the config.json if you're doing long-document analysis.
A common gotcha: make sure your
transformers version is up to date. Earlier versions had a bug where the INT8 weights weren't being correctly mapped to the GPU kernels, meaning you'd see the memory drop but the inference speed would actually decrease because it was falling back to CPU kernels.To run the quantized model, just load it via AutoModelForCausalLM with quantization_config.
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained(
"llama3-8b-int8",
device_map="auto",
trust_remote_code=True
)If you're deploying this in a production API, skip the manual quantization and go straight to a GPTQ-quantized version from HuggingFace, but if you're working with a domain-specific fine-tuned Llama 3, doing the AutoGPTQ process yourself is the only way to ensure the weights don't drift.
All Replies (0)
No replies yet — be the first!
