aclnnInplaceQuantScatter
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| × |
Function
First, quantize updates along the quantAxis. quantScales performs scaling on updates, and quantZeroPoints performs offset. Then, update the values at corresponding positions in selfRef one by one along the specified axis based on the index tensor indices, using the quantized values from updates.
Function Prototype
Each operator is divided into a two-phase API. The "aclnnInplaceQuantScatterGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnInplaceQuantScatter" API is called to perform the computation.
aclnnStatus aclnnInplaceQuantScatterGetWorkspaceSize(
aclTensor *selfRef,
const aclTensor *indices,
const aclTensor *updates,
const aclTensor *quantScales,
const aclTensor *quantZeroPoints,
int64_t axis,
int64_t quantAxis,
int64_t reduction,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnInplaceQuantScatter(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnInplaceQuantScatterGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor selfRef Input|Output Source data tensor. - INT8 ND 3 √ indices Input Index tensor. The value range of indices is [0, selfRef.shape(axis) - updates.shape(axis)). INT32, INT64 ND 1 √ updates Input Update data tensor. The number of dimensions of updates must be the same as that of selfRef. The size of its first dimension must be equal to the size of the first dimension of indices and not greater than the size of the first dimension of selfRef. The size of its axis dimension must not be greater than the size of the axis dimension of selfRef. The sizes of the remaining dimensions must be equal to the sizes of the corresponding dimensions of selfRef. BFLOAT16, FLOAT16 ND Same as selfRef. √ quantScales Input Quantization scale tensor. The number of elements must be equal to the axis size of updates on the quantAxis axis. BFLOAT16, FLOAT32 ND 3 √ quantZeroPoints Optional input Quantization offset tensor. The number of elements must equal the size of updates along the quantAxis axis. BFLOAT16, INT32 ND 3 √ axis Input Axis on updates used for updating. Only -2 is supported. - - - - quantAxis Input Axis on updates for quantization. The value can be -1 or len(updates.shape) - 1. - - - - reduction Input Specifies the data operation mode. The value can only be 1 (update). - - - - workspaceSize output Returns the workspace size to be applied for on the Device side. - - - - - executor Output Returns the operator executor, which contains the operator computation flow. - - - - - Atlas inference products :- BFLOAT16 data type is not supported.
Atlas inference products ,Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products :- The last dimension of selfRef and updates must be 32B-aligned.
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 Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 Returns 161001 if the passed parameter is a required input, output, or required attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type combination of selfRef, indices, updates, quantScales, and quantZeroPoints is not within the supported range. For specific combinations, see Constraints. The number of dimensions of selfRef and updates is inconsistent.
aclnnInplaceQuantScatter
Parameters
Parameter Input/Output Description workspace Input Memory address of the workspace applied for on the Device side. workspaceSize Input Size of the workspace applied for on the Device side, obtained through the first-phase API aclnnInplaceQuantScatterGetWorkspaceSize. executor Input Operator executor that contains the operator computation process. stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnInplaceQuantScatter defaults to a deterministic implementation.
The indices must be one-dimensional. Out-of-bounds indexing is not supported and is not validated. The selfRef data segments mapped by indices must not overlap; otherwise, multiple executions may produce different results due to multi-core concurrency.
The supported data type combinations for selfRef, indices, updates, quantScales, and quantZeroPoints include:
Atlas inference products :selfRef indices updates quantScales quantZeroPoints INT8 INT32 FLOAT16 FLOAT32 INT32 INT8 INT64 FLOAT16 FLOAT32 INT32 Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products ,Ascend 950PR/Ascend 950DT :selfRef indices updates quantScales quantZeroPoints INT8 INT32 BFLOAT16 BFLOAT16 BFLOAT16 INT8 INT64 BFLOAT16 BFLOAT16 BFLOAT16 INT8 INT32 FLOAT16 FLOAT32 INT32 INT8 INT64 FLOAT16 FLOAT32 INT32
Example
For reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_quant_scatter.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 apply for 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);
// 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];
}
// 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;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. See the ACL API manual.
// Fill in the deviceId based on your 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> selfRefShape = {1, 1, 32};
std::vector<int64_t> indicesShape = {1};
std::vector<int64_t> updatesShape = {1, 1, 32};
std::vector<int64_t> quantScalesShape = {1, 1, 32};
std::vector<int64_t> quantZeroPointsShape = {1, 1, 32};
void* selfRefDeviceAddr = nullptr;
void* indicesDeviceAddr = nullptr;
void* updatesDeviceAddr = nullptr;
void* quantScalesDeviceAddr = nullptr;
void* quantZeroPointsDeviceAddr = nullptr;
aclTensor* selfRef = nullptr;
aclTensor* indices = nullptr;
aclTensor* updates = nullptr;
aclTensor* quantScales = nullptr;
aclTensor* quantZeroPoints = nullptr;
std::vector<int8_t> selfRefHostData{32, 0};
std::vector<int32_t> indicesHostData{0};
std::vector<float> updatesHostData{32, 1.0};
std::vector<float> quantScalesHostData{32, 0.5};
std::vector<float> quantZeroPointsHostData{32, 0.5};
int64_t axis = -2;
int64_t quantAxis = -1;
int64_t reduction = 1;
// Create the selfRef aclTensor.
ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_INT8, &selfRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the indices aclTensor.
ret = CreateAclTensor(indicesHostData, indicesShape, &indicesDeviceAddr, aclDataType::ACL_INT32, &indices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the updates aclTensor.
ret = CreateAclTensor(updatesHostData, updatesShape, &updatesDeviceAddr, aclDataType::ACL_FLOAT16, &updates);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the quantScales aclTensor.
ret = CreateAclTensor(quantScalesHostData, quantScalesShape, &quantScalesDeviceAddr, aclDataType::ACL_FLOAT,
&quantScales);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the quantZeroPoints aclTensor.
ret = CreateAclTensor(quantZeroPointsHostData, quantZeroPointsShape, &quantZeroPointsDeviceAddr,
aclDataType::ACL_INT32, &quantZeroPoints);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Modify it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnInplaceQuantScatter.
ret = aclnnInplaceQuantScatterGetWorkspaceSize(selfRef, indices, updates, quantScales, quantZeroPoints, axis,
quantAxis, reduction, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnInplaceQuantScatterGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for 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 aclnnInplaceQuantScatter.
ret = aclnnInplaceQuantScatter(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceQuantScatter failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously 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 based on the specific API definition.
auto size = GetShapeSize(selfRefShape);
std::vector<int8_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), selfRefDeviceAddr,
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 and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(selfRef);
aclDestroyTensor(indices);
aclDestroyTensor(updates);
aclDestroyTensor(quantScales);
aclDestroyTensor(quantZeroPoints);
// 7. Release device resources.
aclrtFree(selfRefDeviceAddr);
aclrtFree(indicesDeviceAddr);
aclrtFree(updatesDeviceAddr);
aclrtFree(quantScalesDeviceAddr);
aclrtFree(quantZeroPointsDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}