Skip to content

Commit 3fc1487

Browse files
liqi-cliqi
andauthored
Merge pull request opencv#18323 from liqi-c:tengine-lite-update
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]>
1 parent 48ddb53 commit 3fc1487

File tree

6 files changed

+209
-153
lines changed

6 files changed

+209
-153
lines changed

3rdparty/libtengine/tengine.cmake

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
# Author: [email protected] or https://github.com/BUG1989
2121
2222
23-
#
2423

25-
SET(TENGINE_COMMIT_VERSION "8a4c58e0e05cd850f4bb0936a330edc86dc0e28c")
24+
SET(TENGINE_COMMIT_VERSION "e89cf8870de2ff0a80cfe626c0b52b2a16fb302e")
2625
SET(OCV_TENGINE_DIR "${OpenCV_BINARY_DIR}/3rdparty/libtengine")
2726
SET(OCV_TENGINE_SOURCE_PATH "${OCV_TENGINE_DIR}/Tengine-${TENGINE_COMMIT_VERSION}")
2827

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

39-
#MESSAGE(STATUS "**** TENGINE DOWNLOAD BEGIN ****")
4038
ocv_download(FILENAME ${OCV_TENGINE_FILENAME}
4139
HASH ${tengine_md5sum}
4240
URL
@@ -62,24 +60,17 @@ ENDIF()
6260
if(BUILD_TENGINE)
6361
SET(HAVE_TENGINE 1)
6462

65-
# android system
66-
if(ANDROID)
67-
if(${ANDROID_ABI} STREQUAL "armeabi-v7a")
68-
SET(CONFIG_ARCH_ARM32 ON)
69-
elseif(${ANDROID_ABI} STREQUAL "arm64-v8a")
70-
SET(CONFIG_ARCH_ARM64 ON)
71-
endif()
72-
else()
63+
if(NOT ANDROID)
7364
# linux system
7465
if(CMAKE_SYSTEM_PROCESSOR STREQUAL arm)
75-
SET(CONFIG_ARCH_ARM32 ON)
66+
SET(TENGINE_TOOLCHAIN_FLAG "-march=armv7-a")
7667
elseif(CMAKE_SYSTEM_PROCESSOR STREQUAL aarch64) ## AARCH64
77-
SET(CONFIG_ARCH_ARM64 ON)
68+
SET(TENGINE_TOOLCHAIN_FLAG "-march=armv8-a")
7869
endif()
7970
endif()
8071

8172
SET(BUILT_IN_OPENCV ON) ## set for tengine compile discern .
82-
SET(Tengine_INCLUDE_DIR "${OCV_TENGINE_SOURCE_PATH}/core/include" CACHE INTERNAL "")
73+
SET(Tengine_INCLUDE_DIR "${OCV_TENGINE_SOURCE_PATH}/include" CACHE INTERNAL "")
8374
if(EXISTS "${OCV_TENGINE_SOURCE_PATH}/CMakeLists.txt")
8475
add_subdirectory("${OCV_TENGINE_SOURCE_PATH}" "${OCV_TENGINE_DIR}/build")
8576
else()

modules/dnn/CMakeLists.txt

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -128,18 +128,11 @@ else()
128128
set(sources_options ${sources_options} EXCLUDE_CUDA)
129129
endif()
130130

131-
132131
if(HAVE_TENGINE)
133132
list(APPEND include_dirs ${TENGINE_INCLUDE_DIRS})
134-
if(EXISTS ${TENGINE_LIBRARIES})
135-
list(APPEND libs ${TENGINE_LIBRARIES})
136-
else()
137-
ocv_add_dependencies(opencv_dnn tengine)
138-
list(APPEND libs ${TENGINE_LIBRARIES})
139-
endif()
133+
list(APPEND libs -Wl,--whole-archive ${TENGINE_LIBRARIES} -Wl,--no-whole-archive)
140134
endif()
141135

142-
143136
ocv_module_include_directories(${include_dirs})
144137
if(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
145138
ocv_append_source_files_cxx_compiler_options(fw_srcs "-Wno-suggest-override") # GCC

modules/dnn/src/dnn.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,9 @@ struct Net::Impl : public detail::NetImplBase
15851585
{
15861586
CV_TRACE_FUNCTION();
15871587
if (preferableBackend == DNN_BACKEND_OPENCV)
1588+
{
15881589
CV_Assert(preferableTarget == DNN_TARGET_CPU || IS_DNN_OPENCL_TARGET(preferableTarget));
1590+
}
15891591
else if (preferableBackend == DNN_BACKEND_HALIDE)
15901592
initHalideBackend();
15911593
else if (preferableBackend == DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019)

modules/dnn/src/layers/convolution_layer.cpp

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,10 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
248248
float power;
249249
#endif
250250

251+
#ifdef HAVE_TENGINE
252+
teng_graph_t tengine_graph;
253+
#endif
254+
251255
#ifdef HAVE_CUDA
252256
cuda4dnn::ConvolutionConfiguration::FusionMode cudaFusionMode;
253257
cuda4dnn::ConvolutionConfiguration::ActivationType cudaActType;
@@ -266,8 +270,20 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
266270
#ifdef HAVE_CUDA
267271
cudaFusionMode = cuda4dnn::ConvolutionConfiguration::FusionMode::NONE;
268272
cudaActType = cuda4dnn::ConvolutionConfiguration::ActivationType::IDENTITY;
273+
#endif
274+
#ifdef HAVE_TENGINE
275+
tengine_graph=NULL;
269276
#endif
270277
}
278+
#ifdef HAVE_TENGINE
279+
~ConvolutionLayerImpl()
280+
{
281+
if(NULL != tengine_graph )
282+
{
283+
tengine_release(tengine_graph);
284+
}
285+
}
286+
#endif
271287

272288
MatShape computeColRowShape(const MatShape &inpShape, const MatShape &outShape) const CV_OVERRIDE
273289
{
@@ -391,6 +407,13 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
391407
for(int i = 0; i < numOutput; i++ )
392408
biasvec[i] = biasMat.at<float>(i);
393409
}
410+
#ifdef HAVE_TENGINE
411+
if(NULL != tengine_graph )
412+
{
413+
tengine_release(tengine_graph);
414+
tengine_graph = NULL ;
415+
}
416+
#endif
394417
#ifdef HAVE_OPENCL
395418
convolutionOp.release();
396419
#endif
@@ -1765,26 +1788,50 @@ class ConvolutionLayerImpl CV_FINAL : public BaseConvolutionLayerImpl
17651788
}
17661789

17671790
#ifdef HAVE_TENGINE
1768-
int inch = inputs[0].size[1]; // inch
1769-
int in_h = inputs[0].size[2]; // in_h
1770-
int in_w = inputs[0].size[3]; // in_w
1791+
bool tengine_ret = false; ;
1792+
1793+
std::vector<Mat> teng_in, teng_out;
1794+
inputs_arr.getMatVector(teng_in);
1795+
outputs_arr.getMatVector(teng_out);
1796+
1797+
int inch = teng_in[0].size[1]; // inch
1798+
int in_h = teng_in[0].size[2]; // in_h
1799+
int in_w = teng_in[0].size[3]; // in_w
17711800

1772-
int out_b = outputs[0].size[0]; // out batch size
1773-
int outch = outputs[0].size[1]; // outch
1774-
int out_h = outputs[0].size[2]; // out_h
1775-
int out_w = outputs[0].size[3]; // out_w
1801+
int out_b = teng_out[0].size[0]; // out batch size
1802+
int outch = teng_out[0].size[1]; // outch
1803+
int out_h = teng_out[0].size[2]; // out_h
1804+
int out_w = teng_out[0].size[3]; // out_w
17761805

1777-
float *input_ = inputs[0].ptr<float>();
1778-
float *output_ = outputs[0].ptr<float>();
1806+
float *input_ = teng_in[0].ptr<float>();
1807+
float *output_ = teng_out[0].ptr<float>();
17791808
float *kernel_ = weightsMat.ptr<float>();
17801809
float *teg_bias = &biasvec[0];
17811810

1782-
bool tengine_ret = tengine_forward(input_, inch, ngroups, in_h, in_w,
1783-
output_, out_b, outch, out_h, out_w,
1784-
kernel_, kernel_size.size(), kernel.height, kernel.width,
1785-
teg_bias, stride.height, stride.width,
1786-
pad.height, pad.width, dilation.height, dilation.width,
1787-
weightsMat.step1(), padMode);
1811+
int nstripes = std::max(getNumThreads(), 1);
1812+
1813+
/* tengine_init will run when first time. */
1814+
if(NULL == tengine_graph)
1815+
{
1816+
tengine_graph = tengine_init(name.c_str(), input_, inch, ngroups, in_h, in_w,
1817+
output_, out_b, outch, out_h, out_w,
1818+
kernel_, kernel_size.size(), kernel.height, kernel.width,
1819+
teg_bias, stride.height, stride.width,
1820+
pad.height, pad.width, dilation.height, dilation.width,
1821+
weightsMat.step1(), padMode, tengine_graph, nstripes);
1822+
/*printf("Init(%s): input=%p(%d %d %d %d ),output=%p(%d %d %d %d ),kernel=%p(%ld %d %d ), bias=%p ,"
1823+
"stride(%d %d), pad(%d %d), dilation(%d %d) ,weightsMat=%ld, padMode=%s ,tengine_graph = %p \n",
1824+
name.c_str(),input_, inch, ngroups, in_h, in_w,
1825+
output_, out_b, outch, out_h, out_w,
1826+
kernel_, kernel_size.size(), kernel.height, kernel.width,
1827+
teg_bias, stride.height, stride.width,
1828+
pad.height, pad.width, dilation.height, dilation.width,
1829+
weightsMat.step1(), padMode.c_str() ,tengine_graph);*/
1830+
}
1831+
if(NULL != tengine_graph)
1832+
{
1833+
tengine_ret = tengine_forward(tengine_graph);
1834+
}
17881835
/* activation */
17891836
if((true == tengine_ret) && activ )
17901837
{

modules/dnn/src/tengine4dnn/include/tengine_graph_convolution.hpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,24 @@
2626
#define TENGINE_GRAPH_CONVOLUTION_HPP
2727

2828
#define FLOAT_TO_REALSIZE (4)
29+
#ifdef HAVE_TENGINE
30+
31+
#include "tengine_c_api.h"
2932

3033
namespace cv
3134
{
3235
namespace dnn
3336
{
34-
bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
37+
teng_graph_t tengine_init(const char* name , float* input_, int inch, int group, int in_h, int in_w,
3538
float *output_, int out_b, int outch, int out_h, int out_w,
3639
float *kernel_,int kernel_s , int kernel_h, int kernel_w,
3740
float *teg_bias, int stride_h,int stride_w,
3841
int pad_h, int pad_w, int dilation_h, int dilation_w,
39-
size_t wstep, const std::string padMode) ;
42+
size_t wstep, const std::string padMode , teng_graph_t& graph, int nstripes) ;
43+
44+
bool tengine_forward(teng_graph_t& graph) ;
45+
bool tengine_release(teng_graph_t& graph) ;
4046
}
4147
}
42-
#endif /* TENGINE_GRAPH_CONVOLUTION_HPP */
48+
#endif
49+
#endif /* TENGINE_GRAPH_CONVOLUTION_HPP */

0 commit comments

Comments
 (0)