|
18 | 18 | from torchcodec.decoders import ( |
19 | 19 | AudioDecoder, |
20 | 20 | AudioStreamMetadata, |
| 21 | + set_cuda_backend, |
21 | 22 | VideoDecoder, |
22 | 23 | VideoStreamMetadata, |
23 | 24 | ) |
| 25 | +from torchcodec.decoders._decoder_utils import _get_cuda_backend |
24 | 26 |
|
25 | 27 | from .utils import ( |
26 | 28 | all_supported_devices, |
@@ -1702,9 +1704,63 @@ def test_beta_cuda_interface_small_h265(self): |
1702 | 1704 |
|
1703 | 1705 | @needs_cuda |
1704 | 1706 | def test_beta_cuda_interface_error(self): |
1705 | | - with pytest.raises(RuntimeError, match="Unsupported device"): |
| 1707 | + with pytest.raises(RuntimeError, match="Invalid device string"): |
1706 | 1708 | VideoDecoder(NASA_VIDEO.path, device="cuda:0:bad_variant") |
1707 | 1709 |
|
| 1710 | + @needs_cuda |
| 1711 | + def test_set_cuda_backend(self): |
| 1712 | + # Tests for the set_cuda_backend() context manager. |
| 1713 | + |
| 1714 | + with pytest.raises(ValueError, match="Invalid CUDA backend"): |
| 1715 | + with set_cuda_backend("bad_backend"): |
| 1716 | + pass |
| 1717 | + |
| 1718 | + # set_cuda_backend() is meant to be used as a context manager. Using it |
| 1719 | + # as a global call does nothing because the "context" is exited right |
| 1720 | + # away. This is a good thing, we prefer users to use it as a CM only. |
| 1721 | + set_cuda_backend("beta") |
| 1722 | + assert _get_cuda_backend() == "ffmpeg" # Not changed to "beta". |
| 1723 | + |
| 1724 | + # Case insensitive |
| 1725 | + with set_cuda_backend("BETA"): |
| 1726 | + assert _get_cuda_backend() == "beta" |
| 1727 | + |
| 1728 | + def assert_decoder_uses(decoder, *, expected_backend): |
| 1729 | + # Assert that a decoder instance is using a given backend. |
| 1730 | + # |
| 1731 | + # We know H265_VIDEO fails on the BETA backend while it works on the |
| 1732 | + # ffmpeg one. |
| 1733 | + if expected_backend == "ffmpeg": |
| 1734 | + decoder.get_frame_at(0) # this would fail if this was BETA |
| 1735 | + else: |
| 1736 | + with pytest.raises(RuntimeError, match="Video is too small"): |
| 1737 | + decoder.get_frame_at(0) |
| 1738 | + |
| 1739 | + # Check that the default is the ffmpeg backend |
| 1740 | + assert _get_cuda_backend() == "ffmpeg" |
| 1741 | + dec = VideoDecoder(H265_VIDEO.path, device="cuda") |
| 1742 | + assert_decoder_uses(dec, expected_backend="ffmpeg") |
| 1743 | + |
| 1744 | + # Check the setting "beta" effectively uses the BETA backend. |
| 1745 | + # We also show that the affects decoder creation only. When the decoder |
| 1746 | + # is created with a given backend, it stays in this backend for the rest |
| 1747 | + # of its life. This is normal and intended. |
| 1748 | + with set_cuda_backend("beta"): |
| 1749 | + dec = VideoDecoder(H265_VIDEO.path, device="cuda") |
| 1750 | + assert _get_cuda_backend() == "ffmpeg" |
| 1751 | + assert_decoder_uses(dec, expected_backend="beta") |
| 1752 | + with set_cuda_backend("ffmpeg"): |
| 1753 | + assert_decoder_uses(dec, expected_backend="beta") |
| 1754 | + |
| 1755 | + # Hacky way to ensure passing "cuda:1" is supported by both backends. We |
| 1756 | + # just check that there's an error when passing cuda:N where N is too |
| 1757 | + # high. |
| 1758 | + bad_device_number = torch.cuda.device_count() + 1 |
| 1759 | + for backend in ("ffmpeg", "beta"): |
| 1760 | + with pytest.raises(RuntimeError, match="invalid device ordinal"): |
| 1761 | + with set_cuda_backend(backend): |
| 1762 | + VideoDecoder(H265_VIDEO.path, device=f"cuda:{bad_device_number}") |
| 1763 | + |
1708 | 1764 |
|
1709 | 1765 | class TestAudioDecoder: |
1710 | 1766 | @pytest.mark.parametrize("asset", (NASA_AUDIO, NASA_AUDIO_MP3, SINE_MONO_S32)) |
|
0 commit comments