aclnnScatterNdUpdate
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
Updates the values in varRef in sequence by referring to the values in the updates tensor based on the specified indices.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnScatterNdUpdateGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call the "aclnnScatterNdUpdate" API to perform the computation.
aclnnStatus aclnnScatterNdUpdateGetWorkspaceSize(
aclTensor* varRef,
const aclTensor* indices,
const aclTensor* updates,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnScatterNdUpdate(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnScatterNdUpdateGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor varRef (aclTensor*) Input/Output Target tensor to be updated. The data type must be the same as that of updates. FLOAT16, BFLOAT16, FLOAT32, INT64, BOOL, INT8, UINT8, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 ND 1-8 √ indices (aclTensor*) Input The specified index tensor. Indices in indices cannot be out of bounds. INT32, INT64 ND - √ updates (aclTensor*) Input Tensor of the data to be updated. The data type must be the same as varRef. FLOAT16, BFLOAT16, FLOAT32, INT64, BOOL, INT8, UINT8, FLOAT8_E4M3FN, FLOAT8_E5M2, HIFLOAT8 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 flow. - - - - - Atlas A3 products ,Atlas A2 products : The FLOAT8_E4M3FN, FLOAT8_E5M2, and HIFLOAT8 data types are not supported on these products.Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
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 input varRef, indices, or updates is a null pointer. ACLNN_ERR_PARAM_INVALID 161002/561103 The shape of varRef, indices, or updates does not meet requirements. The data types of varRef, indices, and updates are out of the supported range.
aclnnScatterNdUpdate
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by the first-phase API aclnnScatterNdUpdateGetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Input shape constraints:
- varRef supports up to 8 dimensions, and varRef.rank is [1,8].
- indices has at least 2 dimensions. The rank of the last dimension of indices indicates the number of index dimensions. The rank value ranges from 1 to 7 and must satisfy rank <= varRef.rank, that is, the number of index dimensions cannot exceed the number of dimensions of varRef.
- Assume the size of the last dimension of indices is a, then the shape of updates equals the shape of indices excluding the last dimension plus the shape of varRef excluding the first a dimensions. For example: if the shape of varRef is (4, 5, 6) and the shape of indices is (3, 2), then the shape of updates must be (3, 6).
Non-contiguous tensor support:
Atlas A3 products andAtlas A2 products : varRef supports the use case where the first axis is non-contiguous (that is, the stride of dimension 0 is greater than the expected value in the contiguous layout, while the other dimensions are contiguous). The operator directly performs the update based on this non-contiguous view, without first converting varRef into a contiguous tensor.
Deterministic computation:
Ascend 950PR&950DT products : defaults to a non-deterministic implementation. The deterministic computation can be enabled by usingaclrtSetSysParamOpt.
Atlas A3 products andAtlas A2 products : default 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_scatter_nd_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 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 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);
// Calculate 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;
}
int main() {
// 1. (Boilerplate) Device/stream initialization. For details, see the acl API reference.
// 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> varRefShape = {2, 3, 7};
std::vector<int64_t> indicesShape = {2, 2};
std::vector<int64_t> updatesShape = {2, 7};
void* varRefDeviceAddr = nullptr;
void* indicesDeviceAddr = nullptr;
void* updatesDeviceAddr = nullptr;
aclTensor* varRef = nullptr;
aclTensor* indices = nullptr;
aclTensor* updates = nullptr;
std::vector<float> varRefHostData = {0.3816, 0.3939, 0.8474, 0.1652, 0.6049, 0.3315, 0.4954,
0.3284, 0.7060, 0.4359, 0.6514, 0.9476, 0.4708, 0.0656,
0.9652, 0.9512, 0.6452, 0.1981, 0.4159, 0.9575, 0.1516,
0.4987, 0.9107, 0.6635, 0.4119, 0.4845, 0.5558, 0.2749,
0.6230, 0.1180, 0.2400, 0.9971, 0.4093, 0.5561, 0.4023,
0.6612, 0.4109, 0.8470, 0.9733, 0.6947, 0.7980, 0.7957};
std::vector<int64_t> indicesHostData = {5, 0, 1, 5};
std::vector<float> updatesHostData = {0.7804, 0.3411, 0.6674, 0.8468, 0.6679, 0.5549, 0.9893,
0.2086, 0.2473, 0.5110, 0.4549, 0.3113, 0.8490, 0.9217};
// Create the varRef aclTensor.
ret = CreateAclTensor(varRefHostData, varRefShape, &varRefDeviceAddr, aclDataType::ACL_FLOAT, &varRef);
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, and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnScatterNdUpdate.
ret = aclnnScatterNdUpdateGetWorkspaceSize(varRef, indices, updates, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterNdUpdateGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize computed 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 aclnnScatterNdUpdate.
ret = aclnnScatterNdUpdate(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterNdUpdate 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 and copy the result from the device side to the host side. Modify it according to the specific API definition.
auto size = GetShapeSize(varRefShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), varRefDeviceAddr,
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 it according to the specific API definition.
aclDestroyTensor(varRef);
aclDestroyTensor(indices);
aclDestroyTensor(updates);
// 7. Release device resources. Set the parameters based on the specific API definition.
aclrtFree(varRefDeviceAddr);
aclrtFree(indicesDeviceAddr);
aclrtFree(updatesDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}