Unverified Commit 149edda4 authored by vfdev's avatar vfdev Committed by GitHub
Browse files

[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 38f8e21242830fed46ddf31287edb67c1abd124a.

* Update torchvision/prototype/features/_feature.py
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>
Co-authored-by: default avatarPhilip Meier <github.pmeier@posteo.de>
parent f467349c
......@@ -6,6 +6,7 @@ from typing import Any, Callable, List, Mapping, Optional, Sequence, Tuple, Type
import PIL.Image
import torch
from torch._C import DisableTorchFunction
from torch.types import _device, _dtype, _size
from torchvision.transforms import InterpolationMode
......@@ -128,6 +129,28 @@ class _Feature(torch.Tensor):
_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
......
......@@ -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,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment