aclnnRepeat
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Supported
Atlas training products : Supported
Function
Description: Replicates the input tensor along each dimension by the number of times specified in repeats.
Example: Assume the input tensor is [[a,b],[c,d],[e,f]] with shape [3,2], and repeats is (2,4). The resulting tensor has shape [6,8] with the following values:
>>> x = torch.tensor([[a,b],[c,d],[e,f]]) >>> x.repeat(2,4) tensor([[a,b,a,b,a,b,a,b], [c,d,c,d,c,d,c,d], [e,f,e,f,e,f,e,f], [a,b,a,b,a,b,a,b], [c,d,c,d,c,d,c,d], [e,f,e,f,e,f,e,f], ])When repeats is (2,4,2), meaning the number of elements in repeats is greater than the number of dimensions in the tensor, the output tensor is equivalent to the following operation: first, expand the shape of the input tensor to the same number of dimensions as repeats: [1,3,2], then expand according to the corresponding dimensions and the values of repeats, resulting in an output tensor shape of [2,12,4], as shown below:
>>> x.repeat(2,4,2) tensor([[[a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f]], [[a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f], [a,b,a,b], [c,d,c,d], [e,f,e,f]]])- The following conditions must be met during computation:
- The number of parameters in repeats must not be less than the number of dimensions of the input tensor.
- The values in repeats must be greater than or equal to 0.
- The following conditions must be met during computation:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnRepeatGetWorkspaceSize" API first to obtain the workspace size required for the computation and the executor containing the operator computation process. Then call the "aclnnRepeat" API to execute the computation.
aclnnStatus aclnnRepeatGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *repeats,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRepeat(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnRepeatGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input - - FLOAT, DOUBLE, FLOAT16, COMPLEX64, COMPLEX128, UINT8, INT8, INT16, INT32, INT64, UINT16, UINT32, UINT64, BOOL, BFLOAT16, HIFLOAT8, FLOAT8_E5M2, FLOAT8_E4M3FN ND ≤8 √ repeats (aclIntArray*) Input - Number of times to repeat the input tensor along each dimension. The number of parameters must not be greater than 8.
Currently, repeat on more than 4 dimensions at the same time is not supported. For details, see Constraints.INT64 - - - out (aclTensor*) Output - - Same as self. ND ≤8 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Atlas A2 products ,Atlas A3 products : Data types HIFLOAT8, FLOAT8_E5M2, and FLOAT8_E4M3FN are not supported
Atlas training products ,Atlas inference products : Data types BFLOAT16, HIFLOAT8, FLOAT8_E5M2, and FLOAT8_E4M3FN are not supported.
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 self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of self and out are not in the supported range. The types of self and out do not match. The number of elements in repeats is less than the dimensions of the input tensor. repeats contains values less than 0. self has more than 8 dimensions. repeats has more than 8 elements. ACLNN_ERR_INNER_NULLPTR 561103 Kernel execution fails. The intermediate result is null. Repeat is performed on more than 4 dimensions at the same time.
aclnnRepeat
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, obtained through the first-phase API aclnnRepeatGetWorkspaceSize. executor Input op executor, containing the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnRepeat defaults to a deterministic implementation.
The broadcast kernel used internally by repeat supports a maximum of 8 dimensions. Cases where the dimension exceeds 8 after broadcasting are not supported currently. Details: Limitation 1. When repeat is required on the first axis, up to 4 dimensions can be repeated at the same time (that is, the number of non-1 values in repeats does not exceed 4).
x.repeat(2, 3, 4, 5, 6) # Not supported. A validation error is reported. The first axis has a repeat value of 2, and there are 5 non-1 repeat parameters in total.
x.repeat(2, 3, 1, 5, 6) # Supported. The first axis has a repeat value of 2, and there are 4 non-1 repeat parameters in total.Limitation 2. When the first axis does not need to be repeated, repeat operations on a maximum of 3 dimensions are supported at the same time (that is, the number of non-1 parameters in repeats does not exceed 3).
x.repeat(1, 3, 4, 5, 6) # Not supported, a validation error is reported. The first axis has a repeat of 1, with four non-1 repeat parameters at the same time.
x.repeat(1, 3, 1, 5, 6) # Supported. The first axis has a repeat of 1, with three non-1 repeat parameters at the same time.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_repeat.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 shape_size = 1;
for (auto i: shape) {
shape_size *= i;
}
return shape_size;
}
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;
}
template <typename T>
void aclCreateIntArrayP(const std::vector<T>& hostData, aclIntArray** intArray) {
*intArray = aclCreateIntArray(hostData.data(), hostData.size());
}
int main() {
// 1. (Boilerplate) Device/stream initialization. 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);
// Process check as required.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output as required by the API.
std::vector<int64_t> selfShape = {3, 2};
std::vector<int64_t> outShape = {2, 12, 4};
std::vector<int64_t> repeatsArray = {2, 4, 2};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclIntArray* repeat = nullptr;
std::vector<float> selfHostData(GetShapeSize(selfShape), 1);
std::vector<float> outHostData(GetShapeSize(outShape), 1);
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the repeat aclIntArray.
aclCreateIntArrayP(repeatsArray, &repeat);
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the aclnnRepeat first-phase API.
ret = aclnnRepeatGetWorkspaceSize(self, repeat, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRepeatGetWorkspaceSize 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 aclnnRepeat second-phase API.
ret = aclnnRepeat(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRepeat failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task 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 from device memory to host memory. Modify based on the actual API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
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. Destroy the aclTensor. Modify based on the actual API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
aclDestroyIntArray(repeat);
// 7. Release device resources. Modify based on the actual API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}