Skip to content

dnn: TIM-VX NPU backend support#21036

Merged
alalek merged 2 commits intoopencv:4.xfrom
fengyuentau:timvx_backend_support
Mar 31, 2022
Merged

dnn: TIM-VX NPU backend support#21036
alalek merged 2 commits intoopencv:4.xfrom
fengyuentau:timvx_backend_support

Conversation

@zihaomu
Copy link
Copy Markdown
Member

@zihaomu zihaomu commented Nov 10, 2021

This PR adds TIM-VX NPU backend to DNN module.

Tutorial on how to use the TIM-VX backend.

https://gist.github.com/fengyuentau/5a7a5ba36328f2b763aea026c43fa45f

Cross compilation for aarch64 and build and run tests with x86_64 simulators using docker: https://gist.github.com/fengyuentau/bf9e54f80dbec4cc0ea13b8f8a985f0b

NPU chip is commonly used DNN acceleration chip. TIM-VX is good NPU backend, it can support 8-bit ops, and its API follows the OpenVX protocol.

Supported Op status
Conv Int8 Done
Conv Float Done
Pooling Int8 Done
MatMulInt8 Done
EltwiseInt8 Done
Const Done
SoftmaxInt8 Partially Done (Only work in ONNX Softmax layer.)
SigmoidInt8 Done
LeakyReluInt8 Done

OpenCV without TIM-VX works with NEON.

Performance Test on Khadas Vim3
Test Model Input size OpenCV with TIM-VX on NPU (ms) OpenCV without TIM-VX(ms)
Convolution float 1 x 3 x 10 x 10 0.082125 0.011292
Convolution float 1 x 3 x 1280 x 1280 376.457 14.0528
Convolution int8 1 x 3 x 10 x 10 0.053208 0.011625
Convolution int8 (Channel-wise Quantize) 1 x 3 x 1280 x 1280 18.7861 18.7861
Convolution int8 (Tensor-wise Quantize) 1 x 3 x 1280 x 1280 17.01 19.5397
ResNet50 int8 (Channel-wise Quantize) 1 x 3 x 224 x 224 525.298 256.678
ResNet50 int8 (Tensor Wise Quantize) 1 x 3 x 224 x 224 20.01 256.678

Pull Request Readiness Checklist

See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request

  • I agree to contribute to the project under Apache 2 License.
  • To the best of my knowledge, the proposed patch is not based on a code under GPL or other license that is incompatible with OpenCV
  • The PR is proposed to proper branch
  • There is reference to original bug report and related work
  • There is accuracy test, performance test and test data in opencv_extra repository, if applicable
    Patch to opencv_extra has the same branch name.
  • The feature is well documented and sample code can be built with the project CMake
**WIP**
force_builders_only=linux,Linux OpenCL,Linux AVX2,docs,mac
test_modules=dnn,java,python3

@fengyuentau fengyuentau force-pushed the timvx_backend_support branch from c939ea9 to 40a1d74 Compare November 30, 2021 09:12
#cmakedefine HAVE_VULKAN

/* TimVx support */
#cmakedefine HAVE_TIMVX
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't change cvconfig.h content anymore with module-specific definitions.
Use add_definitions() in DNN module only.

*/
/** @example samples/dnn/text_detection.cpp
*/

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unnecessary changes from the PR

public:
int input_zp, output_zp;
float output_sc;
float output_sc, input_sc;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

align order of input/output variables.

CV_WRAP DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
CV_WRAP DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload
// DictValue(const uchar *s1) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s1; } //!< @overload
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dead code

DNN_TARGET_CUDA_FP16,
DNN_TARGET_HDDL
DNN_TARGET_NPU,
DNN_TARGET_HDDL,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Enum additions should go to the end.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, @alalek. Thank you for code review. In my test, if I move DNN_BACKEND_TIMVX to the end of enum Backend, I will get an error.

If I do the following, the value of DNN_BACKEND_TIMVX in the OpenCV library is 1000001, while the value of DNN_BACKEND_TIMVX when calling the OpenCV library is 6.
This may be caused by #ifdef __OPENCV_BUILD flag. In this case, I can not use DNN_BACKEND_TIMVX to trigger TimVx Backend, I can only use 1000001 to do it.
It happens on Ubuntu of ARM, but not for the Ubuntu of X86.

    enum Backend
    {
        //! DNN_BACKEND_DEFAULT equals to DNN_BACKEND_INFERENCE_ENGINE if
        //! OpenCV is built with Intel's Inference Engine library or
        //! DNN_BACKEND_OPENCV otherwise.
        DNN_BACKEND_DEFAULT = 0,
        DNN_BACKEND_HALIDE,
        DNN_BACKEND_INFERENCE_ENGINE,            //!< Intel's Inference Engine computational backend
                                                 //!< @sa setInferenceEngineBackendType
        DNN_BACKEND_OPENCV,
        DNN_BACKEND_VKCOM,
        DNN_BACKEND_CUDA,
#ifdef __OPENCV_BUILD
        DNN_BACKEND_INFERENCE_ENGINE_NGRAPH = 1000000,     // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
        DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019,      // internal - use DNN_BACKEND_INFERENCE_ENGINE + setInferenceEngineBackendType()
#endif
        DNN_BACKEND_TIMVX
    };

Copy link
Copy Markdown
Member

@alalek alalek Dec 1, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is about TARGET, not BACKEND.

Requirement of addition to the end comes from preserving values of already existed enum items.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In BACKEND put to the end before #ifdef __OPENCV_BUILD

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank your reply, got it.

case DNN_TARGET_FPGA: out << "FPGA"; colorId = 4; break;
case DNN_TARGET_CUDA: out << "CUDA"; colorId = 5; break;
case DNN_TARGET_CUDA_FP16: out << "CUDA_FP16"; colorId = 6; break;
case DNN_TARGET_NPU: out << "NPU"; colorId = 9; break;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::vector<string> colors must be updated too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, It was fixed in the latest update.


#ifdef HAVE_TIMVX
// For TimVx
input_sc = params.get<float>("input_scale");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Field is defined in public API, so it must be initialized regardless of build flags.

_MM_SET_FLUSH_ZERO_MODE(ftzMode);
_MM_SET_DENORMALS_ZERO_MODE(dazMode);
#endif

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove unnecessary changes

hasVecInput = false;
op = SUM;


Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove

tim::vx::DataType tensorDataType;
switch(matDepth)
{
case 0: // CV_8U
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

0

we don't need magic numbers here. Use CV_8U directly.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, it was fixed in the latest update.

Copy link
Copy Markdown
Member

@fengyuentau fengyuentau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make install will failed when TIM-VX is building from source:

-- Up-to-date: /home/tau/Workspace/fengyuentau/opencv/build/install/lib/libtim-vx.a
-- Up-to-date: /home/tau/Workspace/fengyuentau/opencv/build/install/lib/libtim-vx.a
CMake Error at 3rdparty/libtim-vx/build/src/tim/cmake_install.cmake:78 (file):
  file INSTALL cannot find
  "/home/tau/Workspace/fengyuentau/opencv/include/tim/vx": No such file or
  directory.
Call Stack (most recent call first):
  3rdparty/libtim-vx/build/cmake_install.cmake:42 (include)
  cmake_install.cmake:135 (include)


make: *** [Makefile:86: install] Error 1

We need to find a way to solve this.

Copy link
Copy Markdown
Member

@fengyuentau fengyuentau left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Current version of cmake code requires TIM-VX to be built as static library, which solves the previous make install error. We will wait for the fix of TIM-VX to solve the linking problem.

@zihaomu zihaomu force-pushed the timvx_backend_support branch from 559245f to 7e38da5 Compare December 24, 2021 09:44
@fengyuentau
Copy link
Copy Markdown
Member

@alalek May I know which version of glibc in linux64 in the default ci? We got the following error

/build/precommit_linux64/build/3rdparty/libtim-vx/VIVANTE_SDK/x86_64.prebuilt.ci.sdk/drivers/libOpenVX.so.1: undefined reference to `logf@GLIBC_2.27'
/build/precommit_linux64/build/3rdparty/libtim-vx/VIVANTE_SDK/x86_64.prebuilt.ci.sdk/drivers/libOpenVX.so.1: undefined reference to `powf@GLIBC_2.27'
/build/precommit_linux64/build/3rdparty/libtim-vx/VIVANTE_SDK/x86_64.prebuilt.ci.sdk/drivers/libOpenVX.so.1: undefined reference to `expf@GLIBC_2.27'
collect2: error: ld returned 1 exit status
apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/build.make:99: recipe for target 'bin/opencv_model_diagnostics' failed
make[2]: Leaving directory '/build/precommit_linux64/build'
make[2]: *** [bin/opencv_model_diagnostics] Error 1
make[1]: *** [apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/all] Error 2
CMakeFiles/Makefile2:22736: recipe for target 'apps/model-diagnostics/CMakeFiles/opencv_model_diagnostics.dir/all' failed
make[1]: *** Waiting for unfinished jobs....

implying the unmatched versions of glibc between the one used in ci (older) and the one used to compile libOpenVX (newer).

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 18, 2022

Base image for this builder is ubuntu:16.04 (check "init" step logs).
I added other builders based on Ubuntu 18.04

@fengyuentau
Copy link
Copy Markdown
Member

Base image for this builder is ubuntu:16.04 (check "init" step logs). I added other builders based on Ubuntu 18.04

@alalek So can I get the same image via docker pull ubuntu:16.04? Verisilicon devs need a same image to compile a SDK that works on linux64 in the ci.

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 18, 2022

the same image

Not the same. Image is based on ubuntu:16.04.
CI images configuration could be found here: https://github.com/opencv-infrastructure/opencv-worker-config/tree/linux/images

Take a look on "Linux AVX2" based on Ubuntu 18.04 - there are set of other issues with pre-compiled binaries.


Pre-compiled binaries are not universal in general. Using of pre-compiled binaries reduces set of supported platforms.
Scope of such platforms should be defined on your side.

@zihaomu zihaomu marked this pull request as ready for review January 25, 2022 05:11
@zihaomu zihaomu force-pushed the timvx_backend_support branch from 5e0c5dd to 1ae0621 Compare February 14, 2022 10:55
@fengyuentau fengyuentau changed the title dnn: TimVx NPU backend support dnn: TIM-VX NPU backend support Feb 27, 2022
@zihaomu zihaomu force-pushed the timvx_backend_support branch from b923992 to 4e2a806 Compare March 4, 2022 10:12

#ifdef HAVE_TIMVX
// update all comsumer
void tvUpdateConfictMap(int graphIndex, LayerData& ld, std::vector<std::vector<int> >& graphConflictMap)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To resolve conflicts with merged #21662 please:

  • move methods declaration into modules/dnn/src/net_impl.hpp file + put timVxInfo declaration near it (after all common fields)
  • methods implementation should be moved into op_timvx.cpp (see halide code as example)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@alalek Thanks for your guidance. We have completed the code refactoring.

@zihaomu zihaomu force-pushed the timvx_backend_support branch from 0d5330b to e51bea2 Compare March 18, 2022 06:33
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me!

Please take a look on "white space" check issues from "Docs" builder.

@zihaomu zihaomu force-pushed the timvx_backend_support branch from a00f4a4 to 82eac54 Compare March 18, 2022 11:10
fengyuentau added a commit to fengyuentau/opencv that referenced this pull request Mar 19, 2022
@fengyuentau fengyuentau mentioned this pull request Mar 19, 2022
6 tasks
@vpisarev vpisarev self-requested a review March 24, 2022 02:09
Copy link
Copy Markdown
Contributor

@vpisarev vpisarev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tested it on my machine, works well!

@vpisarev vpisarev requested review from alalek and fengyuentau March 24, 2022 02:10
@fengyuentau fengyuentau force-pushed the timvx_backend_support branch from 1e0cf9b to 2a7387a Compare March 24, 2022 06:51
set(TIMVX_FOUND ON)
else()
set(OCV_TIMVX_FILENAME "${TIMVX_COMMIT_HASH}.zip")
set(OCV_TIMVX_URL "https://github.com/fengyuentau/TIM-VX/archive/")
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are going to have a discussion with TIM-VX devs to work out a branch in TIM-VX repo for us to use.

@zihaomu zihaomu marked this pull request as ready for review March 31, 2022 02:50
@fengyuentau fengyuentau force-pushed the timvx_backend_support branch from 23fc2dc to f45400d Compare March 31, 2022 03:11
Copy link
Copy Markdown
Member

@alalek alalek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM 👍

@alalek alalek merged commit 7b582b7 into opencv:4.x Mar 31, 2022
@opencv-pushbot opencv-pushbot mentioned this pull request Apr 23, 2022
@fengyuentau fengyuentau deleted the timvx_backend_support branch May 17, 2022 07:37
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
dnn: TIM-VX NPU backend support

* Add TimVX NPU backend for DNN module.

* use official branch from tim-vx repo; fix detecting viv sdk

Co-authored-by: fytao <yuantao.feng@outlook.com>
a-sajjad72 pushed a commit to a-sajjad72/opencv that referenced this pull request Mar 30, 2023
…or_timvx_backend

CI for TIM-VX backend

* github actions for TIM-VX backend opencv#21036

* add reference to yuentau/ocv_ubuntu:20.04; remove extra quotes; enable BUILD_TESTS

* rename to timvx_backend_tests.yml

* add image source prefix

* remove if condition for x86_64 simulator
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants