aclnnMoeComputeExpertTokens
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
- Description: In MoE computation, uses binary search to find the position of the last row processed by each expert.
- Formula:$$ for\: i\: in\: range(numExperts) $$$$ out_{i}=BinarySearch(sortedExperts, i) $$
Function Prototype
Each operator uses a two-phase API. First call the "aclnnMoeComputeExpertTokensGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the computation process of the operator, and then call the "aclnnMoeComputeExpertTokens" API to perform the computation.
aclnnStatus aclnnMoeComputeExpertTokensGetWorkspaceSize(
const aclTensor *sortedExperts,
int64_t numExperts,
const aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnMoeComputeExpertTokens(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnMoeComputeExpertTokensGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor sortedExperts Input sortedExperts in the formula, the sorted expert array. The tensor values are in the range [0, numExperts-1], and the shape size must be less than 2**24. INT32 ND 1 √ numExperts Input Indicates the total number of experts. Must be greater than 0 and cannot exceed 2048. INT64 - - - out Output Output in the formula. Shape size equals the number of experts. Same as sortedExperts. ND 1 × workspaceSize Output Returns the workspace size to be requested on the Device. - - - - - executor Output Returns the op executor containing the operator computation flow. - - - - - Return Value
aclnnStatus: return status code. For details, see Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 sortedExperts is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of sortedExperts is not supported. The format of sortedExperts is not supported. ACLNN_ERR_INNER_TILING_ERROR 561002 The shapes of sortedExperts and out are not 1D tensors.
aclnnMoeComputeExpertTokens
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device. workspaceSize Input Size of the workspace requested on the Device, obtained by calling the first-phase API aclnnMoeComputeExpertTokensGetWorkspaceSize. executor Input Executor of the op, which contains the computation process of the operator. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Deterministic computation:
- aclnnMoeComputeExpertTokens defaults to a deterministic implementation.
The input shape size must not exceed the allocable memory limit of the device. Otherwise, the process terminates abnormally.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_compute_expert_tokens.h"
#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)
{
// Boilerplate for resource initialization
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);
// Call aclrtMalloc to allocate device memory.
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);
// Call aclrtMemcpy to copy host data to device memory.
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);
// Compute the strides of the contiguous tensor.
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];
}
// Call aclCreateTensor to create an 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. (boilerplate) Initialize device/stream. For details, see the list of acl APIs.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check based on your needs.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Construct them based on the API.
std::vector<int64_t> sortedExpertForSourceRowShape = {6};
std::vector<int64_t> outShape = {3};
void* sortedExpertForSourceRowAddr = nullptr;
void* outAddr = nullptr;
aclTensor* sortedExperts = nullptr;
aclTensor* out = nullptr;
std::vector<int32_t> sortedExpertForSourceRowData = {0, 0, 1, 1, 2, 2};
std::vector<int32_t> outData = {3, 4, 5};
std::int64_t numExperts = 3;
// Create the input aclTensor.
ret = CreateAclTensor(sortedExpertForSourceRowData,
sortedExpertForSourceRowShape,
&sortedExpertForSourceRowAddr,
aclDataType::ACL_INT32,
&sortedExperts);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the Out aclTensor.
ret = CreateAclTensor(outData, outShape, &outAddr, aclDataType::ACL_INT32, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Call the CANN operator library API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 3. Call the first-phase API aclnnMoeComputeExpertTokens.
ret = aclnnMoeComputeExpertTokensGetWorkspaceSize(
sortedExperts, numExperts, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeComputeExpertTokensGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
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;);
}
// Call the second-phase API aclnnMoeComputeExpertTokens.
ret = aclnnMoeComputeExpertTokens(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeComputeExpertTokens failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronize and wait until the task execution is complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify the code according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<int32_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(),
resultData.size() * sizeof(resultData[0]),
outAddr,
size * sizeof(resultData[0]),
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 < size; i++) {
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);
}
// 6. Release aclTensor. Modify the code according to the specific API definition.
aclDestroyTensor(sortedExperts);
aclDestroyTensor(out);
// 7. Release device resources. Modify the code according to the specific API definition.
aclrtFree(sortedExpertForSourceRowAddr);
aclrtFree(outAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}