昇腾社区首页
中文
注册
开发者
下载

aclnnMoeInitRoutingV3

产品支持情况

[object Object]undefined

功能说明

  • 接口功能:MoE的routing计算,根据的计算结果做routing处理,支持不量化、静态量化和动态量化模式。

    本接口针对V2接口做出如下功能变更,请根据实际情况选择合适的接口:[object Object]

    [object Object]
  • 计算公式:

    1.对输入expertIdx做排序,得出排序后的结果sortedExpertIdx和对应的序号sortedRowIdx:

    sortedExpertIdx,sortedRowIdx=keyValueSort(expertIdx,rowIdx)sortedExpertIdx, sortedRowIdx=keyValueSort(expertIdx,rowIdx)

    2.以sortedRowIdx做位置映射得出expandedRowIdxOut:

    • rowIdxType等于1时, 输出scatter索引

      expandedRowIdxOut[i]=sortedRowIdx[i]expandedRowIdxOut[i]=sortedRowIdx[i]
    • rowIdxType等于0时, 输出gather索引

      expandedRowIdxOut[sortedRowIdx[i]]=iexpandedRowIdxOut[sortedRowIdx[i]]=i

    3.对sortedExpertIdx的每个专家统计直方图结果,得出expertTokensCountOrCumsumOutOptional:

    expertTokensCountOrCumsumOutOptional[i]=Histogram(sortedExpertIdx)expertTokensCountOrCumsumOutOptional[i]=Histogram(sortedExpertIdx)

    4.如果quantMode不等于-1, 计算quant结果:

    • 静态quant
    quantResult=round((xscaleOptional)+offsetOptional)quantResult=round((x∗scaleOptional)+offsetOptional)
    • 动态quant:
      • 若不输入scale:

        dynamicQuantScaleOutOptional=row_max(abs(x))/127dynamicQuantScaleOutOptional = row\_max(abs(x)) / 127 quantResult=round(x/dynamicQuantScaleOutOptional)quantResult = round(x / dynamicQuantScaleOutOptional)
      • 若输入scale:

        dynamicQuantScaleOutOptional=row_max(abs(xscaleOptional))/127dynamicQuantScaleOutOptional = row\_max(abs(x * scaleOptional)) / 127 quantResult=round(x/dynamicQuantScaleOutOptional)quantResult = round(x / dynamicQuantScaleOutOptional)

    5.若活跃的expert范围为全专家范围时,按照Scatter索引搬运token;反之按照Gather索引搬运token。在dropPadMode为1时将每个专家需要处理的Token个数对齐为expertCapacity个,超过expertCapacity个的Token会被Drop,不足的会用0填充。得出expandedXOut:

    • 非量化场景

      • 按照Scatter索引搬运
      expandedXOut[i]=x[scatterRowIdx[i]//K]expandedXOut[i]=x[scatterRowIdx[i] // K]
      • 按照Gather索引搬运
      expandedXOut[gatherRowIdx[i]]=x[i//K]expandedXOut[gatherRowIdx[i]]=x[i // K]
    • 量化场景

      • 按照Scatter索引搬运
      expandedXOut[i]=quantResult[scatterRowIdx[i]//K]expandedXOut[i]=quantResult[scatterRowIdx[i] // K]
      • 按照Gather索引搬运
      expandedXOut[gatherRowIdx[i]]=quantResult[i//K]expandedXOut[gatherRowIdx[i]]=quantResult[i // K]

    6.expandedRowIdxOut的有效元素数量availableIdxNum,计算方式为expertIdx中activeExpertRangeOptional范围内的元素的个数

    availableIdxNum={xexpertIdxexpert_startx<expert_end }availableIdxNum = |\{x\in expertIdx| expert\_start \le x<expert\_end \ \}|

函数原型

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

[object Object]
[object Object]

aclnnMoeInitRoutingV3GetWorkspaceSize

  • 参数说明

    [object Object]
  • 返回值

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

    第一段接口完成入参校验,出现以下场景时报错:

    [object Object][object Object]
  • 不同产品支持情况差异

    • quantMode支持情况差异:
      • [object Object]Atlas A2 训练系列产品/Atlas A2 推理系列产品[object Object]、[object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]:支持-1、0、1。

aclnnMoeInitRoutingV3

  • 参数说明:

    [object Object]
  • 返回值:

    返回aclnnStatus状态码,具体参见

约束说明

  • 确定性计算:

    • aclnnMoeInitRoutingV3默认确定性实现。
  • 该算子在以下产品型号上支持三种性能模板,需要分别额外满足准入条件,否则进入通用模板:

    • 支持性能模板的产品:
      • [object Object]Atlas A2 训练系列产品/Atlas A2 推理系列产品[object Object]
      • [object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]
    • 性能模板的准入条件:[object Object]

调用示例

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

[object Object]