aclnnConvDepthwise2d
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 and the shape of output out is , the output of each kernel is represented as follows:
Where represents the convolution operation, represents the batch size, represents the number of channels, and and 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
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 | ||
|---|---|---|
| self, weight |
|
- |
| 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 |
|
|
| 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;
}