[PR] avfilter/dnn: prevent crash on parameterless LibTorch models (PR #23927)

Raja-89 via ffmpeg-devel <[email protected]> Mon, 27 Jul 2026 17:04:20 -0000
Newsgroups gmane.comp.video.ffmpeg.devel
Message-ID <178517186665.59.7838366219803755554@29965ddac10e>
PR #23927 opened by Raja-89
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23927
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/23927.patch

When loading a TorchScript model that does not contain any learnable
parameters (e.g., a purely functional model), the Torch backend would
crash during inference. This occurred because the code attempted to
dereference the first iterator of the model's parameter list
`parameters().begin()` to determine the device, which results in
Undefined Behavior when the parameter list is empty.
This commit fixes the issue by determining the inference device directly
from the user-configured `ctx->device` string instead of probing the
model parameters, allowing parameterless models to execute safely.

# Summary of changes

Briefly describe what this PR does and why.

<!--
If this PR requires new FATE test samples, attach them to the PR and
list their target paths below (relative to the fate-suite root).

Attached filenames must match the sample's filename:

```fate-samples
# e.g. vorbis/new-sample.ogg
```
-->



>From 5fc312ff0d0bed21ea4b4eb1ff27c2f636a39b0a Mon Sep 17 00:00:00 2001
From: Raja-89 <[email protected]>
Date: Mon, 27 Jul 2026 21:50:47 +0530
Subject: [PATCH] avfilter/dnn: prevent crash on parameterless LibTorch models

When loading a TorchScript model that does not contain any learnable
parameters (e.g., a purely functional model), the Torch backend would
crash during inference. This occurred because the code attempted to
dereference the first iterator of the model's parameter list
`parameters().begin()` to determine the device, which results in
Undefined Behavior when the parameter list is empty.
This commit fixes the issue by determining the inference device directly
from the user-configured `ctx->device` string instead of probing the
model parameters, allowing parameterless models to execute safely.
---
 libavfilter/dnn/dnn_backend_torch.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp
index 9ba6d61377..d4edc5ef0a 100644
--- a/libavfilter/dnn/dnn_backend_torch.cpp
+++ b/libavfilter/dnn/dnn_backend_torch.cpp
@@ -271,7 +271,8 @@ static int th_start_inference(void *args)
         return DNN_GENERIC_ERROR;
     }
     // Transfer tensor to the same device as model
-    c10::Device device = (*th_model->jit_model->parameters().begin()).device();
+    const char *device_name = ctx->device ? ctx->device : "cpu";
+    c10::Device device(device_name);
     if (infer_request->input_tensor->device() != device)
         *infer_request->input_tensor = infer_request->input_tensor->to(device);
     inputs.push_back(*infer_request->input_tensor);
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]