|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | +# All rights reserved. |
| 4 | + |
| 5 | +# This source code is licensed under the license found in the |
| 6 | +# LICENSE file in the root directory of this source tree. |
| 7 | + |
| 8 | +""" |
| 9 | +CI Microbenchmark Runner for PyTorch OSS Benchmark Database |
| 10 | +
|
| 11 | +This script runs microbenchmarks for a given config file |
| 12 | +and outputs results in the format required by the PyTorch OSS benchmark database. |
| 13 | +It reuses functionality from benchmark_runner.py and only adds CI-specific code. |
| 14 | +
|
| 15 | +Usage: |
| 16 | + python ci_microbenchmark_runner.py --config benchmark_config.yml |
| 17 | +
|
| 18 | +The YAML file should contain all necessary configuration parameters for the benchmarks. |
| 19 | +""" |
| 20 | + |
| 21 | +import argparse |
| 22 | +import json |
| 23 | +import platform |
| 24 | +from typing import Any, Dict, List |
| 25 | + |
| 26 | +import torch |
| 27 | + |
| 28 | +from benchmarks.microbenchmarks.benchmark_inference import run as run_inference |
| 29 | +from benchmarks.microbenchmarks.benchmark_runner import ( |
| 30 | + load_benchmark_configs, |
| 31 | +) |
| 32 | +from benchmarks.microbenchmarks.utils import clean_caches |
| 33 | + |
| 34 | + |
| 35 | +def create_benchmark_result( |
| 36 | + benchmark_name: str, |
| 37 | + shape: List[int], |
| 38 | + metric_name: str, |
| 39 | + metric_values: List[float], |
| 40 | + quant_type: str, |
| 41 | + device: str, |
| 42 | +) -> Dict[str, Any]: |
| 43 | + """Create a benchmark result in the PyTorch OSS benchmark database format. |
| 44 | +
|
| 45 | + Args: |
| 46 | + benchmark_name: Name of the benchmark |
| 47 | + shape: List of shape dimensions [M, K, N] |
| 48 | + metric_name: Name of the metric |
| 49 | + metric_values: List of metric values |
| 50 | + quant_type: Quantization type |
| 51 | + device: Device type (cuda/cpu) |
| 52 | +
|
| 53 | + Returns: |
| 54 | + Dictionary containing the benchmark result in the required format |
| 55 | + """ |
| 56 | + print( |
| 57 | + f"Creating benchmark result for {benchmark_name} with shape {shape} and metric {metric_name}" |
| 58 | + ) |
| 59 | + |
| 60 | + # Map device to benchmark device name |
| 61 | + benchmark_device = ( |
| 62 | + torch.cuda.get_device_name(0) |
| 63 | + if device == "cuda" |
| 64 | + else platform.processor() |
| 65 | + if device == "cpu" |
| 66 | + else "unknown" |
| 67 | + ) |
| 68 | + |
| 69 | + # Format shape as M-K-N |
| 70 | + mkn_name = f"{shape[0]}-{shape[1]}-{shape[2]}" if len(shape) == 3 else "unknown" |
| 71 | + |
| 72 | + return { |
| 73 | + "benchmark": { |
| 74 | + "name": "micro-benchmark api", |
| 75 | + "mode": "inference", |
| 76 | + "dtype": quant_type, |
| 77 | + "extra_info": { |
| 78 | + "device": device, |
| 79 | + "arch": benchmark_device, |
| 80 | + }, |
| 81 | + }, |
| 82 | + "model": { |
| 83 | + "name": mkn_name, # name in M-K-N format |
| 84 | + "type": "micro-benchmark custom layer", # type |
| 85 | + "origins": ["torchao"], |
| 86 | + }, |
| 87 | + "metric": { |
| 88 | + "name": f"{metric_name}(wrt bf16)", # name with unit |
| 89 | + "benchmark_values": metric_values, # benchmark_values |
| 90 | + "target_value": 0.0, # TODO: Will need to define the target value |
| 91 | + }, |
| 92 | + "runners": [], |
| 93 | + "dependencies": {}, |
| 94 | + } |
| 95 | + |
| 96 | + |
| 97 | +def run_ci_benchmarks(config_path: str) -> List[Dict[str, Any]]: |
| 98 | + """Run benchmarks using configurations from YAML file and return results in OSS format. |
| 99 | +
|
| 100 | + Args: |
| 101 | + config_path: Path to the benchmark configuration file |
| 102 | +
|
| 103 | + Returns: |
| 104 | + List of benchmark results in the PyTorch OSS benchmark database format |
| 105 | + """ |
| 106 | + # Load configuration using existing function |
| 107 | + configs = load_benchmark_configs(argparse.Namespace(config=config_path)) |
| 108 | + results = [] |
| 109 | + |
| 110 | + # Run benchmarks for each config |
| 111 | + for config in configs: |
| 112 | + # Run benchmark using existing function |
| 113 | + clean_caches() |
| 114 | + result = run_inference(config) |
| 115 | + |
| 116 | + if result is not None: |
| 117 | + # Create benchmark result in OSS format |
| 118 | + benchmark_result = create_benchmark_result( |
| 119 | + benchmark_name="TorchAO Quantization Benchmark", |
| 120 | + shape=[config.m, config.k, config.n], |
| 121 | + metric_name="speedup", |
| 122 | + metric_values=[result.speedup], |
| 123 | + quant_type=config.quantization, |
| 124 | + device=config.device, |
| 125 | + ) |
| 126 | + results.append(benchmark_result) |
| 127 | + |
| 128 | + return results |
| 129 | + |
| 130 | + |
| 131 | +def main(): |
| 132 | + parser = argparse.ArgumentParser( |
| 133 | + description="Run microbenchmarks and output results in PyTorch OSS benchmark database format" |
| 134 | + ) |
| 135 | + parser.add_argument( |
| 136 | + "--config", |
| 137 | + type=str, |
| 138 | + required=True, |
| 139 | + help="Path to benchmark configuration file", |
| 140 | + ) |
| 141 | + parser.add_argument( |
| 142 | + "--output", |
| 143 | + type=str, |
| 144 | + default="benchmark_results.json", |
| 145 | + help="Path to output JSON file", |
| 146 | + ) |
| 147 | + args = parser.parse_args() |
| 148 | + |
| 149 | + # Run benchmarks |
| 150 | + results = run_ci_benchmarks(args.config) |
| 151 | + |
| 152 | + # Save results to JSON file |
| 153 | + with open(args.output, "w") as f: |
| 154 | + json.dump(results, f, indent=2) |
| 155 | + |
| 156 | + print(f"Benchmark results saved to {args.output}") |
| 157 | + |
| 158 | + |
| 159 | +if __name__ == "__main__": |
| 160 | + main() |
0 commit comments