Skip to content

Introduce insert_nodes convenience function #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

Johansmm
Copy link
Contributor

@Johansmm Johansmm commented Jun 3, 2025

Convenience function to insert a set of nodes in an ir.Value(s).

@Johansmm Johansmm requested review from titaiwangms and a team as code owners June 3, 2025 23:04
Copy link

codecov bot commented Jun 3, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 73.83%. Comparing base (d19f998) to head (f077782).
Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #63      +/-   ##
==========================================
+ Coverage   73.59%   73.83%   +0.23%     
==========================================
  Files          37       37              
  Lines        4492     4521      +29     
  Branches      902      907       +5     
==========================================
+ Hits         3306     3338      +32     
+ Misses        858      856       -2     
+ Partials      328      327       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@Johansmm Johansmm force-pushed the introduce-node-convenience-functions branch from a81116e to 7c37020 Compare June 3, 2025 23:07
@Johansmm Johansmm force-pushed the introduce-node-convenience-functions branch from 7c37020 to 7996c44 Compare June 3, 2025 23:10
@Johansmm Johansmm force-pushed the introduce-node-convenience-functions branch from 7996c44 to 680f2db Compare June 3, 2025 23:12
@Johansmm Johansmm force-pushed the introduce-node-convenience-functions branch from 680f2db to ec140aa Compare June 3, 2025 23:33
out_val.name = val.name
# Propagate relevant info from value to in_value.
# TODO(Rama): Perhaps this should be a separate utility function.
in_val.type = val.type

Check failure

Code scanning / lintrunner

MYPY/union-attr Error

Item "None" of "Value | None" has no attribute "type" To disable, use # type: ignore[union-attr]
# Propagate relevant info from value to in_value.
# TODO(Rama): Perhaps this should be a separate utility function.
in_val.type = val.type
in_val.shape = val.shape

Check failure

Code scanning / lintrunner

MYPY/union-attr Error

Item "None" of "Value | None" has no attribute "shape" To disable, use # type: ignore[union-attr]
in_val.type = val.type
in_val.shape = val.shape
# Rename each value, following each input.
val.name = in_val.name

Check failure

Code scanning / lintrunner

MYPY/union-attr Error

Item "None" of "Value | None" has no attribute "name" To disable, use # type: ignore[union-attr]
# 1. Reconnect the users of values to the outputs
replace_all_uses_with(values, outputs)
# 2. Reconnect the users of inputs to values
replace_all_uses_with(inputs, values)

Check failure

Code scanning / lintrunner

MYPY/arg-type Error

Argument 1 to "replace_all_uses_with" has incompatible type "tuple[Value | None, ...]"; expected "ValueProtocol | Sequence[ValueProtocol]" To disable, use # type: ignore[arg-type]
Johansmm added 2 commits June 4, 2025 01:37
Convenience function to insert a set of nodes in value(s).

Signed-off-by: Johansmm <[email protected]>
@Johansmm Johansmm force-pushed the introduce-node-convenience-functions branch from ec140aa to f077782 Compare June 3, 2025 23:38
@Johansmm Johansmm changed the title Introduce node convenience functions Introduce insert_nodes convenience function Jun 3, 2025


def insert_nodes_in_value(
values: _core.Value | Sequence[_core.Value], new_nodes: Sequence[_core.Node]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you share a use case for this function? I am trying to think what the best api for it should be. Right now the order of appearance of the node inputs implicitly matches the values, which may not be ideal. Also the function name may be improved to be more accurate and succinct.

If we generalize this method, what should an “insert_nodes” api do?

Copy link
Contributor Author

@Johansmm Johansmm Jun 4, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am thinking in some case that is needed to insert new nodes without remove previous ones. Following: microsoft/onnxscript#2064 discussion, this is not possible to do this with onnxscript.rewriter, since rewrite_pattern have to redefine target_pattern.

Moreover I believe with this helper it is easer to include new nodes with something like

for node in ir_model.graph:
    if node.op_type == 'expected_op_type':
        insert_node(node.outputs[0], [new_node])

I found this ease to understand/use.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For your other comment (about order), do you have any suggestion ? When len(values) > 1, I though was a good idea to infer the inputs for the list of nodes keeping the order, being needed to change the position of nodes in the list if values need to be re-order.

Maybe it would be enough if it is better specified in the use of the function, right?

@Johansmm Johansmm requested a review from justinchuby June 4, 2025 19:03
@justinchuby
Copy link
Member

justinchuby commented Jun 5, 2025

I wonder if something like this would make sense:

def insert_nodes(values, nodes, incoming_values, outgoing_values)

or if the new nodes are connected linearly

def insert_nodes(values, nodes)

(we can constrain that the inputs only be the first node, and the outputs only be produced by the last node so that the intention is clear)


# Insert new nodes if there is a graph
graph.extend(new_nodes)
graph.sort()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorting after insertion is not ideal. Is there a way to efficiently find the insertion point?

@justinchuby justinchuby self-assigned this Jun 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants