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

aclnnMoeTokenPermuteWithRoutingMap

产品支持情况

[object Object]undefined

功能说明

  • 接口功能:MoE的permute计算,将token和expert的标签作为routingMap传入,根据routingMap将tokens和可选probsOptional广播后排序

  • 计算公式

    tokens_num 为routingMap的第0维大小,expert_num为routingMap的第1维大小 dropAndPad为[object Object]

    expertIndex=arange(tokens_num).expand(expert_num,1)expertIndex=arange(tokens\_num).expand(expert\_num,-1) sortedIndicesFirst=expertIndex.masked_select(routingMap.T)sortedIndicesFirst=expertIndex.masked\_select(routingMap.T) sortedIndicesOut=argsort(sortedIndicesFirst)sortedIndicesOut=argsort(sortedIndicesFirst) topK=numOutTokens//tokens_numtopK = numOutTokens // tokens\_num outToken=topKtokens_numoutToken = topK * tokens\_num permuteTokens[sortedIndicesOut[i]]=tokens[i//topK]permuteTokens[sortedIndicesOut[i]]=tokens[i//topK] permuteProbsOutOptional=probsOptional.T.masked_select(routingMap.T)permuteProbsOutOptional=probsOptional.T.masked\_select(routingMap.T)

    dropAndPad为[object Object]

    capacity=numOutTokens//expert_numcapacity = numOutTokens // expert\_num outToken=capacityexpert_numoutToken = capacity * expert\_num sortedIndicesOut=argsort(routingMap.T,dim=1)[:,:capacity]sortedIndicesOut = argsort(routingMap.T,dim=-1)[:, :capacity] permutedTokensOut=tokens.index_select(0,sorted_indices)permutedTokensOut = tokens.index\_select(0, sorted\_indices)

    如果probs不是none

    probs_T_1D=probsOptional.T.view(1)probs\_T\_1D = probsOptional.T.view(-1) indices_dim0=arange(expert_num).view(expert_num,1)indices\_dim0 = arange(expert\_num).view(expert\_num, 1) indices_dim1=sortedIndicesOut.view(expert_num,capacity)indices\_dim1 = sortedIndicesOut.view(expert\_num, capacity) indices_1D=(indices_dim0tokens_num+indices_dim1).view(1)indices\_1D = (indices\_dim0 * tokens\_num + indices\_dim1).view(-1) permuteProbsOutOptional=probs_T_1D.index_select(0,indices_1D)permuteProbsOutOptional = probs\_T\_1D.index\_select(0, indices\_1D)

函数原型

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

[object Object]
[object Object]

aclnnMoeTokenPermuteWithRoutingMapGetWorkspaceSize

  • 参数说明:

    [object Object]
  • 返回值:

    [object Object]:返回状态码,具体参见 [object Object]aclnn 返回码[object Object]。

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

    [object Object]

aclnnMoeTokenPermuteWithRoutingMap

  • 参数说明:

    [object Object]
  • 返回值:

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

约束说明

  • 确定性计算:

    • aclnnMoeTokenPermuteWithRoutingMap默认确定性实现。
  • tokens_num和experts_num要求小于[object Object],pad模式为false时routingMap 中 每行为1或true的个数固定且小于[object Object]

调用示例

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

[object Object]