forked from var-skip/var-skip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
made.py
1172 lines (1022 loc) · 47.7 KB
/
made.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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import time
import numpy as np
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
# try:
# from typing_extensions import Final
# except:
# # If you don't have `typing_extensions` installed, you can use a
# # polyfill from `torch.jit`.
# from torch.jit import Final
# from typing import Optional
# This is a generic wrapper for any driver function you want to time
def time_this(f):
def timed_wrapper(*args, **kw):
start_time = time.time()
result = f(*args, **kw)
end_time = time.time()
# Time taken = end_time - start_time
print('| func:%r took: %2.4f seconds |' % \
(f.__name__, end_time - start_time))
# print('| func:%r args:[%r, %r] took: %2.4f seconds |' % \
# (f.__name__, args, kw, end_time - start_time))
return result
return timed_wrapper
class MaskedLinear(nn.Linear):
""" same as Linear except has a configurable mask on the weights """
# masked_weight: Optional[torch.Tensor]
def __init__(self,
in_features,
out_features,
bias=True,
condition_on_ordering=False):
super().__init__(in_features, out_features, bias)
self.register_buffer('mask', torch.ones(out_features, in_features))
self.condition_ordering_linear = None
if condition_on_ordering:
self.condition_ordering_linear = nn.Linear(in_features,
out_features,
bias=False)
self.masked_weight = None
def set_mask(self, mask):
"""Accepts a mask of shape [in_features, out_features]."""
self.mask.data.copy_(torch.from_numpy(mask.astype(np.uint8).T))
def set_cached_mask(self, mask):
self.mask.data.copy_(mask)
def get_cached_mask(self):
return self.mask.clone().detach()
def forward(self, input):
if self.masked_weight is None:
mw = self.mask * self.weight
out = F.linear(input, mw, self.bias)
# NOTE: this tied-weight variant has much higher error.
# if self.condition_ordering_linear is None:
# return out
# return out + F.linear(torch.ones_like(input), mw)
else:
# ~17% speedup for Prog Sampling.
out = F.linear(input, self.masked_weight, self.bias)
if self.condition_ordering_linear is None:
return out
return out + F.linear(torch.ones_like(input),
self.mask * self.condition_ordering_linear.weight)
class MaskedResidualBlock(nn.Module):
def __init__(self,
in_features,
out_features,
activation,
condition_on_ordering=False):
assert in_features == out_features, [in_features, out_features]
super().__init__()
self.layers = nn.ModuleList()
self.layers.append(
MaskedLinear(in_features,
out_features,
bias=True,
condition_on_ordering=condition_on_ordering))
self.layers.append(
MaskedLinear(in_features,
out_features,
bias=True,
condition_on_ordering=condition_on_ordering))
self.activation = activation
def set_mask(self, mask):
self.layers[0].set_mask(mask)
self.layers[1].set_mask(mask)
def set_cached_mask(self, mask):
# They have the same mask.
self.layers[0].mask.copy_(mask)
self.layers[1].mask.copy_(mask)
def get_cached_mask(self):
return self.layers[0].mask.clone().detach()
def forward(self, input):
out = input
out = self.activation(out)
out = self.layers[0](out)
out = self.activation(out)
out = self.layers[1](out)
return input + out
# class MADE(torch.jit.ScriptModule):
class MADE(nn.Module):
def __init__(
self,
nin,
hidden_sizes,
nout,
num_masks=1,
natural_ordering=True,
input_bins=None,
activation=nn.ReLU,
do_direct_io_connections=False,
input_encoding=None,
direct_io_bias=True, # True for backward-compat of checkpoints
output_encoding="one_hot",
embed_size=32,
input_no_emb_if_leq=True,
embs_tied=False,
residual_connections=False,
dropout_p=0,
fixed_dropout_p=False,
factor_table=None,
seed=11123,
fixed_ordering=None,
per_row_dropout_p=False,
prefix_dropout=False,
disable_learnable_unk=False,
):
"""MADE.
Args:
nin: integer; number of inputs
hidden sizes: a list of integers; number of units in hidden layers
nout: integer; number of outputs, which usually collectively
parameterize some kind of 1D distribution. note: if nout is e.g. 2x
larger than nin (perhaps the mean and std), then the first nin will
be all the means and the second nin will be stds. i.e. output
dimensions depend on the same input dimensions in "chunks" and
should be carefully decoded downstream appropriately. the output of
running the tests for this file makes this a bit more clear with
examples.
num_masks: can be used to train ensemble over orderings/connections
natural_ordering: force natural ordering of dimensions, don't use
random permutations
input_bins: classes each input var can take on, e.g., [5, 2]
means input x1 has values in {0, ..., 4} and x2 in {0, 1}.
"""
super().__init__()
self.nin = nin
if num_masks > 1:
# Double the weights, so need to reduce the size to be fair.
hidden_sizes = [int(h // 2**0.5) for h in hidden_sizes]
print("Auto reducing MO hidden sizes to", hidden_sizes, num_masks)
# None: feed inputs as-is, no encoding applied. Each column thus
# occupies 1 slot in the input layer. For testing only.
assert input_encoding in [
None, "one_hot", "two_level", "binary", "binary_100p", "embed"
]
self.input_encoding = input_encoding
assert output_encoding in ["one_hot", "bits", "embed"]
self.embed_size = self.emb_dim = embed_size
self.output_encoding = output_encoding
self.activation = activation
self.nout = nout
self.per_row_dropout_p = per_row_dropout_p
self.prefix_dropout = prefix_dropout
print("per row dropout", self.per_row_dropout_p)
print("prefix dropout", self.prefix_dropout)
self.hidden_sizes = hidden_sizes
self.input_bins = input_bins
self.input_no_emb_if_leq = input_no_emb_if_leq
self.do_direct_io_connections = do_direct_io_connections
self.embs_tied = embs_tied
self.dropout_p = dropout_p
if self.prefix_dropout or self.per_row_dropout_p:
assert self.dropout_p
self.fixed_dropout_p = fixed_dropout_p
self.factor_table = factor_table
self.residual_connections = residual_connections
self.disable_learnable_unk = disable_learnable_unk
self.num_masks = num_masks
if nout > nin:
# nout must be integer multiple of nin; or we're given more info.
assert nout % nin == 0 or input_bins is not None
self.fixed_ordering = fixed_ordering
if fixed_ordering is not None:
assert num_masks == 1
print('** Fixed ordering {} supplied, ignoring natural_ordering'.
format(fixed_ordering))
assert self.input_bins is not None
encoded_bins = list(
map(self._get_output_encoded_dist_size, self.input_bins))
self.input_bins_encoded = list(
map(self._get_input_encoded_dist_size, self.input_bins))
self.input_bins_encoded_cumsum = np.cumsum(self.input_bins_encoded)
hs = [nin] + hidden_sizes + [sum(encoded_bins)]
# print('hs={}, nin={}, hiddens={}, encoded_bins={}'.format(
# hs, nin, hidden_sizes, encoded_bins))
print('encoded_bins (output)', encoded_bins)
print('encoded_bins (input)', self.input_bins_encoded)
# define a simple MLP neural net
self.net = []
for h0, h1 in zip(hs, hs[1:]):
if residual_connections:
if h0 == h1:
self.net.extend([
MaskedResidualBlock(
h0,
h1,
activation=activation(inplace=False),
condition_on_ordering=self.num_masks > 1)
])
else:
self.net.extend([
MaskedLinear(h0,
h1,
condition_on_ordering=self.num_masks > 1),
])
else:
self.net.extend([
MaskedLinear(h0,
h1,
condition_on_ordering=self.num_masks > 1),
activation(inplace=True),
])
if not residual_connections:
self.net.pop() # pop the last ReLU for the output layer
self.net = nn.Sequential(*self.net)
if self.input_encoding is not None:
# Input layer should be changed.
assert self.input_bins is not None
input_size = 0
for i, dist_size in enumerate(self.input_bins):
input_size += self._get_input_encoded_dist_size(dist_size)
new_layer0 = MaskedLinear(input_size,
self.net[0].out_features,
condition_on_ordering=self.num_masks > 1)
self.net[0] = new_layer0
if self.input_encoding == "embed":
self.embedding_networks = nn.ModuleList()
if not self.embs_tied:
self.embedding_networks_out = nn.ModuleList()
for i, dist_size in enumerate(self.input_bins):
if dist_size <= self.embed_size and self.input_no_emb_if_leq:
embed = embed2 = None
else:
embed = nn.Embedding(dist_size, self.embed_size)
embed2 = nn.Embedding(dist_size, self.embed_size)
self.embedding_networks.append(embed)
self.embedding_networks_out.append(embed2)
else:
for i, dist_size in enumerate(self.input_bins):
if dist_size <= self.embed_size and self.input_no_emb_if_leq:
embed = None
else:
embed = nn.Embedding(dist_size, self.embed_size)
self.embedding_networks.append(embed)
# Learnable [MASK] representation.
if self.dropout_p:
self.unk_embeddings = nn.ParameterList()
print('Disable learnable?', disable_learnable_unk)
for i, dist_size in enumerate(self.input_bins):
self.unk_embeddings.append(
nn.Parameter(torch.zeros(1, self.input_bins_encoded[i]),
requires_grad=not disable_learnable_unk))
# seeds for orders/connectivities of the model ensemble
self.natural_ordering = natural_ordering
self.num_masks = num_masks
self.seed = seed if seed is not None else 11123 # for cycling through num_masks orderings
print('self.seed', self.seed)
self.direct_io_layer = None
self.logit_indices = np.cumsum(encoded_bins)
self.m = {}
self.cached_masks = {}
self.update_masks() # builds the initial self.m connectivity
# note, we could also precompute the masks and cache them, but this
# could get memory expensive for large number of masks.
# Logit indices for the columns.
self.orderings = [self.m[-1]]
# Optimization: cache some values needed in EncodeInput().
self.bin_as_onehot_shifts = None
def _build_or_update_direct_io(self):
assert self.nout > self.nin and self.input_bins is not None
direct_nin = self.net[0].in_features
direct_nout = self.net[-1].out_features
if self.direct_io_layer is None:
self.direct_io_layer = MaskedLinear(
direct_nin,
direct_nout,
condition_on_ordering=self.num_masks > 1)
mask = np.zeros((direct_nout, direct_nin), dtype=np.uint8)
print('in _build_or_update_direct_io(), self.m[-1]', self.m[-1])
# Inverse: ord_idx -> natural idx.
inv_ordering = [None] * self.nin
for natural_idx in range(self.nin):
inv_ordering[self.m[-1][natural_idx]] = natural_idx
for ord_i in range(self.nin):
nat_i = inv_ordering[ord_i]
# x_(nat_i) in the input occupies range [inp_l, inp_r).
inp_l = 0 if nat_i == 0 else self.input_bins_encoded_cumsum[nat_i -
1]
inp_r = self.input_bins_encoded_cumsum[nat_i]
assert inp_l < inp_r
for ord_j in range(ord_i + 1, self.nin):
nat_j = inv_ordering[ord_j]
# Output x_(nat_j) should connect to input x_(nat_i); it
# occupies range [out_l, out_r) in the output.
out_l = 0 if nat_j == 0 else self.logit_indices[nat_j - 1]
out_r = self.logit_indices[nat_j]
assert out_l < out_r
# print('setting mask[{}:{}, {}:{}]'.format(
# out_l, out_r, inp_l, inp_r))
mask[out_l:out_r, inp_l:inp_r] = 1
# print('do_direct_io_connections mask', mask)
# print('mask', mask)
mask = mask.T
self.direct_io_layer.set_mask(mask)
def _get_input_encoded_dist_size(self, dist_size):
if self.input_encoding == "two_level":
dist_size += 1 + dist_size // 10
elif self.input_encoding == "embed":
if self.input_no_emb_if_leq:
dist_size = min(dist_size, self.embed_size)
else:
dist_size = self.embed_size
elif self.input_encoding == "one_hot":
pass
# if dist_size <= 2:
# dist_size = 1 # don't one-hot encode binary vals
elif self.input_encoding == "binary":
dist_size = max(1, int(np.ceil(np.log2(dist_size))))
elif self.input_encoding == "binary_100p":
if dist_size > 100:
dist_size = max(1, int(np.ceil(np.log2(dist_size))))
elif self.input_encoding is None:
return 1
else:
assert False, self.input_encoding
return dist_size
def _get_output_encoded_dist_size(self, dist_size):
if self.output_encoding == "two_level":
dist_size += 1 + dist_size // 10
elif self.output_encoding == "embed":
if self.input_no_emb_if_leq:
dist_size = min(dist_size, self.embed_size)
else:
dist_size = self.embed_size
elif self.output_encoding == "one_hot":
pass
# if dist_size <= 2:
# dist_size = 1 # don't one-hot encode binary vals
elif self.output_encoding == "binary":
dist_size = max(1, int(np.ceil(np.log2(dist_size))))
elif self.output_encoding == "binary_100p":
if dist_size > 100:
dist_size = max(1, int(np.ceil(np.log2(dist_size))))
return dist_size
def update_masks(self, invoke_order=None):
"""Update m() for all layers and change masks correspondingly.
No-op if "self.num_masks" is 1.
"""
if self.m and self.num_masks == 1:
# FIXME
# assert np.array_equal(invoke_order,
# self.m[-1]), 'invoke={} curr={}'.format(
# invoke_order, self.m[-1])
return # only a single seed, skip for efficiency
L = len(self.hidden_sizes)
layers = [
l for l in self.net if isinstance(l, MaskedLinear) or
isinstance(l, MaskedResidualBlock)
]
### Precedence of several params determining ordering:
#
# invoke_order
# orderings
# fixed_ordering
# natural_ordering
#
# from high precedence to low.
# For multi-order models, we associate RNG seeds with orderings as
# follows:
# orderings = [ o0, o1, o2, ... ]
# seeds = [ 0, 1, 2, ... ]
# This must be consistent across training & inference.
if invoke_order is not None:
# Inference path.
found = False
for i in range(len(self.orderings)):
if np.array_equal(self.orderings[i], invoke_order):
found = True
break
if not found:
print("WARNING: eval on order not trained on", invoke_order)
assert found, 'specified={}, avail={}'.format(
invoke_order, self.orderings)
# print('found, order i=', i)
if self.seed == (i + 1) % self.num_masks and np.array_equal(
self.m[-1], invoke_order):
# During querying, after a multi-order model is configured to
# take a specific ordering, it can be used to do multiple
# forward passes per query.
return
self.seed = i
rng = np.random.RandomState(self.seed)
self.m[-1] = np.asarray(invoke_order)
# print('looking up seed in cached masks:', self.seed)
if self.seed in self.cached_masks:
masks, direct_io_mask = self.cached_masks[self.seed]
assert len(layers) == len(masks), (len(layers), len(masks))
for l, m in zip(layers, masks):
l.set_cached_mask(m)
if self.do_direct_io_connections:
assert direct_io_mask is not None
self.direct_io_layer.set_cached_mask(direct_io_mask)
self.seed = (self.seed + 1) % self.num_masks
# print('found, updated seed to', self.seed)
return # Early return
curr_seed = self.seed
self.seed = (self.seed + 1) % self.num_masks
elif hasattr(self, 'orderings'):
# Training path: cycle through the special orderings.
rng = np.random.RandomState(self.seed)
assert 0 <= self.seed and self.seed < len(self.orderings)
self.m[-1] = self.orderings[self.seed]
if self.seed in self.cached_masks:
masks, direct_io_mask = self.cached_masks[self.seed]
assert len(layers) == len(masks), (len(layers), len(masks))
for l, m in zip(layers, masks):
l.set_cached_mask(m)
if self.do_direct_io_connections:
assert direct_io_mask is not None
self.direct_io_layer.set_cached_mask(direct_io_mask)
# print('using cached masks for seed', self.seed)
self.seed = (self.seed + 1) % self.num_masks
return # Early return
print('constructing masks with seed', self.seed, 'self.m[-1]',
self.m[-1])
curr_seed = self.seed
self.seed = (self.seed + 1) % self.num_masks
else:
# Train-time initial construction: either single-order, or
# .orderings has not been assigned yet.
rng = np.random.RandomState(self.seed)
self.seed = (self.seed + 1) % self.num_masks
self.m[-1] = np.arange(
self.nin) if self.natural_ordering else rng.permutation(
self.nin)
if self.fixed_ordering is not None:
self.m[-1] = np.asarray(self.fixed_ordering)
if self.nin > 1:
for l in range(L):
if self.residual_connections:
# sequential assignment for ResMade: https://arxiv.org/pdf/1904.05626.pdf
# FIXME: this seems incorrect since it's [1, ncols).
self.m[l] = np.array([
(k - 1) % (self.nin - 1)
# [(k - 1) % (self.nin - 1) + 1
for k in range(self.hidden_sizes[l])
])
else:
# Samples from [0, ncols - 1).
self.m[l] = rng.randint(self.m[l - 1].min(),
self.nin - 1,
size=self.hidden_sizes[l])
else:
# This should result in first layer's masks == 0.
# So output units are disconnected to any inputs.
for l in range(L):
self.m[l] = np.asarray([-1] * self.hidden_sizes[l])
# print('ordering', self.m[-1])
# print('self.m', self.m)
# construct the mask matrices
masks = [self.m[l - 1][:, None] <= self.m[l][None, :] for l in range(L)]
masks.append(self.m[L - 1][:, None] < self.m[-1][None, :])
if self.nout > self.nin:
# Last layer's mask needs to be changed.
if self.input_bins is None:
k = int(self.nout / self.nin)
# replicate the mask across the other outputs
# so [x1, x2, ..., xn], ..., [x1, x2, ..., xn].
masks[-1] = np.concatenate([masks[-1]] * k, axis=1)
else:
# [x1, ..., x1], ..., [xn, ..., xn] where the i-th list has
# input_bins[i - 1] many elements (multiplicity, # of classes).
mask = np.asarray([])
for k in range(masks[-1].shape[0]):
tmp_mask = []
for idx, x in enumerate(zip(masks[-1][k], self.input_bins)):
mval, nbins = x[0], self._get_output_encoded_dist_size(
x[1])
tmp_mask.extend([mval] * nbins)
tmp_mask = np.asarray(tmp_mask)
if k == 0:
mask = tmp_mask
else:
mask = np.vstack([mask, tmp_mask])
masks[-1] = mask
if self.input_encoding is not None:
# Input layer's mask should be changed.
assert self.input_bins is not None
# [nin, hidden].
mask0 = masks[0]
new_mask0 = []
for i, dist_size in enumerate(self.input_bins):
dist_size = self._get_input_encoded_dist_size(dist_size)
# [dist size, hidden]
new_mask0.append(
np.concatenate([mask0[i].reshape(1, -1)] * dist_size,
axis=0))
# [sum(dist size), hidden]
new_mask0 = np.vstack(new_mask0)
masks[0] = new_mask0
assert len(layers) == len(masks), (len(layers), len(masks))
for l, m in zip(layers, masks):
l.set_mask(m)
dio_mask = None
if self.do_direct_io_connections:
self._build_or_update_direct_io()
dio_mask = self.direct_io_layer.get_cached_mask()
# Cache.
if hasattr(self, 'orderings'):
print('caching masks for seed', curr_seed)
masks = [l.get_cached_mask() for l in layers]
print('signatures:', [m.sum() for m in masks]
+ [dio_mask.sum() if dio_mask is not None else 0])
assert curr_seed not in self.cached_masks
self.cached_masks[curr_seed] = (masks, dio_mask)
def name(self):
n = 'made'
if self.residual_connections:
n += '-resmade'
n += '-hidden' + '_'.join(str(h) for h in self.hidden_sizes)
n += '-emb' + str(self.embed_size)
if self.num_masks > 1:
n += '-{}masks'.format(self.num_masks)
if not self.natural_ordering:
n += '-nonNatural'
n += ('-no' if not self.do_direct_io_connections else '-') + 'directIo'
n += '-{}In{}Out'.format(self.input_encoding, self.output_encoding)
n += '-embsTied' if self.embs_tied else '-embsNotTied'
if self.input_no_emb_if_leq:
n += '-inputNoEmbIfLeq'
if self.dropout_p:
n += '-dropout'
if self.disable_learnable_unk:
n += '-nolearnableUnk'
else:
n += '-learnableUnk'
if self.fixed_dropout_p:
n += '-fixedDropout{:.2f}'.format(self.dropout_p)
return n
def get_unk(self, i):
if self.disable_learnable_unk:
return torch.zeros_like(self.unk_embeddings[i].detach())
else:
return self.unk_embeddings[i]
# @torch.jit.script
def Embed(self, data, natural_col=None, out=None):
if data is None:
if out is None:
return self.get_unk(natural_col)
out.copy_(self.get_unk(natural_col))
return out
bs = data.size()[0]
y_embed = []
data = data.long()
if natural_col is not None:
# Fast path only for inference. One col.
coli_dom_size = self.input_bins[natural_col]
# Embed?
if coli_dom_size > self.embed_size or not self.input_no_emb_if_leq:
res = self.embedding_networks[natural_col](data.view(-1,))
if out is not None:
out.copy_(res)
return out
return res
else:
if out is None:
out = torch.zeros(bs, coli_dom_size, device=data.device)
out.scatter_(1, data, 1)
return out
else:
if self.per_row_dropout_p == 1 or self.per_row_dropout_p is True:
row_dropout_probs = torch.rand(bs, device=data.device)
elif self.per_row_dropout_p == 2:
# Also per row masking, but makes more sense (draw num masked
# tokens first). In [0, 1).
row_dropout_probs = torch.randint(
0, self.nin, (bs,), device=data.device).float() / self.nin
row_dropout_lim = torch.rand(bs, device=data.device) * len(
self.input_bins)
for i, coli_dom_size in enumerate(self.input_bins):
# Wildcard column? use -1 as special token.
# Inference pass only (see estimators.py).
not_skip = data[:, i] >= 0
data_col = torch.clamp(data[:, i], 0)
# Embed?
if coli_dom_size > self.embed_size or not self.input_no_emb_if_leq:
# assert not self.dropout_p, "not implemented"
col_i_embs = self.embedding_networks[i](data_col)
if not self.dropout_p:
y_embed.append(col_i_embs)
else:
dropped_repr = self.get_unk(i)
# During training, non-dropped 1's are scaled by
# 1/(1-p), so we clamp back to 1.
def dropout_p():
if self.fixed_dropout_p:
return self.dropout_p
return 1. - np.random.randint(
1, self.nin + 1) * 1. / self.nin
batch_mask = torch.clamp(
torch.dropout(
torch.ones(bs, 1, device=data.device),
p=dropout_p(),
# np.random.randint(5, 12) * 1. / self.nin,
train=self.training),
0,
1)
if self.training and self.per_row_dropout_p:
# 1 means original repr, 0 means use masked repr.
batch_mask = (
torch.rand(bs, device=data.device) >=
row_dropout_probs).float().unsqueeze(1)
elif self.training and self.prefix_dropout:
batch_mask = (i * torch.ones(bs, device=data.device)
>
row_dropout_lim).float().unsqueeze(1)
elif not self.training:
batch_mask = not_skip.float().unsqueeze(1)
y_embed.append(batch_mask * col_i_embs +
(1. - batch_mask) * dropped_repr)
else:
y_onehot = torch.zeros(bs,
coli_dom_size,
device=data.device)
y_onehot.scatter_(1, data_col.view(-1, 1), 1)
if self.dropout_p:
dropped_repr = self.get_unk(i)
if self.factor_table and self.factor_table.columns[
i].factor_id:
pass # use prev col's batch mask
else:
# During training, non-dropped 1's are scaled by
# 1/(1-p), so we clamp back to 1.
def dropout_p():
if self.fixed_dropout_p:
return self.dropout_p
return 1. - np.random.randint(
1, self.nin + 1) * 1. / self.nin
batch_mask = torch.clamp(
torch.dropout(
torch.ones(bs, 1, device=data.device),
# p=self.dropout_p,
p=dropout_p(),
# np.random.randint(5, 12) * 1. / self.nin,
train=self.training),
0,
1)
if self.training and self.per_row_dropout_p:
# 1 means original repr, 0 means use masked repr.
batch_mask = (
torch.rand(bs, device=data.device) >=
row_dropout_probs).float().unsqueeze(1)
elif self.training and self.prefix_dropout:
batch_mask = (
i * torch.ones(bs, device=data.device) >
row_dropout_lim).float().unsqueeze(1)
elif not self.training:
batch_mask = not_skip.float().unsqueeze(1)
y_embed.append(batch_mask * y_onehot +
(1. - batch_mask) * dropped_repr)
else:
y_embed.append(y_onehot)
return torch.cat(y_embed, 1)
def ToOneHot(self, data):
assert not self.dropout_p, "not implemented"
bs = data.size()[0]
y_onehots = []
data = data.long()
for i, coli_dom_size in enumerate(self.input_bins):
if coli_dom_size <= 2:
y_onehots.append(data[:, i].view(-1, 1).float())
else:
y_onehot = torch.zeros(bs, coli_dom_size, device=data.device)
y_onehot.scatter_(1, data[:, i].view(-1, 1), 1)
y_onehots.append(y_onehot)
# [bs, sum(dist size)]
return torch.cat(y_onehots, 1)
def ToBinaryAsOneHot(self, data, threshold=0, natural_col=None, out=None):
if data is None:
if out is None:
return self.get_unk(natural_col)
out.copy_(self.get_unk(natural_col))
return out
bs = data.size()[0]
data = data.long()
# print('data.device', data.device)
if self.bin_as_onehot_shifts is None:
# This caching gives very sizable gains.
self.bin_as_onehot_shifts = [None] * self.nin
const_one = torch.ones([], dtype=torch.long, device=data.device)
for i, coli_dom_size in enumerate(self.input_bins):
# Max with 1 to guard against cols with 1 distinct val.
one_hot_dims = max(1, int(np.ceil(np.log2(coli_dom_size))))
self.bin_as_onehot_shifts[i] = const_one << torch.arange(
one_hot_dims, device=data.device)
# print('data.device', data.device, 'const_one', const_one.device,
# 'bin_as_onehot_shifts', self.bin_as_onehot_shifts[0].device)
if natural_col is None:
# Train path.
assert out is None
y_onehots = [None] * self.nin
if self.per_row_dropout_p == 1 or self.per_row_dropout_p is True:
row_dropout_probs = torch.rand(bs, device=data.device)
elif self.per_row_dropout_p == 2:
# Also per row masking, but makes more sense (draw num masked
# tokens first). In [0, 1).
row_dropout_probs = torch.randint(
0, self.nin, (bs,), device=data.device).float() / self.nin
row_dropout_lim = torch.rand(bs, device=data.device) * len(
self.input_bins)
for i, coli_dom_size in enumerate(self.input_bins):
if coli_dom_size > threshold:
# Bit shift in PyTorch + GPU is 27% faster than np.
# data_np = data[:, i].view(-1, 1)
data_np = data.narrow(1, i, 1)
# print(data_np.device, self.bin_as_onehot_shifts[i].device)
binaries = (data_np & self.bin_as_onehot_shifts[i]) > 0
y_onehots[i] = binaries
if self.dropout_p:
dropped_repr = self.get_unk(i)
# During training, non-dropped 1's are scaled by
# 1/(1-p), so we clamp back to 1.
def dropout_p():
if self.fixed_dropout_p:
return self.dropout_p
return 1. - np.random.randint(
1, self.nin + 1) * 1. / self.nin
batch_mask = torch.clamp(
torch.dropout(
torch.ones(bs, 1, device=data.device),
# p=self.dropout_p,
p=dropout_p(),
# np.random.randint(5, 12) * 1. / self.nin,
train=self.training),
0,
1) #.to(torch.int8, non_blocking=True, copy=False)
if self.training and self.per_row_dropout_p:
batch_mask = (
torch.rand(bs, device=data.device) >=
row_dropout_probs).float().unsqueeze(1)
elif self.training and self.prefix_dropout:
batch_mask = (i * torch.ones(bs, device=data.device)
>
row_dropout_lim).float().unsqueeze(1)
binaries = binaries.to(torch.float32,
non_blocking=True,
copy=False)
# print(batch_mask.dtype, binaries.dtype, dropped_repr.dtype)
# assert False
y_onehots[i] = batch_mask * binaries + (
1. - batch_mask) * dropped_repr
else:
# encode as plain one-hot
y_onehot = torch.zeros(bs,
coli_dom_size,
device=data.device)
y_onehot.scatter_(1, data[:, i].view(-1, 1), 1)
y_onehots[i] = y_onehot
# [bs, sum(log2(dist size))]
res = torch.cat(y_onehots, 1)
return res.to(torch.float32, non_blocking=True, copy=False)
else:
# Inference path.
natural_idx = natural_col
coli_dom_size = self.input_bins[natural_idx]
# skip = data is None #data[0, 0] < 0
# if skip:
# if out is None:
# return self.unk_embeddings[natural_idx]
# out.copy_(self.unk_embeddings[natural_idx])
# return out
if coli_dom_size > threshold:
# Bit shift in PyTorch + GPU is 27% faster than np.
# data_np = data[:, i].view(-1, 1)
data_np = data #.narrow(1, 0, 1)
# print(data_np.device, self.bin_as_onehot_shifts[i].device)
if out is None:
res = (data_np & self.bin_as_onehot_shifts[natural_idx]) > 0
return res.to(torch.float32, non_blocking=True, copy=False)
else:
out.copy_(
(data_np & self.bin_as_onehot_shifts[natural_idx]) > 0)
return out
else:
assert False, 'inference'
# encode as plain one-hot
if out is None:
y_onehot = torch.zeros(bs,
coli_dom_size,
device=data.device)
y_onehot.scatter_(
1,
data, #data[:, i].view(-1, 1),
1)
res = y_onehot
return res.to(torch.float32, non_blocking=True, copy=False)
out.scatter_(1, data, 1)
return out
def ToTwoLevel(self, data):
bs = data.size()[0]
y_onehots = []
data = data.long()
for i, coli_dom_size in enumerate(self.input_bins):
y_onehot = torch.zeros(bs, coli_dom_size, device=data.device)
y_onehot.scatter_(1, data[:, i].view(-1, 1), 1)
y_onehot = torch.dropout(y_onehot, p=0.3, train=self.training)
# add on one-hot encoding at coarser second-level
# e.g., for domain of 35, the 2nd level will have domain size of 4
second_level_dom_size = 1 + coli_dom_size // 10
y2_onehot = torch.zeros(bs,
second_level_dom_size,
device=data.device)
y2_onehot.scatter_(1, data[:, i].view(-1, 1) // 10, 1)
y_onehots.append(y_onehot)
y_onehots.append(y2_onehot)
# [bs, sum(dist size) + sum(2nd_level)]
return torch.cat(y_onehots, 1)
# @time_this
def EncodeInput(self, data, natural_col=None, out=None):
""""Warning: this could take up a significant portion of a forward pass.
Args:
natural_col: if specified, 'data' has shape [N, 1] corresponding to
col-'natural-col'. Otherwise 'data' corresponds to all cols.
out: if specified, assign results into this Tensor storage.
"""
if self.input_encoding == "binary":
# TODO: try out=out see if it helps dmv11
return self.ToBinaryAsOneHot(data, natural_col=natural_col, out=out)
elif self.input_encoding == "embed":
return self.Embed(data, natural_col=natural_col, out=out)
elif self.input_encoding is None:
return data
elif self.input_encoding == "one_hot":
return self.ToOneHot(data)
elif self.input_encoding == "two_level":
return self.ToTwoLevel(data)
elif self.input_encoding == "binary_100p":
return self.ToBinaryAsOneHot(data, threshold=100)
else:
assert False, self.input_encoding
# @torch.jit.script_method
# @torch.jit.ignore
def forward(self, x, skip_prefix=[]):
"""Calculates unnormalized logits.
If self.input_bins is not specified, the output units are ordered as:
[x1, x2, ..., xn], ..., [x1, x2, ..., xn].
So they can be reshaped as thus and passed to a cross entropy loss:
out.view(-1, model.nout // model.nin, model.nin)
Otherwise, they are ordered as:
[x1, ..., x1], ..., [xn, ..., xn]
And they can't be reshaped directly.
Args:
x: [bs, ncols].
"""
if skip_prefix:
assert len(skip_prefix) == x.shape[0], (len(skip_prefix), x.shape)
for i, n in enumerate(skip_prefix):
x[i][:n] = -1
x = self.EncodeInput(x)
if self.direct_io_layer is not None:
residual = self.direct_io_layer(x)
return self.net(x) + residual
return self.net(x)