-
-
Notifications
You must be signed in to change notification settings - Fork 35
Tensor duck #40
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
Open
corwinjoy
wants to merge
3
commits into
patrick-kidger:master
Choose a base branch
from
corwinjoy:tensor_duck
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Tensor duck #40
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Test ability to type-check user defined classes which have a "torch-like" interface | ||
# The required interface is defined as the protocol TensorLike in tensor_details.py | ||
|
||
from __future__ import annotations | ||
import pytest | ||
import torch | ||
from torch import rand | ||
|
||
from torchtyping import TensorType, TensorTypeMixin | ||
from typeguard import typechecked | ||
|
||
|
||
# New class that supports the tensor-like interface | ||
class MyTensor: | ||
def __init__(self, tensor: torch.Tensor = torch.zeros(2, 3)): | ||
self.tensor = tensor | ||
self.dtype = self.tensor.dtype | ||
self.layout = "something special" | ||
self.names = self.tensor.names | ||
self.shape = self.tensor.shape | ||
|
||
def is_floating_point(self) -> bool: | ||
return self.dtype == torch.float32 | ||
|
||
# Add tensors and take the mean over the last dimension | ||
# Output drops the last dimension | ||
def __add__(self, o: torch.Tensor) -> MyTensor: | ||
res = self.tensor + o | ||
res_reduced = torch.mean(res, -1) | ||
res_myt = MyTensor(res_reduced) | ||
return res_myt | ||
|
||
|
||
# Create a type corresponding to the new class | ||
class MyTensorType(MyTensor, TensorTypeMixin): | ||
base_cls = MyTensor | ||
|
||
|
||
# make flake8 happy | ||
x = y = None | ||
|
||
|
||
def test_my_tensor1(): | ||
@typechecked | ||
def func(x: MyTensorType["x", "y"], y: TensorType["x", "y"]) -> MyTensorType["x"]: | ||
return x + y | ||
|
||
@typechecked | ||
def bad_func_spec( | ||
x: MyTensorType["x", "y"], y: TensorType["x", "y"] | ||
) -> MyTensorType["x", "y"]: | ||
return x + y | ||
|
||
my_t: MyTensor = MyTensor() | ||
func(my_t, rand((2, 3))) | ||
|
||
# Incorrect input dimensions for x | ||
with pytest.raises(TypeError): | ||
func(MyTensor(rand(1)), rand((2, 3))) | ||
|
||
# Incorrect input dimensions for y | ||
with pytest.raises(TypeError): | ||
func(my_t, rand(1)) | ||
|
||
# Incorrect spec for return dimensions | ||
with pytest.raises(TypeError): | ||
bad_func_spec(my_t, rand((2, 3))) |
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
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
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
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
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure about this last change. As mentioned, the protocol class only supports isintance() because it has properties. This means I had to require default construction.
But, I think this test may be unnecessary - after all the other tests I think we know this is a TensorLike element?
I think it might be better to just get rid of this test. @patrick-kidger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact, it does seem I have a strong motivation to remove this. The case where I want to apply it is to check shape signatures on an abstract base class so default construction may not be an option.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have updated the PR accordingly.