aclnnGeluQuant
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Fuses GeluV2 and DynamicQuant/AscendQuantV2. Applies Gelu activation to the input data self, quantizes the activated result, and outputs the quantized result.
Formula:
First, compute gelu to obtain geluOut
- approximate = tanh
- approximate = none
Then quantize geluOut
- quant_mode = static
quant_mode = dynamic
maxValue: The maximum value of the corresponding data type.
DataType maxValue INT8 127 FLOAT8_E4M3FN 448 FLOAT8_E5M2 57344 HIFLOAT8 32768
Function Prototype
Each operator uses a two-phase API. First, call the "aclnnGeluQuantGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process. Then, call the "aclnnGeluQuant" API to execute the computation.
aclnnStatus aclnnGeluQuantGetWorkspaceSize(
const aclTensor* self,
const aclTensor* inputScaleOptional,
const aclTensor* inputOffsetOptional,
const char* approximate,
const char* quantMode,
const char* roundMode,
int64_t dstType,
const aclTensor* y,
const aclTensor* outScaleOptional,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnGeluQuant(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnGeluQuantGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor self (aclTensor*) Input Input self in the formula. - Empty tensors are not supported.
- When quantMode is "dynamic", shapes of 2-8 dimensions are supported.
- When quantMode is "static", shapes of 1-8 dimensions are supported.
FLOAT16, BFLOAT16, FLOAT ND 1-8 √ inputScaleOptional (aclTensor*) Input Operator input, that is, inputScaleOptional in the formula. - Empty tensors are not supported.
- Only 1-D shapes are supported. The size must be the size of the last dimension of self or 1.
- This is a required input when quantMode is static, and an optional input when quantMode is dynamic.
FLOAT16, BFLOAT16, FLOAT ND 1 √ inputOffsetOptional (aclTensor*) Input Optional input of the operator, which is inputOffsetOptional in the formula. - Empty tensors are not supported.
- Only 1-D shapes are supported. The dtype and shape must be the same as those of inputScaleOptional.
- When quantMode is dynamic, offset is not required if inputScaleOptional is not provided.
FLOAT16, BFLOAT16, FLOAT ND 1 √ approximate (char*) Input Value of approximate in the formula, specifying the mode of the gelu activation function. approximate supports only {"none", "tanh"}. - - - - quantMode(char*) Input quantMode in the formula, the quantization mode. quantMode supports only {"static", "dynamic"}, which correspond to static quantization and dynamic quantization respectively. - - - - roundMode(char*) Input round_mode in the formula, which specifies the quantization rounding mode. - {"rint", "round", "hybrid"} modes are supported.
- When dstType is 2/35/36, corresponding to INT8/FLOAT8_E4M3FN/FLOAT8_E5M2, only {"rint"} is supported.
- When dstType is 34, corresponding to HIFLOAT8, {"round", "hybrid"} is supported.
- - - - dstType (int64_t) Input dst_type in the formula. Specifies the data type of y after conversion. The value ranges from {2, 34, 35, 36}, corresponding to output y data types of {2: INT8, 34: HIFLOAT8, 35: FLOAT8_E5M2, 36: FLOAT8_E4M3FN}. - - - - y (aclTensor*) Output Quantized result corresponding to the output after activation, that is, y in the formula. - Empty tensors are not supported.
- The data type must match dstType.
- The shape size must be the same as that of self.
FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8, INT8 ND 1-8 √ outScaleOptional (aclTensor*) Output Quantization scale for dynamic quantization, that is, outScaleOptional in the formula. - Empty tensors are not supported.
- The shape has one fewer dimension than self.
- Each dimension size is the same as that of self except the last dimension.
- When quantMode is static, the output outScaleOptional must be a null pointer.
FLOAT ND 1-7 √ workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - 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 |
|
| ACLNN_ERR_PARAM_INVALID | 161002 |
|
| ACLNN_ERR_RUNTIME_ERROR | 361001 | The current platform is not within the range of supported platforms. |
aclnnGeluQuant
Parameters:
Parameter Input/Output Description workspace Input Pointer to the workspace memory requested on the Device. workspaceSize Input Size of the workspace requested on the Device, obtained by the first-phase API aclnnGeluQuantGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream on which tasks are executed. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Deterministic computation:
- aclnnGeluQuant defaults to a deterministic implementation.
The data type of inputScaleOptional is the same as that of self, or a higher-precision type is used when the types are different.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_gelu_quant.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define CHECK_FREE_RET(cond, return_expr) \
do { \
if (!(cond)) { \
Finalize(deviceId, stream); \
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 code, 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-side 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-side data to device-side 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 the aclCreateTensor API 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;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
int aclnnGeluQuantTest(int32_t deviceId, 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 according to the API.
std::vector<int64_t> xShape = {4, 2};
std::vector<int64_t> inputScaleShape = {1};
std::vector<int64_t> inputOffsetShape = {1};
std::vector<int64_t> yOutShape = {4, 2};
std::vector<int64_t> emptyShape = {4, 0};
void* xDeviceAddr = nullptr;
void* inputScaleDeviceAddr = nullptr;
void* inputOffsetDeviceAddr = nullptr;
void* yOutDeviceAddr = nullptr;
void* outscaleOutDeviceAddr = nullptr;
aclTensor* x = nullptr;
aclTensor* inputScale = nullptr;
aclTensor* inputOffset = nullptr;
aclTensor* yOut = nullptr;
aclTensor* outScale = nullptr;
std::vector<float> xHostData = {1.3, 2.5, 6.7, -4, -1.4, -1.6, -8, -16.9};
std::vector<float> inputScaleHostData = {1};
std::vector<uint8_t> yOutHostData = {1, 2, 7, 0, 0, 0, 0, 0};
const char* approximate = "tanh";
const char* quantMode = "static";
const char* roundMode = "rint";
int64_t dstType = 2;
// Create the x aclTensor.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT, &x);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> xTensorPtr(x, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> xDeviceAddrPtr(xDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the inputScale aclTensor.
ret = CreateAclTensor(inputScaleHostData, inputScaleShape, &inputScaleDeviceAddr, aclDataType::ACL_FLOAT, &inputScale);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> inputScaleTensorPtr(inputScale, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> inputScaleDeviceAddrPtr(inputScaleDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the yOut aclTensor.
ret = CreateAclTensor(yOutHostData, yOutShape, &yOutDeviceAddr, aclDataType::ACL_INT8, &yOut);
std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor*)> yOutTensorPtr(yOut, aclDestroyTensor);
std::unique_ptr<void, aclError (*)(void*)> yOutDeviceAddrPtr(yOutDeviceAddr, aclrtFree);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Call the CANN operator library API. Replace it with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnGeluQuant.
ret = aclnnGeluQuantGetWorkspaceSize(x, inputScale, inputOffset, approximate, quantMode, roundMode, dstType, yOut, outScale, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGeluQuantGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
std::unique_ptr<void, aclError (*)(void*)> workspaceAddrPtr(nullptr, aclrtFree);
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);
workspaceAddrPtr.reset(workspaceAddr);
}
// Call the second-phase API aclnnGeluQuant.
ret = aclnnGeluQuant(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGeluQuant failed. ERROR: %d\n", ret); return ret);
// (Boilerplate) Synchronously wait for the task to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// Obtain the output value and copy the result from the device side to the host side. Modify the code according to the specific API definition.
auto size = GetShapeSize(yOutShape);
std::vector<int8_t> yOutData(
size, 0);
ret = aclrtMemcpy(yOutData.data(), yOutData.size() * sizeof(yOutData[0]), yOutDeviceAddr,
size * sizeof(yOutData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy yOut from device to host failed. ERROR: %d\n", ret);
return ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("y[%ld] is: %d\n", i, yOutData[i]);
}
return ACL_SUCCESS;
}
int main()
{
// 1. (Boilerplate) Initialize device/stream. Refer to the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnGeluQuantTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnGeluQuantTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}