aclnnConvDepthwise2d

📄 View Source Code

Applicable Products

ProductSupported
Ascend 950PR/Ascend 950DT√
Atlas A3 training products/Atlas A3 inference products√
Atlas A2 training products/Atlas A2 inference products√
Atlas 200I/500 A2 inference products×
Atlas inference products×
Atlas training products×

Function

  • Description: DepthwiseConv2D is a two-dimensional depthwise convolution operation. In this operation, each input channel is convolved with an independent convolution kernel (called a depthwise convolution kernel).

  • Formula:

    Assume that the shape of the input self is (N,Cin,H,W)(N, C_{\text{in}}, H, W), and the shape of the output out is (N,Cout,Hout,Wout)(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}}). Then the output of each convolution kernel is expressed as:

    out(Ni,Coutj)=bias(Coutj)+weight(Coutj,Cinj)⋆self(Ni,Cinj)\text{out}(N_i, C_{\text{out}_j}) = \text{bias}(C_{\text{out}_j}) + \text{weight}(C_{\text{out}_j}, C_{\text{in}_j}) \star \text{self}(N_i, C_{\text{in}_j})

    Where ⋆\star represents the convolution operation, NN represents the batch size, CC represents the number of channels, and WW and HH represent the width and height, respectively.

Function Prototype

Each operator is divided into a two-phase API. You must first call the aclnnConvDepthwise2dGetWorkspaceSize API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the aclnnConvDepthwise2d API to perform the computation.

aclnnStatus aclnnConvDepthwise2dGetWorkspaceSize(
    const aclTensor       *self,
    const aclTensor       *weight,
    const aclIntArray     *kernelSize,
    const aclTensor       *bias,
    const aclIntArray     *stride,
    const aclIntArray     *padding,
    const aclIntArray     *dilation,
    aclTensor             *out,
    int8_t                 cubeMathType,
    uint64_t              *workspaceSize,
    aclOpExecutor         **executor)
aclnnStatus aclnnConvDepthwise2d(
    void            *workspace,
    const uint64_t   workspaceSize,
    aclOpExecutor   *executor,
    aclrtStream      stream)

aclnnConvDepthwise2dGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor
    self (aclTensor*) Input In the formula, self represents the convolution input.
    • Supports empty tensors.
    • The data type of self and weight must satisfy the data type deduction rules (see Deduction Relationship).
    • The shape is (N, Cin, Hin, Win).
    • N ≥ 0, C ≥ 1, H ≥ 0, W ≥ 0.
    FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 NCHW 4 √
    weight (aclTensor*) Input Weight in the formula, representing the convolution weight.
    • Supports empty tensors.
    • The data type must satisfy the data type deduction rules with the data type of self (see Deduction Relationship).
    • The shape is (Cout, Cin/groups, KH, KW).
    • The value of the first dimension of weight must be an integer multiple of the number of channels of self, and the second dimension can only be 1.
    • All dimensions are ≥ 1, and the H and W dimensions must be smaller than the H and W dimensions of self.
    FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 NCHW 4 √
    kernelSize (aclIntArray*) Input Convolution kernel size.
    • A tuple of type (INT64, INT64).
    • The values are the H and W dimensions of weight.
    INT64 - - -
    bias (aclTensor*) Input Bias in the formula, representing the convolution bias.
    • Shape: (Cout).
    • One-dimensional, with a value equal to the first dimension of weight.
    FLOAT, FLOAT16, BFLOAT16 ND 1 √
    stride (aclIntArray*) Input Convolution stride.
    • The array length must equal self dimension minus 2.
    • strideH and strideW ∈ [1, 63].
    INT32 - - -
    padding (aclIntArray*) Input Padding for self.
    • The array length must equal self dimension minus 2.
    • paddingH, paddingW ∈ [0, 255].
    INT32 - - -
    dilation (aclIntArray*) Input Spacing between elements in the convolution kernel.
    • The array length must equal self dimension minus 2.
    • dilationH and dilationW ∈ [1, 255].
    INT32 - - -
    out (aclTensor*) Output In the formula, out represents the convolution output.
    • Supports empty tensors.
    • Shape: (N, Cout, Hout, Wout).
    • The number of channels equals the first dimension of weight, H ≥ 0, W ≥ 0, and other dimensions ≥ 1.
    FLOAT, FLOAT16, BFLOAT16, HIFLOAT8 NCHW 4 √
    cubeMathType (int8_t) Input Determines which computation logic the Cube unit should use for calculation.
    • 0 (KEEP_DTYPE): Keeps the input data type for computation.
    • 1 (ALLOW_FP32_DOWN_PRECISION): Allows FLOAT down-precision computation to improve performance.
    • 2 (USE_FP16): Uses FLOAT16 precision for computation.
    • 3 (USE_HF32): Uses HFLOAT32 (mixed precision) for computation.
    INT8 - - -
    workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - -
  • Return Value

    aclnnStatus: Return status code. For details, see aclnn Return Code.

    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 pointer-type input parameter passed in is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of self, weight, bias, and out are not within the supported range.
    The data types of self, weight, and out are inconsistent.
    The input shapes of stride, padding, and dilation are incorrect.
    The number of channels of weight and self does not meet the requirement.
    The shape of out does not match the infer_shape result.
    self, weight, bias, and out are unsupported empty tensor inputs or outputs.
    ACLNN_ERR_INNER_NULLPTR 561103 API internal validation error, typically caused by input data or attribute specifications falling outside the supported range.
    ACLNN_ERR_RUNTIME_ERROR 361001 An exception occurs when the API calls the npu runtime API, for example, soc_version is not supported.

aclnnConvDepthwise2d

  • Parameters

    Parameter Input/Output Description
    workspace Input. Address of the workspace memory applied for on the Device side.
    workspaceSize Input. Size of the workspace applied for on the Device side, obtained through the first-phase API aclnnConvDepthwise2dGetWorkspaceSize.
    executor Input Operator executor that contains the operator computation process.
    stream Input Specifies the stream for task execution.
  • Return Value

    aclnnStatus: Return status code. For details, see aclnn Return Code.

Constraints

  • Deterministic computation

    • aclnnConvDepthwise2d defaults to a deterministic implementation.
Constraint Type Atlas A2 training products/Atlas A2 inference products, Atlas A3 training products/Atlas A3 inference products Ascend 950PR/Ascend 950DT
self, weight
  • The data type of self and weight does not support HIFLOAT8.
  • The number of channels of self must be less than or equal to 65535.
-
bias The bias data type does not support HIFLOAT8 or FLOAT8_E4M3FN. The data type is the same as that of input and weight. When the self data type is HIFLOAT8, the bias data type is eventually converted to FLOAT for computation.
cubeMathType
  • When set to 1 (ALLOW_FP32_DOWN_PRECISION), if the input is FLOAT, conversion to HFLOAT32 for computation is allowed.
  • When set to 2 (USE_FP16), if the input is BFLOAT16, this option is not supported.
  • When set to 3 (USE_HF32), if the input is FLOAT, it is converted to HFLOAT32 for computation.
  • When set to 1 (ALLOW_FP32_DOWN_PRECISION), if the input is FLOAT, conversion to HFLOAT32 for computation is allowed.
  • When set to 2 (USE_FP16), if the input is BFLOAT16, this option is not supported.
  • When set to 3 (USE_HF32), if the input is FLOAT, it is converted to HFLOAT32 for computation.
kernelSize Constraint The kernelSize value is the size of the H and W dimensions of weight.
Other Constraints The size of each dimension of each tensor in self, weight, and bias must not exceed 1000000.

Example

The following provides example code for reference only. For details about compilation and running, 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 apply for device 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);

  // Calculate 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 aclCreateTensor 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 aclnnConvDepthwise2dTest(int32_t deviceId, aclrtStream& stream)
{
  auto ret = Init(deviceId, &stream);
  // Handle the check based on your requirements.
  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 interface.
  std::vector<int64_t> shapeSelf = {2, 2, 2, 2};
  std::vector<int64_t> shapeWeight = {2, 1, 1, 1};
  std::vector<int64_t> shapeBias = {2};
  std::vector<int64_t> shapeResult = {2, 2, 2, 2};

  void* deviceDataSelf = nullptr;
  void* deviceDataWeight = nullptr;
  void* deviceDataBias = nullptr;
  void* deviceDataResult = nullptr;

  aclTensor* self = nullptr;
  aclTensor* weight = nullptr;
  aclTensor* bias = nullptr;
  aclTensor* result = nullptr;
  aclIntArray* kernelSize = nullptr;
  aclIntArray* stride = nullptr;
  aclIntArray* padding = nullptr;
  aclIntArray* dilation = nullptr;

  std::vector<float> selfData(GetShapeSize(shapeSelf), 1);
  std::vector<float> weightData(GetShapeSize(shapeWeight), 1);
  std::vector<float> biasData(GetShapeSize(shapeBias), 1);
  std::vector<float> outData(GetShapeSize(shapeResult), 1);
  std::vector<int64_t> kernelSizeData = {1, 1};
  std::vector<int64_t> strideData = {1, 1};
  std::vector<int64_t> paddingData = {0, 0};
  std::vector<int64_t> dilationData = {1, 1};

  // Create the self aclTensor.
  ret = CreateAclTensor(selfData, shapeSelf, &deviceDataSelf, aclDataType::ACL_FLOAT, &self);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> selfTensorPtr(self, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> deviceDataSelfPtr(deviceDataSelf, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

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

  // Create the bias aclTensor.
  ret = CreateAclTensor(biasData, shapeBias, &deviceDataBias, aclDataType::ACL_FLOAT, &bias);
  std::unique_ptr<aclTensor, aclnnStatus (*)(const aclTensor *)> biasTensorPtr(bias, aclDestroyTensor);
  std::unique_ptr<void, aclError (*)(void *)> deviceDataBiasPtr(deviceDataBias, aclrtFree);
  CHECK_FREE_RET(ret == ACL_SUCCESS, return ret);

  // Create the out aclTensor.
  ret = CreateAclTensor(outData, 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);

  kernelSize = aclCreateIntArray(kernelSizeData.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> kernelSizePtr(kernelSize, aclDestroyIntArray);
  CHECK_FREE_RET(kernelSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  stride = aclCreateIntArray(strideData.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> stridePtr(stride, aclDestroyIntArray);
  CHECK_FREE_RET(stride != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  padding = aclCreateIntArray(paddingData.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> paddingPtr(padding, aclDestroyIntArray);
  CHECK_FREE_RET(padding != nullptr, return ACL_ERROR_INTERNAL_ERROR);
  dilation = aclCreateIntArray(dilationData.data(), 2);
  std::unique_ptr<aclIntArray, aclnnStatus (*)(const aclIntArray *)> dilationPtr(dilation, aclDestroyIntArray);
  CHECK_FREE_RET(dilation != nullptr, return ACL_ERROR_INTERNAL_ERROR);

  // 3. Call the CANN operator library API. Replace with the specific API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API of aclnnConvDepthwise2d.
  ret = aclnnConvDepthwise2dGetWorkspaceSize(self, weight, kernelSize, bias, stride, padding, dilation, result, 1,
                                             &workspaceSize, &executor);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvDepthwise2dGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Apply for 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 aclnnConvDepthwise2d.
  ret = aclnnConvDepthwise2d(workspaceAddr, workspaceSize, executor, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvDepthwise2d failed. ERROR: %d\n", ret); return ret);

  // 4. (Boilerplate) Synchronize and wait for 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 the device memory to the host memory. Modify based on the specific API definition.
  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/stream. Refer to the acl API manual.
  // Enter the deviceId based on your actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = aclnnConvDepthwise2dTest(deviceId, stream);
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnConvDepthwise2dTest failed. ERROR: %d\n", ret); return ret);

  Finalize(deviceId, stream);
  return 0;
}