onnxscript 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (176) hide show
  1. onnxscript/__init__.py +131 -0
  2. onnxscript/_framework_apis/__init__.py +3 -0
  3. onnxscript/_framework_apis/torch_2_5.py +117 -0
  4. onnxscript/_framework_apis/torch_2_6.py +45 -0
  5. onnxscript/_internal/__init__.py +0 -0
  6. onnxscript/_internal/analysis.py +229 -0
  7. onnxscript/_internal/ast_utils.py +64 -0
  8. onnxscript/_internal/autocast.py +250 -0
  9. onnxscript/_internal/deprecation.py +78 -0
  10. onnxscript/_internal/param_manipulation.py +148 -0
  11. onnxscript/_internal/runtime_typing.py +43 -0
  12. onnxscript/_internal/utils.py +99 -0
  13. onnxscript/_internal/version_utils.py +118 -0
  14. onnxscript/_legacy_ir/__init__.py +341 -0
  15. onnxscript/_legacy_ir/visitor.py +937 -0
  16. onnxscript/_thirdparty/asciichartpy.py +313 -0
  17. onnxscript/backend/__init__.py +2 -0
  18. onnxscript/backend/onnx_backend.py +303 -0
  19. onnxscript/backend/onnx_export.py +885 -0
  20. onnxscript/converter.py +1470 -0
  21. onnxscript/evaluator.py +619 -0
  22. onnxscript/function_libs/tools/torch_lib/deduce_type_constraints.py +403 -0
  23. onnxscript/function_libs/tools/torch_lib/generate_aten_signatures.py +333 -0
  24. onnxscript/function_libs/tools/torch_lib/generate_prims_signatures.py +331 -0
  25. onnxscript/function_libs/torch_lib/__init__.py +12 -0
  26. onnxscript/function_libs/torch_lib/_constants.py +5 -0
  27. onnxscript/function_libs/torch_lib/_flags.py +58 -0
  28. onnxscript/function_libs/torch_lib/graph_building/__init__.py +56 -0
  29. onnxscript/function_libs/torch_lib/graph_building/_graph_building_ir.py +723 -0
  30. onnxscript/function_libs/torch_lib/graph_building/_graph_building_torch.py +1125 -0
  31. onnxscript/function_libs/torch_lib/ops/__init__.py +27 -0
  32. onnxscript/function_libs/torch_lib/ops/common.py +80 -0
  33. onnxscript/function_libs/torch_lib/ops/core.py +8935 -0
  34. onnxscript/function_libs/torch_lib/ops/fft.py +385 -0
  35. onnxscript/function_libs/torch_lib/ops/linalg.py +399 -0
  36. onnxscript/function_libs/torch_lib/ops/nested.py +25 -0
  37. onnxscript/function_libs/torch_lib/ops/nn.py +2713 -0
  38. onnxscript/function_libs/torch_lib/ops/prims.py +850 -0
  39. onnxscript/function_libs/torch_lib/ops/quantized_decomposed.py +63 -0
  40. onnxscript/function_libs/torch_lib/ops/sparse.py +23 -0
  41. onnxscript/function_libs/torch_lib/ops/special.py +387 -0
  42. onnxscript/function_libs/torch_lib/ops/vision.py +25 -0
  43. onnxscript/function_libs/torch_lib/registration.py +151 -0
  44. onnxscript/function_libs/torch_lib/tensor_typing.py +74 -0
  45. onnxscript/ir/__init__.py +153 -0
  46. onnxscript/ir/_convenience.py +447 -0
  47. onnxscript/ir/_core.py +3119 -0
  48. onnxscript/ir/_display.py +49 -0
  49. onnxscript/ir/_enums.py +163 -0
  50. onnxscript/ir/_graph_comparison.py +23 -0
  51. onnxscript/ir/_io.py +97 -0
  52. onnxscript/ir/_linked_list.py +276 -0
  53. onnxscript/ir/_metadata.py +44 -0
  54. onnxscript/ir/_name_authority.py +72 -0
  55. onnxscript/ir/_polyfill.py +25 -0
  56. onnxscript/ir/_protocols.py +598 -0
  57. onnxscript/ir/_schemas.py +548 -0
  58. onnxscript/ir/_tape.py +120 -0
  59. onnxscript/ir/_type_casting.py +106 -0
  60. onnxscript/ir/convenience.py +32 -0
  61. onnxscript/ir/external_data.py +396 -0
  62. onnxscript/ir/passes/__init__.py +33 -0
  63. onnxscript/ir/passes/_pass_infra.py +172 -0
  64. onnxscript/ir/serde.py +1620 -0
  65. onnxscript/ir/tensor_adapters.py +122 -0
  66. onnxscript/ir/traversal.py +82 -0
  67. onnxscript/irbuilder.py +542 -0
  68. onnxscript/main.py +167 -0
  69. onnxscript/onnx_opset/__init__.py +232 -0
  70. onnxscript/onnx_opset/_impl/opset1.py +4100 -0
  71. onnxscript/onnx_opset/_impl/opset10.py +1227 -0
  72. onnxscript/onnx_opset/_impl/opset11.py +4013 -0
  73. onnxscript/onnx_opset/_impl/opset12.py +1078 -0
  74. onnxscript/onnx_opset/_impl/opset13.py +3924 -0
  75. onnxscript/onnx_opset/_impl/opset14.py +999 -0
  76. onnxscript/onnx_opset/_impl/opset15.py +604 -0
  77. onnxscript/onnx_opset/_impl/opset16.py +1255 -0
  78. onnxscript/onnx_opset/_impl/opset17.py +561 -0
  79. onnxscript/onnx_opset/_impl/opset18.py +1803 -0
  80. onnxscript/onnx_opset/_impl/opset19.py +1942 -0
  81. onnxscript/onnx_opset/_impl/opset2.py +218 -0
  82. onnxscript/onnx_opset/_impl/opset20.py +675 -0
  83. onnxscript/onnx_opset/_impl/opset21.py +1976 -0
  84. onnxscript/onnx_opset/_impl/opset22.py +2588 -0
  85. onnxscript/onnx_opset/_impl/opset3.py +199 -0
  86. onnxscript/onnx_opset/_impl/opset4.py +77 -0
  87. onnxscript/onnx_opset/_impl/opset5.py +84 -0
  88. onnxscript/onnx_opset/_impl/opset6.py +944 -0
  89. onnxscript/onnx_opset/_impl/opset7.py +1243 -0
  90. onnxscript/onnx_opset/_impl/opset8.py +444 -0
  91. onnxscript/onnx_opset/_impl/opset9.py +1485 -0
  92. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml1.py +974 -0
  93. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml2.py +112 -0
  94. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml3.py +308 -0
  95. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml4.py +129 -0
  96. onnxscript/onnx_opset/_impl/opset_ai_onnx_ml5.py +158 -0
  97. onnxscript/onnx_opset/_impl/opset_ai_onnx_preview_training1.py +577 -0
  98. onnxscript/onnx_types.py +229 -0
  99. onnxscript/optimizer/__init__.py +39 -0
  100. onnxscript/optimizer/_constant_folding.py +1083 -0
  101. onnxscript/optimizer/_inliner.py +312 -0
  102. onnxscript/optimizer/_legacy/_optimizer.py +98 -0
  103. onnxscript/optimizer/_legacy/_remove_unused_proto.py +144 -0
  104. onnxscript/optimizer/_legacy/_simple_function_folding.py +243 -0
  105. onnxscript/optimizer/_legacy/constant_folding.py +293 -0
  106. onnxscript/optimizer/_legacy/evaluator.py +439 -0
  107. onnxscript/optimizer/_optimizer.py +61 -0
  108. onnxscript/optimizer/_remove_unused.py +106 -0
  109. onnxscript/optimizer/_remove_unused_function.py +72 -0
  110. onnxscript/py.typed +1 -0
  111. onnxscript/rewriter/__init__.py +56 -0
  112. onnxscript/rewriter/_ir_utils.py +111 -0
  113. onnxscript/rewriter/broadcast_to_matmul.py +180 -0
  114. onnxscript/rewriter/cast_constant_of_shape.py +50 -0
  115. onnxscript/rewriter/collapse_slices.py +141 -0
  116. onnxscript/rewriter/erfgelu.py +27 -0
  117. onnxscript/rewriter/function_rule.py +232 -0
  118. onnxscript/rewriter/gemm_to_matmul_add.py +21 -0
  119. onnxscript/rewriter/generic_pattern.py +700 -0
  120. onnxscript/rewriter/llama_rule_sets.py +287 -0
  121. onnxscript/rewriter/no_op.py +56 -0
  122. onnxscript/rewriter/onnxruntime/__init__.py +50 -0
  123. onnxscript/rewriter/onnxruntime/bfloat16_utils/bfloat16_converter.py +99 -0
  124. onnxscript/rewriter/onnxruntime/fused_matmul_rule_sets.py +177 -0
  125. onnxscript/rewriter/onnxruntime/group_normalization_merge_silu.py +64 -0
  126. onnxscript/rewriter/onnxruntime/instance_to_group_normalization.py +155 -0
  127. onnxscript/rewriter/onnxruntime/softmax.py +63 -0
  128. onnxscript/rewriter/onnxruntime/transformers/__init__.py +21 -0
  129. onnxscript/rewriter/onnxruntime/transformers/biassplitgelu.py +31 -0
  130. onnxscript/rewriter/onnxruntime/transformers/fastgelu.py +29 -0
  131. onnxscript/rewriter/onnxruntime/transformers/layernorm.py +47 -0
  132. onnxscript/rewriter/onnxruntime/transformers/multihead_attention.py +715 -0
  133. onnxscript/rewriter/ort_fusions/__init__.py +9 -0
  134. onnxscript/rewriter/ort_fusions/_core.py +28 -0
  135. onnxscript/rewriter/ort_fusions/_smollm_1.py +253 -0
  136. onnxscript/rewriter/ort_fusions/_smollm_2.py +467 -0
  137. onnxscript/rewriter/ort_fusions/_test_models.py +122 -0
  138. onnxscript/rewriter/ort_fusions/_test_utils.py +42 -0
  139. onnxscript/rewriter/ort_fusions/cos_sin_cache.py +154 -0
  140. onnxscript/rewriter/ort_fusions/gqa.py +156 -0
  141. onnxscript/rewriter/ort_fusions/mha.py +198 -0
  142. onnxscript/rewriter/ort_fusions/rms_normalization.py +95 -0
  143. onnxscript/rewriter/ort_fusions/rotary_embedding.py +64 -0
  144. onnxscript/rewriter/ort_fusions/sdpa.py +75 -0
  145. onnxscript/rewriter/ort_fusions/skip_normalization.py +46 -0
  146. onnxscript/rewriter/pattern.py +1714 -0
  147. onnxscript/rewriter/testing.py +77 -0
  148. onnxscript/sourceinfo.py +59 -0
  149. onnxscript/tensor.py +227 -0
  150. onnxscript/testing/__init__.py +482 -0
  151. onnxscript/tools/__init__.py +4 -0
  152. onnxscript/tools/benchmark/__init__.py +23 -0
  153. onnxscript/tools/benchmark/benchmark_helpers.py +783 -0
  154. onnxscript/tools/benchmark/benchmark_run.py +140 -0
  155. onnxscript/tools/benchmark/export_model.py +207 -0
  156. onnxscript/tools/benchmark/export_model_batch.py +146 -0
  157. onnxscript/tools/memory_peak.py +244 -0
  158. onnxscript/tools/training_helper.py +50 -0
  159. onnxscript/tools/transformers_models/__init__.py +190 -0
  160. onnxscript/tools/transformers_models/llama.py +168 -0
  161. onnxscript/tools/transformers_models/mistral.py +238 -0
  162. onnxscript/tools/transformers_models/phi.py +248 -0
  163. onnxscript/tools/transformers_models/phi3.py +259 -0
  164. onnxscript/type_annotation.py +281 -0
  165. onnxscript/utils/__init__.py +0 -0
  166. onnxscript/utils/evaluation_utils.py +56 -0
  167. onnxscript/utils/timing_utils.py +33 -0
  168. onnxscript/utils/utils.py +84 -0
  169. onnxscript/values.py +790 -0
  170. onnxscript/version_converter/__init__.py +21 -0
  171. onnxscript/version_converter/_version_converter.py +314 -0
  172. onnxscript-0.1.0.dist-info/LICENSE +21 -0
  173. onnxscript-0.1.0.dist-info/METADATA +370 -0
  174. onnxscript-0.1.0.dist-info/RECORD +176 -0
  175. onnxscript-0.1.0.dist-info/WHEEL +5 -0
  176. onnxscript-0.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,700 @@
1
+ # Copyright (c) Microsoft Corporation.
2
+ # Licensed under the MIT License.
3
+ from __future__ import annotations
4
+
5
+ import collections
6
+ import inspect
7
+ import os
8
+ import textwrap
9
+ import warnings
10
+ from typing import Any, Callable, Iterator, Sequence
11
+
12
+ import onnxscript.rewriter.pattern as orp
13
+ from onnxscript import ir
14
+
15
+
16
+ class PatternMatchResult:
17
+ """Stores information about a match if a match was successful.
18
+
19
+ * pattern: the GraphPattern which found this result
20
+ * model_nodes: the graph nodes that matched the pattern
21
+ * matched_pattern_to_model_value: a mapping from ValuePattern to ir.Value
22
+ * kwargs: additional attributes the user may add through the method
23
+ :meth:`PatternMatchResult.add_kwargs`
24
+ """
25
+
26
+ def __init__(
27
+ self,
28
+ pattern: orp.GraphPattern,
29
+ model_nodes: Sequence[ir.Node],
30
+ ):
31
+ pattern_nodes: list[orp.NodePattern] = list(pattern)
32
+ assert len(model_nodes) == len(pattern_nodes)
33
+ self.pattern = pattern
34
+ self.model_nodes = model_nodes
35
+ self.kwargs: dict[str, Any] = {}
36
+ self.matched_pattern_to_model_value: dict[orp.ValuePattern, ir.Value] = {}
37
+
38
+ for graph_node, pattern_node in zip(model_nodes, pattern_nodes):
39
+ assert graph_node.op_identifier() == pattern_node.op_identifier(), (
40
+ f"Unexpected type mismatch {graph_node.op_identifier()!r} != {pattern_node.op_identifier()!r}"
41
+ )
42
+ assert len(graph_node.inputs) == len(pattern_node.inputs), (
43
+ f"Unexpected number of inputs for type {graph_node.op_identifier()}"
44
+ )
45
+ for a, b in zip(graph_node.inputs, pattern_node.inputs):
46
+ if b is None:
47
+ # optional input or not an interesting input
48
+ continue
49
+ self._bind(b, a)
50
+
51
+ assert len(graph_node.outputs) == len(pattern_node.outputs), (
52
+ f"Unexpected number of outputs for type {graph_node.op_identifier()}"
53
+ )
54
+ for a, b in zip(graph_node.outputs, pattern_node.outputs):
55
+ self._bind(b, a)
56
+
57
+ def _bind(self, value_pattern: orp.ValuePattern, value: ir.Value) -> None:
58
+ map = self.matched_pattern_to_model_value
59
+ if value_pattern in map:
60
+ assert map[value_pattern] == value, (
61
+ f"Ambiguities, pattern output {value_pattern!r} means "
62
+ f"{value!r} or {map[value_pattern]}"
63
+ )
64
+ else:
65
+ map[value_pattern] = value
66
+
67
+ def add_kwargs(self, name: str, value: Any):
68
+ """Adds an attribute, it can be done when the match is being validated,
69
+ this attribute can be used when building the replacement nodes.
70
+ """
71
+ self.kwargs[name] = value
72
+
73
+ def __repr__(self) -> str:
74
+ return (
75
+ f"PatternMatchResult: {len(self.model_nodes)} nodes ..., {self.pattern.inputs}, "
76
+ f"{self.pattern.outputs})"
77
+ )
78
+
79
+
80
+ def _to_match_result(pmr: PatternMatchResult) -> orp.MatchResult:
81
+ """Converts a PatternMatchResult into a MatchResult.
82
+
83
+ TODO: This is a temporary hack until MatchResult and PatternMatchResult are unified.
84
+ """
85
+ result = orp.MatchResult()
86
+ result.nodes.extend(pmr.model_nodes)
87
+ for var, val in pmr.matched_pattern_to_model_value.items():
88
+ if var.name is not None:
89
+ result.bind(var.name, val)
90
+ result.outputs.extend([pmr.matched_pattern_to_model_value[v] for v in pmr.pattern.outputs])
91
+ return result
92
+
93
+
94
+ def _value_to_str(value: ir.Value | orp.ValuePattern) -> str:
95
+ return value.name if value.name is not None else "anonymous:" + str(id(value))
96
+
97
+
98
+ def _opt_value_to_str(value: ir.Value | orp.ValuePattern | None) -> str:
99
+ return _value_to_str(value) if value is not None else "None"
100
+
101
+
102
+ def _node_to_str(node: ir.Node | orp.NodePattern) -> str:
103
+ inputs = ", ".join(_opt_value_to_str(input) for input in node.inputs)
104
+ outputs = ", ".join(_opt_value_to_str(output) for output in node.outputs)
105
+ op_type = node.op_type
106
+ domain = str(node.domain)
107
+ qualified_op = f"{domain}.{op_type}" if domain else op_type
108
+ return f"{outputs} = {qualified_op}({inputs})"
109
+
110
+
111
+ # def _pattern_node_to_str(node: orp.NodePattern) -> str:
112
+ # inputs = ", ".join(_opt_value_to_str(input) for input in node.inputs)
113
+ # outputs = ", ".join(_opt_value_to_str(output) for output in node.outputs)
114
+ # return f"{outputs} = {node.op_type}({inputs})"
115
+
116
+
117
+ class GenericPatternMatcher(orp.PatternMatcher):
118
+ """
119
+ Implements a pattern optimization for quick experimentation.
120
+
121
+ Current limitation:
122
+
123
+ * The current implementation does match on domain name (easy fix).
124
+ * It does not compares attributes either (easy fix as well).
125
+ """
126
+
127
+ def __init__(self, pattern: orp.GraphPattern) -> None:
128
+ super().__init__(pattern)
129
+
130
+ def enumerate_matches(
131
+ self,
132
+ model: ir.Model,
133
+ graph_or_function: ir.Graph | ir.Function,
134
+ node: ir.Node | None = None,
135
+ verbose: int = 0,
136
+ ) -> Iterator:
137
+ """Enumerates all the matches."""
138
+ if node is None:
139
+ matched = []
140
+ for node in graph_or_function:
141
+ res = self.match(model, graph_or_function, node, verbose=verbose)
142
+ if res:
143
+ matched.append(res)
144
+ yield res
145
+ else:
146
+ res = self.match(model, graph_or_function, node, verbose=verbose)
147
+ if res:
148
+ yield res
149
+
150
+ def none(
151
+ self,
152
+ node: ir.Node | None = None,
153
+ lineno: int | None = None,
154
+ msg: str = "",
155
+ ) -> None:
156
+ """Must be called every time a match fails to trace it.
157
+
158
+ It may be useful which reason made a pattern matching fail.
159
+ Instead of returning None, method *match* can return the following
160
+ expression:
161
+
162
+ ::
163
+
164
+ return self.none(node, inspect.currentframe().f_lineno)
165
+
166
+ By setting the verbosity (see next Section), the user may then know
167
+ which lines in the code returned None and which condition failed.
168
+ If logs are fully enabled, it shows information about matched none
169
+ and the line deciding the matched failed.
170
+ For example, this tells the matching failed at line 601 in ``generic_pattern.py``.
171
+ It happens when propagating the match in the backward directions.
172
+ The unmatched types are Mul, MatMul and below,
173
+ it shows the matched nodes. The first one was Cast.
174
+ And the failure happened at iteration 5.
175
+ ``139774002356544-139774000632672`` is the pair of ids used in container ``matched``.
176
+ ``id(node)`` is used as a unique identifiers of the nodes.
177
+
178
+ ::
179
+
180
+ [RotaryEmbeddingPattern.match] NONE - line: 601:__main__, op_type=Cast
181
+ --hint--: BACKWARD: different node types
182
+ --pattern
183
+ Mul(pos_ids, cast) -> (mul)
184
+ -- model
185
+ MatMul(/_original_modu...Expand_output_0, /_original_modu...b/Cast_output_0) -> (/_original_modu...MatMul_output_0)
186
+ iteration=5
187
+ --matched-- #6
188
+ Cast(/_original_modu...mb/Cos_output_0) ~ Cast(cos) [139774002356544-139774000632672]
189
+ Cos(/_original_modu...ncat_1_output_0) ~ Cos(concattraining-transpose-0) [139774002356448-139774000632048]
190
+ ConcatTraining(/_original_modu...nspose_output_0,/_original_modu...nspose_output_0) ~ ConcatTraining(transpose,transpose) [139774002356352-139774000631712]
191
+ Transpose(/_original_modu...MatMul_output_0) ~ Transpose(mul) [139774002356256-139774000631184]
192
+ Sin(/_original_modu...ncat_1_output_0) ~ Sin(concattraining-transpose-0) [139774002358512-139774000631568]
193
+ Cast(/_original_modu...mb/Sin_output_0) ~ Cast(sin) [139774002358608-139774000632384]
194
+ len(stack)=0:[]
195
+
196
+ 'hints' are not added everywhere. More can easily be added with method ``_hint``.
197
+ """
198
+ if node and self.verbose:
199
+ if self.verbose >= 10:
200
+ if hasattr(self, "_debug"):
201
+ msg2 = self._debug_print()
202
+ if msg2:
203
+ msg2 = f"\n{textwrap.indent(msg2, ' ')}"
204
+ else:
205
+ msg2 = ""
206
+ print(
207
+ f"[{self.__class__.__name__}.match] Match failed at line: {lineno}:"
208
+ f"{os.path.split(self.__class__.__module__)[-1]}, "
209
+ f"op_type={node.op_type}{msg}{msg2}"
210
+ )
211
+ return None
212
+
213
+ def print_match(self, graph_node: ir.Node, pattern_node: orp.NodePattern) -> str:
214
+ s1 = _node_to_str(graph_node)
215
+ s2 = _node_to_str(pattern_node)
216
+ return f"match {s1} with pattern: {s2}"
217
+
218
+ def _debug_print(self) -> str:
219
+ if not hasattr(self, "_debug"):
220
+ return ""
221
+
222
+ def _s(s: str) -> str:
223
+ if len(s) <= 30:
224
+ return s
225
+ return f"{s[:15]}...{s[-15:]}"
226
+
227
+ def _p(n: ir.Node, full: bool = False) -> str:
228
+ if full:
229
+ return str(n)
230
+ return _node_to_str(n)
231
+
232
+ rows = []
233
+ for k, v in sorted(self._debug.items()):
234
+ if k == "stack":
235
+ rows.append(f"len({k})={len(v)}:{v}") # type: ignore[arg-type]
236
+ continue
237
+ if k == "iteration":
238
+ rows.append(f"{k}={v}")
239
+ continue
240
+ if k == "matched":
241
+ rows.append(f"--matched-- #{len(v)}") # type: ignore[arg-type]
242
+ for pattern_node, graph_node in v.items():
243
+ rows.append(
244
+ f" {_p(pattern_node)} ~ {_p(graph_node)} [{id(pattern_node)}-{id(graph_node)}]"
245
+ )
246
+ continue
247
+ if k == "hint":
248
+ rows.append(f"--hint--: {v[0]}") # type: ignore[arg-type]
249
+ for i in v[1:]:
250
+ if isinstance(i, str):
251
+ rows.append(" " + i)
252
+ if isinstance(i, ir.Node):
253
+ rows.append(" " + _p(i, full=True))
254
+ continue
255
+ if k in {"node", "pattern", "pattern_node", "pattern_nodes"}:
256
+ continue
257
+ rows.append(f"-- not shown {k}")
258
+
259
+ return "\n".join(rows)
260
+
261
+ def _hint(self, *args: Any) -> None:
262
+ """Add debugging information to help users."""
263
+ self._debug["hint"] = args
264
+
265
+ def _match_backward(
266
+ self,
267
+ starting_node: ir.Node,
268
+ matched: dict[orp.NodePattern, ir.Node],
269
+ stack: list[orp.NodePattern],
270
+ graph_node: ir.Node,
271
+ pattern_node: orp.NodePattern,
272
+ ) -> int | None:
273
+ """
274
+ Matches backward.
275
+
276
+ Args:
277
+ starting_node: root node (the node the matched begain with, used only for debugging)
278
+ matched: nodes of the pattern matched as already matched
279
+ stack: next node to look into
280
+ graph_node: node coming from the graph
281
+ pattern_node: node coming from the pattern
282
+
283
+ Returns:
284
+ number of matched nodes, None or False to indicate a failed match
285
+ """
286
+ match_count = 0
287
+
288
+ # predecessors
289
+ if len(graph_node.inputs) != len(pattern_node.inputs):
290
+ # not the same number of inputs
291
+ self._hint(
292
+ "BACKWARD: not the same number of inputs",
293
+ "-- pattern",
294
+ pattern_node,
295
+ "-- model",
296
+ graph_node,
297
+ )
298
+ return self.none(starting_node, inspect.currentframe().f_lineno)
299
+
300
+ for graph_input, pattern_input in zip(graph_node.inputs, pattern_node.inputs):
301
+ if len(graph_input.uses()) != len(pattern_input.uses()):
302
+ self._hint(
303
+ "BACKWARD: one input is used outside the pattern",
304
+ "-- pattern",
305
+ pattern_node,
306
+ "-- model",
307
+ graph_node,
308
+ )
309
+ return self.none(starting_node, inspect.currentframe().f_lineno)
310
+
311
+ for graph_value, pattern_value in zip(graph_node.inputs, pattern_node.inputs):
312
+ # TODO(rama): Handle constant-pattern
313
+ pattern_pred = pattern_value.producer()
314
+ if pattern_pred is None:
315
+ # pattern_pred is None means the pattern backward search ends here.
316
+ result = self._match_values_forward(
317
+ starting_node, matched, stack, graph_value, pattern_value
318
+ )
319
+ if result is None:
320
+ return result
321
+ match_count += result
322
+ continue
323
+ graph_pred = graph_value.producer()
324
+ if graph_pred is None:
325
+ # No node in the graph.
326
+ return self.none(starting_node, inspect.currentframe().f_lineno)
327
+ if graph_pred.op_identifier() != pattern_pred.op_identifier():
328
+ self._hint(
329
+ "BACKWARD: different node types",
330
+ "--pattern",
331
+ _node_to_str(pattern_pred),
332
+ "-- model",
333
+ _node_to_str(graph_pred),
334
+ )
335
+ return self.none(starting_node, inspect.currentframe().f_lineno)
336
+ # matching backward
337
+ if pattern_pred not in matched:
338
+ if self.verbose >= 10:
339
+ print(
340
+ f"[GenericPattern._match_backward] {self.print_match(graph_pred, pattern_pred)}"
341
+ )
342
+ matched[pattern_pred] = graph_pred
343
+ stack.append(pattern_pred)
344
+ match_count += 1
345
+ if self.verbose > 5 and match_count > 0:
346
+ print(f"[GenericPatternMatcher._match_backward] add {match_count} nodes")
347
+ return match_count
348
+
349
+ def _match_values_forward(
350
+ self,
351
+ starting_node: ir.Node,
352
+ matched: dict[orp.NodePattern, ir.Node],
353
+ stack: list[orp.NodePattern],
354
+ graph_value: ir.Value,
355
+ pattern_value: orp.ValuePattern,
356
+ ) -> int | None:
357
+ """
358
+ Matches forward.
359
+
360
+ Args:
361
+ starting_node: root node (the node the match begins with, used only for debugging)
362
+ matched: nodes of the pattern matched as already matched
363
+ stack: next node to look into
364
+ graph_value: value coming from the graph
365
+ pattern_value: pattern value coming from the pattern
366
+
367
+ Returns:
368
+ number of matched nodes to continue, None or False to indicate a failed match
369
+ """
370
+ match_count = 0
371
+ graph_node_users = [user for user, _ in graph_value.uses()]
372
+ pattern_node_users = [user for user, _ in pattern_value.uses()]
373
+ if not pattern_node_users:
374
+ # The pattern has no node forward, the matching stops.
375
+ return match_count
376
+ if len(graph_node_users) < len(pattern_node_users):
377
+ # Not enough node in the graph to match the pattern. A match is not possible
378
+ return self.none(starting_node, inspect.currentframe().f_lineno)
379
+
380
+ # Here comes the fun part, there is the same number of successors or more
381
+ # nodes in the graph to match with the pattern.
382
+ # And we have to handle the nodes already matched as found.
383
+ # Hopefully, there is only one option.
384
+
385
+ if len(graph_node_users) == len(pattern_node_users) == 1:
386
+ # Let's deal with the simple case
387
+ if graph_node_users[0].op_identifier() != pattern_node_users[0].op_identifier():
388
+ return self.none(starting_node, inspect.currentframe().f_lineno)
389
+
390
+ node = pattern_node_users[0]
391
+ if node not in matched:
392
+ if self.verbose >= 10:
393
+ print(
394
+ f"[GenericPatternMatcher._match_values_forward]{self.print_match(graph_node_users[0], pattern_node_users[0])}"
395
+ )
396
+ matched[node] = graph_node_users[0]
397
+ stack.append(node)
398
+ match_count += 1
399
+ return match_count
400
+
401
+ # Let's remove the nodes already matched.
402
+ pattern_node_users_not_matched = [
403
+ unmatched_node
404
+ for unmatched_node in pattern_node_users
405
+ if unmatched_node not in matched
406
+ ]
407
+ pattern_node_users_matched = [
408
+ matched[matched_node]
409
+ for matched_node in pattern_node_users
410
+ if matched_node in matched
411
+ ]
412
+ assert len(pattern_node_users_matched) + len(pattern_node_users_not_matched) == len(
413
+ pattern_node_users
414
+ ), (
415
+ f"pattern_node_users_not_matched={pattern_node_users_not_matched}, "
416
+ f"pattern_node_users_matched={pattern_node_users_matched}, "
417
+ f"pattern_node_users={pattern_node_users}, "
418
+ f"matched={matched}"
419
+ )
420
+ free = list(set(graph_node_users) - set(pattern_node_users_matched))
421
+ if not pattern_node_users_not_matched:
422
+ # Everything is already matched.
423
+ return match_count
424
+ if len(free) < len(pattern_node_users_not_matched):
425
+ # Not enough successors to match the remaining patterns.
426
+ return self.none(starting_node, inspect.currentframe().f_lineno)
427
+ if len(pattern_node_users_not_matched) == len(free) == 1:
428
+ # Only one option again.
429
+ graph_node = free[0]
430
+ if pattern_node_users_not_matched[0].op_identifier() != graph_node.op_identifier():
431
+ return self.none(starting_node, inspect.currentframe().f_lineno)
432
+
433
+ key = pattern_node_users_not_matched[0]
434
+ if self.verbose >= 10:
435
+ print(
436
+ f"[GenericPatternMatcher._match_values_forward] {self.print_match(graph_node, pattern_node_users_not_matched[0])}"
437
+ )
438
+ matched[key] = graph_node
439
+ stack.append(key)
440
+ match_count += 1
441
+ return match_count
442
+
443
+ # And now another fun part, let's try to handle the case when
444
+ # there is only one option, matching on node type only returns one
445
+ # option.
446
+ expected_op_type = [_.op_identifier() for _ in pattern_node_users_not_matched]
447
+ got_op_type = [_.op_identifier() for _ in free]
448
+
449
+ ec = collections.Counter(expected_op_type)
450
+ gc = collections.Counter(got_op_type)
451
+ if len(ec) != len(gc) or set(ec) != set(gc):
452
+ # unique operator types is different.
453
+ self._hint(
454
+ "FORWARD: unique operator types are different",
455
+ "-- pattern",
456
+ ec,
457
+ pattern_value,
458
+ "-- model",
459
+ gc,
460
+ graph_value,
461
+ "-- model-matched",
462
+ pattern_node_users_matched,
463
+ )
464
+ return self.none(starting_node, inspect.currentframe().f_lineno)
465
+ for k, v in ec.items():
466
+ if gc[k] < v:
467
+ # Not enough types to match.
468
+ return self.none(starting_node, inspect.currentframe().f_lineno)
469
+
470
+ # At this stage, we know matching the types is possible.
471
+ # We first mark whatever is possible.
472
+ ptype_to_node = {_.op_identifier(): _ for _ in pattern_node_users_not_matched}
473
+ gtype_to_node = {_.op_identifier(): _ for _ in free}
474
+ missing = []
475
+ for k, v in ec.items():
476
+ if gc[k] == v == 1:
477
+ key = id(ptype_to_node[k])
478
+ if key not in matched:
479
+ if self.verbose >= 10:
480
+ print(
481
+ f"[GenericPatternMatcher._match_values_forward] match "
482
+ f"{self.print_match(gtype_to_node[k], ptype_to_node[k])}"
483
+ )
484
+ matched[key] = gtype_to_node[k]
485
+ stack.append(key)
486
+ match_count += 1
487
+ else:
488
+ missing.append(k)
489
+
490
+ if not missing:
491
+ return match_count
492
+
493
+ # At this stage, there are mutiple options for matching. We can:
494
+ # 1. make assumptions and continue
495
+ # 2. mark the node as incomplete matching, we could end up stuck anyway.
496
+ raise NotImplementedError(
497
+ f"There are more than one option, this will be implemented later, ec={ec}, gc={gc}"
498
+ )
499
+
500
+ def _match_forward(
501
+ self,
502
+ starting_node: ir.Node,
503
+ matched: dict[orp.NodePattern, ir.Node],
504
+ stack: list[orp.NodePattern],
505
+ graph_node: ir.Node,
506
+ pattern_node: orp.NodePattern,
507
+ ) -> int | None:
508
+ """
509
+ Matches forward.
510
+
511
+ Args:
512
+ starting_node: root node (the node the match begins with, used only for debugging)
513
+ matched: nodes of the pattern matched as already matched
514
+ stack: next node to look into
515
+ graph_node: node coming from the graph
516
+ pattern_node: node coming from the pattern
517
+
518
+ Returns:
519
+ number of matched nodes to continue, None or False to indicate a failed match
520
+ """
521
+ match_count = 0
522
+
523
+ # successors
524
+ if len(graph_node.outputs) != len(pattern_node.outputs):
525
+ # not the same number of outputs
526
+ self._hint(
527
+ "FORWARD: not the same number of output_names",
528
+ "-- pattern",
529
+ pattern_node,
530
+ "-- model",
531
+ graph_node,
532
+ )
533
+ return self.none(starting_node, inspect.currentframe().f_lineno)
534
+
535
+ for graph_output, pattern_output in zip(graph_node.outputs, pattern_node.outputs):
536
+ result = self._match_values_forward(
537
+ starting_node, matched, stack, graph_output, pattern_output
538
+ )
539
+ if result is None:
540
+ return result
541
+ match_count += result
542
+
543
+ if self.verbose > 5 and match_count > 0:
544
+ print(f"[GenericPatternMatcher._match_forward] add {match_count} nodes")
545
+ return match_count
546
+
547
+ def match(
548
+ self,
549
+ model: ir.Model,
550
+ graph_or_function: ir.Graph | ir.Function,
551
+ node: ir.Node,
552
+ *,
553
+ verbose: int = 0,
554
+ remove_nodes: bool = True,
555
+ tracer: orp.MatchingTracer | None = None,
556
+ ) -> orp.MatchResult | None:
557
+ if not remove_nodes:
558
+ raise NotImplementedError(
559
+ "remove_nodes=False is not implemented in GenericPatternMatcher"
560
+ )
561
+ del model
562
+ del graph_or_function
563
+ self.verbose = verbose
564
+ self._debug = {}
565
+
566
+ # Let's match the last node.
567
+ # Then we need to match successors and predecessors.
568
+ last_pattern_node = self.pattern.node(-1)
569
+ if node.op_identifier() != last_pattern_node.op_identifier():
570
+ # The last node does not have the same op_identifier().
571
+ return self.none()
572
+
573
+ if self.verbose > 5:
574
+ print(
575
+ f"[GenericPatternMatcher.match] Matching started at node: {_node_to_str(node)}"
576
+ )
577
+ if self.verbose >= 10:
578
+ print(f"[GenericPatternMatcher.match] match pattern {self}")
579
+
580
+ all_pattern_nodes = set(self.pattern)
581
+ matched: dict[orp.NodePattern, ir.Node] = {last_pattern_node: node}
582
+ stack: list[orp.NodePattern] = [last_pattern_node]
583
+ iteration = 0
584
+
585
+ if self.verbose > 5:
586
+ self._debug = dict(
587
+ pattern=self.pattern,
588
+ matched=matched,
589
+ stack=stack,
590
+ iteration=iteration,
591
+ node=node,
592
+ pattern_node=last_pattern_node,
593
+ pattern_nodes=self.pattern,
594
+ )
595
+
596
+ max_iter = self.pattern.num_nodes() * 2
597
+ while stack and iteration < max_iter:
598
+ nodes_not_in_pattern = set(matched.keys()) - all_pattern_nodes
599
+ assert not nodes_not_in_pattern, (
600
+ f"Some nodes are not part of the pattern: {nodes_not_in_pattern}"
601
+ f"\nall_pattern_nodes={all_pattern_nodes}"
602
+ )
603
+
604
+ # TODO(justinchuby): Change to a for loop
605
+ iteration += 1
606
+ if self.verbose > 5:
607
+ print(
608
+ f"[GenericPatternMatcher.match] iteration={iteration} "
609
+ f"n_matched={len(matched)}, n_stack={len(stack)}, "
610
+ f"matched_types={collections.Counter(_.op_identifier() for _ in matched)}"
611
+ )
612
+ next_pattern_node = stack.pop()
613
+ next_graph_node = matched[next_pattern_node]
614
+
615
+ result = self._match_backward(
616
+ node, matched, stack, next_graph_node, next_pattern_node
617
+ )
618
+ if result is None:
619
+ if self.verbose > 5:
620
+ print("[GenericPatternMatcher.match] done. backward failed.")
621
+ return result
622
+
623
+ nodes_not_in_pattern = set(matched.keys()) - all_pattern_nodes
624
+ assert not nodes_not_in_pattern, (
625
+ f"Some nodes are not part of the pattern: {nodes_not_in_pattern}"
626
+ )
627
+
628
+ result = self._match_forward(
629
+ node, matched, stack, next_graph_node, next_pattern_node
630
+ )
631
+ if result is None:
632
+ if self.verbose > 5:
633
+ print("[GenericPatternMatcher.match] done. forward failed.")
634
+ return result
635
+
636
+ nodes_not_in_pattern = set(matched.keys()) - all_pattern_nodes
637
+ assert not nodes_not_in_pattern, (
638
+ f"Some nodes are not part of the pattern: {nodes_not_in_pattern}"
639
+ )
640
+
641
+ if self.verbose > 5:
642
+ self._debug["iteration"] = iteration
643
+
644
+ if iteration >= max_iter and stack:
645
+ self._hint(f"reached {iteration}>={max_iter} iterations")
646
+ return self.none(node, inspect.currentframe().f_lineno)
647
+
648
+ if self.verbose > 5:
649
+ print(f"[GenericPatternMatcher.match] done. {len(matched)} matched nodes")
650
+
651
+ # At this point, the pattern is matched but let's make sure.
652
+ assert len(matched) == self.pattern.num_nodes(), (
653
+ f"Number of matched nodes is different, {len(matched)} matched nodes, "
654
+ f"and {len(self.pattern)} nodes in the pattern, matched is {matched}"
655
+ )
656
+ assert len(stack) == 0, f"There are still {len(stack)} nodes to explore."
657
+
658
+ # We order the matched nodes in the same order than the pattern
659
+ # to let next functions to be able to build the matching again.
660
+ matched_nodes = [matched[pattern_node] for pattern_node in self.pattern]
661
+ return _to_match_result(PatternMatchResult(self.pattern, matched_nodes))
662
+
663
+
664
+ def make_pattern_rule(
665
+ match_pattern_function: Callable,
666
+ apply_pattern_function: Callable,
667
+ validate_mapping: Callable | None = None,
668
+ verbose: int = 0,
669
+ ) -> orp.RewriteRule:
670
+ """
671
+ Creates a rewriting rule from a callable or a function proto.
672
+
673
+ Args:
674
+ match_pattern_function: an onnxscript-like function that defines
675
+ the pattern subgraph (nodes) to be replaced
676
+ apply_pattern_function: an onnxscript-like function that constructs
677
+ the replacement subgraph (new nodes replacing the matched nodes)
678
+ validate_mapping: a function that validates the matching subgraph once
679
+ it is found. If it returns False the pattern is not applied.
680
+ If not specified, it is equivalent to a function that always return True
681
+ verbose: verbosity level
682
+
683
+ Returns:
684
+ the rewriting rule
685
+ """
686
+
687
+ warnings.warn(
688
+ "make_pattern_rule(...) is deprecated, use pattern.RewriteRule(...) instead",
689
+ FutureWarning,
690
+ stacklevel=2,
691
+ )
692
+ pattern = orp._to_graph_pattern(match_pattern_function)
693
+ matcher = GenericPatternMatcher(pattern)
694
+ return orp.RewriteRule(
695
+ pattern,
696
+ apply_pattern_function,
697
+ validate_mapping,
698
+ matcher,
699
+ verbose=verbose,
700
+ )