|
| 1 | +from typing import Callable, Dict, Generator, List, Optional |
| 2 | + |
| 3 | +from tritonbench.utils.triton_op import BenchmarkOperator |
| 4 | + |
| 5 | + |
| 6 | +def dynamic_run( |
| 7 | + benchmarks: Dict[str, Callable], |
| 8 | + input_iter: Optional[Generator], |
| 9 | + **kwargs, |
| 10 | +) -> None: |
| 11 | + """ |
| 12 | + Run a list of benchmarks with a given set of inputs and kwargs. |
| 13 | + Kwargs in this case are the command-line arguments available in tritonbench. |
| 14 | +
|
| 15 | + Example: |
| 16 | +
|
| 17 | + def triton_add(x, y): |
| 18 | + ... |
| 19 | +
|
| 20 | + def input_iter(): |
| 21 | + size = 2**12 |
| 22 | + x = torch.rand(size, device="cuda") |
| 23 | + y = torch.rand(size, device="cuda") |
| 24 | + yield x,y |
| 25 | +
|
| 26 | + benchmarks = { |
| 27 | + "triton_add": triton_add, |
| 28 | + "triton_add2": triton_add, |
| 29 | + } |
| 30 | +
|
| 31 | + dynamic_run(benchmarks=benchmarks, input_iter=input_iter, benchmark_name="vector_add") |
| 32 | + """ |
| 33 | + |
| 34 | + # Convert kwargs into a list of command-line arguments |
| 35 | + arg_list = [] |
| 36 | + for k, v in kwargs.items(): |
| 37 | + key = f"--{k.replace('_', '-')}" |
| 38 | + arg_list.append(key) |
| 39 | + arg_list.append(str(v)) |
| 40 | + |
| 41 | + op = BenchmarkOperator(extra_args=arg_list) |
| 42 | + |
| 43 | + op.set_input_iter(input_iter) |
| 44 | + |
| 45 | + for k, v in benchmarks.items(): |
| 46 | + op.add_benchmark(bm_func_name=k, bm_callable=v) |
| 47 | + |
| 48 | + op.run() |
| 49 | + print(op.output) |
| 50 | + return op.output |
| 51 | + |
| 52 | + |
| 53 | +def dynamic_run_once( |
| 54 | + benchmarks: Dict[str, Callable], single_input: Optional[List], **kwargs |
| 55 | +): |
| 56 | + """ |
| 57 | + Run a list of benchmarks with a given set of inputs and kwargs. |
| 58 | + Kwargs in this case are the command-line arguments available in tritonbench. |
| 59 | +
|
| 60 | + The single_input is a list of arguments that will be passed to the benchmark function all together |
| 61 | +
|
| 62 | + Example: |
| 63 | +
|
| 64 | + def triton_add(x, y): |
| 65 | + ... |
| 66 | +
|
| 67 | + benchmarks = { |
| 68 | + "triton_add": triton_add, |
| 69 | + "triton_add2": triton_add, |
| 70 | + } |
| 71 | + size = 2**12 |
| 72 | + x = torch.rand(size, device="cuda") |
| 73 | + y = torch.rand(size, device="cuda") |
| 74 | + dynamic_run_once(benchmarks=benchmarks, single_input=[x, y], benchmark_name="vector_add") |
| 75 | + """ |
| 76 | + |
| 77 | + def input_iterator(*args): |
| 78 | + def generator(): |
| 79 | + yield args |
| 80 | + |
| 81 | + return generator |
| 82 | + |
| 83 | + input_generator = input_iterator(*single_input) |
| 84 | + output = dynamic_run(benchmarks, input_generator, **kwargs) |
| 85 | + return output |
0 commit comments