Skip to content

Commit 149edda

Browse files
vfdev-5pmeier
andauthored
[proto] Reduce number of calls of __torch_function__ (#6681)
* [proto] Reduce number of calls of __torch_function__ * Use DisableTorchFunction and super * Use self._tensor * Fixes mypy and color space handling * revert Image.new_like * WIP * Perf opt with ref to tensor and properties * Removed requires_grad property * Use _tensor ref * Revert "Use _tensor ref" This reverts commit 38f8e21. * Update torchvision/prototype/features/_feature.py Co-authored-by: Philip Meier <[email protected]> Co-authored-by: Philip Meier <[email protected]>
1 parent f467349 commit 149edda

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

torchvision/prototype/features/_feature.py

+23
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import PIL.Image
77
import torch
88
from torch._C import DisableTorchFunction
9+
from torch.types import _device, _dtype, _size
910
from torchvision.transforms import InterpolationMode
1011

1112

@@ -128,6 +129,28 @@ def _F(self) -> ModuleType:
128129
_Feature.__F = functional
129130
return _Feature.__F
130131

132+
# Add properties for common attributes like shape, dtype, device, ndim etc
133+
# this way we return the result without passing into __torch_function__
134+
@property
135+
def shape(self) -> _size: # type: ignore[override]
136+
with DisableTorchFunction():
137+
return super().shape
138+
139+
@property
140+
def ndim(self) -> int: # type: ignore[override]
141+
with DisableTorchFunction():
142+
return super().ndim
143+
144+
@property
145+
def device(self, *args: Any, **kwargs: Any) -> _device: # type: ignore[override]
146+
with DisableTorchFunction():
147+
return super().device
148+
149+
@property
150+
def dtype(self) -> _dtype: # type: ignore[override]
151+
with DisableTorchFunction():
152+
return super().dtype
153+
131154
def horizontal_flip(self) -> _Feature:
132155
return self
133156

torchvision/prototype/features/_video.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ class Video(_Feature):
1515

1616
@classmethod
1717
def _wrap(cls, tensor: torch.Tensor, *, color_space: ColorSpace) -> Video:
18-
image = tensor.as_subclass(cls)
19-
image.color_space = color_space
20-
return image
18+
video = tensor.as_subclass(cls)
19+
video.color_space = color_space
20+
return video
2121

2222
def __new__(
2323
cls,

0 commit comments

Comments
 (0)