aclnnInplaceScatterUpdate
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| √ |
Function
Updates the values in tensor data one by one with the values from tensor updates according to the specified axis and indices. This operator has custom operator semantics and has no corresponding TensorFlow or PyTorch API.
Example:
This operator has three inputs and one attribute: data, updates, indices, and axis. data is the tensor to be updated, updates is the tensor that stores the update values, indices indicates the update positions, and axis is the specified update dimension. When indices is one-dimensional, the following two scenarios exist:
Scenario 1: indices is 1-dimensional, the shape of the dimension specified by axis for update is 1, and indices specifies the offset of each batch dimension (the highest dimension) along the axis dimension.
Sample input: data:(a, b, c, d) updates:(a, b, 1, d) indices:(a,) axis = -2data[i][j][indices[i]][k] = updates[i][j][0][k] # if dim=-2 data[i][j][k][indices[i]] = updates[i][j][k][0] # if dim=-1Scenario 2: indices is 1-dimensional, the shape of the dimension specified by axis for update is greater than 1, and indices specifies the offset of each batch dimension (the highest dimension) along the axis dimension.
Sample input: data:(a, b, c, d) updates:(a, b, e, d), indices[i] + e <= c indices:(a,) axis = -2 or 2data[i][j][indices[i]+k][l] = updates[i][j][k][l] # if dim=-2 data[i][j][k][indices[i]+l] = updates[i][j][k][l] # if dim=-1
Function Prototype
Each operator is divided into a two-phase API. You must first call the "aclnnInplaceScatterUpdateGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnInplaceScatterUpdate" API to perform the computation.
aclnnStatus aclnnInplaceScatterUpdateGetWorkspaceSize(
aclTensor* data,
const aclTensor* indices,
const aclTensor* updates,
int64_t axis,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceScatterUpdate(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnInplaceScatterUpdateGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (Shape) Non-contiguous Tensor data (aclTensor*) Input/Output The tensor to be updated. The number of dimensions must be consistent with that of updates. Empty tensors are not supported. INT8, UINT8, FLOAT16, FLOAT32, INT32, BFLOAT16, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 ND 2-8 √ indices (aclTensor*) Input Specifies the update position indices. Only non-negative indices are supported, and index data must not be out of bounds. Empty tensors are not supported. INT32, INT64 ND 0-2 √ updates (aclTensor*) Input Stores the tensor of update data. The data type must be the same as that of data, and the number of dimensions of the shape must be the same as that of the data shape. Empty tensors are not supported. INT8, UINT8, FLOAT16, FLOAT32, INT32, BFLOAT16, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 ND 2-8 √ axis (int64_t) Input Dimension along which to scatter. The value range is (-data_rank, data_rank), and axis cannot be 0. - - - - workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the operator executor, which contains the operator computation process. - - - - - Atlas training products : The UINT8 and BFLOAT16 data types are not supported.Ascend 950PR/Ascend 950DT : Data types such as FLOAT8_E4M3FN, FLOAT8_E5M2, and HIFLOAT8 are only supported on this model.
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 The passed data, indices, or updates is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of data, indices, or updates is not within the supported range. The data types of data and updates are different. The numbers of dimensions of data and updates are inconsistent. The dimension of indices is not zero-dimensional, one-dimensional, or two-dimensional. When the dimension of indices is 0, the 0th axis of updates is not 1. data, indices, or updates is an empty tensor.
aclnnInplaceScatterUpdate
Parameters
Parameter Input/Output Description workspace Input Specifies the workspace memory address applied for on the Device side. workspaceSize Input Specifies the workspace size applied for on the Device side, which is obtained by the first-phase API aclnnInplaceScatterUpdateGetWorkspaceSize. executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnInplaceScatterUpdate defaults to a deterministic implementation.
The 0-axis of updates shape is consistent with the 0-axis of indices shape.
When indices is 0-dimensional, the 0-axis of updates shape is 1.
The 0-axis of the updates shape is less than or equal to the 0-axis of the data shape.
Except for the axis dimension and the 0-axis, the shapes of updates and data are identical along all other axes.
When the indices shape is two-dimensional, the 1-axis of the shape must be 2.
When the data type of indices is INT32, DtypeSize = 4; when it is INT64, DtypeSize = 8. IndicesShapeSize is the product of the indices shape. The required UB = IndicesShapeSize * DtypeSize + 224. This operation is not supported when the required UB exceeds the total UB size of the corresponding AI processor version.
When indices contain duplicates, the results at the duplicate positions are not guaranteed.
Example
The following provides example code 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_scatter_update.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 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 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. Refer to 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. Custom construction is required based on the API interface.
int64_t axis = -2;
std::vector<int64_t> selfRefShape = {1, 1, 2, 8};
std::vector<int64_t> indicesShape = {1};
std::vector<int64_t> updatesShape = {1, 1, 1, 8};
void* selfRefDeviceAddr = nullptr;
void* indicesDeviceAddr = nullptr;
void* updatesDeviceAddr = nullptr;
aclTensor* selfRef = nullptr;
aclTensor* indices = nullptr;
aclTensor* updates = nullptr;
std::vector<float> selfRefHostData = {1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2};
std::vector<int64_t> indicesHostData = {1};
std::vector<float> updatesHostData = {3, 3, 3, 3, 3, 3, 3, 3};
// Create the selfRef aclTensor.
ret = CreateAclTensor(selfRefHostData, selfRefShape, &selfRefDeviceAddr, aclDataType::ACL_FLOAT, &selfRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the indices aclTensor.
ret = CreateAclTensor(indicesHostData, indicesShape, &indicesDeviceAddr, aclDataType::ACL_INT64, &indices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the updates aclTensor.
ret = CreateAclTensor(updatesHostData, updatesShape, &updatesDeviceAddr, aclDataType::ACL_FLOAT, &updates);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnInplaceScatterUpdate.
ret = aclnnInplaceScatterUpdateGetWorkspaceSize(selfRef, indices, updates, axis, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceScatterUpdateGetWorkspaceSize 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 aclnnInplaceScatterUpdate.
ret = aclnnInplaceScatterUpdate(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceScatterUpdate failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously wait for task execution to finish.
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. Modify based on the specific API definition.
auto size = GetShapeSize(selfRefShape);
std::vector<float> 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: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(selfRef);
aclDestroyTensor(indices);
aclDestroyTensor(updates);
// 7. Release device resources.
aclrtFree(selfRefDeviceAddr);
aclrtFree(indicesDeviceAddr);
aclrtFree(updatesDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}