Skip to content

Commit e97490e

Browse files
author
Molly Xu
committed
switch _.code._get_backend_details() to new api
1 parent 5ac8321 commit e97490e

File tree

2 files changed

+30
-32
lines changed

2 files changed

+30
-32
lines changed

src/torchcodec/decoders/_video_decoder.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def __init__(self):
4141
self.status_known = False
4242
self._nvcuvid_unavailable = False
4343
self._video_not_supported = False
44+
self._backend = ""
4445

4546
def __bool__(self):
4647
"""Returns True if fallback occurred."""
@@ -60,8 +61,11 @@ def __str__(self):
6061
reasons.append("Video not supported")
6162

6263
if reasons:
63-
return "Fallback status: Falling back due to: " + ", ".join(reasons)
64-
return "Fallback status: No fallback required"
64+
return (
65+
f"[{self._backend}] Fallback status: Falling back due to: "
66+
+ ", ".join(reasons)
67+
)
68+
return f"[{self._backend}] Fallback status: No fallback required"
6569

6670

6771
class VideoDecoder:
@@ -241,6 +245,11 @@ def cpu_fallback(self) -> CpuFallbackStatus:
241245
if "status unknown" not in backend_details:
242246
self._cpu_fallback.status_known = True
243247

248+
for backend in ("FFmpeg CUDA", "Beta CUDA", "CPU"):
249+
if backend_details.startswith(backend):
250+
self._cpu_fallback._backend = backend
251+
break
252+
244253
if "CPU fallback" in backend_details:
245254
if "NVCUVID not available" in backend_details:
246255
self._cpu_fallback._nvcuvid_unavailable = True

test/test_decoders.py

Lines changed: 19 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1672,22 +1672,27 @@ def test_beta_cuda_interface_cpu_fallback(self):
16721672
# to the CPU path, too.
16731673

16741674
ref_dec = VideoDecoder(H265_VIDEO.path, device="cuda")
1675-
ref_frames = ref_dec.get_frame_at(0)
1676-
assert (
1677-
_core._get_backend_details(ref_dec._decoder)
1678-
== "FFmpeg CUDA Device Interface. Using CPU fallback."
1679-
)
1675+
1676+
# Before accessing any frames, status should be unknown
1677+
assert not ref_dec.cpu_fallback.status_known
1678+
1679+
ref_frame = ref_dec.get_frame_at(0)
1680+
1681+
assert "FFmpeg CUDA" in str(ref_dec.cpu_fallback)
1682+
assert ref_dec.cpu_fallback.status_known
1683+
assert bool(ref_dec.cpu_fallback)
16801684

16811685
with set_cuda_backend("beta"):
16821686
beta_dec = VideoDecoder(H265_VIDEO.path, device="cuda")
16831687

1684-
assert (
1685-
_core._get_backend_details(beta_dec._decoder)
1686-
== "Beta CUDA Device Interface. Using CPU fallback."
1687-
)
1688+
assert "Beta CUDA" in str(beta_dec.cpu_fallback)
1689+
# For beta interface, status is known immediately
1690+
assert beta_dec.cpu_fallback.status_known
1691+
assert bool(beta_dec.cpu_fallback)
1692+
16881693
beta_frame = beta_dec.get_frame_at(0)
16891694

1690-
assert psnr(ref_frames.data, beta_frame.data) > 25
1695+
assert psnr(ref_frame.data, beta_frame.data) > 25
16911696

16921697
@needs_cuda
16931698
def test_beta_cuda_interface_error(self):
@@ -1715,7 +1720,8 @@ def test_set_cuda_backend(self):
17151720
# Check that the default is the ffmpeg backend
17161721
assert _get_cuda_backend() == "ffmpeg"
17171722
dec = VideoDecoder(H265_VIDEO.path, device="cuda")
1718-
assert _core._get_backend_details(dec._decoder).startswith("FFmpeg CUDA")
1723+
_ = dec.get_frame_at(0)
1724+
assert "FFmpeg CUDA" in str(dec.cpu_fallback)
17191725

17201726
# Check the setting "beta" effectively uses the BETA backend.
17211727
# We also show that the affects decoder creation only. When the decoder
@@ -1724,9 +1730,9 @@ def test_set_cuda_backend(self):
17241730
with set_cuda_backend("beta"):
17251731
dec = VideoDecoder(H265_VIDEO.path, device="cuda")
17261732
assert _get_cuda_backend() == "ffmpeg"
1727-
assert _core._get_backend_details(dec._decoder).startswith("Beta CUDA")
1733+
assert "Beta CUDA" in str(dec.cpu_fallback)
17281734
with set_cuda_backend("ffmpeg"):
1729-
assert _core._get_backend_details(dec._decoder).startswith("Beta CUDA")
1735+
assert "Beta CUDA" in str(dec.cpu_fallback)
17301736

17311737
# Hacky way to ensure passing "cuda:1" is supported by both backends. We
17321738
# just check that there's an error when passing cuda:N where N is too
@@ -1762,23 +1768,6 @@ def test_cpu_fallback_h265_video_ffmpeg_cuda(self):
17621768
assert bool(decoder.cpu_fallback)
17631769
assert "Fallback status: Falling back due to:" in str(decoder.cpu_fallback)
17641770

1765-
@needs_cuda
1766-
def test_cpu_fallback_h265_video_beta_cuda(self):
1767-
"""Test that H265 video triggers CPU fallback on Beta CUDA interface."""
1768-
with set_cuda_backend("beta"):
1769-
decoder = VideoDecoder(H265_VIDEO.path, device="cuda")
1770-
1771-
# Before accessing any frames, status should be unknown
1772-
assert decoder.cpu_fallback.status_known
1773-
1774-
_ = decoder.get_frame_at(0)
1775-
1776-
# After accessing frames, status should be known
1777-
assert decoder.cpu_fallback.status_known
1778-
1779-
assert bool(decoder.cpu_fallback)
1780-
assert "Fallback status: Falling back due to:" in str(decoder.cpu_fallback)
1781-
17821771
@needs_cuda
17831772
def test_cpu_fallback_no_fallback_on_supported_video(self):
17841773
"""Test that supported videos don't trigger fallback on CUDA."""

0 commit comments

Comments
 (0)