aclnnConvDepthwise2d

📄 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: Not supported

Function

  • Description: DepthwiseConv2D is a 2D depthwise convolution operation. In this operation, each input channel is convolved with a separate kernel (called a depthwise kernel).

  • Formula:

    Assuming the shape of input self is (N,Cin,H,W)(N, C_{\text{in}}, H, W) and the shape of output out is (N,Cout,Hout,Wout)(N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}}), the output of each kernel is represented as follows:

    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: call aclnnConvDepthwise2dGetWorkspaceSize first to obtain the workspace size required for the computation and the executor containing the operator computation process, and then call aclnnConvDepthwise2d to execute 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 Dimension (shape) Non-contiguous Tensor
    self (aclTensor*) Input self in the formula, which denotes the convolution input.
    • Supports empty tensors.
    • The data type and the data type of 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, which denotes 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 first dimension of weight must be an integral multiple of the number of channels of self, and the second dimension can only be 1.
    • All dimensions are greater than or equal to 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.
    • (INT64, INT64) tuple.
    • The values are the H and W dimensions of weight.
    INT64 - - -
    bias (aclTensor*) Input bias in the formula, denoting the convolution bias.
    • shape is (Cout).
    • One-dimensional, and the value must equal the first dimension of weight.
    FLOAT, FLOAT16, BFLOAT16 ND 1 √
    stride (aclIntArray*) Input Scan stride of the convolution.
    • The array length must equal the number of self dimensions minus 2.
    • strideH and strideW ∈[1,63].
    INT64 - - -
    padding (aclIntArray*) Input Padding of self.
    • The array length must equal the dimension of self - 2.
    • paddingH, paddingW ∈ [0,255].
    INT64 - - -
    dilation (aclIntArray*) Input Spacing between elements in the convolution kernel.
    • The array length must equal the dimension of self minus 2.
    • dilationH, dilationW∈[1,255].
    INT64 - - -
    out (aclTensor*) Output out in the formula, which denotes the convolution output.
    • Supports empty tensors.
    • Shape: (N,Cout,Hout,Wout).
    • The number of channels must equal 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 uses for computation.
    • 0 (KEEP_DTYPE): Computes with the input data type.
    • 1 (ALLOW_FP32_DOWN_PRECISION): Allows reduced-precision computation of FLOAT 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 requested by the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - -
  • Return Value

    aclnnStatus: Returns a 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 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 requirements.
    The shape of out does not match the infer_shape result.
    self, weight, bias, or out is an empty tensor input or output, which is not supported.
    ACLNN_ERR_INNER_NULLPTR 561103 Internal API verification error, usually caused by specifications of input data or attributes being out of the supported range.
    ACLNN_ERR_RUNTIME_ERROR 361001 An error occurs when the API calls the npu runtime interface, for example, soc_version is not supported.

aclnnConvDepthwise2d

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address requested by the Device side.
    workspaceSize Input Workspace size requested by the Device side, obtained by calling the first-phase API aclnnConvDepthwise2dGetWorkspaceSize.
    executor Input Op executor, 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
    • aclnnConvDepthwise2d defaults to a deterministic implementation.
Constraint Type Atlas A2 products, Atlas A3 products Ascend 950PR&950DT products
self, weight
  • The data types of self and weight do not support HIFLOAT8.
  • The number of channels of self must be less than or equal to 65535.
-
bias The data types of bias do not support HIFLOAT8 or FLOAT8_E4M3FN. The data type is the same as that of input and weight. When the data type of self is HIFLOAT8, the data type of bias is ultimately converted to FLOAT for computation.
cubeMathType
  • If the value is 1 (ALLOW_FP32_DOWN_PRECISION), conversion to HFLOAT32 for computation is allowed when the input is FLOAT.
  • If the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • If the value is 3 (USE_HF32), when the input is FLOAT, the input is converted to HFLOAT32 for computation.
  • If the value is 1 (ALLOW_FP32_DOWN_PRECISION), conversion to HFLOAT32 for computation is allowed when the input is FLOAT.
  • If the value is 2 (USE_FP16), this option is not supported when the input is BFLOAT16.
  • If the value is 3 (USE_HF32), when the input is FLOAT, the input is converted to HFLOAT32 for computation.
kernelSize constraints The kernelSize value is the size of the H and W dimensions of weight.
Other constraints The size of each dimension of each group of tensors in self, weight, and bias must not exceed 1000000.

Example

The following sample code is for reference only. For details about the compile and execution process, 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 side.
  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 side to the 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 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_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 check as needed.
  CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct inputs and outputs. Use custom construction based on the API.
  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 it with the actual 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);
  // Allocate device memory based on the workspaceSize computed 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 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, copy the result from the device memory to the host memory, and modify it according to 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. For details, see the acl API manual.
  // Set deviceId based on the 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;
}