torch.fake_quantize_per_channel_affine

torch.fake_quantize_per_channel_affine(input, scale, zero_point, axis, quant_min, quant_max) Tensor

返回一个新的张量,该张量的数据使用scalezero_pointquant_minquant_max参数,在由axis指定的通道上对input进行通道级别的假量化。

$\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 (Tensor) – 量化比例因子,每个通道用 torch.float32 类型表示

  • zero_point (Tensor) – 量化零点(通道级别),类型为 torch.int32torch.halftorch.float32

  • axis (int32) – 表示通道的轴

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

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

返回值

一个新的通道级伪量化 torch.float32 张量

返回类型

张量

示例:

>>> x = torch.randn(2, 2, 2)
>>> x
tensor([[[-0.2525, -0.0466],
         [ 0.3491, -0.2168]],

        [[-0.5906,  1.6258],
         [ 0.6444, -0.0542]]])
>>> scales = (torch.randn(2) + 1) * 0.05
>>> scales
tensor([0.0475, 0.0486])
>>> zero_points = torch.zeros(2).to(torch.int32)
>>> zero_points
tensor([0, 0])
>>> torch.fake_quantize_per_channel_affine(x, scales, zero_points, 1, 0, 255)
tensor([[[0.0000, 0.0000],
         [0.3405, 0.0000]],

        [[0.0000, 1.6134],
        [0.6323, 0.0000]]])
本页目录