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..61d2920542b 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(RuntimeError, glob="*is not directly callable*"): + _ = dali_resize(test_tensor)