From b59bb13c9ef90979b31c12c47f121b7fa991e70d Mon Sep 17 00:00:00 2001 From: Dmitry Rogozhkin Date: Mon, 7 Apr 2025 19:53:01 +0000 Subject: [PATCH] Rename CudaDevice to CudaDeviceInterface Renaming as requested in: * https://github.com/pytorch/torchcodec/pull/606#discussion_r2030880769 Signed-off-by: Dmitry Rogozhkin --- src/torchcodec/_core/CMakeLists.txt | 2 +- ...CudaDevice.cpp => CudaDeviceInterface.cpp} | 21 +++++++++++-------- .../{CudaDevice.h => CudaDeviceInterface.h} | 6 +++--- 3 files changed, 16 insertions(+), 13 deletions(-) rename src/torchcodec/_core/{CudaDevice.cpp => CudaDeviceInterface.cpp} (94%) rename src/torchcodec/_core/{CudaDevice.h => CudaDeviceInterface.h} (85%) diff --git a/src/torchcodec/_core/CMakeLists.txt b/src/torchcodec/_core/CMakeLists.txt index 1f42e24d..c79f62f3 100644 --- a/src/torchcodec/_core/CMakeLists.txt +++ b/src/torchcodec/_core/CMakeLists.txt @@ -68,7 +68,7 @@ function(make_torchcodec_libraries ) if(ENABLE_CUDA) - list(APPEND decoder_sources CudaDevice.cpp) + list(APPEND decoder_sources CudaDeviceInterface.cpp) endif() set(decoder_library_dependencies diff --git a/src/torchcodec/_core/CudaDevice.cpp b/src/torchcodec/_core/CudaDeviceInterface.cpp similarity index 94% rename from src/torchcodec/_core/CudaDevice.cpp rename to src/torchcodec/_core/CudaDeviceInterface.cpp index 4f6c7407..24443c68 100644 --- a/src/torchcodec/_core/CudaDevice.cpp +++ b/src/torchcodec/_core/CudaDeviceInterface.cpp @@ -4,7 +4,7 @@ #include #include -#include "src/torchcodec/_core/CudaDevice.h" +#include "src/torchcodec/_core/CudaDeviceInterface.h" #include "src/torchcodec/_core/FFMPEGCommon.h" #include "src/torchcodec/_core/SingleStreamDecoder.h" @@ -16,9 +16,10 @@ extern "C" { namespace facebook::torchcodec { namespace { -bool g_cuda = registerDeviceInterface( - torch::kCUDA, - [](const torch::Device& device) { return new CudaDevice(device); }); +bool g_cuda = + registerDeviceInterface(torch::kCUDA, [](const torch::Device& device) { + return new CudaDeviceInterface(device); + }); // We reuse cuda contexts across VideoDeoder instances. This is because // creating a cuda context is expensive. The cache mechanism is as follows: @@ -163,20 +164,21 @@ AVBufferRef* getCudaContext(const torch::Device& device) { } } // namespace -CudaDevice::CudaDevice(const torch::Device& device) : DeviceInterface(device) { +CudaDeviceInterface::CudaDeviceInterface(const torch::Device& device) + : DeviceInterface(device) { if (device_.type() != torch::kCUDA) { throw std::runtime_error("Unsupported device: " + device_.str()); } } -CudaDevice::~CudaDevice() { +CudaDeviceInterface::~CudaDeviceInterface() { if (ctx_) { addToCacheIfCacheHasCapacity(device_, ctx_); av_buffer_unref(&ctx_); } } -void CudaDevice::initializeContext(AVCodecContext* codecContext) { +void CudaDeviceInterface::initializeContext(AVCodecContext* codecContext) { TORCH_CHECK(!ctx_, "FFmpeg HW device context already initialized"); // It is important for pytorch itself to create the cuda context. If ffmpeg @@ -189,7 +191,7 @@ void CudaDevice::initializeContext(AVCodecContext* codecContext) { return; } -void CudaDevice::convertAVFrameToFrameOutput( +void CudaDeviceInterface::convertAVFrameToFrameOutput( const VideoStreamOptions& videoStreamOptions, UniqueAVFrame& avFrame, FrameOutput& frameOutput, @@ -263,7 +265,8 @@ void CudaDevice::convertAVFrameToFrameOutput( // we have to do this because of an FFmpeg bug where hardware decoding is not // appropriately set, so we just go off and find the matching codec for the CUDA // device -std::optional CudaDevice::findCodec(const AVCodecID& codecId) { +std::optional CudaDeviceInterface::findCodec( + const AVCodecID& codecId) { void* i = nullptr; const AVCodec* codec = nullptr; while ((codec = av_codec_iterate(&i)) != nullptr) { diff --git a/src/torchcodec/_core/CudaDevice.h b/src/torchcodec/_core/CudaDeviceInterface.h similarity index 85% rename from src/torchcodec/_core/CudaDevice.h rename to src/torchcodec/_core/CudaDeviceInterface.h index 3aee6e2b..b60eff7a 100644 --- a/src/torchcodec/_core/CudaDevice.h +++ b/src/torchcodec/_core/CudaDeviceInterface.h @@ -10,11 +10,11 @@ namespace facebook::torchcodec { -class CudaDevice : public DeviceInterface { +class CudaDeviceInterface : public DeviceInterface { public: - CudaDevice(const torch::Device& device); + CudaDeviceInterface(const torch::Device& device); - virtual ~CudaDevice(); + virtual ~CudaDeviceInterface(); std::optional findCodec(const AVCodecID& codecId) override;