forked from rycolab/probing-via-prompting
-
Notifications
You must be signed in to change notification settings - Fork 0
/
modeling_gpt2_dp.py
232 lines (206 loc) · 6.88 KB
/
modeling_gpt2_dp.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
from copy import deepcopy
from typing import Optional
import torch
from allennlp.modules import scalar_mix
from allennlp.modules.span_extractors import SelfAttentiveSpanExtractor
from torch import nn
from torch.nn import CrossEntropyLoss
from transformers import GPT2PreTrainedModel
from transformers.file_utils import ModelOutput
import wandb
from utils import STEFunction
class DiagnosticProbingOutputs(ModelOutput):
loss: Optional[torch.FloatTensor] = None
logits: torch.FloatTensor = None
class GPT2ForDiagnosticProbing(GPT2PreTrainedModel):
def __init__(self, config, gpt2):
super().__init__(config)
if config.chinese:
self.transformer = gpt2.transformer # To get of rid of LMHead of GPT2LMHeadModel (GPT2-Chinese)
else:
self.transformer = gpt2
if config.onehot is False:
for param in self.transformer.parameters():
param.requires_grad = False
else:
for param in self.transformer.parameters():
param.requires_grad = True
print("Onehot is True. All parameters are trainable.")
# Model parallel
self.model_parallel = False
self.device_map = None
self.unary = config.unary
self.num_labels = config.num_labels
self.mlp_dropout = config.mlp_dropout
self.mlp_dim = config.mlp_dim
self.mlp_layers: int = config.mlp_layers
self.use_mlp = config.use_mlp
self.n_embd = config.n_embd
self.onehot: bool = config.onehot
self.scalar_mix = scalar_mix.ScalarMix(config.n_layer)
# self.onehot_scalar_mix = scalar_mix.ScalarMix(1)
if self.onehot is False:
self.proj1 = nn.Conv1d(
config.n_embd,
config.mlp_dim,
kernel_size=1,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
)
else:
self.proj1 = nn.Conv1d(
config.vocab_size,
config.mlp_dim,
kernel_size=1,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
)
self.span_extractor1 = SelfAttentiveSpanExtractor(config.mlp_dim)
self.d_inp = self.span_extractor1.get_output_dim()
if not self.unary:
if self.onehot is False:
self.proj2 = nn.Conv1d(
config.n_embd,
config.mlp_dim,
kernel_size=1,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
)
else:
self.proj2 = nn.Conv1d(
config.vocab_size,
config.mlp_dim,
kernel_size=1,
stride=1,
padding=0,
dilation=1,
groups=1,
bias=True,
)
self.span_extractor2 = SelfAttentiveSpanExtractor(config.mlp_dim)
self.d_inp += self.span_extractor2.get_output_dim()
wandb.run.summary["input_layer_dim"] = self.d_inp
if not self.use_mlp:
lin_module_list = []
if self.mlp_layers == 1:
self.classifier = nn.Sequential(
nn.Linear(self.d_inp, self.mlp_dim),
nn.Linear(self.mlp_dim, self.num_labels)
)
elif self.mlp_layers >= 2:
lin_module_list.append(nn.Linear(self.d_inp, self.mlp_dim))
for _ in range(self.mlp_layers - 1):
lin_module_list.append(nn.Linear(self.mlp_dim, self.mlp_dim))
lin_module_list.append(nn.Linear(self.mlp_dim, self.num_labels))
self.classifier = nn.Sequential(*lin_module_list)
else:
input_layer_list = [
nn.Linear(self.d_inp, self.mlp_dim),
nn.Tanh(),
nn.LayerNorm(self.mlp_dim),
nn.Dropout(self.mlp_dropout),
]
output_layer_list = [nn.Linear(self.mlp_dim, self.num_labels)]
if self.mlp_layers == 1:
classifier_module_list = deepcopy(input_layer_list) + deepcopy(output_layer_list)
elif self.mlp_layers >= 2:
classifier_module_list = deepcopy(input_layer_list)
for _ in range(self.mlp_layers - 1):
classifier_module_list.append(nn.Linear(self.mlp_dim, self.mlp_dim))
classifier_module_list.append(nn.Tanh())
classifier_module_list.append(nn.LayerNorm(self.mlp_dim))
classifier_module_list.append(nn.Dropout(self.mlp_dropout))
classifier_module_list += deepcopy(output_layer_list)
else:
raise ValueError(f"The num of MLP layers should be a positive integer. Your input is {self.mlp_layer}")
self.classifier = nn.Sequential(*classifier_module_list)
self.w = nn.Parameter(torch.empty([config.num_hidden_layers, config.num_attention_heads]))
nn.init.xavier_uniform(self.w)
self.num_of_heads = None
self.use_dsp = False
def forward(
self,
input_ids=None,
past_key_values=None,
attention_mask=None,
token_type_ids=None,
position_ids=None,
head_mask=None,
inputs_embeds=None,
encoder_hidden_states=None,
encoder_attention_mask=None,
labels=None,
use_cache=None,
output_attentions=None,
output_hidden_states=None,
return_dict=None,
span1s=None,
span2s=None,
):
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
if self.use_dsp:
head_mask = STEFunction.apply(self.w.view(-1), self.num_of_heads).view_as(self.w)
self.apply_masks(head_mask)
if self.onehot is False:
transformer_outputs = self.transformer(
input_ids,
past_key_values=past_key_values,
attention_mask=attention_mask,
token_type_ids=token_type_ids,
position_ids=position_ids,
head_mask=head_mask,
inputs_embeds=inputs_embeds,
encoder_hidden_states=encoder_hidden_states,
encoder_attention_mask=encoder_attention_mask,
use_cache=use_cache,
output_attentions=output_attentions,
output_hidden_states=True,
return_dict=True,
)
if not self.use_mlp:
contextual_embeddings = transformer_outputs[0]
else:
all_hidden_states = transformer_outputs.hidden_states[1:]
contextual_embeddings = self.scalar_mix(all_hidden_states)
else:
contextual_embeddings = torch.nn.functional.one_hot(input_ids, num_classes=self.config.vocab_size).half()
span_mask = span1s[:, :, 0] != -1
se_proj1 = self.proj1(contextual_embeddings.transpose(1, 2)).transpose(2, 1).contiguous()
span1_emb = self.span_extractor1(se_proj1, span1s, span_indices_mask=span_mask.long())
if not self.unary:
se_proj2 = self.proj2(contextual_embeddings.transpose(1, 2)).transpose(2, 1).contiguous()
span2_emb = self.span_extractor2(se_proj2, span2s, span_indices_mask=span_mask.long())
span_emb = torch.cat([span1_emb, span2_emb], dim=2)
else:
span_emb = span1_emb
logits = self.classifier(span_emb)
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits[span_mask], labels[span_mask])
corrections = logits[span_mask].argmax(-1) == labels[span_mask]
correct_counts = corrections.sum()
total_counts = len(corrections)
accuracy = torch.tensor([[correct_counts, total_counts]], device=corrections.device)
if not return_dict:
output = (accuracy,)
return ((loss,) + output) if loss is not None else output
return DiagnosticProbingOutputs(
loss=loss,
logits=accuracy,
)
def apply_masks(self, head_mask):
head_mask = self.get_head_mask(head_mask, self.config.num_hidden_layers)
self.transformer.apply_masks(head_mask)
def get_masks(self):
return torch.stack(self.transformer.get_masks())
def apply_dsp(self, num_of_heads):
self.num_of_heads = num_of_heads
self.use_dsp = True