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
5 changes: 1 addition & 4 deletions src/torchcodec/_core/BetaCudaDeviceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,7 @@ BetaCudaDeviceInterface::BetaCudaDeviceInterface(const torch::Device& device)
TORCH_CHECK(
device_.type() == torch::kCUDA, "Unsupported device: ", device_.str());

// Initialize CUDA context with a dummy tensor
torch::Tensor dummyTensorForCudaInitialization = torch::empty(
{1}, torch::TensorOptions().dtype(torch::kUInt8).device(device_));

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

Expand Down
8 changes: 8 additions & 0 deletions src/torchcodec/_core/CUDACommon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ PerGpuCache<NppStreamContext> g_cached_npp_ctxs(

} // namespace

void initializeCudaContextWithPytorch(const torch::Device& device) {
// It is important for pytorch itself to create the cuda context. If ffmpeg
// creates the context it may not be compatible with pytorch.
// This is a dummy tensor to initialize the cuda context.
torch::Tensor dummyTensorForCudaInitialization = torch::zeros(
{1}, torch::TensorOptions().dtype(torch::kUInt8).device(device));
}

/* clang-format off */
// Note: [YUV -> RGB Color Conversion, color space and color range]
//
Expand Down
2 changes: 2 additions & 0 deletions src/torchcodec/_core/CUDACommon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ extern "C" {

namespace facebook::torchcodec {

void initializeCudaContextWithPytorch(const torch::Device& device);

// Unique pointer type for NPP stream context
using UniqueNppContext = std::unique_ptr<NppStreamContext>;

Expand Down
9 changes: 4 additions & 5 deletions src/torchcodec/_core/CudaDeviceInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,10 @@ CudaDeviceInterface::CudaDeviceInterface(const torch::Device& device)
TORCH_CHECK(
device_.type() == torch::kCUDA, "Unsupported device: ", device_.str());

// It is important for pytorch itself to create the cuda context. If ffmpeg
// creates the context it may not be compatible with pytorch.
// This is a dummy tensor to initialize the cuda context.
torch::Tensor dummyTensorForCudaInitialization = torch::empty(
{1}, torch::TensorOptions().dtype(torch::kUInt8).device(device_));
initializeCudaContextWithPytorch(device_);

// TODO rename this, this is a hardware device context, not a CUDA context!
// See https://github.com/meta-pytorch/torchcodec/issues/924
ctx_ = getCudaContext(device_);
nppCtx_ = getNppStreamContext(device_);
}
Expand Down
Loading