-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmodels.py
616 lines (506 loc) · 20.2 KB
/
models.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
from pathlib import Path
import torch
from torch import nn as nn
from torch.nn import functional as F
from transformers import AutoModelForSequenceClassification, AutoConfig
from transformers import AutoTokenizer, AutoModel
import torch.nn as nn
from TransFG.models.modeling import VisionTransformer
from sentence_transformers.cross_encoder import CrossEncoder
from global_params import data_source_path
class NDGModel(nn.Module):
def __init__(self):
super(NDGModel, self).__init__()
@property
def latent_layer(self) -> nn.Module:
raise NotImplementedError
@property
def latent_to_pred(self) -> nn.Module:
raise NotImplementedError
def latent_to_bool(self, saved_output) -> torch.BoolTensor:
raise NotImplementedError
def save_hook(self, x):
return x
def bool_to_latent(self, nodes) -> torch.Tensor:
raise NotImplementedError
def new_params(self):
return None
class ALLNLIModel(NDGModel):
def __init__(self, pretrained="sentence_transformers"):
super(ALLNLIModel, self).__init__()
if pretrained == "distilroberta-base":
raise NotImplementedError
self.config = AutoConfig.from_pretrained(pretrained)
self.config.num_labels = 3
self.model = AutoModelForSequenceClassification.from_pretrained(pretrained, config=self.config)
self.tokenizer = AutoTokenizer.from_pretrained(pretrained)
elif pretrained == "sentence_transformers":
ce = CrossEncoder('distilroberta-base', num_labels=3)
self.model = ce.model
self.model.classifier = ModifiedRobertaClassificationHead(self.model.config)
self.model.init_weights()
self.tokenizer = ce.tokenizer
else:
raise NotImplementedError
def forward(self, input):
ret = self.model(**input, return_dict=True).logits
return ret
@property
def latent_layer(self):
# pre sigmoid
return self.model.classifier.dense
@property
def latent_to_pred(self):
p = self.model.classifier._latent_to_pred
return p
def latent_to_bool(self, saved_output):
latent = saved_output > 0
return latent
def bool_to_latent(self, nodes):
nodes = nodes.float()
# latent = (nodes - 0.5) * 2
return nodes
def new_params(self):
names = ['lm_head.dense.bias', 'lm_head.dense.weight', 'roberta.pooler.dense.bias',
'roberta.pooler.dense.weight', 'lm_head.bias', 'lm_head.layer_norm.bias', 'lm_head.decoder.weight',
'lm_head.layer_norm.weight']
params = []
for name, p in self.model.named_parameters():
if name in names:
params.append(p)
params += list(self.model.classifier.parameters())
return params
class NLIModel(NDGModel):
def __init__(self, pretrained="sentence-transformers/all-MiniLM-L6-v2"):
super(NLIModel, self).__init__()
self.tokenizer = AutoTokenizer.from_pretrained(pretrained)
self.model = AutoModel.from_pretrained(pretrained)
if pretrained == "bert-base-uncased":
raise NotImplementedError
self.hidden = 768
elif pretrained == "sentence-transformers/all-MiniLM-L6-v2":
self.hidden = 384
else:
raise NotImplementedError
self.head = nn.ModuleList([nn.ReLU(), nn.Linear(self.hidden, 3)])
def forward(self, input):
pred = self.model(**input.data)
pred = pred.last_hidden_state
out = pred[:, 0, :]
for l in self.head:
out = l(out)
return out
class CUB200Model(NDGModel):
def __init__(self):
super(CUB200Model, self).__init__()
from TransFG.train import get_parser, setup
args = get_parser().parse_args("")
args.pretrained_dir = Path(str(data_source_path / "vit"))
args.actv = 2
head_hidden_size = None
self.args, self.model = setup(args, head_hidden_size)
self.model: VisionTransformer
def forward(self, *input):
ret = self.model(*input)
return ret
@property
def latent_layer(self) -> nn.Module:
return self.model.part_head[0]
@property
def latent_to_pred(self) -> nn.Module:
# pre relu
if self.args.actv:
p = nn.Sequential(*self.model.part_head[1:])
else:
p = nn.Identity()
return p
def latent_to_bool(self, saved_output) -> torch.BoolTensor:
return saved_output > 0
def save_hook(self, x):
return x
def bool_to_latent(self, nodes) -> torch.Tensor:
return nodes.float()
def new_params(self):
return self.model.part_head.parameters()
class CUB200ResNet(NDGModel):
def __init__(self):
super(CUB200ResNet, self).__init__()
from TransFG.train import get_parser, setup
args = get_parser().parse_args("")
args.pretrained_dir = Path("/drive/data/vit")
args.actv = 2
head_hidden_size = None
self.args, self.model = setup(args, head_hidden_size)
self.model: VisionTransformer
def forward(self, *input):
ret = self.model(*input)
return ret
@property
def latent_layer(self) -> nn.Module:
return self.model.part_head[0]
@property
def latent_to_pred(self) -> nn.Module:
# pre relu
if self.args.actv:
p = nn.Sequential(*self.model.part_head[1:])
else:
p = nn.Identity()
return p
def latent_to_bool(self, saved_output) -> torch.BoolTensor:
return saved_output > 0
def save_hook(self, x):
return x
def bool_to_latent(self, nodes) -> torch.Tensor:
return nodes.float()
def new_params(self):
return self.model.part_head.parameters()
class MNISTM(NDGModel):
def __init__(self, num_predicates=32):
super(MNISTM, self).__init__()
enc = MNISTSeq1(num_predicates)
dec = MNISTSeq2(num_predicates)
model = LatentModel(enc, dec, num_predicates)
self.model = model
def forward(self, image):
ret = self.model(image)[0]
return ret
@property
def latent_layer(self):
return self.model.encoder
@property
def multi_latent(self):
return [self.model.encoder.fc1, self.model.decoder.fc2]
@property
def latent_to_pred(self):
return self.model.decoder
def latent_to_bool(self, saved_output):
latent = saved_output > 0.5
return latent
@property
def multi_latent_to_bool(self):
return [relu_latent_to_bool, relu_latent_to_bool]
def bool_to_latent(self, nodes) -> torch.Tensor:
return nodes.float()
def relu_hugging_slice(x):
x = x[:, 0, :]
x = x > 0
return x
def multi_layer_hack(model):
"""
To load old checkpoints
"""
name = str(type(model))
if "MNISTM" in name:
return [model.model.encoder.fc1,
model.model.decoder.fc2], [relu_latent_to_bool, relu_latent_to_bool]
elif "Sentiment" in name:
return [model.model.pre_classifier,
model.model.distilbert.transformer.layer[5].ffn.lin1], [relu_latent_to_bool,
relu_hugging_slice]
elif "ALLNLIModel" in name:
return [model.model.classifier.dense,
model.model.roberta.encoder.layer[5].intermediate.dense], [relu_latent_to_bool, relu_hugging_slice]
elif "CUB200" in name:
return [model.model.part_head[0],
model.model.transformer.encoder.part_layer.ffn.fc1], [relu_latent_to_bool, relu_hugging_slice]
elif "Code" in name:
return [model.model.classifier.dense,
model.model.roberta.encoder.layer[11].intermediate.dense], [relu_latent_to_bool, relu_hugging_slice]
else:
raise NotImplementedError
def inter_layer_selection(model, dataset):
if "MNIST" == dataset:
return [model.encoder.
fc1, model.decoder.
fc2]
elif "MNIST even" == dataset:
return [model.
encoder.fc1, model.decoder.
fc2]
elif "SST2" == dataset:
return [model.pre_classifier,
model.distilbert.transformer.layer[5].ffn.lin1]
elif "AllNLI" == dataset:
return [model.classifier.dense,
model.roberta.encoder.layer[5].intermediate.dense]
elif "CUB200" == dataset:
return [model.part_head[0],
model.transformer.encoder.part_layer.ffn.fc1]
elif "Devign" == dataset:
return [model.classifier.dense,
model.roberta.encoder.layer[11].intermediate.dense]
def relu_latent_to_bool(saved_output):
latent = saved_output > 0
return latent
def sigmoid_latent_to_bool(saved_output):
latent = saved_output > 0.5
return latent
class MNISTParityM(MNISTM):
def __init__(self, num_predicates=32):
super(MNISTParityM, self).__init__()
encoder = MNISTSeq1(num_predicates)
decoder = MNISTSeq2(num_predicates, target=2)
model = LatentModel(encoder, decoder, num_predicates)
self.model = model
class CodeModel(NDGModel):
def __init__(self):
super(CodeModel, self).__init__()
self.tokenizer = AutoTokenizer.from_pretrained("microsoft/codebert-base")
self.model = AutoModelForSequenceClassification.from_pretrained("microsoft/codebert-base")
self.model.classifier = ModifiedRobertaClassificationHead(self.model.config)
self.model.init_weights()
def forward(self, input):
o = self.model(**input)
return o.logits
@property
def latent_layer(self):
# pre sigmoid
return self.model.classifier.dense
@property
def latent_to_pred(self):
p = self.model.classifier._latent_to_pred
return p
def latent_to_bool(self, saved_output):
latent = saved_output > 0
return latent
def bool_to_latent(self, nodes):
nodes = nodes.float()
# latent = (nodes - 0.5) * 2
return nodes
def new_params(self):
names = ['lm_head.dense.bias', 'lm_head.dense.weight', 'roberta.pooler.dense.bias',
'roberta.pooler.dense.weight', 'lm_head.bias', 'lm_head.layer_norm.bias', 'lm_head.decoder.weight',
'lm_head.layer_norm.weight']
params = []
for name, p in self.model.named_parameters():
if name in names:
params.append(p)
params += list(self.model.classifier.parameters())
return params
class Sentiment(NDGModel):
def __init__(self, change_forward=False):
super(Sentiment, self).__init__()
from transformers import AutoTokenizer, AutoModelForSequenceClassification
self.tokenizer = AutoTokenizer.from_pretrained("distilbert-base-uncased-finetuned-sst-2-english")
self.model = AutoModelForSequenceClassification.from_pretrained(
"distilbert-base-uncased-finetuned-sst-2-english")
if change_forward:
from transformers.models.distilbert.modeling_distilbert import SequenceClassifierOutput, MSELoss, \
BCEWithLogitsLoss, CrossEntropyLoss
def forward(
self,
input_ids=None,
attention_mask=None,
head_mask=None,
inputs_embeds=None,
labels=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
):
r"""
labels (:obj:`torch.LongTensor` of shape :obj:`(batch_size,)`, `optional`):
Labels for computing the sequence classification/regression loss. Indices should be in :obj:`[0, ...,
config.num_labels - 1]`. If :obj:`config.num_labels == 1` a regression loss is computed (Mean-Square loss),
If :obj:`config.num_labels > 1` a classification loss is computed (Cross-Entropy).
"""
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
distilbert_output = self.distilbert(
input_ids=input_ids,
attention_mask=attention_mask,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
output_attentions=output_attentions,
output_hidden_states=output_hidden_states,
return_dict=return_dict,
)
hidden_state = distilbert_output[0] # (bs, seq_len, dim)
pooled_output = hidden_state[:, 0] # (bs, dim)
pooled_output = self.pre_classifier(pooled_output) # (bs, dim)
pooled_output = nn.ReLU()(pooled_output) # (bs, dim)
pooled_output = self.dropout(pooled_output) # (bs, dim)
logits = self.classifier(pooled_output) # (bs, num_labels)
loss = None
if labels is not None:
if self.config.problem_type is None:
if self.num_labels == 1:
self.config.problem_type = "regression"
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
self.config.problem_type = "single_label_classification"
else:
self.config.problem_type = "multi_label_classification"
if self.config.problem_type == "regression":
loss_fct = MSELoss()
if self.num_labels == 1:
loss = loss_fct(logits.squeeze(), labels.squeeze())
else:
loss = loss_fct(logits, labels)
elif self.config.problem_type == "single_label_classification":
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
elif self.config.problem_type == "multi_label_classification":
loss_fct = BCEWithLogitsLoss()
loss = loss_fct(logits, labels)
if not return_dict:
output = (logits,) + distilbert_output[1:]
return ((loss,) + output) if loss is not None else output
return SequenceClassifierOutput(
loss=loss,
logits=logits,
hidden_states=distilbert_output.hidden_states,
attentions=distilbert_output.attentions,
)
self.model.forward = forward
def forward(self, input):
o = self.model(**input)
return o.logits
@property
def latent_layer(self):
# pre relu
return self.model.pre_classifier
@property
def latent_to_pred(self):
p = nn.Sequential(nn.ReLU(),
self.model.dropout,
self.model.classifier)
return p
def latent_to_bool(self, saved_output):
latent = saved_output > 0
return latent
def bool_to_latent(self, nodes) -> torch.Tensor:
return nodes.float()
class ModifiedRobertaClassificationHead(nn.Module):
"""Head for sentence-level classification tasks."""
def __init__(self, config):
super().__init__()
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
self.dense2 = nn.Linear(config.hidden_size, config.hidden_size)
classifier_dropout = (
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
)
self.dropout = nn.Dropout(classifier_dropout)
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
self._latent_to_pred = nn.Sequential(nn.Sigmoid(),
self.dropout,
self.out_proj)
def forward(self, features, **kwargs):
x = features[:, 0, :] # take <s> token (equiv. to [CLS])
x = self.dropout(x)
x = self.dense(x)
x = self._latent_to_pred(x)
return x
class CallbackLatentHook:
def __init__(self, model: NDGModel, layer=None, callback=None, latent_to_bool=None):
self.model = model
self.layer = layer or model.latent_layer
self.callback = callback
self.layer.register_forward_hook(self.save_output)
self.layer.register_full_backward_hook(self.save_gradient)
self.saved_output = None
self.saved_gradient = None
self.latent_to_bool = latent_to_bool or self.model.latent_to_bool
def save_output(self, module, input, output):
self.saved_output = self.model.save_hook(output)
def save_gradient(self, module, grad_input, grad_output):
"""
:param module:
:param grad_input: input of the module
:param grad_output: output of the module
:return:
"""
self.saved_gradient = grad_output
def get_latent(self):
if self.callback is None:
return self.model.latent_to_bool(self.saved_output)
else:
ret = self.callback(self.saved_output)
return ret
def attribute(self, input, target):
input.requires_grad = True
model_output = self.model(input)
loss = model_output[:, target]
loss = loss.sum()
loss.backward()
return self.saved_output, self.saved_gradient, model_output
def get_boo(self):
latent = self.get_latent()
boo = self.latent_to_bool(latent)
boo = boo.long().cpu()
return boo
class LatentModel(nn.Module):
def __init__(self, encoder, decoder, num_predicates, discretize=False, only_pred=False):
super(LatentModel, self).__init__()
self.encoder = encoder
self.decoder = decoder
self.num_predicates = num_predicates
self.bridge = None
self.discretize = discretize
self.only_pred = only_pred
def forward(self, x):
nodes = self.encoder(x)
if self.bridge is not None:
nodes = self.bridge.deduce(nodes)
if self.discretize:
nodes[nodes > 0.5] = 1
nodes[nodes <= 0.5] = 0
pred = self.decoder(nodes)
if not self.only_pred:
return pred, nodes
else:
return pred
class MNISTSeq1(nn.Module):
def __init__(self, num_predicates, relu=False):
super(MNISTSeq1, self).__init__()
self.relu = relu
self.conv1 = nn.Conv2d(1, 32, 3, 1)
self.conv2 = nn.Conv2d(32, 64, 3, 1)
self.dropout1 = nn.Dropout(0.25)
self.dropout2 = nn.Dropout(0.5)
self.fc1 = nn.Linear(9216, 128)
self.fc2 = nn.Linear(128, num_predicates)
self.hsm = torch.nn.Sigmoid()
# self.ex = TrickFun()
def forward(self, x):
x = self.conv1(x)
x = F.relu(x)
x = self.conv2(x)
x = F.relu(x)
x = F.max_pool2d(x, 2)
x = self.dropout1(x)
x = torch.flatten(x, 1)
x = self.fc1(x)
x = F.relu(x)
x = self.dropout2(x)
x = self.fc2(x)
if self.relu:
output = torch.relu(x)
else:
# output = torch.sigmoid(self.ex.apply(x))
output = torch.sigmoid(x)
# if not self.training:
# output[output > 0.5] = 1
# output[output <= 0.5] = 0
return output
class MNISTSeq2(nn.Module):
def __init__(self, num_predicates, target=10, hidden=128):
super(MNISTSeq2, self).__init__()
self.num_predicates = num_predicates
self.fc1 = nn.Linear(num_predicates, hidden)
self.fc2 = nn.Linear(hidden, hidden)
self.fc3 = nn.Linear(hidden, target)
self.dropout = nn.Dropout(0.25)
def forward(self, input):
x = self.fc1(input)
x = F.relu(x)
x = self.dropout(x)
x = self.fc2(x)
x = torch.relu(x)
x = self.fc3(x)
return x
class TrickFun(torch.autograd.Function):
@staticmethod
def forward(ctx, input):
return input * 10
@staticmethod
def backward(ctx, grad_output):
return grad_output