CARVIEW |
Navigation Menu
-
-
Notifications
You must be signed in to change notification settings - Fork 56.2k
dnn: Add a Vulkan based backend #12703
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
Conversation
bc05c26
to
804c6d5
Compare
@wzw-intel, as I see, the patch includes quite a bit of binary blobs, which are nearly impossible to support by people different from the author. Is it possible to replace those blobs with the source code? |
@vpisarev, thanks for your comments. Shader source code are in the dnn/src/vkcom/shader directory and with suffix ".comp", but the vkcom backend will directly use the bianry format of shader instead of compiling the .comp on the runtime. The reason is Vulkan driver don't cover the compiling from glsl source code to SPIR-V binary. If we want this functionality, 3 choices:
What is your opinion? |
This commit adds a new backend "DNN_BACKEND_VKCOM" and a new target "DNN_TARGET_VULKAN". VKCOM means vulkan based computation library. This backend uses Vulkan API and SPIR-V shaders to do the inference computation for layers. The layer types that implemented in DNN_BACKEND_VKCOM include: Conv, Concat, ReLU, LRN, PriorBox, Softmax, MaxPooling, AvePooling, Permute This is just a beginning work for Vulkan in OpenCV DNN, more layer types will be supported and performance tuning is on the way. Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>
@wzw-intel: I don't work on opencv, so this might not be accepted by the project. But, maybe you could update opencv's cmake to attempt to detect if glslang is available. If it is available, it can enable vulkan support and use glslang to compile the spir-v source files. If it is not available, then the vulkan support could be disabled. |
@wzw-intel, we discussed it a little bit. More general questions first.
For example, here is the ideal situation with OpenCL acceleration:
|
@vpisarev |
I believe, at first, we should evaluate required tools/SDKs to build/run Vulkan code. OpenCV source code tree has:
|
Hi @alalek, I pushed an update to fix can't find vulkan package issue. You can follow below steps to test the vulkan DNN patch (take Ubuntu 16.04 as an example):
As you can see, this version has dependency on Vulkan runtime (libvulkan.so) for both compile-time and run-time, and can't fallback to CPU path if can't detect Vulkan. I will fix these issues in next update by using dynamic load method. |
985abee
to
efb9e77
Compare
In order to build dnn with Vulkan support, need installing Vulkan SDK and setting environment variable "VULKAN_SDK" and add "-DWITH_VULKAN=ON" to cmake command. You can download Vulkan SDK from: https://vulkan.lunarg.com/sdk/home#linux For how to install, see https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html https://vulkan.lunarg.com/doc/sdk/latest/windows/getting_started.html https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html respectively for linux, windows and mac. To run the vulkan backend, also need installing mesa driver. On Ubuntu, use this command 'sudo apt-get install mesa-vulkan-drivers' To test, use command '$BUILD_DIR/bin/opencv_test_dnn --gtest_filter=*VkCom*' Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>
No compile-time dependency on Vulkan library. If Vulkan runtime is unavailable, fallback to CPU path. Use environment "OPENCL_VULKAN_RUNTIME" to specify path to your own vulkan runtime library. Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com>
…ders The SPIR-V shaders are in format of text-based 32-bit hexadecimal numbers, and inserted into .cpp files as unsigned int32 array.
cmake/OpenCVDetectVulkan.cmake
Outdated
@@ -0,0 +1,17 @@ | |||
set(VULKAN_INCLUDE_DIRS "${OpenCV_SOURCE_DIR}/modules/dnn/src/vkcom/" CACHE PATH "Vulkan include directory") |
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.
Please move 3rdparty headers (original from Khronos) into "/3rdparty/include/vkcom".
cmake/OpenCVDetectVulkan.cmake
Outdated
|
||
set(HAVE_VULKAN 1) | ||
add_definitions(-DVK_NO_PROTOTYPES) | ||
include_directories(${VULKAN_INCLUDE_DIRS}) |
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.
Perhaps we should not add this globally.
In dnn/CMakeLists.txt
:
if(HAVE_VULKAN)
add_definitions(-DVK_NO_PROTOTYPES)
include_directories(${VULKAN_INCLUDE_DIRS})
endif()
// It is subject to the license terms in the LICENSE file found in the top-level directory | ||
// of this distribution and at https://opencv.org/license.html. | ||
// | ||
// Copyright (C) 2017, Intel Corporation, all rights reserved. |
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.
Please fix copyright year.
… fixes Vulkan header files are copied from https://github.com/KhronosGroup/Vulkan-Docs/tree/master/include/vulkan to 3rdparty/include Fix the Copyright declaration issue. Refine OpenCVDetectVulkan.cmake
Hi @alalek, I fixed the issues you mentioned in your review comments, plase check. |
@wzw-intel, big thanks! Excellent job! @dkurt, could you please review it as well? |
using namespace cv::dnn; | ||
using namespace testing; | ||
|
||
static void test(Mat& input, Net& net, Backend backendId, Target targetId, bool skipCheck = false) |
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 think it's better to add Vulkan target tests to existing ones. Please see
test_backends.cpp (whole networks)
test_layers.cpp
test_halide_layers.cpp (there are not only Halide tests actually)
Also fixed some test failures. - Don't use bool variable as uniform for shader - Fix dispathed group number beyond max issue - Bypass "group > 1" convolution. This should be support in future.
Hi @dkurt, |
@wzw-intel, is there a way to choose Vulkan device because now it fallbacks to default CPU implementation for me. |
@dkurt |
@wzw-intel, Thanks! It looks like accuracy tests use Vulkan backend properly. I just wanted to run performance tests and it fallbacks because of the following check. Can you take at look on perf_net.cpp? bool isAvailable()
{
return getContext() != NULL;
} |
@@ -0,0 +1,301 @@ | |||
#ifndef VK_ENTRY |
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.
Please add .hpp
extension (to make happy code editors): function_list.inl.hpp
Test build dumps this:
Notes:
|
Unfortunately I'm not able to run all the tests because of segfault:
It happens not for a specific test case but after some time. |
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.
👍
* dnn: Add a Vulkan based backend This commit adds a new backend "DNN_BACKEND_VKCOM" and a new target "DNN_TARGET_VULKAN". VKCOM means vulkan based computation library. This backend uses Vulkan API and SPIR-V shaders to do the inference computation for layers. The layer types that implemented in DNN_BACKEND_VKCOM include: Conv, Concat, ReLU, LRN, PriorBox, Softmax, MaxPooling, AvePooling, Permute This is just a beginning work for Vulkan in OpenCV DNN, more layer types will be supported and performance tuning is on the way. Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com> * dnn/vulkan: Add FindVulkan.cmake to detect Vulkan SDK In order to build dnn with Vulkan support, need installing Vulkan SDK and setting environment variable "VULKAN_SDK" and add "-DWITH_VULKAN=ON" to cmake command. You can download Vulkan SDK from: https://vulkan.lunarg.com/sdk/home#linux For how to install, see https://vulkan.lunarg.com/doc/sdk/latest/linux/getting_started.html https://vulkan.lunarg.com/doc/sdk/latest/windows/getting_started.html https://vulkan.lunarg.com/doc/sdk/latest/mac/getting_started.html respectively for linux, windows and mac. To run the vulkan backend, also need installing mesa driver. On Ubuntu, use this command 'sudo apt-get install mesa-vulkan-drivers' To test, use command '$BUILD_DIR/bin/opencv_test_dnn --gtest_filter=*VkCom*' Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com> * dnn/Vulkan: dynamically load Vulkan runtime No compile-time dependency on Vulkan library. If Vulkan runtime is unavailable, fallback to CPU path. Use environment "OPENCL_VULKAN_RUNTIME" to specify path to your own vulkan runtime library. Signed-off-by: Wu Zhiwen <zhiwen.wu@intel.com> * dnn/Vulkan: Add a python script to compile GLSL shaders to SPIR-V shaders The SPIR-V shaders are in format of text-based 32-bit hexadecimal numbers, and inserted into .cpp files as unsigned int32 array. * dnn/Vulkan: Put Vulkan headers into 3rdparty directory and some other fixes Vulkan header files are copied from https://github.com/KhronosGroup/Vulkan-Docs/tree/master/include/vulkan to 3rdparty/include Fix the Copyright declaration issue. Refine OpenCVDetectVulkan.cmake * dnn/Vulkan: Add vulkan backend tests into existing ones. Also fixed some test failures. - Don't use bool variable as uniform for shader - Fix dispathed group number beyond max issue - Bypass "group > 1" convolution. This should be support in future. * dnn/Vulkan: Fix multiple initialization in one thread.
This commit adds a new backend "DNN_BACKEND_VKCOM" and a
new target "DNN_TARGET_VULKAN". VKCOM means vulkan based
computation library.
This backend uses Vulkan API and SPIR-V shaders to do
the inference computation for layers. The layer types
that implemented in DNN_BACKEND_VKCOM include:
Conv, Concat, ReLU, LRN, PriorBox, Softmax, MaxPooling,
AvePooling, Permute
This is just a beginning work for Vulkan in OpenCV DNN,
more layer types will be supported and performance
tuning is on the way.
Build OpenCV DNN with Vulkan support
Due to usage of dynamic load mechanism, there is no compile-time dependency on Vulkan Loader Library or vendor driver. You just need add "-DWITH_VULKAN=ON" into cmake options to enable the DNN Vukand support.
Enable DNN Vulkan runtime support
Install Vulkan Loader library
You have 2 choices to install Vulkan Loader library:
1.1 Install the library from Vulkan SDK
Download Vulkan SDK from https://vulkan.lunarg.com/sdk/home#sdk/downloadConfirm/1.1.85.0/linux/vulkansdk-linux-x86_64-1.1.85.0.tar.gz
'mkdir ~/vulkan_sdk'
'tar zxf vulkansdk-linux-x86_64-1.1.85.0.tar.gz -C ~/vulkan_sdk'
Your library is at "$HOME/vulkan_sdk/1.x.x.x/x86_64/libvulkan.so.1"
1.2 Install the library by using apt1.
'sudo apt-get install libvulkan1'
Your library will be loacated at "/usr/lib/x86_64-linux-gnu/libvulkan.so.1"
Install vendor driver (For Intel and AMD GPU)
Also 2 choices:
2.1 Compiling and installing Mesa
Ref https://www.mesa3d.org/install.html for HOWTO.
Additionally, add option "--with-vulkan-drivers=intel" when doing configure to build intel Vulkan driver.
"--with-vulkan-drivers=radeon" for AMD GPU.
2.2 Install the library from apt
'sudo apt-get install mesa-vulkan-drivers'
Install Vulkan utils (optional)
If you have installed Vulkan SDK follow 1.1, you already got the utils.
'source $HOME/vulkan_sdk/1.x.x.x/setup-env.sh' to add the utils path into $PATH
You can also install the vulkan utils by 'sudo apt-get install vulkan-utils'
Verify your Vulkan installation (optional)
run 'vulkaninfo' and if you can see output string like "GPU id : 0 (Intel(R) xxxxxx", that means Vulkan works.
Run test
Before run test, if you installed Vulkan Loader library as 1.1,
you need 'export OPENCV_VULKAN_RUNTIME=$HOME/vulkan_sdk/1.x.x.x/x86_64/libvulkan.so.1'
run '$OPENCV_BUILD_DIR/bin/opencv_test_dnn'
The results with "VKCOM/VULKAN" suffix are for our backend.
How to patch GLSL shaders
The GLSL shader source code are in $OPENCV_SOURCE_DIR/module/dnn/src/vkcom/shader/, with suffix ".comp".
Once you modify the shader source code or add new shaders, go into $OPENCV_SOURCE_DIR/module/dnn/src/vkcom/shader/ and run 'python spirv_generator.py' to compile them into SPIR-V format and attach the binary into .cpp
files. And rebuild the DNN module.
In order to run this script successfully, you need tool "glslangValidator" which is provied by Vulkan SDK.
Signed-off-by: Wu Zhiwen zhiwen.wu@intel.com