Skip to content

[proto] Reduce number of calls of __torch_function__ #6681

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

Merged
merged 16 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
23 changes: 23 additions & 0 deletions torchvision/prototype/features/_feature.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import PIL.Image
import torch
from torch._C import DisableTorchFunction
from torch.types import _device, _dtype, _size
from torchvision.transforms import InterpolationMode


Expand Down Expand Up @@ -128,6 +129,28 @@ def _F(self) -> ModuleType:
_Feature.__F = functional
return _Feature.__F

# 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]
with DisableTorchFunction():
return super().shape

@property
def ndim(self) -> int: # type: ignore[override]
with DisableTorchFunction():
return super().ndim

@property
def device(self, *args: Any, **kwargs: Any) -> _device: # type: ignore[override]
with DisableTorchFunction():
return super().device

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

def horizontal_flip(self) -> _Feature:
return self

Expand Down
6 changes: 3 additions & 3 deletions torchvision/prototype/features/_video.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ class Video(_Feature):

@classmethod
def _wrap(cls, tensor: torch.Tensor, *, color_space: ColorSpace) -> Video:
image = tensor.as_subclass(cls)
image.color_space = color_space
return image
video = tensor.as_subclass(cls)
video.color_space = color_space
return video

def __new__(
cls,
Expand Down