aclnnMoeTokenUnpermuteWithEp
Applicable Products
Ascend 950PR&950DT products : Not 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: Retrieves the input data from permutedTokens based on the index positions stored in sortedIndices, multiplies it with probs, and performs merge accumulation.
Formula:
$$ sortedIndices = sortedIndices[rangeOptional[0]<=i<rangeOptional[1]] $$(1) When probs is not None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$, $j \in {0, 1, 2, ..., topK\_num - 1}$, and $k \in {0, 1, 2, ..., num\_tokens * topK\_num}$:
$$ permutedTokens = permutedTokens.indexSelect(0, sortedIndices) $$$$ permutedTokens_{k} = permutedTokens_{k} * probs_{i,j} $$$$ out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} $$(2) When probs is None, the formula is as follows, where $i \in {0, 1, 2, ..., num\_tokens - 1}$ and $j \in {0, 1, 2, ..., topK\_num - 1}$:
$$ permutedTokens = permutedTokens.indexSelect(0, sortedIndices) $$$$ out_{i} = \sum_{k=i*topK\_num}^{(i+1)*topK\_num - 1 } permutedTokens_{k} $$
Function Prototype
Each operator uses a two-phase API. First call "aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize" to obtain the workspace size required for calculation and the executor that includes calculation of the operator's computational process. Then call "aclnnMoeTokenUnpermuteWithEp" to perform the calculation.
aclnnStatus aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize(
const aclTensor *permutedTokens,
const aclTensor *sortedIndices,
const aclTensor *probsOptional,
int64_t numTopk,
const aclIntArray *rangeOptional,
bool paddedMode,
const aclIntArray *restoreShapeOptional,
const aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor);aclnnStatus aclnnMoeTokenUnpermuteWithEp(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream);aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize
Parameters:
Return value
aclnnStatus: Return status code. For details, see aclnn Return 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 The required input, required output, or required attribute is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of the inputs and outputs are not supported.
aclnnMoeTokenUnpermuteWithEp
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the Device side. workspaceSize Input Size of the workspace requested on the Device side, obtained by calling the first-phase API aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize. executor Input Op executor that includes the calculation flow of the operator. stream Input Specifies the Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn return codes.
Constraints
Deterministic computation:
- aclnnMoeTokenUnpermuteWithEp defaults to a deterministic implementation.
topK_num <= 512.
paddedMode being
Trueis not supported.When rangeOptional is empty, numTopk is ignored, and the execution logic falls back to aclnnMoeTokenUnpermute.
Example
The sample code is as follows, for reference only. For details about compilation and execution, see Compile and Run Samples.
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_token_unpermute_with_ep.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 shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
void PrintOutResult(std::vector<int64_t> &shape, void **deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<float> resultData(size, 0);
auto ret = aclrtMemcpy(
resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr,
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 );
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream *stream) {
// Boilerplate: 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 CreateAclIntArray(const std::vector<T>& hostData, void** deviceAddr, aclIntArray** intArray) {
auto size = GetShapeSize(hostData) * sizeof(T);
// Call aclrtMalloc to allocate memory on the 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);
// Call aclrtMemcpy to copy the data on the host to the memory on the 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);
// Call aclCreateIntArray to create an aclIntArray.
*intArray = aclCreateIntArray(hostData.data(), hostData.size());
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 request memory on the device side.
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 data from the host side to the 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);
// Calculate 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) Device/stream initialization. For details, see the ACL API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret);
return ret);
// 2. Construct the inputs and outputs, customized based on the API.
std::vector<float> permutedTokensData = {2, 2, 1, 1, 3, 3, 2, 2};
std::vector<int64_t> permutedTokensShape = {4, 2};
void *permutedTokensAddr = nullptr;
aclTensor *permutedTokens = nullptr;
ret = CreateAclTensor(permutedTokensData, permutedTokensShape,
&permutedTokensAddr, aclDataType::ACL_FLOAT,
&permutedTokens);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<int> sortedIndicesData = {2, 0, 4, 1, 5, 3};
std::vector<int64_t> sortedIndicesShape = {6};
void *sortedIndicesAddr = nullptr;
aclTensor *sortedIndices = nullptr;
ret =
CreateAclTensor(sortedIndicesData, sortedIndicesShape, &sortedIndicesAddr,
aclDataType::ACL_INT32, &sortedIndices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<float> probsOptionalData = {1, 1, 1, 1, 1, 1};
std::vector<int64_t> probsOptionalShape = {3, 2};
void *probsOptionalAddr = nullptr;
aclTensor *probsOptional = nullptr;
ret =
CreateAclTensor(probsOptionalData, probsOptionalShape, &probsOptionalAddr,
aclDataType::ACL_FLOAT, &probsOptional);
CHECK_RET(ret == ACL_SUCCESS, return ret);
int64_t num_topk = 2;
void* rangeDeviceAddr = nullptr;
aclIntArray* range = nullptr;
std::vector<int64_t> rangeHostData = {1, 5};
ret = CreateAclIntArray(rangeHostData, &rangeDeviceAddr, &range);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<float> outData = {0, 0, 0, 0, 0, 0};
std::vector<int64_t> outShape = {3, 2};
void *outAddr = nullptr;
aclTensor *out = nullptr;
ret = CreateAclTensor(outData, outShape, &outAddr, aclDataType::ACL_FLOAT,
&out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
// Call the aclnnMoeTokenUnpermuteWithEp first-phase API.
ret = aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize(permutedTokens, sortedIndices,
probsOptional, num_topk, range, false, nullptr,
out, &workspaceSize, &executor);
CHECK_RET(
ret == ACL_SUCCESS,
LOG_PRINT("aclnnMoeTokenUnpermuteWithEpGetWorkspaceSize failed. ERROR: %d\n",
ret);
return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnMoeTokenUnpermuteWithEp second-phase API.
ret = aclnnMoeTokenUnpermuteWithEp(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnMoeTokenUnpermuteWithEp failed. ERROR: %d\n", ret);
return ret);
// 4. (boilerplate) Synchronously wait for the task execution to 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 the device to the host. Modify the code based on the API definition.
PrintOutResult(outShape, &outAddr);
// 6. Release aclTensor and aclScalar. Modify the code based on the API definition.
aclDestroyTensor(permutedTokens);
aclDestroyTensor(sortedIndices);
aclDestroyTensor(probsOptional);
aclDestroyTensor(out);
// 7. Release device resources.
aclrtFree(permutedTokensAddr);
aclrtFree(sortedIndicesAddr);
aclrtFree(probsOptionalAddr);
aclrtFree(outAddr);
aclrtFree(rangeDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}