_datapoint.py 6.57 KB
Newer Older
1
2
from __future__ import annotations

3
from typing import Any, Callable, Dict, Mapping, Optional, Sequence, Tuple, Type, TypeVar, Union
Philip Meier's avatar
Philip Meier committed
4
5

import torch
6
from torch._C import DisableTorchFunctionSubclass
7
from torch.types import _device, _dtype, _size
Philip Meier's avatar
Philip Meier committed
8

9
10
from torchvision.datapoints._torch_function_helpers import _FORCE_TORCHFUNCTION_SUBCLASS, _must_return_subclass

11

12
D = TypeVar("D", bound="Datapoint")
13
14


15
class Datapoint(torch.Tensor):
16
17
18
19
20
21
22
    """[Beta] Base class for all datapoints.

    You probably don't want to use this class unless you're defining your own
    custom Datapoints. See
    :ref:`sphx_glr_auto_examples_plot_custom_datapoints.py` for details.
    """

23
24
    @staticmethod
    def _to_tensor(
25
26
        data: Any,
        dtype: Optional[torch.dtype] = None,
27
        device: Optional[Union[torch.device, str, int]] = None,
28
        requires_grad: Optional[bool] = None,
29
    ) -> torch.Tensor:
30
31
        if requires_grad is None:
            requires_grad = data.requires_grad if isinstance(data, torch.Tensor) else False
32
        return torch.as_tensor(data, dtype=dtype, device=device).requires_grad_(requires_grad)
Philip Meier's avatar
Philip Meier committed
33

34
    @classmethod
35
    def wrap_like(cls: Type[D], other: D, tensor: torch.Tensor) -> D:
36
        return tensor.as_subclass(cls)
Philip Meier's avatar
Philip Meier committed
37

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
    @classmethod
    def _wrap_output(
        cls,
        output: torch.Tensor,
        args: Sequence[Any] = (),
        kwargs: Optional[Mapping[str, Any]] = None,
    ) -> torch.Tensor:
        # Same as torch._tensor._convert
        if isinstance(output, torch.Tensor) and not isinstance(output, cls):
            output = output.as_subclass(cls)

        if isinstance(output, (tuple, list)):
            # Also handles things like namedtuples
            output = type(output)(cls._wrap_output(part, args, kwargs) for part in output)
        return output
53

Philip Meier's avatar
Philip Meier committed
54
55
56
57
58
59
60
61
    @classmethod
    def __torch_function__(
        cls,
        func: Callable[..., torch.Tensor],
        types: Tuple[Type[torch.Tensor], ...],
        args: Sequence[Any] = (),
        kwargs: Optional[Mapping[str, Any]] = None,
    ) -> torch.Tensor:
62
63
64
65
66
67
68
        """For general information about how the __torch_function__ protocol works,
        see https://pytorch.org/docs/stable/notes/extending.html#extending-torch

        TL;DR: Every time a PyTorch operator is called, it goes through the inputs and looks for the
        ``__torch_function__`` method. If one is found, it is invoked with the operator as ``func`` as well as the
        ``args`` and ``kwargs`` of the original call.

69
        The default behavior of :class:`~torch.Tensor`'s is to retain a custom tensor type. For the :class:`Datapoint`
70
71
        use case, this has two downsides:

72
        1. Since some :class:`Datapoint`'s require metadata to be constructed, the default wrapping, i.e.
73
74
75
           ``return cls(func(*args, **kwargs))``, will fail for them.
        2. For most operations, there is no way of knowing if the input type is still valid for the output.

76
        For these reasons, the automatic output wrapping is turned off for most operators. The only exceptions are
77
        listed in _FORCE_TORCHFUNCTION_SUBCLASS
78
        """
79
80
81
82
83
84
        # Since super().__torch_function__ has no hook to prevent the coercing of the output into the input type, we
        # need to reimplement the functionality.

        if not all(issubclass(cls, t) for t in types):
            return NotImplemented

85
86
        # Like in the base Tensor.__torch_function__ implementation, it's easier to always use
        # DisableTorchFunctionSubclass and then manually re-wrap the output if necessary
87
        with DisableTorchFunctionSubclass():
88
            output = func(*args, **kwargs or dict())
Philip Meier's avatar
Philip Meier committed
89

90
91
        must_return_subclass = _must_return_subclass()
        if must_return_subclass or (func in _FORCE_TORCHFUNCTION_SUBCLASS and isinstance(args[0], cls)):
92
            # We also require the primary operand, i.e. `args[0]`, to be
93
94
            # an instance of the class that `__torch_function__` was invoked on. The __torch_function__ protocol will
            # invoke this method on *all* types involved in the computation by walking the MRO upwards. For example,
95
96
97
            # `torch.Tensor(...).to(datapoints.Image(...))` will invoke `datapoints.Image.__torch_function__` with
            # `args = (torch.Tensor(), datapoints.Image())` first. Without this guard, the original `torch.Tensor` would
            # be wrapped into a `datapoints.Image`.
98
            return cls._wrap_output(output, args, kwargs)
99

100
        if not must_return_subclass and isinstance(output, cls):
101
102
103
            # DisableTorchFunctionSubclass is ignored by inplace ops like `.add_(...)`,
            # so for those, the output is still a Datapoint. Thus, we need to manually unwrap.
            return output.as_subclass(torch.Tensor)
104

105
        return output
106

107
108
109
110
111
112
    def _make_repr(self, **kwargs: Any) -> str:
        # This is a poor man's implementation of the proposal in https://github.com/pytorch/pytorch/issues/76532.
        # If that ever gets implemented, remove this in favor of the solution on the `torch.Tensor` class.
        extra_repr = ", ".join(f"{key}={value}" for key, value in kwargs.items())
        return f"{super().__repr__()[:-1]}, {extra_repr})"

113
114
115
116
    # Add properties for common attributes like shape, dtype, device, ndim etc
    # this way we return the result without passing into __torch_function__
    @property
    def shape(self) -> _size:  # type: ignore[override]
117
        with DisableTorchFunctionSubclass():
118
119
120
121
            return super().shape

    @property
    def ndim(self) -> int:  # type: ignore[override]
122
        with DisableTorchFunctionSubclass():
123
124
125
126
            return super().ndim

    @property
    def device(self, *args: Any, **kwargs: Any) -> _device:  # type: ignore[override]
127
        with DisableTorchFunctionSubclass():
128
129
130
131
            return super().device

    @property
    def dtype(self) -> _dtype:  # type: ignore[override]
132
        with DisableTorchFunctionSubclass():
133
134
            return super().dtype

135
136
137
138
139
    def __deepcopy__(self: D, memo: Dict[int, Any]) -> D:
        # We need to detach first, since a plain `Tensor.clone` will be part of the computation graph, which does
        # *not* happen for `deepcopy(Tensor)`. A side-effect from detaching is that the `Tensor.requires_grad`
        # attribute is cleared, so we need to refill it before we return.
        # Note: We don't explicitly handle deep-copying of the metadata here. The only metadata we currently have is
Philip Meier's avatar
Philip Meier committed
140
        # `BoundingBoxes.format` and `BoundingBoxes.canvas_size`, which are immutable and thus implicitly deep-copied by
141
        # `BoundingBoxes.clone()`.
142
        return self.detach().clone().requires_grad_(self.requires_grad)  # type: ignore[return-value]