|
3 | 3 | """Unit tests for the DeduplicateInitializersPass."""
|
4 | 4 |
|
5 | 5 | import unittest
|
| 6 | +import onnx |
6 | 7 | import numpy as np
|
7 | 8 |
|
8 |
| -from onnx_ir._core import Tensor, Value, Node, Graph |
9 |
| -from onnx_ir.passes.common.deduplicate_initializers import DeduplicateInitializersPass |
| 9 | +import onnx_ir as ir |
| 10 | +import onnx_ir.passes.common.deduplicate_initializers as dedup_pass |
10 | 11 |
|
11 | 12 |
|
12 |
| -class DeduplicateInitializersPassTest(unittest.TestCase): |
13 |
| - def setUp(self): |
14 |
| - # Shared tensor content |
15 |
| - self.arr = np.array([1, 2, 3]) |
16 |
| - self.tensor1 = Tensor(self.arr) |
17 |
| - self.tensor2 = Tensor(self.arr.copy()) # Identical but separate object |
18 |
| - self.tensor3 = Tensor(self.arr.copy()) # For subgraph |
| 13 | +class DeduplicateInitializersTest(unittest.TestCase): |
| 14 | + def apply_pass(self, model: onnx.ModelProto) -> onnx.ModelProto: |
| 15 | + model_ir = ir.serde.deserialize_model(model) |
| 16 | + dedup_pass.DeduplicateInitializersPass()(model_ir) |
| 17 | + return ir.serde.serialize_model(model_ir) |
19 | 18 |
|
20 |
| - def test_deduplication_in_main_and_subgraph(self): |
21 |
| - v1 = Value(name="w1", const_value=self.tensor1) |
22 |
| - v2 = Value(name="w2", const_value=self.tensor2) |
23 |
| - v3 = Value(name="w3", const_value=self.tensor3) |
24 |
| - |
25 |
| - # Main graph node using w1 and w2 |
26 |
| - main_node = Node("", "Add", inputs=[v1, v2], outputs=[]) |
27 |
| - |
28 |
| - # Subgraph node using w3 |
29 |
| - sub_node = Node("", "Conv", inputs=[v3], outputs=[]) |
30 |
| - subgraph = Graph( |
31 |
| - inputs=[], |
32 |
| - outputs=[], |
33 |
| - nodes=[sub_node], |
34 |
| - initializers=[v3], |
35 |
| - name="subgraph" |
| 19 | + def test_deduplicates_identical_initializers(self): |
| 20 | + model = onnx.parser.parse_model( |
| 21 | + """ |
| 22 | + <ir_version: 10, opset_import: ["" : 17]> |
| 23 | + agraph () => () |
| 24 | + <float[3] w1 = {1.0, 2.0, 3.0}, float[3] w2 = {1.0, 2.0, 3.0}> { |
| 25 | + sum = Add(w1, w2) |
| 26 | + } |
| 27 | + """ |
36 | 28 | )
|
| 29 | + self.assertEqual(len(model.graph.initializer), 2) |
| 30 | + new_model = self.apply_pass(model) |
| 31 | + self.assertEqual(len(new_model.graph.initializer), 1) |
| 32 | + add_node = new_model.graph.node[0] |
| 33 | + self.assertEqual(add_node.input[0], add_node.input[1]) |
37 | 34 |
|
38 |
| - # Link subgraph to main node |
39 |
| - main_node.blocks = [subgraph] |
40 |
| - |
41 |
| - # Main graph with w1 and w2 (duplicates) |
42 |
| - main_graph = Graph( |
43 |
| - inputs=[], |
44 |
| - outputs=[], |
45 |
| - nodes=[main_node], |
46 |
| - initializers=[v1, v2], |
47 |
| - name="main_graph" |
| 35 | + |
| 36 | + def test_initializers_with_different_shapes_not_deduplicated(self): |
| 37 | + model = onnx.parser.parse_model( |
| 38 | + """ |
| 39 | + <ir_version: 10, opset_import: ["" : 17]> |
| 40 | + agraph () => () |
| 41 | + <float[2] w1 = {1.0, 2.0}, float[3] w2 = {1.0, 2.0, 3.0}> { |
| 42 | + sum = Add(w1, w2) |
| 43 | + } |
| 44 | + """ |
48 | 45 | )
|
| 46 | + new_model = self.apply_pass(model) |
| 47 | + self.assertEqual(len(new_model.graph.initializer), 2) |
49 | 48 |
|
50 |
| - DeduplicateInitializersPass().apply(main_graph) |
| 49 | + def test_initializers_with_different_dtypes_not_deduplicated(self): |
| 50 | + model = onnx.parser.parse_model( |
| 51 | + """ |
| 52 | + <ir_version: 10, opset_import: ["" : 17]> |
| 53 | + agraph () => () |
| 54 | + <float[2] w1 = {1.0, 2.0}, double[2] w2 = {1.0, 2.0}> { |
| 55 | + sum = Add(w1, w2) |
| 56 | + } |
| 57 | + """ |
| 58 | + ) |
| 59 | + new_model = self.apply_pass(model) |
| 60 | + self.assertEqual(len(new_model.graph.initializer), 2) |
51 | 61 |
|
52 |
| - # Post conditions |
53 |
| - self.assertIn("w1", main_graph.initializers) |
54 |
| - self.assertNotIn("w2", main_graph.initializers) |
55 |
| - self.assertEqual(main_node.inputs[0].name, "w1") |
56 |
| - self.assertEqual(main_node.inputs[1].name, "w1") |
| 62 | + def test_scalar_initializer_deduplication(self): |
| 63 | + model = onnx.parser.parse_model( |
| 64 | + """ |
| 65 | + <ir_version: 10, opset_import: ["" : 17]> |
| 66 | + agraph () => () |
| 67 | + <float w1 = {5.0}, float w2 = {5.0}> { |
| 68 | + sum = Add(w1, w2) |
| 69 | + } |
| 70 | + """ |
| 71 | + ) |
| 72 | + new_model = self.apply_pass(model) |
| 73 | + self.assertEqual(len(new_model.graph.initializer), 1) |
57 | 74 |
|
58 |
| - # Subgraph should be untouched (no cross-graph deduplication) |
59 |
| - self.assertIn("w3", subgraph.initializers) |
60 |
| - self.assertEqual(sub_node.inputs[0].name, "w3") |
| 75 | + def test_multiple_duplicates(self): |
| 76 | + model = onnx.parser.parse_model( |
| 77 | + """ |
| 78 | + <ir_version: 10, opset_import: ["" : 17]> |
| 79 | + agraph () => () |
| 80 | + <float[2] w1 = {1.0, 1.0}, float[2] w2 = {1.0, 1.0}, float[2] w3 = {1.0, 1.0}> { |
| 81 | + temp = Add(w1, w2) |
| 82 | + out = Add(temp, w3) |
| 83 | + } |
| 84 | + """ |
| 85 | + ) |
| 86 | + new_model = self.apply_pass(model) |
| 87 | + self.assertEqual(len(new_model.graph.initializer), 1) |
61 | 88 |
|
| 89 | + def test_unique_values_not_deduplicated(self): |
| 90 | + model = onnx.parser.parse_model( |
| 91 | + """ |
| 92 | + <ir_version: 10, opset_import: ["" : 17]> |
| 93 | + agraph () => () |
| 94 | + <float[2] w1 = {1.0, 2.0}, float[2] w2 = {2.0, 1.0}> { |
| 95 | + sum = Add(w1, w2) |
| 96 | + } |
| 97 | + """ |
| 98 | + ) |
| 99 | + new_model = self.apply_pass(model) |
| 100 | + self.assertEqual(len(new_model.graph.initializer), 2) |
62 | 101 |
|
| 102 | + |
63 | 103 | if __name__ == "__main__":
|
64 | 104 | unittest.main()
|
0 commit comments