Implement a ONNX to ONNX Script code generator based on libcst #873
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
[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: