torch.fake_quantize_per_tensor_affine

torch.fake_quantize_per_tensor_affine(input, scale, zero_point, quant_min, quant_max) Tensor

返回一个新的张量,使用scalezero_pointquant_minquant_maxinput中的数据进行假量化处理。

$\text{output} = ( min( \text{quant\_max}, max( \text{quant\_min}, \text{std::nearby\_int}(\text{input} / \text{scale}) + \text{zero\_point} ) ) - \text{zero\_point} ) \times \text{scale}$
参数
  • 输入 (Tensor) – 输入的值,类型为 torch.float32 的张量

  • scale(双精度标量或 float32 Tensor)– 量化比例因子

  • zero_point (int64 标量或 int32 张量) — 量化中的零点

  • quant_min (int64) – 量化域的最小值

  • quant_max (int64) – 量化域的最大值

返回值

一个新的假量化的 torch.float32 张量

返回类型

张量

示例:

>>> x = torch.randn(4)
>>> x
tensor([ 0.0552,  0.9730,  0.3973, -1.0780])
>>> torch.fake_quantize_per_tensor_affine(x, 0.1, 0, 0, 255)
tensor([0.1000, 1.0000, 0.4000, 0.0000])
>>> torch.fake_quantize_per_tensor_affine(x, torch.tensor(0.1), torch.tensor(0), 0, 255)
tensor([0.1000, 1.0000, 0.4000, 0.0000])
本页目录