|
1 | 1 | """Script to run a Triton tutorial and collect generated csv files.""" |
2 | 2 |
|
3 | 3 | import argparse |
| 4 | +import dataclasses |
| 5 | +import datetime |
4 | 6 | import importlib.util |
| 7 | +import json |
5 | 8 | import pathlib |
6 | 9 | import shutil |
7 | 10 | import tempfile |
| 11 | +import sys |
| 12 | +from typing import Set, Optional |
8 | 13 |
|
9 | 14 | import triton.testing |
10 | 15 |
|
@@ -33,38 +38,95 @@ def create_argument_parser() -> argparse.ArgumentParser: |
33 | 38 | """Creates argument parser.""" |
34 | 39 | parser = argparse.ArgumentParser() |
35 | 40 | parser.add_argument('tutorial', help='Tutorial to run') |
36 | | - parser.add_argument('--reports', required=False, type=str, default='.', |
37 | | - help='Directory to store tutorial CSV reports, default: %(default)s') |
| 41 | + parser.add_argument('--reports', required=False, type=str, help='Directory to store tutorial CSV reports') |
| 42 | + parser.add_argument('--skip-list', required=False, type=str, help='Skip list for tutorials') |
38 | 43 | return parser |
39 | 44 |
|
40 | 45 |
|
41 | | -def run_tutorial(path: pathlib.Path): |
42 | | - """Runs """ |
| 46 | +def get_skip_list(path: pathlib.Path) -> Set[str]: |
| 47 | + """Loads skip list from the specified file.""" |
| 48 | + skip_list = set() |
| 49 | + if path.exists() and path.is_file(): |
| 50 | + for item in path.read_text().splitlines(): |
| 51 | + skip_list.add(item.strip()) |
| 52 | + return skip_list |
| 53 | + |
| 54 | + |
| 55 | +def run_tutorial(path: pathlib.Path) -> float: |
| 56 | + """Runs tutorial.""" |
43 | 57 | spec = importlib.util.spec_from_file_location('__main__', path) |
44 | 58 | if not spec or not spec.loader: |
45 | 59 | raise AssertionError(f'Failed to load module from {path}') |
46 | 60 | module = importlib.util.module_from_spec(spec) |
47 | | - # set __file__ to the absolute name, a workaround for 10i-experimental-block-pointer, which |
| 61 | + # Set __file__ to the absolute name, a workaround for 10i-experimental-block-pointer, which |
48 | 62 | # uses dirname of its location to find 10-experimental-block-pointer. |
49 | 63 | module.__file__ = path.resolve().as_posix() |
| 64 | + # Reset sys.argv because some tutorials, such as 09, parse their command line arguments. |
| 65 | + sys.argv = [str(path)] |
| 66 | + start_time = datetime.datetime.now() |
50 | 67 | spec.loader.exec_module(module) |
| 68 | + elapsed_time = datetime.datetime.now() - start_time |
| 69 | + return elapsed_time.total_seconds() |
| 70 | + |
| 71 | + |
| 72 | +@dataclasses.dataclass |
| 73 | +class TutorialInfo: |
| 74 | + """A record about tutorial execution.""" |
| 75 | + name: str |
| 76 | + path: Optional[pathlib.Path] = None |
| 77 | + |
| 78 | + def _report(self, **kwargs): |
| 79 | + """Writes tutorial info.""" |
| 80 | + if self.path: |
| 81 | + with self.path.open(mode='w') as f: |
| 82 | + json.dump({'name': self.name} | kwargs, f) |
| 83 | + |
| 84 | + def report_pass(self, time: float): |
| 85 | + """Reports successful tutorial.""" |
| 86 | + self._report(result='PASS', time=time) |
| 87 | + |
| 88 | + def report_skip(self): |
| 89 | + """Reports skipped tutorial.""" |
| 90 | + self._report(result='SKIP') |
| 91 | + |
| 92 | + def report_fail(self, message: str): |
| 93 | + """Reports failed tutorial.""" |
| 94 | + self._report(result='FAIL', message=message) |
51 | 95 |
|
52 | 96 |
|
53 | 97 | def main(): |
54 | 98 | """Main.""" |
| 99 | + skip_list = set() |
55 | 100 | args = create_argument_parser().parse_args() |
56 | 101 | tutorial_path = pathlib.Path(args.tutorial) |
57 | | - reports_path = pathlib.Path(args.reports) |
| 102 | + |
| 103 | + reports_path = pathlib.Path(args.reports) if args.reports else None |
| 104 | + if args.skip_list: |
| 105 | + skip_list = get_skip_list(pathlib.Path(args.skip_list)) |
| 106 | + |
58 | 107 | name = tutorial_path.stem |
59 | | - report_path = reports_path / name |
60 | | - report_path.mkdir(parents=True, exist_ok=True) |
| 108 | + if reports_path: |
| 109 | + report_path = reports_path / name |
| 110 | + report_path.mkdir(parents=True, exist_ok=True) |
| 111 | + info = TutorialInfo(name=name, path=reports_path / f'tutorial-{name}.json') |
| 112 | + else: |
| 113 | + info = TutorialInfo(name=name) |
61 | 114 |
|
62 | 115 | def perf_report(benchmarks): |
63 | 116 | """Marks a function for benchmarking.""" |
64 | 117 | return lambda fn: CustomMark(fn, benchmarks, report_path) |
65 | 118 |
|
66 | | - triton.testing.perf_report = perf_report |
67 | | - run_tutorial(tutorial_path) |
| 119 | + if name in skip_list: |
| 120 | + info.report_skip() |
| 121 | + else: |
| 122 | + if reports_path: |
| 123 | + triton.testing.perf_report = perf_report |
| 124 | + try: |
| 125 | + time = run_tutorial(tutorial_path) |
| 126 | + info.report_pass(time) |
| 127 | + except Exception as e: |
| 128 | + info.report_fail(str(e)) |
| 129 | + raise |
68 | 130 |
|
69 | 131 |
|
70 | 132 | if __name__ == '__main__': |
|
0 commit comments