-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcat.py
66 lines (51 loc) · 1.67 KB
/
cat.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
60
61
62
63
64
65
66
import torch
from time import time
torch.manual_seed(0)
warmups = 100 # iterations
total_times = 10 # seconds
# cat(inputs, dim)
# number of inputs: N
# input size: [H, W]
def run_single_test(H, W, N, dim, contiguous=True):
inputs = []
for k in range(N):
if contiguous:
inputs.append(torch.randn(H, W))
else:
inputs.append(torch.randn(H, W + 16).narrow(1, 0, W))
for i in range(warmups):
output = torch.cat(inputs, dim)
ttime = 0
iters = 0
while (ttime < total_times):
t1 = time()
output = torch.cat(inputs, dim)
t2 = time()
ttime = ttime + t2 - t1
iters = iters + 1
throughput = H * W * N * 4 * iters / ttime * 1e-9
print("input size: [{}, {}]; input number: [{}]; {}; thoughput: GB/s = {:.3f}".format(
H, W, N, 'contiguous' if contiguous else 'non-contiguous', throughput))
def benchmark():
for contiguous in [True, False]:
for ninputs in [2, 16]:
run_single_test(64, 1000, ninputs, 0, contiguous)
run_single_test(128, 1000, ninputs, 0, contiguous)
run_single_test(64, 10000, ninputs, 0, contiguous)
run_single_test(128, 10000, ninputs, 0, contiguous)
benchmark()
def validate(use_bfloat=False):
t1 = torch.randn(2, 5)
t2 = torch.randn(2, 5)
t3 = torch.randn(3, 5)
if use_bfloat:
t1, t2, t3 = t1.bfloat16(), t2.bfloat16(), t3.bfloat16()
o1 = torch.cat([t1, t2], 0)
o2 = torch.cat([t1, t3], 0)
print('tensor 1: ', t1)
print('tensor 2: ', t2)
print('tensor 3: ', t3)
print('cat 1&2: ', o1)
print('cat 1&3: ', o2)
#validate()
#validate(True)