aclnnConvDepthwise2d
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
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 , and the shape of the output out is . Then the output of each convolution kernel is expressed as:
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. 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
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 | ||
|---|---|---|
| self, weight |
|
- |
| 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 |
|
|
| 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;
}