FFmpeg
dnn_backend_torch.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /**
22  * @file
23  * DNN Torch backend implementation.
24  */
25 
26 #include <torch/torch.h>
27 #include <torch/script.h>
28 
29 extern "C" {
30 #include "dnn_io_proc.h"
31 #include "dnn_backend_common.h"
32 #include "libavutil/opt.h"
33 #include "libavutil/mem.h"
34 #include "libavutil/cpu.h"
35 #include "queue.h"
36 #include "safe_queue.h"
37 }
38 
39 typedef struct THModel {
42  torch::jit::Module *jit_model;
46 } THModel;
47 
48 typedef struct THInferRequest {
49  torch::Tensor *output;
50  torch::Tensor *input_tensor;
52 
53 typedef struct THRequestItem {
56  uint32_t lltask_count;
59 
60 
61 #define OFFSET(x) offsetof(THOptions, x)
62 #define FLAGS AV_OPT_FLAG_FILTERING_PARAM
63 static const AVOption dnn_th_options[] = {
64  { "optimize", "turn on graph executor optimization", OFFSET(optimize), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, FLAGS},
65  { NULL }
66 };
67 
68 static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
69 {
70  THModel *th_model = (THModel *)task->model;
71  DnnContext *ctx = th_model->ctx;
72  LastLevelTaskItem *lltask = (LastLevelTaskItem *)av_malloc(sizeof(*lltask));
73  if (!lltask) {
74  av_log(ctx, AV_LOG_ERROR, "Failed to allocate memory for LastLevelTaskItem\n");
75  return AVERROR(ENOMEM);
76  }
77  task->inference_todo = 1;
78  task->inference_done = 0;
79  lltask->task = task;
80  if (ff_queue_push_back(lltask_queue, lltask) < 0) {
81  av_log(ctx, AV_LOG_ERROR, "Failed to push back lltask_queue.\n");
82  av_freep(&lltask);
83  return AVERROR(ENOMEM);
84  }
85  return 0;
86 }
87 
88 static void th_free_request(THInferRequest *request)
89 {
90  if (!request)
91  return;
92  if (request->output) {
93  delete(request->output);
94  request->output = NULL;
95  }
96  if (request->input_tensor) {
97  delete(request->input_tensor);
98  request->input_tensor = NULL;
99  }
100  return;
101 }
102 
104 {
105  THRequestItem *item;
106  if (!arg || !*arg) {
107  return;
108  }
109  item = *arg;
111  av_freep(&item->infer_request);
112  av_freep(&item->lltasks);
114  av_freep(arg);
115 }
116 
117 static void dnn_free_model_th(DNNModel **model)
118 {
119  THModel *th_model;
120  if (!model || !*model)
121  return;
122 
123  th_model = (THModel *)(*model);
124 
125  if (th_model->request_queue) {
126  ff_dnn_wait_requests(th_model->request_queue, th_model->ctx->nireq);
127  while (ff_safe_queue_size(th_model->request_queue) != 0) {
129  destroy_request_item(&item);
130  }
132  }
133 
134  if (th_model->lltask_queue)
135  ff_queue_destroy(th_model->lltask_queue);
136  if (th_model->task_queue)
137  ff_queue_destroy(th_model->task_queue);
138 
139  if (th_model->jit_model)
140  delete th_model->jit_model;
141 
142  av_freep(&th_model);
143  *model = NULL;
144 }
145 
146 static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
147 {
148  input->dt = DNN_FLOAT;
149  input->order = DCO_RGB;
150  input->layout = DL_NCHW;
151  input->dims[0] = 1;
152  input->dims[1] = 3;
153  input->dims[2] = -1;
154  input->dims[3] = -1;
155  return 0;
156 }
157 
158 static void deleter(void *arg)
159 {
160  av_freep(&arg);
161 }
162 
163 static int fill_model_input_th(THModel *th_model, THRequestItem *request)
164 {
165  LastLevelTaskItem *lltask = NULL;
166  TaskItem *task = NULL;
167  THInferRequest *infer_request = NULL;
168  DNNData input = { 0 };
169  DnnContext *ctx = th_model->ctx;
170  int ret, width_idx, height_idx, channel_idx;
171  int batch_size = ctx->batch_size;
172  float *batch_data = NULL;
173  int frame_size = 0;
174 
175  infer_request = request->infer_request;
176 
177  ret = get_input_th(&th_model->model, &input, NULL);
178  if (ret != 0) {
179  goto err;
180  }
181  width_idx = dnn_get_width_idx_by_layout(input.layout);
182  height_idx = dnn_get_height_idx_by_layout(input.layout);
183  channel_idx = dnn_get_channel_idx_by_layout(input.layout);
184 
185  lltask = (LastLevelTaskItem *)ff_queue_peek_front(th_model->lltask_queue);
186  if (!lltask) {
187  ret = AVERROR(EINVAL);
188  goto err;
189  }
190  task = lltask->task;
191  input.dims[height_idx] = task->in_frame->height;
192  input.dims[width_idx] = task->in_frame->width;
193 
194  frame_size = input.dims[height_idx] * input.dims[width_idx] * input.dims[channel_idx];
195  batch_data = (float *)av_malloc(batch_size * frame_size * sizeof(float));
196  if (!batch_data) {
197  ret = AVERROR(ENOMEM);
198  goto err;
199  }
200 
201  for (int i = 0; i < batch_size; i++) {
202  lltask = (LastLevelTaskItem *)ff_queue_pop_front(th_model->lltask_queue);
203  if (!lltask)
204  break;
205 
206  request->lltasks[i] = lltask;
207  request->lltask_count = i + 1;
208  task = lltask->task;
209 
210  input.data = batch_data + i * frame_size;
211 
212  switch (th_model->model.func_type) {
213  case DFT_PROCESS_FRAME:
214  input.scale = 255;
215  if (task->do_ioproc) {
216  if (th_model->model.frame_pre_proc != NULL) {
217  th_model->model.frame_pre_proc(task->in_frame, &input, th_model->model.filter_ctx);
218  } else {
220  }
221  }
222  break;
223  default:
224  avpriv_report_missing_feature(NULL, "model function type %d", th_model->model.func_type);
225  break;
226  }
227  }
228 
229  infer_request->input_tensor = new torch::Tensor();
230  infer_request->output = new torch::Tensor();
231  *infer_request->input_tensor = torch::from_blob(batch_data,
232  {request->lltask_count, input.dims[channel_idx], input.dims[height_idx], input.dims[width_idx]},
233  deleter, torch::kFloat32);
234 
235  return 0;
236 
237 err:
238  if (batch_data)
239  av_freep(&batch_data);
240  th_free_request(infer_request);
241  return ret;
242 }
243 
244 static int th_start_inference(void *args)
245 {
246  THRequestItem *request = (THRequestItem *)args;
247  THInferRequest *infer_request = NULL;
248  LastLevelTaskItem *lltask = NULL;
249  TaskItem *task = NULL;
250  THModel *th_model = NULL;
251  DnnContext *ctx = NULL;
252  std::vector<torch::jit::IValue> inputs;
253  torch::NoGradGuard no_grad;
254 
255  if (!request) {
256  av_log(NULL, AV_LOG_ERROR, "THRequestItem is NULL\n");
257  return AVERROR(EINVAL);
258  }
259  infer_request = request->infer_request;
260  lltask = request->lltasks[0];
261  task = lltask->task;
262  th_model = (THModel *)task->model;
263  ctx = th_model->ctx;
264 
265  if (ctx->torch_option.optimize)
266  torch::jit::setGraphExecutorOptimize(true);
267  else
268  torch::jit::setGraphExecutorOptimize(false);
269 
270  if (!infer_request->input_tensor || !infer_request->output) {
271  av_log(ctx, AV_LOG_ERROR, "input or output tensor is NULL\n");
272  return DNN_GENERIC_ERROR;
273  }
274  // Transfer tensor to the same device as model
275  c10::Device device = (*th_model->jit_model->parameters().begin()).device();
276  if (infer_request->input_tensor->device() != device)
277  *infer_request->input_tensor = infer_request->input_tensor->to(device);
278  inputs.push_back(*infer_request->input_tensor);
279 
280  *infer_request->output = th_model->jit_model->forward(inputs).toTensor();
281 
282  return 0;
283 }
284 
285 static void infer_completion_callback(void *args) {
286  THRequestItem *request = (THRequestItem*)args;
287  THInferRequest *infer_request = request->infer_request;
288  LastLevelTaskItem *lltask = request->lltasks[0];
289  THModel *th_model = (THModel *)lltask->task->model;
290  torch::Tensor *output = infer_request->output;
291  DNNData outputs = { 0 };
292 
293  auto slices = torch::split(*output, /*split_size=*/1, /*dim=*/0);
294  for (uint32_t i = 0; i < request->lltask_count; i++) {
295  lltask = request->lltasks[i];
296  TaskItem *task = lltask->task;
297  torch::Tensor out_slice = slices[i];
298  c10::IntArrayRef sizes = out_slice.sizes();
299 
300  outputs.order = DCO_RGB;
301  outputs.layout = DL_NCHW;
302  outputs.dt = DNN_FLOAT;
303 
304  if (sizes.size() == 4) {
305  // 4 dimensions: [batch_size, channel, height, width]
306  // this format of data is normally used for video frame SR
307  outputs.dims[0] = sizes.at(0); // N
308  outputs.dims[1] = sizes.at(1); // C
309  outputs.dims[2] = sizes.at(2); // H
310  outputs.dims[3] = sizes.at(3); // W
311  } else {
312  avpriv_report_missing_feature(th_model->ctx, "Support of this kind of model");
313  goto err;
314  }
315 
316  switch (th_model->model.func_type) {
317  case DFT_PROCESS_FRAME:
318  if (task->do_ioproc) {
319  // Post process can only deal with CPU memory.
320  if (out_slice.device() != torch::kCPU)
321  out_slice = out_slice.to(torch::kCPU);
322  outputs.scale = 255;
323  outputs.data = out_slice.data_ptr();
324  if (th_model->model.frame_post_proc != NULL) {
325  th_model->model.frame_post_proc(task->out_frame, &outputs, th_model->model.filter_ctx);
326  } else {
327  ff_proc_from_dnn_to_frame(task->out_frame, &outputs, th_model->ctx);
328  }
329  } else {
332  }
333  break;
334  default:
335  avpriv_report_missing_feature(th_model->ctx, "model function type %d", th_model->model.func_type);
336  goto err;
337  }
338  task->inference_done++;
339  }
340 
341 err:
342  for (uint32_t i = 0; i < request->lltask_count; i++) {
343  av_freep(&request->lltasks[i]);
344  }
345  request->lltask_count = 0;
346 
347  th_free_request(infer_request);
348 
349  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
350  destroy_request_item(&request);
351  av_log(th_model->ctx, AV_LOG_ERROR, "Unable to push back request_queue when failed to start inference.\n");
352  }
353 }
354 
355 static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
356 {
357  THModel *th_model = NULL;
358  LastLevelTaskItem *lltask;
359  TaskItem *task = NULL;
360  int ret = 0;
361 
362  if (ff_queue_size(lltask_queue) == 0) {
363  destroy_request_item(&request);
364  return 0;
365  }
366 
367  lltask = (LastLevelTaskItem *)ff_queue_peek_front(lltask_queue);
368  if (lltask == NULL) {
369  av_log(NULL, AV_LOG_ERROR, "Failed to get LastLevelTaskItem\n");
370  ret = AVERROR(EINVAL);
371  goto err;
372  }
373  task = lltask->task;
374  th_model = (THModel *)task->model;
375 
376  ret = fill_model_input_th(th_model, request);
377  if (ret != 0) {
378  goto err;
379  }
380 
381  if (task->async) {
382  ret = ff_dnn_start_inference_async(th_model->ctx, &request->exec_module);
383  if (ret != 0) {
384  goto err;
385  }
386  return 0;
387  } else {
388  // Synchronous execution path
389  ret = th_start_inference((void *)(request));
390  if (ret != 0) {
391  goto err;
392  }
393  infer_completion_callback(request);
394  return (task->inference_done == task->inference_todo) ? 0 : DNN_GENERIC_ERROR;
395  }
396 
397 err:
398  th_free_request(request->infer_request);
399  if (ff_safe_queue_push_back(th_model->request_queue, request) < 0) {
400  destroy_request_item(&request);
401  }
402  return ret;
403 }
404 
405 static int get_output_th(DNNModel *model, const char *input_name, int input_width, int input_height,
406  const char *output_name, int *output_width, int *output_height)
407 {
408  int ret = 0;
409  THModel *th_model = (THModel*) model;
410  DnnContext *ctx = th_model->ctx;
411  TaskItem task = { 0 };
412  THRequestItem *request = NULL;
413  DNNExecBaseParams exec_params = {
414  .input_name = input_name,
415  .output_names = &output_name,
416  .nb_output = 1,
417  .in_frame = NULL,
418  .out_frame = NULL,
419  };
420  ret = ff_dnn_fill_gettingoutput_task(&task, &exec_params, th_model, input_height, input_width, ctx);
421  if ( ret != 0) {
422  goto err;
423  }
424 
425  ret = extract_lltask_from_task(&task, th_model->lltask_queue);
426  if ( ret != 0) {
427  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
428  goto err;
429  }
430 
431  request = (THRequestItem*) ff_safe_queue_pop_front(th_model->request_queue);
432  if (!request) {
433  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
434  ret = AVERROR(EINVAL);
435  goto err;
436  }
437 
438  ret = execute_model_th(request, th_model->lltask_queue);
439  *output_width = task.out_frame->width;
440  *output_height = task.out_frame->height;
441 
442 err:
443  av_frame_free(&task.out_frame);
444  av_frame_free(&task.in_frame);
445  return ret;
446 }
447 
449 {
450  THInferRequest *request = (THInferRequest *)av_malloc(sizeof(THInferRequest));
451  if (!request) {
452  return NULL;
453  }
454  request->input_tensor = NULL;
455  request->output = NULL;
456  return request;
457 }
458 
460 {
461  DNNModel *model = NULL;
462  THModel *th_model = NULL;
463  THRequestItem *item = NULL;
464  const char *device_name = ctx->device ? ctx->device : "cpu";
465 
466  th_model = (THModel *)av_mallocz(sizeof(THModel));
467  if (!th_model)
468  return NULL;
469  model = &th_model->model;
470  th_model->ctx = ctx;
471 
472  c10::Device device = c10::Device(device_name);
473  if (device.is_xpu()) {
474  if (!at::hasXPU()) {
475  av_log(ctx, AV_LOG_ERROR, "No XPU device found\n");
476  goto fail;
477  }
478 #if TORCH_VERSION_MAJOR > 2 || (TORCH_VERSION_MAJOR == 2 && TORCH_VERSION_MINOR >= 6)
479  at::detail::getXPUHooks().init();
480 #else
481  at::detail::getXPUHooks().initXPU();
482 #endif
483  } else if (device.is_cuda()) {
484  // CUDA device - works for both NVIDIA CUDA and AMD ROCm (which uses CUDA-compatible API)
485  if (!torch::cuda::is_available()) {
486  av_log(ctx, AV_LOG_ERROR, "CUDA/ROCm is not available\n");
487  goto fail;
488  }
489  av_log(ctx, AV_LOG_INFO, "Using CUDA/ROCm device: %s\n", device_name);
490  } else if (!device.is_cpu()) {
491  av_log(ctx, AV_LOG_ERROR, "Not supported device:\"%s\"\n", device_name);
492  goto fail;
493  }
494 
495  try {
496  th_model->jit_model = new torch::jit::Module;
497  (*th_model->jit_model) = torch::jit::load(ctx->model_filename);
498  th_model->jit_model->to(device);
499  } catch (const c10::Error& e) {
500  av_log(ctx, AV_LOG_ERROR, "Failed to load torch model\n");
501  goto fail;
502  }
503 
504  if (ctx->nireq <= 0) {
505  ctx->nireq = av_cpu_count() / 2 + 1;
506  }
507 
508  th_model->request_queue = ff_safe_queue_create();
509  if (!th_model->request_queue) {
510  goto fail;
511  }
512 
513  for (int i = 0; i < ctx->nireq; i++) {
514  item = (THRequestItem *)av_mallocz(sizeof(THRequestItem));
515  if (!item) {
516  goto fail;
517  }
519  if (!item->infer_request) {
520  goto fail;
521  }
522  item->lltasks = (LastLevelTaskItem **)av_malloc_array(ctx->batch_size, sizeof(*item->lltasks));
523  if (!item->lltasks) {
524  goto fail;
525  }
526  item->lltask_count = 0;
527 
530  item->exec_module.args = item;
531 
532  if (ff_safe_queue_push_back(th_model->request_queue, item) < 0) {
533  goto fail;
534  }
535  item = NULL;
536  }
537 
538  th_model->task_queue = ff_queue_create();
539  th_model->lltask_queue = ff_queue_create();
540 
541  model->get_input = &get_input_th;
542  model->get_output = &get_output_th;
543  model->filter_ctx = filter_ctx;
544  model->func_type = func_type;
545  return model;
546 
547 fail:
548  if (item) {
549  destroy_request_item(&item);
550  }
551  dnn_free_model_th(&model);
552  return NULL;
553 }
554 
555 static int dnn_execute_model_th(const DNNModel *model, DNNExecBaseParams *exec_params)
556 {
557  THModel *th_model = (THModel *)model;
558  DnnContext *ctx = th_model->ctx;
559  TaskItem *task;
560  THRequestItem *request;
561  int ret = 0;
562 
563  ret = ff_check_exec_params(ctx, DNN_TH, model->func_type, exec_params);
564  if (ret != 0) {
565  av_log(ctx, AV_LOG_ERROR, "exec parameter checking fail.\n");
566  return ret;
567  }
568 
569  task = (TaskItem *)av_malloc(sizeof(TaskItem));
570  if (!task) {
571  av_log(ctx, AV_LOG_ERROR, "unable to alloc memory for task item.\n");
572  return AVERROR(ENOMEM);
573  }
574 
575  ret = ff_dnn_fill_task(task, exec_params, th_model, ctx->async, 1);
576  if (ret != 0) {
577  av_freep(&task);
578  av_log(ctx, AV_LOG_ERROR, "unable to fill task.\n");
579  return ret;
580  }
581 
582  ret = ff_queue_push_back(th_model->task_queue, task);
583  if (ret < 0) {
584  av_freep(&task);
585  av_log(ctx, AV_LOG_ERROR, "unable to push back task_queue.\n");
586  return ret;
587  }
588 
589  ret = extract_lltask_from_task(task, th_model->lltask_queue);
590  if (ret != 0) {
591  av_log(ctx, AV_LOG_ERROR, "unable to extract last level task from task.\n");
592  return ret;
593  }
594 
595  while (ff_queue_size(th_model->lltask_queue) >= ctx->batch_size) {
596  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
597  if (!request) {
598  av_log(ctx, AV_LOG_ERROR, "unable to get infer request.\n");
599  return AVERROR(EINVAL);
600  }
601 
602  ret = execute_model_th(request, th_model->lltask_queue);
603  if (ret != 0) {
604  return ret;
605  }
606  }
607 
608  return 0;
609 }
610 
612 {
613  THModel *th_model = (THModel *)model;
614  return ff_dnn_get_result_common(th_model->task_queue, in, out);
615 }
616 
617 static int dnn_flush_th(const DNNModel *model)
618 {
619  THModel *th_model = (THModel *)model;
620  THRequestItem *request;
621 
622  if (ff_queue_size(th_model->lltask_queue) == 0)
623  // no pending task need to flush
624  return 0;
625 
626  request = (THRequestItem *)ff_safe_queue_pop_front(th_model->request_queue);
627  if (!request) {
628  av_log(th_model->ctx, AV_LOG_ERROR, "unable to get infer request.\n");
629  return AVERROR(EINVAL);
630  }
631 
632  return execute_model_th(request, th_model->lltask_queue);
633 }
634 
635 extern const DNNModule ff_dnn_backend_torch = {
636  .clazz = DNN_DEFINE_CLASS(dnn_th),
637  .type = DNN_TH,
638  .load_model = dnn_load_model_th,
639  .execute_model = dnn_execute_model_th,
640  .get_result = dnn_get_result_th,
641  .flush = dnn_flush_th,
642  .free_model = dnn_free_model_th,
643 };
THModel::lltask_queue
Queue * lltask_queue
Definition: dnn_backend_torch.cpp:45
THRequestItem::infer_request
THInferRequest * infer_request
Definition: dnn_backend_torch.cpp:54
THModel::ctx
DnnContext * ctx
Definition: dnn_backend_torch.cpp:41
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
ff_safe_queue_pop_front
void * ff_safe_queue_pop_front(SafeQueue *sq)
Remove and free first element from the queue in SafeQueue.
Definition: safe_queue.c:105
out
static FILE * out
Definition: movenc.c:55
deleter
static void deleter(void *arg)
Definition: dnn_backend_torch.cpp:158
FLAGS
#define FLAGS
Definition: dnn_backend_torch.cpp:62
THModel
Definition: dnn_backend_torch.cpp:39
DNNAsyncExecModule
Common Async Execution Mechanism for the DNN Backends.
Definition: dnn_backend_common.h:66
DNNFunctionType
DNNFunctionType
Definition: dnn_interface.h:57
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:226
ff_queue_pop_front
void * ff_queue_pop_front(Queue *q)
Remove and free first element from the Queue.
Definition: queue.c:151
ff_check_exec_params
int ff_check_exec_params(void *ctx, DNNBackendType backend, DNNFunctionType func_type, DNNExecBaseParams *exec_params)
Definition: dnn_backend_common.c:31
ff_queue_size
size_t ff_queue_size(Queue *q)
Return the length of the Queue.
Definition: queue.c:88
DNN_GENERIC_ERROR
#define DNN_GENERIC_ERROR
Definition: dnn_interface.h:33
av_frame_free
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
Definition: frame.c:64
LastLevelTaskItem
Definition: dnn_backend_common.h:58
ff_dnn_backend_torch
const DNNModule ff_dnn_backend_torch
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:472
AVFrame::width
int width
Definition: frame.h:544
is_available
static int is_available(const VVCFrameContext *fc, const int x0, const int y0)
Definition: mvs.c:552
SafeQueue
Double-ended queue with mutex locks ensuring data consistency while multithreading.
Definition: safe_queue.c:46
dnn_execute_model_th
static int dnn_execute_model_th(const DNNModel *model, DNNExecBaseParams *exec_params)
Definition: dnn_backend_torch.cpp:555
AVOption
AVOption.
Definition: opt.h:428
DNNModel::frame_pre_proc
FramePrePostProc frame_pre_proc
Definition: dnn_interface.h:111
av_cpu_count
int av_cpu_count(void)
Definition: cpu.c:228
DNNExecBaseParams::input_name
const char * input_name
Definition: dnn_interface.h:82
dnn_io_proc.h
TaskItem
Definition: dnn_backend_common.h:44
DNNAsyncExecModule::callback
void(* callback)(void *args)
Completion Callback for the backend.
Definition: dnn_backend_common.h:78
cpu.h
DNNModel::filter_ctx
AVFilterContext * filter_ctx
Definition: dnn_interface.h:100
ff_queue_create
Queue * ff_queue_create(void)
Create a Queue instance.
Definition: queue.c:47
dnn_get_width_idx_by_layout
static int dnn_get_width_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:209
TaskItem::model
void * model
Definition: dnn_backend_common.h:45
DnnContext
Definition: dnn_interface.h:150
filter_ctx
static FilteringContext * filter_ctx
Definition: transcode.c:52
ff_dnn_wait_requests
void ff_dnn_wait_requests(SafeQueue *request_queue, int nireq)
Wait for all inference requests to complete before teardown.
Definition: dnn_backend_common.c:106
Queue
Linear double-ended data structure.
Definition: executor.c:51
ff_queue_push_back
int ff_queue_push_back(Queue *q, void *v)
Add data to the tail of the queue.
Definition: queue.c:130
THModel::jit_model
torch::jit::Module * jit_model
Definition: dnn_backend_torch.cpp:42
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
LastLevelTaskItem::task
TaskItem * task
Definition: dnn_backend_common.h:59
destroy_request_item
static void destroy_request_item(THRequestItem **arg)
Definition: dnn_backend_torch.cpp:103
frame_size
int frame_size
Definition: mxfenc.c:2489
th_create_inference_request
static THInferRequest * th_create_inference_request(void)
Definition: dnn_backend_torch.cpp:448
ff_queue_destroy
void ff_queue_destroy(Queue *q)
Destroy the Queue instance.
Definition: queue.c:72
DNNData
Definition: dnn_interface.h:70
DNNModule::clazz
const AVClass clazz
Definition: dnn_interface.h:188
ff_dnn_fill_gettingoutput_task
int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx)
Allocate input and output frames and fill the Task with execution parameters.
Definition: dnn_backend_common.c:165
DNNModel::get_output
int(* get_output)(struct DNNModel *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
Definition: dnn_interface.h:107
ctx
static AVFormatContext * ctx
Definition: movenc.c:49
TaskItem::inference_todo
uint32_t inference_todo
Definition: dnn_backend_common.h:53
DL_NCHW
@ DL_NCHW
Definition: dnn_interface.h:66
av_mallocz
#define av_mallocz(s)
Definition: tableprint_vlc.h:31
dnn_load_model_th
static DNNModel * dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
Definition: dnn_backend_torch.cpp:459
arg
const char * arg
Definition: jacosubdec.c:65
if
if(ret)
Definition: filter_design.txt:179
ff_safe_queue_size
size_t ff_safe_queue_size(SafeQueue *sq)
Return the length of the SafeQueue.
Definition: safe_queue.c:80
ff_proc_from_frame_to_dnn
int ff_proc_from_frame_to_dnn(AVFrame *frame, DNNData *input, void *log_ctx)
Definition: dnn_io_proc.c:182
fail
#define fail
Definition: test.h:478
THRequestItem::exec_module
DNNAsyncExecModule exec_module
Definition: dnn_backend_torch.cpp:57
NULL
#define NULL
Definition: coverity.c:32
sizes
static const int sizes[][2]
Definition: img2dec.c:62
get_input_th
static int get_input_th(DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_backend_torch.cpp:146
ff_safe_queue_create
SafeQueue * ff_safe_queue_create(void)
Create and initialize a SafeQueue instance.
Definition: safe_queue.c:52
get_output_th
static int get_output_th(DNNModel *model, const char *input_name, int input_width, int input_height, const char *output_name, int *output_width, int *output_height)
Definition: dnn_backend_torch.cpp:405
ff_dnn_async_module_cleanup
int ff_dnn_async_module_cleanup(DNNAsyncExecModule *async_module)
Join the Async Execution thread and set module pointers to NULL.
Definition: dnn_backend_common.c:87
infer_completion_callback
static void infer_completion_callback(void *args)
Definition: dnn_backend_torch.cpp:285
TaskItem::in_frame
AVFrame * in_frame
Definition: dnn_backend_common.h:46
extract_lltask_from_task
static int extract_lltask_from_task(TaskItem *task, Queue *lltask_queue)
Definition: dnn_backend_torch.cpp:68
inputs
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several inputs
Definition: filter_design.txt:244
DnnContext::nireq
int nireq
Definition: dnn_interface.h:166
THInferRequest::output
torch::Tensor * output
Definition: dnn_backend_torch.cpp:49
TaskItem::async
uint8_t async
Definition: dnn_backend_common.h:50
TaskItem::inference_done
uint32_t inference_done
Definition: dnn_backend_common.h:54
i
#define i(width, name, range_min, range_max)
Definition: cbs_h264.c:63
queue.h
DNNModel::func_type
DNNFunctionType func_type
Definition: dnn_interface.h:102
avpriv_report_missing_feature
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
av_malloc
#define av_malloc(s)
Definition: ops_static.c:44
ff_safe_queue_destroy
void ff_safe_queue_destroy(SafeQueue *sq)
Destroy the SafeQueue instance.
Definition: safe_queue.c:69
split
static char * split(char *message, char delim)
Definition: af_channelmap.c:89
DNN_FLOAT
@ DNN_FLOAT
Definition: dnn_interface.h:42
dnn_get_result_th
static DNNAsyncStatusType dnn_get_result_th(const DNNModel *model, AVFrame **in, AVFrame **out)
Definition: dnn_backend_torch.cpp:611
ff_dnn_fill_task
int ff_dnn_fill_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int async, int do_ioproc)
Fill the Task for Backend Execution.
Definition: dnn_backend_common.c:51
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:221
DNN_DEFINE_CLASS
#define DNN_DEFINE_CLASS(fname)
Definition: dnn_backend_common.h:40
THRequestItem
Definition: dnn_backend_torch.cpp:53
ff_safe_queue_push_back
int ff_safe_queue_push_back(SafeQueue *sq, void *v)
Add data to the tail of queue in the SafeQueue after locking mutex.
Definition: safe_queue.c:95
th_start_inference
static int th_start_inference(void *args)
Definition: dnn_backend_torch.cpp:244
THInferRequest::input_tensor
torch::Tensor * input_tensor
Definition: dnn_backend_torch.cpp:50
av_malloc_array
#define av_malloc_array(a, b)
Definition: tableprint_vlc.h:32
DNNAsyncExecModule::start_inference
int(* start_inference)(void *request)
Synchronous inference function for the backend with corresponding request item as the argument.
Definition: dnn_backend_common.h:71
DNNAsyncExecModule::args
void * args
Argument for the execution functions.
Definition: dnn_backend_common.h:84
safe_queue.h
THInferRequest
Definition: dnn_backend_torch.cpp:48
outputs
static const AVFilterPad outputs[]
Definition: af_aap.c:310
ret
ret
Definition: filter_design.txt:187
TaskItem::out_frame
AVFrame * out_frame
Definition: dnn_backend_common.h:47
AVFrame::height
int height
Definition: frame.h:544
dnn_backend_common.h
THModel::model
DNNModel model
Definition: dnn_backend_torch.cpp:40
dnn_th_options
static const AVOption dnn_th_options[]
Definition: dnn_backend_torch.cpp:63
execute_model_th
static int execute_model_th(THRequestItem *request, Queue *lltask_queue)
Definition: dnn_backend_torch.cpp:355
OFFSET
#define OFFSET(x)
Definition: dnn_backend_torch.cpp:61
AV_OPT_TYPE_INT
@ AV_OPT_TYPE_INT
Underlying C type is int.
Definition: opt.h:258
THRequestItem::lltasks
LastLevelTaskItem ** lltasks
Definition: dnn_backend_torch.cpp:55
ff_dnn_get_result_common
DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVFrame **out)
Extract input and output frame from the Task Queue after asynchronous inference.
Definition: dnn_backend_common.c:145
ff_queue_peek_front
void * ff_queue_peek_front(Queue *q)
Return a pointer to the data at the head of the queue.
Definition: queue.c:93
DCO_RGB
@ DCO_RGB
Definition: dnn_interface.h:47
AVFilterContext
An instance of a filter.
Definition: avfilter.h:273
ff_dnn_start_inference_async
int ff_dnn_start_inference_async(void *ctx, DNNAsyncExecModule *async_module)
Start asynchronous inference routine for the TensorFlow model on a detached thread.
Definition: dnn_backend_common.c:114
DNNModel
Definition: dnn_interface.h:98
DNN_TH
@ DNN_TH
Definition: dnn_interface.h:38
mem.h
dnn_get_height_idx_by_layout
static int dnn_get_height_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:214
dnn_flush_th
static int dnn_flush_th(const DNNModel *model)
Definition: dnn_backend_torch.cpp:617
THModel::task_queue
Queue * task_queue
Definition: dnn_backend_torch.cpp:44
dnn_get_channel_idx_by_layout
static int dnn_get_channel_idx_by_layout(DNNLayout layout)
Definition: dnn_interface.h:219
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:35
DNNExecBaseParams
Definition: dnn_interface.h:81
DNNModel::get_input
int(* get_input)(struct DNNModel *model, DNNData *input, const char *input_name)
Definition: dnn_interface.h:105
dnn_free_model_th
static void dnn_free_model_th(DNNModel **model)
Definition: dnn_backend_torch.cpp:117
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
TaskItem::do_ioproc
uint8_t do_ioproc
Definition: dnn_backend_common.h:51
DNNAsyncStatusType
DNNAsyncStatusType
Definition: dnn_interface.h:50
DFT_PROCESS_FRAME
@ DFT_PROCESS_FRAME
Definition: dnn_interface.h:59
DNNModule
Definition: dnn_interface.h:187
fill_model_input_th
static int fill_model_input_th(THModel *th_model, THRequestItem *request)
Definition: dnn_backend_torch.cpp:163
THModel::request_queue
SafeQueue * request_queue
Definition: dnn_backend_torch.cpp:43
THRequestItem::lltask_count
uint32_t lltask_count
Definition: dnn_backend_torch.cpp:56
ff_proc_from_dnn_to_frame
int ff_proc_from_dnn_to_frame(AVFrame *frame, DNNData *output, void *log_ctx)
Definition: dnn_io_proc.c:42
th_free_request
static void th_free_request(THInferRequest *request)
Definition: dnn_backend_torch.cpp:88