Skip to content

refactor: make AnyTensor a class#1552

Merged
samsja merged 15 commits into
docarray:mainfrom
makram93:fix-type-coercion-np
May 30, 2023
Merged

refactor: make AnyTensor a class#1552
samsja merged 15 commits into
docarray:mainfrom
makram93:fix-type-coercion-np

Conversation

@makram93

@makram93 makram93 commented May 17, 2023

Copy link
Copy Markdown
Contributor

This PR solves #1439 .

As mentioned by @JohannesMessner, simply updating the validate() method of NdArray to support type coercion from Torch/TensorFlow to NumPy produces unwanted side-effects with AnyTensor.

AnyTensor is currently defined as: AnyTensor == Union[NdArray, TorchTensor, TensorFlowTensor]. Union validates the type from left to right, as such if we give torch tensors then it gets converted to NdArray. If we change the order as
AnyTensor == Union[TorchTensor, TensorFlowTensor, NdArray] then the NumPy value gets converted to TorchTensor.

In this PR:

  • Make AnyTensor a class that inherits from the AbstractTensors and implements the override() method only. This method basically checks the type of value and returns the class accordingly.
    Note: AnyTensor does not inherit [TorchTensor, TensorflowTensor, NdArray] neither does it implement any functions from the classes
  • Keep the union of tensors inside TYPE_CHECKING so that mypy does not complain about the missing attribute.
  • Similar changes have been done for AnyEmbedding, AudioTensor, ImageTensor, VideoTensor.
  • Allow coercion of [torch.Tensor, TorchTensor, tf.Tensor, TensorFlowTensor] to np in NdArray

makram93 added 7 commits May 17, 2023 17:38
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
@makram93 makram93 force-pushed the fix-type-coercion-np branch from 693f57a to 7465cd3 Compare May 24, 2023 09:18
makram93 added 3 commits May 24, 2023 11:41
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Comment thread docarray/utils/_internal/_typing.py Outdated
Comment thread docarray/utils/_internal/_typing.py Outdated
Comment thread docarray/typing/tensor/tensor.py Outdated
Comment thread docarray/typing/tensor/image/image_tensor.py Outdated
@JohannesMessner JohannesMessner changed the title Fix type coercion to any refactor: make AnyTensor a class May 24, 2023
@JohannesMessner

Copy link
Copy Markdown
Member

@makram93 could you pleas update the description of the PR?
The problem you are solving, and how you are solving it. Thanks you!

makram93 added 3 commits May 24, 2023 16:30
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>
@makram93 makram93 marked this pull request as ready for review May 24, 2023 15:06
@JoanFM JoanFM linked an issue May 25, 2023 that may be closed by this pull request

@JohannesMessner JohannesMessner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Big picture looks good, I think this is the way we should solve this!! I will do a more thorough review when I have time (thursday, friday I am not so docarray focused)

Comment thread docarray/typing/tensor/ndarray.py Outdated
Comment thread docarray/typing/tensor/tensor.py
Comment thread docarray/typing/tensor/tensor.py Outdated

@samsja samsja left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice trick ! I added minor comments

makram93 added 2 commits May 26, 2023 14:34
Signed-off-by: Mohammad Kalim Akram <kalim.akram@jina.ai>

@samsja samsja left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks very good !

@samsja samsja merged commit 4eec559 into docarray:main May 30, 2023
@samsja samsja mentioned this pull request Jun 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Type coercion from torch to numpy does not work

3 participants