Skip to content

Commit

Permalink
Merge pull request opencv#18323 from liqi-c:tengine-lite-update
Browse files Browse the repository at this point in the history
Tengine lite update

* update tengine

* Modify for arm32 build.

* format optimization

* add teng_ befor some tengine api

* update graph_t to teng_graph_t

* update graph_t to teng_graph_t

* Code structure optimization

* optimization

* optimization

* remove space

* update tengine url

Co-authored-by: liqi <[email protected]>
  • Loading branch information
liqi-c and liqi authored Sep 23, 2020
1 parent 48ddb53 commit 3fc1487
Show file tree
Hide file tree
Showing 6 changed files with 209 additions and 153 deletions.
25 changes: 8 additions & 17 deletions 3rdparty/libtengine/tengine.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@
# Author: [email protected] or https://github.com/BUG1989
# [email protected]
# [email protected]
#

SET(TENGINE_COMMIT_VERSION "8a4c58e0e05cd850f4bb0936a330edc86dc0e28c")
SET(TENGINE_COMMIT_VERSION "e89cf8870de2ff0a80cfe626c0b52b2a16fb302e")
SET(OCV_TENGINE_DIR "${OpenCV_BINARY_DIR}/3rdparty/libtengine")
SET(OCV_TENGINE_SOURCE_PATH "${OCV_TENGINE_DIR}/Tengine-${TENGINE_COMMIT_VERSION}")

Expand All @@ -32,11 +31,10 @@ IF(EXISTS "${OCV_TENGINE_SOURCE_PATH}")
SET(Tengine_FOUND ON)
SET(BUILD_TENGINE ON)
ELSE()
SET(OCV_TENGINE_FILENAME "${TENGINE_COMMIT_VERSION}.zip")#name2
SET(OCV_TENGINE_URL "https://github.com/OAID/Tengine/archive/") #url2
SET(tengine_md5sum f51ca8f3963faeeff3f019a6f6edc206) #md5sum2
SET(OCV_TENGINE_FILENAME "${TENGINE_COMMIT_VERSION}.zip")#name
SET(OCV_TENGINE_URL "https://github.com/OAID/Tengine/archive/") #url
SET(tengine_md5sum 23f61ebb1dd419f1207d8876496289c5) #md5sum

#MESSAGE(STATUS "**** TENGINE DOWNLOAD BEGIN ****")
ocv_download(FILENAME ${OCV_TENGINE_FILENAME}
HASH ${tengine_md5sum}
URL
Expand All @@ -62,24 +60,17 @@ ENDIF()
if(BUILD_TENGINE)
SET(HAVE_TENGINE 1)

# android system
if(ANDROID)
if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
SET(CONFIG_ARCH_ARM32 ON)
elseif(${ANDROID_ABI} STREQUAL "arm64-v8a")
SET(CONFIG_ARCH_ARM64 ON)
endif()
else()
if(NOT ANDROID)
# linux system
if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
SET(CONFIG_ARCH_ARM32 ON)
SET(TENGINE_TOOLCHAIN_FLAG "-march=armv7-a")
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) ## AARCH64
SET(CONFIG_ARCH_ARM64 ON)
SET(TENGINE_TOOLCHAIN_FLAG "-march=armv8-a")
endif()
endif()

SET(BUILT_IN_OPENCV ON) ## set for tengine compile discern .
SET(Tengine_INCLUDE_DIR "${OCV_TENGINE_SOURCE_PATH}/core/include" CACHE INTERNAL "")
SET(Tengine_INCLUDE_DIR "${OCV_TENGINE_SOURCE_PATH}/include" CACHE INTERNAL "")
if(EXISTS "${OCV_TENGINE_SOURCE_PATH}/CMakeLists.txt")
add_subdirectory("${OCV_TENGINE_SOURCE_PATH}" "${OCV_TENGINE_DIR}/build")
else()
Expand Down
9 changes: 1 addition & 8 deletions modules/dnn/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,11 @@ else()
set(sources_options ${sources_options} EXCLUDE_CUDA)
endif()


if(HAVE_TENGINE)
list(APPEND include_dirs ${TENGINE_INCLUDE_DIRS})
if(EXISTS ${TENGINE_LIBRARIES})
list(APPEND libs ${TENGINE_LIBRARIES})
else()
ocv_add_dependencies(opencv_dnn tengine)
list(APPEND libs ${TENGINE_LIBRARIES})
endif()
list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
endif()


ocv_module_include_directories(${include_dirs})
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override") # GCC
Expand Down
2 changes: 2 additions & 0 deletions modules/dnn/src/dnn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1585,7 +1585,9 @@ struct Net::Impl : public detail::NetImplBase
{
CV_TRACE_FUNCTION();
if (preferableBackend == DNN_BACKEND_OPENCV)
{
CV_Assert(preferableTarget == DNN_TARGET_CPU || IS_DNN_OPENCL_TARGET(preferableTarget));
}
else if (preferableBackend == DNN_BACKEND_HALIDE)
initHalideBackend();
else if (preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)
Expand Down
77 changes: 62 additions & 15 deletions modules/dnn/src/layers/convolution_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,10 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
float power;
#endif

#ifdef HAVE_TENGINE
teng_graph_t tengine_graph;
#endif

#ifdef HAVE_CUDA
cuda4dnn::ConvolutionConfiguration::FusionMode cudaFusionMode;
cuda4dnn::ConvolutionConfiguration::ActivationType cudaActType;
Expand All @@ -266,8 +270,20 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
#ifdef HAVE_CUDA
cudaFusionMode = cuda4dnn::ConvolutionConfiguration::FusionMode::NONE;
cudaActType = cuda4dnn::ConvolutionConfiguration::ActivationType::IDENTITY;
#endif
#ifdef HAVE_TENGINE
tengine_graph=NULL;
#endif
}
#ifdef HAVE_TENGINE
~ConvolutionLayerImpl()
{
if(NULL != tengine_graph )
{
tengine_release(tengine_graph);
}
}
#endif

MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
{
Expand Down Expand Up @@ -391,6 +407,13 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
for(int i = 0; i < numOutput; i++ )
biasvec[i] = biasMat.at<float>(i);
}
#ifdef HAVE_TENGINE
if(NULL != tengine_graph )
{
tengine_release(tengine_graph);
tengine_graph = NULL ;
}
#endif
#ifdef HAVE_OPENCL
convolutionOp.release();
#endif
Expand Down Expand Up @@ -1765,26 +1788,50 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
}

#ifdef HAVE_TENGINE
int inch = inputs[0].size[1]; // inch
int in_h = inputs[0].size[2]; // in_h
int in_w = inputs[0].size[3]; // in_w
bool tengine_ret = false; ;

std::vector<Mat> teng_in, teng_out;
inputs_arr.getMatVector(teng_in);
outputs_arr.getMatVector(teng_out);

int inch = teng_in[0].size[1]; // inch
int in_h = teng_in[0].size[2]; // in_h
int in_w = teng_in[0].size[3]; // in_w

int out_b = outputs[0].size[0]; // out batch size
int outch = outputs[0].size[1]; // outch
int out_h = outputs[0].size[2]; // out_h
int out_w = outputs[0].size[3]; // out_w
int out_b = teng_out[0].size[0]; // out batch size
int outch = teng_out[0].size[1]; // outch
int out_h = teng_out[0].size[2]; // out_h
int out_w = teng_out[0].size[3]; // out_w

float *input_ = inputs[0].ptr<float>();
float *output_ = outputs[0].ptr<float>();
float *input_ = teng_in[0].ptr<float>();
float *output_ = teng_out[0].ptr<float>();
float *kernel_ = weightsMat.ptr<float>();
float *teg_bias = &biasvec[0];

bool tengine_ret = tengine_forward(input_, inch, ngroups, in_h, in_w,
output_, out_b, outch, out_h, out_w,
kernel_, kernel_size.size(), kernel.height, kernel.width,
teg_bias, stride.height, stride.width,
pad.height, pad.width, dilation.height, dilation.width,
weightsMat.step1(), padMode);
int nstripes = std::max(getNumThreads(), 1);

/* tengine_init will run when first time. */
if(NULL == tengine_graph)
{
tengine_graph = tengine_init(name.c_str(), input_, inch, ngroups, in_h, in_w,
output_, out_b, outch, out_h, out_w,
kernel_, kernel_size.size(), kernel.height, kernel.width,
teg_bias, stride.height, stride.width,
pad.height, pad.width, dilation.height, dilation.width,
weightsMat.step1(), padMode, tengine_graph, nstripes);
/*printf("Init(%s): input=%p(%d %d %d %d ),output=%p(%d %d %d %d ),kernel=%p(%ld %d %d ), bias=%p ,"
"stride(%d %d), pad(%d %d), dilation(%d %d) ,weightsMat=%ld, padMode=%s ,tengine_graph = %p \n",
name.c_str(),input_, inch, ngroups, in_h, in_w,
output_, out_b, outch, out_h, out_w,
kernel_, kernel_size.size(), kernel.height, kernel.width,
teg_bias, stride.height, stride.width,
pad.height, pad.width, dilation.height, dilation.width,
weightsMat.step1(), padMode.c_str() ,tengine_graph);*/
}
if(NULL != tengine_graph)
{
tengine_ret = tengine_forward(tengine_graph);
}
/* activation */
if((true == tengine_ret) && activ )
{
Expand Down
13 changes: 10 additions & 3 deletions modules/dnn/src/tengine4dnn/include/tengine_graph_convolution.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,24 @@
#define TENGINE_GRAPH_CONVOLUTION_HPP

#define FLOAT_TO_REALSIZE (4)
#ifdef HAVE_TENGINE

#include "tengine_c_api.h"

namespace cv
{
namespace dnn
{
bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
teng_graph_t tengine_init(const char* name , float* input_, int inch, int group, int in_h, int in_w,
float *output_, int out_b, int outch, int out_h, int out_w,
float *kernel_,int kernel_s , int kernel_h, int kernel_w,
float *teg_bias, int stride_h,int stride_w,
int pad_h, int pad_w, int dilation_h, int dilation_w,
size_t wstep, const std::string padMode) ;
size_t wstep, const std::string padMode , teng_graph_t& graph, int nstripes) ;

bool tengine_forward(teng_graph_t& graph) ;
bool tengine_release(teng_graph_t& graph) ;
}
}
#endif /* TENGINE_GRAPH_CONVOLUTION_HPP */
#endif
#endif /* TENGINE_GRAPH_CONVOLUTION_HPP */
Loading

0 comments on commit 3fc1487

Please sign in to comment.