Python / Python Deep Learning and Neural Networks Interview Questions
What is model quantization in deep learning and how does PyTorch support it?
Quantization reduces model size and inference latency by representing weights and activations in lower-precision integer formats (INT8, INT4, INT2) rather than FP32 or FP16. A 32-bit float weight is replaced by an 8-bit integer plus a scale factor and zero-point: x_float = scale × (x_int - zero_point). This yields 4× memory reduction for INT8, enabling larger models to fit on limited hardware and significantly faster integer arithmetic on CPUs and mobile accelerators.
Three main approaches: (1) Post-Training Quantization (PTQ) — quantize a trained FP32 model without retraining, using a small calibration dataset to determine optimal scale factors; (2) Quantization-Aware Training (QAT) — simulate quantization noise during training (fake quantization), allowing the model to adapt and typically recovering the accuracy lost by PTQ; (3) Dynamic quantization — weights are quantized ahead of time, activations quantized dynamically at inference (simplest, good baseline for RNNs).
import torch import torch.nn as nn from torch.quantization import quantize_dynamic, prepare, convert # âââ Dynamic Quantization (simplest â weights INT8, activations FP32) âââ model_fp32 = nn.LSTM(input_size=64, hidden_size=128) model_int8 = quantize_dynamic( model_fp32, qconfig_spec={nn.Linear, nn.LSTM}, dtype=torch.qint8 ) print('FP32 size:', sum(p.numel() * 4 for p in model_fp32.parameters()), 'bytes') # INT8 model is ~4x smaller # âââ Post-Training Static Quantization âââ model = nn.Sequential(nn.Linear(784, 256), nn.ReLU(), nn.Linear(256, 10)) model.qconfig = torch.quantization.get_default_qconfig('fbgemm') model_prepared = prepare(model) # insert observer modules # Calibrate with representative data model_prepared.eval() with torch.no_grad(): for X_cal, _ in calibration_loader: model_prepared(X_cal) model_int8 = convert(model_prepared) # convert to INT8 # âââ Modern approach: bitsandbytes / llm.int8() for LLMs âââ # 8-bit quantization of LLM weights with minimal accuracy loss # Allows running 7B+ parameter models on consumer GPUs # from transformers import AutoModelForCausalLM # model = AutoModelForCausalLM.from_pretrained('gpt2', load_in_8bit=True)
More Related questions...