Skip to content

Commit 5643d10

Browse files
committed
Drop releaseContext
Signed-off-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@intel.com> tmp Signed-off-by: Dmitry Rogozhkin <dmitry.v.rogozhkin@intel.com>
1 parent 19097f0 commit 5643d10

File tree

5 files changed

+13
-21
lines changed

5 files changed

+13
-21
lines changed

src/torchcodec/decoders/_core/CudaDevice.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ torch::DeviceIndex getFFMPEGCompatibleDeviceIndex(const torch::Device& device) {
5151

5252
void addToCacheIfCacheHasCapacity(
5353
const torch::Device& device,
54-
AVCodecContext* codecContext) {
54+
AVBufferRef* hwContext) {
5555
torch::DeviceIndex deviceIndex = getFFMPEGCompatibleDeviceIndex(device);
5656
if (static_cast<int>(deviceIndex) >= MAX_CUDA_GPUS) {
5757
return;
@@ -62,8 +62,7 @@ void addToCacheIfCacheHasCapacity(
6262
MAX_CONTEXTS_PER_GPU_IN_CACHE) {
6363
return;
6464
}
65-
g_cached_hw_device_ctxs[deviceIndex].push_back(codecContext->hw_device_ctx);
66-
codecContext->hw_device_ctx = nullptr;
65+
g_cached_hw_device_ctxs[deviceIndex].push_back(hwContext);
6766
}
6867

6968
AVBufferRef* getFromCache(const torch::Device& device) {
@@ -168,17 +167,22 @@ CudaDevice::CudaDevice(const torch::Device& device) : DeviceInterface(device) {
168167
}
169168
}
170169

171-
void CudaDevice::releaseContext(AVCodecContext* codecContext) {
172-
addToCacheIfCacheHasCapacity(device_, codecContext);
170+
CudaDevice::~CudaDevice() {
171+
if (ctx_) {
172+
addToCacheIfCacheHasCapacity(device_, ctx_);
173+
}
173174
}
174175

175176
void CudaDevice::initializeContext(AVCodecContext* codecContext) {
177+
TORCH_CHECK(!ctx_, "FFmpeg HW device context already initialized");
178+
176179
// It is important for pytorch itself to create the cuda context. If ffmpeg
177180
// creates the context it may not be compatible with pytorch.
178181
// This is a dummy tensor to initialize the cuda context.
179182
torch::Tensor dummyTensorForCudaInitialization = torch::empty(
180183
{1}, torch::TensorOptions().dtype(torch::kUInt8).device(device_));
181-
codecContext->hw_device_ctx = getCudaContext(device_);
184+
ctx_ = getCudaContext(device_);
185+
codecContext->hw_device_ctx = av_buffer_ref(ctx_);
182186
return;
183187
}
184188

src/torchcodec/decoders/_core/CudaDevice.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class CudaDevice : public DeviceInterface {
1414
public:
1515
CudaDevice(const torch::Device& device);
1616

17-
virtual ~CudaDevice(){};
17+
virtual ~CudaDevice();
1818

1919
std::optional<const AVCodec*> findCodec(const AVCodecID& codecId) override;
2020

@@ -27,7 +27,8 @@ class CudaDevice : public DeviceInterface {
2727
std::optional<torch::Tensor> preAllocatedOutputTensor =
2828
std::nullopt) override;
2929

30-
void releaseContext(AVCodecContext* codecContext) override;
30+
private:
31+
AVBufferRef* ctx_ = nullptr;
3132
};
3233

3334
} // namespace facebook::torchcodec

src/torchcodec/decoders/_core/DeviceInterface.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,6 @@ class DeviceInterface {
4646
VideoDecoder::FrameOutput& frameOutput,
4747
std::optional<torch::Tensor> preAllocatedOutputTensor = std::nullopt) = 0;
4848

49-
virtual void releaseContext(AVCodecContext* codecContext) = 0;
50-
5149
protected:
5250
torch::Device device_;
5351
};

src/torchcodec/decoders/_core/VideoDecoder.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,15 +95,6 @@ VideoDecoder::VideoDecoder(
9595
initializeDecoder();
9696
}
9797

98-
VideoDecoder::~VideoDecoder() {
99-
for (auto& [streamIndex, streamInfo] : streamInfos_) {
100-
auto& deviceInterface = streamInfo.deviceInterface;
101-
if (deviceInterface) {
102-
deviceInterface->releaseContext(streamInfo.codecContext.get());
103-
}
104-
}
105-
}
106-
10798
void VideoDecoder::initializeDecoder() {
10899
TORCH_CHECK(!initialized_, "Attempted double initialization.");
109100

src/torchcodec/decoders/_core/VideoDecoder.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ class DeviceInterface;
2424
// Do not call non-const APIs concurrently on the same object.
2525
class VideoDecoder {
2626
public:
27-
~VideoDecoder();
28-
2927
// --------------------------------------------------------------------------
3028
// CONSTRUCTION API
3129
// --------------------------------------------------------------------------

0 commit comments

Comments
 (0)