Skip to content

Commit 90ad5e7

Browse files
authored
Improve documentation on replace_all_uses_with (onnx#237)
Update the docstring to reflect current usage and improve tips. Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
1 parent 112b0c5 commit 90ad5e7

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

src/onnx_ir/_convenience/__init__.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -315,9 +315,23 @@ def replace_all_uses_with(
315315
users of the first value is replaced with the first replacement, and so on.
316316
317317
.. note::
318-
You still need to update the graph outputs if any of the values being
319-
replaced are part of the graph outputs. Be sure to remove the old nodes
320-
from the graph using ``graph.remove()`` if they are no longer needed.
318+
Be sure to remove the old nodes from the graph using ``graph.remove()``
319+
if they are no longer needed, or use :class:`onnx_ir.passes.common.RemoveUnusedNodesPass`
320+
to remove all unused nodes in the graph.
321+
322+
.. tip::
323+
**Handling graph outputs**
324+
325+
To also replace graph outputs that reference the values being replaced, either
326+
set ``replace_graph_outputs`` to True, or manually update the graph outputs
327+
before calling this function to avoid an error being raised when ``replace_graph_outputs=False``.
328+
329+
Be careful when a value appears multiple times in the graph outputs -
330+
this is invalid. An identity node will need to be added on each duplicated
331+
outputs to ensure a valid ONNX graph.
332+
333+
You may also want to assign the name of this value to the replacement value
334+
to maintain the name when it is a graph output.
321335
322336
.. versionadded:: 0.1.12
323337
The ``replace_graph_outputs`` parameter is added.

src/onnx_ir/_core.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2453,13 +2453,19 @@ def replace_all_uses_with(
24532453
) -> None:
24542454
"""Replace all uses of this value with another value.
24552455
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.
2456+
.. tip::
2457+
**Handling graph outputs**
24602458
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.
2459+
To also replace graph outputs that reference the values being replaced, either
2460+
set ``replace_graph_outputs`` to True, or manually update the graph outputs
2461+
before calling this function to avoid an error being raised when ``replace_graph_outputs=False``.
2462+
2463+
Be careful when a value appears multiple times in the graph outputs -
2464+
this is invalid. An identity node will need to be added on each duplicated
2465+
outputs to ensure a valid ONNX graph.
2466+
2467+
You may also want to assign the name of this value to the replacement value
2468+
to maintain the name when it is a graph output.
24632469
24642470
To replace usage of a sequence of values with another sequence of values, consider using
24652471
:func:`onnx_ir.convenience.replace_all_uses_with`.

src/onnx_ir/passes/common/topological_sort.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@
1313

1414

1515
class TopologicalSortPass(ir.passes.InPlacePass):
16-
"""Topologically sort graphs and functions in a model."""
16+
"""Topologically sort graphs and functions in a model.
17+
18+
The sort is stable, preserving the relative order of nodes that are not
19+
dependent on each other. Read more at :meth:`onnx_ir.Graph.sort`.
20+
"""
1721

1822
def call(self, model: ir.Model) -> ir.passes.PassResult:
1923
original_nodes = list(model.graph)

0 commit comments

Comments
 (0)