Unify BuilderBase and GraphBuilder: shared call_op with feature flags#2910
Unify BuilderBase and GraphBuilder: shared call_op with feature flags#2910gramalingam wants to merge 2 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2910 +/- ##
==========================================
+ Coverage 72.61% 72.63% +0.01%
==========================================
Files 259 259
Lines 31597 31651 +54
Branches 2973 2980 +7
==========================================
+ Hits 22945 22990 +45
- Misses 7643 7653 +10
+ Partials 1009 1008 -1 ☔ View full report in Codecov by Sentry. |
Rename and restructure: - Rename OpBuilderBase to BuilderBase - Merge op() and _make_node() into single op() method - Promote _create_op to BuilderBase.call_op() as the core node-creation method Feature flags (BuilderFeature enum): - SCHEMA_PARTITION, CAST_INPUTS, CAST_ATTRIBUTES, INFER_SHAPES, CONSTANT_PROPAGATION - FULL = all flags (used by GraphBuilder) - NONE = no extra processing (used by TapeBuilder) GraphBuilder inherits from BuilderBase: - Shares call_op() with feature-gated hooks - Default hook implementations (_get_schema, _partition_inputs_attributes, _cast_inputs, _cast_attributes, _constant_propagation, _infer_shapes) live in BuilderBase - GraphBuilder only overrides: _promote_constant (cached initializers), _generate_node_name, _adapt_outputs (named outputs), _annotate_node (scope metadata), _qualify_value_name (scope prefixing) Constant promotion in BuilderBase: - _input_to_ir_value with scalar-to-tensor promotion and CastLike - _promote_constant hook: base class uses Constant nodes (no name collisions), GraphBuilder uses cached initializers Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
f90ed1f to
6cb4e71
Compare
…27 lint - Remove unused _PYTHON_TYPE_TO_DTYPE from tape_builder.py (builder.py has its own local copy that is actually used) - Remove dead import of _PYTHON_TYPE_TO_DTYPE in builder.py - Add noqa: B027 to _annotate_node (intentional no-op hook, not abstract) Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
d2b25f5 to
d3b52dd
Compare
There was a problem hiding this comment.
Pull request overview
This PR unifies the op-building infrastructure by introducing a shared BuilderBase.call_op() pipeline with feature flags, and refactors GraphBuilder/TapeBuilder and call sites to use the new op(op_type, *args, **kwargs) API shape.
Changes:
- Renames
OpBuilderBase→BuilderBase, addsBuilderFeatureflags, and consolidates node creation intoBuilderBase.call_op()intape_builder.py. - Refactors
GraphBuilderto inherit fromBuilderBaseand implement the storage + graph-specific hooks while using feature-flagged post-processing. - Updates rewriter/optimizer/version-converter call sites and exports (
onnxscript.__init__) to use the new builder names andop()calling convention.
Reviewed changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| onnxscript/version_converter/_version_converter.py | Updates VC context alias/import to BuilderBase. |
| onnxscript/rewriter/rules/common/_remove_optional_bias.py | Updates explicit op construction to new op.op(op_type, *inputs, **attrs) style. |
| onnxscript/rewriter/rules/common/_min_max_to_clip.py | Updates explicit op construction to pass inputs positionally. |
| onnxscript/rewriter/rules/common/_fuse_pad_into_conv.py | Updates explicit op construction to new positional inputs + _domain/_name reserved kwargs. |
| onnxscript/rewriter/rules/common/_fuse_batchnorm.py | Updates explicit op construction to positional inputs + expanded attributes mapping. |
| onnxscript/rewriter/_context.py | Re-exports/aliases updated to BuilderBase + TapeBuilder. |
| onnxscript/rewriter/_context_test.py | Updates tests to validate new op.op(...) calling convention (including **node.attributes). |
| onnxscript/optimizer/_constant_folding.py | Updates optimizer context alias/import to BuilderBase. |
| onnxscript/_internal/tape_builder.py | Introduces BuilderFeature, BuilderBase with shared call_op() and hook-based processing; TapeBuilder updated accordingly. |
| onnxscript/_internal/builder.py | Refactors GraphBuilder to inherit BuilderBase and provide graph-backed implementations/hooks; removes now-duplicated call_op implementation. |
| onnxscript/init.py | Public exports updated to include BuilderBase and BuilderFeature (and drop OpBuilderBase). |
| class GraphBuilder(BuilderBase): | ||
| """Imperative builder for constructing ONNX IR graphs with automatic constant promotion, type casting, and shape inference.""" | ||
|
|
||
| def __init__(self, graph: ir.Graph, *, parent: GraphBuilder | None = None) -> None: | ||
| super().__init__(features=BuilderFeature.FULL) | ||
| self._graph = graph |
ReviewSynthesized from a multi-reviewer pass (correctness, adversarial, readability). In-scope findings only, sorted by severity. �� Critical / Bug1. 2. 🟠 Major3. Silent breakage of the old 4. 5. Missing tests for the new 🟡 Minor6. Name-determinism shift. 7. Recursive 8. 9. Duplicated helpers. 10. ✅ PraiseThe feature-flag pipeline is a good architectural direction — it centralizes schema/cast/inference/storage that was duplicated across builders, and the hook-based extension points are well-chosen. Section dividers in Follow-up suggestions (out of scope)
RecommendationRequest changes, focused on #1 and #2 (real bugs). Then re-discuss #3/#4 (compat policy) and #5 (tests). Rest can land in follow-ups. |
Summary
Extends the
BuilderBase/TapeBuilderinfrastructure (from PR #2905) so thatGraphBuilderinherits fromBuilderBase, sharing a singlecall_op()implementation gated by feature flags.Changes
Class rename and API merge
OpBuilderBase→BuilderBase(no backward-compat alias needed — not yet released)op()and_make_node()methods into a singleop(op_type, *args, **kwargs)ergonomic API_create_optoBuilderBase.call_op()— the single core node-creation methodFeature flags (
BuilderFeatureenum)SCHEMA_PARTITION— partition positional args into inputs vs attributes using op schemaCAST_INPUTS— promote scalars to tensors, match type variablesCAST_ATTRIBUTES— cast attributes using schema info (stub for future)INFER_SHAPES— run shape inference after node creationCONSTANT_PROPAGATION— run constant propagation after node creationFULL= all of the above (used by GraphBuilder)NONE= no extra processing (used by TapeBuilder for rewriter/optimizer/version-converter)GraphBuilder inherits from BuilderBase
GraphBuilder(BuilderBase)withfeatures=BuilderFeature.FULLcall_op()is inherited directly — no override needed_get_schema,_partition_inputs_attributes,_cast_inputs,_cast_attributes) live inBuilderBase_input_to_ir_value— constant promotion with caching_generate_node_name/_adapt_outputs— qualified naming_post_create_node— constant propagation + shape inference_annotate_node— scope metadataExports
BuilderFeatureexported fromonnxscriptpackageTesting