pytorch_quantization.calib

pytorch_quantization.calib provides Calibrator classes that collect data statistics and determine pytorch_quantization parameters.

MaxCalibrator

class pytorch_quantization.calib.MaxCalibrator(num_bits, axis, unsigned, track_amax=False)[source]

Max calibrator, tracks the maximum value globally

Parameters
  • calib_desc – A MaxCalibDescriptor.

  • num_bits – An integer. Number of bits of quantization.

  • axis – A tuple. see QuantDescriptor.

  • unsigned – A boolean. using unsigned quantization.

Readonly Properties:

amaxs: A list of amax. Numpy array is saved as it is likely to be used for some plot.

collect(x)[source]

Tracks the absolute max of all tensors

Parameters

x – A tensor

Raises

RuntimeError – If amax shape changes

compute_amax()[source]

Return the absolute max of all tensors collected

reset()[source]

Reset the collected absolute max

HistogramCalibrator

class pytorch_quantization.calib.HistogramCalibrator(num_bits, axis, unsigned, num_bins=2048, grow_method=None, skip_zeros=False, torch_hist=False)[source]

Unified histogram calibrator

Histogram will be only collected once. compute_amax() performs entropy, percentile, or mse

calibration based on arguments

Parameters
  • num_bits – An integer. Number of bits of quantization.

  • axis – A tuple. see QuantDescriptor.

  • unsigned – A boolean. using unsigned quantization.

  • num_bins – An integer. Number of histograms bins. Default 2048.

  • grow_method – A string. DEPRECATED. default None.

  • skip_zeros – A boolean. If True, skips zeros when collecting data for histogram. Default False.

  • torch_hist – A boolean. If True, collect histogram by torch.histc instead of np.histogram. If input tensor is on GPU, histc will also be running on GPU. Default False.

collect(x)[source]

Collect histogram

compute_amax(method: str, *, stride: int = 1, start_bin: int = 128, percentile: float = 99.99)[source]

Compute the amax from the collected histogram

Parameters

method – A string. One of [‘entropy’, ‘mse’, ‘percentile’]

Keyword Arguments
  • stride – An integer. Default 1

  • start_bin – An integer. Default 128

  • percentils – A float number between [0, 100]. Default 99.99.

Returns

amax – a tensor

reset()[source]

Reset the collected histogram