MCPcopy Create free account
hub / github.com/pytorch/pytorch / test_custom_class

Method test_custom_class

test/export/test_serialize.py:686–718  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

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
721if __name__ == '__main__':

Callers

nothing calls this directly

Calls 11

exportFunction · 0.90
serializeFunction · 0.90
deserializeFunction · 0.90
isinstanceFunction · 0.85
topMethod · 0.80
zerosMethod · 0.45
inserting_beforeMethod · 0.45
call_functionMethod · 0.45
onesMethod · 0.45
assertEqualMethod · 0.45
__getstate__Method · 0.45

Tested by

no test coverage detected