aclnnMoeInitRoutingQuant
Applicable Products
| Product | Supported |
|---|---|
| ร | |
| โ | |
| โ | |
| ร | |
| ร | |
| ร |
Function
Description: Performs MoE routing computation, processes routing based on the computation result of aclnnMoeGatingTopKSoftmax, and quantizes the result.
Formula:
Flattens the expertIdx with the input shape [NUM_ROWS, K] into a single row for sorting, where NUM_ROWS is the number of input tokens and K is the number of experts selected for each token.
Function Prototype
Each operator is divided into a two-phase API. The "aclnnMoeInitRoutingQuantGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnMoeInitRoutingQuant" API is called to perform the computation.
aclnnStatus aclnnMoeInitRoutingQuantGetWorkspaceSize(
const aclTensor *x,
const aclTensor *rowIdx,
const aclTensor *expertIdx,
int64_t activeNum,
double scale,
double offset,
const aclTensor *expandedXOut,
const aclTensor *expandedRowIdxOut,
const aclTensor *expandedExpertIdxOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnMoeInitRoutingQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnMoeInitRoutingQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor x Input The input of MoE, i.e., the token feature input. - Supports null tensor.
- Must be a 2D tensor with shape (NUM_ROWS, H).
FLOAT16, BFLOAT16, FLOAT32 ND 2 โ rowIdx Input Indicates the original row position corresponding to each position. - Supports null tensor.
- The shape must be consistent with expertIdx, with values starting from 0 and incrementing along dimension 1.
INT32 ND 2 Yes expertIdx Input K processing experts corresponding to each row of features. - Supports null tensor.
- Must be a 2D tensor with shape (NUM_ROWS, K).
INT32 ND 2 โ activeNum Input Indicates the total maximum number of rows to process. The value range is greater than or equal to 0. Only this many rows in expandedXOut are valid. - - - - scale Input The scaling factor required for quantization computation. Typical value: S = (Rmax - Rmin) / (Qmax - Qmin), where Rmax denotes the maximum value among the input floating-point numbers, Rmin denotes the minimum value among the input floating-point numbers, Qmax denotes the maximum fixed-point value, and Qmin denotes the minimum fixed-point value. DOUBLE - - - offset Input Offset required for quantization computation. - - - - - expandedXOut Output Features expanded based on expertIdx. - Supports null tensor.
- Must be a 2D tensor with shape (min(NUM_ROWS, activeNum) * k, H).
INT8 ND 2 ร expandedRowIdxOut Output Mapping between expandedX and x. - Supports null tensor.
- Must be a 1D tensor with shape (NUM_ROWS*K).
INT32 ND 1 ร expandedExpertIdxOut Output Outputs the sorted result of expertIdx. - Supports null tensor.
- Must be a 1D tensor with shape (NUM_ROWS*K).
INT32 ND 1 ร workspaceSize Output Returns the workspace size to be allocated on the Device side. - - - - - executor Output Returns the operator executor, which includes the operator computation flow. - - - - - Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Return Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input or output tensor is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of the input and output are not within the supported range. ACLNN_ERR_INNER_TILING_ERROR 561002 The shape dimension of x is not 2. The shape of rowIdx is not 2, or the shapes of rowIdx and expertIdx are not equal. activeNum is less than 0. The shapes of expandedRowIdxOut and expandedExpertIdxOut are not equal, and are not equal to num_rows * k. The shape of expandedXOut is not equal to (min(num_rows, activeNum) * k, H).
aclnnMoeInitRoutingQuant
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side, obtained from the first-phase API aclnnMoeInitRoutingQuantGetWorkspaceSize.executor Input Operator executor that includes the calculation process. stream Input Specifies the stream on which the task is executed. Return value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnMoeInitRoutingQuant defaults to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include "acl/acl.h"
#include "aclnnop/aclnn_moe_init_routing_quant.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) {
// 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 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);
// Copy data from the host side to the device memory using aclrtMemcpy.
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 a 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];
}
// Create an aclTensor by calling the aclCreateTensor API.
*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. See the list of external ACL APIs.
// Set deviceId based on your actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle the check based on your own needs.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct inputs and outputs based on the API definition.
std::vector<int64_t> xShape = {3, 4};
std::vector<int64_t> idxShape = {3, 2};
std::vector<int64_t> expandedXOutShape = {6, 4};
std::vector<int64_t> idxOutShape = {6};
void* xDeviceAddr = nullptr;
void* rowIdxDeviceAddr = nullptr;
void* expertIdxDeviceAddr = nullptr;
void* expandedXOutDeviceAddr = nullptr;
void* expandedRowIdxOutDeviceAddr = nullptr;
void* expandedExpertIdxOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* rowIdx = nullptr;
aclTensor* expertIdx = nullptr;
int64_t activeNum = 3;
double scale = 1.0;
double offset = 1.0;
aclTensor* expandedXOut = nullptr;
aclTensor* expandedRowIdxOut = nullptr;
aclTensor* expandedExpertIdxOut = 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<int> rowIdxHostData = {0, 3, 1, 4, 2, 5};
std::vector<int8_t> 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> expandedExpertIdxOutHostData = {0, 0, 0, 0, 0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(rowIdxHostData, idxShape, &rowIdxDeviceAddr, aclDataType::ACL_INT32, &rowIdx);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(expertIdxHostData, idxShape, &expertIdxDeviceAddr, aclDataType::ACL_INT32, &expertIdx);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(expandedXOutHostData, expandedXOutShape, &expandedXOutDeviceAddr, aclDataType::ACL_INT8, &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(expandedExpertIdxOutHostData, idxOutShape, &expandedExpertIdxOutDeviceAddr, aclDataType::ACL_INT32, &expandedExpertIdxOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnMoeInitRoutingQuant.
ret = aclnnMoeInitRoutingQuantGetWorkspaceSize(x, rowIdx, expertIdx, activeNum, scale, offset, expandedXOut, expandedRowIdxOut, expandedExpertIdxOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingQuantGetWorkspaceSize 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 of aclnnMoeInitRoutingQuant.
ret = aclnnMoeInitRoutingQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMoeInitRoutingQuant failed. ERROR: %d\n", ret); return ret);
// 4. Boilerplate: synchronize and wait for 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 memory to the host memory. Modify this based on the specific API definition.
auto expandedXSize = GetShapeSize(expandedXOutShape);
std::vector<int8_t> expandedXData(expandedXSize, 0);
ret = aclrtMemcpy(expandedXData.data(), expandedXData.size() * sizeof(expandedXData[0]), expandedXOutDeviceAddr, expandedXSize * sizeof(int8_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 < expandedXSize; i++) {
LOG_PRINT("expandedXData[%ld] is: %d\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 expandedExpertIdxSize = GetShapeSize(idxOutShape);
std::vector<int> expandedExpertIdxData(expandedExpertIdxSize, 0);
ret = aclrtMemcpy(expandedExpertIdxData.data(), expandedExpertIdxData.size() * sizeof(expandedExpertIdxData[0]), expandedExpertIdxOutDeviceAddr, expandedExpertIdxSize * 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 < expandedExpertIdxSize; i++) {
LOG_PRINT("expandedExpertIdxData[%ld] is: %d\n", i, expandedExpertIdxData[i]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(rowIdx);
aclDestroyTensor(expertIdx);
aclDestroyTensor(expandedXOut);
aclDestroyTensor(expandedRowIdxOut);
aclDestroyTensor(expandedExpertIdxOut);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(rowIdxDeviceAddr);
aclrtFree(expertIdxDeviceAddr);
aclrtFree(expandedXOutDeviceAddr);
aclrtFree(expandedRowIdxOutDeviceAddr);
aclrtFree(expandedExpertIdxOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}