文件最后提交记录最后更新时间
26 天前
26 天前
4 个月前
22 天前
22 天前
22 天前
7 个月前
2 个月前
README

AddRmsNormQuant

产品支持情况

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

功能说明

  • 算子功能:RmsNorm是大模型常用的标准化操作,相比LayerNorm,其去掉了减去均值的部分。AddRmsNormQuant算子将RmsNorm前的Add算子以及RmsNorm归一化的输出给到1个或2个Quantize算子融合起来,减少搬入搬出操作。

  • 计算公式:

    xi=x1i+x2ix_i={x1}_i+{x2}_i

    yi=xiRms⁡(x)gi+beta, where Rms⁡(x)=1n∑i=1nxi2+epsy_i=\frac{x_i}{\operatorname{Rms}(\mathbf{x})} g_i + beta, \quad \text { where } \operatorname{Rms}(\mathbf{x})=\sqrt{\frac{1}{n} \sum_{i=1}^n x_i^2+eps}

    • divMode为True时:

      y1=round((y/scales1)+zero_points1)y1=round((y/scales1)+zero\_points1)

      y2=round((y/scales2)+zero_points2)y2=round((y/scales2)+zero\_points2)

    • divMode为False时:

      y1=round((y∗scales1)+zero_points1)y1=round((y*scales1)+zero\_points1)

      y2=round((y∗scales2)+zero_points2)y2=round((y*scales2)+zero\_points2)

参数说明

参数名 输入/输出/属性 描述 数据类型 数据格式
x1 输入 表示标准化过程中的源数据张量,对应公式中的`x1`。 FLOAT32、FLOAT16、BFLOAT16 ND
x2 输入 表示标准化过程中的源数据张量,对应公式中的`x2`。 FLOAT32、FLOAT16、BFLOAT16 ND
gamma 输入 表示标准化过程中的权重张量,对应公式中的`g`。shape与x1需要norm(层归一化)的维度保持一致。 FLOAT32、FLOAT16、BFLOAT16 ND
scales1 输入 表示量化过程中得到y1的scales张量,对应公式中的`scales1`。 FLOAT32、FLOAT16、BFLOAT16 ND
scales2 可选输入 表示量化过程中得到y2的scales张量,对应公式中的`scales2`。 FLOAT32、FLOAT16、BFLOAT16 ND
zero_points1 可选输入 表示量化过程中得到y1的offset张量,对应公式中的`zero_points1`。 INT32、FLOAT32、FLOAT16、BFLOAT16 ND
zero_points2 可选输入 表示量化过程中得到y2的offset张量,对应公式中的`zero_points2`。 INT32、FLOAT32、FLOAT16、BFLOAT16 ND
beta 可选输入 表示标准化过程中的偏置项,公式中的`beta`。 FLOAT32、FLOAT16、BFLOAT16 ND
axis 可选属性
  • 表示需要进行量化的element-wise轴,其他的轴做broadcast,指定的轴不能超过输入x的维度数。当前仅支持-1,传其他值均不生效。
  • 默认值为-1。
INT -
epsilon 可选属性
  • 用于防止除0错误,对应公式中的`epsilon`。
  • 默认值为1e-6。
FLOAT32 -
div_mode 可选属性
  • 公式中决定量化公式是否使用除法的参数,对应公式中的`divMode`。
  • 默认值为true。
BOOL -
y1 输出 表示量化输出Tensor,对应公式中的`y1`。 INT8、HIFLOAT8、FLOAT8_E5M2、FLOAT8_E4M3FN ND
y2 输出 表示量化输出Tensor,对应公式中的`y2`。 INT8、HIFLOAT8、FLOAT8_E5M2、FLOAT8_E4M3FN ND
x 输出 表示x1和x2的和,对应公式中的`x`。 FLOAT32、FLOAT16、BFLOAT16 ND
  • Atlas 推理系列产品:x1、x2、gamma、scales1、scales2、zero_points1、zero_points2、beta、x的数据类型不支持BFLOAT16,输出参数y1、y2的数据类型仅支持INT8。

  • Atlas A2 训练系列产品/Atlas A2 推理系列产品、Atlas A3 训练系列产品/Atlas A3 推理系列产品:x1、x2、gamma、x、beta的数据类型不支持FLOAT32,输出参数y1、y2的数据类型仅支持INT8。

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

    • x1、x2、gamma、beta和x的数据类型只支持FLOAT16。
    • scales1和scales2的数据类型只支持FLOAT32。
    • zero_points1和zero_points2的数据类型只支持INT32。
    • y1和y2的数据类型只支持INT8。

约束说明

Atlas 推理系列产品:x1x2需要norm的维度数据个数不能小于32。gammabetascales1scales2zero_points1zero_points2的数据个数不能小于32。

调用说明

调用方式 样例代码 说明
aclnn接口 test_aclnn_add_rms_norm_quant 通过aclnnAddRmsNormQuant接口方式调用AddRmsNormQuant算子。
图模式 - 通过算子IR构图方式调用AddRmsNormQuant算子。