|
| 1 | +# Sparse benchmarks |
| 2 | + |
| 3 | +# This benchmark is for sparse matmul performance test. |
| 4 | +# They exist for comparing the performance of sparse matrix routines |
| 5 | +# `sparse @ vector`, `sparse @ sparse` and `sparse @ dense` with different backends (CPU/CUDA) |
| 6 | +# and with other frameworks such as scipy. |
| 7 | + |
| 8 | +import sys |
| 9 | +import argparse |
| 10 | +import torch |
| 11 | +import torch.utils.benchmark as benchmark_utils |
| 12 | +from .utils import load_dlmc_dataset |
| 13 | +from scipy.sparse import isspmatrix |
| 14 | +import os |
| 15 | + |
| 16 | + |
| 17 | +def scipy_matmul(mat1, mat2): |
| 18 | + if isspmatrix(mat1) and isspmatrix(mat2): |
| 19 | + return mat1.dot(mat2).tocoo() |
| 20 | + return mat1.dot(mat2) |
| 21 | + |
| 22 | +def matmul_backward(a_dense, b_dense, grad_output): |
| 23 | + r1 = a_dense.matmul(b_dense) |
| 24 | + r1.backward(grad_output) |
| 25 | + |
| 26 | + |
| 27 | +def sparse_matmul_backward(a, b, grad_output): |
| 28 | + c = torch.sparse.mm(a, b) |
| 29 | + c.backward(grad_output) |
| 30 | + |
| 31 | + |
| 32 | +OPS_MAP = { |
| 33 | + "sparse@sparse": "torch.sparse.mm", |
| 34 | + "sparse@dense": "torch.matmul", |
| 35 | + "sparse@vector": "torch.matmul", |
| 36 | +} |
| 37 | + |
| 38 | + |
| 39 | +# also get the arguments as input from the user using `argparse` |
| 40 | +def parse_args(): |
| 41 | + parser = argparse.ArgumentParser(description='matmul benchmark') |
| 42 | + parser.add_argument('--path', type=str, help='DLMC dataset path') |
| 43 | + parser.add_argument('--dataset', type=str, default='magnitude_pruning') |
| 44 | + parser.add_argument('--hidden_size', default=2048, type=int) |
| 45 | + parser.add_argument('--backward_test', action="store_true") |
| 46 | + parser.add_argument('--operation', type=str, help="|".join(OPS_MAP.keys()), default=next(iter(OPS_MAP))) |
| 47 | + parser.add_argument('--with_cuda', action='store_true') |
| 48 | + parser.add_argument('--timer_min_run_time', default=1, type=float) |
| 49 | + return parser |
| 50 | + |
| 51 | + |
| 52 | +def get_tasks(op, backward_test, device): |
| 53 | + def filter_ops(operation): |
| 54 | + if backward_test: |
| 55 | + test_name = device + ":matmul-backward" |
| 56 | + return [ |
| 57 | + (test_name, device, "torch:" + operation.replace("sparse", "dense"), |
| 58 | + "matmul_backward(dx, dy, grad_output)"), |
| 59 | + (test_name, device, "torch:" + operation, "sparse_matmul_backward(x, y, sparse_grad_output)") |
| 60 | + ] |
| 61 | + else: |
| 62 | + test_name = device + ":matmul-forward" |
| 63 | + return list(filter(None, [ |
| 64 | + (test_name, device, "torch:" + operation.replace("sparse", "dense"), |
| 65 | + "{}(dx, dy)".format(OPS_MAP[operation])), |
| 66 | + (test_name, device, "torch:" + operation, "{}(x, y)".format(OPS_MAP[operation])), |
| 67 | + (test_name, device, "scipy:" + operation, "scipy_matmul(sx, sy)") if device == "cpu" else None |
| 68 | + ])) |
| 69 | + |
| 70 | + all_operations = { |
| 71 | + "sparse@sparse": filter_ops("sparse@sparse"), |
| 72 | + "sparse@dense": filter_ops("sparse@dense"), |
| 73 | + "sparse@vector": filter_ops("sparse@vector"), |
| 74 | + } |
| 75 | + return all_operations[op] |
| 76 | + |
| 77 | + |
| 78 | +if __name__ == '__main__': |
| 79 | + parser = parse_args() |
| 80 | + args = parser.parse_args() |
| 81 | + |
| 82 | + if args.with_cuda and not torch.cuda.is_available(): |
| 83 | + raise RuntimeError("No CUDA available") |
| 84 | + |
| 85 | + dataset_path = args.path |
| 86 | + dataset_name = args.dataset |
| 87 | + dataset_path = os.path.join(dataset_path, dataset_name) |
| 88 | + device = 'cuda' if args.with_cuda else 'cpu' |
| 89 | + |
| 90 | + tasks = get_tasks(args.operation, args.backward_test, device) |
| 91 | + repeats = 3 |
| 92 | + timers = [ |
| 93 | + benchmark_utils.Timer( |
| 94 | + stmt=stmt, |
| 95 | + globals={ |
| 96 | + "scipy_matmul": scipy_matmul, |
| 97 | + "matmul_backward": matmul_backward, |
| 98 | + "sparse_matmul_backward": sparse_matmul_backward, |
| 99 | + **variables |
| 100 | + }, |
| 101 | + label=label, |
| 102 | + sub_label=sub_label, |
| 103 | + description=f"{sparsity}", |
| 104 | + env=device, |
| 105 | + ) |
| 106 | + for sparsity in [0.5, 0.7, 0.8, 0.9, 0.95, 0.98] |
| 107 | + for label, device, sub_label, stmt in tasks |
| 108 | + for variables in |
| 109 | + load_dlmc_dataset(dataset_path, args.operation, args.hidden_size, sparsity, device, args.backward_test) |
| 110 | + ] |
| 111 | + measurements = [] |
| 112 | + |
| 113 | + for i, timer in enumerate(timers * repeats): |
| 114 | + m = timer.blocked_autorange(min_run_time=args.timer_min_run_time) |
| 115 | + m.metadata = { |
| 116 | + "device": 'cuda' if m.task_spec.env.find("cuda") >= 0 else 'cpu' |
| 117 | + } |
| 118 | + measurements.append(m) |
| 119 | + print(f"\r{i + 1} / {len(timers) * repeats}", end="") |
| 120 | + sys.stdout.flush() |
| 121 | + print() |
| 122 | + |
| 123 | + comparison = benchmark_utils.Compare(measurements) |
| 124 | + |
| 125 | + print("== Results " + "=" * 80 + "\n" + "/" * 95 + "\n") |
| 126 | + comparison.print() |
0 commit comments