|
| 1 | +import argparse |
| 2 | +import csv |
| 3 | +import itertools |
| 4 | +import math |
| 5 | +import pathlib |
| 6 | + |
| 7 | +import torch |
| 8 | +import torch.nn.functional as F |
| 9 | +import torch.utils.benchmark as benchmark |
| 10 | +from torchao.kernel.intmm_triton import int_matmul, int_scaled_matmul |
| 11 | + |
| 12 | +torch._dynamo.config.cache_size_limit = 128 |
| 13 | +torch._dynamo.config.accumulated_cache_size_limit = 128 |
| 14 | + |
| 15 | +dtype = torch.float16 |
| 16 | +device = "cuda" |
| 17 | + |
| 18 | + |
| 19 | +def benchmark_in_ms(warmup, iters, f, *args, **kwargs): |
| 20 | + for _ in range(warmup): |
| 21 | + f(*args, **kwargs) |
| 22 | + torch.cuda.synchronize() |
| 23 | + start_event = torch.cuda.Event(enable_timing=True) |
| 24 | + end_event = torch.cuda.Event(enable_timing=True) |
| 25 | + start_event.record() |
| 26 | + |
| 27 | + for _ in range(iters): |
| 28 | + f(*args, **kwargs) |
| 29 | + |
| 30 | + end_event.record() |
| 31 | + torch.cuda.synchronize() |
| 32 | + return start_event.elapsed_time(end_event) / float(iters) |
| 33 | + |
| 34 | + |
| 35 | +@torch.compile(mode="max-autotune") |
| 36 | +def compiled_mm(x, w): |
| 37 | + return torch.mm(x, w) |
| 38 | + |
| 39 | + |
| 40 | +@torch.compile(mode="max-autotune") |
| 41 | +def compiled_int_mm(x, w): |
| 42 | + return torch._int_mm(x, w) |
| 43 | + |
| 44 | + |
| 45 | +def run_int_mm_benchmark(x, w, b): |
| 46 | + fp_time = benchmark_in_ms(10, 100, torch.mm, x, w) |
| 47 | + x_int = x.to(dtype=torch.int8) |
| 48 | + w_int = w.to(dtype=torch.int8) |
| 49 | + int_mm_time = benchmark_in_ms(10, 100, int_matmul, x_int, w_int) |
| 50 | + return fp_time, int_mm_time |
| 51 | + |
| 52 | + |
| 53 | +def run_int_scaled_mm_benchmark(x, w, b): |
| 54 | + scales = x.sum(-1, keepdim=True) |
| 55 | + fp_time = benchmark_in_ms(10, 100, lambda x, w, s: torch.mm(x, w) * s, x, w, scales) |
| 56 | + x_int = x.to(dtype=torch.int8) |
| 57 | + w_int = w.to(dtype=torch.int8) |
| 58 | + int_scaled_mm_time = benchmark_in_ms( |
| 59 | + 10, 100, int_scaled_matmul, x_int, w_int, scales |
| 60 | + ) |
| 61 | + return fp_time, int_scaled_mm_time |
| 62 | + |
| 63 | + |
| 64 | +def run_benchmarks(shapes): |
| 65 | + print("fn,m,k,n,fp_time,int_mm_time,ratio") |
| 66 | + positives = [] |
| 67 | + dtype = torch.bfloat16 |
| 68 | + device = "cuda" |
| 69 | + for fn, (m, k, n) in itertools.product( |
| 70 | + [run_int_mm_benchmark, run_int_scaled_mm_benchmark], shapes |
| 71 | + ): |
| 72 | + x = torch.randn(m, k, dtype=dtype, device=device) |
| 73 | + w = torch.randn(n, k, dtype=dtype, device=device).t() |
| 74 | + b = torch.randn(m, n, dtype=dtype, device=device) |
| 75 | + |
| 76 | + fp_time, int_mm_time = fn(x, w, b) |
| 77 | + ratio = fp_time / int_mm_time |
| 78 | + result = ",".join(map(str, [fn, m, k, n, fp_time, int_mm_time, ratio])) |
| 79 | + print(result) |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == "__main__": |
| 83 | + parser = argparse.ArgumentParser(description="integer matmul benchmarks") |
| 84 | + parser.add_argument("file_path", type=str, help="Path to csv file with shapes") |
| 85 | + args = parser.parse_args() |
| 86 | + # Access the file path provided as an argument |
| 87 | + file_path = args.file_path |
| 88 | + file_path = pathlib.Path(file_path) |
| 89 | + assert file_path.is_file() |
| 90 | + |
| 91 | + # Format is (m, k, n) |
| 92 | + shapes = list(csv.reader(open(file_path, "r")))[1:] |
| 93 | + # Turn into list of int tuples |
| 94 | + shapes = list(map(lambda x: tuple(map(int, x)), shapes)) |
| 95 | + |
| 96 | + run_benchmarks(shapes) |
0 commit comments