From: <m.kaindl0208@gmail.com> To: <ffmpeg-devel@ffmpeg.org> Subject: [FFmpeg-devel] [PATCH v2 FFmpeg 5/20] libavfilter/dnn: libtorch add CUDA suppoort Date: Mon, 10 Mar 2025 20:52:55 +0100 Message-ID: <003a01db91f6$04324fb0$0c96ef10$@gmail.com> (raw) Signed-off-by: MaximilianKaindl <m.kaindl0208@gmail.com> --- libavfilter/dnn/dnn_backend_torch.cpp | 37 +++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/libavfilter/dnn/dnn_backend_torch.cpp b/libavfilter/dnn/dnn_backend_torch.cpp index 2e4326d9d4..062821949d 100644 --- a/libavfilter/dnn/dnn_backend_torch.cpp +++ b/libavfilter/dnn/dnn_backend_torch.cpp @@ -34,6 +34,10 @@ extern "C" { #include "queue.h" #include "safe_queue.h" } +#if (CONFIG_LIBTORCH_CUDA == 1) +#include <ATen/cuda/CUDAContext.h> +#include <c10/cuda/CUDAStream.h> +#endif typedef struct THModel { DNNModel model; @@ -435,7 +439,40 @@ static DNNModel *dnn_load_model_th(DnnContext *ctx, DNNFunctionType func_type, A av_log(ctx, AV_LOG_ERROR, "No XPU device found\n"); goto fail; } +#if (CONFIG_LIBTORCH_CUDA == 0) at::detail::getXPUHooks().initXPU(); +#else + at::detail::getXPUHooks().init(); + } else if (device.is_cuda()) { + if (!torch::cuda::is_available()) { + av_log(ctx, AV_LOG_ERROR, "CUDA is not available!\n"); + goto fail; + } + // Initialize CUDA + try { + int device_idx = 0; + const char *device_num = strstr(device_name, ":"); + if (device_num) { + char *endptr = NULL; + device_idx = strtol(device_num + 1, &endptr, 10); + if (*endptr != '\0' && !isspace(*endptr)) { + av_log(ctx, AV_LOG_ERROR, "Invalid device number format: %s\n", device_num + 1); + goto fail; + } + } + if (device_idx >= static_cast<int>(torch::cuda::device_count())) { + av_log(ctx, AV_LOG_ERROR, "Requested CUDA device %d but only %ld devices available\n", device_idx, + torch::cuda::device_count()); + goto fail; + } + c10::cuda::set_device(device_idx); + c10::cuda::setCurrentCUDAStream(c10::cuda::getDefaultCUDAStream()); + torch::cuda::synchronize(); + } catch (const c10::Error &e) { + av_log(ctx, AV_LOG_ERROR, "CUDA initialization failed: %s\n", e.what()); + goto fail; + } +#endif } else if (!device.is_cpu()) { av_log(ctx, AV_LOG_ERROR, "Not supported device:\"%s\"\n", device_name); goto fail; -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
reply other threads:[~2025-03-10 19:53 UTC|newest] Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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='003a01db91f6$04324fb0$0c96ef10$@gmail.com' \ --to=m.kaindl0208@gmail.com \ --cc=ffmpeg-devel@ffmpeg.org \ /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