Skip to content

Commit ebfbef1

Browse files
Add operators support for Ascend NPU (CANN backend)
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]>
1 parent d51add3 commit ebfbef1

36 files changed

+5885
-0
lines changed

.github/workflows/PR-4.x.yaml

+4
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,7 @@ jobs:
2929

3030
Linux-RISC-V-Clang:
3131
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-4.x-RISCV.yaml@main
32+
33+
openEuler2203-x64:
34+
if: "${{ contains(github.event.pull_request.labels.*.name, 'category: cann') }}"
35+
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-4.x-O22-CANN.yaml@main

modules/cannops/CMakeLists.txt

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
if(IOS OR WINRT OR ANDROID OR APPLE OR WIN32 OR (NOT HAVE_CANN))
2+
ocv_module_disable(cannops)
3+
endif()
4+
5+
set(the_description "Ascend-accelerated Operations on Matrices")
6+
7+
ocv_add_module(cannops opencv_core WRAP python)
8+
ocv_module_include_directories(${CANN_INCLUDE_DIRS})
9+
ocv_glob_module_sources()
10+
ocv_install_used_external_targets(${CANN_LIBRARIES})
11+
ocv_create_module(${CANN_LIBRARIES})
12+
13+
ocv_include_directories(${CMAKE_SOURCE_DIR}/modules/ts/include)
14+
15+
ocv_add_accuracy_tests(DEPENDS_ON opencv_cannops)
16+
ocv_add_perf_tests(DEPENDS_ON opencv_cannops)
17+
ocv_add_samples(opencv_cannops)

modules/cannops/Dockerfile

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
# User guides
2+
#
3+
# 0. Install Ascend driver on host.
4+
# (https://www.hiascend.com/en/hardware/firmware-drivers)
5+
#
6+
# 1. Run docker container.
7+
# docker run -it \
8+
# --name opencv \
9+
# --device /dev/davinci0 \
10+
# --device /dev/davinci_manager \
11+
# --device /dev/devmm_svm \
12+
# --device /dev/hisi_hdc \
13+
# -v /usr/local/dcmi:/usr/local/dcmi \
14+
# -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
15+
# -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
16+
# opencv bash
17+
#
18+
# 2. Check environment.
19+
# npu-smi info
20+
#
21+
# 3. Compile opencv with Ascend NPU backend.
22+
# cmake -DWITH_CANN=1
23+
#
24+
# 4. Run opencv_test_cannops.
25+
# ./bin/opencv_test_cannops
26+
27+
FROM openeuler/openeuler:22.03-lts-sp2
28+
29+
RUN yum install -y \
30+
git \
31+
wget \
32+
gcc \
33+
g++ \
34+
cmake \
35+
make \
36+
python-pip \
37+
python3-devel
38+
39+
RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple \
40+
numpy \
41+
sympy \
42+
decorator \
43+
scipy \
44+
attrs \
45+
psutil
46+
47+
# Install CANN
48+
RUN wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%207.0.RC1/Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run && \
49+
chmod +x Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run && \
50+
./Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run --quiet --install && \
51+
rm -f ./Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run
52+
53+
# Install kernel
54+
RUN wget https://ascend-repo.obs.cn-east-2.myhuaweicloud.com/CANN/CANN%207.0.RC1/Ascend-cann-kernels-310p_7.0.RC1_linux.run && \
55+
chmod +x Ascend-cann-kernels-310p_7.0.RC1_linux.run && \
56+
./Ascend-cann-kernels-310p_7.0.RC1_linux.run --quiet --install && \
57+
rm -f ./Ascend-cann-kernels-310p_7.0.RC1_linux.run
58+
59+
ENV LD_LIBRARY_PATH=/usr/local/Ascend/driver/lib64:/usr/local/Ascend/driver/lib64/common:/usr/local/Ascend/driver/lib64/driver:$LD_LIBRARY_PATH:/usr/lib64
60+
ENV ASCEND_TOOLKIT_HOME=/usr/local/Ascend/ascend-toolkit/latest
61+
ENV LD_LIBRARY_PATH=${ASCEND_TOOLKIT_HOME}/lib64:${ASCEND_TOOLKIT_HOME}/lib64/plugin/opskernel:${ASCEND_TOOLKIT_HOME}/lib64/plugin/nnengine:${ASCEND_TOOLKIT_HOME}/opp/built-in/op_impl/ai_core/tbe/op_tiling:$LD_LIBRARY_PATH
62+
ENV PYTHONPATH=${ASCEND_TOOLKIT_HOME}/python/site-packages:${ASCEND_TOOLKIT_HOME}/opp/built-in/op_impl/ai_core/tbe:$PYTHONPATH
63+
ENV PATH=${ASCEND_TOOLKIT_HOME}/bin:${ASCEND_TOOLKIT_HOME}/compiler/ccec_compiler/bin:$PATH
64+
ENV ASCEND_AICPU_PATH=${ASCEND_TOOLKIT_HOME}
65+
ENV ASCEND_OPP_PATH=${ASCEND_TOOLKIT_HOME}/opp
66+
ENV TOOLCHAIN_HOME=${ASCEND_TOOLKIT_HOME}/toolkit
67+
ENV ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME}

0 commit comments

Comments
 (0)