昇腾社区首页
中文
注册

aclnnMoeTokenPermuteWithEp

产品支持情况

产品 是否支持
[object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]
[object Object]Atlas A2 训练系列产品/Atlas 800I A2 推理产品/A200I A2 Box 异构组件[object Object]
[object Object]Atlas 200I/500 A2 推理产品[object Object] ×
[object Object]Atlas 推理系列产品 [object Object] ×
[object Object]Atlas 训练系列产品[object Object] ×

功能说明

  • 算子功能:MoE的permute计算,根据索引indices将tokens和可选probs广播后排序并按照rangeOptional中范围切片。
  • 计算公式
    • paddedModefalse

      sortedIndicesFirst=argSort(indices)sortedIndicesFirst=argSort(indices) sortedIndicesOut=argSort(sortedIndices)sortedIndicesOut=argSort(sortedIndices)

      当rangeOptional[0] <= sortedIndices[i] < rangeOptional[1]时

      permuteTokensOut[sortedIndices[i]range[0]]=tokens[i//topK]permuteTokensOut[sortedIndices[i]-range[0]]=tokens[i//topK] permuteProbsOut[sortedIndices[i]rangeOptional[0]]=probsOptional[i]permuteProbsOut[sortedIndices[i]-rangeOptional[0]]=probsOptional[i]
    • paddedMode为true

      permuteTokensOut[i]=tokens[indices[i]]permuteTokensOut[i]=tokens[indices[i]] sortedIndicesOut=indicessortedIndicesOut=indices

函数原型

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

  • aclnnStatus aclnnMoeTokenPermuteWithEpGetWorkspaceSize(const aclTensor *tokens, const aclTensor *indices, const aclTensor *probsOptional, const aclIntArray *rangeOptional, int64_t numOutTokens, bool paddedMode, const aclTensor *permuteTokensOut, const aclTensor *sortedIndicesOut, const aclTensor *permuteProbsOut, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnMoeTokenPermuteWithEp(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

aclnnMoeTokenPermuteWithEpGetWorkspaceSize

  • 参数说明:

    • tokens(aclTensor *,计算输入):表示permute中的输入tokens,公式中的tokens,Device侧的aclTensor。shape支持2D维度,shape为(num_tokens,hidden_size),num_tokens为tokens的数目,hidden_size为每个tokens的长度。数据类型支持BFLOAT16、FLOAT16、FLOAT32,undefined支持ND。支持undefined,不支持空Tensor。
    • indices(aclTensor *,计算输入):表示输入tokens对应的专家索引,公式中的indices,Device侧的aclTensor。shape支持1D或2D维度。paddedMode为false时表示每一个输入token对应的topK个处理专家索引,shape为(num_tokens,topK_num)或(num_tokens),paddedMode为true时表示每个专家选中的token索引(暂不支持)。要求元素个数小于16777215,值大于等于0小于16777215,topK_num小于等于512。数据类型支持INT32、INT64,undefined支持ND。支持undefined,不支持空Tensor。
    • probsOptional(aclTensor *,计算输入):表示输入tokens对应的专家概率,公式中的probsOptional,Device侧的aclTensor。可选计算输入,与计算输出permuteProbsOut对应,传入空则不输出permuteProbsOut。shape与indices的shape相同。数据类型支持BFLOAT16、FLOAT16、FLOAT32,undefined支持ND。支持undefined
    • rangeOptional(aclIntArray *,计算输入):ep切分的有效范围,size为2。为空时,忽略probsOptional和permuteTokensOut,执行逻辑回退到undefined
    • numOutTokens(int64_t,计算输入):有效输出token数,在rangeOptional为空时生效。设置为0时,表示不会删除任何token。不为0时,会按照numOutTokens进行切片丢弃按照indices排序好的token中超过numOutTokens的部分,为负数时按照切片索引为负数时处理。
    • paddedMode(bool,计算输入):paddedMode为true时表示indices已被填充为代表每个专家选中的token索引,此时不对indices进行排序。目前仅支持paddedMode为false。
    • permuteTokensOut(aclTensor *,计算输出):表示根据indices进行扩展并排序过的tokens,公式中的permuteTokensOut,Device侧的aclTensor。shape支持2D维度,shape为(rangeOptional[1] - rangeOptional[0],hidden_size)。数据类型同tokens,undefined支持ND。
    • sortedIndicesOut(aclTensor *,计算输出):表示permuteTokensOut和tokens的映射关系,公式中的sortedIndicesOut,Device侧的aclTensor。shape支持1D维度,Shape为(num_tokens * topK_num)。数据类型支持INT32,undefined要求为ND。
    • permuteProbsOut(aclTensor *,计算输出):表示根据indices进行扩展并排序过的probs,公式中的permuteProbsOut,Device侧的aclTensor。shape支持1D维度,shape为(rangeOptional[1] - rangeOptional[0])。数据类型同tokens,undefined要求为ND。
    • workspaceSize(uint64_t *,出参):返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor **,出参):返回op执行器,包含了算子计算流程。
  • 返回值:

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

    [object Object]

aclnnMoeTokenPermuteWithEp

  • 参数说明:

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

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

约束说明

  • indices 要求元素个数小于16777215,值大于等于0小于16777215(单点支持int32或int64的最大或最小值,其余值不在范围内排序结果不正确)。
  • topK小于等于512
  • 不支持paddedMode为True
  • 当rangeOptional为空时,忽略probsOptional和permuteTokensOut,执行逻辑回退到undefined

调用示例

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

[object Object]