34
34
#ifdef HAVE_TENGINE
35
35
36
36
#include " tengine_c_api.h"
37
- #include " tengine_c_compat.h"
38
- #include " tengine_operations.h"
37
+
39
38
40
39
namespace cv
41
40
{
42
41
namespace dnn
43
42
{
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)
46
44
{
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);
50
48
51
49
int dims[4 ] = {1 , inch, in_h, in_w};
52
- set_tensor_shape (tensor, dims, 4 );
50
+ teng_set_tensor_shape (tensor, dims, 4 );
53
51
54
- release_graph_tensor (tensor);
55
- release_graph_node (node);
52
+ teng_release_graph_tensor (tensor);
53
+ teng_release_graph_node (node);
56
54
57
55
return 0 ;
58
56
}
59
57
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,
61
59
int kernel_h, int kernel_w, int stride_h, int stride_w, int pad_h, int pad_w, int inch, int outch, int group,
62
60
int dilation_h, int dilation_w, int activation, std::string padMode)
63
61
{
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);
66
64
67
65
if (input_tensor == NULL )
68
66
{
69
- CV_LOG_WARNING (NULL ," Tengine : input_tensor is NULL . " );
67
+ CV_LOG_WARNING (NULL ," Tengine: input_tensor is NULL. " );
70
68
return -1 ;
71
69
}
72
70
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);
75
73
76
74
/* 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);
78
76
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);
81
79
82
80
/* weight */
83
81
std::string weight_name (node_name);
84
82
weight_name += " /weight" ;
85
83
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);
90
88
int w_dims[] = {outch, inch / group, kernel_h, kernel_w};
91
89
92
- set_tensor_shape (w_tensor, w_dims, 4 );
90
+ teng_set_tensor_shape (w_tensor, w_dims, 4 );
93
91
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);
96
94
97
95
/* bias */
98
96
std::string bias_name (node_name);
99
97
bias_name += " /bias" ;
100
98
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);
104
102
int b_dims[] = {outch};
105
103
106
- set_tensor_shape (b_tensor, b_dims, 1 );
104
+ teng_set_tensor_shape (b_tensor, b_dims, 1 );
107
105
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);
111
109
112
110
int pad_h1 = pad_h;
113
111
int pad_w1 = pad_w;
@@ -127,31 +125,32 @@ int create_conv_node(graph_t graph, const char* node_name, const char* input_nam
127
125
}
128
126
129
127
/* 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);
145
144
146
145
return 0 ;
147
146
}
148
147
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,
151
150
int kernel_h, int kernel_w,
152
151
int stride_h,int stride_w,
153
152
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 )
155
154
{
156
155
node_t conv_node = NULL ;
157
156
@@ -170,123 +169,130 @@ graph_t create_conv_graph(float *input_data, int inch, int group, int in_h, int
170
169
int input_num = 0 ;
171
170
172
171
/* create graph */
173
- graph_t graph = create_graph (NULL , NULL , NULL );
172
+ teng_graph_t graph = teng_create_graph (NULL , NULL , NULL );
174
173
bool ok = true ;
175
174
176
175
if (graph == NULL )
177
176
{
178
- CV_LOG_WARNING (NULL ," Tengine : create_graph failed . " );
177
+ CV_LOG_WARNING (NULL ," Tengine: create_graph failed. " );
179
178
ok = false ;
180
179
}
181
180
182
181
const char * input_name = " data" ;
183
- const char * conv_name = " conv " ;
182
+ const char * conv_name = layer_name ;
184
183
185
184
if (ok && create_input_node (graph, input_name, inch, in_h, in_w) < 0 )
186
185
{
187
- CV_LOG_WARNING (NULL ," Tengine : create_input_node failed. " );
186
+ CV_LOG_WARNING (NULL ," Tengine: create_input_node failed." );
188
187
ok = false ;
189
188
}
190
189
191
190
if (ok && create_conv_node (graph, conv_name, input_name, in_h, in_w, out_h, out_w, kernel_h, kernel_w,
192
191
stride_h, stride_w, pad_h, pad_w, inch, outch, group, dilation_h, dilation_w, activation, padMode) < 0 )
193
192
{
194
- CV_LOG_WARNING (NULL ," Tengine : create conv node failed. " );
193
+ CV_LOG_WARNING (NULL ," Tengine: create conv node failed." );
195
194
ok = false ;
196
195
}
197
196
198
197
/* set input/output node */
199
198
const char * inputs_name[] = {input_name};
200
199
const char * outputs_name[] = {conv_name};
201
200
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 )
203
202
{
204
- CV_LOG_WARNING (NULL ," Tengine : set inputs failed . " );
203
+ CV_LOG_WARNING (NULL ," Tengine: set inputs failed. " );
205
204
ok = false ;
206
205
}
207
206
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 )
209
208
{
210
- CV_LOG_WARNING (NULL ," Tengine : set outputs failed . " );
209
+ CV_LOG_WARNING (NULL ," Tengine: set outputs failed. " );
211
210
ok = false ;
212
211
}
213
212
214
213
/* set input data */
215
214
if (ok)
216
215
{
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);
219
218
if (buf_size != in_size * FLOAT_TO_REALSIZE)
220
219
{
221
- CV_LOG_WARNING (NULL ," Tengine : Input data size check failed . " );
220
+ CV_LOG_WARNING (NULL ," Tengine: Input data size check failed. " );
222
221
ok = false ;
223
222
}
224
223
}
225
224
226
225
if (ok)
227
226
{
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);
230
229
231
230
/* create convolution node */
232
231
/* 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);
236
235
237
236
if (buf_size != weight_size * FLOAT_TO_REALSIZE)
238
237
{
239
- CV_LOG_WARNING (NULL ," Input weight size check failed . " );
238
+ CV_LOG_WARNING (NULL ," Tengine: Input weight size check failed. " );
240
239
ok = false ;
241
240
}
242
241
}
243
242
244
243
if (ok)
245
244
{
246
- set_tensor_buffer (weight_tensor, teg_weight, buf_size);
245
+ teng_set_tensor_buffer (weight_tensor, teg_weight, buf_size);
247
246
248
247
/* set bias node */
249
- input_num = get_node_input_number (conv_node);
248
+ input_num = teng_get_node_input_number (conv_node);
250
249
if (input_num > 2 )
251
250
{
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);
254
253
if (buf_size != bias_size * FLOAT_TO_REALSIZE)
255
254
{
256
- CV_LOG_WARNING (NULL ," Tengine : Input bias size check failed . " );
255
+ CV_LOG_WARNING (NULL ," Tengine: Input bias size check failed. " );
257
256
ok = false ;
258
257
}
259
- else set_tensor_buffer (bias_tensor, teg_bias, buf_size);
258
+ else teng_set_tensor_buffer (bias_tensor, teg_bias, buf_size);
260
259
}
261
260
}
262
261
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
+
263
269
if (ok)
264
270
{
265
271
/* 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);
268
274
if (ret)
269
275
{
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 ;
271
278
}
272
279
}
273
280
274
- if (! ok)
281
+ if (false == ok)
275
282
{
276
- destroy_graph (graph);
277
- return NULL ;
283
+ teng_destroy_graph (graph) ;
284
+ return NULL ;
278
285
}
279
286
return graph;
280
287
}
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,
283
290
float *output_, int out_b, int outch, int out_h, int out_w,
284
291
float *kernel_, int kernel_s ,int kernel_h, int kernel_w,
285
292
float *teg_bias, int stride_h,int stride_w,
286
293
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 )
288
295
{
289
- graph_t graph = NULL ;
290
296
std::vector<float > teg_weight_vec;
291
297
float *teg_weight = NULL ;
292
298
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,
296
302
if (!(kernel_s == 2 && kernel_h == kernel_w && pad_h == pad_w
297
303
&& dilation_h == dilation_w && stride_h == stride_w
298
304
&& out_b == 1 && pad_h < 10 )) // just for Conv2D
299
- return false ;
305
+ {
306
+ // printf("return : just for Conv2D\n");
307
+ return NULL ;
308
+ }
300
309
301
310
{
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,
305
314
kernel_w, kernel_h,
306
315
stride_w, stride_h,
307
316
dilation_w, dilation_h,
308
- pad_w,pad_h);*/
309
-
317
+ pad_w, pad_h);
318
+ */
310
319
// weight
311
320
if (kernel_inwh != wstep)
312
321
{
@@ -323,35 +332,42 @@ bool tengine_forward(float *input_, int inch, int group, int in_h, int in_w,
323
332
}
324
333
325
334
/* initial the resoruce of tengine */
326
- init_tengine ();
335
+ if (false == tengine_init_flag)
336
+ {
337
+ init_tengine ();
338
+ tengine_init_flag = true ;
339
+ }
327
340
328
341
/* 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,
330
343
output_, outch, out_h, out_w,
331
344
kernel_h, kernel_w, stride_h,stride_w,
332
345
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 )
337
348
{
338
- CV_LOG_WARNING (NULL , " Tengine :prerun_graph failed ." );
339
- return false ;
349
+ return NULL ;
340
350
}
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);
351
351
}
352
- return true ;
352
+ return graph ;
353
353
}
354
354
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
+ }
355
371
}
356
372
}
357
373
#endif
0 commit comments