pypto.quantize#
产品支持情况#
产品 |
是否支持 |
|---|---|
Ascend 950PR/Ascend 950DT |
√ |
Atlas A3 训练系列产品/Atlas A3 推理系列产品 |
√ |
Atlas A2 训练系列产品/Atlas A2 推理系列产品 |
√ |
功能说明#
将高精度浮点数据转换为低精度格式, 当前支持
输入DT_FP32的Tensor通过对称量化转换为DT_INT8的Tensor $\( \text{dst} = round(\text{input} * \text{scale}) \)$
输入DT_FP32的Tensor通过非对称量化转换为DT_UINT8的Tensor $\( \text{dst} = round(\text{input} * \text{scale} + \text{zero\_points}) \)$
函数原型#
quantize(input: Tensor, scale: Tensor, otype: DataType, axis: int, zero_points: Tensor) -> Tensor
参数说明#
参数名 |
输入/输出 |
说明 |
|---|---|---|
input |
输入 |
源操作数。 |
scale |
输入 |
缩放因子。 |
otype |
输入 |
返回值的数值类型 |
axis |
输入 |
指定量化压缩的轴 |
zero_points |
输入 |
可选的非对称量化的偏移因子 |
返回值说明#
返回输出Tensor,Tensor的数据类型由otype指定,Shape与input相同。
调用示例#
TileShape设置示例#
说明:调用该operation接口前,应通过set_vec_tile_shapes设置TileShape。
TileShape维度应和输出一致。
示例1:输入input shape为[m, n],输出为[m, n], TileShape设置为[m1, n1], 则m1, n1分别用于切分m, n轴。
pypto.set_vec_tile_shapes(4, 16)
接口调用示例#
x = pypto.tensor([3, 4], pypto.DT_FP32)
scale = pypto.tensor([3], pypto.DT_FP32)
zero_points = pypto.tensor([3], pypto.DT_FP32)
# fp32 -> int8 对称量化
y1 = pypto.quantize(x, scale, pypto.DT_INT8, -1, None)
# fp32 -> uint8 非对称量化
y2 = pypto.quantize(x, scale, pypto.DT_UINT8, -1, zero_points)
结果示例如下:
Input x:[[1.1, -2.2, 3.3, -4.4], [1.1, -2.2, 3.3, -4.4], [1.1, -2.2, 3.3, -4.4]]
Input scale:[1.0, 1.0, 1.0]
Input zero_points:[-5.0, -5.0, -5.0]
Output y1:[[1, -2, 3, -4], [1, -2, 3, -4], [1, -2, 3, -4]]
Output y2:[[6, 3, 8, 1], [6, 3, 8, 1], [6, 3, 8, 1]]