|
28 | 28 | shadowing_names, |
29 | 29 | onnx_dtype_name, |
30 | 30 | extract_subset_of_nodes, |
| 31 | + make_subfunction, |
31 | 32 | make_submodel, |
| 33 | + make_model_with_local_functions, |
32 | 34 | select_model_inputs_outputs, |
33 | 35 | _enumerate_model_node_outputs, |
| 36 | + pretty_onnx, |
34 | 37 | ) |
35 | 38 |
|
36 | 39 | TFLOAT = TensorProto.FLOAT |
@@ -537,6 +540,46 @@ def _type_rank_fn(name): |
537 | 540 | check_model(new_model) |
538 | 541 | self.check_ort(new_model) |
539 | 542 |
|
| 543 | + def test_make_subfunction(self): |
| 544 | + model = oh.make_model( |
| 545 | + oh.make_graph( |
| 546 | + [ |
| 547 | + oh.make_node("Unsqueeze", ["X", "zero"], ["xu1"]), |
| 548 | + oh.make_node("Unsqueeze", ["xu1", "un"], ["xu2"]), |
| 549 | + oh.make_node("Reshape", ["xu2", "shape1"], ["xm1"]), |
| 550 | + oh.make_node("Reshape", ["Y", "shape2"], ["xm2c"]), |
| 551 | + oh.make_node("Cast", ["xm2c"], ["xm2"], to=1), |
| 552 | + oh.make_node("MatMul", ["xm1", "xm2"], ["xm"]), |
| 553 | + oh.make_node("Reshape", ["xm", "shape3"], ["Z"]), |
| 554 | + ], |
| 555 | + "dummy", |
| 556 | + [oh.make_tensor_value_info("X", TFLOAT, [320, 1280])], |
| 557 | + [oh.make_tensor_value_info("Z", TFLOAT, [3, 5, 320, 640])], |
| 558 | + [ |
| 559 | + onh.from_array( |
| 560 | + np.random.rand(3, 5, 1280, 640).astype(np.float32), name="Y" |
| 561 | + ), |
| 562 | + onh.from_array(np.array([0], dtype=np.int64), name="zero"), |
| 563 | + onh.from_array(np.array([1], dtype=np.int64), name="un"), |
| 564 | + onh.from_array(np.array([1, 320, 1280], dtype=np.int64), name="shape1"), |
| 565 | + onh.from_array(np.array([15, 1280, 640], dtype=np.int64), name="shape2"), |
| 566 | + onh.from_array(np.array([3, 5, 320, 640], dtype=np.int64), name="shape3"), |
| 567 | + ], |
| 568 | + ), |
| 569 | + opset_imports=[oh.make_opsetid("", 18)], |
| 570 | + ir_version=9, |
| 571 | + ) |
| 572 | + new_function = make_subfunction( |
| 573 | + "localf", |
| 574 | + model.graph.node[:4], |
| 575 | + opset_imports=model.opset_import, |
| 576 | + output_names=["xm1", "xm2c"], |
| 577 | + ) |
| 578 | + self.assertIsInstance(new_function, FunctionProto) |
| 579 | + self.assertEqual(len(new_function.node), 4) |
| 580 | + self.assertEqual(new_function.output, ["xm1", "xm2c"]) |
| 581 | + self.assertEqual(new_function.input, ["X", "Y", "shape1", "shape2", "un", "zero"]) |
| 582 | + |
540 | 583 | def test_extract_subset_of_nodes_bigger(self): |
541 | 584 | model = onnx.load( |
542 | 585 | os.path.join( |
@@ -670,6 +713,153 @@ def enumerate_model_tensors(model): |
670 | 713 | got = sess.run(None, {"X": x})[0] |
671 | 714 | self.assertEqual((x**2 + y).tolist(), got.tolist()) |
672 | 715 |
|
| 716 | + def test_make_model_with_local_functions(self): |
| 717 | + model = oh.make_model( |
| 718 | + oh.make_graph( |
| 719 | + [ |
| 720 | + oh.make_node("Unsqueeze", ["X", "zero"], ["xu1"]), |
| 721 | + oh.make_node("Unsqueeze", ["xu1", "un"], ["xu2"]), |
| 722 | + oh.make_node("Reshape", ["xu2", "shape1"], ["xm1"]), |
| 723 | + oh.make_node("Reshape", ["Y", "shape2"], ["xm2c"]), |
| 724 | + oh.make_node("Cast", ["xm2c"], ["xm2"], to=1), |
| 725 | + oh.make_node("MatMul", ["xm1", "xm2"], ["xm"]), |
| 726 | + oh.make_node("Reshape", ["xm", "shape3"], ["Z"]), |
| 727 | + ], |
| 728 | + "dummy", |
| 729 | + [oh.make_tensor_value_info("X", TFLOAT, [320, 1280])], |
| 730 | + [oh.make_tensor_value_info("Z", TFLOAT, [3, 5, 320, 640])], |
| 731 | + [ |
| 732 | + onh.from_array( |
| 733 | + np.random.rand(3, 5, 1280, 640).astype(np.float32), name="Y" |
| 734 | + ), |
| 735 | + onh.from_array(np.array([0], dtype=np.int64), name="zero"), |
| 736 | + onh.from_array(np.array([1], dtype=np.int64), name="un"), |
| 737 | + onh.from_array(np.array([1, 320, 1280], dtype=np.int64), name="shape1"), |
| 738 | + onh.from_array(np.array([15, 1280, 640], dtype=np.int64), name="shape2"), |
| 739 | + onh.from_array(np.array([3, 5, 320, 640], dtype=np.int64), name="shape3"), |
| 740 | + ], |
| 741 | + ), |
| 742 | + opset_imports=[oh.make_opsetid("", 18)], |
| 743 | + ir_version=9, |
| 744 | + ) |
| 745 | + for i_node in [0, 1, 2, 3]: |
| 746 | + node = model.graph.node[i_node] |
| 747 | + meta = node.metadata_props.add() |
| 748 | + meta.key = "namespace" |
| 749 | + meta.value = "LLL" |
| 750 | + new_model = make_model_with_local_functions(model, "^LLL$") |
| 751 | + check_model(model) |
| 752 | + self.assertEqual(len(new_model.functions), 1) |
| 753 | + self.assertEqual( |
| 754 | + ["X", "Y", "shape1", "shape2", "un", "zero"], new_model.functions[0].input |
| 755 | + ) |
| 756 | + self.assertEqual(["xm1", "xm2c"], new_model.functions[0].output) |
| 757 | + self.assertEqual("LLL", new_model.functions[0].name) |
| 758 | + self.assertEqual("local_function", new_model.functions[0].domain) |
| 759 | + self.assertIn("LLL[local_function]", pretty_onnx(new_model)) |
| 760 | + check_model(new_model) |
| 761 | + |
| 762 | + def test_make_model_with_local_functions_bug(self): |
| 763 | + model = oh.make_model( |
| 764 | + oh.make_graph( |
| 765 | + [ |
| 766 | + oh.make_node("Unsqueeze", ["X", "zero"], ["xu1"]), |
| 767 | + oh.make_node("Unsqueeze", ["xu1", "un"], ["xu2"]), |
| 768 | + oh.make_node("Reshape", ["xu2", "shape1"], ["xm1"]), |
| 769 | + oh.make_node("Reshape", ["Y", "shape2"], ["xm2c"]), |
| 770 | + oh.make_node("Cast", ["xm2c"], ["xm2"], to=1), |
| 771 | + oh.make_node("MatMul", ["xm1", "xm2"], ["xm"]), |
| 772 | + oh.make_node("Reshape", ["xm", "shape3"], ["Z"]), |
| 773 | + ], |
| 774 | + "dummy", |
| 775 | + [oh.make_tensor_value_info("X", TFLOAT, [320, 1280])], |
| 776 | + [oh.make_tensor_value_info("Z", TFLOAT, [3, 5, 320, 640])], |
| 777 | + [ |
| 778 | + onh.from_array( |
| 779 | + np.random.rand(3, 5, 1280, 640).astype(np.float32), name="Y" |
| 780 | + ), |
| 781 | + onh.from_array(np.array([0], dtype=np.int64), name="zero"), |
| 782 | + onh.from_array(np.array([1], dtype=np.int64), name="un"), |
| 783 | + onh.from_array(np.array([1, 320, 1280], dtype=np.int64), name="shape1"), |
| 784 | + onh.from_array(np.array([15, 1280, 640], dtype=np.int64), name="shape2"), |
| 785 | + onh.from_array(np.array([3, 5, 320, 640], dtype=np.int64), name="shape3"), |
| 786 | + ], |
| 787 | + ), |
| 788 | + opset_imports=[oh.make_opsetid("", 18)], |
| 789 | + ir_version=9, |
| 790 | + ) |
| 791 | + for i_node in [0, 2, 3, 4]: |
| 792 | + node = model.graph.node[i_node] |
| 793 | + meta = node.metadata_props.add() |
| 794 | + meta.key = "namespace" |
| 795 | + meta.value = "LLL" |
| 796 | + self.assertRaise( |
| 797 | + lambda: make_model_with_local_functions(model, "^LLL$"), |
| 798 | + ValueError, |
| 799 | + "Results {'xu1'} are needed for inputs ['X', 'Y', 'shape1', " |
| 800 | + "'shape2', 'xu2', 'zero'] but also requires ['xm1', 'xm2', 'xu1'] " |
| 801 | + "which is not allowed.", |
| 802 | + ) |
| 803 | + check_model(model) |
| 804 | + |
| 805 | + @hide_stdout() |
| 806 | + def test_make_model_with_local_functions_2(self): |
| 807 | + model = oh.make_model( |
| 808 | + oh.make_graph( |
| 809 | + [ |
| 810 | + oh.make_node("Unsqueeze", ["X", "zero"], ["xu1"]), |
| 811 | + oh.make_node("Unsqueeze", ["xu1", "un"], ["xu2"]), |
| 812 | + oh.make_node("Reshape", ["xu2", "shape1"], ["xm1"]), |
| 813 | + oh.make_node("Reshape", ["Y", "shape2"], ["xm2c"]), |
| 814 | + oh.make_node("Cast", ["xm2c"], ["xm2"], to=1), |
| 815 | + oh.make_node("MatMul", ["xm1", "xm2"], ["xm"]), |
| 816 | + oh.make_node("Reshape", ["xm", "shape3"], ["Z"]), |
| 817 | + ], |
| 818 | + "dummy", |
| 819 | + [oh.make_tensor_value_info("X", TFLOAT, [320, 1280])], |
| 820 | + [oh.make_tensor_value_info("Z", TFLOAT, [3, 5, 320, 640])], |
| 821 | + [ |
| 822 | + onh.from_array( |
| 823 | + np.random.rand(3, 5, 1280, 640).astype(np.float32), name="Y" |
| 824 | + ), |
| 825 | + onh.from_array(np.array([0], dtype=np.int64), name="zero"), |
| 826 | + onh.from_array(np.array([1], dtype=np.int64), name="un"), |
| 827 | + onh.from_array(np.array([1, 320, 1280], dtype=np.int64), name="shape1"), |
| 828 | + onh.from_array(np.array([15, 1280, 640], dtype=np.int64), name="shape2"), |
| 829 | + onh.from_array(np.array([3, 5, 320, 640], dtype=np.int64), name="shape3"), |
| 830 | + ], |
| 831 | + ), |
| 832 | + opset_imports=[oh.make_opsetid("", 18)], |
| 833 | + ir_version=9, |
| 834 | + ) |
| 835 | + for i_node in [0, 1, 2, 3]: |
| 836 | + node = model.graph.node[i_node] |
| 837 | + meta = node.metadata_props.add() |
| 838 | + meta.key = f"source[{i_node}]" |
| 839 | + meta.value = f"LLL{i_node//3}" |
| 840 | + new_model = make_model_with_local_functions( |
| 841 | + model, "^LLL[01]$", metadata_key_prefix="source[", verbose=1 |
| 842 | + ) |
| 843 | + check_model(model) |
| 844 | + self.assertEqual(len(new_model.functions), 2) |
| 845 | + p = pretty_onnx(new_model) |
| 846 | + self.assertIn("LLL0[local_function]", p) |
| 847 | + self.assertIn("LLL1[local_function]", p) |
| 848 | + |
| 849 | + self.assertEqual(["X", "shape1", "un", "zero"], new_model.functions[0].input) |
| 850 | + self.assertEqual(["xm1"], new_model.functions[0].output) |
| 851 | + self.assertEqual("LLL0", new_model.functions[0].name) |
| 852 | + self.assertEqual("local_function", new_model.functions[0].domain) |
| 853 | + self.assertEqual(len(new_model.functions[0].node), 3) |
| 854 | + |
| 855 | + self.assertEqual(["Y", "shape2"], new_model.functions[1].input) |
| 856 | + self.assertEqual(["xm2c"], new_model.functions[1].output) |
| 857 | + self.assertEqual("LLL1", new_model.functions[1].name) |
| 858 | + self.assertEqual("local_function", new_model.functions[1].domain) |
| 859 | + self.assertEqual(len(new_model.functions[1].node), 1) |
| 860 | + |
| 861 | + check_model(new_model) |
| 862 | + |
673 | 863 |
|
674 | 864 | if __name__ == "__main__": |
675 | 865 | unittest.main(verbosity=2) |
0 commit comments