Commit cca4e78e authored by rusty1s's avatar rusty1s
Browse files

update benchmark

parent d86c6ff1
...@@ -6,7 +6,6 @@ import argparse ...@@ -6,7 +6,6 @@ import argparse
import os.path as osp import os.path as osp
import torch import torch
import torch_sparse # noqa
from scipy.io import loadmat from scipy.io import loadmat
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
...@@ -45,87 +44,39 @@ def time_func(matrix, op, duration=5.0, warmup=1.0): ...@@ -45,87 +44,39 @@ def time_func(matrix, op, duration=5.0, warmup=1.0):
t = time.time() t = time.time()
while (time.time() - t) < warmup: while (time.time() - t) < warmup:
op(matrix) op(matrix)
torch.cuda.synchronize()
count = 0 count = 0
t = time.time() t = time.time()
while (time.time() - t) < duration: while (time.time() - t) < duration:
op(matrix) op(matrix)
count += 1 count += 1
torch.cuda.synchronize()
return (time.time() - t) / count return (time.time() - t) / count
def op1(matrix): def bucketize(matrix):
# https://github.com/pytorch/pytorch/blob/3a777b67926c5f02bc287b25e572c521d6d11fb0/torch/_tensor.py#L928-L940
row_indices = matrix.indices()[0] row_indices = matrix.indices()[0]
ro = [0] arange = torch.arange(matrix.size(0) + 1, device=row_indices.device)
i = 0 return torch.bucketize(arange, row_indices)
for irow in range(matrix.shape[0]):
while i < row_indices.shape[0] and row_indices[i] == irow:
i += 1
ro.append(i)
torch.tensor(ro, dtype=torch.long)
def op2(matrix): def convert_coo_to_csr(matrix):
# https://github.com/pearu/gcs/blob/b54ba0cba9c853b797274ef26b6c42386f2cafa3/gcs/storage.py#L24-L45
row_indices = matrix.indices()[0] row_indices = matrix.indices()[0]
nnz = row_indices.shape[0] return torch._convert_coo_to_csr(row_indices, matrix.size(0) + 1)
compressed = [0] * (matrix.shape[0] + 1)
k = 1
last_index = 0
for i in range(nnz):
index = row_indices[i]
for n in range(last_index, index):
compressed[k] = i
k += 1
last_index = index
for n in range(k, matrix.shape[0] + 1): for device in ['cpu', 'cuda']:
compressed[n] = nnz print('DEVICE:', device)
for group, name in matrices:
matrix = get_torch_sparse_coo_tensor(args.root, group, name)
matrix = matrix.to(device)
torch.tensor(compressed, dtype=torch.long) out1 = bucketize(matrix)
out2 = convert_coo_to_csr(matrix)
assert out1.tolist() == out2.tolist()
t = time_func(matrix, bucketize, duration=5.0, warmup=1.0)
def op3(matrix): print('old impl:', t)
row_indices = matrix.indices()[0] t = time_func(matrix, convert_coo_to_csr, duration=5.0, warmup=1.0)
print('new impl:', t)
bincount = torch.bincount(row_indices) print()
out = torch.empty((matrix.shape[0] + 1), dtype=torch.long)
out[0] = 0
torch.cumsum(bincount, dim=0, out=out[1:])
out[bincount.numel() + 1:] = row_indices.shape[0]
def op4(matrix):
row_indices = matrix.indices()[0]
torch.ops.torch_sparse.ind2ptr(row_indices, matrix.shape[0])
def op5(matrix):
row_indices = matrix.indices()[0]
arange = torch.arange(matrix.size(0) + 1)
torch.searchsorted(row_indices, arange)
def op6(matrix):
row_indices = matrix.indices()[0]
arange = torch.arange(matrix.size(0) + 1)
torch.bucketize(arange, row_indices)
for group, name in matrices:
matrix = get_torch_sparse_coo_tensor(args.root, group, name)
duration = time_func(matrix, op1, duration=5.0, warmup=1.0)
print('current:', duration)
duration = time_func(matrix, op2, duration=5.0, warmup=1.0)
print('compressed indices:', duration)
duration = time_func(matrix, op3, duration=5.0, warmup=1.0)
print('vectorized:', duration)
duration = time_func(matrix, op4, duration=5.0, warmup=1.0)
print('torch-sparse:', duration)
duration = time_func(matrix, op5, duration=5.0, warmup=1.0)
print('searchsorted:', duration)
duration = time_func(matrix, op6, duration=5.0, warmup=1.0)
print('bucketize:', duration)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment