aclnnConvolution

📄 View Source Code

Applicable Products

  • Ascend 950PR&950DT products: Supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Supported
  • Atlas inference products: Supported
  • Atlas training products: Not supported

Function

  • Description: Implements convolution, supporting 1D convolution, 2D convolution, and 3D convolution, as well as transposed convolution, dilated convolution, and grouped convolution. When the input parameter is transposed = True, transposed convolution or fractionally-strided convolution is used. It can be viewed as the gradient of a normal convolution or as its reverse operation, that is, recovering the input shape from the output shape of the convolution while keeping the connectivity pattern compatible with the convolution. Its parameters are similar to those of a normal convolution, including the number of input channels, the number of output channels, kernel size, stride, padding, output padding, groups, bias, dilation, etc.

  • Formula:

    Assume that the shape of the input is (N,Cin,D,H,W)(N, C_{\text{in}}, D, H, W), the shape of (weight) is (Cout,Cin,Kd,Kh,Kw)(C_{\text{out}}, C_{\text{in}}, K_d, K_h, K_w), and the shape of the output is (N,Cout,Dout,Hout,Wout)(N, C_{\text{out}}, D_{\text{out}}, H_{\text{out}}, W_{\text{out}}), where NN denotes the batch size, CC is the number of channels, DD, HH, and WW are the depth, height, and width of the sample, respectively, and KdK_d, KhK_h, and KwK_w are the depth, height, and width of the convolution kernel, respectively. Then the output is represented as:

    output(Ni,Coutj,Dout,Hout,Wout)=∑k=0Cin−1weight(Coutj,k)⋆input(Ni,k)+bias(Coutj)\text{output}(N_i, C_{\text{out}_j}, D_{\text{out}}, H_{\text{out}}, W_{\text{out}}) = \sum_{k = 0}^{C_{\text{in}} - 1} \text{weight}(C_{\text{out}_j}, k) \star \text{input}(N_i, k) + \text{bias}(C_{\text{out}_j})

    where ⋆\star denotes the convolution operation, and the convolution type (dilated convolution, grouped convolution) depends on the dimensions of the convolution input. NN denotes the batch size, CC denotes the number of channels, and DD, HH, and WW denote the depth, height, and width, respectively. The formula for calculating the corresponding output dimensions is as follows:

    • When the parameter transposed = False:

      Dout=[(D+2×padding[0]−dilation[0]×(Kd−1)−1)/stride[0]]+1Hout=[(H+2×padding[1]−dilation[1]×(Kh−1)−1)/stride[1]]+1Wout=[(W+2×padding[2]−dilation[2]×(Kw−1)−1)/stride[2]]+1D_{\text{out}}=[(D + 2 \times padding[0] - dilation[0] \times (K_d - 1) - 1 ) / stride[0]] + 1 \\ H_{\text{out}}=[(H + 2 \times padding[1] - dilation[1] \times (K_h - 1) - 1 ) / stride[1]] + 1 \\ W_{\text{out}}=[(W + 2 \times padding[2] - dilation[2] \times (K_w - 1) - 1 ) / stride[2]] + 1
    • When the parameter transposed = True:

      Dout=(D−1)×stride[0]−2×padding[0]+dilation[0]×(Kd−1)+outputPadding[0]+1Hout=(H−1)×stride[1]−2×padding[1]+dilation[1]×(Kh−1)+outputPadding[1]+1Wout=(W−1)×stride[2]−2×padding[2]+dilation[2]×(Kw−1)+outputPadding[2]+1D_{\text{out}}=(D - 1) \times \text{stride}[0] - 2 \times \text{padding}[0] + \text{dilation}[0] \times (K_d - 1) + \text {outputPadding}[0] + 1 \\ H_{\text{out}}=(H - 1) \times \text{stride}[1] - 2 \times \text{padding}[1] + \text{dilation}[1] \times (K_h - 1) + \text {outputPadding}[1] + 1 \\ W_{\text{out}}=(W - 1) \times \text{stride}[2] - 2 \times \text{padding}[2] + \text{dilation}[2] \times (K_w - 1) + \text {outputPadding}[2] + 1

Function Prototype

Each operator uses a two-phase API. Call the aclnnConvolutionGetWorkspaceSize API first to obtain the workspace size required for computation and the executor containing the operator computation process, and then call the aclnnConvolution API to perform the computation.

aclnnStatus aclnnConvolutionGetWorkspaceSize(
    const aclTensor       *input,
    const aclTensor       *weight,
    const aclTensor       *bias,
    const aclIntArray     *stride,
    const aclIntArray     *padding,
    const aclIntArray     *dilation,
    bool                   transposed,
    const aclIntArray     *outputPadding,
    const int64_t          groups,
    aclTensor             *output,
    int8_t                 cubeMathType,
    uint64_t              *workspaceSize,
    aclOpExecutor         **executor)
aclnnStatus aclnnConvolution(
    void            *workspace,
    const uint64_t   workspaceSize,
    aclOpExecutor   *executor,
    aclrtStream      stream)

aclnnConvolutionGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    input (aclTensor*) Input input in the formula, indicating the convolution input. FLOAT, FLOAT16, BFLOAT16, HIFLOAT8, FLOAT8_E4M3FN NCL, NCHW, NCDHW 3-5 √
    weight (aclTensor*) Input weight in the formula, indicating the convolution weight. FLOAT, FLOAT16, BFLOAT16, HIFLOAT8, FLOAT8_E4M3FN NCL, NCHW, NCDHW 3-5 √
    bias (aclTensor*) Input The bias in the formula, indicating the convolution bias.
    • For use cases without bias, pass in nullptr.
    • When transposed=false, it is 1D and its value equals the first dimension of weight; when transposed=true, it is 1D and its value equals weight.shape[1] * groups.
    FLOAT, FLOAT16, BFLOAT16 ND, NCL, NCHW, NCDHW 1-5 √
    stride (aclIntArray*) Input Convolution scan stride. The array length must equal the input dimension minus 2, and the values must be greater than 0. INT64 - - -
    padding (aclIntArray*) Input Padding for input. When the dimension of input is 3 and transposed=false, the length of the padding array must be 1 or 2. When the dimension of input is 3 and transposed=true, the length of the padding array must be 1. When the dimension of input is 4, the length of the padding array must be 2 or 4. When the dimension of input is 5, the length of the padding array must be 3. The padding values must be greater than or equal to 0. INT64 - - -
    dilation (aclIntArray*) Input Spacing between elements in the convolution kernel. The array length must equal the number of input dimensions minus 2, and values must be greater than 0. INT64 - - -
    transposed (bool) Input Whether the convolution is transposed. - BOOL - - -
    outputPadding(aclIntArray*) Input Padding on all sides of the output for transposed convolution. This configuration is ignored for non-transposed convolution. The array length must equal the number of input dimensions minus 2. Each value must be greater than or equal to 0 and less than the corresponding dimension value of stride or dilation. INT64 - - -
    groups (int64_t) Input Indicates the number of blocked connections from input channels to output channels. The value must be in the range of [1,65535] and satisfy groups*C dimension of weight = C dimension of input. INT64 - - -
    output (aclTensor*) Output out in the formula, indicating the convolution output.
    • Empty tensor is supported.
    • The data type must be the same as the data type of input and weight after type promotion.
    • The number of channels equals the first dimension of weight, and other dimensions must be ≥ 0.
    FLOAT, FLOAT16, BFLOAT16, HIFLOAT8, FLOAT8_E4M3FN NCL, NCHW, NCDHW 3-5 √
    cubeMathType (int8_t) Input Determines which computation logic the Cube unit uses.
    • If the input data types have a deduction relationship, this parameter processes the data types after deduction by default.
    • The supported enumeration values are as follows:
      • 0 (KEEP_DTYPE): Computes with the input data type retained.
      • 1 (ALLOW_FP32_DOWN_PRECISION): Allows FLOAT to be computed with reduced precision to improve performance.
      • 2 (USE_FP16): Computes with FLOAT16 precision.
      • 3 (USE_HF32): Computes with HFLOAT32 (mixed precision).
    INT8 - - -
    workspaceSize (uint64_t*) Output Returns the workspace size to be allocated on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, containing the operator computation flow. - - - - -
  • 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 pointer parameter is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of input, weight, bias, and output are not within the supported range.
    The data types of input and output are inconsistent; when transposed=false, inconsistent data types between input and output are supported and do not trigger this type error.
    The input shape of stride, padding, dilation, or outputPadding is incorrect.
    The groups input is incorrect.
    The shape of output does not match the infershape result.
    The outputPadding value does not meet the requirement.
    The empty tensors passed to input, weight, bias, and output with some dimensions being zero do not meet the requirement.
    When transposed=false, the spatial size of input after padding is smaller than the spatial size of weight (dilated by dilation if dilation>1).
    The shape of bias is not 1 when transposed=true.
    stride or dilation is less than 0.
    The current processor does not support convolution.
    ACLNN_ERR_INNER_NULLPTR 561103 API internal check error, usually caused by input data or attribute specifications out of the supported range.
    ACLNN_ERR_RUNTIME_ERROR 361001 API call to NPU runtime is abnormal, such as unsupported SocVersion.

aclnnConvolution

  • Parameters

    Parameter Input/Output Description
    workspace Input Address of the workspace memory allocated on the device.
    workspaceSize Input Size of the workspace memory allocated on the device, obtained by calling the first-phase API aclnnConvolutionGetWorkspaceSize.
    executor Input Op executor that contains the operator computation flow.
    stream Input Stream for task execution.
  • Return Value

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

Constraints

  • Deterministic computation
    • aclnnConvolution defaults to a deterministic implementation.
Constraint type Atlas A2 products, Atlas A3 products Atlas 200I/500 A2 inference products Atlas inference products Ascend 950PR&950DT products
input, weight
  • The data types of input and weight do not support HIFLOAT8 or FLOAT8_E4M3FN.
  • In the conv2d and conv3d transposed=true use cases, the H and W sizes of weight must be in the range [1,255], and other dimensions must be greater than or equal to 1.
  • In the conv1d transposed=true use case, the L size of weight must be in the range [1,255], and other dimensions must be greater than or equal to 1.
  • In the conv3d forward use case, the H and W sizes of weight must be in the range [1,511].
The data types of input and weight do not support HIFLOAT8 or FLOAT8_E4M3FN.
  • The data types of input and weight do not support BFLOAT16, HIFLOAT8, or FLOAT8_E4M3FN.
  • The H and W dimensions of input must be less than or equal to 4096, and the D, H, and W dimensions of weight must be less than or equal to 255.
The data types of input and weight support FLOAT, FLOAT16, BFLOAT16, and HIFLOAT8.
  • transposed=true: The data type of input additionally supports FLOAT8_E4M3FN. When the data type of input is HIFLOAT8 or FLOAT8_E4M3FN, the data types of output and weight must be the same as that of input. The N dimension must be greater than or equal to 0, and each of the other dimensions must be greater than or equal to 1. The data type of weight additionally supports FLOAT8_E4M3FN, and all dimensions must be greater than or equal to 0 (if the D, H, or W dimension is 0, the corresponding dimension of the inferred output must also be 0).
  • transposed=false: When the data type of input is HIFLOAT8, the data type of weight must be the same as that of input. The N dimension must be greater than or equal to 0. The D, H, and W dimensions must be greater than or equal to 0 (a value of 0 is supported only when the corresponding inferred D, H, or W dimension of output is also 0). The C dimension must be greater than or equal to 0 (a value of 0 is supported only when one of the inferred N, C, D, H, or W dimensions of output is 0). The H and W sizes of weight must be in the range [1,511]. The N dimension must be greater than or equal to 0 (a value of 0 is supported only when the N dimensions of bias and output are also 0). The supported C dimension size is the same as that of the C dimension of input.
bias
  • The data type of bias does not support HIFLOAT8 or FLOAT8_E4M3FN. The data type must be the same as those of input and weight.
  • In the conv1d, conv2d, and conv3d forward use cases, bias is converted to FLOAT for computation.
  • The bias data type cannot be HIFLOAT8 or FLOAT8_E4M3FN. The data type must be the same as that of input and weight.
  • In forward scenarios of conv1d, conv2d, and conv3d, bias is converted to FLOAT for computation.
The bias data type cannot be BFLOAT16, HIFLOAT8, or FLOAT8_E4M3FN. In the Conv3D forward scenario, only FLOAT16 is supported. The bias data type cannot be HIFLOAT8 or FLOAT8_E4M3FN.
  • If transposed=true: when the data types of input and weight are HIFLOAT8 or FLOAT8_E4M3FN, bias is not supported.
  • If transposed=false: when the data types of input and weight are HIFLOAT8, bias is converted to FLOAT for computation.
stride In the conv3d transposed=true scenario, strideD must be greater than or equal to 1, and strideH and strideW must be in the range [1,63]. In the conv1d and conv2d transposed=true scenarios, each value must be greater than or equal to 1. In the conv3d forward scenario, strideH and strideW must be in the range [1,63]. In the conv3d transposed=true scenario, strideD, strideH, and strideW must be greater than or equal to 1. In the conv3d transposed=false scenario, strideH and strideW must be in the range [1,63], and strideD must be in the range [1, 1000000].
padding In the conv3d transposed=true scenario, paddingD must be greater than or equal to 0, and paddingH and paddingW must be in the range [0,255]. In the conv1d and conv2d transposed=false scenarios, each value must be in the range [0,255]. In the conv3d forward scenario, paddingH and paddingW must be in the range [0,255]. In the conv3d transposed=true scenario, paddingD, paddingH, and paddingW must be greater than or equal to 0. In the conv3d transposed=false scenario, paddingH and paddingW must be in the range [0,255], and paddingD must be in the range [0, 1000000].
dilation For the conv1d, conv2d, and conv3d transposed=true use cases, all values must be within the range [1,255]. For the conv3d forward use case, dilationH and dilationW must be within the range [1,255]. For the conv3d transposed=true use case, dilationD, dilationH, and dilationW must be greater than or equal to 1. For the conv3d transposed=false use case, dilationH and dilationW must be within the range [1,255], and dilationD must be within the range [1, 1000000].
cubeMathType
  • When the value is 0 (KEEP_DTYPE), the option is currently not supported when the input is FLOAT.
  • When the value is 1 (ALLOW_FP32_DOWN_PRECISION), inputs of type FLOAT are allowed to be converted to HFLOAT32 for computation.
  • When the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • When the value is 3 (USE_HF32), inputs of type FLOAT are converted to HFLOAT32 for computation.
  • When the value is 0 (KEEP_DTYPE), the option is currently not supported when the input is FLOAT.
  • When the value is 1 (ALLOW_FP32_DOWN_PRECISION), inputs of type FLOAT are allowed to be converted to HFLOAT32 for computation.
  • When the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • When the value is 3 (USE_HF32), inputs of type FLOAT are converted to HFLOAT32 for computation.
  • When the value is 0 (KEEP_DTYPE), the option is currently not supported when the input is FLOAT.
  • When the value is 1 (ALLOW_FP32_DOWN_PRECISION), inputs of type FLOAT are allowed to be converted to FLOAT16 for computation.
  • When the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • When the value is 3 (USE_HF32), this option is currently not supported.
  • When the value is 1 (ALLOW_FP32_DOWN_PRECISION), inputs of type FLOAT are allowed to be converted to HFLOAT32 for computation.
  • When the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • When the value is 3 (USE_HF32), inputs of type FLOAT are converted to HFLOAT32 for computation.
Other constraints For input, weight, and bias, the size of each dimension of each tensor must not exceed 1000000.
  • Currently, only 2D convolution with transposed=false is supported. 1D and 3D convolution are not supported yet.
  • For input, weight, and bias, the size of each dimension of each tensor must not exceed 1000000.
  • Supports 1D and 2D convolution. For 3D convolution, only FLOAT16 is supported in forward propagation.
  • The size of each dimension of each group of tensors in input, weight, and bias must not exceed 1000000.
When transposed is true, 1D, 2D, and 3D convolution are supported, and empty tensors are supported. In this case, the computation behavior of gradients in the padding area depends on the input shape. Depending on the operator optimization strategy, gradients in the padding area may be directly set to 0.

Example

The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.

#include <iostream>
#include <memory>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_convolution.h"

#define CHECK_RET(cond, return_expr) \
  do {                               \
    if (!(cond)) {                   \
      return_expr;                   \
    }                                \
  } while (0)

#define CHECK_FREE_RET(cond, return_expr) \
  do {                                    \
    if (!(cond)) {                        \
      Finalize(deviceId, stream);         \
      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 shape_size = 1;
  for (auto i: shape) {
    shape_size *= i;
  }
  return shape_size;
}

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

  // 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_NCHW,
                            shape.data(), shape.size(), *deviceAddr);
  return 0;
}

void Finalize(int32_t deviceId, aclrtStream& stream)
{
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
}

int aclnnConvolutionTest(int32_t deviceId, aclrtStream& stream)
{
  auto ret = Init(deviceId, &stream);
  // Process the check as needed
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct the input and output. Customize the construction based on the API
  std::vector<int64_t> shapeInput = {2, 2, 2, 2};
  std::vector<int64_t> shapeWeight = {1, 2, 1, 1};
  std::vector<int64_t> shapeResult = {2, 1, 2, 2};
  std::vector<int64_t> convStrides;
  std::vector<int64_t> convPads;
  std::vector<int64_t> convOutPads;
  std::vector<int64_t> convDilations;

  void* deviceDataA = nullptr;
  void* deviceDataB = nullptr;
  void* deviceDataResult = nullptr;

  aclTensor* input = nullptr;
  aclTensor* weight = nullptr;
  aclTensor* result = nullptr;
  std::vector<float> inputData(GetShapeSize(shapeInput), 1);
  std::vector<float> weightData(GetShapeSize(shapeWeight), 1);
  std::vector<float> outputData(GetShapeSize(shapeResult), 1);
  convStrides = {1, 1};
  convPads = {0, 0};
  convOutPads = {0, 0};
  convDilations = {1, 1};

  // Create the input aclTensor.
  ret = CreateAclTensor(inputData, shapeInput, &deviceDataA, aclDataType::ACL_FLOAT, &input);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> inputTensorPtr(input, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> deviceDataAPtr(deviceDataA, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  // Create the weight aclTensor.
  ret = CreateAclTensor(weightData, shapeWeight, &deviceDataB, aclDataType::ACL_FLOAT, &weight);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> weightTensorPtr(weight, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> deviceDataBPtr(deviceDataB, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  // Create the out aclTensor.
  ret = CreateAclTensor(outputData, shapeResult, &deviceDataResult, aclDataType::ACL_FLOAT, &result);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> outputTensorPtr(result, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> deviceDataResultPtr(deviceDataResult, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  aclIntArray *strides = aclCreateIntArray(convStrides.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> stridesPtr(strides, aclDestroyIntArray);
  CHECK_FREE_RET(strides != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  aclIntArray *pads = aclCreateIntArray(convPads.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> padsPtr(pads, aclDestroyIntArray);
  CHECK_FREE_RET(pads != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  aclIntArray *outPads = aclCreateIntArray(convOutPads.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> outPadsPtr(outPads, aclDestroyIntArray);
  CHECK_FREE_RET(outPads != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  aclIntArray *dilations = aclCreateIntArray(convDilations.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> dilationsPtr(dilations, aclDestroyIntArray);
  CHECK_FREE_RET(dilations != nullptr, return ACL_ERROR_INTERNAL_ERROR);

  // 3. Call the CANN operator library API. Replace it with the actual API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnConvolution.
  ret = aclnnConvolutionGetWorkspaceSize(input, weight, nullptr, strides, pads, dilations, false, outPads, 1, result, 1,
                                         &workspaceSize, &executor);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvolutionGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the workspaceSize calculated by the first-phase API.
  void* workspaceAddr = nullptr;
  std::unique_ptr<void, aclError (*)(void *)> workspaceAddrPtr(nullptr, aclrtFree);
  if (workspaceSize > 0) {
    ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
    CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
    workspaceAddrPtr.reset(workspaceAddr);
  }
  // Call the second-phase API of aclnnConvolution.
  ret = aclnnConvolution(workspaceAddr, workspaceSize, executor, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvolution failed. ERROR: %d\n", ret); return ret);

  // 4. (boilerplate) Synchronously wait for the task execution to complete.
  ret = aclrtSynchronizeStream(stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 5. Obtain the output value and copy the result from device memory to host memory. Modify it according to the definition of the actual API.
  auto size = GetShapeSize(shapeResult);
  std::vector<float> resultData(size, 0);
  ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), deviceDataResult,
                    size * sizeof(float), ACL_MEMCPY_DEVICE_TO_HOST);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
  for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
  }

  return ACL_SUCCESS;
}

int main() {
  // 1. (boilerplate) Initialize the device and stream. For details, see the ACL API manual.
  // Fill in deviceId based on your actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = aclnnConvolutionTest(deviceId, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvolutionTest failed. ERROR: %d\n", ret); return ret);

  Finalize(deviceId, stream);
  return 0;
}