aclnnSplitWithSize
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: supported
Function
Splits the input self along the dim axis into segments of sizes specified by each element in splitSize.
Function Prototype
Each operator uses a two-phase API. Call "aclnnSplitWithSizeGetWorkspaceSize" first to obtain the workspace size required for computation and the executor containing the operator computation process. Then call "aclnnSplitWithSize" 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 Dimension (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 Size of each block to split. The total size of all blocks must be equal to the shape size of self along dim. INT64 and INT32 - - - dim (int64_t) Input Dimension along which the input tensor is split. - INT64 - - - out (aclTensorList*) Output Represents the list of output tensors after splitting. - FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16. ND - √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the computation flow of the operator. - - - - - - 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 products, Atlas A3 products: When the number of outputs exceeds 32, the data type does not support DOUBLE, COMPLEX128, and COMPLEX64.
-
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 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 length of tensors in out is not within the supported range. The value of dim is out of range, not within [-dimNum, dimNum -1]. dimNum is the number of dimensions of self. The sum of elements in splitSize does not equal the shape size of the dimension to be split.
aclnnSplitWithSize
-
Parameters
Parameter Input/Output Description workspace Input Workspace memory address requested on the device. workspaceSize Input Size of the workspace requested on the device. The value is obtained by the first-phase API aclnnSplitWithSizeGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream for executing the task. -
Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnSplitWithSize defaults 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 <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 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.
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 to the 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 aclCreateTensor 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. For details, see the acl API manual.
// Set deviceId based on the actual device in use.
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 as required by 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. Change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// 3. 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);
// 4. Allocate device memory according to 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);
}
// 5. 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, aclIntArray, and aclTensorList. Modify this step as required by the specific API definition
aclDestroyTensor(self);
aclDestroyIntArray(splitSize);
aclDestroyTensorList(out);
// 7. Release device resources
aclrtFree(selfDeviceAddr);
aclrtFree(shape1DeviceAddr);
aclrtFree(shape2DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}