-
Notifications
You must be signed in to change notification settings - Fork 5.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add operators support for Ascend NPU (CANN backend) #3552
Conversation
fe53efd
to
0a751c3
Compare
4176ecd
to
dc927ce
Compare
Performance Test ReasultVM from huawei cloud
|
381d853
to
b07acec
Compare
Hi @vpisarev, Could you please help me to review this PR or assign someone else to do it? This PR is mainly to enable Ascend NPU (CANN backend) to be used as an accelerated backend of OpenCV and implemented several simple arithmetic operators, these operators do seem to have a certain acceleration effect. In addition, I have two more questions and would like to get your advice:
Thanks. |
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at http://opencv.org/license.html. | ||
|
||
#ifndef OPENCV_CANN_HPP |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Identifier should have the following format: OPENCV_<module>_<header_underscrore_subpath>_HPP
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the code review, will fix them in next commit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. change all identifiers in hpp.
CV_EXPORTS_W void subtract(InputArray src1, InputArray src2, OutputArray dst, | ||
InputArray mask = noArray(), int dtype = -1, | ||
AclStream& stream = AclStream::Null()); | ||
#ifdef NEVER_DEFINED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NEVER_DEFINED
What is that?
We should not have platform-specific conditional compilation in OpenCV public headers.
Also OpenCV bindings generators can't properly handle that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DEVER_DEFINED means this part of code will never be compiled. I did this only to cheat to python bindings to generate these two interfaces. Only did this can it support Mat subtract scalar. Otherwise, only Mat is accepted.
To be hoest, it's not a very good idea, do you have any suggestions?
#ifndef OPENCV_CANN_STREAM_ACCESSOR_HPP | ||
#define OPENCV_CANN_STREAM_ACCESSOR_HPP | ||
|
||
#include <acl/acl.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
BTW, this is platform-specific include in public header.
Some assumptions should be applied on the User side (like properly configured build environment, including headers paths and defines).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did some code refactoring, <acl/acl.h> only defined in cann_call.hpp, other files can't call acl functions directly.
In this commit(opencv/opencv#22634), OpenCVFindCANN.cmake has already introduced, and cmake will set headers and libraries correctly.
void aclTwoInputs(const AclMat& src1, const AclMat& src2, AclMat& dst, const char* op, | ||
AclStream& stream = AclStream::Null()); | ||
|
||
void transNCHWToNHWC(const AclMat& src, AclMat& dst, AclStream& stream = AclStream::Null()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need a reverse operation?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, we do. Not this function is renamed to transData, it support transform all kinds of img formats.
|
||
#include "opencv2/cann.hpp" | ||
|
||
typedef std::vector<cann::AclMat> vector_AclMat; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any python test?
misc/python/test/test_cann.py
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A python interface testcase is added.
cv::cann::initAcl(); | ||
cv::cann::setDevice(0); | ||
|
||
cv::cann::AclMat aclMat = cv::cann::AclMat(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cv::cann::AclMat aclMat = cv::cann::AclMat();
Just:
cv::cann::AclMat aclMat;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed.
Mat cpuMat1 = randomMat(10, 10, CV_32SC3); \ | ||
Mat cpuMat2 = randomMat(10, 10, CV_32SC3); \ | ||
Mat cpuDst; \ | ||
cv::op(cpuMat1, cpuMat2, cpuDst, __VA_ARGS__); \ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to avoid multi-line macros.
Especially in tests, as we can't debug code line-by-line (macros is a single complex line of code)
Use templates instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FIxed. But it's still exists some multi-line macros. I will change them in next code refactoring(maybe in separate PR).
dbceb99
to
c5770bc
Compare
Hi @opencv-alalek , Thanks for your review. I fixed the review coments, and did some code refactoring. Could you please review it again? Thanks. |
c5770bc
to
981baad
Compare
3df0507
to
2820b30
Compare
Hello @hipudding , does this PR build against opencv/opencv#24277? |
Yes, it is. This PR need some predeclaration, which have to put in opencv main repo. |
26650b0
to
d9dc1e5
Compare
@fengyuentau Good weekends. Now this pr is not depend on opencv's main repo anymore(except OpenCVFindCANN.cmake). Please review it again. Thanks. |
2be0761
to
cb3809a
Compare
aedad52
to
310a96f
Compare
310a96f
to
2b98295
Compare
CANN (Compute Architecture of Neural Networks), developped by Huawei, is a heterogeneous computing architecture for AI. Opencv DNN has already suppoted CANN backend [#22634](opencv/opencv#22634). There are more and more users using [Ascend NPU](https://www.hiascend.com/) and programming with CANN, and the number is still growing rapidly. AI training and inference are inseparable from data preprocessing. When users use OpenCV to work with CANN backend, data preprocessing can only run on CPUs, resulting in inefficiency. The purpose of this commit is to enable OpenCV operators on CANN backend. The usage of CANN backend is consistent, Please refer to OpenCV DNN: [CANN backend manual] (https://gist.github.com/fengyuentau/083f7f339592545c1f1d2c1fde6a53dc#file-a_ocv_cann-md): 1. [Install dependencies] (https://gist.github.com/fengyuentau/083f7f339592545c1f1d2c1fde6a53dc#install-dependencies) 2. [Install CANN] (https://gist.github.com/fengyuentau/083f7f339592545c1f1d2c1fde6a53dc#install-cann) 3. [Compile OpenCV with CANN] (https://gist.github.com/fengyuentau/083f7f339592545c1f1d2c1fde6a53dc#build-opencv-with-cann) The CANN backend is used in a similar way to CUDA: | Object | CANN | CUDA | | --------- | ------------ | -------- | | Namespace | cv::cann | cv::cuda | | Matrix | AscendMat | GpuMat | | Stream | AscendStream | Stream | | Event | AscendEvent | Event | The current commit provides CANN backend operator support framework, In order to make code viewing easy, only a few basic interfaces are implemented, all of the following operators are tested and compared result with CPU backend. More operators will continue implement in new independent commits. Co-authored-by: CaoMengqing <[email protected]>
2b98295
to
ebfbef1
Compare
@hipudding @fengyuentau Hi, When cv::Canny and cv::GaussianBlur operators can be supported with CANN? |
Thank you for your interest in OpenCV’s CANN support. Unfortunately, we do not have any plans for support these operators. OpenCV's CANN support mainly provides the ability of calling CANN build-in operators and run AscendC kernels(see also #3614). Can we talk more details about technical issues via email? ([email protected]) |
Add additional image processing operators for Ascend NPU by utilizing DVPP #3608 The user base for [Ascend NPU](https://www.hiascend.com/en/) and programming with CANN is increasing rapidly, with a growing number of users joining each day. To facilitate the use of these users, this PR provides more support for Ascend backend operators. All operators this PR offers are using use DVPP as the computational unit. Digital Vision Pre-Processing (DVPP) is an image processing unit built into the Ascend AI processor. Its main functions include image and video encoding/decoding, as well as image cropping and scaling. The high-frequency operators with NPU as the backend and basic data structure AscendMat has been provided in #3552, while it still lacks many image processing operators. Moreover, only two interpolation algorithms for the resize operator are supported in #3552. In this PR, the bilinear interpolation algorithm and nearest neighbour interpolation algorithm are implemented for the resize operator, as well as the Ascend implementation of the copyMakeBorder operator. In addition, the serialization of image processing operations is widely used in the preprocessing and post-processing stages of computer vision deep learning methods. Therefore, providing integrated operators is very meaningful for improving the convenience of use for OpenCV and deep learning crossover users. For example, torchvision also provides similar operators: [RESIZED_CROP](https://pytorch.org/vision/stable/generated/torchvision.transforms.functional.resized_crop.html?highlight=resizedcrop). Thus, this PR also provides two serialization processing operators: cropResize and cropResizeMakeBorder. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [N/A] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
Add additional image processing operators for Ascend NPU by utilizing DVPP opencv#3608 The user base for [Ascend NPU](https://www.hiascend.com/en/) and programming with CANN is increasing rapidly, with a growing number of users joining each day. To facilitate the use of these users, this PR provides more support for Ascend backend operators. All operators this PR offers are using use DVPP as the computational unit. Digital Vision Pre-Processing (DVPP) is an image processing unit built into the Ascend AI processor. Its main functions include image and video encoding/decoding, as well as image cropping and scaling. The high-frequency operators with NPU as the backend and basic data structure AscendMat has been provided in opencv#3552, while it still lacks many image processing operators. Moreover, only two interpolation algorithms for the resize operator are supported in opencv#3552. In this PR, the bilinear interpolation algorithm and nearest neighbour interpolation algorithm are implemented for the resize operator, as well as the Ascend implementation of the copyMakeBorder operator. In addition, the serialization of image processing operations is widely used in the preprocessing and post-processing stages of computer vision deep learning methods. Therefore, providing integrated operators is very meaningful for improving the convenience of use for OpenCV and deep learning crossover users. For example, torchvision also provides similar operators: [RESIZED_CROP](https://pytorch.org/vision/stable/generated/torchvision.transforms.functional.resized_crop.html?highlight=resizedcrop). Thus, this PR also provides two serialization processing operators: cropResize and cropResizeMakeBorder. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [N/A] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake
CANN (Compute Architecture of Neural Networks), developped by Huawei, is a heterogeneous computing architecture for AI. Opencv DNN has already suppoted CANN backend #22634.
There are more and more users using Ascend NPU and programming with CANN, and the number is still growing rapidly. AI training and inference are inseparable from data preprocessing. When users use OpenCV to work with CANN backend, data preprocessing can only run on CPUs, resulting in inefficiency.
The purpose of this PR is to enable OpenCV operators on CANN backend. We also complete a E2E test on Ascend 310 for new added operators, see test results.
The usage of CANN backend is consistent, Please refer to OpenCV DNN: CANN backend manual:
The CANN backend is used in a similar way to CUDA:
The current PR provides CANN backend operator support framework, In order to make code viewing easy, only some basic interfaces are implemented, all of the following operators are tested and compared result with CPU backend:
More operators will continue implement in new independent PRs.
OpenCVFindCANN.cmake is modified in opencv#24488.
Pull Request Readiness Checklist
See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request
Patch to opencv_extra has the same branch name.
Co-authored-by: CaoMengqing [email protected]