History

libavfilter/dnn: add batch mode for async execution

the default number of batch_size is 1

Guo, Yejun committed 5 years ago

Commit 64ea15f1 parent 57dae575 files · +196 −35Browse files

Unified · Split

5 files
  1. libavfilter/dnn/dnn_backend_openvino.c +155 −32
  2. libavfilter/dnn/dnn_backend_openvino.h +1 −0
  3. libavfilter/dnn/dnn_interface.c +1 −0
  4. libavfilter/dnn_interface.h +2 −0
  5. libavfilter/vf_dnn_processing.c +37 −3
libavfilter/dnn/dnn_backend_openvino.c+155−32
@@ -37,6 +37,7 @@ · 36 unmodified lines
3737 typedef struct OVOptions{
3838 char *device_type;
3939 int nireq;
40+ int batch_size;
4041 } OVOptions;
4142
4243 typedef struct OVContext {
@@ -70,7 +71,8 @@ typedef struct TaskItem { · 27 unmodified lines
7071
7172 typedef struct RequestItem {
7273 ie_infer_request_t *infer_request;
73− TaskItem *task;
74+ TaskItem **tasks;
75+ int task_count;
7476 ie_complete_call_back_t callback;
7577 } RequestItem;
7678
@@ -83,6 +85,7 @@ typedef struct RequestItem { · 6 unmodified lines
8385 static const AVOption dnn_openvino_options[] = {
8486 { "device", "device to run model", OFFSET(options.device_type), AV_OPT_TYPE_STRING, { .str = "CPU" }, 0, 0, FLAGS },
8587 { "nireq", "number of request", OFFSET(options.nireq), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, INT_MAX, FLAGS },
88+ { "batch_size", "batch size per request", OFFSET(options.batch_size), AV_OPT_TYPE_INT, { .i64 = 1 }, 1, 1000, FLAGS},
8689 { NULL }
8790 };
8891
@@ -100,7 +103,19 @@ static DNNDataType precision_to_datatype(precision_e precision) · 11 unmodified lines
100103 }
101104 }
102105
103−static DNNReturnType fill_model_input_ov(OVModel *ov_model, TaskItem *task, RequestItem *request)
106+static int get_datatype_size(DNNDataType dt)
107+{
108+ switch (dt)
109+ {
110+ case DNN_FLOAT:
111+ return sizeof(float);
112+ default:
113+ av_assert0(!"not supported yet.");
114+ return 1;
115+ }
116+}
117+
118+static DNNReturnType fill_model_input_ov(OVModel *ov_model, RequestItem *request)
104119 {
105120 dimensions_t dims;
106121 precision_e precision;
@@ -109,6 +124,7 @@ static DNNReturnType fill_model_input_ov(OVModel *ov_model, TaskItem *task, Requ · 2 unmodified lines
109124 IEStatusCode status;
110125 DNNData input;
111126 ie_blob_t *input_blob = NULL;
127+ TaskItem *task = request->tasks[0];
112128
113129 status = ie_infer_request_get_blob(request->infer_request, task->input_name, &input_blob);
114130 if (status != OK) {
@@ -134,12 +150,19 @@ static DNNReturnType fill_model_input_ov(OVModel *ov_model, TaskItem *task, Requ · 19 unmodified lines
134150 input.channels = dims.dims[1];
135151 input.data = blob_buffer.buffer;
136152 input.dt = precision_to_datatype(precision);
137− if (task->do_ioproc) {
138− if (ov_model->model->pre_proc != NULL) {
139− ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
140− } else {
141− proc_from_frame_to_dnn(task->in_frame, &input, ctx);
153+
154+ av_assert0(request->task_count <= dims.dims[0]);
155+ for (int i = 0; i < request->task_count; ++i) {
156+ task = request->tasks[i];
157+ if (task->do_ioproc) {
158+ if (ov_model->model->pre_proc != NULL) {
159+ ov_model->model->pre_proc(task->in_frame, &input, ov_model->model->filter_ctx);
160+ } else {
161+ proc_from_frame_to_dnn(task->in_frame, &input, ctx);
162+ }
142163 }
164+ input.data = (uint8_t *)input.data
165+ + input.width * input.height * input.channels * get_datatype_size(input.dt);
143166 }
144167 ie_blob_free(&input_blob);
145168
@@ -152,7 +175,7 @@ static void infer_completion_callback(void *args) · 6 unmodified lines
152175 precision_e precision;
153176 IEStatusCode status;
154177 RequestItem *request = args;
155− TaskItem *task = request->task;
178+ TaskItem *task = request->tasks[0];
156179 ie_blob_t *output_blob = NULL;
157180 ie_blob_buffer_t blob_buffer;
158181 DNNData output;
@@ -194,41 +217,56 @@ static void infer_completion_callback(void *args) · 35 unmodified lines
194217 output.width = dims.dims[3];
195218 output.dt = precision_to_datatype(precision);
196219 output.data = blob_buffer.buffer;
197− if (task->do_ioproc) {
198− if (task->ov_model->model->post_proc != NULL) {
199− task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
220+
221+ av_assert0(request->task_count <= dims.dims[0]);
222+ av_assert0(request->task_count >= 1);
223+ for (int i = 0; i < request->task_count; ++i) {
224+ task = request->tasks[i];
225+ if (task->do_ioproc) {
226+ if (task->ov_model->model->post_proc != NULL) {
227+ task->ov_model->model->post_proc(task->out_frame, &output, task->ov_model->model->filter_ctx);
228+ } else {
229+ proc_from_dnn_to_frame(task->out_frame, &output, ctx);
230+ }
200231 } else {
201− proc_from_dnn_to_frame(task->out_frame, &output, ctx);
232+ task->out_frame->width = output.width;
233+ task->out_frame->height = output.height;
202234 }
203− } else {
204− task->out_frame->width = output.width;
205− task->out_frame->height = output.height;
235+ task->done = 1;
236+ output.data = (uint8_t *)output.data
237+ + output.width * output.height * output.channels * get_datatype_size(output.dt);
206238 }
207239 ie_blob_free(&output_blob);
208240
241+ request->task_count = 0;
242+
209243 if (task->async) {
210− request->task = NULL;
211244 if (ff_safe_queue_push_back(task->ov_model->request_queue, request) < 0) {
212245 av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
213246 return;
214247 }
215248 }
216−
217− task->done = 1;
218249 }
219250
220−static DNNReturnType execute_model_ov(TaskItem *task, RequestItem *request)
251+static DNNReturnType execute_model_ov(RequestItem *request)
221252 {
222253 IEStatusCode status;
254+ DNNReturnType ret;
255+ TaskItem *task = request->tasks[0];
223256 OVContext *ctx = &task->ov_model->ctx;
224257
225− DNNReturnType ret = fill_model_input_ov(task->ov_model, task, request);
226− if (ret != DNN_SUCCESS) {
227− return ret;
228− }
229−
230258 if (task->async) {
231− request->task = task;
259+ if (request->task_count < ctx->options.batch_size) {
260+ if (ff_safe_queue_push_front(task->ov_model->request_queue, request) < 0) {
261+ av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
262+ return DNN_ERROR;
263+ }
264+ return DNN_SUCCESS;
265+ }
266+ ret = fill_model_input_ov(task->ov_model, request);
267+ if (ret != DNN_SUCCESS) {
268+ return ret;
269+ }
232270 status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
233271 if (status != OK) {
234272 av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
@@ -241,12 +279,15 @@ static DNNReturnType execute_model_ov(TaskItem *task, RequestItem *request) · 6 unmodified lines
241279 }
242280 return DNN_SUCCESS;
243281 } else {
282+ ret = fill_model_input_ov(task->ov_model, request);
283+ if (ret != DNN_SUCCESS) {
284+ return ret;
285+ }
244286 status = ie_infer_request_infer(request->infer_request);
245287 if (status != OK) {
246288 av_log(ctx, AV_LOG_ERROR, "Failed to start synchronous model inference\n");
247289 return DNN_ERROR;
248290 }
249− request->task = task;
250291 infer_completion_callback(request);
251292 return task->done ? DNN_SUCCESS : DNN_ERROR;
252293 }
@@ -319,6 +360,7 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 66 unmodified lines
319360 RequestItem request;
320361 AVFrame *in_frame = av_frame_alloc();
321362 AVFrame *out_frame = NULL;
363+ TaskItem *ptask = &task;
322364
323365 if (!in_frame) {
324366 av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for input frame\n");
@@ -343,8 +385,10 @@ static DNNReturnType get_output_ov(void *model, const char *input_name, int inpu · 18 unmodified lines
343385 task.ov_model = ov_model;
344386
345387 request.infer_request = ov_model->infer_request;
388+ request.task_count = 1;
389+ request.tasks = &ptask;
346390
347− ret = execute_model_ov(&task, &request);
391+ ret = execute_model_ov(&request);
348392 *output_width = out_frame->width;
349393 *output_height = out_frame->height;
350394
@@ -393,6 +437,24 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, · 42 unmodified lines
393437 if (status != OK)
394438 goto err;
395439
440+ // batch size
441+ if (ctx->options.batch_size <= 0) {
442+ ctx->options.batch_size = 1;
443+ }
444+
445+ if (ctx->options.batch_size > 1) {
446+ input_shapes_t input_shapes;
447+ status = ie_network_get_input_shapes(ov_model->network, &input_shapes);
448+ if (status != OK)
449+ goto err;
450+ for (int i = 0; i < input_shapes.shape_num; i++)
451+ input_shapes.shapes[i].shape.dims[0] = ctx->options.batch_size;
452+ status = ie_network_reshape(ov_model->network, input_shapes);
453+ ie_network_input_shapes_free(&input_shapes);
454+ if (status != OK)
455+ goto err;
456+ }
457+
396458 status = ie_core_load_network(ov_model->core, ov_model->network, ctx->options.device_type, &config, &ov_model->exe_network);
397459 if (status != OK) {
398460 av_log(ctx, AV_LOG_ERROR, "Failed to init OpenVINO model\n");
@@ -426,17 +488,24 @@ DNNModel *ff_dnn_load_model_ov(const char *model_filename, const char *options, · 27 unmodified lines
426488 }
427489
428490 for (int i = 0; i < ctx->options.nireq; i++) {
429− ie_infer_request_t *request;
430491 RequestItem *item = av_mallocz(sizeof(*item));
431492 if (!item) {
432493 goto err;
433494 }
434− status = ie_exec_network_create_infer_request(ov_model->exe_network, &request);
495+
496+ status = ie_exec_network_create_infer_request(ov_model->exe_network, &item->infer_request);
435497 if (status != OK) {
436498 av_freep(&item);
437499 goto err;
438500 }
439− item->infer_request = request;
501+
502+ item->tasks = av_malloc_array(ctx->options.batch_size, sizeof(*item->tasks));
503+ if (!item->tasks) {
504+ av_freep(&item);
505+ goto err;
506+ }
507+ item->task_count = 0;
508+
440509 item->callback.completeCallBackFunc = infer_completion_callback;
441510 item->callback.args = item;
442511 if (ff_safe_queue_push_back(ov_model->request_queue, item) < 0) {
@@ -469,6 +538,7 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 26 unmodified lines
469538 OVContext *ctx = &ov_model->ctx;
470539 TaskItem task;
471540 RequestItem request;
541+ TaskItem *ptask = &task;
472542
473543 if (!in_frame) {
474544 av_log(ctx, AV_LOG_ERROR, "in frame is NULL when execute model.\n");
@@ -487,6 +557,11 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 12 unmodified lines
487557 return DNN_ERROR;
488558 }
489559
560+ if (ctx->options.batch_size > 1) {
561+ av_log(ctx, AV_LOG_ERROR, "do not support batch mode for sync execution.\n");
562+ return DNN_ERROR;
563+ }
564+
490565 task.done = 0;
491566 task.do_ioproc = 1;
492567 task.async = 0;
@@ -497,8 +572,10 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 4 unmodified lines
497572 task.ov_model = ov_model;
498573
499574 request.infer_request = ov_model->infer_request;
575+ request.task_count = 1;
576+ request.tasks = &ptask;
500577
501− return execute_model_ov(&task, &request);
578+ return execute_model_ov(&request);
502579 }
503580
504581 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
@@ -545,7 +622,8 @@ DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *i · 40 unmodified lines
545622 return DNN_ERROR;
546623 }
547624
548− return execute_model_ov(task, request);
625+ request->tasks[request->task_count++] = task;
626+ return execute_model_ov(request);
549627 }
550628
551629 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out)
@@ -569,6 +647,48 @@ DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **i · 17 unmodified lines
569647 return DAST_SUCCESS;
570648 }
571649
650+DNNReturnType ff_dnn_flush_ov(const DNNModel *model)
651+{
652+ OVModel *ov_model = (OVModel *)model->model;
653+ OVContext *ctx = &ov_model->ctx;
654+ RequestItem *request;
655+ IEStatusCode status;
656+ DNNReturnType ret;
657+
658+ request = ff_safe_queue_pop_front(ov_model->request_queue);
659+ if (!request) {
660+ av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
661+ return DNN_ERROR;
662+ }
663+
664+ if (request->task_count == 0) {
665+ // no pending task need to flush
666+ if (ff_safe_queue_push_back(ov_model->request_queue, request) < 0) {
667+ av_log(ctx, AV_LOG_ERROR, "Failed to push back request_queue.\n");
668+ return DNN_ERROR;
669+ }
670+ return DNN_SUCCESS;
671+ }
672+
673+ ret = fill_model_input_ov(ov_model, request);
674+ if (ret != DNN_SUCCESS) {
675+ av_log(ctx, AV_LOG_ERROR, "Failed to fill model input.\n");
676+ return ret;
677+ }
678+ status = ie_infer_set_completion_callback(request->infer_request, &request->callback);
679+ if (status != OK) {
680+ av_log(ctx, AV_LOG_ERROR, "Failed to set completion callback for inference\n");
681+ return DNN_ERROR;
682+ }
683+ status = ie_infer_request_infer_async(request->infer_request);
684+ if (status != OK) {
685+ av_log(ctx, AV_LOG_ERROR, "Failed to start async inference\n");
686+ return DNN_ERROR;
687+ }
688+
689+ return DNN_SUCCESS;
690+}
691+
572692 void ff_dnn_free_model_ov(DNNModel **model)
573693 {
574694 if (*model){
@@ -578,12 +698,15 @@ void ff_dnn_free_model_ov(DNNModel **model) · 3 unmodified lines
578698 if (item && item->infer_request) {
579699 ie_infer_request_free(&item->infer_request);
580700 }
701+ av_freep(&item->tasks);
581702 av_freep(&item);
582703 }
583704 ff_safe_queue_destroy(ov_model->request_queue);
584705
585706 while (ff_queue_size(ov_model->task_queue) != 0) {
586707 TaskItem *item = ff_queue_pop_front(ov_model->task_queue);
708+ av_frame_free(&item->in_frame);
709+ av_frame_free(&item->out_frame);
587710 av_freep(&item);
588711 }
589712 ff_queue_destroy(ov_model->task_queue);
libavfilter/dnn/dnn_backend_openvino.h+1−0
@@ -36,6 +36,7 @@ DNNReturnType ff_dnn_execute_model_ov(const DNNModel *model, const char *input_n · 35 unmodified lines
3636 DNNReturnType ff_dnn_execute_model_async_ov(const DNNModel *model, const char *input_name, AVFrame *in_frame,
3737 const char **output_names, uint32_t nb_output, AVFrame *out_frame);
3838 DNNAsyncStatusType ff_dnn_get_async_result_ov(const DNNModel *model, AVFrame **in, AVFrame **out);
39+DNNReturnType ff_dnn_flush_ov(const DNNModel *model);
3940
4041 void ff_dnn_free_model_ov(DNNModel **model);
4142
libavfilter/dnn/dnn_interface.c+1−0
@@ -60,6 +60,7 @@ DNNModule *ff_get_dnn_module(DNNBackendType backend_type) · 59 unmodified lines
6060 dnn_module->execute_model = &ff_dnn_execute_model_ov;
6161 dnn_module->execute_model_async = &ff_dnn_execute_model_async_ov;
6262 dnn_module->get_async_result = &ff_dnn_get_async_result_ov;
63+ dnn_module->flush = &ff_dnn_flush_ov;
6364 dnn_module->free_model = &ff_dnn_free_model_ov;
6465 #else
6566 av_freep(&dnn_module);
libavfilter/dnn_interface.h+2−0
@@ -82,6 +82,8 @@ typedef struct DNNModule{ · 81 unmodified lines
8282 const char **output_names, uint32_t nb_output, AVFrame *out_frame);
8383 // Retrieve inference result.
8484 DNNAsyncStatusType (*get_async_result)(const DNNModel *model, AVFrame **in, AVFrame **out);
85+ // Flush all the pending tasks.
86+ DNNReturnType (*flush)(const DNNModel *model);
8587 // Frees memory allocated for model.
8688 void (*free_model)(DNNModel **model);
8789 } DNNModule;
libavfilter/vf_dnn_processing.c+37−3
@@ -33,6 +33,7 @@ · 32 unmodified lines
3333 #include "formats.h"
3434 #include "internal.h"
3535 #include "libswscale/swscale.h"
36+#include "libavutil/time.h"
3637
3738 typedef struct DnnProcessingContext {
3839 const AVClass *class;
@@ -369,6 +370,37 @@ static int activate_sync(AVFilterContext *filter_ctx) · 330 unmodified lines
369370 return FFERROR_NOT_READY;
370371 }
371372
373+static int flush_frame(AVFilterLink *outlink, int64_t pts, int64_t *out_pts)
374+{
375+ DnnProcessingContext *ctx = outlink->src->priv;
376+ int ret;
377+ DNNAsyncStatusType async_state;
378+
379+ ret = (ctx->dnn_module->flush)(ctx->model);
380+ if (ret != DNN_SUCCESS) {
381+ return -1;
382+ }
383+
384+ do {
385+ AVFrame *in_frame = NULL;
386+ AVFrame *out_frame = NULL;
387+ async_state = (ctx->dnn_module->get_async_result)(ctx->model, &in_frame, &out_frame);
388+ if (out_frame) {
389+ if (isPlanarYUV(in_frame->format))
390+ copy_uv_planes(ctx, out_frame, in_frame);
391+ av_frame_free(&in_frame);
392+ ret = ff_filter_frame(outlink, out_frame);
393+ if (ret < 0)
394+ return ret;
395+ if (out_pts)
396+ *out_pts = out_frame->pts + pts;
397+ }
398+ av_usleep(5000);
399+ } while (async_state >= DAST_NOT_READY);
400+
401+ return 0;
402+}
403+
372404 static int activate_async(AVFilterContext *filter_ctx)
373405 {
374406 AVFilterLink *inlink = filter_ctx->inputs[0];
@@ -396,7 +428,7 @@ static int activate_async(AVFilterContext *filter_ctx) · 21 unmodified lines
396428 av_frame_copy_props(out, in);
397429 if ((ctx->dnn_module->execute_model_async)(ctx->model, ctx->model_inputname, in,
398430 (const char **)&ctx->model_outputname, 1, out) != DNN_SUCCESS) {
399− return FFERROR_NOT_READY;
431+ return AVERROR(EIO);
400432 }
401433 }
402434 } while (ret > 0);
@@ -423,14 +455,16 @@ static int activate_async(AVFilterContext *filter_ctx) · 20 unmodified lines
423455
424456 if (ff_inlink_acknowledge_status(inlink, &status, &pts)) {
425457 if (status == AVERROR_EOF) {
426− ff_outlink_set_status(outlink, status, pts);
458+ int64_t out_pts = pts;
459+ ret = flush_frame(outlink, pts, &out_pts);
460+ ff_outlink_set_status(outlink, status, out_pts);
427461 return ret;
428462 }
429463 }
430464
431465 FF_FILTER_FORWARD_WANTED(outlink, inlink);
432466
433− return FFERROR_NOT_READY;
467+ return 0;
434468 }
435469
436470 static int activate(AVFilterContext *filter_ctx)

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t