feat: parametrized tensors#850
Merged
Merged
Conversation
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
samsja
reviewed
Nov 25, 2022
samsja
reviewed
Nov 25, 2022
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
Signed-off-by: Johannes Messner <messnerjo@gmail.com>
samsja
approved these changes
Nov 25, 2022
| ShapeT = TypeVar('ShapeT') | ||
|
|
||
|
|
||
| class AbstractTensor(AbstractType, Generic[ShapeT], ABC): |
Contributor
There was a problem hiding this comment.
Since AbstractType is inheried, we don't need ABC ?
Member
Author
There was a problem hiding this comment.
Actually ABC is not inherited like other classes, due to it using a custom metaclass. I believe we need to inherit from ABC again here.
This is a quite advanced topic in Python that is usually not important day to day, but if you are interested you can read more here: https://peps.python.org/pep-3119/
| tensor = parse_obj_as(TorchTensor[128], torch.zeros(128)) | ||
| assert isinstance(tensor, TorchTensor) | ||
| assert isinstance(tensor, torch.Tensor) | ||
| assert tensor.shape == (128,) |
Member
Author
There was a problem hiding this comment.
Without the comma it would just be an int, but we want it to be a tuple
samsja
approved these changes
Nov 28, 2022
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Goals:
Have parametrized types for our tensor types, where the tensor shape can be specified directly in the type:
How it works:
The
__class_getitem__()that is reponsible for the[...]syntax is defined in a general manner inAbstractTensor.This means that concrete tensor types like
TorchTensorandNdArrayonly have to implement__validate_shape__(), the rest works automatically.Todo:
Tensortype -> this does not work, not supported