Skip to content

Add an option to not inline a function when building the graph#2851

Open
justinchuby wants to merge 4 commits intomainfrom
justinchu/function-inline
Open

Add an option to not inline a function when building the graph#2851
justinchuby wants to merge 4 commits intomainfrom
justinchu/function-inline

Conversation

@justinchuby
Copy link
Collaborator

This pull request introduces a new mechanism for function calls in the ONNXScript builder, allowing users to choose between inlining a function's body into the graph or creating a single function call node. It also adds tracking and registration of called functions, exposes the registered functions via a property, and provides comprehensive tests to ensure correctness of the new feature.

Function call mechanism enhancements:

  • Added an _inline option to the call method in GraphBuilder and OpBuilder, allowing users to either inline the function's body (default) or create a single function call node and register the function. When _inline=False, the function is added to the builder's functions dictionary and a single node is created in the graph; when _inline=True, the function is inlined and not registered. (onnxscript/_internal/builder.py [1] [2] [3] [4]
  • Introduced a functions property to both GraphBuilder and OpBuilder, exposing the dictionary of registered functions for inspection and downstream use. (onnxscript/_internal/builder.py [1] [2] [3]

Testing and validation:

  • Added extensive unit tests for the new _inline option, verifying function registration, node creation, output renaming, prefix handling, and the difference in node count between inlining and non-inlining. (onnxscript/_internal/builder_test.py onnxscript/_internal/builder_test.pyR851-R1001)

API improvements:

Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Signed-off-by: Justin Chu <justinchuby@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Adds support in the internal GraphBuilder to optionally not inline an ONNXScript function call, emitting a single function-call node and tracking the referenced function definitions for later export/attachment.

Changes:

  • Add Op.domain convenience property to expose an op’s opset domain.
  • Extend GraphBuilder.call/OpBuilder.call with _inline flag; when _inline=False, emit a single call node and register the callee in GraphBuilder.functions.
  • Add unit tests covering _inline=False behavior (single node emission, output renaming, prefix handling, function registration).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 6 comments.

File Description
onnxscript/_internal/values.py Adds Op.domain property forwarding to the bound opset domain.
onnxscript/_internal/builder.py Introduces function registry and _inline option to either inline or emit a function-call node.
onnxscript/_internal/builder_test.py Adds tests verifying non-inlined call behavior and registration.

Comment on lines +594 to +606
node = ir.node(
op_type=function.name,
inputs=args,
attributes=kwargs or None,
outputs=[
ir.Value(name=output_renaming[output.name]) for output in graph.outputs
],
domain=function.domain,
name=self._qualify_node_name(function.name),
)
outputs = node.outputs
self.add_node(node)
self._functions[function.identifier] = function
Comment on lines +594 to +603
node = ir.node(
op_type=function.name,
inputs=args,
attributes=kwargs or None,
outputs=[
ir.Value(name=output_renaming[output.name]) for output in graph.outputs
],
domain=function.domain,
name=self._qualify_node_name(function.name),
)
Comment on lines +598 to +604
outputs=[
ir.Value(name=output_renaming[output.name]) for output in graph.outputs
],
domain=function.domain,
name=self._qualify_node_name(function.name),
)
outputs = node.outputs
Comment on lines +734 to +735
called as a separate node. When False, the function will be added
to the ``.functions`` dictionary. Defaults to True.

# The function should be registered
self.assertEqual(len(op.builder.functions), 1)
registered = next(iter(op.builder.functions.values()))
@codecov
Copy link

codecov bot commented Mar 13, 2026

Codecov Report

❌ Patch coverage is 82.72727% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 71.90%. Comparing base (4c4f7a0) to head (72639b1).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnxscript/_internal/builder_test.py 81.60% 16 Missing ⚠️
onnxscript/_internal/builder.py 90.00% 1 Missing and 1 partial ⚠️
onnxscript/_internal/values.py 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2851      +/-   ##
==========================================
+ Coverage   71.86%   71.90%   +0.03%     
==========================================
  Files         239      239              
  Lines       29139    29241     +102     
  Branches     2875     2876       +1     
==========================================
+ Hits        20942    21026      +84     
- Misses       7219     7237      +18     
  Partials      978      978              

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

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
*args,
_outputs: Sequence[str] | None = None,
_prefix: str = "",
_inline: bool = True,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Should we enforce that _prefix is "" if _inline is False?

@gramalingam
Copy link
Collaborator

Not sure if it is better to have a separate function or an option (as in this PR). For now, this seems fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Development

Successfully merging this pull request may close these issues.

3 participants