forked from Jamesyang2333/SAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_model.py
498 lines (415 loc) · 15.7 KB
/
eval_model.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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
"""Evaluate estimators (Naru or others) on queries."""
import argparse
import collections
import glob
import os
import pickle
import re
import time
import numpy as np
import pandas as pd
import torch
import json
import common
import datasets
import estimators as estimators_lib
import made
# For inference speed.
torch.backends.cudnn.deterministic = False
torch.backends.cudnn.benchmark = True
DEVICE = 'cuda' if torch.cuda.is_available() else 'cpu'
print('Device', DEVICE)
parser = argparse.ArgumentParser()
parser.add_argument('--inference-opts',
action='store_true',
help='Tracing optimization for better latency.')
parser.add_argument('--random-workload', action='store_true', help='is random workload file?')
parser.add_argument('--dataset', type=str, default='dmv', help='Dataset.')
parser.add_argument('--err-csv',
type=str,
default='results.csv',
help='Save result csv to what path?')
parser.add_argument('--glob',
type=str,
help='Checkpoints to glob under models/.')
parser.add_argument('--blacklist',
type=str,
help='Remove some globbed checkpoint files.')
parser.add_argument('--psample',
type=int,
default=1000,
help='# of progressive samples to use per query.')
parser.add_argument(
'--column-masking',
action='store_true',
help='Turn on wildcard skipping. Requires checkpoints be trained with '\
'column masking.')
parser.add_argument('--order',
nargs='+',
type=int,
help='Use a specific order?')
# MADE.
parser.add_argument('--fc-hiddens',
type=int,
default=128,
help='Hidden units in FC.')
parser.add_argument('--layers', type=int, default=2, help='# layers in FC.')
parser.add_argument('--residual', action='store_true', help='ResMade?')
parser.add_argument('--direct-io', action='store_true', help='Do direct IO?')
parser.add_argument(
'--inv-order',
action='store_true',
help='Set this flag iff using MADE and specifying --order. Flag --order'\
'lists natural indices, e.g., [0 2 1] means variable 2 appears second.'\
'MADE, however, is implemented to take in an argument the inverse '\
'semantics (element i indicates the position of variable i). Transformer'\
' does not have this issue and thus should not have this flag on.')
parser.add_argument(
'--input-encoding',
type=str,
default='binary',
help='Input encoding for MADE/ResMADE, {binary, one_hot, embed}.')
parser.add_argument(
'--output-encoding',
type=str,
default='one_hot',
help='Iutput encoding for MADE/ResMADE, {one_hot, embed}. If embed, '
'then input encoding should be set to embed as well.')
args = parser.parse_args()
def InvertOrder(order):
if order is None:
return None
# 'order'[i] maps nat_i -> position of nat_i
# Inverse: position -> natural idx. This it the "true" ordering -- it's how
# heuristic orders are generated + (less crucially) how Transformer works.
nin = len(order)
inv_ordering = [None] * nin
for natural_idx in range(nin):
inv_ordering[order[natural_idx]] = natural_idx
return inv_ordering
def MakeTable():
assert args.dataset in ['dmv', 'census', 'cup98']
if args.dataset == 'dmv':
table = datasets.LoadDmv()
elif args.dataset == 'census':
table = datasets.LoadCensus()
elif args.dataset == 'cup98':
table = datasets.LoadCup98()
oracle_est = estimators_lib.Oracle(table)
return table, None, oracle_est
def ErrorMetric(est_card, card):
if card == 0 and est_card != 0:
return est_card
if card != 0 and est_card == 0:
return card
if card == 0 and est_card == 0:
return 1.0
return max(est_card / card, card / est_card)
def Query(estimators,
do_print=True,
oracle_card=None,
query=None,
table=None,
oracle_est=None):
assert query is not None
cols, ops, vals = query
### Actually estimate the query.
def pprint(*args, **kwargs):
if do_print:
print(*args, **kwargs)
# Actual.
card = oracle_est.Query(cols, ops,
vals) if oracle_card is None else oracle_card
# if card == 0:
# return
pprint('Q(', end='')
for c, o, v in zip(cols, ops, vals):
pprint('{} {} {}, '.format(c.name, o, str(v)), end='')
pprint('): ', end='')
pprint('\n actual {} ({:.3f}%) '.format(card,
card / table.cardinality * 100),
end='')
for est in estimators:
est_card = est.Query(cols, ops, vals)
err = ErrorMetric(est_card, card)
est.AddError(err, est_card, card)
pprint('{} {} (err={:.3f}) '.format(str(est), est_card, err), end='')
pprint()
def RunEpoch(split,
model,
opt,
train_data,
val_data=None,
batch_size=100,
upto=None,
epoch_num=None,
verbose=False,
log_every=10,
return_losses=False,
table_bits=None):
torch.set_grad_enabled(split == 'train')
model.train() if split == 'train' else model.eval()
dataset = train_data if split == 'train' else val_data
losses = []
loader = torch.utils.data.DataLoader(dataset,
batch_size=batch_size,
shuffle=(split == 'train'))
# How many orderings to run for the same batch?
nsamples = 1
if hasattr(model, 'orderings'):
nsamples = len(model.orderings)
for step, (idx, xb) in enumerate(loader):
if split == 'train':
base_lr = 8e-4
for param_group in opt.param_groups:
if args.constant_lr:
lr = args.constant_lr
elif args.warmups:
t = args.warmups
d_model = model.embed_size
global_steps = len(loader) * epoch_num + step + 1
lr = (d_model**-0.5) * min(
(global_steps**-.5), global_steps * (t**-1.5))
else:
lr = 1e-2
param_group['lr'] = lr
if upto and step >= upto:
break
xb = xb.to(DEVICE).to(torch.float32)
# Forward pass, potentially through several orderings.
xbhat = None
model_logits = []
num_orders_to_forward = 1
if split == 'test' and nsamples > 1:
# At test, we want to test the 'true' nll under all orderings.
num_orders_to_forward = nsamples
for i in range(num_orders_to_forward):
if hasattr(model, 'update_masks'):
# We want to update_masks even for first ever batch.
model.update_masks()
model_out = model(xb)
model_logits.append(model_out)
if xbhat is None:
xbhat = torch.zeros_like(model_out)
xbhat += model_out
if xbhat.shape == xb.shape:
if mean:
xb = (xb * std) + mean
loss = F.binary_cross_entropy_with_logits(
xbhat, xb, size_average=False) / xbhat.size()[0]
else:
if model.input_bins is None:
# NOTE: we have to view() it in this order due to the mask
# construction within MADE. The masks there on the output unit
# determine which unit sees what input vars.
xbhat = xbhat.view(-1, model.nout // model.nin, model.nin)
# Equivalent to:
loss = F.cross_entropy(xbhat, xb.long(), reduction='none') \
.sum(-1).mean()
else:
if num_orders_to_forward == 1:
loss = model.nll(xbhat, xb).mean()
else:
# Average across orderings & then across minibatch.
#
# p(x) = 1/N sum_i p_i(x)
# log(p(x)) = log(1/N) + log(sum_i p_i(x))
# = log(1/N) + logsumexp ( log p_i(x) )
# = log(1/N) + logsumexp ( - nll_i (x) )
#
# Used only at test time.
logps = [] # [batch size, num orders]
assert len(model_logits) == num_orders_to_forward, len(
model_logits)
for logits in model_logits:
# Note the minus.
logps.append(-model.nll(logits, xb))
logps = torch.stack(logps, dim=1)
logps = logps.logsumexp(dim=1) + torch.log(
torch.tensor(1.0 / nsamples, device=logps.device))
loss = (-logps).mean()
# print(loss.item())
losses.append(loss.item())
if split == 'train':
opt.zero_grad()
loss.backward()
opt.step()
if verbose:
print('%s epoch average loss: %f' % (split, np.mean(losses)))
if return_losses:
return losses
return np.mean(losses)
def QueryTwosided(estimators,
do_print=True,
oracle_card=None,
query=None,
table=None,
oracle_est=None):
assert query is not None
wildcard_indicator, valid_i_list = query
### Actually estimate the query.
def pprint(*args, **kwargs):
if do_print:
print(*args, **kwargs)
# Actual.
card = oracle_card
# if card == 0:
# return
pprint('\n actual {} ({:.3f}%) '.format(card,
card / table.cardinality * 100),
end='')
for est in estimators:
est_card = est.QueryTwosided(len(wildcard_indicator), wildcard_indicator, valid_i_list)
err = ErrorMetric(est_card, card)
est.AddError(err, est_card, card)
pprint('{} {} (err={:.3f}) '.format(str(est), est_card, err), end='')
pprint()
def ReportEsts(estimators):
v = -1
for est in estimators:
print(est.name, 'max', np.max(est.errs), '99th',
np.quantile(est.errs, 0.99), '95th', np.quantile(est.errs, 0.95),
'median', np.quantile(est.errs, 0.5), 'mean', np.mean(est.errs))
v = max(v, np.max(est.errs))
return v
def RunNwithQueries(table,
estimators,
query_list,
rng=None,
log_every=50,
oracle_cards=None,
oracle_est=None):
if rng is None:
rng = np.random.RandomState(1234)
last_time = None
num = len(query_list)
columns_list = []
operators_list = []
vals_list = []
for query in query_list:
cols = query[0]
ops = query[1]
vals = query[2]
columns_list.append(cols)
operators_list.append(ops)
vals_list.append(vals)
wildcard_indicator, valid_i_list = estimators[0].ProcessQuery(args.dataset,
columns_list,
operators_list,
vals_list)
valid_i_list = np.array(valid_i_list)
for i in range(num):
do_print = False
if i % log_every == 0:
if last_time is not None:
print('{:.1f} queries/sec'.format(log_every /
(time.time() - last_time)))
do_print = True
print('Query {}:'.format(i), end=' ')
last_time = time.time()
QueryTwosided(estimators,
do_print,
oracle_card=oracle_cards[i]
if oracle_cards is not None and i < len(oracle_cards) else None,
query=(wildcard_indicator[i], valid_i_list[i]),
table=table,
oracle_est=oracle_est)
max_err = ReportEsts(estimators)
return False
def MakeMade(scale, cols_to_train, seed, fixed_ordering=None):
if args.inv_order:
print('Inverting order!')
fixed_ordering = InvertOrder(fixed_ordering)
model = made.MADE(
nin=len(cols_to_train),
hidden_sizes=[scale] *
args.layers if args.layers > 0 else [512, 256, 512, 128, 1024],
# hidden_sizes = [128, 512],
nout=sum([c.DistributionSize() for c in cols_to_train]),
input_bins=[c.DistributionSize() for c in cols_to_train],
input_encoding=args.input_encoding,
output_encoding=args.output_encoding,
embed_size=32,
seed=seed,
do_direct_io_connections=args.direct_io,
natural_ordering=False if seed is not None and seed != 0 else True,
residual_connections=args.residual,
fixed_ordering=fixed_ordering,
column_masking=args.column_masking,
).to(DEVICE)
return model
def ReportModel(model, blacklist=None):
ps = []
for name, p in model.named_parameters():
if blacklist is None or blacklist not in name:
ps.append(np.prod(p.size()))
num_params = sum(ps)
mb = num_params * 4 / 1024 / 1024
print('Number of model parameters: {} (~= {:.1f}MB)'.format(num_params, mb))
print(model)
return mb
def SaveEstimators(path, estimators, return_df=False):
# name, query_dur_ms, errs, est_cards, true_cards
results = pd.DataFrame()
for est in estimators:
data = {
'est': [est.name] * len(est.errs),
'err': est.errs,
'est_card': est.est_cards,
'true_card': est.true_cards,
'query_dur_ms': est.query_dur_ms,
}
results = results.append(pd.DataFrame(data))
if return_df:
return results
results.to_csv(path, index=False)
def Main():
# all_ckpts = glob.glob('./models/{}'.format(args.glob))
all_ckpts = glob.glob(args.glob)
if args.blacklist:
all_ckpts = [ckpt for ckpt in all_ckpts if args.blacklist not in ckpt]
selected_ckpts = all_ckpts
print('ckpts', selected_ckpts)
# OK to load tables now
table, train_data, oracle_est = MakeTable()
cols_to_train = table.columns
table_train = table
train_data = common.TableDataset(table_train)
Ckpt = collections.namedtuple(
'Ckpt', 'epoch path loaded_model seed')
parsed_ckpts = []
for s in selected_ckpts:
order = None
if args.order is not None:
order = list(args.order)
model = MakeMade(
scale=args.fc_hiddens,
cols_to_train=table.columns,
seed=0,
fixed_ordering=order,
)
assert order is None or len(order) == model.nin, order
ReportModel(model)
print('Loading ckpt:', s)
model.load_state_dict(torch.load(s, map_location='cuda:1'))
model.eval()
parsed_ckpts.append(
Ckpt(path=s,
epoch=None,
loaded_model=model,
seed=0))
print('Training done; evaluating likelihood on full data:')
all_losses = RunEpoch('test',
model,
train_data=train_data,
val_data=train_data,
opt=None,
batch_size=1024,
log_every=500,
table_bits=0,
return_losses=True)
model_nats = np.mean(all_losses)
model_bits = model_nats / np.log(2)
print(model_bits)
if __name__ == '__main__':
Main()