Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor Datapoint dispatch mechanism #7747

Merged
merged 26 commits into from
Aug 2, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
d9e1379
[PoC] refactor Datapoint dispatch mechanism
pmeier Jul 19, 2023
36b9d36
fix test
pmeier Jul 19, 2023
f36c64c
Merge branch 'main' into kernel-registration
pmeier Jul 26, 2023
bbaa35c
add dispatch to adjust_brightness
pmeier Jul 27, 2023
ca4ad32
enforce no register overwrite
pmeier Jul 27, 2023
d23a80e
[PoC] make wrapping interal kernel more convenient
pmeier Jul 27, 2023
bf47188
[PoC] enforce explicit no-ops
pmeier Jul 27, 2023
74d5054
fix adjust_brightness tests and remove methods
pmeier Jul 27, 2023
e88be5e
Merge branch 'main' into kernel-registration
pmeier Jul 27, 2023
f178373
address minor comments
pmeier Jul 27, 2023
65e80d0
make no-op registration a decorator
pmeier Jul 28, 2023
9614477
Merge branch 'main'
pmeier Aug 1, 2023
6ac08e4
explicit metadata
pmeier Aug 1, 2023
cac079b
implement dispatchers for erase five/ten_crop and temporal_subsample
pmeier Aug 1, 2023
c7256b4
make shape getters proper dispatchers
pmeier Aug 1, 2023
bf78cd6
fix
pmeier Aug 1, 2023
f86f89b
port normalize and to_dtype
pmeier Aug 2, 2023
d90daf6
address comments
pmeier Aug 2, 2023
09eec9a
address comments and cleanup
pmeier Aug 2, 2023
3730811
more cleanup
pmeier Aug 2, 2023
7203453
Merge branch 'main' into kernel-registration
pmeier Aug 2, 2023
31bee5f
port all remaining dispatchers to the new mechanism
pmeier Jul 28, 2023
a924013
put back legacy test_dispatch_datapoint
pmeier Aug 2, 2023
b3c2c88
minor test fixes
pmeier Aug 2, 2023
a1f5ea4
Update torchvision/transforms/v2/functional/_utils.py
pmeier Aug 2, 2023
d29d95b
reinstante antialias tests
pmeier Aug 2, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions test/test_transforms_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -1419,8 +1419,6 @@ def test_antialias_warning():
with pytest.warns(UserWarning, match=match):
datapoints.Image(tensor_img).resized_crop(0, 0, 10, 10, (20, 20))

with pytest.warns(UserWarning, match=match):
datapoints.Video(tensor_video).resize((20, 20))
pmeier marked this conversation as resolved.
Show resolved Hide resolved
with pytest.warns(UserWarning, match=match):
datapoints.Video(tensor_video).resized_crop(0, 0, 10, 10, (20, 20))

Expand Down
27 changes: 16 additions & 11 deletions test/test_transforms_v2_refactored.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,17 +165,20 @@ def _check_dispatcher_dispatch(dispatcher, kernel, input, *args, **kwargs):
preserved in doing so. For bounding boxes also checks that the format is preserved.
"""
if isinstance(input, datapoints._datapoint.Datapoint):
# Due to our complex dispatch architecture for datapoints, we cannot spy on the kernel directly,
# but rather have to patch the `Datapoint.__F` attribute to contain the spied on kernel.
spy = mock.MagicMock(wraps=kernel, name=kernel.__name__)
with mock.patch.object(F, kernel.__name__, spy):
# Due to Python's name mangling, the `Datapoint.__F` attribute is only accessible from inside the class.
# Since that is not the case here, we need to prefix f"_{cls.__name__}"
# See https://docs.python.org/3/tutorial/classes.html#private-variables for details
with mock.patch.object(datapoints._datapoint.Datapoint, "_Datapoint__F", new=F):
output = dispatcher(input, *args, **kwargs)

spy.assert_called_once()
if dispatcher is F.resize:
pmeier marked this conversation as resolved.
Show resolved Hide resolved
output = dispatcher(input, *args, **kwargs)
else:
# Due to our complex dispatch architecture for datapoints, we cannot spy on the kernel directly,
# but rather have to patch the `Datapoint.__F` attribute to contain the spied on kernel.
spy = mock.MagicMock(wraps=kernel, name=kernel.__name__)
with mock.patch.object(F, kernel.__name__, spy):
# Due to Python's name mangling, the `Datapoint.__F` attribute is only accessible from inside the class.
# Since that is not the case here, we need to prefix f"_{cls.__name__}"
# See https://docs.python.org/3/tutorial/classes.html#private-variables for details
with mock.patch.object(datapoints._datapoint.Datapoint, "_Datapoint__F", new=F):
output = dispatcher(input, *args, **kwargs)

spy.assert_called_once()
else:
with mock.patch(f"{dispatcher.__module__}.{kernel.__name__}", wraps=kernel) as spy:
output = dispatcher(input, *args, **kwargs)
Expand Down Expand Up @@ -249,6 +252,8 @@ def _check_dispatcher_kernel_signature_match(dispatcher, *, kernel, input_type):

def _check_dispatcher_datapoint_signature_match(dispatcher):
"""Checks if the signature of the dispatcher matches the corresponding method signature on the Datapoint class."""
if dispatcher is F.resize:
return
pmeier marked this conversation as resolved.
Show resolved Hide resolved
dispatcher_signature = inspect.signature(dispatcher)
dispatcher_params = list(dispatcher_signature.parameters.values())[1:]

Expand Down
15 changes: 0 additions & 15 deletions torchvision/datapoints/_bounding_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,21 +110,6 @@ def vertical_flip(self) -> BoundingBox:
)
return BoundingBox.wrap_like(self, output)

def resize( # type: ignore[override]
self,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> BoundingBox:
output, spatial_size = self._F.resize_bounding_box(
self.as_subclass(torch.Tensor),
spatial_size=self.spatial_size,
size=size,
max_size=max_size,
)
return BoundingBox.wrap_like(self, output, spatial_size=spatial_size)

def crop(self, top: int, left: int, height: int, width: int) -> BoundingBox:
output, spatial_size = self._F.crop_bounding_box(
self.as_subclass(torch.Tensor), self.format, top=top, left=left, height=height, width=width
Expand Down
11 changes: 0 additions & 11 deletions torchvision/datapoints/_datapoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,6 @@ def horizontal_flip(self) -> Datapoint:
def vertical_flip(self) -> Datapoint:
return self

# TODO: We have to ignore override mypy error as there is torch.Tensor built-in deprecated op: Tensor.resize
# https://github.com/pytorch/pytorch/blob/e8727994eb7cdb2ab642749d6549bc497563aa06/torch/_tensor.py#L588-L593
def resize( # type: ignore[override]
self,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> Datapoint:
return self

def crop(self, top: int, left: int, height: int, width: int) -> Datapoint:
return self

Expand Down
12 changes: 0 additions & 12 deletions torchvision/datapoints/_image.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,6 @@ def vertical_flip(self) -> Image:
output = self._F.vertical_flip_image_tensor(self.as_subclass(torch.Tensor))
return Image.wrap_like(self, output)

def resize( # type: ignore[override]
self,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> Image:
output = self._F.resize_image_tensor(
self.as_subclass(torch.Tensor), size, interpolation=interpolation, max_size=max_size, antialias=antialias
)
return Image.wrap_like(self, output)

def crop(self, top: int, left: int, height: int, width: int) -> Image:
output = self._F.crop_image_tensor(self.as_subclass(torch.Tensor), top, left, height, width)
return Image.wrap_like(self, output)
Expand Down
10 changes: 0 additions & 10 deletions torchvision/datapoints/_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,6 @@ def vertical_flip(self) -> Mask:
output = self._F.vertical_flip_mask(self.as_subclass(torch.Tensor))
return Mask.wrap_like(self, output)

def resize( # type: ignore[override]
self,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.NEAREST,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> Mask:
output = self._F.resize_mask(self.as_subclass(torch.Tensor), size, max_size=max_size)
return Mask.wrap_like(self, output)

def crop(self, top: int, left: int, height: int, width: int) -> Mask:
output = self._F.crop_mask(self.as_subclass(torch.Tensor), top, left, height, width)
return Mask.wrap_like(self, output)
Expand Down
16 changes: 0 additions & 16 deletions torchvision/datapoints/_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,6 @@ def vertical_flip(self) -> Video:
output = self._F.vertical_flip_video(self.as_subclass(torch.Tensor))
return Video.wrap_like(self, output)

def resize( # type: ignore[override]
self,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> Video:
output = self._F.resize_video(
self.as_subclass(torch.Tensor),
size,
interpolation=interpolation,
max_size=max_size,
antialias=antialias,
)
return Video.wrap_like(self, output)

def crop(self, top: int, left: int, height: int, width: int) -> Video:
output = self._F.crop_video(self.as_subclass(torch.Tensor), top, left, height, width)
return Video.wrap_like(self, output)
Expand Down
2 changes: 1 addition & 1 deletion torchvision/transforms/v2/functional/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from torchvision.transforms import InterpolationMode # usort: skip

from ._utils import is_simple_tensor # usort: skip
from ._utils import is_simple_tensor, register_kernel # usort: skip
pmeier marked this conversation as resolved.
Show resolved Hide resolved

from ._meta import (
clamp_bounding_box,
Expand Down
68 changes: 43 additions & 25 deletions torchvision/transforms/v2/functional/_geometry.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

from ._meta import clamp_bounding_box, convert_format_bounding_box, get_spatial_size_image_pil

from ._utils import is_simple_tensor
from ._utils import _get_kernel, is_simple_tensor, register_kernel


def _check_interpolation(interpolation: Union[InterpolationMode, int]) -> InterpolationMode:
Expand Down Expand Up @@ -158,6 +158,32 @@ def _compute_resized_output_size(
return __compute_resized_output_size(spatial_size, size=size, max_size=max_size)


def resize(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to move the definition of the dispatcher above the kernel definitions, since the dispatcher is used in the decorator. Other than that, only the datapoint branch was changed.

inpt: datapoints._InputTypeJIT,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> datapoints._InputTypeJIT:
if not torch.jit.is_scripting():
_log_api_usage_once(resize)
if torch.jit.is_scripting() or is_simple_tensor(inpt):
return resize_image_tensor(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias)
elif isinstance(inpt, datapoints._datapoint.Datapoint):
kernel = _get_kernel(resize, type(inpt))
return kernel(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias)
elif isinstance(inpt, PIL.Image.Image):
if antialias is False:
warnings.warn("Anti-alias option is always applied for PIL Image input. Argument antialias is ignored.")
return resize_image_pil(inpt, size, interpolation=interpolation, max_size=max_size)
else:
raise TypeError(
f"Input can either be a plain tensor, any TorchVision datapoint, or a PIL image, "
f"but got {type(inpt)} instead."
)


@register_kernel(resize, datapoints.Image)
pmeier marked this conversation as resolved.
Show resolved Hide resolved
def resize_image_tensor(
image: torch.Tensor,
size: List[int],
Expand Down Expand Up @@ -274,6 +300,11 @@ def resize_mask(mask: torch.Tensor, size: List[int], max_size: Optional[int] = N
return output


@register_kernel(resize, datapoints.Mask)
def _resize_mask_dispatch(mask: torch.Tensor, size: List[int], max_size: Optional[int] = None, **kwargs):
return resize_mask(mask, size, max_size=max_size)
pmeier marked this conversation as resolved.
Show resolved Hide resolved


def resize_bounding_box(
bounding_box: torch.Tensor, spatial_size: Tuple[int, int], size: List[int], max_size: Optional[int] = None
) -> Tuple[torch.Tensor, Tuple[int, int]]:
Expand All @@ -292,6 +323,17 @@ def resize_bounding_box(
)


@register_kernel(resize, datapoints.BoundingBox, datapoint_wrapping=False)
def _resize_bounding_box_dispatch(
bounding_box: datapoints.BoundingBox, size: List[int], max_size: Optional[int] = None, **kwargs
):
pmeier marked this conversation as resolved.
Show resolved Hide resolved
output, spatial_size = resize_bounding_box(
bounding_box.as_subclass(torch.Tensor), bounding_box.spatial_size, size, max_size=max_size
)
return datapoints.BoundingBox.wrap_like(bounding_box, output, spatial_size=spatial_size)


@register_kernel(resize, datapoints.Video)
def resize_video(
video: torch.Tensor,
size: List[int],
Expand All @@ -302,30 +344,6 @@ def resize_video(
return resize_image_tensor(video, size=size, interpolation=interpolation, max_size=max_size, antialias=antialias)


def resize(
inpt: datapoints._InputTypeJIT,
size: List[int],
interpolation: Union[InterpolationMode, int] = InterpolationMode.BILINEAR,
max_size: Optional[int] = None,
antialias: Optional[Union[str, bool]] = "warn",
) -> datapoints._InputTypeJIT:
if not torch.jit.is_scripting():
_log_api_usage_once(resize)
if torch.jit.is_scripting() or is_simple_tensor(inpt):
return resize_image_tensor(inpt, size, interpolation=interpolation, max_size=max_size, antialias=antialias)
elif isinstance(inpt, datapoints._datapoint.Datapoint):
return inpt.resize(size, interpolation=interpolation, max_size=max_size, antialias=antialias)
elif isinstance(inpt, PIL.Image.Image):
if antialias is False:
warnings.warn("Anti-alias option is always applied for PIL Image input. Argument antialias is ignored.")
return resize_image_pil(inpt, size, interpolation=interpolation, max_size=max_size)
else:
raise TypeError(
f"Input can either be a plain tensor, any TorchVision datapoint, or a PIL image, "
f"but got {type(inpt)} instead."
)


def _affine_parse_args(
angle: Union[int, float],
translate: List[float],
Expand Down
23 changes: 23 additions & 0 deletions torchvision/transforms/v2/functional/_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import functools
from typing import Any

import torch
Expand All @@ -6,3 +7,25 @@

def is_simple_tensor(inpt: Any) -> bool:
return isinstance(inpt, torch.Tensor) and not isinstance(inpt, Datapoint)


_KERNEL_REGISTRY = {}


def register_kernel(dispatcher, datapoint_cls, *, datapoint_wrapping=True):
def datapoint_wrapper(kernel):
@functools.wraps(kernel)
def wrapper(inpt, *args, **kwargs):
return type(inpt).wrap_like(inpt, kernel(inpt.as_subclass(torch.Tensor), *args, **kwargs))

return wrapper
pmeier marked this conversation as resolved.
Show resolved Hide resolved

def decorator(kernel):
_KERNEL_REGISTRY[(dispatcher, datapoint_cls)] = datapoint_wrapper(kernel) if datapoint_wrapping else kernel
return kernel

return decorator


def _get_kernel(dispatcher, datapoint_cls):
return _KERNEL_REGISTRY[(dispatcher, datapoint_cls)]
pmeier marked this conversation as resolved.
Show resolved Hide resolved
Loading