Skip to content

Commit 3fc1487

Browse files
liqi-cliqi
and
liqi
authoredSep 23, 2020
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

+8-17
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,8 @@
2020
# Author: qtang@openailab.com or https://github.com/BUG1989
2121
# qli@openailab.com
2222
# sqfu@openailab.com
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

+1-8
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

+2
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

+62-15
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

+10-3
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 */

‎modules/dnn/src/tengine4dnn/src/tengine_graph_convolution.cpp

+126-110
Original file line numberDiff line numberDiff line change
@@ -34,80 +34,78 @@
3434
#ifdef HAVE_TENGINE
3535

3636
#include "tengine_c_api.h"
37-
#include "tengine_c_compat.h"
38-
#include "tengine_operations.h"
37+
3938

4039
namespace cv
4140
{
4241
namespace dnn
4342
{
44-
45-
int create_input_node(graph_t graph, const char* node_name, int inch, int in_h, int in_w)
43+
static int create_input_node(teng_graph_t graph, const char* node_name, int inch, int in_h, int in_w)
4644
{
47-
node_t node = create_graph_node(graph, node_name, "InputOp");
48-
tensor_t tensor = create_graph_tensor(graph, node_name, TENGINE_DT_FP32);
49-
set_node_output_tensor(node, 0, tensor, TENSOR_TYPE_INPUT);
45+
node_t node = teng_create_graph_node(graph, node_name, "InputOp");
46+
tensor_t tensor = teng_create_graph_tensor(graph, node_name, TENGINE_DT_FP32);
47+
teng_set_node_output_tensor(node, 0, tensor, TENSOR_TYPE_INPUT);
5048

5149
int dims[4] = {1, inch, in_h, in_w};
52-
set_tensor_shape(tensor, dims, 4);
50+
teng_set_tensor_shape(tensor, dims, 4);
5351

54-
release_graph_tensor(tensor);
55-
release_graph_node(node);
52+
teng_release_graph_tensor(tensor);
53+
teng_release_graph_node(node);
5654

5755
return 0;
5856
}
5957

60-
int create_conv_node(graph_t graph, const char* node_name, const char* input_name, int in_h, int in_w, int out_h, int out_w,
58+
static int create_conv_node(teng_graph_t graph, const char* node_name, const char* input_name, int in_h, int in_w, int out_h, int out_w,
6159
int kernel_h, int kernel_w, int stride_h, int stride_w, int pad_h, int pad_w, int inch, int outch, int group,
6260
int dilation_h, int dilation_w, int activation, std::string padMode)
6361
{
64-
node_t conv_node = create_graph_node(graph, node_name, "Convolution");
65-
tensor_t input_tensor = get_graph_tensor(graph, input_name);
62+
node_t conv_node = teng_create_graph_node(graph, node_name, "Convolution");
63+
tensor_t input_tensor = teng_get_graph_tensor(graph, input_name);
6664

6765
if (input_tensor == NULL)
6866
{
69-
CV_LOG_WARNING(NULL,"Tengine :input_tensor is NULL . " );
67+
CV_LOG_WARNING(NULL,"Tengine: input_tensor is NULL." );
7068
return -1;
7169
}
7270

73-
set_node_input_tensor(conv_node, 0, input_tensor);
74-
release_graph_tensor(input_tensor);
71+
teng_set_node_input_tensor(conv_node, 0, input_tensor);
72+
teng_release_graph_tensor(input_tensor);
7573

7674
/* output */
77-
tensor_t output_tensor = create_graph_tensor(graph, node_name, TENGINE_DT_FP32);
75+
tensor_t output_tensor = teng_create_graph_tensor(graph, node_name, TENGINE_DT_FP32);
7876

79-
set_node_output_tensor(conv_node, 0, output_tensor, TENSOR_TYPE_VAR);
80-
release_graph_tensor(output_tensor);
77+
teng_set_node_output_tensor(conv_node, 0, output_tensor, TENSOR_TYPE_VAR);
78+
teng_release_graph_tensor(output_tensor);
8179

8280
/* weight */
8381
std::string weight_name(node_name);
8482
weight_name += "/weight";
8583

86-
node_t w_node = create_graph_node(graph, weight_name.c_str(), "Const");
87-
tensor_t w_tensor = create_graph_tensor(graph, weight_name.c_str(), TENGINE_DT_FP32);
88-
set_node_output_tensor(w_node, 0, w_tensor, TENSOR_TYPE_CONST);
89-
set_node_input_tensor(conv_node, 1, w_tensor);
84+
node_t w_node = teng_create_graph_node(graph, weight_name.c_str(), "Const");
85+
tensor_t w_tensor = teng_create_graph_tensor(graph, weight_name.c_str(), TENGINE_DT_FP32);
86+
teng_set_node_output_tensor(w_node, 0, w_tensor, TENSOR_TYPE_CONST);
87+
teng_set_node_input_tensor(conv_node, 1, w_tensor);
9088
int w_dims[] = {outch, inch / group, kernel_h, kernel_w};
9189

92-
set_tensor_shape(w_tensor, w_dims, 4);
90+
teng_set_tensor_shape(w_tensor, w_dims, 4);
9391

94-
release_graph_node(w_node);
95-
release_graph_tensor(w_tensor);
92+
teng_release_graph_node(w_node);
93+
teng_release_graph_tensor(w_tensor);
9694

9795
/* bias */
9896
std::string bias_name(node_name);
9997
bias_name += "/bias";
10098

101-
node_t b_node = create_graph_node(graph, bias_name.c_str(), "Const");
102-
tensor_t b_tensor = create_graph_tensor(graph, bias_name.c_str(), TENGINE_DT_FP32);
103-
set_node_output_tensor(b_node, 0, b_tensor, TENSOR_TYPE_CONST);
99+
node_t b_node = teng_create_graph_node(graph, bias_name.c_str(), "Const");
100+
tensor_t b_tensor = teng_create_graph_tensor(graph, bias_name.c_str(), TENGINE_DT_FP32);
101+
teng_set_node_output_tensor(b_node, 0, b_tensor, TENSOR_TYPE_CONST);
104102
int b_dims[] = {outch};
105103

106-
set_tensor_shape(b_tensor, b_dims, 1);
104+
teng_set_tensor_shape(b_tensor, b_dims, 1);
107105

108-
set_node_input_tensor(conv_node, 2, b_tensor);
109-
release_graph_node(b_node);
110-
release_graph_tensor(b_tensor);
106+
teng_set_node_input_tensor(conv_node, 2, b_tensor);
107+
teng_release_graph_node(b_node);
108+
teng_release_graph_tensor(b_tensor);
111109

112110
int pad_h1 = pad_h;
113111
int pad_w1 = pad_w;
@@ -127,31 +125,32 @@ int create_conv_node(graph_t graph, const char* node_name, const char* input_nam
127125
}
128126

129127
/* attr */
130-
set_node_attr_int(conv_node, "kernel_h", &kernel_h);
131-
set_node_attr_int(conv_node, "kernel_w", &kernel_w);
132-
set_node_attr_int(conv_node, "stride_h", &stride_h);
133-
set_node_attr_int(conv_node, "stride_w", &stride_w);
134-
set_node_attr_int(conv_node, "pad_h0", &pad_h);
135-
set_node_attr_int(conv_node, "pad_w0", &pad_w);
136-
set_node_attr_int(conv_node, "pad_h1", &pad_h1);
137-
set_node_attr_int(conv_node, "pad_w1", &pad_w1);
138-
set_node_attr_int(conv_node, "output_channel", &outch);
139-
set_node_attr_int(conv_node, "group", &group);
140-
set_node_attr_int(conv_node, "dilation_h", &dilation_h);
141-
set_node_attr_int(conv_node, "dilation_w", &dilation_w);
142-
set_node_attr_int(conv_node, "activation", &activation);
143-
144-
release_graph_node(conv_node);
128+
teng_set_node_attr_int(conv_node, "kernel_h", &kernel_h);
129+
teng_set_node_attr_int(conv_node, "kernel_w", &kernel_w);
130+
teng_set_node_attr_int(conv_node, "stride_h", &stride_h);
131+
teng_set_node_attr_int(conv_node, "stride_w", &stride_w);
132+
teng_set_node_attr_int(conv_node, "pad_h0", &pad_h);
133+
teng_set_node_attr_int(conv_node, "pad_w0", &pad_w);
134+
teng_set_node_attr_int(conv_node, "pad_h1", &pad_h1);
135+
teng_set_node_attr_int(conv_node, "pad_w1", &pad_w1);
136+
teng_set_node_attr_int(conv_node, "output_channel", &outch);
137+
teng_set_node_attr_int(conv_node, "input_channel", &inch);
138+
teng_set_node_attr_int(conv_node, "group", &group);
139+
teng_set_node_attr_int(conv_node, "dilation_h", &dilation_h);
140+
teng_set_node_attr_int(conv_node, "dilation_w", &dilation_w);
141+
// set_node_attr_int(conv_node, "activation", &activation);
142+
143+
teng_release_graph_node(conv_node);
145144

146145
return 0;
147146
}
148147

149-
graph_t create_conv_graph(float *input_data, int inch, int group, int in_h, int in_w,
150-
float *output_data, int outch, int out_h, int out_w,
148+
static teng_graph_t create_conv_graph(const char* layer_name, float* input_data, int inch, int group, int in_h, int in_w,
149+
float* output_data, int outch, int out_h, int out_w,
151150
int kernel_h, int kernel_w,
152151
int stride_h,int stride_w,
153152
int pad_h, int pad_w, int dilation_h, int dilation_w, int activation,
154-
float * teg_weight , float * teg_bias , std::string padMode)
153+
float* teg_weight, float* teg_bias, std::string padMode, int nstripes)
155154
{
156155
node_t conv_node = NULL;
157156

@@ -170,123 +169,130 @@ graph_t create_conv_graph(float *input_data, int inch, int group, int in_h, int
170169
int input_num = 0;
171170

172171
/* create graph */
173-
graph_t graph = create_graph(NULL, NULL, NULL);
172+
teng_graph_t graph = teng_create_graph(NULL, NULL, NULL);
174173
bool ok = true;
175174

176175
if(graph == NULL)
177176
{
178-
CV_LOG_WARNING(NULL,"Tengine :create_graph failed . " );
177+
CV_LOG_WARNING(NULL,"Tengine: create_graph failed." );
179178
ok = false;
180179
}
181180

182181
const char* input_name = "data";
183-
const char* conv_name = "conv";
182+
const char* conv_name = layer_name;
184183

185184
if (ok && create_input_node(graph, input_name, inch, in_h, in_w) < 0)
186185
{
187-
CV_LOG_WARNING(NULL,"Tengine :create_input_node failed. " );
186+
CV_LOG_WARNING(NULL,"Tengine: create_input_node failed." );
188187
ok = false;
189188
}
190189

191190
if (ok && create_conv_node(graph, conv_name, input_name, in_h, in_w, out_h, out_w, kernel_h, kernel_w,
192191
stride_h, stride_w, pad_h, pad_w, inch, outch, group, dilation_h, dilation_w, activation, padMode) < 0)
193192
{
194-
CV_LOG_WARNING(NULL,"Tengine :create conv node failed. " );
193+
CV_LOG_WARNING(NULL,"Tengine: create conv node failed." );
195194
ok = false;
196195
}
197196

198197
/* set input/output node */
199198
const char* inputs_name[] = {input_name};
200199
const char* outputs_name[] = {conv_name};
201200

202-
if (ok && set_graph_input_node(graph, inputs_name, sizeof(inputs_name) / sizeof(char*)) < 0)
201+
if (ok && teng_set_graph_input_node(graph, inputs_name, sizeof(inputs_name) / sizeof(char*)) < 0)
203202
{
204-
CV_LOG_WARNING(NULL,"Tengine :set inputs failed . " );
203+
CV_LOG_WARNING(NULL,"Tengine: set inputs failed." );
205204
ok = false;
206205
}
207206

208-
if (ok && set_graph_output_node(graph, outputs_name, sizeof(outputs_name) / sizeof(char*)) < 0)
207+
if (ok && teng_set_graph_output_node(graph, outputs_name, sizeof(outputs_name) / sizeof(char*)) < 0)
209208
{
210-
CV_LOG_WARNING(NULL,"Tengine :set outputs failed . " );
209+
CV_LOG_WARNING(NULL,"Tengine: set outputs failed." );
211210
ok = false;
212211
}
213212

214213
/* set input data */
215214
if (ok)
216215
{
217-
input_tensor = get_graph_input_tensor(graph, 0, 0);
218-
buf_size = get_tensor_buffer_size(input_tensor);
216+
input_tensor = teng_get_graph_input_tensor(graph, 0, 0);
217+
buf_size = teng_get_tensor_buffer_size(input_tensor);
219218
if (buf_size != in_size * FLOAT_TO_REALSIZE)
220219
{
221-
CV_LOG_WARNING(NULL,"Tengine :Input data size check failed . ");
220+
CV_LOG_WARNING(NULL,"Tengine: Input data size check failed.");
222221
ok = false;
223222
}
224223
}
225224

226225
if (ok)
227226
{
228-
set_tensor_buffer(input_tensor, (float *)input_data, buf_size);
229-
release_graph_tensor(input_tensor);
227+
teng_set_tensor_buffer(input_tensor, (float *)input_data, buf_size);
228+
teng_release_graph_tensor(input_tensor);
230229

231230
/* create convolution node */
232231
/* set weight node */
233-
conv_node = get_graph_node(graph, "conv");
234-
weight_tensor = get_node_input_tensor(conv_node, 1);
235-
buf_size = get_tensor_buffer_size(weight_tensor);
232+
conv_node = teng_get_graph_node(graph, conv_name);
233+
weight_tensor = teng_get_node_input_tensor(conv_node, 1);
234+
buf_size = teng_get_tensor_buffer_size(weight_tensor);
236235

237236
if (buf_size != weight_size * FLOAT_TO_REALSIZE)
238237
{
239-
CV_LOG_WARNING(NULL,"Input weight size check failed . ");
238+
CV_LOG_WARNING(NULL,"Tengine: Input weight size check failed.");
240239
ok = false;
241240
}
242241
}
243242

244243
if (ok)
245244
{
246-
set_tensor_buffer(weight_tensor, teg_weight, buf_size);
245+
teng_set_tensor_buffer(weight_tensor, teg_weight, buf_size);
247246

248247
/* set bias node */
249-
input_num = get_node_input_number(conv_node);
248+
input_num = teng_get_node_input_number(conv_node);
250249
if (input_num > 2)
251250
{
252-
bias_tensor = get_node_input_tensor(conv_node, 2);
253-
buf_size = get_tensor_buffer_size(bias_tensor);
251+
bias_tensor = teng_get_node_input_tensor(conv_node, 2);
252+
buf_size = teng_get_tensor_buffer_size(bias_tensor);
254253
if (buf_size != bias_size * FLOAT_TO_REALSIZE)
255254
{
256-
CV_LOG_WARNING(NULL,"Tengine :Input bias size check failed . ");
255+
CV_LOG_WARNING(NULL,"Tengine: Input bias size check failed.");
257256
ok = false;
258257
}
259-
else set_tensor_buffer(bias_tensor, teg_bias, buf_size);
258+
else teng_set_tensor_buffer(bias_tensor, teg_bias, buf_size);
260259
}
261260
}
262261

262+
/* prerun */
263+
if (ok && teng_prerun_graph_multithread(graph, TENGINE_CLUSTER_BIG, nstripes) < 0)
264+
{
265+
CV_LOG_WARNING(NULL, "Tengine: prerun_graph failed.");
266+
ok = false;
267+
}
268+
263269
if (ok)
264270
{
265271
/* set output data */
266-
output_tensor = get_node_output_tensor(conv_node, 0);
267-
int ret = set_tensor_buffer(output_tensor, output_data, out_size * FLOAT_TO_REALSIZE);
272+
output_tensor = teng_get_node_output_tensor(conv_node, 0);
273+
int ret = teng_set_tensor_buffer(output_tensor, output_data, out_size * FLOAT_TO_REALSIZE);
268274
if(ret)
269275
{
270-
CV_LOG_WARNING(NULL,"Tengine :Set output tensor buffer failed . " );
276+
CV_LOG_WARNING(NULL,"Tengine: Set output tensor buffer failed." );
277+
ok = false;
271278
}
272279
}
273280

274-
if (!ok)
281+
if (false == ok)
275282
{
276-
destroy_graph(graph);
277-
return NULL;
283+
teng_destroy_graph(graph) ;
284+
return NULL ;
278285
}
279286
return graph;
280287
}
281-
282-
bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
288+
static bool tengine_init_flag = false;
289+
teng_graph_t tengine_init(const char* layer_name, float* input_, int inch, int group, int in_h, int in_w,
283290
float *output_, int out_b, int outch, int out_h, int out_w,
284291
float *kernel_, int kernel_s ,int kernel_h, int kernel_w,
285292
float *teg_bias, int stride_h,int stride_w,
286293
int pad_h, int pad_w, int dilation_h, int dilation_w,
287-
size_t wstep,const std::string padMode)
294+
size_t wstep, const std::string padMode, teng_graph_t &graph, int nstripes)
288295
{
289-
graph_t graph = NULL;
290296
std::vector<float> teg_weight_vec;
291297
float *teg_weight = NULL;
292298
int kernel_inwh = (inch / group) * kernel_w * kernel_h;
@@ -296,17 +302,20 @@ bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
296302
if (!(kernel_s == 2 && kernel_h == kernel_w && pad_h == pad_w
297303
&& dilation_h == dilation_w && stride_h == stride_w
298304
&& out_b == 1 && pad_h < 10)) // just for Conv2D
299-
return false;
305+
{
306+
// printf("return : just for Conv2D\n");
307+
return NULL;
308+
}
300309

301310
{
302-
/*printf("Tengine: input (1 x %d x %d x %d),output (%d x %d x %d x %d), kernel (%d x %d), stride (%d x %d), dilation (%d x %d), pad (%d x %d).\n",
303-
inch, in_h, in_w,
304-
out_b,outch,out_h,out_w,
311+
/* printf("Tengine(%s): input (1 x %d x %d x %d),output (%d x %d x %d x %d), kernel (%d x %d), stride (%d x %d), dilation (%d x %d), pad (%d x %d).\n",
312+
layer_name, inch, in_h, in_w,
313+
out_b, outch, out_h, out_w,
305314
kernel_w, kernel_h,
306315
stride_w, stride_h,
307316
dilation_w, dilation_h,
308-
pad_w,pad_h);*/
309-
317+
pad_w, pad_h);
318+
*/
310319
// weight
311320
if (kernel_inwh != wstep)
312321
{
@@ -323,35 +332,42 @@ bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
323332
}
324333

325334
/* initial the resoruce of tengine */
326-
init_tengine();
335+
if(false == tengine_init_flag)
336+
{
337+
init_tengine();
338+
tengine_init_flag = true;
339+
}
327340

328341
/* create the convolution graph */
329-
graph = create_conv_graph( input_, inch, group, in_h, in_w,
342+
graph = create_conv_graph(layer_name, input_, inch, group, in_h, in_w,
330343
output_, outch, out_h, out_w,
331344
kernel_h, kernel_w, stride_h,stride_w,
332345
pad_h, pad_w, dilation_h, dilation_w, activation,
333-
teg_weight , teg_bias , padMode);
334-
335-
/* prerun */
336-
if(prerun_graph(graph) < 0)
346+
teg_weight, teg_bias, padMode, nstripes);
347+
if(NULL == graph )
337348
{
338-
CV_LOG_WARNING(NULL, "Tengine :prerun_graph failed .");
339-
return false ;
349+
return NULL;
340350
}
341-
342-
/* run */
343-
if(run_graph(graph, 1) < 0)
344-
{
345-
CV_LOG_WARNING(NULL,"Tengine :run_graph failed .");
346-
return false ;
347-
}
348-
349-
postrun_graph(graph);
350-
destroy_graph(graph);
351351
}
352-
return true ;
352+
return graph ;
353353
}
354354

355+
bool tengine_forward(teng_graph_t &graph)
356+
{
357+
/* run */
358+
if(teng_run_graph(graph, 1) < 0)
359+
{
360+
CV_LOG_WARNING(NULL,"Tengine: run_graph failed.");
361+
return false ;
362+
}
363+
return true;
364+
}
365+
bool tengine_release(teng_graph_t &graph)
366+
{
367+
teng_postrun_graph(graph);
368+
teng_destroy_graph(graph);
369+
return true;
370+
}
355371
}
356372
}
357373
#endif

0 commit comments

Comments
 (0)
Please sign in to comment.