Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
From: Raja Rathour via ffmpeg-devel <ffmpeg-devel@ffmpeg.org>
To: ffmpeg-devel@ffmpeg.org
Cc: Raja Rathour <imraja729@gmail.com>
Subject: [FFmpeg-devel] [PATCH 4/4] avfilter/dnn_backend_torch: improve device selection and error handling
Date: Tue, 20 Jan 2026 19:39:39 +0530
Message-ID: <20260120140939.32403-4-imraja729@gmail.com> (raw)
In-Reply-To: <20260120140939.32403-1-imraja729@gmail.com>

---
 libavfilter/dnn/dnn_backend_torch.cpp | 38 +++++++++++++++++++++------
 1 file changed, 30 insertions(+), 8 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp
index 4f7ae17aab..73eadc6b7e 100644
--- a/libavfilter/dnn/dnn_backend_torch.cpp
+++ b/libavfilter/dnn/dnn_backend_torch.cpp
@@ -255,16 +255,30 @@ static int th_start_inference(void *args)
     LastLevelTaskItem *lltask = request->lltask;
     TaskItem *task = lltask->task;
     THModel *th_model = (THModel *)task->model;
+    DnnContext *ctx = th_model->ctx;
     std::vector<torch::jit::IValue> inputs;
 
-    torch::jit::setGraphExecutorOptimize(!!th_model->ctx->torch_option.optimize);
+    torch::jit::setGraphExecutorOptimize(!!ctx->torch_option.optimize);
+
+    if (!infer_request->input_tensor || !infer_request->output) {
+        av_log(ctx, AV_LOG_ERROR, "input or output tensor is NULL\n");
+        return DNN_GENERIC_ERROR;
+    }
+
+    const char *device_name = ctx->device ? ctx->device : "cpu";
+    c10::Device device = c10::Device(device_name);
 
-    c10::Device device = (*th_model->jit_model->parameters().begin()).device();
     if (infer_request->input_tensor->device() != device)
         *infer_request->input_tensor = infer_request->input_tensor->to(device);
 
     inputs.push_back(*infer_request->input_tensor);
-    *infer_request->output = th_model->jit_model->forward(inputs).toTensor();
+
+    try {
+        *infer_request->output = th_model->jit_model->forward(inputs).toTensor();
+    } catch (const c10::Error& e) {
+        av_log(ctx, AV_LOG_ERROR, "Torch forward pass failed: %s\n", e.what());
+        return DNN_GENERIC_ERROR;
+    }
 
     return 0;
 }
@@ -415,14 +429,23 @@ static DNNModel *dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, A
 {
     THModel *th_model = av_mallocz(sizeof(THModel));
     THRequestItem *item = NULL;
+    const char *device_name = ctx->device ? ctx->device : "cpu";
 
     if (!th_model)
         return NULL;
 
     th_model->ctx = ctx;
-    th_model->jit_model = new torch::jit::Module;
-    // Commit 1 uses the simplest loading logic
-    *th_model->jit_model = torch::jit::load(ctx->model_filename);
+
+    // Robustness: Wrap model loading and device movement in try-catch
+    try {
+        c10::Device device = c10::Device(device_name);
+        th_model->jit_model = new torch::jit::Module;
+        (*th_model->jit_model) = torch::jit::load(ctx->model_filename);
+        th_model->jit_model->to(device);
+    } catch (const c10::Error& e) {
+        av_log(ctx, AV_LOG_ERROR, "Failed to load torch model: %s\n", e.what());
+        goto fail;
+    }
 
     th_model->request_queue = ff_safe_queue_create();
     if (!th_model->request_queue)
@@ -436,7 +459,6 @@ static DNNModel *dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, A
     if (!item->infer_request)
         goto fail;
 
-    // Infrastructure setup for Async Module
     item->exec_module.start_inference = &th_start_inference;
     item->exec_module.callback = &infer_completion_callback;
     item->exec_module.args = item;
@@ -463,7 +485,7 @@ static DNNModel *dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, A
 fail:
     if (item)
         destroy_request_item(&item);
-    // Passing the address of the model pointer
+    
     DNNModel *temp_model = &th_model->model;
     dnn_free_model_th(&temp_model);
     return NULL;
-- 
2.51.0

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

  parent reply	other threads:[~2026-01-20 14:11 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-20 14:09 [FFmpeg-devel] [PATCH 1/4] avfilter/dnn_backend_torch: implement common async infrastructure Raja Rathour via ffmpeg-devel
2026-01-20 14:09 ` [FFmpeg-devel] [PATCH 2/4] avfilter/dnn_backend_torch: fix memory leak with persistent input buffer Raja Rathour via ffmpeg-devel
2026-01-20 14:09 ` [FFmpeg-devel] [PATCH 3/4] avfilter/dnn_backend_torch: add support for dynamic input shapes Raja Rathour via ffmpeg-devel
2026-01-20 14:09 ` Raja Rathour via ffmpeg-devel [this message]
2026-01-21  2:08 ` [FFmpeg-devel] Re: [PATCH 1/4] avfilter/dnn_backend_torch: implement common async infrastructure Guo, Yejun via ffmpeg-devel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260120140939.32403-4-imraja729@gmail.com \
    --to=ffmpeg-devel@ffmpeg.org \
    --cc=imraja729@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git