Skip to content
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

[CLI] Add example command to CLI #1010

Merged
merged 24 commits into from
May 22, 2020
Merged

[CLI] Add example command to CLI #1010

merged 24 commits into from
May 22, 2020

Conversation

rexwangcc
Copy link
Collaborator

@rexwangcc rexwangcc commented May 18, 2020

Related issue = #775

Implementation plan and thoughts

  • First iteration to make something like ti example mpm88 work, with dead-simple invalid argument handling (ti example fake). Leveraging the existing condition branches for simplicity.
  • Update dev_install docs as I setup development environment. (Please let me know if this makes the PR too convoluted, I could remove and cherry-pick it from a separate [doc] PR)
  • [ ] Instead of using the long-winded if-elif-else, move the example subcommand to a more Pythonic argparse based CLI. This will need insights from the core devs since it requires larger refactor, and might be ideal to implement in a separate PR. out of scope
  • Also could be moved to a spearate task, ideally the examples files could be registered someway, instead of being called by os.system(), which is error-prone. I ended up using runpy with absolute paths.
  • [ ] Add tests for CLI for example command. out of scope
  • [ ] The order of returned set of available example names when it errors out is not guaranteed, this is more of an UX question than a technical problem. I'll leave it as it is for now. out of scope
  • ti example is now runnable from anywhere on the system, as long as the script is registered.

Yak shaving

  • Fixed a missing shutil import for convert subcommand.

Testing commands

ti example 
ti example fake-example
ti example bitmasked
ti example cornell_box
ti example euler
ti example fractal
ti example game_of_life
ti example image_io
ti example keyboard
ti example laplace
ti example lists
ti example mgpcg
ti example mgpcg_advanced
ti example minimal
ti example minimization_trivial
ti example mlp
ti example mnist
ti example mpm128
ti example mpm88
ti example mpm99
ti example mpm_lagrangian_forces
ti example nbody_oscillator
ti example particle_renderer
ti example pbf2d
ti example pytorch_tensor_ad
ti example pytorch_tensor_io
ti example quadtree
ti example regression
ti example renderer_utils
ti example sdf2d
ti example sdf_renderer
ti example simple_autodiff
ti example stable_fluid
ti example taichi_logo
ti example taichi_sparse
ti example waterwave

[Click here for the format server]

docs/dev_install.rst Outdated Show resolved Hide resolved
@archibate
Copy link
Collaborator

Also could be moved to a spearate task, ideally the examples files could be registered someway, instead of being called by os.system(), which is error-prone.

To run a file in python without os.system(), you may want to try this:

exec(f'import examples.{name}')

(see Suite.__init__ in benchmark/run.py)

@rexwangcc
Copy link
Collaborator Author

rexwangcc commented May 18, 2020

@archibate Thanks for your insights! Apparently exec worked better for os.system() from the compatibility perspective, inspired by which I ended up using importlib for the sake of avoid exec/eval as much as I could.

Per:

Instead of using the long-winded if-elif-else, move the example subcommand to a more Pythonic argparse based CLI. This will need insights from the core devs since it requires larger refactor, and might be ideal to implement in a separate PR.

I'd like to get your insights, especially on whether we shall do a refactor in this PR or a seprate one. Right now I don't see any tests for the CLI, which makes the development around it a bit tricky. Ideally if we could switch to something like:

commands = parser.add_subparsers(help='Available Taichi commands')
examples = commands.add_parser('examples', help='Interact with Taichi examples')
examples.add_argument(****)

for most of the commands/flags/arguments, it will be much eaiser to unit-test them and it's less error-prone.

I left example command in the if-elif-else blocks since moving it to above style requires refactoring on other commands too, I figured it's preferred to have them refactored altogether in a separate PR.

@rexwangcc rexwangcc marked this pull request as ready for review May 19, 2020 17:12
@yuanming-hu yuanming-hu mentioned this pull request May 20, 2020
18 tasks
@rexwangcc
Copy link
Collaborator Author

Hi folks! This PR is ready for review, please take a look when you get a chance, thanks 😊

Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! This is very helpful. LGTM in general except for a few minor issues.

python/taichi/main.py Outdated Show resolved Hide resolved
python/taichi/main.py Outdated Show resolved Hide resolved
python/taichi/main.py Outdated Show resolved Hide resolved
Copy link
Member

@yuanming-hu yuanming-hu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much! Everything looks good to me now. After adding this line https://github.com/taichi-dev/taichi/pull/1010/files#diff-2eeaed663bd0d25b7e608891384b7298R19
Your CLI works when Taichi is installed as a Python package. I'll merge after CI passes!

@yuanming-hu yuanming-hu merged commit 97348e7 into taichi-dev:master May 22, 2020
@rexwangcc rexwangcc deleted the add-example-to-cli branch May 22, 2020 19:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants