开发者
下载
[object Object]

[object Object][object Object]undefined
[object Object]
  • 接口功能:MoE计算中,对输入x做Sigmoid、SoftMax或者SqrtSoftplus计算,对计算结果分组进行排序,最后根据分组排序的结果选取前k个专家。支持两种模式:

    • TopK模式:对normValue进行TopK排序选择专家
    • Hash模式:根据inputIds从tid2eid映射表中获取预计算的专家索引,跳过排序步骤直接输出(aclnnMoeGatingTopKV2新增支持)
  • 计算公式:

    TopK模式:

    Step 1: 归一化

    根据normType对输入x做归一化:

    normOut={SoftMax(x),normType=0Sigmoid(x),normType=1Softplus(x),normType=2(仅Ascend 950PR/Ascend 950DT支持)normOut = \begin{cases} \text{SoftMax}(x), & normType = 0 \\ \text{Sigmoid}(x), & normType = 1 \\ \sqrt{\text{Softplus}(x)}, & normType = 2\quad \text{(仅Ascend 950PR/Ascend 950DT支持)} \end{cases}

    Step 2: 加偏置

    若bias不为空,加偏置得到用于选择的值:

    normValue=normOut+biasnormValue = normOut + bias

    否则 normValue=normOutnormValue = normOut

    Step 3: 分组筛选(仅groupCount > 1 时执行)

    将normValue按groupCount分组,根据groupSelectMode计算每组得分:

    groupedValue=Reshape(normValue, [batch, groupCount, 1])groupedValue = Reshape(normValue,\ [batch,\ groupCount,\ -1]) groupScore={ReduceMax(groupedValue, dim=1),groupSelectMode=0ReduceSum(TopK(groupedValue, k=2, dim=1), dim=1),groupSelectMode=1groupScore = \begin{cases} ReduceMax(groupedValue,\ dim=-1), & groupSelectMode = 0 \\ ReduceSum(TopK(groupedValue,\ k=2,\ dim=-1),\ dim=-1), & groupSelectMode = 1 \end{cases}

    选取得分最高的kGroup个组,将未选中组的对应位置置为 -\infty

    groupIdx=TopK(groupScore, k=kGroup).indicesgroupIdx = TopK(groupScore,\ k=kGroup).indices normValue=Mask(groupedValue, groupIdx, fillValue=)normValue = Mask(groupedValue,\ groupIdx,\ fillValue=-\infty)

    Step 4: Top-K专家选择

    对normValue取Top-K得到专家索引,这里只需要expertIdxOut:

    y,expertIdxOut=TopK(normValue[groupIdx,:], k=k)y, expertIdxOut = TopK(normValue[groupIdx, :],\ k=k)

    Step 5: Renorm与缩放

    根据expertIdxOut从normOut中取出对应的k个专家得分:

    gathered=normOut[expertIdxOut]gathered = normOut[\text{expertIdxOut}]

    normType=1 or normType=2 时做归一化;normType=0 时,renorm参数生效,renorm=1 时做renorm:

    if (normType=1 or normType=2) or (normType=0 and renorm=1):if\ (normType = 1\ or\ normType = 2)\ or\ (normType = 0\ and\ renorm = 1): yOut=gatheredReduceSum(normOut, dim=1)+eps\quad yOut = \frac{gathered}{ReduceSum(normOut,\ dim=-1) + eps}

    否则 yOut=gatheredyOut = gathered

    最终输出:

    yOut=yOut×routedScalingFactoryOut = yOut \times routedScalingFactor

    Step 6: 可选输出

    若outFlag为True,第三个输出为normOut;否则为空。

    Hash模式:

    当提供inputIds和tid2eid时,启用Hash模式:

    Step 1: 归一化

    根据normType对输入x做归一化(与TopK模式相同):

    normOut={SoftMax(x),normType=0Sigmoid(x),normType=1Softplus(x),normType=2 (<term>Ascend950PR/Ascend950DT</term>支持)normOut = \begin{cases} SoftMax(x), & normType = 0 \\ Sigmoid(x), & normType = 1 \\ \sqrt{Softplus(x)}, & normType = 2\ (仅<term>Ascend 950PR/Ascend 950DT</term>支持) \end{cases}

    Step 2: Hash索引查找

    根据inputIds从tid2eid映射表获取专家索引:

    expertIdxOut=tid2eid[inputIds,:]expertIdxOut = tid2eid[inputIds, :]

    其中tid2eid的shape为[numKeys, k],inputIds的shape为[batch],每个inputIds值对应一行k个专家索引。

    Step 3: Gather与缩放

    根据expertIdxOut从normOut中取出对应的k个专家得分:

    gathered=normOut[expertIdxOut]gathered = normOut[expertIdxOut]

    normType=1 or normType=2 时做归一化;normType=0 时,renorm参数生效,renorm=1 时做renorm:

    if (normType=1 or normType=2) or (normType=0 and renorm=1):if\ (normType = 1\ or\ normType = 2)\ or\ (normType = 0\ and\ renorm = 1): yOut=gatheredReduceSum(gathered)+eps\quad yOut = \frac{gathered}{ReduceSum(gathered) + eps}

    否则 yOut=gatheredyOut = gathered

    最终输出:

    yOut=yOut×routedScalingFactoryOut = yOut \times routedScalingFactor
[object Object]

每个算子分为两段式接口,必须先调用"aclnnMoeGatingTopKV2GetWorkspaceSizeV2"接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用"aclnnMoeGatingTopKV2"接口执行计算。

[object Object]
[object Object]
[object Object]
  • 参数说明:

    [object Object]
  • 返回值:

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

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

    [object Object]
[object Object]
  • 参数说明:

    [object Object]
  • 返回值:

    返回aclnnStatus状态码,具体参见

[object Object]
  • 确定性计算:
    • aclnnMoeGatingTopKV2默认确定性实现。
  • 输入shape限制:
    • x最后一维(即专家数)要求不大于2048。
  • 输入值域限制:
    • 要求1 <= k <= x_shape[-1] / groupCount * kGroup。
    • 要求1 <= kGroup <= groupCount,并且kGroup * x_shape[-1] / groupCount的值要大于等于k。
    • 要求groupCount > 0,x_shape[-1]能够被groupCount整除且整除后的结果大于groupSelectMode,并且整除的结果按照32个数对齐后乘groupCount的结果不大于2048。
  • 其他限制:
    • groupSelectMode取值0和1,0表示使用最大值对group进行排序, 1表示使用topk2的sum值对group进行排序。
    • normType取值0、1和2(仅[object Object]Ascend 950PR/Ascend 950DT[object Object]支持),0表示使用Softmax函数,1表示使用Sigmoid函数,2表示使用SqrtSoftplus函数。
    • normType取值为1或2时,renorm参数无效;normType取值为0时,renorm参数生效,renorm取值为0和1,0表示不做renorm,1表示做renorm。
    • outFlag取值true和false,true表示输出,false表示不输出。
  • Hash模式限制
    • Hash模式需同时提供inputIdsOptional和tid2eidOptional,否则为TopK模式。
    • Hash模式仅支持简化路径(kGroup == groupCount或groupCount == expertCount)。
    • Hash模式下k要求不大于64。
    • tid2eid的shape必须为[numKeys, k],其中numKeys为映射表总行数,k与参数k相等。
    • inputIds的shape必须为[batch],与x的第一维相等。
    • inputIds中的每个值应在[0, numKeys-1]范围内。
    • inputIdsOptional和tid2eidOptional的数据类型支持INT32和INT64的组合。
[object Object]

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

[object Object]