Refactor ir.Function builders to use build_function from onnxscript 0.7.0 - #189
Conversation
….7.0 Replace manual ir.Graph + GraphBuilder + ir.Function construction boilerplate with the new builder.build_function() utility. This eliminates ~25 lines of setup per function and automatically handles initializer lifting to Constant nodes. Updated files: - causal_conv.py: body extracted to trace function - linear_attention.py: body extracted, conditional inputs preserved - packed_multi_head_attention.py: body extracted, raw ir.Node via op._builder._graph.append() for ref_attr_name forwarding - skip_layer_normalization.py: both functions refactored similarly - pyproject.toml: bumped onnxscript requirement to >=0.7.0 Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: G Ramalingam <grama@microsoft.com>
Replace *args unpacking with named parameters and None entries. The function signature is now stable (6 formals) across all update_rule variants. Absent optional inputs (decay, beta) get placeholder formals via build_function; the trace function receives None and branches naturally with 'if decay_v is not None'. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: G Ramalingam <grama@microsoft.com>
- skip_layer_normalization: Replace raw ir.Node with op.LayerNormalization and op.RMSNormalization, passing ir.Attr objects with ref_attr_name directly as kwargs. OpBuilder passes them through to ir.node() correctly. Remove outdated docstring note about OpBuilder limitation. - packed_multi_head_attention: Replace raw ir.Node with op.Attention, passing ir.Attr objects with ref_attr_name for num_heads and scale. - linear_attention: Revert to per-variant input arity. The function is specialized per-model, so only inputs used by the update_rule are declared. This matches call sites (Mamba2/Bamba pass 5 args for 'gated', GatedDeltaNet passes 6 for 'gated_delta'). Shape inference does strict arity checking. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: G Ramalingam <grama@microsoft.com>
The strict arity check is in the Python onnx-shape-inference package (onnx_shape_inference._functions.infer_function_call_output_shapes), not in ONNX's C++ shape inference. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Signed-off-by: G Ramalingam <grama@microsoft.com>
Performance Comparison
|
justinchuby
left a comment
There was a problem hiding this comment.
Thanks! This reminds me we can also update the call sites now.
There was a problem hiding this comment.
Pull request overview
Refactors Mobius’ standard-ONNX ir.Function fallback builders to use onnxscript._internal.builder.build_function() (onnxscript >= 0.7.0), removing manual ir.Graph/GraphBuilder boilerplate while preserving the same function signatures and attribute-forwarding behavior used by InlinePass expansion.
Changes:
- Bump
onnxscriptdependency to>=0.7.0. - Refactor multiple
ir.Functionfactories to define abody(op, ...)trace function and build viabuilder.build_function(...). - Replace prior raw
ir.Nodeconstruction patterns withOpBuildercalls usingir.Attr(..., ref_attr_name=...)where needed.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| pyproject.toml | Updates the minimum onnxscript version to support build_function(). |
| src/mobius/functions/causal_conv.py | Switches to build_function() with a traced body, keeping the same function inputs/outputs and activation attribute. |
| src/mobius/functions/linear_attention.py | Switches to build_function() and preserves per-variant input arity while keeping attribute forwarding. |
| src/mobius/functions/packed_multi_head_attention.py | Switches to build_function() and emits op.Attention(...) with forwarded ir.Attr kwargs. |
| src/mobius/functions/skip_layer_normalization.py | Switches to build_function() and uses op.LayerNormalization/op.RMSNormalization with forwarded epsilon via ir.Attr. |
🏗️ Architecture Diff
No architecture changes detected. ✅ Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
|
The author of this PR, gramalingam, is not an activated member of this organization on Codecov. |
Summary
Replaces manual
ir.Graph+GraphBuilder+ir.Functionconstruction boilerplate with the newbuilder.build_function()utility from onnxscript 0.7.0. This eliminates ~25 lines of setup per function and automatically handles initializer lifting to Constant nodes.Changes
>=0.7.0op.Attention(...)withir.Attrkwargs forref_attr_nameforwarding (eliminates rawir.Nodeconstruction)op.LayerNormalization/op.RMSNormalizationwithir.Attrkwargs forref_attr_name(eliminates rawir.Nodeconstruction and outdated docstring note)Key findings
ir.Attrwithref_attr_nameas kwargs — it flows through toir.node()which acceptsir.AttrinSupportedAttrTypes. This eliminates the need for rawir.Nodeconstruction in all three files that previously required it.onnx-shape-inference(Python package) does a strictlen(f.inputs) != len(node.inputs)check. ONNX's own C++ shape inference (onnx.shape_inference.infer_shapes) accepts trailing optional inputs fine, even in strict mode. A TODO is added to investigate relaxing this upstream.Testing
All 2558 tests pass, 39 skipped (unchanged from baseline).