Which PyTorch quantization strategy minimizes memory overhead for low-traffic CPU inference?
0 reputation · 13 Sept 2020, 15:15 UTC
0 reputation · 13 Sept 2020, 15:15 UTC
Reducing operational costs for low-traffic workloads often involves migrating from GPU instances to CPU-based environments. To minimize the memory footprint and hardware requirements, PyTorch provides several model compression techniques, including pruning and quantization.
When targeting CPU inference, there is a trade-off between the simplicity of Dynamic Quantization—where weights are quantized offline and activations are quantized during runtime—and the potential performance gains of Static Quantization, which requires a calibration dataset to determine activation ranges.
For a deployment where latency requirements are flexible but memory costs must be minimized, it is unclear which approach provides the most efficient balance of memory reduction and accuracy preservation without extensive fine-tuning.
35275 reputation · 13 Sept 2020, 23:02 UTC
Static post‑training quantization (PTQ) provides the smallest memory footprint for low‑traffic CPU inference in PyTorch.
Confirmed fact: static PTQ converts both weights and activations from FP32 (4 bytes) to INT8 (1 byte), reducing activation storage to one‑quarter of the original size. Dynamic quantization only quantizes weights, leaving activations in FP32, so the activation memory remains unchanged.
torch.quantization.fuse_modules(model, [['conv', 'bn', 'relu']], inplace=True) – fuse supported patterns.model.qconfig = torch.quantization.get_default_qconfig('fbgemm').torch.quantization.prepare(model, inplace=True) – insert observers.torch.quantization.convert(model, inplace=True) – produce the INT8 model.os.path.getsize('model_fp32.pt') vs os.path.getsize('model_int8.pt').psutil.Process().memory_info().rss) and confirm it drops roughly to 25 % of the FP32 baseline.If you do not have a representative calibration dataset, the static quantization step may produce sub‑optimal scales, potentially hurting accuracy more than dynamic quantization. In that case, please confirm whether a suitable calibration set is available; otherwise, dynamic quantization is the safer fallback.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.