module owlite.calib.calibrator
class Calibrator
Base calibrator abstract class.
Uses the forward hook to collect the data needed for calibration and update the quantizer's stepsize and zeropoint.
In OwLite, calibrator classes collect the necessary data for calibration based on the data passing through the FakeQuantizer
. This process enables the determination of the FakeQuantizer
's step_size
and zero_point
.
Attributes:
hook_handler
(torch.utils.hooks.RemovableHandle
,optional
): A hook handler.quantizer
(FakeQuantizer
): TheFakeQuantizer
to which the calibration will be applied.
method __init__
python
__init__(quantizer: 'FakeQuantizer')
method check_calib_ready
python
check_calib_ready() → bool
Check that the conditions for calibration are met.
Returns:
True
, if all conditions for calibration are met, False
otherwise.
method prepare
python
prepare() → RemovableHandle
Prepare calibration for the quantizer.
Set temporal attributes on the quantizer and register a hook on the quantizer.
Raises:
ValueError
: If the attributions are already set.
Returns:
torch.utils.hooks.RemovableHandle
: A registered hook handler.
method update
python
update() → None
Calculate stepsize and zeropoint of quantizer and update them. Then remove the registered hook.
method update_fake_quantizer_param_with_max_min
python
update_fake_quantizer_param_with_max_min(
max_value: Tensor,
min_value: Tensor | None = None
) → None
Find and apply the stepsize and zeropoints of a quantizer with the given values as min and max.
Each parameters are updated as follows(For symmetric quantization, min_value only uses 0).
stepsize = (maxvalue - minvalue) / (quantmax - quantmin) zeropoint = - round(minvalue / stepsize) + quant_min
Args:
max_value
(Tensor): The maximum value that will not be clipped.min_value
(Tensor | None): The minimum value that will not be clipped. This value will only be used for asymmetric quantization. Defaults to None.
Raises:
TypeError
: When the shape of the fake quantizer's parameters and arguments do not match.TypeError
: When min_value is not specified in asymmetric quantization
Updated: 2024-06-13T23:42:41