Add custom operator names to the script decorator - #2977
Draft
ilia-sokolov wants to merge 2 commits into
Draft
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for specifying a custom ONNX operator name (op_type) when using the @script decorator, while preserving the original Python function name for introspection. This enables defining ONNX functions that must match externally required operator names without renaming the Python symbol.
Changes:
- Added keyword-only
op_type: str | Noneto@script, with validation for type and non-empty strings. - Applied
op_typeconsistently by setting the IR function name and IR graph name prior to constructing theOnnxFunction. - Updated nested-function collection/deduplication to key by full ONNX identifier and raise on conflicting definitions; added comprehensive tests for the new behavior.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| onnxscript/_internal/main.py | Adds op_type to @script and applies it to the generated IR before wrapping as an OnnxFunction. |
| onnxscript/_internal/main_test.py | Adds tests covering custom operator naming, identifier conflict behavior, and input validation. |
| onnxscript/_internal/irbuilder.py | Changes called-function tracking to use full operator identifiers and to error on conflicting definitions. |
| @@ -0,0 +1,155 @@ | |||
| # Copyright (c) Microsoft Corporation. | |||
| @@ -0,0 +1,155 @@ | |||
| # Copyright (c) Microsoft Corporation. | |||
|
|
||
| import onnx | ||
|
|
||
| import onnxscript |
| invalid_values = ("", " ", 42) | ||
|
|
||
| for value in invalid_values: | ||
| with self.subTest(value=value): |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #2977 +/- ##
==========================================
+ Coverage 72.63% 72.67% +0.03%
==========================================
Files 265 266 +1
Lines 32204 32320 +116
Branches 3041 3048 +7
==========================================
+ Hits 23391 23488 +97
- Misses 7779 7797 +18
- Partials 1034 1035 +1 ☔ View full report in Codecov by Harness. |
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
op_typeargument to@scriptFunctionProto, and nested call nodes(domain, name, overload)and report conflicting definitions instead of silently dropping oneWhy
Python function names do not always match the operator name required by an existing runtime or interoperability contract. This enables:
while keeping
custom_op.__name__ == "custom_op".Validation
python -m pytest onnxscript/_internal/main_test.py onnxscript/_internal/values_test.py onnxscript/_internal/builder_test.py -qgit diff --checkFixes #1985