-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark.py
More file actions
188 lines (157 loc) · 7.83 KB
/
benchmark.py
File metadata and controls
188 lines (157 loc) · 7.83 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import torch
from e3nn import o3
from coo_tp import COOTensorProduct
from einsum_tp import EinsumTensorProduct
from cuda_tp import CudaTensorProduct
import cuequivariance as cueq
import cuequivariance_torch as cueq_torch
import openequivariance as oeq
from openequivariance.benchmark.tpp_creation_utils import FullyConnectedTPProblem as FCTPP
from openequivariance.benchmark.tpp_creation_utils import ChannelwiseTPP as CTPP
import time
torch.set_default_device('cuda')
BATCH_SIZE = 50000
NUM_WARMUP_ROUNDS = 10
NUM_TEST_ROUNDS = 100
ERROR_TOLERANCE = 1e-10
configs = [
('tetris-poly-1', '1x0e + 1x1o + 1x2e + 1x3o', '1x0e + 1x1o + 1x2e + 1x3o', '64x0e + 24x1e + 24x1o + 16x2e + 16x2o'),
('tetris-poly-2', '64x0e + 24x1e + 24x1o + 16x2e + 16x2o', '1x0e + 1x1o + 1x2e', '1x0o + 6x0e'),
('DiffDock-L=1', '10x1o + 10x1e + 48x0e + 48x0o', '1x0e + 1x1o', '10x1o + 10x1e + 48x0e + 48x0o'),
('DiffDock-L=2', '10x1o + 10x1e + 48x0e + 48x0o', '1x0e + 1x1o + 1x2e', '10x1o + 10x1e + 48x0e + 48x0o'),
('mace-large', '128x0e + 128x1o + 128x2e', '1x0e + 1x1o + 1x2e + 1x3o', '128x0e + 128x1o + 128x2e + 128x3o'),
('mace-medium', '128x0e + 128x1o', '1x0e + 1x1o + 1x2e + 1x3o', '128x0e + 128x1o + 128x2e'),
('nequip-lips', '32x0o + 32x0e + 32x1o + 32x1e + 32x2o + 32x2e', '1x0e + 1x1o + 1x2e', '32x0o + 32x0e + 32x1o + 32x1e + 32x2o + 32x2e'),
('nequip-revmd17-aspirin', '32x0o + 32x0e + 32x1o + 32x1e + 32x2o + 32x2e', '1x0e + 1x1o', '64x0o + 64x0e + 64x1o + 64x1e'),
('nequip-revmd17-toluene', '64x0o + 64x0e + 64x1o + 64x1e + 64x2o + 64x2e', '1x0e + 1x1o + 1x2e', '64x0o + 64x0e + 64x1o + 64x1e + 64x2o + 64x2e'),
# nequip-revmd17-benzene triggers some sort of bug.
#('nequip-revmd17-benzene', '64x0o + 64x0e + 64x1o + 64x1e + 64x2o + 64x2e + 64x3o + 64x3e', '1x0e + 1x1o + 1x2e + 1x3o', '64x0o + 64x0e + 64x1o + 64x1e + 64x2o + 64x2e + 64x3o + 64x3e'),
('nequip-water', '32x0o + 32x0e + 32x1o + 32x1e', '1x0e + 1x1o', '32x0o + 32x0e + 32x1o + 32x1e'),
]
def time_test(callback, name):
for i in range(0, NUM_WARMUP_ROUNDS):
callback()
torch.cuda.synchronize()
start = time.time()
for i in range(0, NUM_TEST_ROUNDS):
callback()
torch.cuda.synchronize()
end = time.time()
throughput = BATCH_SIZE * NUM_TEST_ROUNDS / (end - start)
print('{} throughput: {:.2E}'.format(name, throughput))
def full_tp_benchmark():
print('==== Natural tensor product benchmarks ====')
for config in configs:
print(config[0])
irreps_in1 = o3.Irreps(config[1]).sort().irreps.simplify()
irreps_in2 = o3.Irreps(config[2]).sort().irreps.simplify()
e3nn_tp = o3.FullTensorProduct(irreps_in1, irreps_in2)
e3nn_tp.compile()
test_tp = CudaTensorProduct(irreps_in1, irreps_in2)
in1 = torch.randn((BATCH_SIZE, irreps_in1.dim))
in2 = torch.randn((BATCH_SIZE, irreps_in2.dim))
assert(float(torch.sum((e3nn_tp(in1, in2) - test_tp(in1, in2))**2)) / BATCH_SIZE / irreps_in1.dim / irreps_in2.dim < ERROR_TOLERANCE)
def e3nn_cb():
e3nn_tp(in1, in2)
time_test(e3nn_cb, 'e3nn')
def test_cb():
test_tp(in1, in2)
time_test(test_cb, 'test')
def fc_tp_benchmark():
print('==== Fully connected benchmarks ====')
for config in configs:
print(config[0])
irreps_in1 = o3.Irreps(config[1]).sort().irreps.simplify()
irreps_in2 = o3.Irreps(config[2]).sort().irreps.simplify()
irreps_out = o3.Irreps(config[3]).sort().irreps.simplify()
start = time.time()
e3nn_tp = o3.FullyConnectedTensorProduct(irreps_in1, irreps_in2, irreps_out)
e3nn_tp.compile()
print('e3nn compile time: {:.2E}s'.format(time.time() - start))
start = time.time()
test_tp = CudaTensorProduct(irreps_in1, irreps_in2, irreps_out, 'full')
print('test compile time: {:.2E}s'.format(time.time() - start))
start = time.time()
cueq_tp = cueq_torch.FullyConnectedTensorProduct(cueq.Irreps(cueq.O3, config[1]),
cueq.Irreps(cueq.O3, config[2]),
cueq.Irreps(cueq.O3, config[3]),
device='cuda', internal_weights=False, shared_weights=True)
print('cueq compile time: {:.2E}s'.format(time.time() - start))
start = time.time()
oeq_tp = FCTPP(irreps_in1, irreps_in2, irreps_out)
oeq_tp = oeq.TensorProduct(oeq_tp, torch_op=True)
print('oeq compile time: {:.2E}s'.format(time.time() - start))
cueq_ir_mul_tp = cueq_torch.FullyConnectedTensorProduct(cueq.Irreps(cueq.O3, config[1]),
cueq.Irreps(cueq.O3, config[2]),
cueq.Irreps(cueq.O3, config[3]),
layout=cueq.ir_mul, device='cuda',
internal_weights=False, shared_weights=True)
in1 = torch.randn((BATCH_SIZE, irreps_in1.dim))
in2 = torch.randn((BATCH_SIZE, irreps_in2.dim))
weights = torch.tensor(e3nn_tp.weight).reshape((1, test_tp.num_weights))
weights_perm = test_tp.convert_weights(weights.flatten())
assert(e3nn_tp(in1, in2).shape == test_tp(in1, in2, weights.flatten()).shape)
assert(float(torch.sum((e3nn_tp(in1, in2) - test_tp(in1, in2, weights_perm))**2)) / BATCH_SIZE / irreps_out.dim < ERROR_TOLERANCE)
def e3nn_cb():
e3nn_tp(in1, in2)
time_test(e3nn_cb, 'e3nn')
def oeq_cb():
oeq_tp(in1, in2, weights)
time_test(oeq_cb, 'oeq')
def cueq_cb():
cueq_tp(in1, in2, weights)
time_test(cueq_cb, 'cueq')
def cueq_ir_mul_cb():
cueq_ir_mul_tp(in1, in2, weights)
time_test(cueq_ir_mul_cb, 'cueq (ir_mul layout)')
def test_prepermute_cb():
test_tp(in1, in2, weights_perm)
time_test(test_prepermute_cb, 'test')
def channel_tp_benchmark():
print('==== Channel Wise benchmarks ====')
for config in configs:
print(config[0])
irreps_in1 = o3.Irreps(config[1]).sort().irreps.simplify()
irreps_in2 = o3.Irreps(config[2]).sort().irreps.simplify()
start = time.time()
test_tp = CudaTensorProduct(irreps_in1, irreps_in2, irreps_out=None,
connection_mode='channel')
print('test compile time: {:.2E}s'.format(time.time() - start))
cueq_tp = cueq_torch.ChannelWiseTensorProduct(cueq.Irreps(cueq.O3, config[1]),
cueq.Irreps(cueq.O3, config[2]),
device='cuda', shared_weights=True)
print('cueq compile time: {:.2E}s'.format(time.time() - start))
irreps_out = o3.Irreps(str(cueq_tp.irreps_out)).sort().irreps.simplify()
start = time.time()
oeq_tp = CTPP(irreps_in1, irreps_in2, irreps_out)
oeq_tp = oeq.TensorProduct(oeq_tp, torch_op=True)
print('oeq compile time: {:.2E}s'.format(time.time() - start))
cueq_ir_mul_tp = cueq_torch.ChannelWiseTensorProduct(cueq.Irreps(cueq.O3, config[1]),
cueq.Irreps(cueq.O3, config[2]),
layout=cueq.ir_mul,
device='cuda', shared_weights=True)
in1 = torch.randn((BATCH_SIZE, irreps_in1.dim))
in2 = torch.randn((BATCH_SIZE, irreps_in2.dim))
weights = cueq_tp.weight.flatten()
weights_test = weights
weights_oeq = weights.reshape((1, -1)).repeat(BATCH_SIZE, 1)
def test_cb():
test_tp(in1, in2, weights_test)
time_test(test_cb, 'test')
def cueq_cb():
cueq_tp(in1, in2)
time_test(cueq_cb, 'cueq')
def cueq_ir_mul_cb():
cueq_ir_mul_tp(in1, in2)
time_test(cueq_ir_mul_cb, 'cueq (ir_mul layout)')
def oeq_cb():
oeq_tp(in1, in2, weights_oeq)
time_test(oeq_cb, 'oeq')
def roofline():
torch.einsum('bi,bj->bij', in1, in2)
time_test(roofline, 'roofline')
full_tp_benchmark()
print()
fc_tp_benchmark()
print()
channel_tp_benchmark()