Skip to content

Commit 75a3325

Browse files
authored
Add _core._get_backend_details() utility for testing and debugging (#987)
1 parent 97344e9 commit 75a3325

13 files changed

+57
-19
lines changed

src/torchcodec/_core/BetaCudaDeviceInterface.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -699,4 +699,9 @@ void BetaCudaDeviceInterface::convertAVFrameToFrameOutput(
699699
avFrame, device_, nppCtx_, nvdecStream, preAllocatedOutputTensor);
700700
}
701701

702+
std::string BetaCudaDeviceInterface::getDetails() {
703+
return std::string("Beta CUDA Device Interface. Using ") +
704+
(cpuFallback_ ? "CPU fallback." : "NVDEC.");
705+
}
706+
702707
} // namespace facebook::torchcodec

src/torchcodec/_core/BetaCudaDeviceInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ class BetaCudaDeviceInterface : public DeviceInterface {
5959
int frameReadyForDecoding(CUVIDPICPARAMS* picParams);
6060
int frameReadyInDisplayOrder(CUVIDPARSERDISPINFO* dispInfo);
6161

62+
std::string getDetails() override;
63+
6264
private:
6365
int sendCuvidPacket(CUVIDSOURCEDATAPACKET& cuvidPacket);
6466

src/torchcodec/_core/CpuDeviceInterface.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,4 +346,8 @@ torch::Tensor CpuDeviceInterface::convertAVFrameToTensorUsingFilterGraph(
346346
return rgbAVFrameToTensor(filterGraph_->convert(avFrame));
347347
}
348348

349+
std::string CpuDeviceInterface::getDetails() {
350+
return std::string("CPU Device Interface.");
351+
}
352+
349353
} // namespace facebook::torchcodec

src/torchcodec/_core/CpuDeviceInterface.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ class CpuDeviceInterface : public DeviceInterface {
3939
std::optional<torch::Tensor> preAllocatedOutputTensor =
4040
std::nullopt) override;
4141

42+
std::string getDetails() override;
43+
4244
private:
4345
int convertAVFrameToTensorUsingSwScale(
4446
const UniqueAVFrame& avFrame,

src/torchcodec/_core/CudaDeviceInterface.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,12 @@ void CudaDeviceInterface::convertAVFrameToFrameOutput(
284284
frameOutput.data = cpuFrameOutput.data.to(device_);
285285
}
286286

287+
usingCPUFallback_ = true;
287288
return;
288289
}
289290

291+
usingCPUFallback_ = false;
292+
290293
// Above we checked that the AVFrame was on GPU, but that's not enough, we
291294
// also need to check that the AVFrame is in AV_PIX_FMT_NV12 format (8 bits),
292295
// because this is what the NPP color conversion routines expect. This SHOULD
@@ -351,4 +354,12 @@ std::optional<const AVCodec*> CudaDeviceInterface::findCodec(
351354
return std::nullopt;
352355
}
353356

357+
std::string CudaDeviceInterface::getDetails() {
358+
// Note: for this interface specifically the fallback is only known after a
359+
// frame has been decoded, not before: that's when FFmpeg decides to fallback,
360+
// so we can't know earlier.
361+
return std::string("FFmpeg CUDA Device Interface. Using ") +
362+
(usingCPUFallback_ ? "CPU fallback." : "NVDEC.");
363+
}
364+
354365
} // namespace facebook::torchcodec

src/torchcodec/_core/CudaDeviceInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ class CudaDeviceInterface : public DeviceInterface {
4040
std::optional<torch::Tensor> preAllocatedOutputTensor =
4141
std::nullopt) override;
4242

43+
std::string getDetails() override;
44+
4345
private:
4446
// Our CUDA decoding code assumes NV12 format. In order to handle other
4547
// kinds of input, we need to convert them to NV12. Our current implementation
@@ -60,6 +62,8 @@ class CudaDeviceInterface : public DeviceInterface {
6062
// maybeConvertAVFrameToNV12().
6163
std::unique_ptr<FiltersContext> nv12ConversionContext_;
6264
std::unique_ptr<FilterGraph> nv12Conversion_;
65+
66+
bool usingCPUFallback_ = false;
6367
};
6468

6569
} // namespace facebook::torchcodec

src/torchcodec/_core/DeviceInterface.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ class DeviceInterface {
119119
avcodec_flush_buffers(codecContext_.get());
120120
}
121121

122+
virtual std::string getDetails() {
123+
return "";
124+
}
125+
122126
protected:
123127
torch::Device device_;
124128
SharedAVCodecContext codecContext_;

src/torchcodec/_core/SingleStreamDecoder.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1702,4 +1702,9 @@ double SingleStreamDecoder::getPtsSecondsForFrame(int64_t frameIndex) {
17021702
streamInfo.allFrames[frameIndex].pts, streamInfo.timeBase);
17031703
}
17041704

1705+
std::string SingleStreamDecoder::getDeviceInterfaceDetails() const {
1706+
TORCH_CHECK(deviceInterface_ != nullptr, "Device interface doesn't exist.");
1707+
return deviceInterface_->getDetails();
1708+
}
1709+
17051710
} // namespace facebook::torchcodec

src/torchcodec/_core/SingleStreamDecoder.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ class SingleStreamDecoder {
186186
DecodeStats getDecodeStats() const;
187187
void resetDecodeStats();
188188

189+
std::string getDeviceInterfaceDetails() const;
190+
189191
private:
190192
// --------------------------------------------------------------------------
191193
// STREAMINFO AND ASSOCIATED STRUCTS

src/torchcodec/_core/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
)
1515
from .ops import (
1616
_add_video_stream,
17+
_get_backend_details,
1718
_get_key_frame_indices,
1819
_test_frame_pts_equality,
1920
add_audio_stream,

0 commit comments

Comments
 (0)