Skip to content

Auto OpSchema for trace_only functions | feat(op_schema) #674

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

Merged
merged 94 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
94 commits
Select commit Hold shift + click to select a range
c6505cf
Move version_utils to _internal | chore
justinchuby Apr 12, 2023
268ad8f
Merge `attrs` and `attr_protos` in `IRFunction` | chore(irbuilder)
justinchuby Apr 12, 2023
327a7d6
Auto generate OpSchema for functions | feat
justinchuby Apr 12, 2023
0883d6f
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
95c4ba6
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
962c13b
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
a353d73
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
821821c
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
fa16ca5
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 12, 2023
6b9106b
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
46fa00f
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
bbe8e7e
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
43345e2
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
26d5caa
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
f835d9b
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
b3a035d
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
e953774
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
03cc7f4
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
7fda2d1
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
7321b22
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
b760abc
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 13, 2023
efc7708
Update base for Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 14, 2023
606be97
Update on "Auto generate OpSchema for functions | feat"
justinchuby Apr 14, 2023
67a8ee0
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 14, 2023
a3f9b50
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 14, 2023
1d4a0b4
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 14, 2023
08a27fa
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 14, 2023
2d158ac
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 17, 2023
28b4a48
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 17, 2023
622b688
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 18, 2023
19f9484
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 18, 2023
31bb69c
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 18, 2023
b61543a
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 18, 2023
2c6be92
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 18, 2023
6cfa67c
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 18, 2023
a9a0845
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 20, 2023
0502482
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 20, 2023
79d3605
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 20, 2023
ad57790
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 20, 2023
1439aaf
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 20, 2023
f2455dc
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 20, 2023
ea41c8f
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 21, 2023
a2fde87
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 21, 2023
138c2ed
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 22, 2023
b334880
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 22, 2023
90208e1
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 22, 2023
cef03af
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 22, 2023
2d9627e
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 22, 2023
ed79fce
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 22, 2023
8f5f7ba
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 22, 2023
6376c93
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 22, 2023
49d8d0e
Update base for Update on "Auto generate OpSchema for functions | fea…
justinchuby Apr 25, 2023
dd80bff
Update on "Auto generate OpSchema for functions | feat(op_schema)"
justinchuby Apr 25, 2023
b1c2c8f
Auto OpSchema for trace_only functions | feat(torchlib)
justinchuby Apr 25, 2023
1ffa271
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
70499c2
Update on "Auto OpSchema for trace_only functions | feat(torchlib)"
justinchuby Apr 27, 2023
ddfadb7
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
a216686
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 27, 2023
732b8ed
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
472938a
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 27, 2023
e4b64dd
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
44b0e2b
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 27, 2023
e5cc885
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
fef104b
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 27, 2023
9a7d093
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 27, 2023
63ef23d
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 27, 2023
86e94b2
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
fe6ee4b
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
ac947d6
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
b097c1a
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
1d27554
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
8abbf32
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
2de051c
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
c45f86d
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
4b0c0e8
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
aa1f1f1
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
eebda4c
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
c6bb7f9
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
97215d1
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
0d99f0a
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
e402626
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
035391c
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
187bf66
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
4bc0463
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
69a7c50
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
99bd508
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
15bb55b
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
3888ad5
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
16f3922
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
d4d6dcd
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
3e9595a
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
bc10086
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
d3b2bb9
Update base for Update on "Auto OpSchema for trace_only functions | f…
justinchuby Apr 28, 2023
01a423b
Update on "Auto OpSchema for trace_only functions | feat(op_schema)"
justinchuby Apr 28, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions onnxscript/_internal/ast_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Utilities for working with Python ASTs."""
from __future__ import annotations

import ast
import inspect
import textwrap
import types


def get_src_and_ast(f: types.FunctionType) -> tuple[str, ast.FunctionDef]:
try:
src = inspect.getsource(f)
except OSError as e:
raise RuntimeError(
f"Decorator script does not work on dynamically "
f"compiled function {f.__name__}."
) from e
src = textwrap.dedent(src)
top_level_ast = ast.parse(src)
assert isinstance(top_level_ast, ast.Module)
assert len(top_level_ast.body) == 1
f_ast = top_level_ast.body[0]
assert isinstance(f_ast, ast.FunctionDef)
return src, f_ast
7 changes: 4 additions & 3 deletions onnxscript/analysis_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
import unittest
from typing import Any

from onnxscript import analysis, main
from onnxscript import analysis
from onnxscript._internal import ast_utils
from onnxscript.onnx_opset import opset15 as op
from onnxscript.sourceinfo import formatter

Expand All @@ -27,7 +28,7 @@ def generic_visit(self, node):

class TestLivenessAnalysis(unittest.TestCase):
def analyze(self, fun):
source, parse_tree = main.get_src_and_ast(fun)
source, parse_tree = ast_utils.get_src_and_ast(fun)
analysis.do_liveness_analysis(parse_tree, formatter(source))
visitor = AnalysisResultsVisitor()
visitor.visit(parse_tree)
Expand Down Expand Up @@ -97,7 +98,7 @@ def while_eg(x):

class TestExposedUses(unittest.TestCase):
def assertUses(self, f, expected):
source, parse_tree = main.get_src_and_ast(f)
source, parse_tree = ast_utils.get_src_and_ast(f)
result = analysis.exposed_uses(parse_tree.body, formatter(source))
self.assertEqual(result, set(expected))

Expand Down
14 changes: 9 additions & 5 deletions onnxscript/function_libs/torch_lib/registration.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def torch_op(
registry: Optional[Registry] = None,
trace_only: bool = False,
private: bool = False,
) -> Callable[[Callable[..., Any]], onnxscript.OnnxFunction | Callable[..., Any]]:
) -> Callable[[FunctionType], onnxscript.OnnxFunction | onnxscript.values.TracedOnnxFunction]:
"""Register a torch op.

Args:
Expand All @@ -81,12 +81,16 @@ def torch_op(
if registry is None:
registry = default_registry

def wrapper(func: Callable[..., Any]) -> onnxscript.OnnxFunction | Callable[..., Any]:
def wrapper(
func: FunctionType,
) -> onnxscript.OnnxFunction | onnxscript.values.TracedOnnxFunction:
# Compile the function
custom_opset = onnxscript.values.Opset(domain="onnxscript.atenlib", version=1)

processed_func: onnxscript.OnnxFunction | onnxscript.values.TracedOnnxFunction
if trace_only:
processed_func = func
processed_func = onnxscript.values.TracedOnnxFunction(custom_opset, func)
else:
# Compile the function
custom_opset = onnxscript.values.Opset(domain="onnxscript.atenlib", version=1)
assert isinstance(func, FunctionType)
processed_func = onnxscript.script(opset=custom_opset)(func)

Expand Down
4 changes: 2 additions & 2 deletions onnxscript/irbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def __str__(self):

args = _format(self.args, "(", ", ", ")", _opt_var_to_str)
domain = self.callee.opset.domain
opname = self.callee.opname
opname = self.callee.name
callee = f"{domain}.{opname}" if (domain != "") else opname
return f"{lhs} = {callee} {attrs}{args}"

Expand All @@ -212,7 +212,7 @@ def debug_print(self):

def to_node_proto(self, node_name: str) -> onnx.NodeProto:
n = helper.make_node(
self.callee.opname,
self.callee.name,
[_opt_var_to_str(x) for x in self.args],
[str(x) for x in self.result],
domain=self.callee.opset.domain,
Expand Down
26 changes: 2 additions & 24 deletions onnxscript/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,14 @@
import ast
import inspect
import sys
import textwrap
import types
from typing import Any, Callable, Optional, Sequence, cast

import onnx.helper

import onnxscript
from onnxscript import converter, irbuilder, values


def get_src_and_ast(f: types.FunctionType) -> tuple[str, ast.FunctionDef]:
try:
src = inspect.getsource(f)
except OSError as e:
raise RuntimeError(
f"Decorator script does not work on dynamically "
f"compiled function {f.__name__}."
) from e
src = textwrap.dedent(src)
top_level_ast = ast.parse(src)
assert isinstance(top_level_ast, ast.Module)
assert len(top_level_ast.body) == 1
f_ast = top_level_ast.body[0]
assert isinstance(f_ast, ast.FunctionDef)
return src, f_ast


def get_ast(f: types.FunctionType) -> ast.FunctionDef:
_, f_ast = get_src_and_ast(f)
return f_ast
from onnxscript._internal import ast_utils


def script_check(
Expand Down Expand Up @@ -104,7 +82,7 @@ def transform(f: types.FunctionType) -> onnxscript.OnnxFunction:
if not inspect.isfunction(f):
raise TypeError("The ONNXScript decorator should be applied to functions only.")

src, f_ast = get_src_and_ast(f) # pylint: disable=redefined-outer-name
src, f_ast = ast_utils.get_src_and_ast(f)
# The script should be compiled using the globals/locals at the definition site.
# This allows the script to reference names defined outside the script,
# which is used for a few different purposes.
Expand Down
13 changes: 13 additions & 0 deletions onnxscript/tests/function_libs/torch_lib/ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def test_script_function_has_op_schema(self, _, func_with_wrangler):
self.assertIsNotNone(schema)
self.assertEqual(schema.name, func.name)

@parameterized.parameterized.expand(
list(ops_test_data.OPINFO_FUNCTION_MAPPING_TRACE_ONLY.items())
)
@unittest.skipIf(
version_utils.onnx_older_than("1.15"),
"OpSchema is not writable before ONNX 1.15",
)
def test_trace_only_function_has_op_schema(self, _, func_with_wrangler):
func, _ = _split_function_and_wrangler(func_with_wrangler)
schema = func.opschema
self.assertIsNotNone(schema)
self.assertEqual(schema.name, func.name)


def run_test_output_match(
test_suite: unittest.TestCase,
Expand Down
Loading