Skip to content

Decorator for trace_only functions #694

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

Open
justinchuby opened this issue Apr 28, 2023 · 3 comments
Open

Decorator for trace_only functions #694

justinchuby opened this issue Apr 28, 2023 · 3 comments
Assignees
Labels

Comments

@justinchuby
Copy link
Collaborator

justinchuby commented Apr 28, 2023

There is interest in enabling a trace_only function as a general ONNX Script feature. #674 (comment). Currently, we have the script: FunctionType -> OnnxFunction decorator that scripts a python function. Should we have a similar decorator for creating a trace only function?

Option 1

Retain script and create a new trace: FunctionType -> TracedOnnxFunction decorator. This is like torch.jit.trace and torch.jit.script, where the difference is we do symbolic tracing. TracedOnnxFunction is something that is OpLike and has scope information which we can use to dynamically generate functions during graph build.

Examples

@onnxscript.script()
def aten_abs(self: TReal) -> TReal:
    return op.Abs(self)

@onnxscript.trace()
def aten_abs(self: TReal) -> TReal:
    return op.Abs(self)

Option 2

Replace @script with @function, which does scripting by default, and add an option @function(trace=True). This is like tf.function with the jit_compile option (similar only on an api level. tf.function is always tracing I believe).

One of the pros of this option is a more natural representation of an ONNX "function".

Examples

@onnxscript.function()
def aten_abs(self: TReal) -> TReal:
    return op.Abs(self)

@onnxscript.function(trace=True)
def aten_abs(self: TReal) -> TReal:
    return op.Abs(self)

cc @gramalingam @BowenBao @titaiwangms @xadupre @jcwchen @fatcat-z @xiaowuhu @shubhambhokare1

@BowenBao
Copy link
Contributor

Feels like we are only discussing api difference here?

Another key aspect to highlight is that in terms of graph creation. Is it correct to say script is aot compilation while trace is jit compilation, i.e. no onnx graph for trace decorated functions until feeding inputs (tensor can be maybe symbolic, but attributes must be concrete) to it?

@justinchuby
Copy link
Collaborator Author

Another key aspect to highlight is that in terms of graph creation. Is it correct to say script is aot compilation while trace is jit compilation, i.e. no onnx graph for trace decorated functions until feeding inputs (tensor can be maybe symbolic, but attributes must be concrete) to it?

That is how I understand it yes.

@titaiwangms
Copy link
Contributor

Personally, I vote for option2. But I am not sure how much effort/cost/effect would be if we change the main api.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants