aclnnIndexCopy&aclnnInplaceIndexCopy
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
Uses the element values in the index tensor as indices to copy elements from source to the corresponding positions in selfRef or out along the specified axis dim.
Function Prototype
- aclnnIndexCopy and aclnnInplaceIndexCopy implement the same function. The differences in usage are as follows. Select the appropriate API based on your actual scenario.
- aclnnIndexCopy: Requires creating a new output tensor object to store the computation result.
- aclnnInplaceIndexCopy: No need to create a new output tensor object. The computation result is stored directly in the memory of the input tensor.
- Each operator uses a two-phase API. Call "aclnnIndexCopyGetWorkspaceSize" or "aclnnInplaceIndexCopyGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor that contains the computation process of the operator, and then call "aclnnIndexCopy" or "aclnnInplaceIndexCopy" to execute the computation.
aclnnStatus aclnnIndexCopyGetWorkspaceSize(
aclTensor* selfRef,
int64_t dim,
const aclTensor* index,
const aclTensor* source,
aclTensor* outRef,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnIndexCopy(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnStatus aclnnInplaceIndexCopyGetWorkspaceSize(
aclTensor* selfRef,
int64_t dim,
const aclTensor* index,
const aclTensor* source,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceIndexCopy(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnIndexCopyGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor selfRef Input Input aclTensor. - FLOAT, BFLOAT16, FLOAT16, INT32, UINT32, INT64, UINT64, INT16, INT8, UINT8, DOUBLE, BOOL, COMPLEX128, COMPLEX64 ND - √ dim Input Specifies the dimension axis for the index copy. In the range [-selfRef.dim(), selfRef.dim() - 1]. int64 - - - index Input Input aclTensor. - The dimension must not be greater than 1. The number of elements must be the same as the axis size of dim in tensor source.
- If index contains duplicate index values, the result is not order-preserving.
- Out-of-bounds index values in index are not supported.
INT32, INT64 ND - √ source Input Input aclTensor. The axis size of dim is the same as the number of elements in index, and other dimensions are the same as the shape of selfRef. Consistent with selfRef ND Consistent with selfRef √ outRef Output Output aclTensor. - Consistent with selfRef - Consistent with selfRef - workspaceSize Output Returns the size of the workspace to be requested on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation flow. - - - - - Atlas A2 products ,Atlas A3 products : The UINT32 and UINT64 data types are not supported.
Return Value
aclnnStatus: Return status. See also 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 selfRef, index, source, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of selfRef and index are not in the supported range. The data types of selfRef and source are different. The size of source on the dim axis is not equal to the number of elements in index. The shapes of selfRef and source have different sizes on axes other than dim. The number of dimensions of index is greater than 1. When source is a scalar, the number of elements in index is not 1. When source and selfRef are not scalars, their dimensions are different. selfRef and out have different shapes or data types. dim is out of range.
aclnnIndexCopy
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied for on the device side. workspaceSize Input Workspace size applied for on the device side. The value is obtained by the first-phase API aclnnIndexCopyGetWorkspaceSize. executor Input Op executor, containing the operator computation flow. stream Input Stream used to execute tasks. Return Value
aclnnStatus: return status code, see also aclnn return code.
aclnnInplaceIndexCopyGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor selfRef Input Input aclTensor. - FLOAT, BFLOAT16, FLOAT16, INT32, UINT32, INT64, UINT64, INT16, INT8, UINT8, DOUBLE, BOOL, COMPLEX128, COMPLEX64 ND - √ dim Input Input aclTensor. In the range of [-selfRef.dim(), selfRef.dim() - 1]. int64 - - - index Input Input aclTensor. - The dimension cannot be greater than 1, and the number of elements must be the same as the size on axis dim in tensor source.
- If index contains duplicate index values, the result order is not preserved.
INT32, INT64 ND - √ source Input Input aclTensor. The size on the dim axis is the same as the number of elements in index. Other dimensions are the same as the shape of selfRef. Consistent with selfRef. ND Consistent with selfRef. √ workspaceSize Output Size of the workspace to be requested on the Device. - - - - - executor Output Returns the op executor, which contains the operator computation process. - - - - - Atlas A2 products andAtlas A3 products : The data type does not support UINT32 or UINT64.
Return Value
aclnnStatus: Return status code, see also 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 selfRef, index, source, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of selfRef and index are not in the supported range. The data types of selfRef and source are different. The size of source on axis dim is not equal to the number of elements of index. The shapes of selfRef and source differ in size on axes other than dim. The dimension of index is greater than 1. source is a scalar, and the number of elements of index is not 1. source and selfRef are not scalars and have different numbers of dimensions. dim is out of range.
aclnnInplaceIndexCopy
Parameters
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Workspace size allocated on the Device side. Obtained by the first-phase API aclnnInplaceIndexCopyGetWorkspaceSize. executor Input Executor of the op, which contains the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: return status code, see also aclnn return code.
Constraints
Deterministic computation:
Ascend 950PR&950DT products : defaults to a non-deterministic implementation. Deterministic computation can be enabled throughaclrtSetSysParamOpt.
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_index_copy.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 code 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 memory on the device side.
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) Initialize the device/stream. For details, see the acl API manual.
// 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 failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. The construction must be customized based on the API.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> indexShape = {4};
std::vector<int64_t> sourceShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
void* selfDeviceAddr = nullptr;
void* indexDeviceAddr = nullptr;
void* sourceDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* index = nullptr;
aclTensor* source = nullptr;
aclTensor* out = nullptr;
int64_t dim = 0;
std::vector<float> selfHostData = {0, 0, 0, 0, 0, 0, 0, 0};
std::vector<int64_t> indexHostData = {3, 2, 1, 0};
std::vector<float> sourceHostData = {1, 2, 3, 4, 5, 6, 7, 8};
std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create index aclTensor.
ret = CreateAclTensor(indexHostData, indexShape, &indexDeviceAddr, aclDataType::ACL_INT64, &index);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create source aclTensor.
ret = CreateAclTensor(sourceHostData, sourceShape, &sourceDeviceAddr, aclDataType::ACL_FLOAT, &source);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnIndexCopy.
ret = aclnnIndexCopyGetWorkspaceSize(self, dim, index, source, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnIndexCopyGetWorkspaceSize 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 of aclnnIndexCopy.
ret = aclnnIndexCopy(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnIndexCopy failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Wait synchronously 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, copy the result on the device side to the host side, and modify it based on the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr, size * sizeof(float),
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("aclnnIndexCopy result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar, and modify it based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(index);
aclDestroyTensor(source);
aclDestroyTensor(out);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(indexDeviceAddr);
aclrtFree(sourceDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}