aclnnSplitTensor
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: Supported
- Atlas training products: Supported
Function
Splits the input self evenly along the dim axis by the splitSections size. If the dim axis cannot be evenly divided, the size of all but the last chunk equals splitSections, and the last chunk is smaller than splitSections.
Function Prototype
Each operator uses a two-phase API. First call the "aclnnSplitTensorGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process, and then call the "aclnnSplitTensor" API to perform the computation.
aclnnStatus aclnnSplitTensorGetWorkspaceSize(
const aclTensor* self,
uint64_t splitSections,
int64_t dim,
aclTensorList* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)
aclnnStatus aclnnSplitTensor(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)
aclnnSplitTensorGetWorkspaceSize
-
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self (aclTensor*) Input Indicates the input tensor to be split. - FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16 ND 1-8 √ splitSections (uint64_t) Input Indicates the size of each block after the tensor is evenly split along the dim axis. The last block can be smaller than splitSections. - UINT64 - - - dim (int64_t) Input Specifies the dimension along which input self is split. Value range: [-self.dim(), self.dim()). - INT64 - - - out (aclTensorList*) Output List of output tensors after the split. Each output must have the same dtype. Each output tensor in TensorList has the same dimensions as self. 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. - - - - - executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - - Atlas inference products, Atlas training products: The BFLOAT16 data type is 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 type of self or out is not in the supported range. The length of self is not in the supported range. The length of the tensor in out is not in the supported range. The value of dim is out of range. The shape of the dimension to be split is 0 and splitSections is not 0. The shape of the dimension to be split is not 0 and splitSections is 0.
aclnnSplitTensor
-
Parameters
Parameter Input/Output Description workspace Input Memory address of the workspace requested on the Device. workspaceSize Input Size of the workspace requested on the Device, obtained by calling the first-phase API aclnnSplitTensorGetWorkspaceSize. executor Input Executor of the op, containing the operator computation flow. stream Input Stream used for executing tasks. -
Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnSplitTensor 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_tensor.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: 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);
// 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;
}
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) device/stream initialization. See the acl API guide.
// Fill in 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 = {4, 2};
std::vector<int64_t> shape1 = {2, 2};
std::vector<int64_t> shape2 = {2, 2};
uint64_t splitSections = 2;
int64_t dim = 0;
void* selfDeviceAddr = nullptr;
void* shape1DeviceAddr = nullptr;
void* shape2DeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* shape1Addr = nullptr;
aclTensor* shape2Addr = nullptr;
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> shape1HostData = {0, 1, 4, 5};
std::vector<float> shape2HostData = {2, 3, 6, 7};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, 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, LOG_PRINT("aclCreateTensorList failed.\n"); return ACL_ERROR_INTERNAL_ERROR);
// Call the CANN operator library API. Modify it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnSplitTensor.
ret = aclnnSplitTensorGetWorkspaceSize(self, splitSections, dim, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSplitTensorGetWorkspaceSize 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) {
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 aclnnSplitTensor.
ret = aclnnSplitTensor(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnSplitTensor failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CheckResult({shape1, shape2}, {shape1DeviceAddr, shape2DeviceAddr});
// 6. Destroy aclTensor and aclScalar, which needs to be modified based on the specific API definition.
aclDestroyTensor(self);
aclDestroyTensorList(out);
// 7. Release device resources.
aclrtFree(selfDeviceAddr);
aclrtFree(shape1DeviceAddr);
aclrtFree(shape2DeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}