aclnnSplitWithSize

📄 View Source Code

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. First call "aclnnSplitWithSizeGetWorkspaceSize" to obtain the workspace size required for the computation and the executor that contains the computation flow of the operator. Then call "aclnnSplitWithSize" to execute 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 Indicates the input tensor to be split. - FLOAT, FLOAT16, DOUBLE, INT32, UINT32, INT64, UINT64, INT16, UINT16, INT8, UINT8, BOOL, COMPLEX128, COMPLEX64, BFLOAT16. ND - √
    splitSize(aclIntArray*) Input Indicates the size of each chunk to be split. The total size of all blocks must equal the shape size of self along dim. INT64 and INT32 - - -
    dim (int64_t) Input 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 size of the workspace to be allocated on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - -
    • Atlas training products: The data type BFLOAT16 is not supported. When the number of outputs is greater than 32, the data types DOUBLE, COMPLEX128, and COMPLEX64 are not supported.
    • Atlas A2 products and Atlas A3 products: When the number of outputs is greater than 32, the data types DOUBLE, COMPLEX128, and COMPLEX64 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 Values Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 Returned when self, splitSize, 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 number of tensors in out is out of the supported range.
    The value of dim is out of bounds and is not in the range [-dimNum, dimNum -1]. dimNum is the number of dimensions of self.
    The sum of elements in splitSize is not equal to the shape size of the split dimension.

aclnnSplitWithSize

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address requested on the Device side.
    workspaceSize Input Size of the workspace requested on the Device side. Obtained by the first-phase API aclnnSplitWithSizeGetWorkspaceSize.
    executor Input Executor of the op, containing the computation flow of the operator.
    stream Input Stream for task execution.
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

Constraints

  • Deterministic computation:
    • aclnnSplitWithSize defaults to a deterministic implementation.

Example

The 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, 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);

    // 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;
}

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 device/stream. For details, see the acl API reference.
    // 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 inputs and outputs. Customize the construction 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 it with 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 computed 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. Destroy aclTensor, aclIntArray, and aclTensorList. Modify this step according to the specific API definition.
    aclDestroyTensor(self);
    aclDestroyIntArray(splitSize);
    aclDestroyTensorList(out);

    // 7. Destroy device resources.
    aclrtFree(selfDeviceAddr);
    aclrtFree(shape1DeviceAddr);
    aclrtFree(shape2DeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}