伪量化算子

class horizon_plugin_pytorch.quantization.FakeQuantize (observer: type = <class 'horizon_plugin_pytorch.quantization.observer.MovingAverageMinMaxObserver'>, saturate: bool | None = None, in_place: bool = False, compat_mask: bool = True, channel_len: int = 1, fast_training=True, **observer_kwargs)

Simulate the quantize and dequantize operations in training time.

The output of this module is given by

fake_quant_x = clamp(round(x / scale), quant_min, quant_max) * scale

  • scale defines the scale factor used for quantization.

  • zero_point specifies the quantized value to which 0 in floating point maps to

  • quant_min specifies the minimum allowable quantized value.

  • quant_max specifies the maximum allowable quantized value.

  • fake_quant_enabled controls the application of fake quantization on tensors, note that statistics can still be updated.

  • observer_enabled controls statistics collection on tensors

  • dtype specifies the quantized dtype that is being emulated with fake-quantization, the allowable values is qint8 and qint16. The values of quant_min and quant_max should be chosen to be consistent with the dtype

  • Parameters:

    • observer (type) – Module for observing statistics on input tensors and calculating scale and zero-point.
    • saturate (Optional[bool]) – Whether zero out the grad for value out of quanti range.
    • in_place (bool) – Whether use in place fake quantize.
    • compat_mask (bool) – Whether pack the bool mask into bitfield when saturate = True.
    • channel_len (int) – Size of data at channel dim.
    • fast_training – Whether use fast training mode. If True, computing scale and fake quantization will be done in one step.
    • observer_kwargs – Arguments for the observer module

observer

User provided module that collects statistics on the input tensor and provides a method to calculate scale and zero-point.

set_qparams (scale: Tensor | Sequence | float, zero_point: Tensor | Sequence | int | None = None)

Set qparams, default symmetric.

  • Parameters:
    • scale ( Tensor | Sequence | float)
    • zero_point ( Tensor | Sequence | int | None)

classmethod with_args (**kwargs)

Wrapper that allows creation of class factories.

This can be useful when there is a need to create classes with the same constructor arguments, but different instances. Can be used in conjunction with _callable_args

Example:

>>> # xdoctest: +SKIP("Undefined vars")
>>> Foo.with_args = classmethod(_with_args)
>>> foo_builder = Foo.with_args(a=3, b=4).with_args(answer=42)
>>> foo_instance1 = foo_builder()
>>> foo_instance2 = foo_builder()
>>> id(foo_instance1) == id(foo_instance2)
False