文件最后提交记录最后更新时间
3 个月前
3 个月前
5 个月前
5 个月前
4 个月前
5 个月前
7 个月前
3 个月前
README.md

DynamicQuantV2

产品支持情况

产品 是否支持
Ascend 950PR/Ascend 950DT
Atlas A3 训练系列产品/Atlas A3 推理系列产品
Atlas A2 训练系列产品/Atlas A2 推理系列产品
Atlas 200I/500 A2 推理产品 ×
Atlas 推理系列产品
Atlas 训练系列产品
Kirin X90 处理器系列产品
Kirin 9030 处理器系列产品

功能说明

  • 算子功能:为输入张量进行per-token对称/非对称动态量化。在MOE场景下,每个专家的smooth_scales是不同的,根据输入的group_index进行区分。

  • 计算公式:

    • 对称量化:

      • 若不输入smooth_scales,则

        scale=row_max(abs(x))/127 scale=row\_max(abs(x))/127

        y=round(x/scale) y=round(x/scale)

      • 若输入smooth_scales,则

        input=x⋅smoothscales input = x\cdot smooth_scales

        scale=row_max(abs(input))/127 scale=row\_max(abs(input))/127

        y=round(input/scale) y=round(input/scale)

    • 非对称量化:

      • 若不输入smooth_scales,则

        scale=(row_max(x)−row_min(x))/scale_opt scale=(row\_max(x) - row\_min(x))/scale\_opt

        offset=offset_opt−row_max(x)/scale offset=offset\_opt-row\_max(x)/scale

        y=round(x/scale+offset) y=round(x/scale+offset)

      • 若输入smooth_scales,则

        input=x⋅smoothscales input = x\cdot smooth_scales

        scale=(row_max(input)−row_min(input))/scale_opt scale=(row\_max(input) - row\_min(input))/scale\_opt

        offset=offset_opt−row_max(input)/scale offset=offset\_opt-row\_max(input)/scale

        y=round(input/scale+offset) y=round(input/scale+offset)

    其中:

    • row_max代表每行求最大值。
    • row_min代表每行求最小值。
    • 当输出y类型为INT8时,scale_opt为255.0,offset_opt为127.0。
    • 当输出y类型为INT4时,scale_opt为15.0,offset_opt为7.0。

参数说明

参数名 输入/输出/属性 描述 数据类型 数据格式
x 输入
  • 算子输入的Tensor,对应公式中的`x`。
  • shape维度要大于1。
FLOAT16、BFLOAT16 ND
smooth_scales 可选输入
  • 算子输入的Tensor,对应公式中的`smooth_scales`。
  • 数据类型与x的数据类型保持一致。当group_index为空时,形状为1维。Dim[0]是x的最后一个维度;当group_index不为空时,形状为2维。Dim[0]是专家数(E)。E必须不大于1024。Dim[1]是x的最后一个维度。
FLOAT16、BFLOAT16 ND
group_index 可选输入
  • 指定组的索引。
  • shape支持1D,为[E, ],shape的第一维与smooth_scales形状的第一维相同。
INT32 ND
dst_type 可选属性
  • 指定输出y的数据类型。支持DT_INT4(29)、DT_INT8(2)、DT_FLOAT8_E5M2(35)、DT_FLOAT8_E4M3FN(36)、DT_HIFLOAT8(34)。
  • 默认值为DT_INT8(2)。
INT -
is_symmetrical 可选属性
  • 是否使用对称量化。
  • 默认值为false。
BOOL -
quant_mode 可选属性
  • 指定量化模式。支持"pertoken"、"pertensor"。
  • 默认值为"pertoken"。
STRING -
y 输出
  • 量化后的输出张量,对应公式中的`y`。
  • shape与输入x的保持一致。当y的数据类型为INT4,x的最后一个维度必须能被2整除。
INT8、INT4、FLOAT8_E5M2、FLOAT8_E4M3FN、HIFLOAT8 ND
scale 输出
  • 量化后的缩放系数,对应公式中的`scale`。
  • 当quant_mode为"pertoken"时,形状与x去掉最后一个维度后的形状相同。当quant_mode为"pertensor"时,形状为(1,)。
FLOAT32 ND
offset 输出
  • 非对称量化使用的offset,对应公式中的`offset`。
  • shape与scale的shape保持一致。
FLOAT32 ND
  • Atlas 推理系列产品、Atlas 训练系列产品 :

    • 输入x:数据类型仅支持FLOAT16。
    • 输出y:数据类型仅支持INT8。
    • 输入smooth_scalesgroup_index为预留参数,当前版本不参与计算。
    • 输入dst_type:只支持配置为2。
  • Atlas A3 训练系列产品/Atlas A3 推理系列产品、Atlas A2 训练系列产品/Atlas A2 推理系列产品:输出y的数据类型仅支持INT8、INT4。

  • Kirin X90/Kirin 9030 处理器系列产品:

    • 输入x:数据类型仅支持FLOAT16。
    • 可选输入smooth_scales:数据类型仅支持FLOAT16。
    • 输出y:数据类型仅支持INT8。

约束说明

  • E不应大于x去掉最后一个维度后的维度的乘积结果(S)。
  • group_index的值应递增,范围从0到S。最后一个值应为S。否则结果将无意义。

调用说明

调用方式 样例代码 说明
aclnn接口 test_aclnn_dynamic_quant_v2 通过aclnnDynamicQuantV2接口方式调用DynamicQuantV2算子。
图模式 - 通过算子IR构图方式调用DynamicQuantV2算子。