(self)
| 684 | torch.ops.load_library(str(lib_file_path)) |
| 685 | |
| 686 | def test_custom_class(self): |
| 687 | custom_obj = torch.classes._TorchScriptTesting._PickleTester([3, 4]) |
| 688 | |
| 689 | def f(x): |
| 690 | return x + x |
| 691 | |
| 692 | inputs = (torch.zeros(4, 4),) |
| 693 | ep = export(f, inputs) |
| 694 | |
| 695 | # Replace one of the values with an instance of our custom class |
| 696 | for node in ep.graph.nodes: |
| 697 | if node.op == "call_function" and node.target == torch.ops.aten.add.Tensor: |
| 698 | with ep.graph.inserting_before(node): |
| 699 | custom_node = ep.graph.call_function( |
| 700 | torch.ops._TorchScriptTesting.take_an_instance.default, |
| 701 | (custom_obj,), |
| 702 | ) |
| 703 | custom_node.meta["val"] = torch.ones(4, 4) |
| 704 | arg0, _ = node.args |
| 705 | node.args = (arg0, custom_node) |
| 706 | |
| 707 | serialized_vals = serialize(ep) |
| 708 | deserialized_ep = deserialize(serialized_vals) |
| 709 | |
| 710 | for node in deserialized_ep.graph.nodes: |
| 711 | if ( |
| 712 | node.op == "call_function" and |
| 713 | node.target == torch.ops._TorchScriptTesting.take_an_instance.default |
| 714 | ): |
| 715 | arg = node.args[0] |
| 716 | self.assertTrue(isinstance(arg, torch._C.ScriptObject)) |
| 717 | self.assertEqual(arg.__getstate__(), custom_obj.__getstate__()) |
| 718 | self.assertEqual(arg.top(), 7) |
| 719 | |
| 720 | |
| 721 | if __name__ == '__main__': |
nothing calls this directly
no test coverage detected