Skip to content

Add custom operator names to the script decorator - #2977

Draft
ilia-sokolov wants to merge 2 commits into
microsoft:mainfrom
ilia-sokolov:contrib/1985-custom-op-name
Draft

Add custom operator names to the script decorator#2977
ilia-sokolov wants to merge 2 commits into
microsoft:mainfrom
ilia-sokolov:contrib/1985-custom-op-name

Conversation

@ilia-sokolov

Copy link
Copy Markdown

Summary

  • add a keyword-only op_type argument to @script
  • apply the custom name consistently to the ONNX callable, signature, IR graph, FunctionProto, and nested call nodes
  • preserve the original Python function name for introspection
  • track called functions by their full ONNX identifier (domain, name, overload) and report conflicting definitions instead of silently dropping one

Why

Python function names do not always match the operator name required by an existing runtime or interoperability contract. This enables:

@script(op_type="MY_NEW_NAME_OP")
def custom_op(x):
    ...

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 -q
  • 102 tests and 6 subtests passed
  • git diff --check

Fixes #1985

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 | None to @script, with validation for type and non-empty strings.
  • Applied op_type consistently by setting the IR function name and IR graph name prior to constructing the OnnxFunction.
  • 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.
Comment thread onnxscript/_internal/main_test.py Outdated

import onnx

import onnxscript
Comment thread onnxscript/_internal/main_test.py Outdated
invalid_values = ("", " ", 42)

for value in invalid_values:
with self.subTest(value=value):
@codecov

codecov Bot commented Jul 26, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.16667% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.67%. Comparing base (9ef0aac) to head (f7d14d7).

Files with missing lines Patch % Lines
onnxscript/_internal/main_test.py 81.90% 18 Missing and 1 partial ⚠️
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.
📢 Have feedback on the report? Share it here.

Copilot AI review requested due to automatic review settings July 27, 2026 06:02

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 3 out of 3 changed files in this pull request and generated no new comments.

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.

Define a custom op with a custom name

3 participants