How Tensors and Modules work in Quanto
mainTensors
Quanto uses a Tensor subclass that handles projection and mapping:
- Projection: Maps source Tensors to the optimal range for the destination type to minimize saturated or zeroed values. Projection is symmetric per-tensor/per-channel for
int8/float8, and group-wise affine for lower bitwidths. - Mapping: Uses native PyTorch
Tensor.to()for floating-point types andtorch.round()for integer types.
Modules
Quanto replaces standard torch modules with quantized versions:
- Weight Quantization: Weights are typically quantized per-channel along the first dimension (output features). They are dynamically converted until the model is
freeze()ed. - Bias Handling: Biases are not quantized to preserve accuracy and avoid the complexity of extremely small scales.
- Activation Quantization: Activations are dynamically quantized per-tensor using static scales (defaulting to
[-1, 1]). Calibration is recommended to find optimal scales.
Supported Modules:
QLinear(fromtorch.nn.Linear): Weights are quantized; biases are not. Inputs/outputs can be quantized.QConv2D(fromtorch.nn.Conv2d): Weights are quantized; biases are not. Inputs/outputs can be quantized.LayerNorm(fromtorch.nn.LayerNorm): Weights and biases are not quantized. Outputs can be quantized.