-
Notifications
You must be signed in to change notification settings - Fork 7
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
Enable Offline-ER for NestedTensors #336
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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 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 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 |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
from torch.utils.data import TensorDataset | ||
|
||
from renate.utils import pytorch | ||
from renate.utils.pytorch import randomly_split_data | ||
from renate.utils.pytorch import cat_nested_tensors, get_length_nested_tensors, randomly_split_data | ||
|
||
|
||
@pytest.mark.parametrize("model", [torchvision.models.resnet18(pretrained=True)]) | ||
|
@@ -61,3 +61,44 @@ def test_random_splitting_sample_split_with_same_random_seed(): | |
for i in range(5): | ||
assert torch.equal(d_1_split_1[i][0], d_2_split_1[i][0]) | ||
assert torch.equal(d_1_split_2[i][0], d_2_split_2[i][0]) | ||
|
||
|
||
def test_get_length_nested_tensors(): | ||
expected_batch_size = 10 | ||
t = torch.zeros(expected_batch_size) | ||
assert get_length_nested_tensors(t) == expected_batch_size | ||
tuple_tensor = (t, t) | ||
assert get_length_nested_tensors(tuple_tensor) == expected_batch_size | ||
dict_tensor = {"k1": t, "k2": t} | ||
assert get_length_nested_tensors(dict_tensor) == expected_batch_size | ||
|
||
|
||
def test_cat_nested_tensors(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be good to test also the behavior in case of failure (e.g., shape mismatch) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I've added a test |
||
tensor_dim = 2 | ||
first_dim_ones = 8 | ||
zeros = torch.zeros((2, tensor_dim)) | ||
ones = torch.ones((first_dim_ones, tensor_dim)) | ||
result = cat_nested_tensors((zeros, ones)) | ||
assert get_length_nested_tensors(result) == 10 | ||
assert result.mean() == 0.8 | ||
tuple_tensor = (zeros, ones) | ||
result = cat_nested_tensors((tuple_tensor, tuple_tensor)) | ||
assert get_length_nested_tensors(result) == 4 | ||
assert result[0].sum() == 0 | ||
assert result[1].sum() == 2 * first_dim_ones * tensor_dim | ||
dict_tensor = {"zeros": zeros, "ones": ones} | ||
result = cat_nested_tensors((dict_tensor, dict_tensor)) | ||
assert get_length_nested_tensors(result) == 4 | ||
assert result["zeros"].sum() == 0 | ||
assert result["ones"].sum() == 2 * first_dim_ones * tensor_dim | ||
|
||
|
||
def test_cat_nested_tensors_wrong_shape(): | ||
tensor1 = torch.zeros((2, 2)) | ||
tensor2 = torch.zeros((2, 3)) | ||
with pytest.raises(RuntimeError, match=r"Sizes of tensors must match except in dimension 0.*"): | ||
cat_nested_tensors((tensor1, tensor2)) | ||
with pytest.raises(RuntimeError, match=r"Sizes of tensors must match except in dimension 0.*"): | ||
cat_nested_tensors(((tensor1, tensor1), (tensor1, tensor2))) | ||
with pytest.raises(RuntimeError, match=r"Sizes of tensors must match except in dimension 0.*"): | ||
cat_nested_tensors(({"k1": tensor1, "k2": tensor1}, {"k1": tensor1, "k2": tensor2})) |
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.
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.
are we assuming all the tensors in the tuple have the same shape? or that only the first one actually contains data? I think it's important to make this clear.
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.
You are right, technically they could be of different shapes. I've renamed the function to reflect that it now returns the first dim only and the docstring to say that we expect the first dim to match.