aclnnSwiGluQuant
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: Adds the quant operation after the SwiGlu activation function to perform SwiGluQuant computation on input x.
Supported scope: SwiGluQuant currently supports only MoE scenarios. The inputs x and group_index of SwiGluQuant come from the outputs of the GroupedMatMul operator and MoeInitRouting. The group_index parameter enables MoE group-based dynamic quantization, static per_tensor quantization, and static per_channel quantization.
Dynamic quantization formula:
where A represents the first half of input x, B represents the second half of input x, g represents group_index, and G is the number of groups in group_index.
Static quantization formula:
where A represents the first half of input x, B represents the second half of input x, g represents group_index, and G is the number of groups in group_index.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnSwiGluQuantGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the operator computation flow, and then call the "aclnnSwiGluQuant" API to execute the computation.
aclnnStatus aclnnSwiGluQuantGetWorkspaceSize(
const aclTensor *x,
const aclTensor *smoothScalesOptional,
const aclTensor *offsetsOptional,
const aclTensor *groupIndexOptional,
bool activateLeft,
char *quantModeOptional,
const aclTensor *yOut,
const aclTensor *scaleOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnSwiGluQuant(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnSwiGluQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor x (aclTensor*) Input Data to be processed, x in the formula. - Empty tensors are not supported.
- The last dimension of x must be a multiple of 2, and the number of dimensions of x must be greater than 1. Currently, the length of the last dimension of input x cannot exceed 8192.
FLOAT16, BFLOAT16, FLOAT ND - × smoothScalesOptional (aclTensor*) Input smooth_scales for quantization, smooth_scales in the formula. - Empty tensors are not supported.
- The supported shapes are [G, N] and [G, ], where G is the number of groupIndex groups and N is half the size of the last dimension of the input x. In the current version, passing a null pointer is not supported.
FLOAT ND - × offsetsOptional (aclTensor*) Input The offsets in the formula. - Empty tensors are supported.
- This parameter does not take effect in dynamic quantization use cases. Passing a null pointer is sufficient.
- In static quantization use cases: the data type must be FLOAT.
- In per_channel mode, the supported shape is [G, N].
- In per_tensor mode, the supported shape is [G, ], and the data type and shape must be the same as those of smoothScalesOptional.
FLOAT ND - × groupIndexOptional (aclTensor*) Input group_index required for MoE grouping. It is the group_index in the formula. - Empty tensors are supported.
- The supported shape is [G, ]. The elements in group_index must be non-decreasing, and the maximum value must not exceed the product of the sizes of all dimensions of input x except the last dimension. The value of G must not exceed the product of the sizes of all dimensions of input x except the last dimension.
INT32 ND - × activateLeft (bool) Input Input for computation. true indicates activating the first half of input x, and false indicates activating the second half of input x. BOOL - - - quantModeOptional (char*) Input Computation input. "static" indicates static quantization, "dynamic" indicates dynamic quantization, and "dynamic_msd" indicates dynamic MSD quantization. Currently, only "dynamic" (dynamic quantization) and "static" (static quantization) are supported. Static quantization supports only per_tensor and per_channel quantization. Passing in a null pointer indicates dynamic quantization. STRING - - - yOut (aclTensor*) Output Output tensor. The size of the last dimension of output yOut is half that of input x, and the other dimensions are the same as those of x. INT8 ND - × scaleOut (aclTensor*) Output Output tensor. The shape of output scaleOut is the same as that of input x, except that the last dimension is removed. FLOAT ND - × workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 x or yOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the input or output is out of the supported range. The dimension of the input or output parameter is out of the supported range. quantModeOptional is out of the specified value range.
aclnnSwiGluQuant
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Size of the workspace allocated on the Device, obtained by the first-phase API aclnnSwiGluQuantGetWorkspaceSize. executor Input Op executor, containing the computation flow of the operator. stream Input Stream used to execute tasks. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnSwiGluQuant defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_swi_glu_quant.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 shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
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 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.
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 to 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);
// 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 the device/stream. For details, see the ACL API.
// 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 input and output. Customize the construction based on the API.
std::vector<int64_t> xShape = {3, 32};
std::vector<int64_t> smoothScalesShape = {2, 16};
std::vector<int64_t> groupIndexShape = {2};
std::vector<int64_t> outShape = {3, 16};
std::vector<int64_t> scaleShape = {3};
void* xDeviceAddr = nullptr;
void* smoothScalesDeviceAddr = nullptr;
void* groupIndexDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* scaleDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* smoothScales = nullptr;
aclTensor* groupIndex = nullptr;
aclTensor* out = nullptr;
aclTensor* scale = nullptr;
std::vector<float> xHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42,
43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82,
83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95};
std::vector<float> smoothScalesHostData = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1};
std::vector<int32_t> groupIndexHostData = {1, 3};
std::vector<int8_t> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
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<float> scaleHostData = {0, 0, 0};
// Create the x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the smoothScales aclTensor.
ret = CreateAclTensor(smoothScalesHostData, smoothScalesShape, &smoothScalesDeviceAddr, aclDataType::ACL_FLOAT, &smoothScales);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the groupIndex aclTensor.
ret = CreateAclTensor(groupIndexHostData, groupIndexShape, &groupIndexDeviceAddr, aclDataType::ACL_INT32, &groupIndex);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_INT8, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the scale aclTensor.
ret = CreateAclTensor(scaleHostData, scaleShape, &scaleDeviceAddr, aclDataType::ACL_FLOAT, &scale);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnSwiGluQuant.
ret = aclnnSwiGluQuantGetWorkspaceSize(x, smoothScales, nullptr, groupIndex, false, "dynamic", out, scale, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSwiGluQuantGetWorkspaceSize 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 aclnnSwiGluQuant.
ret = aclnnSwiGluQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSwiGluQuant 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. Copy the result from device memory to host memory. Modify according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<int8_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, 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 the aclTensor and aclScalar. Modify according to the specific API definition.
aclDestroyTensor(x);
aclDestroyTensor(smoothScales);
aclDestroyTensor(groupIndex);
aclDestroyTensor(out);
aclDestroyTensor(scale);
// 7. Release device resources. Modify according to the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(smoothScalesDeviceAddr);
aclrtFree(groupIndexDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(scaleDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}