|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | + |
| 3 | +# pyre-ignore-all-errors |
| 4 | + |
| 5 | +""" |
| 6 | +Unified benchmark runner for CinderX. |
| 7 | +
|
| 8 | +Usage: |
| 9 | + python -m cinderx.benchmarks # Run all available benchmarks |
| 10 | + python -m cinderx.benchmarks binary_trees # Run specific benchmark(s) |
| 11 | + python -m cinderx.benchmarks --list # List available benchmarks |
| 12 | +""" |
| 13 | + |
| 14 | +import os |
| 15 | +import subprocess |
| 16 | +import sys |
| 17 | + |
| 18 | +# Lightweight benchmarks that only require cinderx itself. |
| 19 | +LIGHTWEIGHT_BENCHMARKS = [ |
| 20 | + "binary_trees", |
| 21 | + "fannkuch", |
| 22 | + "nbody", |
| 23 | + "richards", |
| 24 | + "spectral_norm", |
| 25 | +] |
| 26 | + |
| 27 | +# JIT compilation time benchmarks (measure compilation speed, not runtime). |
| 28 | +JIT_COMPILATION_BENCHMARKS = [ |
| 29 | + "compile_time", |
| 30 | +] |
| 31 | + |
| 32 | +# Heavyweight benchmarks with extra dependencies. |
| 33 | +HEAVYWEIGHT_BENCHMARKS = { |
| 34 | + "fastmark": "requirements-fastmark.txt", |
| 35 | + "torchrec_pt2": "requirements-torchrec.txt", |
| 36 | +} |
| 37 | + |
| 38 | +ALL_BENCHMARK_NAMES = ( |
| 39 | + LIGHTWEIGHT_BENCHMARKS |
| 40 | + + JIT_COMPILATION_BENCHMARKS |
| 41 | + + list(HEAVYWEIGHT_BENCHMARKS.keys()) |
| 42 | +) |
| 43 | + |
| 44 | + |
| 45 | +def run_benchmark(name, iterations): |
| 46 | + """Run a single benchmark as a subprocess. Returns True on success.""" |
| 47 | + benchmarks_dir = os.path.dirname(os.path.abspath(__file__)) |
| 48 | + |
| 49 | + if name in LIGHTWEIGHT_BENCHMARKS: |
| 50 | + script = os.path.join(benchmarks_dir, f"{name}.py") |
| 51 | + cmd = [sys.executable, script, str(iterations)] |
| 52 | + elif name == "compile_time": |
| 53 | + cmd = [sys.executable, "-m", "cinderx.benchmarks.compile_time"] |
| 54 | + elif name == "fastmark": |
| 55 | + script = os.path.join(benchmarks_dir, "fastmark.py") |
| 56 | + cmd = [sys.executable, script, "--cinderx", "--scale", "10"] |
| 57 | + elif name == "torchrec_pt2": |
| 58 | + script = os.path.join(benchmarks_dir, "torchrec_pt2", "run_with_cinderx.py") |
| 59 | + cmd = [sys.executable, script] |
| 60 | + else: |
| 61 | + print(f"Unknown benchmark: {name}", file=sys.stderr) |
| 62 | + return False |
| 63 | + |
| 64 | + print(f"\n{'='*60}") |
| 65 | + print(f"Running: {name}") |
| 66 | + print(f"{'='*60}") |
| 67 | + |
| 68 | + try: |
| 69 | + result = subprocess.run(cmd, check=False) |
| 70 | + if result.returncode != 0: |
| 71 | + return False |
| 72 | + except FileNotFoundError: |
| 73 | + print(f" Error: could not execute {cmd[0]}", file=sys.stderr) |
| 74 | + return False |
| 75 | + |
| 76 | + return True |
| 77 | + |
| 78 | + |
| 79 | +def check_deps(name): |
| 80 | + """Check if a benchmark's dependencies are available.""" |
| 81 | + if name in LIGHTWEIGHT_BENCHMARKS or name in JIT_COMPILATION_BENCHMARKS: |
| 82 | + return True |
| 83 | + elif name == "fastmark": |
| 84 | + try: |
| 85 | + import importlib.util |
| 86 | + |
| 87 | + return importlib.util.find_spec("pyperformance") is not None |
| 88 | + except ImportError: |
| 89 | + return False |
| 90 | + elif name == "torchrec_pt2": |
| 91 | + try: |
| 92 | + import importlib.util |
| 93 | + |
| 94 | + return importlib.util.find_spec("torchrec") is not None |
| 95 | + except ImportError: |
| 96 | + return False |
| 97 | + return True |
| 98 | + |
| 99 | + |
| 100 | +def main(): |
| 101 | + args = sys.argv[1:] |
| 102 | + |
| 103 | + # Parse --iterations N |
| 104 | + iterations = 3 |
| 105 | + if "--iterations" in args: |
| 106 | + idx = args.index("--iterations") |
| 107 | + try: |
| 108 | + iterations = int(args[idx + 1]) |
| 109 | + args = args[:idx] + args[idx + 2:] |
| 110 | + except (IndexError, ValueError): |
| 111 | + print("Error: --iterations requires an integer argument", file=sys.stderr) |
| 112 | + sys.exit(1) |
| 113 | + |
| 114 | + if "--list" in args or "-l" in args: |
| 115 | + print("Available benchmarks:") |
| 116 | + print() |
| 117 | + print("Lightweight (no extra dependencies):") |
| 118 | + for name in LIGHTWEIGHT_BENCHMARKS: |
| 119 | + print(f" {name}") |
| 120 | + print() |
| 121 | + print("JIT compilation time:") |
| 122 | + for name in JIT_COMPILATION_BENCHMARKS: |
| 123 | + print(f" {name}") |
| 124 | + print() |
| 125 | + print("Heavyweight (extra dependencies required):") |
| 126 | + for name, req in HEAVYWEIGHT_BENCHMARKS.items(): |
| 127 | + print(f" {name} (pip install -r {req})") |
| 128 | + return |
| 129 | + |
| 130 | + if "--help" in args or "-h" in args: |
| 131 | + print(__doc__) |
| 132 | + print("Options:") |
| 133 | + print(" --list, -l List available benchmarks") |
| 134 | + print(" --iterations N Number of iterations for lightweight benchmarks (default: 3)") |
| 135 | + print(" --help, -h Show this help message") |
| 136 | + print() |
| 137 | + print("Examples:") |
| 138 | + print(" python -m cinderx.benchmarks") |
| 139 | + print(" python -m cinderx.benchmarks binary_trees fannkuch") |
| 140 | + print(" python -m cinderx.benchmarks compile_time") |
| 141 | + return |
| 142 | + |
| 143 | + benchmarks_to_run = args if args else ALL_BENCHMARK_NAMES |
| 144 | + |
| 145 | + # Validate names |
| 146 | + for name in benchmarks_to_run: |
| 147 | + if name not in ALL_BENCHMARK_NAMES: |
| 148 | + print(f"Unknown benchmark: {name}", file=sys.stderr) |
| 149 | + print(f"Run 'python -m cinderx.benchmarks --list' to see available benchmarks") |
| 150 | + sys.exit(1) |
| 151 | + |
| 152 | + passed = 0 |
| 153 | + skipped = 0 |
| 154 | + failed = 0 |
| 155 | + |
| 156 | + for name in benchmarks_to_run: |
| 157 | + if not check_deps(name): |
| 158 | + req_file = HEAVYWEIGHT_BENCHMARKS.get(name) |
| 159 | + print(f"\nSkipping {name} — missing dependencies.") |
| 160 | + if req_file: |
| 161 | + print(f" Install with: pip install -r cinderx/benchmarks/{req_file}") |
| 162 | + skipped += 1 |
| 163 | + continue |
| 164 | + |
| 165 | + success = run_benchmark(name, iterations) |
| 166 | + if success: |
| 167 | + passed += 1 |
| 168 | + else: |
| 169 | + failed += 1 |
| 170 | + |
| 171 | + print(f"\n{'='*60}") |
| 172 | + print(f"Results: {passed} passed, {skipped} skipped, {failed} failed") |
| 173 | + print(f"{'='*60}") |
| 174 | + |
| 175 | + if failed: |
| 176 | + sys.exit(1) |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + main() |
0 commit comments