aclnnScatterList
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: Writes values in updates as a whole segment to the corresponding tensor in the var tensor list, starting at the position given by indice along the dimension specified by axis.
Formula:
Let B be the number of tensors in the varRef list,
ithe list index (0 <= i < B),startthe start position for writing to thei-th tensor, andlenthe write length. Then:In the formula, the ellipsis denotes the remaining dimensions other than the axis dimension, and writes are performed along the dimension specified by axis.
startandlenare given by indice:- When indice is 1-dimensional,
start = indice[i], andlenis the size of updates along the axis dimension (tensors have the same write length). - When indice is 2-dimensional,
start = indice[i][0], andlen = indice[i][1](tensors can have different write lengths).
The optional input maskOptional gates whether to perform the write for each list element. reduce currently supports only "update", that is, direct overwrite.
- When indice is 1-dimensional,
Function Prototype
Each operator uses a two-phase API. Call "aclnnScatterListGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor that contains the operator computation flow. Then call "aclnnScatterList" to execute the computation.
aclnnStatus aclnnScatterListGetWorkspaceSize(
aclTensorList* varRef,
const aclTensor* indice,
const aclTensor* updates,
const aclTensor* maskOptional,
char* reduceOptional,
int64_t axis,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnScatterList(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnScatterListGetWorkspaceSize
Parameters:
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 The input tensor or tensorList is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the tensor in varRef is not supported. The data type of indice is not supported. reduceOptional is not supported. The data type of the tensor in varRef is inconsistent with that of updates.
aclnnScatterList
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace requested by the Device. workspaceSize Input Size of the workspace requested by the Device, obtained by the first-phase API aclnnScatterListGetWorkspaceSize. executor Input Op executor, contains the operator computation flow. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Deterministic computation:
- aclnnScatterList defaults to a non-deterministic implementation. You can enable deterministic computation through aclrtCtxSetSysParamOpt.
Ascend 950PR&950DT products : The shapes of all inputs must satisfy the following relationships. Otherwise, the first-phase API returns 561002. Let B be the number of tensors in the varRef list.- varRef: Each tensor in the list must have the same shape, and the number of dimensions of each tensor must be greater than or equal to 1.
- updates: The number of dimensions equals that of a single tensor in varRef plus 1; the size of the first dimension equals B; the size of the axis dimension is not greater than the corresponding dimension of varRef; the remaining dimensions are the same as those of varRef.
- indice: The shape is 1D or 2D. For a 1D shape of (B,), only the write start position is given; for a 2D shape of (B, 2), both the write start position and length are given.
- maskOptional: The shape is 1D, and the size of the first dimension equals B.
- axis: After normalization (negative values are converted based on the number of dimensions of updates), the value must fall in the open interval (0, number of dimensions of updates), that is, it cannot point to dimension 0 or be out of bounds. The default value -2 requires that updates have at least 3 dimensions.
indice semantics and value range (values are guaranteed by the caller and not validated by the operator): indice specifies the write start position, not the scatter target index. The write covers a contiguous range starting from this position, and the entire range must stay within the axis dimension.
- 1D:
indice[i]is the write start position of the i-th tensor, and the write length is the size of updates along axis. - 2D:
indice[i][0]is the start position, andindice[i][1]is the write length, which should not exceed the size of updates along axis.
- 1D:
maskOptional value range (guaranteed by the caller and not validated by the operator): only 0 and 1 are supported. 0 indicates that no write is performed on the list element, 1 indicates that the write is performed, and the behavior for other values is undefined.
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_list.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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<float> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, 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);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate for 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);
// 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;
}
int main() {
// 1. (Boilerplate) Device/stream initialization. For details, see the acl API documentation.
// 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 inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> varShape = {5, 3, 4};
std::vector<int64_t> indicesShape = {1, 2};
std::vector<int64_t> updatesShape = {1, 5, 3, 4};
void* varDeviceAddr = nullptr;
void* indicesDeviceAddr = nullptr;
void* updatesDeviceAddr = nullptr;
aclTensor* varTensor = nullptr;
aclTensor* indices = nullptr;
aclTensor* updates = nullptr;
std::vector<float> varHostData(5 * 3 * 4, 0);
std::vector<int64_t> indicesHostData = {0, 1};
std::vector<float> updatesHostData(1 * 5 * 3 * 4, 10);
ret = CreateAclTensor(varHostData, varShape, &varDeviceAddr, aclDataType::ACL_FLOAT, &varTensor);
CHECK_RET(ret == ACL_SUCCESS, return ret);
aclTensor* varArray[] = {varTensor};
auto varTensorList = aclCreateTensorList(varArray, 1);
ret = CreateAclTensor(indicesHostData, indicesShape, &indicesDeviceAddr, aclDataType::ACL_INT64, &indices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(updatesHostData, updatesShape, &updatesDeviceAddr, aclDataType::ACL_FLOAT, &updates);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the aclnnScatterList first-phase API.
ret = aclnnScatterListGetWorkspaceSize(varTensorList, indices, updates, nullptr, "update", -2, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterListGetWorkspaceSize 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 aclnnScatterList.
ret = aclnnScatterList(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnScatterList 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 on the device side to the host side.
PrintOutResult(varShape, &varDeviceAddr);
// 6. Release the aclTensor.
aclDestroyTensorList(varTensorList);
aclDestroyTensor(indices);
aclDestroyTensor(updates);
// 7. Release the device resources.
aclrtFree(varDeviceAddr);
aclrtFree(indicesDeviceAddr);
aclrtFree(updatesDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}