昇腾社区首页
中文
注册

aclnnQuantMatmulDequant

支持的产品型号

  • [object Object]Atlas 推理系列产品[object Object]。

功能说明

  • 算子功能:对输入x进行量化,矩阵乘以及反量化。
  • 计算公式:
    1.若输入smoothScaleOptional,则x=xscalesmoothx = x\cdot scale_{smooth} 2.若不输入xScaleOptional,则为动态量化,需要计算x量化系数。scalex=row_max(abs(x))/maxquantDataTypescale_{x}=row\_max(abs(x))/max_{quantDataType} 3.量化xquantized=round(x/scalex)x_{quantized}=round(x/scale_{x}) 4.矩阵乘+反量化out=(xquantized@weightquantized+bias)scaleweightscalexout = (x_{quantized}@weight_{quantized} + bias) * scale_{weight} * scale_{x}

函数原型

每个算子分为,必须先调用“aclnnQuantMatmulDequantGetWorkspaceSize”接口获取入参并根据流程计算所需workspace大小,再调用“aclnnQuantMatmulDequant”接口执行计算。

  • aclnnStatus aclnnQuantMatmulDequantGetWorkspaceSize(const aclTensor *x, const aclTensor *weight, const aclTensor *weightScale, const aclTensor *biasOptional, const aclTensor *xScaleOptional, const aclTensor *xOffsetOptional, const aclTensor *smoothScaleOptional, char *xQuantMode, bool transposeWeight, const aclTensor *out, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnQuantMatmulDequant(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

aclnnQuantMatmulDequantGetWorkspaceSize

  • 参数说明:

    • x(aclTensor*,计算输入):必选参数,Device侧的aclTensor,公式中的输入xx,数据类型支持FLOAT16,支持ND。支持和空Tensor。shape支持2维,各个维度表示:(m,k)。
    • weight(aclTensor*,计算输入):必选参数,Device侧的aclTensor,公式中的weightquantizedweight_{quantized},数据类型支持INT8,支持昇腾亲和数据排布格式和ND格式。支持空Tensor。
      • ND格式下,shape支持2维。
        • 在transposeWeight为true情况下各个维度表示:(n,k)。
        • 在transposeWeight为false情况下各个维度表示:(k,n)。
      • 昇腾亲和数据排布格式下,shape支持4维。
        • 在transposeWeight为true情况下各个维度表示:(k1,n1,n0,k0),其中k0 = 32,n0 = 16,k1和x的k需要满足以下关系:ceilDiv(k,32)= k1。
        • 在transposeWeight为false情况下各个维度表示:(n1,k1,k0,n0),其中k0 = 16,n0 = 32,k1和x的k需要满足以下关系:ceilDiv(k,16)= k1。
        • 可使用aclnnCalculateMatmulWeightSizeV2接口以及aclnnTransMatmulWeight接口完成输入Format从ND到昇腾亲和数据排布格式的转换。
    • weightScale(aclTensor*,计算输入):必选参数,Device侧的aclTensor,weight的量化系数,公式中的scaleweightscale_{weight},数据类型支持FLOAT32,支持ND,支持和空Tensor。shape是1维(n,),其中n与weight的n一致。
    • biasOptional(aclTensor*,计算输入):可选参数,Device侧的aclTensor,公式中的biasbias,当前仅支持传入空指针。
    • xScaleOptional(aclTensor*,计算输入):可选参数,Device侧的aclTensor,x的量化系数,公式中的scalexscale_{x},数据类型支持FLOAT32,支持ND,支持。当xQuantMode为pertensor时,shape是1维(1,);当xQuantMode为pertoken时,shape是1维(m,),其中m与输入x的m一致。若为空则为动态量化。
    • xOffsetOptional(aclTensor*,计算输入):可选参数,Device侧的aclTensor,当前仅支持传入空指针。
    • smoothScaleOptional(aclTensor*,计算输入):可选参数,Device侧的aclTensor,x的平滑系数,公式中的scalesmoothscale_{smooth},数据类型支持FLOAT16,支持ND,支持,shape是1维(k,),其中k与x的k一致。
    • xQuantMode(string,计算输入):host侧的string,指定输入x的量化模式,支持取值pertoken/pertensor,动态量化时只支持pertoken。
    • transposeWeight(bool,计算输入):Host侧的bool,表示输入weight是否转置,类型支持bool。当前只支持true。
    • out(aclTensor*,计算输出):必选参数,Device侧的aclTensor,计算结果,公式中的outout,数据类型支持FLOAT16,支持ND,只支持连续Tensor, shape支持2维,各个维度表示:(m,n)。其中m与x的m一致,n与weight的n一致。
    • workspaceSize(uint64_t *,出参):返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor *,出参):返回op执行器,包含了算子计算流程。
  • 返回值:

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

    [object Object]

aclnnQuantMatmulDequant

  • 参数说明:

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

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

约束说明

n,k都需要是16的整数倍。

调用示例

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

[object Object]