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
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
85bb386
Bugfix: shutil not imported for command:convert.
rexwangcc May 17, 2020
071edd9
Merge branch 'master' of https://github.com/taichi-dev/taichi into ad…
rexwangcc May 17, 2020
2038b1e
Minor improvement to dev_install docs.
rexwangcc May 17, 2020
9edb5f0
Document some hacks for dev_install under Ubuntu.
rexwangcc May 17, 2020
84b9647
The naive way to implement this.
rexwangcc May 18, 2020
fc7afe8
Include the examples folder in Python build.
rexwangcc May 18, 2020
ed8bc91
Nice, macOS could use Homebrew.
rexwangcc May 18, 2020
1787d60
Error out when invalid example name is given.
rexwangcc May 18, 2020
0575229
[skip ci] enforce code format
taichi-gardener May 18, 2020
7f207c0
Revert changes to docs for correctness and simplicity.
rexwangcc May 18, 2020
a7c1c85
Resolve merge conflict with the gardener.
rexwangcc May 18, 2020
94a4da8
Switch off os.system and use importlib.
rexwangcc May 18, 2020
daa7876
Remove redundant comments.
rexwangcc May 18, 2020
b7a5e0b
Travis CI failed. Please retry.
rexwangcc May 19, 2020
860b085
Resolve merge conflict.
rexwangcc May 19, 2020
61d0720
[skip ci] enforce code format
taichi-gardener May 19, 2020
08eae82
Travis CI failed. Please retry.
rexwangcc May 19, 2020
e4fe241
Merge branch 'add-example-to-cli' of github.com:rexwangcc/taichi into…
rexwangcc May 19, 2020
99b3b9b
In case the user forgets to specify anything. we SHOULD really switch…
rexwangcc May 20, 2020
d566618
[skip ci] i must be sleepy.
rexwangcc May 20, 2020
417a105
How about factor them out.
rexwangcc May 21, 2020
28c4417
One more thing to make it runnable from anywhere.
rexwangcc May 21, 2020
6cba5b8
Alright lint.
rexwangcc May 21, 2020
e4131f2
add taichi.examples to package
yuanming-hu May 22, 2020
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
2 changes: 1 addition & 1 deletion docs/utilities.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ The suggested workflow for the performance related PR author to run the regressi
* Run ``git checkout -b your-branch-name``.

* Do works on the issue, stage 1.

* Run ``ti benchmark && ti regression`` to obtain the result.

* (If result BAD) Do further improvements, until the result is satisfying.
Expand Down
3 changes: 3 additions & 0 deletions python/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def get_python_executable():

shutil.rmtree('taichi/lib', ignore_errors=True)
shutil.rmtree('taichi/tests', ignore_errors=True)
shutil.rmtree('taichi/examples', ignore_errors=True)
os.makedirs('taichi/lib', exist_ok=True)
shutil.rmtree('build', ignore_errors=True)
shutil.rmtree('dist', ignore_errors=True)
Expand All @@ -80,6 +81,7 @@ def get_python_executable():
'taichi/lib/taichi_core.pyd')

shutil.copytree('../tests/python', './taichi/tests')
shutil.copytree('../examples', './taichi/examples')

if get_os_name() != 'osx':
libdevice_path = ti.core.libdevice_path()
Expand All @@ -106,6 +108,7 @@ def get_python_executable():

shutil.rmtree('taichi/lib')
shutil.rmtree('taichi/tests')
shutil.rmtree('taichi/examples')
shutil.rmtree('./build')

if mode == 'upload':
Expand Down
47 changes: 42 additions & 5 deletions python/taichi/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from collections import defaultdict
from colorama import Fore, Back, Style
from taichi.tools.video import make_video, interpolate_frames, mp4_to_gif, scale_video, crop_video, accelerate_video
import importlib


def test_python(args):
Expand Down Expand Up @@ -72,6 +73,31 @@ def test_cpp(args):
return int(task.run(*test_files))


def run_example(name: list):
"""Run an example based on the example NAME."""
import taichi as ti
from pathlib import Path

root_dir = ti.package_root() if ti.is_release() else ti.get_repo_directory(
)
examples_dir = Path(root_dir) / 'examples'
all_examples = examples_dir.rglob('*.py')
all_example_names = {
str(f.resolve()).split('/')[-1].split('.')[0]
for f in all_examples
}
if len(name) != 1:
sys.exit(
f"Sorry, please specify a name of example!\nAvailable examples names are: {all_example_names}"
rexwangcc marked this conversation as resolved.
Show resolved Hide resolved
)
elif name[0] not in all_example_names:
sys.exit(
f"Sorry, {name} is not an available example name!\nAvailable examples names are: {all_example_names}"
)
print(f"Running example {name[0]} ...")
importlib.import_module(f"examples.{name[0]}")
rexwangcc marked this conversation as resolved.
Show resolved Hide resolved


def get_benchmark_baseline_dir():
import taichi as ti
return os.path.join(ti.core.get_repo_dir(), 'benchmarks', 'baseline')
Expand All @@ -86,8 +112,10 @@ def display_benchmark_regression(xd, yd, args):
def parse_dat(file):
dict = {}
for line in open(file).readlines():
try: a, b = line.strip().split(':')
except: continue
try:
a, b = line.strip().split(':')
except:
continue
b = float(b)
if abs(b % 1.0) < 1e-5: # codegen_*
b = int(b)
Expand Down Expand Up @@ -120,7 +148,8 @@ def plot_in_gui(scatter):
gui = ti.GUI('Regression Test', (640, 480), 0x001122)
print('[Hint] press SPACE to go for next display')
for key, data in scatter.items():
data = np.array([((i + 0.5)/len(data), x/2) for i, x in enumerate(data)])
data = np.array([((i + 0.5) / len(data), x / 2)
for i, x in enumerate(data)])
while not gui.get_event((ti.GUI.PRESS, ti.GUI.SPACE)):
gui.core.title = key
gui.line((0, 0.5), (1, 0.5), 1.8, 0x66ccff)
Expand Down Expand Up @@ -183,7 +212,9 @@ def make_argument_parser():
'--verbose',
action='store_true',
help='Run with verbose outputs')
parser.add_argument('-r', '--rerun', help='Rerun failed tests for given times')
parser.add_argument('-r',
'--rerun',
help='Rerun failed tests for given times')
parser.add_argument('-t',
'--threads',
help='Number of threads for parallel testing')
Expand Down Expand Up @@ -263,7 +294,9 @@ def main(debug=False):
" ti gif |-> Convert mp4 file to gif\n"
" ti doc |-> Build documentation\n"
" ti release |-> Make source code release\n"
" ti debug [script.py] |-> Debug script\n")
" ti debug [script.py] |-> Debug script\n"
" ti example [name] |-> Run an example by name\n"
)
return 0

t = time.time()
Expand Down Expand Up @@ -407,6 +440,7 @@ def main(debug=False):
framerate = 24
mp4_to_gif(input_fn, output_fn, framerate)
elif mode == "convert":
import shutil
# http://www.commandlinefu.com/commands/view/3584/remove-color-codes-special-characters-with-sed
# TODO: Windows support
for fn in sys.argv[2:]:
Expand Down Expand Up @@ -438,6 +472,9 @@ def main(debug=False):
fn = f'taichi-src-v{ver[0]}-{ver[1]}-{ver[2]}-{commit}-{md5}.zip'
import shutil
shutil.move('release.zip', fn)
elif mode == "example":
example = sys.argv[2:]
run_example(name=example)
rexwangcc marked this conversation as resolved.
Show resolved Hide resolved
else:
name = sys.argv[1]
print('Running task [{}]...'.format(name))
Expand Down