|
| 1 | +import torch |
| 2 | +from torchvision.prototype import features |
| 3 | + |
| 4 | + |
| 5 | +def test_isinstance(): |
| 6 | + assert isinstance( |
| 7 | + features.Label([0, 1, 0], categories=["foo", "bar"]), |
| 8 | + torch.Tensor, |
| 9 | + ) |
| 10 | + |
| 11 | + |
| 12 | +def test_wrapping_no_copy(): |
| 13 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 14 | + label = features.Label(tensor, categories=["foo", "bar"]) |
| 15 | + |
| 16 | + assert label.data_ptr() == tensor.data_ptr() |
| 17 | + |
| 18 | + |
| 19 | +def test_to_wrapping(): |
| 20 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 21 | + label = features.Label(tensor, categories=["foo", "bar"]) |
| 22 | + |
| 23 | + label_to = label.to(torch.int32) |
| 24 | + |
| 25 | + assert type(label_to) is features.Label |
| 26 | + assert label_to.dtype is torch.int32 |
| 27 | + assert label_to.categories is label.categories |
| 28 | + |
| 29 | + |
| 30 | +def test_to_feature_reference(): |
| 31 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 32 | + label = features.Label(tensor, categories=["foo", "bar"]).to(torch.int32) |
| 33 | + |
| 34 | + tensor_to = tensor.to(label) |
| 35 | + |
| 36 | + assert type(tensor_to) is torch.Tensor |
| 37 | + assert tensor_to.dtype is torch.int32 |
| 38 | + |
| 39 | + |
| 40 | +def test_clone_wrapping(): |
| 41 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 42 | + label = features.Label(tensor, categories=["foo", "bar"]) |
| 43 | + |
| 44 | + label_clone = label.clone() |
| 45 | + |
| 46 | + assert type(label_clone) is features.Label |
| 47 | + assert label_clone.data_ptr() != label.data_ptr() |
| 48 | + assert label_clone.categories is label.categories |
| 49 | + |
| 50 | + |
| 51 | +def test_other_op_no_wrapping(): |
| 52 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 53 | + label = features.Label(tensor, categories=["foo", "bar"]) |
| 54 | + |
| 55 | + # any operation besides .to() and .clone() will do here |
| 56 | + output = label * 2 |
| 57 | + |
| 58 | + assert type(output) is torch.Tensor |
| 59 | + |
| 60 | + |
| 61 | +def test_new_like(): |
| 62 | + tensor = torch.tensor([0, 1, 0], dtype=torch.int64) |
| 63 | + label = features.Label(tensor, categories=["foo", "bar"]) |
| 64 | + |
| 65 | + # any operation besides .to() and .clone() will do here |
| 66 | + output = label * 2 |
| 67 | + |
| 68 | + label_new = features.Label.new_like(label, output) |
| 69 | + |
| 70 | + assert type(label_new) is features.Label |
| 71 | + assert label_new.data_ptr() == output.data_ptr() |
| 72 | + assert label_new.categories is label.categories |
0 commit comments