-
Notifications
You must be signed in to change notification settings - Fork 2
/
benchmark.py
312 lines (280 loc) · 15.2 KB
/
benchmark.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
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import argparse
from typing import Callable, Dict, Tuple
import time
import numpy as np
import torch
from torch import nn
from kan import create_dataset
from kan import KAN as pyKAN
from efficient_kan import KAN as effKAN
from FourierKAN.fftKAN import NaiveFourierKANLayer
from ChebyKAN.ChebyKANLayer import ChebyKANLayer
from fastkan import FastKAN
from faster_kan.fasterkan import FasterKAN
from rbf_kan.RBF_KAN import RBFKAN
from wav_kan.KAN import KAN as WavKAN
class MLP(nn.Module):
def __init__(self, layers: Tuple[int, int, int], device: str):
super().__init__()
self.layer1 = nn.Linear(layers[0], layers[1], device=device)
self.layer2 = nn.Linear(layers[1], layers[2], device=device)
def forward(self, x: torch.Tensor):
x = self.layer1(x)
x = nn.functional.relu(x)
x = self.layer2(x)
x = nn.functional.sigmoid(x)
return x
class FourierKAN(nn.Module):
def __init__(self, layers: Tuple[int, int, int], gridsize: int, device: str):
super().__init__()
self.layer1 = NaiveFourierKANLayer(layers[0], layers[1], gridsize=gridsize).to(device)
self.layer2 = NaiveFourierKANLayer(layers[1], layers[2], gridsize=gridsize).to(device)
def forward(self, x: torch.Tensor):
x = self.layer1(x)
x = self.layer2(x)
return x
class ChebyKAN(nn.Module):
def __init__(self, layers: Tuple[int, int, int], device: str):
super().__init__()
self.layer1 = ChebyKANLayer(layers[0], layers[1], degree=9).to(device)
self.layer2 = ChebyKANLayer(layers[1], layers[2], degree=9).to(device)
def forward(self, x: torch.Tensor):
x = self.layer1(x)
x = self.layer2(x)
return x
def benchmark(
dataset: Dict[str, torch.Tensor],
device: str,
bs: int,
loss_fn: Callable[[torch.Tensor, torch.Tensor], torch.Tensor],
model: nn.Module,
reps: int
) -> Dict[str, float]:
forward_times = []
backward_times = []
forward_mems = []
backward_mems = []
for k in range(1 reps):
train_id = np.random.choice(dataset['train_input'].shape[0], bs, replace=False)
tensor_input = dataset['train_input'][train_id]
tensor_input = tensor_input.to(device)
tensor_output = dataset['train_label'][train_id]
tensor_output = tensor_output.to(device)
if device == 'cpu':
t0 = time.time()
pred = model(tensor_input)
t1 = time.time()
if k > 0:
forward_times.append((t1 - t0) * 1000)
train_loss = loss_fn(pred, tensor_output)
t2 = time.time()
train_loss.backward()
t3 = time.time()
if k > 0:
backward_times.append((t3 - t2) * 1000)
elif device == 'cuda':
torch.cuda.reset_peak_memory_stats()
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
pred = model(tensor_input)
end.record()
torch.cuda.synchronize()
if k > 0:
forward_times.append(start.elapsed_time(end))
forward_mems.append(torch.cuda.max_memory_allocated())
train_loss = loss_fn(pred, tensor_output)
torch.cuda.reset_peak_memory_stats()
start = torch.cuda.Event(enable_timing=True)
end = torch.cuda.Event(enable_timing=True)
start.record()
train_loss.backward()
end.record()
torch.cuda.synchronize()
if k > 0:
backward_times.append(start.elapsed_time(end))
backward_mems.append(torch.cuda.max_memory_allocated())
return {
'forward': np.mean(forward_times),
'backward': np.mean(backward_times),
'forward-memory': np.mean(forward_mems) / (1024 ** 3),
'backward-memory': np.mean(backward_mems) / (1024 ** 3),
}
def save_results(t: Dict[str, Dict[str, float]], out_path: str):
maxlen = np.max([len(k) for k in t.keys()])
with open(out_path, 'w') as f:
print(f"{' '*maxlen} | {'forward':>11} | {'backward':>11} | {'forward':>11} | {'backward':>11} | {'num params':>11} | {'num trainable params':>20}", file=f)
print(f"{' '*maxlen} | {'forward':>11} | {'backward':>11} | {'forward':>11} | {'backward':>11} | {'num params':>11} | {'num trainable params':>20}")
print('-'*130, file=f)
print('-'*130)
for key in t.keys():
print(f"{key:<{maxlen}} | {t[key]['forward']:8.2f} ms | {t[key]['backward']:8.2f} ms | {t[key]['forward-memory']:8.2f} GB | {t[key]['backward-memory']:8.2f} GB | {t[key]['params']:>11} | {t[key]['train_params']:>20}", file=f)
print(f"{key:<{maxlen}} | {t[key]['forward']:8.2f} ms | {t[key]['backward']:8.2f} ms | {t[key]['forward-memory']:8.2f} GB | {t[key]['backward-memory']:8.2f} GB | {t[key]['params']:>11} | {t[key]['train_params']:>20}")
def count_params(model: nn.Module) -> Tuple[int, int]:
pytorch_total_params = sum(p.numel() for p in model.parameters())
pytorch_total_params_train = sum(p.numel() for p in model.parameters() if p.requires_grad)
return pytorch_total_params, pytorch_total_params_train
def _create_parser():
parser = argparse.ArgumentParser()
parser.add_argument('--output-path', default='times.txt', type=str)
parser.add_argument('--method', choices=[
'pykan', 'efficientkan', 'fourierkan',
'fusedfourierkan', 'chebykan', 'cufkan',
'fast-kan', 'faster-kan', 'rbf-kan',
'wav-kan', 'mlp', 'all'
],
type=str)
parser.add_argument('--batch-size', type=int, default=32)
parser.add_argument('--inp-size', type=int, default=2, help='The dimension of the input variables.')
parser.add_argument('--hid-size', type=int, default=50, help='The dimension of the hidden layer.')
parser.add_argument('--reps', type=int, default=10, help='Number of times to repeat execution and average.')
parser.add_argument('--just-cuda', action='store_true', help='Whether to only execute the cuda version.')
return parser
def main():
parser = _create_parser()
args = parser.parse_args()
f = lambda x: torch.exp(torch.sin(torch.pi*x[:,[0]]) x[:,[1]]**2)
dataset = create_dataset(
f,
n_var=args.inp_size,
ranges = [-1,1],
train_num=1000,
test_num=1000,
normalize_input=False,
normalize_label=False,
device='cpu',
seed=0
)
loss_fn = lambda x, y: torch.mean((x - y) ** 2)
res = {}
if args.method == 'pykan':
if not args.just_cuda:
model = pyKAN(width=[args.inp_size, args.hid_size, 1], grid=5, k=3, seed=0)
model.to('cpu')
res['pykan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['pykan-cpu']['params'], res['pykan-cpu']['train_params'] = count_params(model)
model = pyKAN(width=[args.inp_size, args.hid_size, 1], grid=5, k=3, seed=0, device='cuda') # For gpu pass device here
res['pykan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['pykan-gpu']['params'], res['pykan-gpu']['train_params'] = count_params(model)
if args.method == 'efficientkan' or args.method == 'all':
model = effKAN(layers_hidden=[args.inp_size, args.hid_size, 1], grid_size=5, spline_order=3)
if not args.just_cuda:
model.to('cpu')
res['effkan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['effkan-cpu']['params'], res['effkan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['effkan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['effkan-gpu']['params'], res['effkan-gpu']['train_params'] = count_params(model)
if args.method == 'fourierkan' or args.method == 'all':
model = FourierKAN(layers=[args.inp_size, args.hid_size, 1], gridsize=5, device='cpu')
if not args.just_cuda:
res['fourierkan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['fourierkan-cpu']['params'], res['fourierkan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['fourierkan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['fourierkan-gpu']['params'], res['fourierkan-gpu']['train_params'] = count_params(model)
if args.method == 'fusedfourierkan' or args.method == 'all':
# Installation of this layer is more cumbersome
# Therefore the imports are here so that they are not needed for the other methods
try:
from FusedFourierKAN.FusedFourierKANLayer import FusedFourierKANLayer
class FusedFourierKAN(nn.Module):
def __init__(self, layers: Tuple[int, int, int], gridsize: int, device: str):
super().__init__()
torch.manual_seed(42)
self.layer1 = FusedFourierKANLayer(layers[0], layers[1], gridsize=gridsize).to(device)
self.layer2 = FusedFourierKANLayer(layers[1], layers[2], gridsize=gridsize).to(device)
def forward(self, x: torch.Tensor):
x = self.layer1(x)
x = self.layer2(x)
return x
model = FusedFourierKAN(layers=[args.inp_size, args.hid_size, 1], gridsize=5, device='cpu')
if not args.just_cuda:
res['fusedfourierkan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['fusedfourierkan-cpu']['params'], res['fusedfourierkan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['fusedfourierkan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['fusedfourierkan-gpu']['params'], res['fusedfourierkan-gpu']['train_params'] = count_params(model)
except Exception as e:
print(e)
print('FusedFourierKAN is not properly installed.')
if args.method == 'cufkan' or args.method == 'all':
# Installation of this layer is more cumbersome
# Therefore the imports are here so that they are not needed for the other methods
try:
from cuFKAN import FKANLayer
class FKAN(nn.Module):
def __init__(self, layers: Tuple[int, int, int], gridsize: int, device: str):
super().__init__()
torch.manual_seed(42)
self.layer1 = FKANLayer(layers[0], layers[1], gridsize=gridsize).to(device)
self.layer2 = FKANLayer(layers[1], layers[2], gridsize=gridsize).to(device)
def forward(self, x: torch.Tensor):
x = self.layer1(x)
x = self.layer2(x)
return x
model = FKAN(layers=[args.inp_size, args.hid_size, 1], gridsize=5, device='cpu')
if not args.just_cuda:
res['cufkan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['cufkan-cpu']['params'], res['cufkan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['cufkan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['cufkan-gpu']['params'], res['cufkan-gpu']['train_params'] = count_params(model)
except Exception as e:
print(e)
print('cuFKAN is not properly installed.')
if args.method == 'chebykan' or args.method == 'all':
model = ChebyKAN(layers=[args.inp_size, args.hid_size, 1], device='cpu')
if not args.just_cuda:
res['chebykan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['chebykan-cpu']['params'], res['chebykan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['chebykan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['chebykan-gpu']['params'], res['chebykan-gpu']['train_params'] = count_params(model)
if args.method == 'mlp' or args.method == 'all':
model = MLP(layers=[args.inp_size, args.hid_size * 10, 1], device='cpu')
if not args.just_cuda:
res['mlp-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['mlp-cpu']['params'], res['mlp-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['mlp-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['mlp-gpu']['params'], res['mlp-gpu']['train_params'] = count_params(model)
if args.method == 'fast-kan' or args.method == 'all':
model = FastKAN(layers_hidden=[args.inp_size, args.hid_size, 1], num_grids=9)
model.to('cpu')
if not args.just_cuda:
res['fast-kan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['fast-kan-cpu']['params'], res['fast-kan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['fast-kan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['fast-kan-gpu']['params'], res['fast-kan-gpu']['train_params'] = count_params(model)
if args.method == 'faster-kan' or args.method == 'all':
model = FasterKAN(layers_hidden=[args.inp_size, args.hid_size, 1], num_grids=10)
model.to('cpu')
if not args.just_cuda:
res['faster-kan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['faster-kan-cpu']['params'], res['faster-kan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['faster-kan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['faster-kan-gpu']['params'], res['faster-kan-gpu']['train_params'] = count_params(model)
if args.method == 'rbf-kan' or args.method == 'all':
model = RBFKAN(layers_hidden=[args.inp_size, args.hid_size, 1], num_grids=9)
model.to('cpu')
if not args.just_cuda:
res['rbf-kan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['rbf-kan-cpu']['params'], res['rbf-kan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['rbf-kan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['rbf-kan-gpu']['params'], res['rbf-kan-gpu']['train_params'] = count_params(model)
if args.method == 'wav-kan' or args.method == 'all':
model = WavKAN(layers_hidden=[args.inp_size, 2 * args.hid_size, 1])
model.to('cpu')
if not args.just_cuda:
res['wav-kan-cpu'] = benchmark(dataset, 'cpu', args.batch_size, loss_fn, model, args.reps)
res['wav-kan-cpu']['params'], res['wav-kan-cpu']['train_params'] = count_params(model)
model.to('cuda')
res['wav-kan-gpu'] = benchmark(dataset, 'cuda', args.batch_size, loss_fn, model, args.reps)
res['wav-kan-gpu']['params'], res['wav-kan-gpu']['train_params'] = count_params(model)
save_results(res, args.output_path)
if __name__=='__main__':
main()