Skip to content
Closed
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
11 changes: 4 additions & 7 deletions docarray/typing/tensor/torch_tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def validate(
field: 'ModelField',
config: 'BaseConfig',
) -> T:
if isinstance(value, TorchTensor):
if isinstance(value, cls):
return cast(T, value)
elif isinstance(value, torch.Tensor):
return cls._docarray_from_native(value)
Expand Down Expand Up @@ -195,10 +195,7 @@ def _docarray_from_native(cls: Type[T], value: torch.Tensor) -> T:
:param value: the native `torch.Tensor`
:return: a `TorchTensor`
"""
if cls.__unparametrizedcls__: # This is not None if the tensor is parametrized
value.__class__ = cls.__unparametrizedcls__ # type: ignore
else:
value.__class__ = cls
value.__class__ = cls
return cast(T, value)

@classmethod
Expand Down Expand Up @@ -254,11 +251,11 @@ def __torch_function__(cls, func, types, args=(), kwargs=None):
# this tells torch to treat all of our custom tensors just like
# torch.Tensor's. Otherwise, torch will complain that it doesn't
# know how to handle our custom tensor type.
docarray_torch_tensors = TorchTensor.__subclasses__()
docarray_torch_tensors = cls.__subclasses__() + [cls]
types_ = tuple(
torch.Tensor if t in docarray_torch_tensors else t for t in types
)
return super().__torch_function__(func, types_, args, kwargs)
return torch.Tensor.__torch_function__(func, types_, args, kwargs)

@classmethod
def _docarray_from_ndarray(cls: Type[T], value: np.ndarray) -> T:
Expand Down