opencv/modules/dnn/src/op_vkcom.hpp

75 lines
2.2 KiB
C++
Raw Normal View History

Merge pull request #12703 from wzw-intel:vkcom * 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.
2018-10-29 22:51:26 +08:00
// This file is part of OpenCV project.
// 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.
//
// Copyright (C) 2018, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
#ifndef OPENCV_DNN_OP_VKCOM_HPP
#define OPENCV_DNN_OP_VKCOM_HPP
#include <opencv2/dnn/shape_utils.hpp>
#ifdef HAVE_VULKAN
#include "vkcom/include/vkcom.hpp"
#endif // HAVE_VULKAN
namespace cv
{
namespace dnn
{
#ifdef HAVE_VULKAN
std::vector<vkcom::Tensor> VkComTensors(const std::vector<Ptr<BackendWrapper> >& ptrs);
vkcom::Tensor VkComTensor(const Ptr<BackendWrapper>& ptr);
// Data copied from/to Mat to/from Tensor. Change the shape of dst if
// needed to make it the same shape as src
void copyToTensor(vkcom::Tensor &dst, const Mat &src);
void copyToMat(Mat &dst, const vkcom::Tensor &src);
class VkComBackendNode : public BackendNode
{
public:
VkComBackendNode(const std::vector<Ptr<BackendWrapper> >& inputsWrapper,
const std::shared_ptr<vkcom::OpBase> &op,
const std::vector<Ptr<BackendWrapper> >& blobsWrapper =
std::vector<Ptr<BackendWrapper> >());
bool forward(std::vector<vkcom::Tensor>& outs);
private:
std::vector<vkcom::Tensor> ins;
std::vector<vkcom::Tensor> blobs;
std::vector<Ptr<BackendWrapper> > inputsWrapper_;
std::shared_ptr<vkcom::OpBase> operation;
};
class VkComBackendWrapper : public BackendWrapper
{
public:
VkComBackendWrapper(Mat& m);
VkComBackendWrapper(const Ptr<BackendWrapper>& baseBuffer, Mat& m);
virtual void copyToHost() CV_OVERRIDE;
virtual void setHostDirty() CV_OVERRIDE;
void setDeviceDirty();
void copyToDevice();
vkcom::Tensor getTensor();
private:
vkcom::Tensor tensor;
Mat* host;
bool hostDirty;
bool deviceDirty;
};
#endif // HAVE_VULKAN
void forwardVkCom(std::vector<Ptr<BackendWrapper> > &outputs, const Ptr<BackendNode>& node);
bool haveVulkan();
} // namespace dnn
} // namespace cv
#endif // OPENCV_DNN_OP_VKCOM_HPP