Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define a custom op with a custom name #1985

Open
Johansmm opened this issue Dec 26, 2024 · 3 comments
Open

Define a custom op with a custom name #1985

Johansmm opened this issue Dec 26, 2024 · 3 comments
Labels
contribution welcome We welcome code contributions for this enhancement New feature or request
Milestone

Comments

@Johansmm
Copy link

I would like to know if it is possible to define a custom operator with a different name that its function name.

Example:

import onnx

# We use ONNX opset 15 to define the function below.
from onnxscript import FLOAT, script
from onnxscript import opset15 as op


@script(op_type="MY_NEW_NAME_OP")
def custom_op(X):
    return 5*X


def sample_model(X: FLOAT[64, 128]) -> FLOAT[64, 128]:
    return custom_op(X)


# onnx_model is an in-memory ModelProto
onnx_model = sample_model.to_model_proto()

# Check node op_type
assert model.graph.node[0].op_type == "MY_NEW_NAME_OP"
@justinchuby justinchuby added the enhancement New feature or request label Dec 27, 2024
@justinchuby
Copy link
Collaborator

Could you share your usage? When would this be useful?

@justinchuby justinchuby added the contribution welcome We welcome code contributions for this label Dec 30, 2024
@Johansmm
Copy link
Author

Johansmm commented Jan 6, 2025

My use case can be explained with the gelu function. As a rule, function names in python are usually written in snake or camel cases, but ONNX legacy operators are capital case. That is why if I defined gelu function with onnscript :

@script()
def gelu(X):
    ...

gelu.to_function_proto() method should map a ONNX function named 'gelu' and not 'Gelu'.

It is in this kind of cases that I think it is convenient to have the option to serialize the operator with a different name than the python function.

@justinchuby
Copy link
Collaborator

justinchuby commented Jan 6, 2025

Makes sense. Thanks! cc @gramalingam

@justinchuby justinchuby added this to the 0.1 milestone Jan 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
contribution welcome We welcome code contributions for this enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants