Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/torchcodec/_core/BetaCudaDeviceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
#include "src/torchcodec/_core/FFMPEGCommon.h"
#include "src/torchcodec/_core/NVDECCache.h"

// #include <cuda_runtime.h> // For cudaStreamSynchronize
#include "src/torchcodec/_core/NVCUVIDRuntimeLoader.h"
#include "src/torchcodec/_core/nvcuvid_include/cuviddec.h"
#include "src/torchcodec/_core/nvcuvid_include/nvcuvid.h"

Expand Down Expand Up @@ -155,6 +155,7 @@ std::optional<cudaVideoCodec> validateCodecSupport(AVCodecID codecId) {
bool nativeNVDECSupport(const SharedAVCodecContext& codecContext) {
// Return true iff the input video stream is supported by our NVDEC
// implementation.

auto codecType = validateCodecSupport(codecContext->codec_id);
if (!codecType.has_value()) {
return false;
Expand Down Expand Up @@ -222,6 +223,8 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)

initializeCudaContextWithPytorch(device_);
nppCtx_ = getNppStreamContext(device_);

nvcuvidAvailable_ = loadNVCUVIDLibrary();
}

BetaCudaDeviceInterface::~BetaCudaDeviceInterface() {
Expand Down Expand Up @@ -249,7 +252,7 @@ void BetaCudaDeviceInterface::initialize(
const AVStream* avStream,
const UniqueDecodingAVFormatContext& avFormatCtx,
[[maybe_unused]] const SharedAVCodecContext& codecContext) {
if (!nativeNVDECSupport(codecContext)) {
if (!nvcuvidAvailable_ || !nativeNVDECSupport(codecContext)) {
cpuFallback_ = createDeviceInterface(torch::kCPU);
TORCH_CHECK(
cpuFallback_ != nullptr, "Failed to create CPU device interface");
Expand Down Expand Up @@ -700,8 +703,16 @@ void BetaCudaDeviceInterface::convertAVFrameToFrameOutput(
}

std::string BetaCudaDeviceInterface::getDetails() {
return std::string("Beta CUDA Device Interface. Using ") +
(cpuFallback_ ? "CPU fallback." : "NVDEC.");
std::string details = "Beta CUDA Device Interface.";
if (cpuFallback_) {
details += " Using CPU fallback.";
if (!nvcuvidAvailable_) {
details += " NVCUVID not available!";
}
} else {
details += " Using NVDEC.";
}
return details;
}

} // namespace facebook::torchcodec
1 change: 1 addition & 0 deletions src/torchcodec/_core/BetaCudaDeviceInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ class BetaCudaDeviceInterface : public DeviceInterface {
UniqueNppContext nppCtx_;

std::unique_ptr<DeviceInterface> cpuFallback_;
bool nvcuvidAvailable_ = false;
};

} // namespace facebook::torchcodec
Expand Down
20 changes: 1 addition & 19 deletions src/torchcodec/_core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ function(make_torchcodec_libraries
)

if(ENABLE_CUDA)
list(APPEND core_sources CudaDeviceInterface.cpp BetaCudaDeviceInterface.cpp NVDECCache.cpp CUDACommon.cpp)
list(APPEND core_sources CudaDeviceInterface.cpp BetaCudaDeviceInterface.cpp NVDECCache.cpp CUDACommon.cpp NVCUVIDRuntimeLoader.cpp)
endif()

set(core_library_dependencies
Expand All @@ -108,27 +108,9 @@ function(make_torchcodec_libraries
)

if(ENABLE_CUDA)
# Try to find NVCUVID. Try the normal way first. This should work locally.
find_library(NVCUVID_LIBRARY NAMES nvcuvid)
# If not found, try with version suffix, or hardcoded path. Appears
# to be necessary on the CI.
if(NOT NVCUVID_LIBRARY)
find_library(NVCUVID_LIBRARY NAMES nvcuvid.1 PATHS /usr/lib64 /usr/lib)
endif()
if(NOT NVCUVID_LIBRARY)
set(NVCUVID_LIBRARY "/usr/lib64/libnvcuvid.so.1")
endif()

if(NVCUVID_LIBRARY)
message(STATUS "Found NVCUVID: ${NVCUVID_LIBRARY}")
else()
message(FATAL_ERROR "Could not find NVCUVID library")
endif()

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seeing a bunch of cmake code removed makes me irrationally happy. :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait until you see the dlopen() stuff 😅

list(APPEND core_library_dependencies
${CUDA_nppi_LIBRARY}
${CUDA_nppicc_LIBRARY}
${NVCUVID_LIBRARY}
)
endif()

Expand Down
Loading
Loading