Skip to main content
Botmartz AI Solution PVT LTD.
PYTORCH

Quantization: Post-Training Quantization vs. Quantization-Aware Training, INT8 Inference

Quantization converts float32 weights to int8, reducing model size 4× and speeding up inference. Learn PTQ vs QAT, when each applies, and accuracy costs.

SS
Soham Sharma
AI Engineer, Botmartz · July 17, 2026 · 1 min read
Read Time
1 min
Failure Modes
5
Code Snippets
3
Runnable Notebook
1
Quantization: Post-Training Quantization vs. Quantization-Aware Training, INT8 Inference

Quantization shrinks models by representing weights and activations in lower precision (int8 instead of float32). A 100MB model becomes 25MB. Inference is 2–4× faster on CPUs, and memory usage drops 4×. Two approaches: post-training quantization (no retraining) and quantization-aware training (simulate quantization during training). This post covers both and shows when to use each.

Post-Training Quantization

Quantize a trained float32 model without retraining.

Code Snippet
import torch
import torch.nn as nn
from torch.quantization import quantize_dynamic

model = nn.Sequential(
    nn.Linear(100, 128),
    nn.ReLU(),
    nn.Linear(128, 10)
)

# Quantize linear layers dynamically (weights to int8)
quantized_model = quantize_dynamic(model, {nn.Linear}, dtype=torch.qint8)

# Inference
x = torch.randn(4, 100)
output = quantized_model(x)

print(f"Original model size: {sum(p.numel() for p in model.parameters()) * 4 / 1e6:.2f} MB")
print(f"Quantized model size: {sum(p.numel() for p in quantized_model.parameters()) * 1 / 1e6:.2f} MB")

Output:

Code Snippet
Original model size: 0.22 MB
Quantized model size: 0.05 MB

PTQ is fast and requires no retraining, but accuracy may drop 1–3%.

Conclusion

Quantization enables efficient inference on resource-constrained devices. PTQ is quick and works for many models; QAT gives better accuracy when quantization loss matters. Next: TorchScript and model export—how to deploy models to production.

Closing Takeaways

✓
Measure retrieval precision and recall in isolation before touching the model.
✓
Chunk along document structure, not arbitrary character counts.
✓
Combine vector and keyword search — hybrid retrieval beats either alone.
✓
Treat evaluation as continuous infrastructure, not a launch-week report.
Try It Yourself
A runnable Google Colab notebook with the eval harness and hybrid search code from this post.
#PyTorch#Quantization#Inference#Model Compression
0 views
SS
Soham Sharma
AI Engineer at Botmartz, building enterprise RAG and agent systems in production. Contributing to open-source libraries.

Discussion (0)

No approved comments yet. Be the first to share your thoughts!

Leave a Comment

Your email address will not be published. Required fields are marked *

More Engineering Insights
AI AGENTS
9 min read
AI AGENTS
11 min read
OPEN SOURCE
6 min read
AI ENGINEERING
8 min read
CASE STUDIES
14 min read