-
Notifications
You must be signed in to change notification settings - Fork 2
/
calc_timing.py
293 lines (184 loc) · 9.67 KB
/
calc_timing.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
import torch
import time
from configs import Configs
from utils.plotting import get_projections, get_plots, MAP, offset, layer_bottom_pos, cell_thickness, Xmax, Xmin, Zmax, Zmin
from tqdm import tqdm
import numpy as np
import torch.nn as nn
from pyro.nn import ConditionalDenseNN, DenseNN, ConditionalAutoRegressiveNN
import pyro.distributions as dist
import pyro.distributions.transforms as T
from custom_pyro import ConditionalAffineCouplingTanH
from models.CaloClouds import *
########################
### PARAMS #############
########################
cfg = Configs()
cfg.device = 'cpu'
#use single thread
torch.set_num_threads(1)
# min and max energy of the generated events
min_e = 10
max_e = 100
num = 2000 # total number of generated events
bs = 1 # batch size # optimized: bs=64 for GPU, bs=512 for CPU (multi-threaded), bs=1 for CPU (single-threaded)
iterations = 5 # number of iterations for timing
########################
########################
########################
def main(cfg, min_e, max_e, num, bs, iterations):
num_blocks = 10
flow, distribution = compile_HybridTanH_model(num_blocks,
num_inputs=32, ### when 'condioning' on additional Esum, Nhits etc add them on as inputs rather than
num_cond_inputs=1, device=cfg.device) # num_cond_inputs
checkpoint = torch.load('/beegfs/desy/user/akorol/chekpoints/ECFlow/EFlow+CFlow_138.pth')
flow.load_state_dict(checkpoint['model'])
flow.eval().to(cfg.device)
cfg.sched_mode = 'quardatic'
model = CaloClouds(cfg).to(cfg.device)
checkpoint = torch.load('./logs/point-cloud/AllCond_epicVAE_nFlow_PointDiff_100s_MSE_loss_smired_possitions_quardatic2023_04_06__16_34_39/ckpt_0.000000_837000.pt') # quadratic
model.load_state_dict(checkpoint['state_dict'])
model.eval()
times_per_shower = []
for _ in range(iterations):
s_t = time.time()
fake_showers = gen_showers_batch(distribution, model, min_e, max_e, num, bs)
t = time.time() - s_t
print(fake_showers.shape)
print('total time (seconds): ', t)
print('time per shower (seconds): ', t / num)
times_per_shower.append(t / num)
print('mean time per shower (seconds): ', np.mean(times_per_shower))
print('std time per shower (seconds): ', np.std(times_per_shower))
def compile_HybridTanH_model(num_blocks, num_inputs, num_cond_inputs, device):
# the latent space distribution: choosing a 2-dim Gaussian
base_dist = dist.Normal(torch.zeros(num_inputs).to(device), torch.ones(num_inputs).to(device))
input_dim = num_inputs
count_bins = 8
transforms = []
transforms2 = []
input_dim = num_inputs
split_dim = num_inputs//2
param_dims1 = [input_dim-split_dim, input_dim-split_dim]
param_dims2 = [input_dim * count_bins, input_dim * count_bins, input_dim * (count_bins - 1), input_dim * count_bins]
torch.manual_seed(42)
for _ in range(num_blocks):
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
hypernet = DenseNN(num_cond_inputs, [input_dim*4, input_dim*4], param_dims2)
ctf = T.ConditionalSpline(hypernet, input_dim, count_bins)
transforms2.append(ctf)
transforms.append(ctf)
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
hypernet = ConditionalDenseNN(split_dim, num_cond_inputs, [input_dim*10, input_dim*10], param_dims1)
ctf = ConditionalAffineCouplingTanH(split_dim, hypernet)
transforms2.append(ctf)
transforms.append(ctf)
perm = torch.randperm(input_dim, dtype=torch.long).to(device)
ff = T.Permute(perm)
transforms.append(ff)
modules = nn.ModuleList(transforms2)
flow_dist = dist.ConditionalTransformedDistribution(base_dist, transforms)
return modules, flow_dist
def get_scale_factor(num_clusters):
coef_real = np.array([ 2.57988645e-09, -2.94056522e-05, 3.42194568e-01, 5.34968378e+01])
coef_fake = np.array([ 3.85057207e-09, -4.16463897e-05, 4.19800713e-01, 5.82246858e+01])
poly_fn_real = np.poly1d(coef_real)
poly_fn_fake = np.poly1d(coef_fake)
scale_factor = poly_fn_real(num_clusters) / poly_fn_fake(num_clusters)
return scale_factor
def get_shower(model, num_points, energy, cond_N, bs=1):
e = torch.ones((bs, 1), device=cfg.device) * energy
n = torch.ones((bs, 1), device=cfg.device) * cond_N
if cfg.norm_cond:
e = e / 100 * 2 -1 # max incident energy: 100 GeV
n = n / cfg.max_points * 2 - 1
cond_feats = torch.cat([e, n], -1)
with torch.no_grad():
fake_shower = model.sample(cond_feats, num_points, cfg.flexibility)
return fake_shower
# batch inference
def gen_showers_batch(distribution, model, e_min, e_max, num=2000, bs=32):
cond_E = torch.FloatTensor(num, 1).uniform_(e_min, e_max).to(cfg.device)
samples = distribution.condition(cond_E/100).sample(torch.Size([num, ])).cpu().numpy()
energies = (samples[:, 0] * 2.5 * 1000).reshape(num, 1)
clusters_per_layer_gen = samples[:, 2:] * 400
clusters_per_layer_gen[clusters_per_layer_gen < 0] = 0
scale_factor = get_scale_factor(clusters_per_layer_gen.sum(axis=1))
scale_factor = np.expand_dims(scale_factor, axis=1)
clusters_per_layer_gen = (clusters_per_layer_gen * scale_factor).astype(int)
# sort by number of clusters for better batching and faster inference
mask = np.argsort(clusters_per_layer_gen.sum(axis=1))
clusters_per_layer_gen = clusters_per_layer_gen[mask]
energies = energies[mask]
cond_E = cond_E[mask]
fake_showers_list = []
for evt_id in tqdm(range(0, num, bs)):
if (num - evt_id) < bs:
bs = num - evt_id
# convert clusters_per_layer_gen to a fractions of points in the layer out of sum(points in the layer) of event
# multuply clusters_per_layer_gen by corrected tottal num of points
hits_per_layer_all = clusters_per_layer_gen[evt_id : evt_id+bs] # shape (bs, num_layers)
max_num_clusters = hits_per_layer_all.sum(axis=1).max()
cond_N = torch.Tensor(hits_per_layer_all.sum(axis=1)).to(cfg.device).unsqueeze(-1)
fs = get_shower(model, max_num_clusters, cond_E[evt_id : evt_id+bs], cond_N, bs=bs)
fs = fs.cpu().numpy()\
fs[:, :, -1][fs[:, :, -1] < 0] = 0 # setting negative energies to 0
fs[:, :, -1] = fs[:, :, -1] / fs[:, :, -1].sum(axis=1).reshape(bs, 1) * energies[evt_id : evt_id+bs] # energy rescaling to predicted e_sum
length = 6000 - fs.shape[1]
fs = np.concatenate((fs, np.zeros((bs, length, 4))), axis=1)
fake_showers_list.append(fs)
fake_showers = np.vstack(fake_showers_list)
# y calibration
for i, hits_per_layer in enumerate(clusters_per_layer_gen):
n_hits_to_concat = 6000 - hits_per_layer.sum()
y_flow = np.repeat(layer_bottom_pos+cell_thickness/2, hits_per_layer)
y_flow = np.concatenate([y_flow, np.zeros(n_hits_to_concat)])
mask = np.concatenate([ np.ones( hits_per_layer.sum() ), np.zeros( n_hits_to_concat ) ])
fake_showers[i, :, 1][mask == 0] = 10
idx_dm = np.argsort(fake_showers[i, :, 1])
fake_showers[i, :, 1][idx_dm] = y_flow
fake_showers[i, :, :][y_flow==0] = 0
fake_showers = np.moveaxis(fake_showers, -1, -2)
fake_showers[:, 0, :] = (fake_showers[:, 0, :] + 1) / 2
fake_showers[:, 2, :] = (fake_showers[:, 2, :] + 1) / 2
fake_showers[:, 0] = fake_showers[:, 0] * (Xmin-Xmax) + Xmax
fake_showers[:, 2] = fake_showers[:, 2] * (Zmin-Zmax) + Zmax
return fake_showers
if __name__ == '__main__':
main(cfg, min_e, max_e, num, bs, iterations)