forked from pytorch/pytorch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
59 lines (43 loc) · 1.48 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import functools
import operator
import random
import time
import numpy as np
import torch
# shim for torch.cuda.Event when running on cpu
class Event:
def __init__(self, enable_timing):
pass
def record(self):
self.time = time.perf_counter()
def elapsed_time(self, end_event):
assert isinstance(end_event, Event)
return end_event.time - self.time
def gen_sparse_csr(shape, nnz):
fill_value = 0
total_values = functools.reduce(operator.mul, shape, 1)
dense = np.random.randn(total_values)
fills = random.sample(list(range(total_values)), total_values - nnz)
for f in fills:
dense[f] = fill_value
dense = torch.from_numpy(dense.reshape(shape))
return dense.to_sparse_csr()
def gen_sparse_coo(shape, nnz):
dense = np.random.randn(*shape)
values = []
indices = [[], []]
for n in range(nnz):
row = random.randint(0, shape[0] - 1)
col = random.randint(0, shape[1] - 1)
indices[0].append(row)
indices[1].append(col)
values.append(dense[row, col])
return torch.sparse_coo_tensor(indices, values, size=shape)
def gen_sparse_coo_and_csr(shape, nnz):
total_values = functools.reduce(operator.mul, shape, 1)
dense = np.random.randn(total_values)
fills = random.sample(list(range(total_values)), total_values - nnz)
for f in fills:
dense[f] = 0
dense = torch.from_numpy(dense.reshape(shape))
return dense.to_sparse(), dense.to_sparse_csr()