- Notifications
You must be signed in to change notification settings - Fork0
Adds command line to translate de model into code#49
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
Show all changes
3 commits Select commitHold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletionsCHANGELOGS.rst
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions_unittests/ut_xrun_doc/test_command_lines1.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from contextlib import redirect_stdout | ||
| from io import StringIO | ||
| from onnx import TensorProto | ||
| from onnx.helper import ( | ||
| make_graph, | ||
| make_model, | ||
| make_node, | ||
| make_opsetid, | ||
| make_tensor_value_info, | ||
| ) | ||
| from onnx_array_api.ext_test_case import ExtTestCase | ||
| from onnx_array_api._command_lines_parser import ( | ||
| get_main_parser, | ||
| get_parser_translate, | ||
| main, | ||
| ) | ||
| class TestCommandLines1(ExtTestCase): | ||
| def test_main_parser(self): | ||
| st = StringIO() | ||
| with redirect_stdout(st): | ||
| get_main_parser().print_help() | ||
| text = st.getvalue() | ||
| self.assertIn("translate", text) | ||
| def test_parser_translate(self): | ||
| st = StringIO() | ||
| with redirect_stdout(st): | ||
| get_parser_translate().print_help() | ||
| text = st.getvalue() | ||
| self.assertIn("model", text) | ||
| def test_command_translate(self): | ||
| X = make_tensor_value_info("X", TensorProto.FLOAT, [None, None]) | ||
| Y = make_tensor_value_info("Y", TensorProto.FLOAT, [5, 6]) | ||
| Z = make_tensor_value_info("Z", TensorProto.FLOAT, [None, None]) | ||
| graph = make_graph( | ||
| [ | ||
| make_node("Add", ["X", "Y"], ["res"]), | ||
| make_node("Cos", ["res"], ["Z"]), | ||
| ], | ||
| "g", | ||
| [X, Y], | ||
| [Z], | ||
| ) | ||
| onnx_model = make_model(graph, opset_imports=[make_opsetid("", 18)]) | ||
| with tempfile.TemporaryDirectory() as root: | ||
| model_file = os.path.join(root, "model.onnx") | ||
| with open(model_file, "wb") as f: | ||
| f.write(onnx_model.SerializeToString()) | ||
| args = ["translate", "-m", model_file] | ||
| st = StringIO() | ||
| with redirect_stdout(st): | ||
| main(args) | ||
| code = st.getvalue() | ||
| self.assertIn("model = make_model(", code) | ||
| args = ["translate", "-m", model_file, "-a", "light"] | ||
| st = StringIO() | ||
| with redirect_stdout(st): | ||
| main(args) | ||
| code = st.getvalue() | ||
| self.assertIn("start(opset=", code) | ||
| if __name__ == "__main__": | ||
| unittest.main(verbosity=2) |
4 changes: 4 additions & 0 deletionsonnx_array_api/__main__.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from ._command_lines_parser import main | ||
| if __name__ == "__main__": | ||
| main() |
94 changes: 94 additions & 0 deletionsonnx_array_api/_command_lines_parser.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import sys | ||
| import onnx | ||
| from typing import Any, List, Optional | ||
| from argparse import ArgumentParser | ||
| from textwrap import dedent | ||
| def get_main_parser() -> ArgumentParser: | ||
| parser = ArgumentParser( | ||
| prog="onnx-array-api", | ||
| description="onnx-array-api main command line.", | ||
| epilog="Type 'python -m onnx_array_api <cmd> --help' " | ||
| "to get help for a specific command.", | ||
| ) | ||
| parser.add_argument( | ||
| "cmd", | ||
| choices=["translate"], | ||
| help=dedent( | ||
| """ | ||
| Selects a command. | ||
| 'translate' exports an onnx graph into a piece of code replicating it. | ||
| """ | ||
| ), | ||
| ) | ||
| return parser | ||
| def get_parser_translate() -> ArgumentParser: | ||
| parser = ArgumentParser( | ||
| prog="translate", | ||
| description=dedent( | ||
| """ | ||
| Translates an onnx model into a piece of code to replicate it. | ||
| The result is printed on the standard output. | ||
| """ | ||
| ), | ||
| epilog="This is mostly used to write unit tests without adding " | ||
| "an onnx file to the repository.", | ||
| ) | ||
| parser.add_argument( | ||
| "-m", | ||
| "--model", | ||
| type=str, | ||
| required=True, | ||
| help="onnx model to translate", | ||
| ) | ||
| parser.add_argument( | ||
| "-a", | ||
| "--api", | ||
| choices=["onnx", "light"], | ||
| default="onnx", | ||
| help="API to choose, API from onnx package or light API.", | ||
| ) | ||
| return parser | ||
| def _cmd_translate(argv: List[Any]): | ||
| from .light_api import translate | ||
| parser = get_parser_translate() | ||
| args = parser.parse_args(argv[1:]) | ||
| onx = onnx.load(args.model) | ||
| code = translate(onx, api=args.api) | ||
| print(code) | ||
| def main(argv: Optional[List[Any]] = None): | ||
| fcts = dict(translate=_cmd_translate) | ||
| if argv is None: | ||
| argv = sys.argv[1:] | ||
| if (len(argv) <= 1 and argv[0] not in fcts) or argv[-1] in ("--help", "-h"): | ||
| if len(argv) < 2: | ||
| parser = get_main_parser() | ||
| parser.parse_args(argv) | ||
| else: | ||
| parsers = dict(translate=get_parser_translate) | ||
| cmd = argv[0] | ||
| if cmd not in parsers: | ||
| raise ValueError( | ||
| f"Unknown command {cmd!r}, it should be in {list(sorted(parsers))}." | ||
| ) | ||
| parser = parsers[cmd]() | ||
| parser.parse_args(argv[1:]) | ||
| raise RuntimeError("The programme should have exited before.") | ||
| cmd = argv[0] | ||
| if cmd in fcts: | ||
| fcts[cmd](argv) | ||
| else: | ||
| raise ValueError( | ||
| f"Unknown command {cmd!r}, use --help to get the list of known command." | ||
| ) |
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.