昇腾社区首页
中文
注册

aclnnDynamicQuantV2

支持的产品型号

  • Atlas A2训练系列产品/Atlas 800I A2推理产品

接口原型

每个算子分为undefined,必须先调用“aclnnDynamicQuantV2GetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnDynamicQuantV2”接口执行计算。

  • aclnnstatus aclnnDynamicQuantV2GetWorkspaceSize(const aclTensor *x, aclTensor *smoothScalesOptional, aclTensor *groupIndexesOptional, int64_t dstType, aclTensor *yOut, aclTensor *scaleOut, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnstatus aclnnDynamicQuantV2(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

功能描述

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

  • 计算公式:

    • 对称量化:
    • 若不输入smoothScale,则
    scaleOut=row_max(abs(x))/127scaleOut=row\_max(abs(x))/127 yOut=round(x/scalOut)yOut=round(x/scalOut)
    • 若输入smoothScales,则scaleOut=row_max(abs(xsmoothScalesOptional))/127scaleOut=row\_max(abs(x\cdot smoothScalesOptional))/127
    yOut=round(x/scalOut)yOut=round(x/scalOut)
    • 非对称量化:
    • 若不输入smoothScale,则scaleOut=(row_max(x)row_min(x))/scale_optscaleOut=(row\_max(x) - row\_min(x))/scale\_opt offset=offset_optrow_max(x)/scaleOutoffset=offset\_opt-row\_max(x)/scaleOut yOut=round(x/scaleOut+offset)yOut=round(x/scaleOut+offset)
    • 若输入smoothScales,则input=xsmoothScalesOptionalinput = x\cdot smoothScalesOptional scaleOut=(row_max(input)row_min(input))/scale_optscaleOut=(row\_max(input) - row\_min(input))/scale\_opt offset=offset_optrow_max(input)/scaleOutoffset=offset\_opt-row\_max(input)/scaleOut yOut=round(input/scaleOut+offset)yOut=round(input/scaleOut+offset) 其中row_max代表每行求最大值,row_min代表每行求最小值。当输出yOut类型为INT8时,scale_opt为255.0,offset_opt为127.0;yOut类型为INT4时,scale_opt为15.0,offset_opt为7.0。

aclnnDynamicQuantV2GetWorkspaceSize

  • 参数说明:

    • x(aclTensor*, 计算输入):必选参数,算子输入的Tensor,shape维度要大于1,Device侧的aclTensor,数据类型支持FLOAT16、BFLOAT16,支持undefinedundefined支持ND。
    • smoothScalesOptional(aclTensor*, 计算输入):可选参数,算子输入的smoothScales,当没有groupIndexsOptional输入时shape维度时x的最后一维,有groupIndexsOptional输入时shape是两维,第一维大小是专家数,不超过1024,第二维大小是x的最后一维,Device侧的aclTensor,数据类型支持FLOAT16、BFLOAT16,并且数据类型要和x保持一致,支持undefinedundefined支持ND。
    • groupIndexesOptional(aclTensor*, 计算输入):可选参数,算子输入的groupIndexes,shape只有一维,Device侧的aclTensor,数据类型支持INT32,支持undefinedundefined支持ND。
    • dstType (int64_t, 计算输入):可选参数,输出y的类型对应的枚举值,Host侧的int,如果输出y类型为INT8,则为2;y类型为INT4时,则为29,默认为2。
    • yOut(aclTensor*, 计算输出):量化后的输出Tensor,shape维度和x保持一致,Device侧的aclTensor,数据类型支持INT4,INT8,暂不支持非连续的Tensor,undefined支持ND。
    • scaleOut(aclTensor*, 计算输出):量化使用的scale,shape维度为x的shape剔除最后一维,Device侧的aclTensor,数据类型支持FLOAT,暂不支持非连续的Tensor,undefined支持ND。
    • workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。
  • 返回值:

    aclnnStatus: 返回状态码,具体参见undefined

    [object Object]

aclnnDynamicQuantV2

  • 参数说明:

    • workspace(void *, 入参):在Device侧申请的workspace内存地址。
    • workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnAbsGetWorkspaceSize获取。
    • executor(aclOpExecutor *, 入参):op执行器,包含了算子计算流程。
    • stream(aclrtStream, 入参):指定执行任务的AscendCL Stream流。
  • 返回值:

    aclnnStatus: 返回状态码,具体参见undefined

约束与限制

针对Atlas A2训练系列产品/Atlas 800I A2推理产品,groupIndexesOptional的维度不超过1024。

调用示例

示例代码如下,仅供参考,具体编译和执行过程请参考undefined

[object Object]