Add GraphBuilder.subgraph() and TensorType.to_ir() for control-flow ops#2824
Merged
gramalingam merged 4 commits intomainfrom Feb 25, 2026
Merged
Add GraphBuilder.subgraph() and TensorType.to_ir() for control-flow ops#2824gramalingam merged 4 commits intomainfrom
gramalingam merged 4 commits intomainfrom
Conversation
- Add TensorType.to_ir() classmethod in onnx_types.py that converts tensor-type annotations (e.g. FLOAT[1024], FLOAT['M','N'], FLOAT[...]) to ir.TypeAndShape. - Add GraphBuilder.subgraph(trace_fn, input_types, output_types, *, name) method in builder.py. Builds an ir.Graph suitable for use as a graph-valued attribute to control-flow ops like Scan, Loop, and If. Opset version is inherited from the parent GraphBuilder. Accepts both ir.TypeAndShape and TensorType subclasses as type specs via the TypeSpec / _resolve_type_spec helpers. - Add TensorTypeToIrTest and BuildSubgraphTest test classes in builder_test.py (13 new tests). - Document the new feature in docs/tutorial/builder/graph_builder.md with a 'Building Subgraphs for Control-Flow Ops' section including a cumulative-sum Scan example. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
…attr Replace the duck-typed hasattr(spec, 'to_ir') check with an explicit isinstance/issubclass check against TensorType. This makes the accepted types clear and produces a better error message for invalid inputs. TensorType is imported lazily inside the function to avoid a potential circular import (onnxscript.__init__ imports onnx_types before builder, but a top-level import in builder.py could break if builder were ever imported directly first). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Tests for TensorType.to_ir() belong alongside onnx_types.py, not in the builder test module. Remove the now-unused INT64 import from builder_test.py. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2824 +/- ##
==========================================
+ Coverage 71.66% 71.78% +0.11%
==========================================
Files 238 239 +1
Lines 28864 28989 +125
Branches 2849 2859 +10
==========================================
+ Hits 20686 20809 +123
- Misses 7207 7209 +2
Partials 971 971 ☔ View full report in Codecov by Sentry. |
Collaborator
|
Is there an option to not specify input_types and output_types? I don't think they are required for sub-graphs, right? |
Collaborator
Author
Yes, that would be useful. I thought about it, but decided to leave it as strict for now. Will get back to it later. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Adds utilities to simplify building ONNX control-flow ops (Scan, Loop, If) that use graph-valued attributes.
New: TensorType.to_ir()
Converts tensor-type annotations to ir.TypeAndShape, enabling the convenient FLOAT[1024] / FLOAT['M', 'N'] /
FLOAT[...] notation wherever a type spec is needed.
New: GraphBuilder.subgraph()
Builds an ir.Graph suitable for use as a graph-valued attribute. The body is defined by a trace function fn(op,
*inputs) — the same imperative style as the outer graph. The opset version is inherited from the parent GraphBuilder
.
Files changed: