aclnnMoeInitRoutingV2
支持的产品型号
- Atlas A2训练系列产品/Atlas 800I A2推理产品
接口原型
每个算子分为两段式接口,必须先调用 “aclnnMoeInitRoutingV2GetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnMoeInitRoutingV2”接口执行计算。
aclnnStatus aclnnMoeInitRoutingV2GetWorkspaceSize(const aclTensor *x, const aclTensor *expertIdx, int64_t activeNum, int64_t expertCapacity, int64_t expertNum, int64_t dropPadMode, int64_t expertTokensCountOrCumsumFlag, bool expertTokensBeforeCapacityFlag, aclTensor *expandedXOut, aclTensor *expandedRowIdxOut, aclTensor *expertTokensCountOrCumsumOut, aclTensor *expertTokensBeforeCapacityOut, uint64_t *workspaceSize, aclOpExecutor **executor)
aclnnStatus aclnnMoeInitRoutingV2(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)
功能描述
算子功能:MoE的routing计算,根据aclnnMoeGatingTopKSoftmax的计算结果做routing处理。
本接口针对aclnnMoeInitRouting做了如下功能变更,请根据实际情况选择合适的接口:
- 新增drop模式,在该模式下输出内容会根据每个专家的expertCapacity处理,超过expertCapacity不做处理,不足的会补0。
- 新增Dropless模式下expertTokensCountOrCumsumOut可选输出,drop场景下expertTokensBeforeCapacityOut可选输出。
- 删除rowIdx输入。
计算公式:
1.对输入expertIdx做排序,得出排序后的结果sortedExpertIdx和对应的序号sortedRowIdx:
2.以sortedRowIdx做位置映射得出expandedRowIdxOut:
3.对x取前numRows个sortedRowIdx的对应位置的值,得出expandedXOut:
4.对sortedExpertIdx的每个专家统计直方图结果,再进行Cumsum,得出expertTokensCountOrCumsumOut:
5.对sortedExpertIdx的每个专家统计直方图结果,得出expertTokensBeforeCapacityOut:
aclnnMoeInitRoutingV2GetWorkspaceSize
参数说明:
- x(aclTensor*,计算输入):MOE的输入即token特征输入,要求为一个2D的Tensor,shape为[NUM_ROWS, H],H代表每个Token的长度,数据类型支持FLOAT16、BFLOAT16、FLOAT32,数据格式要求为ND,支持非连续的Tensor。
- expertIdx (aclTensor*,计算输入):aclnnMoeGatingTopKSoftmax的输出每一行特征对应的K个处理专家,要求是一个2D的shape [NUM_ROWS, K]。数据类型支持int32,数据格式要求为ND,支持非连续的Tensor。值域范围是[0, expertNum - 1]。
- activeNum(int64_t,计算输入):表示是否为Active场景,该属性在dropPadMode为0时生效,值范围大于等于0;0表示Dropless场景,大于0时表示Active场景,约束所有专家共同处理tokens总量
- expertCapacity(int64_t, 计算输入):表示每个专家能够处理的tokens数,值范围大于等于0;Drop/Pad场景下expertCapacity需大于0,此时各专家将超过capacity的tokens drop掉,不够capacity阈值时则pad全0 tokens;其他场景不关心该属性值。
- expertNum(int64_t, 计算输入):表示专家数,值范围大于等于0;Drop/Pad场景下或者expertTokensCountOrCumsumFlag大于0需要输出expertTokensCountOrCumsumOut时,expertNum需大于0。
- dropPadMode(int64_t, 计算输入):表示是否为Drop/Pad场景,取值为0和1。
- 0:表示非Drop/Pad场景,该场景下不校验expertCapacity。
- 1:表示Drop/Pad场景,需要校验expertNum和expertCapacity,对于每个专家处理的超过和不足expertCapacity的值会做相应的处理。
- expertTokensCountOrCumsumFlag(int64_t, 计算输入):取值为0、1和2。
- 0:表示不输出expertTokensCountOrCumsumOut。
- 1:表示输出的值为各个专家处理的token数量的累计值。
- 2:表示输出的值为各个专家处理的token数量。
- expertTokensBeforeCapacityFlag(bool,计算输入):取值为false和true。
- false:表示不输出expertTokensBeforeCapacityOut。
- true:表示输出的值为在drop之前各个专家处理的token数量。
- expandedXOut(aclTensor*,计算输出):根据expertIdx进行扩展过的特征,在Dropless/Active场景下要求是一个2D的Tensor,Dropless场景shape为[NUM_ROWS * K, H],Active场景shape为[activeNum, H],在Drop/Pad场景下要求是一个3D的Tensor,shape为[expertNum, expertCapacity, H]。数据类型同x,支持FLOAT16、BFLOAT16、FLOAT32,数据格式要求为ND,不支持非连续的Tensor。
- expandedRowIdxOut(aclTensor*,计算输出):expandedXOut和x的索引映射关系, 要求是一个1D的Tensor,Shape为[NUM_ROWS*K, ],数据类型支持int32,数据格式要求为ND,不支持非连续的Tensor。
- expertTokensCountOrCumsumOut(aclTensor*,计算输出):输出每个专家处理的token数量的统计结果及累加值,通过expertTokensCountOrCumsumFlag参数控制是否输出,该值仅在非Drop/Pad场景下输出,要求是一个1D的Tensor,Shape为[expertNum, ],数据类型支持int32,数据格式要求为ND,不支持非连续的Tensor。
- expertTokensBeforeCapacityOut(aclTensor*,计算输出):输出drop之前每个专家处理的token数量的统计结果,通过expertTokensBeforeCapacityFlag参数控制是否输出,该值仅在Drop/Pad场景下输出,要求是一个1D的Tensor,Shape为[expertNum, ],数据类型支持int32,数据格式要求为ND,不支持非连续的Tensor。
- workspaceSize(uint64_t*,出参):返回用户需要在Device侧申请的workspace大小。
- executor(aclOpExecutor**,出参):返回op执行器,包含了算子计算流程。
返回值
返回aclnnStatus状态码,具体参见aclnn返回码。
第一段接口完成入参校验,出现以下场景时报错: 161001(ACLNN_ERR_PARAM_NULLPTR):1. 计算输入和必选计算输出是空指针 161002(ACLNN_ERR_PARAM_INVALID):1. 计算输入和输出的数据类型和格式不在支持的范围内 561002(ACLNN_ERR_INNER_TILING_ERROR): 1. x和expertIdx的shape维度不等于2,且第一维不相等 2. activeNum、expertNum、expertCapacity的值小于0 3. dropPadMode、expertTokensCountOrCumsumFlag、expertTokensBeforeCapacityFlag不在取值范围内 4. dropPadMode等于1时,expertCapacity和expertNum等于0 5. expertTokensCountOrCumsumOut需要输出时,expertNum等于0
aclnnMoeInitRoutingV2
参数说明:
- workspace(void*,入参):在Device侧申请的workspace内存地址。
- workspaceSize(uint64_t,入参):在Device侧申请的workspace大小,由第一段接口aclnnMoeInitRoutingV2GetWorkspaceSize获取。
- executor(aclOpExecutor*,入参):op执行器,包含了算子计算流程。
- stream(aclrtStream,入参):指定执行任务的AscendCL stream流。
返回值:
返回aclnnStatus状态码,具体参见aclnn返回码。
约束与限制
无。
调用示例
示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_init_routing_v2.h"
#include <iostream>
#include <vector>
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape) {
int64_t shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// 固定写法,AscendCL初始化
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape) * sizeof(T);
// 调用aclrtMalloc申请device侧内存
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// 调用aclrtMemcpy将host侧数据拷贝到device侧内存上
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// 计算连续tensor的strides
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// 调用aclCreateTensor接口创建aclTensor
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. 固定写法,device/stream初始化, 参考acl对外接口列表
// 根据自己的实际device填写deviceId
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// check根据自己的需要处理
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. 构造输入与输出,需要根据API的接口定义构造
std::vector<int64_t> xShape = {3, 4};
std::vector<int64_t> idxShape = {3, 2};
std::vector<int64_t> expandedXOutShape = {3, 2, 4};
std::vector<int64_t> idxOutShape = {6};
std::vector<int64_t> expertTokenOutShape = {3};
void* xDeviceAddr = nullptr;
void* expertIdxDeviceAddr = nullptr;
void* expandedXOutDeviceAddr = nullptr;
void* expandedRowIdxOutDeviceAddr = nullptr;
void* expertTokenBeforeCapacityOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* expertIdx = nullptr;
int64_t activeNum = 0;
int64_t expertCapacity = 2;
int64_t expertNum = 3;
int64_t dropPadMode = 1;
int64_t expertTokensCountOrCumsumFlag = 0;
bool expertTokensBeforeCapacityFlag = true;
aclTensor* expandedXOut = nullptr;
aclTensor* expandedRowIdxOut = nullptr;
aclTensor* expertTokensBeforeCapacityOut = nullptr;
std::vector<float> xHostData = {0.1, 0.1, 0.1, 0.1, 0.2, 0.2, 0.2, 0.2, 0.3, 0.3, 0.3, 0.3};
std::vector<int> expertIdxHostData = {1, 2, 0, 1, 0, 2};
std::vector<float> expandedXOutHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
std::vector<int> expandedRowIdxOutHostData = {0, 0, 0, 0, 0, 0};
std::vector<int> expertTokensBeforeCapacityOutHostData = {0, 0, 0};
// 创建self aclTensor
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(expertIdxHostData, idxShape, &expertIdxDeviceAddr, aclDataType::ACL_INT32, &expertIdx);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 创建out aclTensor
ret = CreateAclTensor(expandedXOutHostData, expandedXOutShape, &expandedXOutDeviceAddr, aclDataType::ACL_FLOAT, &expandedXOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(expandedRowIdxOutHostData, idxOutShape, &expandedRowIdxOutDeviceAddr, aclDataType::ACL_INT32, &expandedRowIdxOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(expertTokensBeforeCapacityOutHostData, expertTokenOutShape, &expertTokenBeforeCapacityOutDeviceAddr, aclDataType::ACL_INT32, &expertTokensBeforeCapacityOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. 调用CANN算子库API,需要修改为具体的API
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 调用aclnnMoeInitRoutingV2第一段接口
ret = aclnnMoeInitRoutingV2GetWorkspaceSize(x, expertIdx, activeNum, expertCapacity, expertNum, dropPadMode, expertTokensCountOrCumsumFlag, expertTokensBeforeCapacityFlag, expandedXOut, expandedRowIdxOut, nullptr, expertTokensBeforeCapacityOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;);
}
// 调用aclnnMoeInitRoutingV2第二段接口
ret = aclnnMoeInitRoutingV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingV2 failed. ERROR: %d\n", ret); return ret);
// 4. 固定写法,同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改
auto expandedXSize = GetShapeSize(expandedXOutShape);
std::vector<float> expandedXData(expandedXSize, 0);
ret = aclrtMemcpy(expandedXData.data(), expandedXData.size() * sizeof(expandedXData[0]), expandedXOutDeviceAddr, expandedXSize * sizeof(float),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < expandedXSize; i++) {
LOG_PRINT("expandedXData[%ld] is: %f\n", i, expandedXData[i]);
}
auto expandedRowIdxSize = GetShapeSize(idxOutShape);
std::vector<int> expandedRowIdxData(expandedRowIdxSize, 0);
ret = aclrtMemcpy(expandedRowIdxData.data(), expandedRowIdxData.size() * sizeof(expandedRowIdxData[0]), expandedRowIdxOutDeviceAddr, expandedRowIdxSize * sizeof(int32_t),
ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < expandedRowIdxSize; i++) {
LOG_PRINT("expandedRowIdxData[%ld] is: %d\n", i, expandedRowIdxData[i]);
}
auto expertTokensBeforeCapacitySize = GetShapeSize(expertTokenOutShape);
std::vector<int> expertTokenIdxData(expertTokensBeforeCapacitySize, 0);
ret = aclrtMemcpy(expertTokenIdxData.data(), expertTokenIdxData.size() * sizeof(expertTokenIdxData[0]), expertTokenBeforeCapacityOutDeviceAddr, expertTokensBeforeCapacitySize * sizeof(int32_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
for (int64_t i = 0; i < expertTokensBeforeCapacitySize; i++) {
LOG_PRINT("expertTokenIdxData[%ld] is: %d\n", i, expertTokenIdxData[i]);
}
// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
aclDestroyTensor(x);
aclDestroyTensor(expertIdx);
aclDestroyTensor(expandedXOut);
aclDestroyTensor(expandedRowIdxOut);
aclDestroyTensor(expertTokensBeforeCapacityOut);
// 7. 释放device资源,需要根据具体API的接口定义修改
aclrtFree(xDeviceAddr);
aclrtFree(expertIdxDeviceAddr);
aclrtFree(expandedXOutDeviceAddr);
aclrtFree(expandedRowIdxOutDeviceAddr);
aclrtFree(expertTokenBeforeCapacityOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}