onnx-ir 0.1.11__py3-none-any.whl → 0.1.12__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.
Potentially problematic release.
This version of onnx-ir might be problematic. Click here for more details.
- onnx_ir/__init__.py +1 -1
- onnx_ir/_convenience/__init__.py +25 -14
- onnx_ir/_core.py +49 -0
- onnx_ir/_protocols.py +11 -0
- onnx_ir/passes/common/common_subexpression_elimination.py +3 -2
- onnx_ir/passes/common/constant_manipulation.py +1 -1
- onnx_ir/passes/common/identity_elimination.py +4 -10
- onnx_ir/passes/common/initializer_deduplication.py +2 -2
- {onnx_ir-0.1.11.dist-info → onnx_ir-0.1.12.dist-info}/METADATA +1 -2
- {onnx_ir-0.1.11.dist-info → onnx_ir-0.1.12.dist-info}/RECORD +13 -13
- {onnx_ir-0.1.11.dist-info → onnx_ir-0.1.12.dist-info}/WHEEL +0 -0
- {onnx_ir-0.1.11.dist-info → onnx_ir-0.1.12.dist-info}/licenses/LICENSE +0 -0
- {onnx_ir-0.1.11.dist-info → onnx_ir-0.1.12.dist-info}/top_level.txt +0 -0
onnx_ir/__init__.py
CHANGED
onnx_ir/_convenience/__init__.py
CHANGED
|
@@ -280,6 +280,7 @@ def convert_attributes(
|
|
|
280
280
|
def replace_all_uses_with(
|
|
281
281
|
values: _protocols.ValueProtocol | Sequence[_protocols.ValueProtocol],
|
|
282
282
|
replacements: _protocols.ValueProtocol | Sequence[_protocols.ValueProtocol],
|
|
283
|
+
replace_graph_outputs: bool = False,
|
|
283
284
|
) -> None:
|
|
284
285
|
"""Replace all uses of the given values with the replacements.
|
|
285
286
|
|
|
@@ -318,9 +319,22 @@ def replace_all_uses_with(
|
|
|
318
319
|
replaced are part of the graph outputs. Be sure to remove the old nodes
|
|
319
320
|
from the graph using ``graph.remove()`` if they are no longer needed.
|
|
320
321
|
|
|
322
|
+
.. versionadded:: 0.1.12
|
|
323
|
+
The ``replace_graph_outputs`` parameter is added.
|
|
324
|
+
|
|
325
|
+
.. versionadded:: 0.1.12
|
|
326
|
+
ValueError is raised when ``replace_graph_outputs`` is False && when the value to
|
|
327
|
+
replace is a graph output.
|
|
328
|
+
|
|
321
329
|
Args:
|
|
322
330
|
values: The value or values to be replaced.
|
|
323
331
|
replacements: The new value or values to use as inputs.
|
|
332
|
+
replace_graph_outputs: If True, graph outputs that reference the values
|
|
333
|
+
being replaced will also be updated to reference the replacements.
|
|
334
|
+
|
|
335
|
+
Raises:
|
|
336
|
+
ValueError: When ``replace_graph_outputs`` is False && when the value to
|
|
337
|
+
replace is a graph output.
|
|
324
338
|
"""
|
|
325
339
|
if not isinstance(values, Sequence):
|
|
326
340
|
values = (values,)
|
|
@@ -329,8 +343,7 @@ def replace_all_uses_with(
|
|
|
329
343
|
if len(values) != len(replacements):
|
|
330
344
|
raise ValueError("The number of values and replacements must match.")
|
|
331
345
|
for value, replacement in zip(values, replacements):
|
|
332
|
-
|
|
333
|
-
user_node.replace_input_with(index, replacement)
|
|
346
|
+
value.replace_all_uses_with(replacement, replace_graph_outputs=replace_graph_outputs)
|
|
334
347
|
|
|
335
348
|
|
|
336
349
|
def create_value_mapping(graph: _core.Graph) -> dict[str, _core.Value]:
|
|
@@ -397,20 +410,18 @@ def replace_nodes_and_values(
|
|
|
397
410
|
"""
|
|
398
411
|
for old_value, new_value in zip(old_values, new_values):
|
|
399
412
|
# Propagate relevant info from old value to new value
|
|
400
|
-
# TODO(Rama): Perhaps this should be a separate utility function.
|
|
401
|
-
|
|
402
|
-
new_value.
|
|
403
|
-
new_value.
|
|
404
|
-
|
|
405
|
-
|
|
413
|
+
# TODO(Rama): Perhaps this should be a separate utility function.
|
|
414
|
+
new_value.type = old_value.type if old_value.type is not None else new_value.type
|
|
415
|
+
new_value.shape = old_value.shape if old_value.shape is not None else new_value.shape
|
|
416
|
+
new_value.const_value = (
|
|
417
|
+
old_value.const_value
|
|
418
|
+
if old_value.const_value is not None
|
|
419
|
+
else new_value.const_value
|
|
420
|
+
)
|
|
421
|
+
new_value.name = old_value.name if old_value.name is not None else new_value.name
|
|
406
422
|
|
|
407
423
|
# Reconnect the users of the deleted values to use the new values
|
|
408
|
-
replace_all_uses_with(old_values, new_values)
|
|
409
|
-
# Update graph/function outputs if the node generates output
|
|
410
|
-
replacement_mapping = dict(zip(old_values, new_values))
|
|
411
|
-
for idx, graph_or_function_output in enumerate(graph_or_function.outputs):
|
|
412
|
-
if graph_or_function_output in replacement_mapping:
|
|
413
|
-
graph_or_function.outputs[idx] = replacement_mapping[graph_or_function_output]
|
|
424
|
+
replace_all_uses_with(old_values, new_values, replace_graph_outputs=True)
|
|
414
425
|
|
|
415
426
|
# insert new nodes after the index node
|
|
416
427
|
graph_or_function.insert_after(insertion_point, new_nodes)
|
onnx_ir/_core.py
CHANGED
|
@@ -2448,6 +2448,55 @@ class Value(_protocols.ValueProtocol, _display.PrettyPrintable):
|
|
|
2448
2448
|
"""Whether the value is an initializer of a graph."""
|
|
2449
2449
|
return self._is_initializer
|
|
2450
2450
|
|
|
2451
|
+
def replace_all_uses_with(
|
|
2452
|
+
self, replacement: Value, /, replace_graph_outputs: bool = False
|
|
2453
|
+
) -> None:
|
|
2454
|
+
"""Replace all uses of this value with another value.
|
|
2455
|
+
|
|
2456
|
+
If the value is an output of a graph and ``replace_graph_outputs`` is ``True``,
|
|
2457
|
+
the graph output will also be replaced. Be careful when a value appears multiple times
|
|
2458
|
+
in the graph outputs - this is invalid. An identity node will need to be added on each
|
|
2459
|
+
duplicated outputs to ensure a valid ONNX graph.
|
|
2460
|
+
|
|
2461
|
+
You may also want to assign the name of this value to the replacement value
|
|
2462
|
+
to maintain the name when it is a graph output.
|
|
2463
|
+
|
|
2464
|
+
To replace usage of a sequence of values with another sequence of values, consider using
|
|
2465
|
+
:func:`onnx_ir.convenience.replace_all_uses_with`.
|
|
2466
|
+
|
|
2467
|
+
.. versionadded:: 0.1.12
|
|
2468
|
+
|
|
2469
|
+
Args:
|
|
2470
|
+
replacement: The value to replace all uses with.
|
|
2471
|
+
replace_graph_outputs: If True, graph outputs that reference this value
|
|
2472
|
+
will also be updated to reference the replacement.
|
|
2473
|
+
|
|
2474
|
+
Raises:
|
|
2475
|
+
ValueError: When ``replace_graph_outputs`` is False && when the value to
|
|
2476
|
+
replace is a graph output.
|
|
2477
|
+
"""
|
|
2478
|
+
# NOTE: Why we don't replace the value name when the value is an output:
|
|
2479
|
+
# When the replacement value is already an output of the graph, renaming it
|
|
2480
|
+
# to the name of this value will cause name conflicts. It is better to let
|
|
2481
|
+
# the user handle the renaming explicitly and insert identity nodes if needed.
|
|
2482
|
+
if self.is_graph_output():
|
|
2483
|
+
graph = self.graph
|
|
2484
|
+
assert graph is not None
|
|
2485
|
+
|
|
2486
|
+
if not replace_graph_outputs:
|
|
2487
|
+
raise ValueError(
|
|
2488
|
+
f"{self!r} is an output of graph {graph.name!r}. "
|
|
2489
|
+
"Set replace_graph_outputs=True or replace the graph output frist before "
|
|
2490
|
+
"calling replace_all_uses_with."
|
|
2491
|
+
)
|
|
2492
|
+
|
|
2493
|
+
for i, output in enumerate(graph.outputs):
|
|
2494
|
+
if output is self:
|
|
2495
|
+
graph.outputs[i] = replacement
|
|
2496
|
+
|
|
2497
|
+
for user_node, index in self.uses():
|
|
2498
|
+
user_node.replace_input_with(index, replacement)
|
|
2499
|
+
|
|
2451
2500
|
|
|
2452
2501
|
@deprecated("Input is deprecated since 0.1.9. Use ir.val(...) instead.")
|
|
2453
2502
|
def Input(
|
onnx_ir/_protocols.py
CHANGED
|
@@ -203,6 +203,17 @@ class ValueProtocol(Protocol):
|
|
|
203
203
|
"""Whether this value is an output of a graph."""
|
|
204
204
|
...
|
|
205
205
|
|
|
206
|
+
def replace_all_uses_with(
|
|
207
|
+
self, new_value: ValueProtocol | None, replace_graph_outputs: bool = False
|
|
208
|
+
) -> None:
|
|
209
|
+
"""Replace all uses of this value with the given new value.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
new_value: The new value to replace this value with.
|
|
213
|
+
replace_graph_outputs: Whether to replace graph outputs that use this value.
|
|
214
|
+
"""
|
|
215
|
+
...
|
|
216
|
+
|
|
206
217
|
|
|
207
218
|
@typing.runtime_checkable
|
|
208
219
|
class NodeProtocol(Protocol):
|
|
@@ -150,8 +150,6 @@ def _remove_node_and_replace_values(
|
|
|
150
150
|
remove_values: The values to replace.
|
|
151
151
|
new_values: The values to replace with.
|
|
152
152
|
"""
|
|
153
|
-
# Reconnect the users of the deleted values to use the new values
|
|
154
|
-
ir.convenience.replace_all_uses_with(remove_values, new_values)
|
|
155
153
|
# Update graph/function outputs if the node generates output
|
|
156
154
|
if any(remove_value.is_graph_output() for remove_value in remove_values):
|
|
157
155
|
replacement_mapping = dict(zip(remove_values, new_values))
|
|
@@ -185,6 +183,9 @@ def _remove_node_and_replace_values(
|
|
|
185
183
|
new_value.name = graph_output.name
|
|
186
184
|
graph.outputs[idx] = new_value
|
|
187
185
|
|
|
186
|
+
# Reconnect the users of the deleted values to use the new values
|
|
187
|
+
ir.convenience.replace_all_uses_with(remove_values, new_values)
|
|
188
|
+
|
|
188
189
|
graph.remove(remove_node, safe=True)
|
|
189
190
|
|
|
190
191
|
|
|
@@ -78,7 +78,7 @@ class LiftConstantsToInitializersPass(ir.passes.InPlacePass):
|
|
|
78
78
|
assert node.graph is not None
|
|
79
79
|
node.graph.register_initializer(initializer)
|
|
80
80
|
# Replace the constant node with the initializer
|
|
81
|
-
|
|
81
|
+
node.outputs[0].replace_all_uses_with(initializer)
|
|
82
82
|
node.graph.remove(node, safe=True)
|
|
83
83
|
count += 1
|
|
84
84
|
logger.debug(
|
|
@@ -105,20 +105,14 @@ class IdentityEliminationPass(ir.passes.InPlacePass):
|
|
|
105
105
|
|
|
106
106
|
# Case 1 & 2 (merged): Eliminate the identity node
|
|
107
107
|
# Replace all uses of output with input
|
|
108
|
-
ir.convenience.replace_all_uses_with(
|
|
108
|
+
ir.convenience.replace_all_uses_with(
|
|
109
|
+
output_value, input_value, replace_graph_outputs=True
|
|
110
|
+
)
|
|
109
111
|
|
|
110
112
|
# If output is a graph output, we need to rename input and update graph outputs
|
|
111
113
|
if output_is_graph_output:
|
|
112
|
-
# Store the original output name
|
|
113
|
-
original_output_name = output_value.name
|
|
114
|
-
|
|
115
114
|
# Update the input value to have the output's name
|
|
116
|
-
input_value.name =
|
|
117
|
-
|
|
118
|
-
# Update graph outputs to point to the input value
|
|
119
|
-
for idx, graph_output in enumerate(graph_like.outputs):
|
|
120
|
-
if graph_output is output_value:
|
|
121
|
-
graph_like.outputs[idx] = input_value
|
|
115
|
+
input_value.name = output_value.name
|
|
122
116
|
|
|
123
117
|
# Remove the identity node
|
|
124
118
|
graph_like.remove(node, safe=True)
|
|
@@ -100,7 +100,7 @@ class DeduplicateInitializersPass(ir.passes.InPlacePass):
|
|
|
100
100
|
if key in initializers:
|
|
101
101
|
modified = True
|
|
102
102
|
initializer_to_keep = initializers[key] # type: ignore[index]
|
|
103
|
-
|
|
103
|
+
initializer.replace_all_uses_with(initializer_to_keep)
|
|
104
104
|
assert initializer.name is not None
|
|
105
105
|
graph.initializers.pop(initializer.name)
|
|
106
106
|
logger.info(
|
|
@@ -165,7 +165,7 @@ class DeduplicateHashedInitializersPass(ir.passes.InPlacePass):
|
|
|
165
165
|
continue
|
|
166
166
|
modified = True
|
|
167
167
|
initializer_to_keep = initializers[key] # type: ignore[index]
|
|
168
|
-
|
|
168
|
+
initializer.replace_all_uses_with(initializer_to_keep)
|
|
169
169
|
assert initializer.name is not None
|
|
170
170
|
graph.initializers.pop(initializer.name)
|
|
171
171
|
logger.info(
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: onnx-ir
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.12
|
|
4
4
|
Summary: Efficient in-memory representation for ONNX
|
|
5
5
|
Author-email: ONNX Contributors <onnx-technical-discuss@lists.lfaidata.foundation>
|
|
6
6
|
License-Expression: Apache-2.0
|
|
@@ -20,7 +20,6 @@ Dynamic: license-file
|
|
|
20
20
|
# <img src="docs/_static/logo-light.png" alt="ONNX IR" width="250"/>
|
|
21
21
|
|
|
22
22
|
[](https://pypi.org/project/onnx-ir)
|
|
23
|
-
[](https://pypi.org/project/onnx-ir)
|
|
24
23
|
[](https://github.com/astral-sh/ruff)
|
|
25
24
|
[](https://codecov.io/gh/onnx/ir-py)
|
|
26
25
|
[](https://pepy.tech/projects/onnx-ir)
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
onnx_ir/__init__.py,sha256=
|
|
2
|
-
onnx_ir/_core.py,sha256=
|
|
1
|
+
onnx_ir/__init__.py,sha256=bPBQNMnNPrPCuJslVrQdyzwxDjsjPyWqqtR7rOt95vM,3441
|
|
2
|
+
onnx_ir/_core.py,sha256=A5Zz7AadmKCX_ibMeP7APJUa06xZBL1poj8KBv9N1IE,149270
|
|
3
3
|
onnx_ir/_display.py,sha256=230bMN_hVy47Ug3HkA4o5Tf5Hr21AnBEoq5w0fxjyTs,1300
|
|
4
4
|
onnx_ir/_enums.py,sha256=E7WQ7yQzulBeimamc9q_k4fEUoyH_2PWtaOMpwck_W0,13915
|
|
5
5
|
onnx_ir/_graph_comparison.py,sha256=8_D1gu547eCDotEUqxfIJhUGU_Ufhfji7sfsSraOj3g,727
|
|
@@ -9,7 +9,7 @@ onnx_ir/_linked_list.py,sha256=PXVcbHLMXHLZ6DxZnElnJLWfhBPvYcXUxM8Y3K4J7lM,10592
|
|
|
9
9
|
onnx_ir/_metadata.py,sha256=lzmCaYy4kAJrPW-PSGKF4a78LisxF0hiofySNQ3Mwhg,1544
|
|
10
10
|
onnx_ir/_name_authority.py,sha256=PnoV9TRgMLussZNufWavJXosDWx5avPfldVjMWEEz18,3036
|
|
11
11
|
onnx_ir/_polyfill.py,sha256=LzAGBKQbVDlURC0tgQgaxgkYU4rESgCYnqVs-u-Vsx8,887
|
|
12
|
-
onnx_ir/_protocols.py,sha256=
|
|
12
|
+
onnx_ir/_protocols.py,sha256=PHJtdhATDNwnfocIpvQUNM2dPn1sxfGbU1JPhFDvoIA,21667
|
|
13
13
|
onnx_ir/_tape.py,sha256=nEGY6VZVKuB8FDyXeYr0MTq8j7E4HKOE2yN8qpz4ia0,7007
|
|
14
14
|
onnx_ir/_type_casting.py,sha256=hbikTmgFEu0SEfnbgv2R1LbpuPQ2MCfqto3-oLWhcBc,1645
|
|
15
15
|
onnx_ir/_version_utils.py,sha256=bZThuE7meVHFOY1DLsmss9WshVIp9iig7udGfDbVaK4,1333
|
|
@@ -21,7 +21,7 @@ onnx_ir/tape.py,sha256=4FyfAHmVhQoMsfHMYnBwP2azi6UF6b6pj--ercObqZs,350
|
|
|
21
21
|
onnx_ir/tensor_adapters.py,sha256=1YxOgYro9QV-Dw4XsiD9WQgs1_6h13nu6tfmA1mG7IA,7568
|
|
22
22
|
onnx_ir/testing.py,sha256=WTrjf2joWizDWaYMJlV1KjZMQw7YmZ8NvuBTVn1uY6s,8803
|
|
23
23
|
onnx_ir/traversal.py,sha256=Wy4XphwuapAvm94-5iaz6G8LjIoMFpY7qfPfXzYViEE,4488
|
|
24
|
-
onnx_ir/_convenience/__init__.py,sha256=
|
|
24
|
+
onnx_ir/_convenience/__init__.py,sha256=UWaFlqLwgdk09Mn-9O9jk-ZZHiBqvDzP_zFpMgNp5fw,20183
|
|
25
25
|
onnx_ir/_convenience/_constructors.py,sha256=HqCGtNPMzFFEerC7I5VEyMdBuIdOJDucn9UEdwuymcg,11519
|
|
26
26
|
onnx_ir/_thirdparty/asciichartpy.py,sha256=afQ0fsqko2uYRPAR4TZBrQxvCb4eN8lxZ2yDFbVQq_s,10533
|
|
27
27
|
onnx_ir/passes/__init__.py,sha256=M_Tcl_-qGSNPluFIvOoeDyh0qAwNayaYyXDS5UJUJPQ,764
|
|
@@ -29,18 +29,18 @@ onnx_ir/passes/_pass_infra.py,sha256=xIOw_zZIuOqD4Z_wZ4OvsqXfh2IZMoMlDp1xQ_MPQlc
|
|
|
29
29
|
onnx_ir/passes/common/__init__.py,sha256=NYBrXvkq_CbWRwcZptSsF4u_-1zfN_BClLhVQY0pwYc,1738
|
|
30
30
|
onnx_ir/passes/common/_c_api_utils.py,sha256=g6riA6xNGVWaO5YjVHZ0krrfslWHmRlryRkwB8X56cg,2907
|
|
31
31
|
onnx_ir/passes/common/clear_metadata_and_docstring.py,sha256=YwouLfsNFSaTuGd7uMOGjdvVwG9yHQTkSphUgDlM0ME,2365
|
|
32
|
-
onnx_ir/passes/common/common_subexpression_elimination.py,sha256=
|
|
33
|
-
onnx_ir/passes/common/constant_manipulation.py,sha256=
|
|
34
|
-
onnx_ir/passes/common/identity_elimination.py,sha256=
|
|
35
|
-
onnx_ir/passes/common/initializer_deduplication.py,sha256=
|
|
32
|
+
onnx_ir/passes/common/common_subexpression_elimination.py,sha256=p5hZjWyswn8qm91M6BqlqMUBu78ohxaw72Ky12zrPZ0,7949
|
|
33
|
+
onnx_ir/passes/common/constant_manipulation.py,sha256=qKC--Mr8IzrXPmr6xKJKOVO54M9wh8Ruho423LQ1mfU,9375
|
|
34
|
+
onnx_ir/passes/common/identity_elimination.py,sha256=90HYaq4QYUilHMyRoN_qhwWt0bQpqm5Z69KNU_7z_As,4376
|
|
35
|
+
onnx_ir/passes/common/initializer_deduplication.py,sha256=2OK6h6cLp2VmfT5VUxsknsRXr1fgbY2w5npn0hV1cdE,7221
|
|
36
36
|
onnx_ir/passes/common/inliner.py,sha256=wBoO6yXt6F1AObQjYZHMQ0wn3YH681N4HQQVyaMAYd4,13702
|
|
37
37
|
onnx_ir/passes/common/naming.py,sha256=l_LrUiI3gAoSEVs8YeQ5kRNWp7aMOlBK-SfFUsKobZI,10687
|
|
38
38
|
onnx_ir/passes/common/onnx_checker.py,sha256=_sPmJ2ff9pDB1g9q7082BL6fyubomRaj6svE0cCyDew,1691
|
|
39
39
|
onnx_ir/passes/common/shape_inference.py,sha256=LVdvxjeKtcIEbPcb6mKisxoPJOOawzsm3tzk5j9xqeM,3992
|
|
40
40
|
onnx_ir/passes/common/topological_sort.py,sha256=Vcu1YhBdfRX4LROr0NScjB1Pwz2DjBFD0Z_GxqaxPF8,999
|
|
41
41
|
onnx_ir/passes/common/unused_removal.py,sha256=cBNqaqGnUVyCWxsD7hBzYk4qSglVPo3SmHAvkUo5-Oc,7613
|
|
42
|
-
onnx_ir-0.1.
|
|
43
|
-
onnx_ir-0.1.
|
|
44
|
-
onnx_ir-0.1.
|
|
45
|
-
onnx_ir-0.1.
|
|
46
|
-
onnx_ir-0.1.
|
|
42
|
+
onnx_ir-0.1.12.dist-info/licenses/LICENSE,sha256=z8d0m5b2O9McPEK1xHG_dWgUBT6EfBDz6wA0F7xSPTA,11358
|
|
43
|
+
onnx_ir-0.1.12.dist-info/METADATA,sha256=cys4n_vm2TolOB7h4oGA-LPc0ippsJKJf4hzqoa7bXM,3238
|
|
44
|
+
onnx_ir-0.1.12.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
|
45
|
+
onnx_ir-0.1.12.dist-info/top_level.txt,sha256=W5tROO93YjO0XRxIdjMy4wocp-5st5GiI2ukvW7UhDo,8
|
|
46
|
+
onnx_ir-0.1.12.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|