mindspore.nn.ActQuant

class mindspore.nn.ActQuant(activation, ema=False, ema_decay=0.999, fake_before=False, quant_config=quant_config_default, quant_dtype=QuantDtype.INT8)[source]

Quantization aware training activation function.

Add the fake quantized operation to the end of activation operation, by which the output of activation operation will be truncated. For more detials about Quantilization, please refer to mindspore.nn.FakeQuantWithMinMaxObserver.

Parameters
  • activation (Cell) – Activation cell.

  • ema (bool) – The exponential Moving Average algorithm updates min and max. Default: False.

  • ema_decay (float) – Exponential Moving Average algorithm parameter. Default: 0.999.

  • fake_before (bool) – Whether add fake quantized operation before activation. Default: False.

  • quant_config (QuantConfig) – Configures the oberser types and quant settings of weight and activation. Can be generated by compression.quant.create_quant_config method. Default: both set to default FakeQuantWithMinMaxObserver.

  • quant_dtype (QuantDtype) – Specifies the FakeQuant datatype. Default: QuantDtype.INT8.

Inputs:
  • input (Tensor) - The input of ActQuant.

Outputs:

Tensor, with the same type and shape as the input.

Raises
  • TypeError – If activation is not an instance of Cell.

  • TypeError – If fake_before is not a bool.

Supported Platforms:

Ascend GPU

Examples

>>> qconfig = compression.quant.create_quant_config()
>>> act_quant = nn.ActQuant(nn.ReLU(), quant_config=qconfig)
>>> input = Tensor(np.array([[1, 2, -1], [-2, 0, -1]]), mindspore.float32)
>>> output = act_quant(input)
>>> print(output)
[[0.9882355 1.9764705 0.       ]
 [0.        0.        0.       ]]