(self, tensor_cls, as_param)
| 75 | @parametrize_tensor_cls |
| 76 | @parametrize("as_param", [False, True]) |
| 77 | def test_serialization(self, tensor_cls, as_param): |
| 78 | with tempfile.TemporaryFile() as f: |
| 79 | x = self._create_tensor(tensor_cls) |
| 80 | if as_param: |
| 81 | x = nn.Parameter(x) |
| 82 | torch.save(x, f) |
| 83 | f.seek(0) |
| 84 | x_loaded = torch.load(f) |
| 85 | |
| 86 | self.assertEqual(x, x_loaded) |
| 87 | self.assertIsNot(x, x_loaded) |
| 88 | self.assertIsInstance(x_loaded, tensor_cls) |
| 89 | if as_param: |
| 90 | # Serialization should preserve both custom type and "parameter-ness". |
| 91 | self.assertIsInstance(x_loaded, nn.Parameter) |
| 92 | |
| 93 | @skipIfTorchDynamo("Visible only with functorch as functorch monkeypatches tensor str") |
| 94 | @parametrize_tensor_cls |
nothing calls this directly
no test coverage detected