From d84095ea918b32f39cced57bd70a3cb961c8a85c Mon Sep 17 00:00:00 2001 From: Marek Dabek Date: Wed, 27 May 2026 10:21:42 +0200 Subject: [PATCH 1/2] Exception for isolated objective operator execution Signed-off-by: Marek Dabek --- .../dali/experimental/torchvision/v2/compose.py | 2 +- .../dali/experimental/torchvision/v2/operator.py | 12 ++++++++++++ dali/test/python/torchvision/test_tv_compose.py | 9 +++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/dali/python/nvidia/dali/experimental/torchvision/v2/compose.py b/dali/python/nvidia/dali/experimental/torchvision/v2/compose.py index 54cf80d7d36..2fe3a6e5082 100644 --- a/dali/python/nvidia/dali/experimental/torchvision/v2/compose.py +++ b/dali/python/nvidia/dali/experimental/torchvision/v2/compose.py @@ -77,7 +77,7 @@ def _pipeline_function(op_list: Sequence, layout: str = "HWC", input_device: str """ input_node = fn.external_source(name="input_data", layout=layout, device=input_device) for op in op_list: - input_node = op(input_node) + input_node = op._invoke(input_node) return input_node diff --git a/dali/python/nvidia/dali/experimental/torchvision/v2/operator.py b/dali/python/nvidia/dali/experimental/torchvision/v2/operator.py index 217851f0987..456c5dcb05c 100644 --- a/dali/python/nvidia/dali/experimental/torchvision/v2/operator.py +++ b/dali/python/nvidia/dali/experimental/torchvision/v2/operator.py @@ -242,7 +242,19 @@ def _kernel(self, data_input): pass def __call__(self, data_input): + """ + Torchvision creates callable objects, but DALI Torchvision needs to implement a pipeline. + The pipeline is implemented in Compose class and uses _invoke to execute operators' logic. + """ + raise RuntimeError( + f"Operator {self!r} is not directly callable. Use it only inside Compose pipeline." + ) + def _invoke(self, data_input): + """ + Private method is used to execute operator from a Compose pipeline + Note: Do not call directly + """ type(self).verify_data(data_input) # Original input is transfered to GPU, before being preprocess_data. diff --git a/dali/test/python/torchvision/test_tv_compose.py b/dali/test/python/torchvision/test_tv_compose.py index 7471becd4b6..d11d694dee0 100644 --- a/dali/test/python/torchvision/test_tv_compose.py +++ b/dali/test/python/torchvision/test_tv_compose.py @@ -422,3 +422,12 @@ def test_compose_non_uint8_dtype_resize(dtype): else: with assert_raises(RuntimeError): _ = dali_pipeline(test_tensor) + + +def test_error_in_isolated_operators(): + """Operators must not be called outside of Compose""" + + test_tensor = torch.ones(5, 3, 10, 10, dtype=torch.uint8) + dali_resize = Resize(size=(7, 7)) + with assert_raises(TypeError, glob="*is not directly callable*"): + _ = dali_resize(test_tensor) From 8301018a342e7663c613f716ca7c18485d85bf60 Mon Sep 17 00:00:00 2001 From: Marek Dabek Date: Wed, 27 May 2026 10:51:06 +0200 Subject: [PATCH 2/2] Adjust Compose tests Signed-off-by: Marek Dabek --- dali/test/python/torchvision/test_tv_compose.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dali/test/python/torchvision/test_tv_compose.py b/dali/test/python/torchvision/test_tv_compose.py index d11d694dee0..61d2920542b 100644 --- a/dali/test/python/torchvision/test_tv_compose.py +++ b/dali/test/python/torchvision/test_tv_compose.py @@ -429,5 +429,5 @@ def test_error_in_isolated_operators(): test_tensor = torch.ones(5, 3, 10, 10, dtype=torch.uint8) dali_resize = Resize(size=(7, 7)) - with assert_raises(TypeError, glob="*is not directly callable*"): + with assert_raises(RuntimeError, glob="*is not directly callable*"): _ = dali_resize(test_tensor)