Skip to content

Comments

Allow GraphBuilder to call script functions#2820

Open
gramalingam wants to merge 14 commits intomainfrom
rama/builder2
Open

Allow GraphBuilder to call script functions#2820
gramalingam wants to merge 14 commits intomainfrom
rama/builder2

Conversation

@gramalingam
Copy link
Collaborator

  • This extension allows calls to scripted functions within traced (GraphBuilder) calls.
  • As part of this extension, allow the use of OpBuilder within scripted function
  • Also fix the default value naming strategy to include node number to ensure uniqueness (in common cases).

TODO later: Consider integration of standard execution of script functions with execution of OpBuilder calls, as well as other extensions within script-mode to ensure both modes are seamless and uniform

Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Copy link
Contributor

@github-advanced-security github-advanced-security bot left a comment

Choose a reason for hiding this comment

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

lintrunner found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.

@codecov
Copy link

codecov bot commented Feb 20, 2026

Codecov Report

❌ Patch coverage is 79.51807% with 34 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.80%. Comparing base (7c7f760) to head (85418f9).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnxscript/_internal/builder_test.py 82.29% 17 Missing ⚠️
onnxscript/_internal/_inliner.py 63.15% 5 Missing and 2 partials ⚠️
onnxscript/_internal/builder.py 87.50% 2 Missing and 3 partials ⚠️
onnxscript/_internal/converter.py 54.54% 3 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2820      +/-   ##
==========================================
+ Coverage   70.76%   70.80%   +0.04%     
==========================================
  Files         231      232       +1     
  Lines       27667    27818     +151     
  Branches     2775     2798      +23     
==========================================
+ Hits        19579    19697     +118     
- Misses       7127     7154      +27     
- Partials      961      967       +6     

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

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

This pull request enables GraphBuilder/OpBuilder to call @script-decorated ONNXScript functions, allowing seamless composition of imperative (builder) and declarative (script) code. The PR also updates the default output naming strategy to include node numbers for better uniqueness.

Changes:

  • Added call() method to GraphBuilder and OpBuilder that inlines script functions into the builder's graph
  • Modified converter to accept OpBuilder as a default_opset parameter, converting it to Opset internally
  • Updated default output naming from {op_type}_output to {op_type}_n{count}_output for uniqueness
  • Created new _inliner.py module with function instantiation logic

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 10 comments.

Show a summary per file
File Description
onnxscript/_internal/builder.py Added call() methods and domain/version properties to OpBuilder; updated naming strategy
onnxscript/_internal/converter.py Added support for OpBuilder as default_opset with automatic conversion to Opset
onnxscript/_internal/_inliner.py New module providing function instantiation/inlining capabilities
onnxscript/_internal/builder_test.py Comprehensive tests for call() functionality with various options (_outputs, _prefix)
docs/tutorial/builder/graph_builder.md Documentation for calling script functions from OpBuilder

gramalingam and others added 5 commits February 20, 2026 16:58
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Co-authored-by: Justin Chu <justinchuby@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
Signed-off-by: Ganesan Ramalingam <grama@microsoft.com>
@gramalingam gramalingam enabled auto-merge (squash) February 21, 2026 01:20
@@ -0,0 +1,58 @@
# Copyright (c) Microsoft Corporation.

Check warning

Code scanning / lintrunner

RUFF/format Warning

Run lintrunner -a to apply this patch.
@@ -0,0 +1,58 @@
# Copyright (c) Microsoft Corporation.

Check warning

Code scanning / lintrunner

RUFF-FORMAT/format Warning

Run lintrunner -a to apply this patch.
nodes = [cloner.clone_node(n) for n in function]
outputs = [value_map.get(v) for v in function.outputs]
return nodes, outputs

Check warning

Code scanning / lintrunner

RUFF/W391 Warning

f"Too many inputs: got {len(inputs)}, "
f"but function has {len(formal_inputs)} parameters."
)
value_map: dict[ir.Value, ir.Value | None] = {

Check notice

Code scanning / lintrunner

PYLINT/R1721 Note

Unnecessary use of a comprehension, use dict(zip(formal_inputs, inputs)) instead. (unnecessary-comprehension)
See unnecessary-comprehension. To disable, use # pylint: disable=unnecessary-comprehension
f"Too many inputs: got {len(inputs)}, "
f"but function has {len(formal_inputs)} parameters."
)
value_map: dict[ir.Value, ir.Value | None] = {

Check notice

Code scanning / lintrunner

RUFF/C416 Note

Unnecessary dict comprehension (rewrite using dict()).
See https://docs.astral.sh/ruff/rules/unnecessary-comprehension
else:
names = [
f"{op_type}_output{i}" if op_type else f"output{i}" for i in range(outputs)
f"{op_type}_n{count}_output{i}" if op_type else f"n{count}_output{i}" for i in range(outputs)
Copy link
Collaborator

Choose a reason for hiding this comment

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

There may be some conflicts: I updated the naming patterns in #2819

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

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants