aclnnSplitTensor
Applicable Products
| Product | 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 is divided into a two-phase API. The "aclnnSplitTensorGetWorkspaceSize" API must be called first to obtain the workspace size required for computation and the executor that encapsulates the operator computation flow. Then, the "aclnnSplitTensor" API is called 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
Name Input/Output Description Usage 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 1-8 โ splitSections (uint64_t) Input Size of the block after even splitting along the dim axis. The last block can be smaller than splitSections. - UINT64 - - - dim (int64_t) Input Dimension of the input self to be split. The value range is [โself.dim(), self.dim()). - INT64 - - - out (aclTensorList*) Output List of output tensors after splitting. The dtype of each output must be the same. The number of dimensions of each output tensor in `TensorList` is the same as that of `self`. FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16 ND - โ workspaceSize (uint64_t*) Output Size of the workspace to be allocated on the device. - - - - - executor (aclOpExecutor**) Output Operator executor, containing the operator computation process. - - - - - Atlas inference products andAtlas training products : The data type cannot be BFLOAT16.
Returns
aclnnStatus: 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 Indicates that self or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of self and out are not within the supported range. The length of self is not within the supported range. The length of a tensor in out is not within the supported range. The value of dim is out of bounds. The shape of the dimension being split is 0 and splitSections is not 0. The shape of the split dimension is not 0 and splitSections is 0.
aclnnSplitTensor
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained from the first-phase API aclnnSplitTensorGetWorkspaceSize. executor Input Operator executor that contains the operator computation process. stream Input Specifies the stream that executes the task. Return Value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnSplitTensor 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_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 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 apply for 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 to the device.
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 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. Refer to the ACL API manual.
// Fill in the deviceId based on your 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 inputs and outputs. 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, return ret);
// Call the CANN operator library API. Replace with the actual 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);
// 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 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. Release aclTensor and aclScalar. Modify based on the specific API definition.
aclDestroyTensor(self);
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;
}