-
-
Notifications
You must be signed in to change notification settings - Fork 985
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* make ProvenanceTensor behave more like a Tensor (closes #3218) the data is now stored in the Tensor itself instead of an attribute. This fixes torch.to_tensor returning empty tensors when called with a ProvenanceTensor and and a device as arguments * fix compatibility with PyTorch 1.11 * make detach_provenance always return the exact same object this is important when using Tensors as keys in a dict, e.g. the Pyro param store * preserve .unconstrained attribute in detach_provenance * simplify * add unit test * simplify further * use Tensor.as_subclass instead of modifying __class__ also remove unnecessary check in __init__
- Loading branch information
Showing
2 changed files
with
51 additions
and
5 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright Contributors to the Pyro project. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
|
||
import pytest | ||
import torch | ||
|
||
from pyro.ops.provenance import ProvenanceTensor | ||
from tests.common import assert_equal, requires_cuda | ||
|
||
|
||
@requires_cuda | ||
@pytest.mark.parametrize( | ||
"dtype1", | ||
[ | ||
torch.float16, | ||
torch.float32, | ||
torch.float64, | ||
torch.int8, | ||
torch.int16, | ||
torch.int32, | ||
torch.int64, | ||
torch.uint8, | ||
], | ||
) | ||
@pytest.mark.parametrize( | ||
"dtype2", | ||
[ | ||
torch.float16, | ||
torch.float32, | ||
torch.float64, | ||
torch.int8, | ||
torch.int16, | ||
torch.int32, | ||
torch.int64, | ||
torch.uint8, | ||
], | ||
) | ||
def test_provenance_tensor(dtype1, dtype2): | ||
device = torch.device("cuda") | ||
x = torch.tensor([1, 2, 3], dtype=dtype1) | ||
y = ProvenanceTensor(x, frozenset(["x"])) | ||
z = torch.as_tensor(y, device=device, dtype=dtype2) | ||
|
||
assert x.shape == y.shape == z.shape | ||
assert_equal(x, z.cpu()) |