-
Notifications
You must be signed in to change notification settings - Fork 59
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
Implement a ONNX to ONNX Script code generator based on libcst #873
base: main
Are you sure you want to change the base?
Conversation
- Adds some general codegen utilities based on libcst - Implements an ONNX to ONNX Script generator: the base converter produces ONNX Script that is very 1:1 with the structure of ONNX, and transformers are implemented to raise the generated code to more idiomatic Python that ONNX Script supports; this commit provides support for raising to Python binary operators and raising Constant/make_tensor to supported Python constants; more transformers need to be implemented, but this commit can be used as a guide. - Adds a new top-level command line interface, allowing the code generator to be invoked: python -m onnxscript convert model.onnx
❌ 27 Tests Failed:
View the top 3 failed tests by shortest run time
To view more test analytics, go to the Test Analytics Dashboard |
|
||
numpy_tensor = onnx.numpy_helper.to_array(tensor_proto) | ||
|
||
return cst.Call( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is one example where verbosity makes this harder to read than the existing template-based mechanism. Do you think it might make sense to judiciously mix parsed strings, with formatted strings, with cst constructors to get the best of both worlds?
@abock please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.
Contributor License AgreementContribution License AgreementThis Contribution License Agreement (“Agreement”) is agreed to by the party signing below (“You”),
|
qualname: str, | ||
source: cstmeta.QualifiedNameSource | None = None, | ||
) -> bool: | ||
... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
qualname: re.Pattern[str], | ||
source: cstmeta.QualifiedNameSource | None = None, | ||
) -> re.Match[str] | None: | ||
... |
Check notice
Code scanning / CodeQL
Statement has no effect Note
from __future__ import annotations | ||
|
||
import argparse | ||
from pathlib import Path |
Check warning
Code scanning / lintrunner
RUFF/TID251 Warning
See https://docs.astral.sh/ruff/rules/banned-api
import os | ||
from collections import defaultdict | ||
from dataclasses import dataclass | ||
from pathlib import Path |
Check warning
Code scanning / lintrunner
RUFF/TID251 Warning
See https://docs.astral.sh/ruff/rules/banned-api
from pathlib import Path | ||
from typing import Final, Protocol, Sequence, runtime_checkable | ||
|
||
import libcst as cst |
Check failure
Code scanning / lintrunner
MYPY/import-not-found Error
from typing import Final, Protocol, Sequence, runtime_checkable | ||
|
||
import libcst as cst | ||
import libcst.matchers as cstm |
Check failure
Code scanning / lintrunner
MYPY/import-not-found Error
|
||
import libcst as cst | ||
import libcst.matchers as cstm | ||
import libcst.metadata as cstmeta |
Check failure
Code scanning / lintrunner
MYPY/import-not-found Error
name: str | ||
version: int = 0 | ||
|
||
def __eq__(self, __value: object) -> bool: |
Check warning
Code scanning / lintrunner
RUFF/PYI063 Warning
See https://docs.astral.sh/ruff/rules/pep484-style-positional-only-parameter
def translate_graph_proto( | ||
self, | ||
function_proto: onnx.GraphProto, | ||
func_type: Literal["graph"] | Literal["script"] = "graph", |
Check warning
Code scanning / lintrunner
RUFF/PYI030 Warning
See https://docs.astral.sh/ruff/rules/unnecessary-literal-union
def __make_function( | ||
self, | ||
proto: onnx.GraphProto | onnx.FunctionProto, | ||
func_type: Literal["graph"] | Literal["script"], |
Check warning
Code scanning / lintrunner
RUFF/PYI030 Warning
See https://docs.astral.sh/ruff/rules/unnecessary-literal-union
|
||
for node in proto.node: | ||
for stmt in self.translate_node_proto(node): | ||
body.append(stmt) |
Check warning
Code scanning / lintrunner
RUFF/PERF402 Warning
See https://docs.astral.sh/ruff/rules/manual-list-copy
return constant_op_call | ||
vals_expr = vals_expr.elements[0].value | ||
else: | ||
kwarg += "s" |
Check failure
Code scanning / lintrunner
PYLINT/E0602 Error
See undefined-variable. To disable, use # pylint: disable=undefined-variable
[WIP] Proposal to use libcst for code generation
This PR is somewhat experimental, but goes far enough to illustrate using libcst for code generation of ONNX to ONNX Script. It is nearly as complete as the existing template-based
onnxscript.proto2python
, and lays the groundwork for implementing more "raising" passes to produce idiomatic Python code.I implemented this to learn more about libcst and currently I am heavily leaning on wanting to take a full dependency on libcst. I see adoption happening in three phases:
Continue with the work in this PR by implementing more transformers to produce idiomatic Python ONNX Script from ONNX. This keeps the libcst dependency scoped to just this feature.
Port the existing
opgen
generator to use libcst. Again, the libcst dependency would be scoped to just this code generator.Finally, let's port our existing Python AST support that uses the builtin
ast
module tolibcst
. I suspect if we do this, lots of utilities that are necessary for (1) and (2) will be highly useful here. More importantly, libcst provides scope analysis and qualified name resolving, which is the basis for doing real semantic analysis work with CSTTransformer.Also note, that if we implement various passes as CSTTransformers, we can begin to define a very nice UX in IDEs for "lightbulb fixes". For example any pass we may apply as part of builtin code generation for ONNX to ONNX Script (phase 1), we can surface to the IDEs to upgrade user code to more idiomatic Python. PyTorch is also beginning to use libcst for providing fixes to PyTorch code as well.
Depending on libcst
libcst itself does not carry many dependencies and is relatively small (about 10MB). Comparatively, onnx itself is rather large (112MB). Thus, I am not too concerned about the size of the libcst dependency, considering it would become central to ONNX Script across the three identified phases.
Dependency Cost
python3.10 -m venv venv --upgrade-deps
pip install onnx
pip install libcst
Current capability of this PR:
Adds some general codegen utilities based on libcst
Implements an ONNX to ONNX Script generator: the base converter produces ONNX Script that is very 1:1 with the structure of ONNX, and transformers are implemented to raise the generated code to more idiomatic Python that ONNX Script supports; this commit provides support for raising to Python binary operators and raising Constant/make_tensor to supported Python constants; more transformers need to be implemented, but this commit can be used as a guide.
Adds a new top-level command line interface, allowing the code generator to be invoked: