aclnnSplitWithSize
Applicable Products
| Product | Supported |
|---|---|
| โ | |
| โ | |
| โ | |
| ร | |
| ร | |
| โ |
Function
Splits the input self along the dim axis into sizes specified by each element in splitSize.
Function Prototype
Each operator is divided into a two-phase API. The "aclnnSplitWithSizeGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnSplitWithSize" API is called to perform the computation.
aclnnStatus aclnnSplitWithSizeGetWorkspaceSize(
const aclTensor *self,
const aclIntArray *splitSize,
int64_t dim,
aclTensorList *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnSplitWithSize(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnSplitWithSizeGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Shape Non-Contiguous Tensor self (aclTensor*) Input Input tensor to be split. - FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16. ND - โ splitSize (aclIntArray*) Input Specifies the size of each split block. The sum of all split sizes must equal the shape size of self along the dim dimension. INT64 and INT32 - - - dim (int64_t) Input Specifies the dimension along which the input tensor is split. - INT64 - - - out (aclTensorList*) Output List of output tensors after the split operation. - FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16. ND - โ workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the operator executor, which contains the operator computation process. - - - - - Atlas training products : The data type does not support BFLOAT16. When the number of outputs exceeds 32, DOUBLE, COMPLEX128, and COMPLEX64 are not supported.Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products : When the number of outputs exceeds 32, the data type does not support DOUBLE, COMPLEX128, and COMPLEX64.
Return Value
aclnnStatus: return 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 self, splitSize, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self or out is not within the supported range. The length of self is not within the supported range. The tensor length in out is not within the supported range. The value of dim is out of range, not within [-dimNum, dimNum - 1], where dimNum is the dimension size of self. The sum of elements in splitSize does not equal the size of the split dimension.
aclnnSplitWithSize
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 from the first-phase API aclnnSplitWithSizeGetWorkspaceSize. executor Input Operator executor, which contains the operator computation flow. stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnSplitWithSize defaults to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <chrono>
#include <algorithm>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_split_with_size.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-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 data from the host side to the 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 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 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;
}
void CheckResult(const std::vector<std::vector<int64_t>> &shapeList, const std::vector<void *> addrList) {
for (size_t i = 0; i < shapeList.size(); i++) {
auto size = GetShapeSize(shapeList[i]);
std::vector<float> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), addrList[i],
size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return);
for (int64_t j = 0; j < size; j++) {
LOG_PRINT("result[%ld] is: %f\n", j, resultData[j]);
}
}
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. 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 acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Custom construction is required based on the API.
std::vector<int64_t> selfShape = {5, 2};
std::vector<int64_t> shape1 = {1, 2};
std::vector<int64_t> shape2 = {4, 2};
int64_t splitValue[] = {1, 4};
int64_t dim = 0;
void* selfDeviceAddr = nullptr;
void* shape1DeviceAddr = nullptr;
void* shape2DeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* shape1Addr = nullptr;
aclTensor* shape2Addr = nullptr;
aclIntArray *splitSize = nullptr;
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
std::vector<float> shape1HostData = {0, 5};
std::vector<float> shape2HostData = {1, 2, 3, 4, 6, 7, 8, 9};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
splitSize = aclCreateIntArray(splitValue, 2);
CHECK_RET(splitSize != nullptr, return ret);
ret = CreateAclTensor(shape1HostData, shape1, &shape1DeviceAddr, aclDataType::ACL_FLOAT, &shape1Addr);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(shape2HostData, shape2, &shape2DeviceAddr, aclDataType::ACL_FLOAT, &shape2Addr);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensorList.
std::vector<aclTensor*> tmp = {shape1Addr, shape2Addr};
aclTensorList* out = aclCreateTensorList(tmp.data(), tmp.size());
CHECK_RET(out != nullptr, return ret);
// Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
// Call the first-phase API of aclnnSplitWithSize.
ret = aclnnSplitWithSizeGetWorkspaceSize(self, splitSize, dim, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSplitWithSizeGetWorkspaceSize 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) {
auto 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 aclnnSplitWithSize.
ret = aclnnSplitWithSize(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSplitWithSize failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CheckResult({shape1, shape2}, {shape1DeviceAddr, shape2DeviceAddr});
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(self);
aclDestroyIntArray(splitSize);
aclDestroyTensorList(out);
aclDestroyTensor(shape1Addr);
aclDestroyTensor(shape2Addr);
// 7. Release device resources.
aclrtFree(selfDeviceAddr);
aclrtFree(shape1DeviceAddr);
aclrtFree(shape2DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}