| 1 |
import time |
| 2 |
import itertools |
| 3 |
|
| 4 |
import argparse |
| 5 |
import torch |
| 6 |
from scipy.io import loadmat |
| 7 |
|
| 8 |
from torch_scatter import gather_coo, gather_csr |
| 9 |
|
| 10 |
from scatter_segment import short_rows, long_rows, download, bold |
| 11 |
|
| 12 |
|
| 13 |
@torch.no_grad() |
| 14 |
def correctness(dataset): |
| 15 |
group, name = dataset |
| 16 |
mat = loadmat(f'{name}.mat')['Problem'][0][0][2].tocsr() |
| 17 |
rowptr = torch.from_numpy(mat.indptr).to(args.device, torch.long) |
| 18 |
row = torch.from_numpy(mat.tocoo().row).to(args.device, torch.long) |
| 19 |
dim_size = rowptr.size(0) - 1 |
| 20 |
|
| 21 |
for size in sizes[1:]: |
| 22 |
try: |
| 23 |
x = torch.randn((dim_size, size), device=args.device) |
| 24 |
x = x.squeeze(-1) if size == 1 else x |
| 25 |
|
| 26 |
out1 = x.index_select(0, row) |
| 27 |
out2 = gather_coo(x, row) |
| 28 |
out3 = gather_csr(x, rowptr) |
| 29 |
|
| 30 |
assert torch.allclose(out1, out2, atol=1e-4) |
| 31 |
assert torch.allclose(out1, out3, atol=1e-4) |
| 32 |
except RuntimeError as e: |
| 33 |
if 'out of memory' not in str(e): |
| 34 |
raise RuntimeError(e) |
| 35 |
torch.cuda.empty_cache() |
| 36 |
|
| 37 |
|
| 38 |
def time_func(func, x): |
| 39 |
try: |
| 40 |
if torch.cuda.is_available(): |
| 41 |
torch.cuda.synchronize() |
| 42 |
t = time.perf_counter() |
| 43 |
|
| 44 |
if not args.with_backward: |
| 45 |
with torch.no_grad(): |
| 46 |
for _ in range(iters): |
| 47 |
func(x) |
| 48 |
else: |
| 49 |
x = x.requires_grad_() |
| 50 |
for _ in range(iters): |
| 51 |
out = func(x) |
| 52 |
torch.autograd.grad(out, x, out, only_inputs=True) |
| 53 |
|
| 54 |
if torch.cuda.is_available(): |
| 55 |
torch.cuda.synchronize() |
| 56 |
return time.perf_counter() - t |
| 57 |
except RuntimeError as e: |
| 58 |
if 'out of memory' not in str(e): |
| 59 |
raise RuntimeError(e) |
| 60 |
torch.cuda.empty_cache() |
| 61 |
return float('inf') |
| 62 |
|
| 63 |
|
| 64 |
def timing(dataset): |
| 65 |
group, name = dataset |
| 66 |
mat = loadmat(f'{name}.mat')['Problem'][0][0][2].tocsr() |
| 67 |
rowptr = torch.from_numpy(mat.indptr).to(args.device, torch.long) |
| 68 |
row = torch.from_numpy(mat.tocoo().row).to(args.device, torch.long) |
| 69 |
dim_size = rowptr.size(0) - 1 |
| 70 |
avg_row_len = row.size(0) / dim_size |
| 71 |
|
| 72 |
def select(x): |
| 73 |
return x.index_select(0, row) |
| 74 |
|
| 75 |
def gather(x): |
| 76 |
return x.gather(0, row.view(-1, 1).expand(-1, x.size(1))) |
| 77 |
|
| 78 |
def gat_coo(x): |
| 79 |
return gather_coo(x, row) |
| 80 |
|
| 81 |
def gat_csr(x): |
| 82 |
return gather_csr(x, rowptr) |
| 83 |
|
| 84 |
t1, t2, t3, t4 = [], [], [], [] |
| 85 |
for size in sizes: |
| 86 |
try: |
| 87 |
x = torch.randn((dim_size, size), device=args.device) |
| 88 |
|
| 89 |
t1 += [time_func(select, x)] |
| 90 |
t2 += [time_func(gather, x)] |
| 91 |
t3 += [time_func(gat_coo, x)] |
| 92 |
t4 += [time_func(gat_csr, x)] |
| 93 |
|
| 94 |
del x |
| 95 |
|
| 96 |
except RuntimeError as e: |
| 97 |
if 'out of memory' not in str(e): |
| 98 |
raise RuntimeError(e) |
| 99 |
torch.cuda.empty_cache() |
| 100 |
for t in (t1, t2, t3, t4): |
| 101 |
t.append(float('inf')) |
| 102 |
|
| 103 |
ts = torch.tensor([t1, t2, t3, t4]) |
| 104 |
winner = torch.zeros_like(ts, dtype=torch.bool) |
| 105 |
winner[ts.argmin(dim=0), torch.arange(len(sizes))] = 1 |
| 106 |
winner = winner.tolist() |
| 107 |
|
| 108 |
name = f'{group}/{name}' |
| 109 |
print(f'{bold(name)} (avg row length: {avg_row_len:.2f}):') |
| 110 |
print('\t'.join([' '] + [f'{size:>5}' for size in sizes])) |
| 111 |
print('\t'.join([bold('SELECT ')] + |
| 112 |
[bold(f'{t:.5f}', f) for t, f in zip(t1, winner[0])])) |
| 113 |
print('\t'.join([bold('GAT ')] + |
| 114 |
[bold(f'{t:.5f}', f) for t, f in zip(t2, winner[1])])) |
| 115 |
print('\t'.join([bold('GAT_COO')] + |
| 116 |
[bold(f'{t:.5f}', f) for t, f in zip(t3, winner[2])])) |
| 117 |
print('\t'.join([bold('GAT_CSR')] + |
| 118 |
[bold(f'{t:.5f}', f) for t, f in zip(t4, winner[3])])) |
| 119 |
print() |
| 120 |
|
| 121 |
|
| 122 |
if __name__ == '__main__': |
| 123 |
parser = argparse.ArgumentParser() |
| 124 |
parser.add_argument('--with_backward', action='store_true') |
| 125 |
parser.add_argument('--device', type=str, default='cuda') |
| 126 |
args = parser.parse_args() |
| 127 |
iters = 1 if args.device == 'cpu' else 20 |
| 128 |
sizes = [1, 16, 32, 64, 128, 256, 512] |
| 129 |
sizes = sizes[:3] if args.device == 'cpu' else sizes |
| 130 |
|
| 131 |
for _ in range(10): # Warmup. |
| 132 |
torch.randn(100, 100, device=args.device).sum() |
| 133 |
for dataset in itertools.chain(short_rows, long_rows): |
| 134 |
download(dataset) |
| 135 |
correctness(dataset) |
| 136 |
timing(dataset) |