Skip to content

Commit

Permalink
Add operators support for Ascend NPU (CANN backend) (#3552)
Browse files Browse the repository at this point in the history
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 <cmq0113@163.com>
  • Loading branch information
hipudding and MengqingCao authored Nov 21, 2023
1 parent e8ec6ac commit 3c5635e
Show file tree
Hide file tree
Showing 36 changed files with 5,885 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/PR-4.x.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ jobs:

Linux-RISC-V-Clang:
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-4.x-RISCV.yaml@main

openEuler2203-x64:
if: "${{ contains(github.event.pull_request.labels.*.name, 'category: cann') }}"
uses: opencv/ci-gha-workflow/.github/workflows/OCV-Contrib-PR-4.x-O22-CANN.yaml@main
17 changes: 17 additions & 0 deletions modules/cannops/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
if(IOS OR WINRT OR ANDROID OR APPLE OR WIN32 OR (NOT HAVE_CANN))
ocv_module_disable(cannops)
endif()

set(the_description "Ascend-accelerated Operations on Matrices")

ocv_add_module(cannops opencv_core WRAP python)
ocv_module_include_directories(${CANN_INCLUDE_DIRS})
ocv_glob_module_sources()
ocv_install_used_external_targets(${CANN_LIBRARIES})
ocv_create_module(${CANN_LIBRARIES})

ocv_include_directories(${CMAKE_SOURCE_DIR}/modules/ts/include)

ocv_add_accuracy_tests(DEPENDS_ON opencv_cannops)
ocv_add_perf_tests(DEPENDS_ON opencv_cannops)
ocv_add_samples(opencv_cannops)
67 changes: 67 additions & 0 deletions modules/cannops/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# User guides
#
# 0. Install Ascend driver on host.
# (https://www.hiascend.com/en/hardware/firmware-drivers)
#
# 1. Run docker container.
# docker run -it \
# --name opencv \
# --device /dev/davinci0 \
# --device /dev/davinci_manager \
# --device /dev/devmm_svm \
# --device /dev/hisi_hdc \
# -v /usr/local/dcmi:/usr/local/dcmi \
# -v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
# -v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
# opencv bash
#
# 2. Check environment.
# npu-smi info
#
# 3. Compile opencv with Ascend NPU backend.
# cmake -DWITH_CANN=1
#
# 4. Run opencv_test_cannops.
# ./bin/opencv_test_cannops

FROM openeuler/openeuler:22.03-lts-sp2

RUN yum install -y \
git \
wget \
gcc \
g++ \
cmake \
make \
python-pip \
python3-devel

RUN pip install -i https://pypi.tuna.tsinghua.edu.cn/simple \
numpy \
sympy \
decorator \
scipy \
attrs \
psutil

# Install CANN
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 && \
chmod +x Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run && \
./Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run --quiet --install && \
rm -f ./Ascend-cann-toolkit_7.0.RC1_linux-"$(uname -i)".run

# Install kernel
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 && \
chmod +x Ascend-cann-kernels-310p_7.0.RC1_linux.run && \
./Ascend-cann-kernels-310p_7.0.RC1_linux.run --quiet --install && \
rm -f ./Ascend-cann-kernels-310p_7.0.RC1_linux.run

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
ENV ASCEND_TOOLKIT_HOME=/usr/local/Ascend/ascend-toolkit/latest
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
ENV PYTHONPATH=${ASCEND_TOOLKIT_HOME}/python/site-packages:${ASCEND_TOOLKIT_HOME}/opp/built-in/op_impl/ai_core/tbe:$PYTHONPATH
ENV PATH=${ASCEND_TOOLKIT_HOME}/bin:${ASCEND_TOOLKIT_HOME}/compiler/ccec_compiler/bin:$PATH
ENV ASCEND_AICPU_PATH=${ASCEND_TOOLKIT_HOME}
ENV ASCEND_OPP_PATH=${ASCEND_TOOLKIT_HOME}/opp
ENV TOOLCHAIN_HOME=${ASCEND_TOOLKIT_HOME}/toolkit
ENV ASCEND_HOME_PATH=${ASCEND_TOOLKIT_HOME}
Loading

0 comments on commit 3c5635e

Please sign in to comment.