Omar
commited on
Commit
·
904c81d
1
Parent(s):
0fbf7fe
update results
Browse filesThis view is limited to 50 files because it contains too many changes.
See raw diff
- config.json +30 -4
- finetune/boolq/all_results.json +16 -0
- finetune/boolq/config.json +57 -0
- finetune/boolq/eval_results.json +11 -0
- finetune/boolq/merges.txt +0 -0
- finetune/boolq/predict_results.txt +724 -0
- finetune/boolq/pytorch_model.bin +3 -0
- finetune/boolq/special_tokens_map.json +15 -0
- finetune/boolq/structformer_as_hf_no_parser.py +754 -0
- finetune/boolq/tokenizer_config.json +65 -0
- finetune/boolq/train_results.json +8 -0
- finetune/boolq/trainer_state.json +25 -0
- finetune/boolq/training_args.bin +3 -0
- finetune/boolq/vocab.json +0 -0
- finetune/cola/all_results.json +16 -0
- finetune/cola/checkpoint-400/config.json +57 -0
- finetune/cola/checkpoint-400/merges.txt +0 -0
- finetune/cola/checkpoint-400/optimizer.pt +3 -0
- finetune/cola/checkpoint-400/pytorch_model.bin +3 -0
- finetune/cola/checkpoint-400/rng_state.pth +3 -0
- finetune/cola/checkpoint-400/scheduler.pt +3 -0
- finetune/cola/checkpoint-400/special_tokens_map.json +15 -0
- finetune/cola/checkpoint-400/structformer_as_hf_no_parser.py +754 -0
- finetune/cola/checkpoint-400/tokenizer_config.json +65 -0
- finetune/cola/checkpoint-400/trainer_state.json +27 -0
- finetune/cola/checkpoint-400/training_args.bin +3 -0
- finetune/cola/checkpoint-400/vocab.json +0 -0
- finetune/cola/config.json +57 -0
- finetune/cola/eval_results.json +11 -0
- finetune/cola/merges.txt +0 -0
- finetune/cola/predict_results.txt +1020 -0
- finetune/cola/pytorch_model.bin +3 -0
- finetune/cola/special_tokens_map.json +15 -0
- finetune/cola/structformer_as_hf_no_parser.py +754 -0
- finetune/cola/tokenizer_config.json +65 -0
- finetune/cola/train_results.json +8 -0
- finetune/cola/trainer_state.json +42 -0
- finetune/cola/training_args.bin +3 -0
- finetune/cola/vocab.json +0 -0
- finetune/control_raising_control/all_results.json +16 -0
- finetune/control_raising_control/checkpoint-400/config.json +57 -0
- finetune/control_raising_control/checkpoint-400/merges.txt +0 -0
- finetune/control_raising_control/checkpoint-400/optimizer.pt +3 -0
- finetune/control_raising_control/checkpoint-400/pytorch_model.bin +3 -0
- finetune/control_raising_control/checkpoint-400/rng_state.pth +3 -0
- finetune/control_raising_control/checkpoint-400/scheduler.pt +3 -0
- finetune/control_raising_control/checkpoint-400/special_tokens_map.json +15 -0
- finetune/control_raising_control/checkpoint-400/structformer_as_hf_no_parser.py +754 -0
- finetune/control_raising_control/checkpoint-400/tokenizer_config.json +65 -0
- finetune/control_raising_control/checkpoint-400/trainer_state.json +27 -0
config.json
CHANGED
@@ -1,11 +1,15 @@
|
|
1 |
{
|
2 |
"architectures": [
|
3 |
-
"StructformerModel"
|
|
|
4 |
],
|
|
|
5 |
"auto_map": {
|
6 |
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
7 |
-
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel"
|
|
|
8 |
},
|
|
|
9 |
"conv_size": 9,
|
10 |
"dropatt": 0.1,
|
11 |
"dropout": 0.1,
|
@@ -25,5 +29,27 @@
|
|
25 |
"relative_bias": false,
|
26 |
"torch_dtype": "float32",
|
27 |
"transformers_version": "4.18.0",
|
28 |
-
"weight_act": "softmax"
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
{
|
2 |
"architectures": [
|
3 |
+
"StructformerModel",
|
4 |
+
"StructformerModelForSequenceClassification"
|
5 |
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
"auto_map": {
|
8 |
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
9 |
+
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel",
|
10 |
+
"AutoModelForSequenceClassification": "structformer_as_hf_no_parser.StructformerModelForSequenceClassification"
|
11 |
},
|
12 |
+
|
13 |
"conv_size": 9,
|
14 |
"dropatt": 0.1,
|
15 |
"dropout": 0.1,
|
|
|
29 |
"relative_bias": false,
|
30 |
"torch_dtype": "float32",
|
31 |
"transformers_version": "4.18.0",
|
32 |
+
"weight_act": "softmax",
|
33 |
+
|
34 |
+
|
35 |
+
"bos_token_id": 0,
|
36 |
+
"classifier_dropout": null,
|
37 |
+
|
38 |
+
"eos_token_id": 2,
|
39 |
+
"hidden_act": "gelu",
|
40 |
+
"hidden_dropout_prob": 0.1,
|
41 |
+
"initializer_range": 0.02,
|
42 |
+
"intermediate_size": 3072,
|
43 |
+
"layer_norm_eps": 1e-05,
|
44 |
+
"max_position_embeddings": 514,
|
45 |
+
|
46 |
+
"num_attention_heads": 12,
|
47 |
+
"num_hidden_layers": 12,
|
48 |
+
"pad_token_id": 1,
|
49 |
+
"position_embedding_type": "absolute",
|
50 |
+
|
51 |
+
"type_vocab_size": 1,
|
52 |
+
"use_cache": true,
|
53 |
+
"vocab_size": 32000
|
54 |
+
|
55 |
+
}
|
finetune/boolq/all_results.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"eval_accuracy": 0.6293222904205322,
|
4 |
+
"eval_f1": 0.7067833698030634,
|
5 |
+
"eval_loss": 1.2662478685379028,
|
6 |
+
"eval_mcc": 0.20890142408340617,
|
7 |
+
"eval_runtime": 1.0109,
|
8 |
+
"eval_samples": 723,
|
9 |
+
"eval_samples_per_second": 715.225,
|
10 |
+
"eval_steps_per_second": 90.021,
|
11 |
+
"train_loss": 0.33231258392333984,
|
12 |
+
"train_runtime": 67.9575,
|
13 |
+
"train_samples": 2072,
|
14 |
+
"train_samples_per_second": 304.896,
|
15 |
+
"train_steps_per_second": 2.649
|
16 |
+
}
|
finetune/boolq/config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "final_models/transformer_base_final_2",
|
3 |
+
"architectures": [
|
4 |
+
"StructformerModelForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
9 |
+
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel",
|
10 |
+
"AutoModelForSequenceClassification": "structformer_as_hf_no_parser.StructformerModelForSequenceClassification"
|
11 |
+
},
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"classifier_dropout": null,
|
14 |
+
"conv_size": 9,
|
15 |
+
"dropatt": 0.1,
|
16 |
+
"dropout": 0.1,
|
17 |
+
"eos_token_id": 2,
|
18 |
+
"hidden_act": "gelu",
|
19 |
+
"hidden_dropout_prob": 0.1,
|
20 |
+
"hidden_size": 768,
|
21 |
+
"id2label": {
|
22 |
+
"0": 0,
|
23 |
+
"1": 1
|
24 |
+
},
|
25 |
+
"initializer_range": 0.02,
|
26 |
+
"intermediate_size": 3072,
|
27 |
+
"label2id": {
|
28 |
+
"0": 0,
|
29 |
+
"1": 1
|
30 |
+
},
|
31 |
+
"layer_norm_eps": 1e-05,
|
32 |
+
"max_position_embeddings": 514,
|
33 |
+
"model_type": "structformer",
|
34 |
+
"n_context_layers": 0,
|
35 |
+
"n_parser_layers": 0,
|
36 |
+
"nhead": 12,
|
37 |
+
"nlayers": 12,
|
38 |
+
"ntokens": 32000,
|
39 |
+
"num_attention_heads": 12,
|
40 |
+
"num_hidden_layers": 12,
|
41 |
+
"pad": 0,
|
42 |
+
"pad_token_id": 1,
|
43 |
+
"pos_emb": true,
|
44 |
+
"position_embedding_type": "absolute",
|
45 |
+
"problem_type": "single_label_classification",
|
46 |
+
"relations": [
|
47 |
+
"head",
|
48 |
+
"child"
|
49 |
+
],
|
50 |
+
"relative_bias": false,
|
51 |
+
"torch_dtype": "float32",
|
52 |
+
"transformers_version": "4.26.1",
|
53 |
+
"type_vocab_size": 1,
|
54 |
+
"use_cache": true,
|
55 |
+
"vocab_size": 32000,
|
56 |
+
"weight_act": "softmax"
|
57 |
+
}
|
finetune/boolq/eval_results.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"eval_accuracy": 0.6293222904205322,
|
4 |
+
"eval_f1": 0.7067833698030634,
|
5 |
+
"eval_loss": 1.2662478685379028,
|
6 |
+
"eval_mcc": 0.20890142408340617,
|
7 |
+
"eval_runtime": 1.0109,
|
8 |
+
"eval_samples": 723,
|
9 |
+
"eval_samples_per_second": 715.225,
|
10 |
+
"eval_steps_per_second": 90.021
|
11 |
+
}
|
finetune/boolq/merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/boolq/predict_results.txt
ADDED
@@ -0,0 +1,724 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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
|
39 |
+
37 0
|
40 |
+
38 1
|
41 |
+
39 1
|
42 |
+
40 0
|
43 |
+
41 1
|
44 |
+
42 1
|
45 |
+
43 1
|
46 |
+
44 1
|
47 |
+
45 1
|
48 |
+
46 1
|
49 |
+
47 0
|
50 |
+
48 1
|
51 |
+
49 1
|
52 |
+
50 0
|
53 |
+
51 1
|
54 |
+
52 1
|
55 |
+
53 1
|
56 |
+
54 0
|
57 |
+
55 0
|
58 |
+
56 1
|
59 |
+
57 0
|
60 |
+
58 0
|
61 |
+
59 1
|
62 |
+
60 1
|
63 |
+
61 0
|
64 |
+
62 0
|
65 |
+
63 1
|
66 |
+
64 1
|
67 |
+
65 1
|
68 |
+
66 1
|
69 |
+
67 1
|
70 |
+
68 0
|
71 |
+
69 1
|
72 |
+
70 0
|
73 |
+
71 1
|
74 |
+
72 0
|
75 |
+
73 1
|
76 |
+
74 0
|
77 |
+
75 1
|
78 |
+
76 0
|
79 |
+
77 0
|
80 |
+
78 1
|
81 |
+
79 0
|
82 |
+
80 1
|
83 |
+
81 1
|
84 |
+
82 0
|
85 |
+
83 1
|
86 |
+
84 1
|
87 |
+
85 1
|
88 |
+
86 1
|
89 |
+
87 1
|
90 |
+
88 1
|
91 |
+
89 0
|
92 |
+
90 1
|
93 |
+
91 1
|
94 |
+
92 1
|
95 |
+
93 1
|
96 |
+
94 0
|
97 |
+
95 1
|
98 |
+
96 1
|
99 |
+
97 1
|
100 |
+
98 1
|
101 |
+
99 1
|
102 |
+
100 1
|
103 |
+
101 0
|
104 |
+
102 0
|
105 |
+
103 0
|
106 |
+
104 1
|
107 |
+
105 1
|
108 |
+
106 1
|
109 |
+
107 0
|
110 |
+
108 0
|
111 |
+
109 1
|
112 |
+
110 1
|
113 |
+
111 1
|
114 |
+
112 0
|
115 |
+
113 1
|
116 |
+
114 1
|
117 |
+
115 1
|
118 |
+
116 1
|
119 |
+
117 0
|
120 |
+
118 1
|
121 |
+
119 1
|
122 |
+
120 1
|
123 |
+
121 0
|
124 |
+
122 1
|
125 |
+
123 0
|
126 |
+
124 0
|
127 |
+
125 1
|
128 |
+
126 1
|
129 |
+
127 0
|
130 |
+
128 1
|
131 |
+
129 0
|
132 |
+
130 0
|
133 |
+
131 0
|
134 |
+
132 0
|
135 |
+
133 1
|
136 |
+
134 1
|
137 |
+
135 1
|
138 |
+
136 1
|
139 |
+
137 0
|
140 |
+
138 0
|
141 |
+
139 1
|
142 |
+
140 0
|
143 |
+
141 1
|
144 |
+
142 1
|
145 |
+
143 1
|
146 |
+
144 0
|
147 |
+
145 1
|
148 |
+
146 1
|
149 |
+
147 1
|
150 |
+
148 1
|
151 |
+
149 1
|
152 |
+
150 0
|
153 |
+
151 1
|
154 |
+
152 1
|
155 |
+
153 1
|
156 |
+
154 1
|
157 |
+
155 0
|
158 |
+
156 0
|
159 |
+
157 0
|
160 |
+
158 0
|
161 |
+
159 1
|
162 |
+
160 1
|
163 |
+
161 1
|
164 |
+
162 1
|
165 |
+
163 1
|
166 |
+
164 0
|
167 |
+
165 1
|
168 |
+
166 1
|
169 |
+
167 1
|
170 |
+
168 0
|
171 |
+
169 0
|
172 |
+
170 1
|
173 |
+
171 0
|
174 |
+
172 0
|
175 |
+
173 1
|
176 |
+
174 0
|
177 |
+
175 1
|
178 |
+
176 1
|
179 |
+
177 1
|
180 |
+
178 0
|
181 |
+
179 1
|
182 |
+
180 0
|
183 |
+
181 1
|
184 |
+
182 0
|
185 |
+
183 1
|
186 |
+
184 0
|
187 |
+
185 1
|
188 |
+
186 0
|
189 |
+
187 0
|
190 |
+
188 1
|
191 |
+
189 1
|
192 |
+
190 1
|
193 |
+
191 0
|
194 |
+
192 0
|
195 |
+
193 0
|
196 |
+
194 1
|
197 |
+
195 0
|
198 |
+
196 1
|
199 |
+
197 1
|
200 |
+
198 0
|
201 |
+
199 1
|
202 |
+
200 1
|
203 |
+
201 1
|
204 |
+
202 1
|
205 |
+
203 1
|
206 |
+
204 1
|
207 |
+
205 1
|
208 |
+
206 1
|
209 |
+
207 1
|
210 |
+
208 0
|
211 |
+
209 1
|
212 |
+
210 1
|
213 |
+
211 1
|
214 |
+
212 1
|
215 |
+
213 0
|
216 |
+
214 1
|
217 |
+
215 0
|
218 |
+
216 1
|
219 |
+
217 0
|
220 |
+
218 1
|
221 |
+
219 1
|
222 |
+
220 1
|
223 |
+
221 0
|
224 |
+
222 1
|
225 |
+
223 0
|
226 |
+
224 0
|
227 |
+
225 0
|
228 |
+
226 1
|
229 |
+
227 0
|
230 |
+
228 0
|
231 |
+
229 1
|
232 |
+
230 0
|
233 |
+
231 1
|
234 |
+
232 1
|
235 |
+
233 0
|
236 |
+
234 0
|
237 |
+
235 1
|
238 |
+
236 1
|
239 |
+
237 1
|
240 |
+
238 1
|
241 |
+
239 1
|
242 |
+
240 1
|
243 |
+
241 1
|
244 |
+
242 1
|
245 |
+
243 1
|
246 |
+
244 0
|
247 |
+
245 1
|
248 |
+
246 1
|
249 |
+
247 0
|
250 |
+
248 1
|
251 |
+
249 1
|
252 |
+
250 1
|
253 |
+
251 0
|
254 |
+
252 1
|
255 |
+
253 1
|
256 |
+
254 1
|
257 |
+
255 1
|
258 |
+
256 1
|
259 |
+
257 1
|
260 |
+
258 1
|
261 |
+
259 0
|
262 |
+
260 1
|
263 |
+
261 0
|
264 |
+
262 1
|
265 |
+
263 1
|
266 |
+
264 0
|
267 |
+
265 1
|
268 |
+
266 1
|
269 |
+
267 0
|
270 |
+
268 1
|
271 |
+
269 1
|
272 |
+
270 0
|
273 |
+
271 1
|
274 |
+
272 1
|
275 |
+
273 1
|
276 |
+
274 1
|
277 |
+
275 1
|
278 |
+
276 1
|
279 |
+
277 0
|
280 |
+
278 0
|
281 |
+
279 1
|
282 |
+
280 1
|
283 |
+
281 1
|
284 |
+
282 0
|
285 |
+
283 0
|
286 |
+
284 0
|
287 |
+
285 1
|
288 |
+
286 1
|
289 |
+
287 1
|
290 |
+
288 1
|
291 |
+
289 0
|
292 |
+
290 0
|
293 |
+
291 0
|
294 |
+
292 1
|
295 |
+
293 1
|
296 |
+
294 1
|
297 |
+
295 1
|
298 |
+
296 1
|
299 |
+
297 1
|
300 |
+
298 1
|
301 |
+
299 1
|
302 |
+
300 1
|
303 |
+
301 1
|
304 |
+
302 1
|
305 |
+
303 0
|
306 |
+
304 1
|
307 |
+
305 0
|
308 |
+
306 0
|
309 |
+
307 0
|
310 |
+
308 1
|
311 |
+
309 0
|
312 |
+
310 0
|
313 |
+
311 1
|
314 |
+
312 1
|
315 |
+
313 1
|
316 |
+
314 1
|
317 |
+
315 1
|
318 |
+
316 1
|
319 |
+
317 1
|
320 |
+
318 1
|
321 |
+
319 1
|
322 |
+
320 1
|
323 |
+
321 0
|
324 |
+
322 1
|
325 |
+
323 1
|
326 |
+
324 1
|
327 |
+
325 0
|
328 |
+
326 1
|
329 |
+
327 1
|
330 |
+
328 1
|
331 |
+
329 1
|
332 |
+
330 0
|
333 |
+
331 0
|
334 |
+
332 1
|
335 |
+
333 1
|
336 |
+
334 0
|
337 |
+
335 1
|
338 |
+
336 0
|
339 |
+
337 1
|
340 |
+
338 1
|
341 |
+
339 1
|
342 |
+
340 1
|
343 |
+
341 0
|
344 |
+
342 1
|
345 |
+
343 0
|
346 |
+
344 0
|
347 |
+
345 1
|
348 |
+
346 1
|
349 |
+
347 1
|
350 |
+
348 1
|
351 |
+
349 1
|
352 |
+
350 1
|
353 |
+
351 1
|
354 |
+
352 1
|
355 |
+
353 1
|
356 |
+
354 0
|
357 |
+
355 1
|
358 |
+
356 0
|
359 |
+
357 1
|
360 |
+
358 1
|
361 |
+
359 0
|
362 |
+
360 1
|
363 |
+
361 1
|
364 |
+
362 0
|
365 |
+
363 1
|
366 |
+
364 0
|
367 |
+
365 1
|
368 |
+
366 1
|
369 |
+
367 1
|
370 |
+
368 1
|
371 |
+
369 1
|
372 |
+
370 0
|
373 |
+
371 0
|
374 |
+
372 1
|
375 |
+
373 1
|
376 |
+
374 1
|
377 |
+
375 1
|
378 |
+
376 1
|
379 |
+
377 1
|
380 |
+
378 1
|
381 |
+
379 0
|
382 |
+
380 0
|
383 |
+
381 0
|
384 |
+
382 1
|
385 |
+
383 1
|
386 |
+
384 1
|
387 |
+
385 0
|
388 |
+
386 1
|
389 |
+
387 0
|
390 |
+
388 1
|
391 |
+
389 1
|
392 |
+
390 1
|
393 |
+
391 0
|
394 |
+
392 0
|
395 |
+
393 0
|
396 |
+
394 1
|
397 |
+
395 1
|
398 |
+
396 1
|
399 |
+
397 1
|
400 |
+
398 0
|
401 |
+
399 0
|
402 |
+
400 1
|
403 |
+
401 0
|
404 |
+
402 1
|
405 |
+
403 1
|
406 |
+
404 0
|
407 |
+
405 1
|
408 |
+
406 0
|
409 |
+
407 0
|
410 |
+
408 1
|
411 |
+
409 1
|
412 |
+
410 1
|
413 |
+
411 1
|
414 |
+
412 0
|
415 |
+
413 1
|
416 |
+
414 1
|
417 |
+
415 0
|
418 |
+
416 1
|
419 |
+
417 1
|
420 |
+
418 0
|
421 |
+
419 1
|
422 |
+
420 1
|
423 |
+
421 1
|
424 |
+
422 1
|
425 |
+
423 1
|
426 |
+
424 1
|
427 |
+
425 0
|
428 |
+
426 1
|
429 |
+
427 1
|
430 |
+
428 0
|
431 |
+
429 1
|
432 |
+
430 1
|
433 |
+
431 0
|
434 |
+
432 1
|
435 |
+
433 1
|
436 |
+
434 0
|
437 |
+
435 0
|
438 |
+
436 1
|
439 |
+
437 1
|
440 |
+
438 0
|
441 |
+
439 0
|
442 |
+
440 0
|
443 |
+
441 0
|
444 |
+
442 1
|
445 |
+
443 0
|
446 |
+
444 1
|
447 |
+
445 1
|
448 |
+
446 1
|
449 |
+
447 0
|
450 |
+
448 1
|
451 |
+
449 1
|
452 |
+
450 1
|
453 |
+
451 0
|
454 |
+
452 1
|
455 |
+
453 1
|
456 |
+
454 1
|
457 |
+
455 0
|
458 |
+
456 1
|
459 |
+
457 0
|
460 |
+
458 1
|
461 |
+
459 1
|
462 |
+
460 1
|
463 |
+
461 1
|
464 |
+
462 0
|
465 |
+
463 0
|
466 |
+
464 0
|
467 |
+
465 0
|
468 |
+
466 1
|
469 |
+
467 1
|
470 |
+
468 1
|
471 |
+
469 0
|
472 |
+
470 1
|
473 |
+
471 1
|
474 |
+
472 1
|
475 |
+
473 1
|
476 |
+
474 1
|
477 |
+
475 0
|
478 |
+
476 0
|
479 |
+
477 1
|
480 |
+
478 0
|
481 |
+
479 1
|
482 |
+
480 1
|
483 |
+
481 1
|
484 |
+
482 0
|
485 |
+
483 1
|
486 |
+
484 1
|
487 |
+
485 1
|
488 |
+
486 1
|
489 |
+
487 1
|
490 |
+
488 1
|
491 |
+
489 0
|
492 |
+
490 1
|
493 |
+
491 1
|
494 |
+
492 0
|
495 |
+
493 1
|
496 |
+
494 1
|
497 |
+
495 1
|
498 |
+
496 1
|
499 |
+
497 1
|
500 |
+
498 1
|
501 |
+
499 1
|
502 |
+
500 1
|
503 |
+
501 0
|
504 |
+
502 0
|
505 |
+
503 1
|
506 |
+
504 0
|
507 |
+
505 1
|
508 |
+
506 1
|
509 |
+
507 1
|
510 |
+
508 1
|
511 |
+
509 0
|
512 |
+
510 1
|
513 |
+
511 1
|
514 |
+
512 1
|
515 |
+
513 1
|
516 |
+
514 0
|
517 |
+
515 0
|
518 |
+
516 1
|
519 |
+
517 1
|
520 |
+
518 1
|
521 |
+
519 1
|
522 |
+
520 1
|
523 |
+
521 1
|
524 |
+
522 0
|
525 |
+
523 1
|
526 |
+
524 1
|
527 |
+
525 1
|
528 |
+
526 1
|
529 |
+
527 1
|
530 |
+
528 0
|
531 |
+
529 0
|
532 |
+
530 1
|
533 |
+
531 0
|
534 |
+
532 0
|
535 |
+
533 1
|
536 |
+
534 0
|
537 |
+
535 0
|
538 |
+
536 1
|
539 |
+
537 0
|
540 |
+
538 0
|
541 |
+
539 1
|
542 |
+
540 0
|
543 |
+
541 1
|
544 |
+
542 1
|
545 |
+
543 1
|
546 |
+
544 0
|
547 |
+
545 1
|
548 |
+
546 1
|
549 |
+
547 1
|
550 |
+
548 1
|
551 |
+
549 1
|
552 |
+
550 1
|
553 |
+
551 0
|
554 |
+
552 0
|
555 |
+
553 1
|
556 |
+
554 0
|
557 |
+
555 0
|
558 |
+
556 1
|
559 |
+
557 0
|
560 |
+
558 0
|
561 |
+
559 1
|
562 |
+
560 0
|
563 |
+
561 1
|
564 |
+
562 1
|
565 |
+
563 1
|
566 |
+
564 1
|
567 |
+
565 0
|
568 |
+
566 1
|
569 |
+
567 0
|
570 |
+
568 1
|
571 |
+
569 1
|
572 |
+
570 1
|
573 |
+
571 1
|
574 |
+
572 1
|
575 |
+
573 1
|
576 |
+
574 0
|
577 |
+
575 1
|
578 |
+
576 1
|
579 |
+
577 1
|
580 |
+
578 1
|
581 |
+
579 1
|
582 |
+
580 0
|
583 |
+
581 0
|
584 |
+
582 1
|
585 |
+
583 1
|
586 |
+
584 0
|
587 |
+
585 1
|
588 |
+
586 1
|
589 |
+
587 1
|
590 |
+
588 0
|
591 |
+
589 1
|
592 |
+
590 0
|
593 |
+
591 1
|
594 |
+
592 1
|
595 |
+
593 1
|
596 |
+
594 1
|
597 |
+
595 0
|
598 |
+
596 0
|
599 |
+
597 1
|
600 |
+
598 0
|
601 |
+
599 1
|
602 |
+
600 1
|
603 |
+
601 0
|
604 |
+
602 1
|
605 |
+
603 1
|
606 |
+
604 1
|
607 |
+
605 0
|
608 |
+
606 0
|
609 |
+
607 1
|
610 |
+
608 1
|
611 |
+
609 1
|
612 |
+
610 1
|
613 |
+
611 1
|
614 |
+
612 1
|
615 |
+
613 1
|
616 |
+
614 1
|
617 |
+
615 0
|
618 |
+
616 1
|
619 |
+
617 1
|
620 |
+
618 1
|
621 |
+
619 1
|
622 |
+
620 1
|
623 |
+
621 1
|
624 |
+
622 1
|
625 |
+
623 1
|
626 |
+
624 1
|
627 |
+
625 0
|
628 |
+
626 1
|
629 |
+
627 1
|
630 |
+
628 1
|
631 |
+
629 0
|
632 |
+
630 1
|
633 |
+
631 1
|
634 |
+
632 0
|
635 |
+
633 1
|
636 |
+
634 1
|
637 |
+
635 0
|
638 |
+
636 1
|
639 |
+
637 1
|
640 |
+
638 1
|
641 |
+
639 1
|
642 |
+
640 1
|
643 |
+
641 1
|
644 |
+
642 1
|
645 |
+
643 0
|
646 |
+
644 0
|
647 |
+
645 1
|
648 |
+
646 1
|
649 |
+
647 0
|
650 |
+
648 0
|
651 |
+
649 1
|
652 |
+
650 1
|
653 |
+
651 1
|
654 |
+
652 1
|
655 |
+
653 0
|
656 |
+
654 0
|
657 |
+
655 1
|
658 |
+
656 1
|
659 |
+
657 0
|
660 |
+
658 1
|
661 |
+
659 1
|
662 |
+
660 1
|
663 |
+
661 0
|
664 |
+
662 1
|
665 |
+
663 1
|
666 |
+
664 1
|
667 |
+
665 1
|
668 |
+
666 0
|
669 |
+
667 1
|
670 |
+
668 0
|
671 |
+
669 1
|
672 |
+
670 1
|
673 |
+
671 1
|
674 |
+
672 1
|
675 |
+
673 0
|
676 |
+
674 1
|
677 |
+
675 0
|
678 |
+
676 1
|
679 |
+
677 1
|
680 |
+
678 1
|
681 |
+
679 1
|
682 |
+
680 0
|
683 |
+
681 1
|
684 |
+
682 1
|
685 |
+
683 1
|
686 |
+
684 0
|
687 |
+
685 1
|
688 |
+
686 0
|
689 |
+
687 1
|
690 |
+
688 0
|
691 |
+
689 1
|
692 |
+
690 1
|
693 |
+
691 0
|
694 |
+
692 0
|
695 |
+
693 1
|
696 |
+
694 1
|
697 |
+
695 1
|
698 |
+
696 0
|
699 |
+
697 1
|
700 |
+
698 1
|
701 |
+
699 1
|
702 |
+
700 0
|
703 |
+
701 1
|
704 |
+
702 1
|
705 |
+
703 1
|
706 |
+
704 1
|
707 |
+
705 1
|
708 |
+
706 1
|
709 |
+
707 1
|
710 |
+
708 1
|
711 |
+
709 1
|
712 |
+
710 1
|
713 |
+
711 0
|
714 |
+
712 0
|
715 |
+
713 0
|
716 |
+
714 0
|
717 |
+
715 1
|
718 |
+
716 0
|
719 |
+
717 1
|
720 |
+
718 0
|
721 |
+
719 0
|
722 |
+
720 1
|
723 |
+
721 1
|
724 |
+
722 1
|
finetune/boolq/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c5483030e47ecf2b2adcbfba33ea2c1741db1d170a0f2b751457e63d49fd38c8
|
3 |
+
size 442624943
|
finetune/boolq/special_tokens_map.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": "<s>",
|
3 |
+
"cls_token": "<s>",
|
4 |
+
"eos_token": "</s>",
|
5 |
+
"mask_token": {
|
6 |
+
"content": "<mask>",
|
7 |
+
"lstrip": true,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"pad_token": "<pad>",
|
13 |
+
"sep_token": "</s>",
|
14 |
+
"unk_token": "<unk>"
|
15 |
+
}
|
finetune/boolq/structformer_as_hf_no_parser.py
ADDED
@@ -0,0 +1,754 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torch.nn import init
|
5 |
+
from transformers import PreTrainedModel
|
6 |
+
from transformers import PretrainedConfig
|
7 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
8 |
+
from typing import List
|
9 |
+
from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss
|
10 |
+
from transformers.modeling_outputs import (
|
11 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
12 |
+
BaseModelOutputWithPoolingAndCrossAttentions,
|
13 |
+
MaskedLMOutput,
|
14 |
+
SequenceClassifierOutput
|
15 |
+
)
|
16 |
+
|
17 |
+
##########################################
|
18 |
+
# HuggingFace Config
|
19 |
+
##########################################
|
20 |
+
class StructformerConfig(PretrainedConfig):
|
21 |
+
model_type = "structformer"
|
22 |
+
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
hidden_size=768,
|
26 |
+
n_context_layers=2,
|
27 |
+
nlayers=6,
|
28 |
+
ntokens=32000,
|
29 |
+
nhead=8,
|
30 |
+
dropout=0.1,
|
31 |
+
dropatt=0.1,
|
32 |
+
relative_bias=False,
|
33 |
+
pos_emb=False,
|
34 |
+
pad=0,
|
35 |
+
n_parser_layers=4,
|
36 |
+
conv_size=9,
|
37 |
+
relations=('head', 'child'),
|
38 |
+
weight_act='softmax',
|
39 |
+
**kwargs,
|
40 |
+
):
|
41 |
+
self.hidden_size = hidden_size
|
42 |
+
self.n_context_layers = n_context_layers
|
43 |
+
self.nlayers = nlayers
|
44 |
+
self.ntokens = ntokens
|
45 |
+
self.nhead = nhead
|
46 |
+
self.dropout = dropout
|
47 |
+
self.dropatt = dropatt
|
48 |
+
self.relative_bias = relative_bias
|
49 |
+
self.pos_emb = pos_emb
|
50 |
+
self.pad = pad
|
51 |
+
self.n_parser_layers = n_parser_layers
|
52 |
+
self.conv_size = conv_size
|
53 |
+
self.relations = relations
|
54 |
+
self.weight_act = weight_act
|
55 |
+
super().__init__(**kwargs)
|
56 |
+
|
57 |
+
##########################################
|
58 |
+
# Custom Layers
|
59 |
+
##########################################
|
60 |
+
def _get_activation_fn(activation):
|
61 |
+
"""Get specified activation function."""
|
62 |
+
if activation == "relu":
|
63 |
+
return nn.ReLU()
|
64 |
+
elif activation == "gelu":
|
65 |
+
return nn.GELU()
|
66 |
+
elif activation == "leakyrelu":
|
67 |
+
return nn.LeakyReLU()
|
68 |
+
|
69 |
+
raise RuntimeError(
|
70 |
+
"activation should be relu/gelu, not {}".format(activation))
|
71 |
+
|
72 |
+
class Conv1d(nn.Module):
|
73 |
+
"""1D convolution layer."""
|
74 |
+
|
75 |
+
def __init__(self, hidden_size, kernel_size, dilation=1):
|
76 |
+
"""Initialization.
|
77 |
+
|
78 |
+
Args:
|
79 |
+
hidden_size: dimension of input embeddings
|
80 |
+
kernel_size: convolution kernel size
|
81 |
+
dilation: the spacing between the kernel points
|
82 |
+
"""
|
83 |
+
super(Conv1d, self).__init__()
|
84 |
+
|
85 |
+
if kernel_size % 2 == 0:
|
86 |
+
padding = (kernel_size // 2) * dilation
|
87 |
+
self.shift = True
|
88 |
+
else:
|
89 |
+
padding = ((kernel_size - 1) // 2) * dilation
|
90 |
+
self.shift = False
|
91 |
+
self.conv = nn.Conv1d(
|
92 |
+
hidden_size,
|
93 |
+
hidden_size,
|
94 |
+
kernel_size,
|
95 |
+
padding=padding,
|
96 |
+
dilation=dilation)
|
97 |
+
|
98 |
+
def forward(self, x):
|
99 |
+
"""Compute convolution.
|
100 |
+
|
101 |
+
Args:
|
102 |
+
x: input embeddings
|
103 |
+
Returns:
|
104 |
+
conv_output: convolution results
|
105 |
+
"""
|
106 |
+
|
107 |
+
if self.shift:
|
108 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)[:, 1:]
|
109 |
+
else:
|
110 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)
|
111 |
+
|
112 |
+
class MultiheadAttention(nn.Module):
|
113 |
+
"""Multi-head self-attention layer."""
|
114 |
+
|
115 |
+
def __init__(self,
|
116 |
+
embed_dim,
|
117 |
+
num_heads,
|
118 |
+
dropout=0.,
|
119 |
+
bias=True,
|
120 |
+
v_proj=True,
|
121 |
+
out_proj=True,
|
122 |
+
relative_bias=True):
|
123 |
+
"""Initialization.
|
124 |
+
|
125 |
+
Args:
|
126 |
+
embed_dim: dimension of input embeddings
|
127 |
+
num_heads: number of self-attention heads
|
128 |
+
dropout: dropout rate
|
129 |
+
bias: bool, indicate whether include bias for linear transformations
|
130 |
+
v_proj: bool, indicate whether project inputs to new values
|
131 |
+
out_proj: bool, indicate whether project outputs to new values
|
132 |
+
relative_bias: bool, indicate whether use a relative position based
|
133 |
+
attention bias
|
134 |
+
"""
|
135 |
+
|
136 |
+
super(MultiheadAttention, self).__init__()
|
137 |
+
self.embed_dim = embed_dim
|
138 |
+
|
139 |
+
self.num_heads = num_heads
|
140 |
+
self.drop = nn.Dropout(dropout)
|
141 |
+
self.head_dim = embed_dim // num_heads
|
142 |
+
assert self.head_dim * num_heads == self.embed_dim, ("embed_dim must be "
|
143 |
+
"divisible by "
|
144 |
+
"num_heads")
|
145 |
+
|
146 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
147 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
148 |
+
if v_proj:
|
149 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
150 |
+
else:
|
151 |
+
self.v_proj = nn.Identity()
|
152 |
+
|
153 |
+
if out_proj:
|
154 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
155 |
+
else:
|
156 |
+
self.out_proj = nn.Identity()
|
157 |
+
|
158 |
+
if relative_bias:
|
159 |
+
self.relative_bias = nn.Parameter(torch.zeros((self.num_heads, 512)))
|
160 |
+
else:
|
161 |
+
self.relative_bias = None
|
162 |
+
|
163 |
+
self._reset_parameters()
|
164 |
+
|
165 |
+
def _reset_parameters(self):
|
166 |
+
"""Initialize attention parameters."""
|
167 |
+
|
168 |
+
init.xavier_uniform_(self.q_proj.weight)
|
169 |
+
init.constant_(self.q_proj.bias, 0.)
|
170 |
+
|
171 |
+
init.xavier_uniform_(self.k_proj.weight)
|
172 |
+
init.constant_(self.k_proj.bias, 0.)
|
173 |
+
|
174 |
+
if isinstance(self.v_proj, nn.Linear):
|
175 |
+
init.xavier_uniform_(self.v_proj.weight)
|
176 |
+
init.constant_(self.v_proj.bias, 0.)
|
177 |
+
|
178 |
+
if isinstance(self.out_proj, nn.Linear):
|
179 |
+
init.xavier_uniform_(self.out_proj.weight)
|
180 |
+
init.constant_(self.out_proj.bias, 0.)
|
181 |
+
|
182 |
+
def forward(self, query, key_padding_mask=None, attn_mask=None):
|
183 |
+
"""Compute multi-head self-attention.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
query: input embeddings
|
187 |
+
key_padding_mask: 3D mask that prevents attention to certain positions
|
188 |
+
attn_mask: 3D mask that rescale the attention weight at each position
|
189 |
+
Returns:
|
190 |
+
attn_output: self-attention output
|
191 |
+
"""
|
192 |
+
|
193 |
+
length, bsz, embed_dim = query.size()
|
194 |
+
assert embed_dim == self.embed_dim
|
195 |
+
|
196 |
+
head_dim = embed_dim // self.num_heads
|
197 |
+
assert head_dim * self.num_heads == embed_dim, ("embed_dim must be "
|
198 |
+
"divisible by num_heads")
|
199 |
+
scaling = float(head_dim)**-0.5
|
200 |
+
|
201 |
+
q = self.q_proj(query)
|
202 |
+
k = self.k_proj(query)
|
203 |
+
v = self.v_proj(query)
|
204 |
+
|
205 |
+
q = q * scaling
|
206 |
+
|
207 |
+
if attn_mask is not None:
|
208 |
+
assert list(attn_mask.size()) == [bsz * self.num_heads,
|
209 |
+
query.size(0), query.size(0)]
|
210 |
+
|
211 |
+
q = q.contiguous().view(length, bsz * self.num_heads,
|
212 |
+
head_dim).transpose(0, 1)
|
213 |
+
k = k.contiguous().view(length, bsz * self.num_heads,
|
214 |
+
head_dim).transpose(0, 1)
|
215 |
+
v = v.contiguous().view(length, bsz * self.num_heads,
|
216 |
+
head_dim).transpose(0, 1)
|
217 |
+
|
218 |
+
attn_output_weights = torch.bmm(q, k.transpose(1, 2))
|
219 |
+
assert list(
|
220 |
+
attn_output_weights.size()) == [bsz * self.num_heads, length, length]
|
221 |
+
|
222 |
+
if self.relative_bias is not None:
|
223 |
+
pos = torch.arange(length, device=query.device)
|
224 |
+
relative_pos = torch.abs(pos[:, None] - pos[None, :]) + 256
|
225 |
+
relative_pos = relative_pos[None, :, :].expand(bsz * self.num_heads, -1,
|
226 |
+
-1)
|
227 |
+
|
228 |
+
relative_bias = self.relative_bias.repeat_interleave(bsz, dim=0)
|
229 |
+
relative_bias = relative_bias[:, None, :].expand(-1, length, -1)
|
230 |
+
relative_bias = torch.gather(relative_bias, 2, relative_pos)
|
231 |
+
attn_output_weights = attn_output_weights + relative_bias
|
232 |
+
|
233 |
+
if key_padding_mask is not None:
|
234 |
+
attn_output_weights = attn_output_weights + key_padding_mask
|
235 |
+
|
236 |
+
if attn_mask is None:
|
237 |
+
attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
|
238 |
+
else:
|
239 |
+
attn_output_weights = torch.sigmoid(attn_output_weights) * attn_mask
|
240 |
+
|
241 |
+
attn_output_weights = self.drop(attn_output_weights)
|
242 |
+
|
243 |
+
attn_output = torch.bmm(attn_output_weights, v)
|
244 |
+
|
245 |
+
assert list(attn_output.size()) == [bsz * self.num_heads, length, head_dim]
|
246 |
+
attn_output = attn_output.transpose(0, 1).contiguous().view(
|
247 |
+
length, bsz, embed_dim)
|
248 |
+
attn_output = self.out_proj(attn_output)
|
249 |
+
|
250 |
+
return attn_output
|
251 |
+
|
252 |
+
class TransformerLayer(nn.Module):
|
253 |
+
"""TransformerEncoderLayer is made up of self-attn and feedforward network."""
|
254 |
+
|
255 |
+
def __init__(self,
|
256 |
+
d_model,
|
257 |
+
nhead,
|
258 |
+
dim_feedforward=2048,
|
259 |
+
dropout=0.1,
|
260 |
+
dropatt=0.1,
|
261 |
+
activation="leakyrelu",
|
262 |
+
relative_bias=True):
|
263 |
+
"""Initialization.
|
264 |
+
|
265 |
+
Args:
|
266 |
+
d_model: dimension of inputs
|
267 |
+
nhead: number of self-attention heads
|
268 |
+
dim_feedforward: dimension of hidden layer in feedforward layer
|
269 |
+
dropout: dropout rate
|
270 |
+
dropatt: drop attention rate
|
271 |
+
activation: activation function
|
272 |
+
relative_bias: bool, indicate whether use a relative position based
|
273 |
+
attention bias
|
274 |
+
"""
|
275 |
+
|
276 |
+
super(TransformerLayer, self).__init__()
|
277 |
+
|
278 |
+
self.self_attn = MultiheadAttention(
|
279 |
+
d_model, nhead, dropout=dropatt, relative_bias=relative_bias)
|
280 |
+
|
281 |
+
# Implementation of Feedforward model
|
282 |
+
self.feedforward = nn.Sequential(
|
283 |
+
nn.LayerNorm(d_model), nn.Linear(d_model, dim_feedforward),
|
284 |
+
_get_activation_fn(activation), nn.Dropout(dropout),
|
285 |
+
nn.Linear(dim_feedforward, d_model))
|
286 |
+
|
287 |
+
self.norm = nn.LayerNorm(d_model)
|
288 |
+
self.dropout1 = nn.Dropout(dropout)
|
289 |
+
self.dropout2 = nn.Dropout(dropout)
|
290 |
+
|
291 |
+
self.nhead = nhead
|
292 |
+
|
293 |
+
def forward(self, src, attn_mask=None, key_padding_mask=None):
|
294 |
+
"""Pass the input through the encoder layer.
|
295 |
+
|
296 |
+
Args:
|
297 |
+
src: the sequence to the encoder layer (required).
|
298 |
+
attn_mask: the mask for the src sequence (optional).
|
299 |
+
key_padding_mask: the mask for the src keys per batch (optional).
|
300 |
+
Returns:
|
301 |
+
src3: the output of transformer layer, share the same shape as src.
|
302 |
+
"""
|
303 |
+
src2 = self.self_attn(
|
304 |
+
self.norm(src), attn_mask=attn_mask, key_padding_mask=key_padding_mask)
|
305 |
+
src2 = src + self.dropout1(src2)
|
306 |
+
src3 = self.feedforward(src2)
|
307 |
+
src3 = src2 + self.dropout2(src3)
|
308 |
+
|
309 |
+
return src3
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
class RobertaClassificationHead(nn.Module):
|
314 |
+
"""Head for sentence-level classification tasks."""
|
315 |
+
|
316 |
+
def __init__(self, config):
|
317 |
+
super().__init__()
|
318 |
+
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
|
319 |
+
classifier_dropout = (
|
320 |
+
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
|
321 |
+
)
|
322 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
323 |
+
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
|
324 |
+
|
325 |
+
def forward(self, features, **kwargs):
|
326 |
+
x = features[:, 0, :] # take <s> token (equiv. to [CLS])
|
327 |
+
x = self.dropout(x)
|
328 |
+
x = self.dense(x)
|
329 |
+
x = torch.tanh(x)
|
330 |
+
x = self.dropout(x)
|
331 |
+
x = self.out_proj(x)
|
332 |
+
return x
|
333 |
+
|
334 |
+
|
335 |
+
##########################################
|
336 |
+
# Custom Models
|
337 |
+
##########################################
|
338 |
+
def cumprod(x, reverse=False, exclusive=False):
|
339 |
+
"""cumulative product."""
|
340 |
+
if reverse:
|
341 |
+
x = x.flip([-1])
|
342 |
+
|
343 |
+
if exclusive:
|
344 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=1)
|
345 |
+
|
346 |
+
cx = x.cumprod(-1)
|
347 |
+
|
348 |
+
if reverse:
|
349 |
+
cx = cx.flip([-1])
|
350 |
+
return cx
|
351 |
+
|
352 |
+
def cumsum(x, reverse=False, exclusive=False):
|
353 |
+
"""cumulative sum."""
|
354 |
+
bsz, _, length = x.size()
|
355 |
+
device = x.device
|
356 |
+
if reverse:
|
357 |
+
if exclusive:
|
358 |
+
w = torch.ones([bsz, length, length], device=device).tril(-1)
|
359 |
+
else:
|
360 |
+
w = torch.ones([bsz, length, length], device=device).tril(0)
|
361 |
+
cx = torch.bmm(x, w)
|
362 |
+
else:
|
363 |
+
if exclusive:
|
364 |
+
w = torch.ones([bsz, length, length], device=device).triu(1)
|
365 |
+
else:
|
366 |
+
w = torch.ones([bsz, length, length], device=device).triu(0)
|
367 |
+
cx = torch.bmm(x, w)
|
368 |
+
return cx
|
369 |
+
|
370 |
+
def cummin(x, reverse=False, exclusive=False, max_value=1e9):
|
371 |
+
"""cumulative min."""
|
372 |
+
if reverse:
|
373 |
+
if exclusive:
|
374 |
+
x = F.pad(x[:, :, 1:], (0, 1), value=max_value)
|
375 |
+
x = x.flip([-1]).cummin(-1)[0].flip([-1])
|
376 |
+
else:
|
377 |
+
if exclusive:
|
378 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=max_value)
|
379 |
+
x = x.cummin(-1)[0]
|
380 |
+
return x
|
381 |
+
|
382 |
+
class Transformer(nn.Module):
|
383 |
+
"""Transformer model."""
|
384 |
+
|
385 |
+
def __init__(self,
|
386 |
+
hidden_size,
|
387 |
+
nlayers,
|
388 |
+
ntokens,
|
389 |
+
nhead=8,
|
390 |
+
dropout=0.1,
|
391 |
+
dropatt=0.1,
|
392 |
+
relative_bias=True,
|
393 |
+
pos_emb=False,
|
394 |
+
pad=0):
|
395 |
+
"""Initialization.
|
396 |
+
|
397 |
+
Args:
|
398 |
+
hidden_size: dimension of inputs and hidden states
|
399 |
+
nlayers: number of layers
|
400 |
+
ntokens: number of output categories
|
401 |
+
nhead: number of self-attention heads
|
402 |
+
dropout: dropout rate
|
403 |
+
dropatt: drop attention rate
|
404 |
+
relative_bias: bool, indicate whether use a relative position based
|
405 |
+
attention bias
|
406 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
407 |
+
pad: pad token index
|
408 |
+
"""
|
409 |
+
|
410 |
+
super(Transformer, self).__init__()
|
411 |
+
|
412 |
+
self.drop = nn.Dropout(dropout)
|
413 |
+
|
414 |
+
self.emb = nn.Embedding(ntokens, hidden_size)
|
415 |
+
if pos_emb:
|
416 |
+
self.pos_emb = nn.Embedding(500, hidden_size)
|
417 |
+
|
418 |
+
self.layers = nn.ModuleList([
|
419 |
+
TransformerLayer(hidden_size, nhead, hidden_size * 4, dropout,
|
420 |
+
dropatt=dropatt, relative_bias=relative_bias)
|
421 |
+
for _ in range(nlayers)])
|
422 |
+
|
423 |
+
self.norm = nn.LayerNorm(hidden_size)
|
424 |
+
|
425 |
+
self.output_layer = nn.Linear(hidden_size, ntokens)
|
426 |
+
self.output_layer.weight = self.emb.weight
|
427 |
+
|
428 |
+
self.init_weights()
|
429 |
+
|
430 |
+
self.nlayers = nlayers
|
431 |
+
self.nhead = nhead
|
432 |
+
self.ntokens = ntokens
|
433 |
+
self.hidden_size = hidden_size
|
434 |
+
self.pad = pad
|
435 |
+
|
436 |
+
def init_weights(self):
|
437 |
+
"""Initialize token embedding and output bias."""
|
438 |
+
initrange = 0.1
|
439 |
+
self.emb.weight.data.uniform_(-initrange, initrange)
|
440 |
+
if hasattr(self, 'pos_emb'):
|
441 |
+
self.pos_emb.weight.data.uniform_(-initrange, initrange)
|
442 |
+
self.output_layer.bias.data.fill_(0)
|
443 |
+
|
444 |
+
def visibility(self, x, device):
|
445 |
+
"""Mask pad tokens."""
|
446 |
+
visibility = (x != self.pad).float()
|
447 |
+
visibility = visibility[:, None, :].expand(-1, x.size(1), -1)
|
448 |
+
visibility = torch.repeat_interleave(visibility, self.nhead, dim=0)
|
449 |
+
return visibility.log()
|
450 |
+
|
451 |
+
def encode(self, x, pos):
|
452 |
+
"""Standard transformer encode process."""
|
453 |
+
h = self.emb(x)
|
454 |
+
if hasattr(self, 'pos_emb'):
|
455 |
+
h = h + self.pos_emb(pos)
|
456 |
+
h_list = []
|
457 |
+
visibility = self.visibility(x, x.device)
|
458 |
+
|
459 |
+
for i in range(self.nlayers):
|
460 |
+
h_list.append(h)
|
461 |
+
h = self.layers[i](
|
462 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
463 |
+
|
464 |
+
output = h
|
465 |
+
h_array = torch.stack(h_list, dim=2)
|
466 |
+
|
467 |
+
return output, h_array
|
468 |
+
|
469 |
+
def forward(self, x, pos):
|
470 |
+
"""Pass the input through the encoder layer.
|
471 |
+
|
472 |
+
Args:
|
473 |
+
x: input tokens (required).
|
474 |
+
pos: position for each token (optional).
|
475 |
+
Returns:
|
476 |
+
output: probability distributions for missing tokens.
|
477 |
+
state_dict: parsing results and raw output
|
478 |
+
"""
|
479 |
+
|
480 |
+
batch_size, length = x.size()
|
481 |
+
|
482 |
+
raw_output, _ = self.encode(x, pos)
|
483 |
+
raw_output = self.norm(raw_output)
|
484 |
+
raw_output = self.drop(raw_output)
|
485 |
+
|
486 |
+
output = self.output_layer(raw_output)
|
487 |
+
return output.view(batch_size * length, -1), {'raw_output': raw_output,}
|
488 |
+
|
489 |
+
class StructFormer(Transformer):
|
490 |
+
"""StructFormer model."""
|
491 |
+
|
492 |
+
def __init__(self,
|
493 |
+
hidden_size,
|
494 |
+
n_context_layers,
|
495 |
+
nlayers,
|
496 |
+
ntokens,
|
497 |
+
nhead=8,
|
498 |
+
dropout=0.1,
|
499 |
+
dropatt=0.1,
|
500 |
+
relative_bias=False,
|
501 |
+
pos_emb=False,
|
502 |
+
pad=0,
|
503 |
+
n_parser_layers=4,
|
504 |
+
conv_size=9,
|
505 |
+
relations=('head', 'child'),
|
506 |
+
weight_act='softmax'):
|
507 |
+
"""Initialization.
|
508 |
+
|
509 |
+
Args:
|
510 |
+
hidden_size: dimension of inputs and hidden states
|
511 |
+
nlayers: number of layers
|
512 |
+
ntokens: number of output categories
|
513 |
+
nhead: number of self-attention heads
|
514 |
+
dropout: dropout rate
|
515 |
+
dropatt: drop attention rate
|
516 |
+
relative_bias: bool, indicate whether use a relative position based
|
517 |
+
attention bias
|
518 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
519 |
+
pad: pad token index
|
520 |
+
n_parser_layers: number of parsing layers
|
521 |
+
conv_size: convolution kernel size for parser
|
522 |
+
relations: relations that are used to compute self attention
|
523 |
+
weight_act: relations distribution activation function
|
524 |
+
"""
|
525 |
+
|
526 |
+
super(StructFormer, self).__init__(
|
527 |
+
hidden_size,
|
528 |
+
nlayers,
|
529 |
+
ntokens,
|
530 |
+
nhead=nhead,
|
531 |
+
dropout=dropout,
|
532 |
+
dropatt=dropatt,
|
533 |
+
relative_bias=relative_bias,
|
534 |
+
pos_emb=pos_emb,
|
535 |
+
pad=pad)
|
536 |
+
|
537 |
+
|
538 |
+
def encode(self, x, pos):
|
539 |
+
h = self.emb(x)
|
540 |
+
if hasattr(self, 'pos_emb'):
|
541 |
+
h = h + self.pos_emb(pos)
|
542 |
+
h_list = []
|
543 |
+
visibility = self.visibility(x, x.device)
|
544 |
+
|
545 |
+
for i in range(self.nlayers):
|
546 |
+
h_list.append(h)
|
547 |
+
h = self.layers[i](
|
548 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
549 |
+
|
550 |
+
output = h
|
551 |
+
h_array = torch.stack(h_list, dim=2)
|
552 |
+
|
553 |
+
return output
|
554 |
+
|
555 |
+
|
556 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
557 |
+
|
558 |
+
x = input_ids
|
559 |
+
batch_size, length = x.size()
|
560 |
+
|
561 |
+
if position_ids is None:
|
562 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
563 |
+
|
564 |
+
raw_output = self.encode(x, pos)
|
565 |
+
raw_output = self.norm(raw_output)
|
566 |
+
raw_output = self.drop(raw_output)
|
567 |
+
|
568 |
+
output = self.output_layer(raw_output)
|
569 |
+
|
570 |
+
loss = None
|
571 |
+
if labels is not None:
|
572 |
+
loss_fct = nn.CrossEntropyLoss()
|
573 |
+
loss = loss_fct(output.view(batch_size * length, -1), labels.reshape(-1))
|
574 |
+
|
575 |
+
return MaskedLMOutput(
|
576 |
+
loss=loss, # shape: 1
|
577 |
+
logits=output, # shape: (batch_size * length, ntokens)
|
578 |
+
hidden_states=None,
|
579 |
+
attentions=None,
|
580 |
+
)
|
581 |
+
|
582 |
+
##########################################
|
583 |
+
# HuggingFace Model
|
584 |
+
##########################################
|
585 |
+
class StructformerModel(PreTrainedModel):
|
586 |
+
config_class = StructformerConfig
|
587 |
+
|
588 |
+
def __init__(self, config):
|
589 |
+
super().__init__(config)
|
590 |
+
self.model = StructFormer(
|
591 |
+
hidden_size=config.hidden_size,
|
592 |
+
n_context_layers=config.n_context_layers,
|
593 |
+
nlayers=config.nlayers,
|
594 |
+
ntokens=config.ntokens,
|
595 |
+
nhead=config.nhead,
|
596 |
+
dropout=config.dropout,
|
597 |
+
dropatt=config.dropatt,
|
598 |
+
relative_bias=config.relative_bias,
|
599 |
+
pos_emb=config.pos_emb,
|
600 |
+
pad=config.pad,
|
601 |
+
n_parser_layers=config.n_parser_layers,
|
602 |
+
conv_size=config.conv_size,
|
603 |
+
relations=config.relations,
|
604 |
+
weight_act=config.weight_act
|
605 |
+
)
|
606 |
+
|
607 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
608 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
609 |
+
|
610 |
+
|
611 |
+
class StructFormerClassification(Transformer):
|
612 |
+
"""StructFormer model."""
|
613 |
+
|
614 |
+
def __init__(self,
|
615 |
+
hidden_size,
|
616 |
+
n_context_layers,
|
617 |
+
nlayers,
|
618 |
+
ntokens,
|
619 |
+
nhead=8,
|
620 |
+
dropout=0.1,
|
621 |
+
dropatt=0.1,
|
622 |
+
relative_bias=False,
|
623 |
+
pos_emb=False,
|
624 |
+
pad=0,
|
625 |
+
n_parser_layers=4,
|
626 |
+
conv_size=9,
|
627 |
+
relations=('head', 'child'),
|
628 |
+
weight_act='softmax',
|
629 |
+
config=None,
|
630 |
+
):
|
631 |
+
|
632 |
+
|
633 |
+
super(StructFormerClassification, self).__init__(
|
634 |
+
hidden_size,
|
635 |
+
nlayers,
|
636 |
+
ntokens,
|
637 |
+
nhead=nhead,
|
638 |
+
dropout=dropout,
|
639 |
+
dropatt=dropatt,
|
640 |
+
relative_bias=relative_bias,
|
641 |
+
pos_emb=pos_emb,
|
642 |
+
pad=pad)
|
643 |
+
|
644 |
+
self.num_labels = config.num_labels
|
645 |
+
self.config = config
|
646 |
+
|
647 |
+
self.classifier = RobertaClassificationHead(config)
|
648 |
+
|
649 |
+
def encode(self, x, pos):
|
650 |
+
h = self.emb(x)
|
651 |
+
if hasattr(self, 'pos_emb'):
|
652 |
+
h = h + self.pos_emb(pos)
|
653 |
+
h_list = []
|
654 |
+
visibility = self.visibility(x, x.device)
|
655 |
+
|
656 |
+
for i in range(self.nlayers):
|
657 |
+
h_list.append(h)
|
658 |
+
h = self.layers[i](
|
659 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
660 |
+
|
661 |
+
output = h
|
662 |
+
h_array = torch.stack(h_list, dim=2)
|
663 |
+
|
664 |
+
return output
|
665 |
+
|
666 |
+
|
667 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
668 |
+
|
669 |
+
x = input_ids
|
670 |
+
batch_size, length = x.size()
|
671 |
+
|
672 |
+
if position_ids is None:
|
673 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
674 |
+
|
675 |
+
raw_output = self.encode(x, pos)
|
676 |
+
raw_output = self.norm(raw_output)
|
677 |
+
raw_output = self.drop(raw_output)
|
678 |
+
|
679 |
+
#output = self.output_layer(raw_output)
|
680 |
+
logits = self.classifier(raw_output)
|
681 |
+
|
682 |
+
loss = None
|
683 |
+
if labels is not None:
|
684 |
+
if self.config.problem_type is None:
|
685 |
+
if self.num_labels == 1:
|
686 |
+
self.config.problem_type = "regression"
|
687 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
688 |
+
self.config.problem_type = "single_label_classification"
|
689 |
+
else:
|
690 |
+
self.config.problem_type = "multi_label_classification"
|
691 |
+
|
692 |
+
if self.config.problem_type == "regression":
|
693 |
+
loss_fct = MSELoss()
|
694 |
+
if self.num_labels == 1:
|
695 |
+
loss = loss_fct(logits.squeeze(), labels.squeeze())
|
696 |
+
else:
|
697 |
+
loss = loss_fct(logits, labels)
|
698 |
+
elif self.config.problem_type == "single_label_classification":
|
699 |
+
loss_fct = CrossEntropyLoss()
|
700 |
+
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
701 |
+
elif self.config.problem_type == "multi_label_classification":
|
702 |
+
loss_fct = BCEWithLogitsLoss()
|
703 |
+
loss = loss_fct(logits, labels)
|
704 |
+
|
705 |
+
|
706 |
+
return SequenceClassifierOutput(
|
707 |
+
loss=loss,
|
708 |
+
logits=logits,
|
709 |
+
hidden_states=None,
|
710 |
+
attentions=None,
|
711 |
+
)
|
712 |
+
|
713 |
+
|
714 |
+
class StructformerModelForSequenceClassification(PreTrainedModel):
|
715 |
+
config_class = StructformerConfig
|
716 |
+
def __init__(self, config):
|
717 |
+
super().__init__(config)
|
718 |
+
self.model = StructFormerClassification(
|
719 |
+
hidden_size=config.hidden_size,
|
720 |
+
n_context_layers=config.n_context_layers,
|
721 |
+
nlayers=config.nlayers,
|
722 |
+
ntokens=config.ntokens,
|
723 |
+
nhead=config.nhead,
|
724 |
+
dropout=config.dropout,
|
725 |
+
dropatt=config.dropatt,
|
726 |
+
relative_bias=config.relative_bias,
|
727 |
+
pos_emb=config.pos_emb,
|
728 |
+
pad=config.pad,
|
729 |
+
n_parser_layers=config.n_parser_layers,
|
730 |
+
conv_size=config.conv_size,
|
731 |
+
relations=config.relations,
|
732 |
+
weight_act=config.weight_act,
|
733 |
+
config=config)
|
734 |
+
|
735 |
+
def _init_weights(self, module):
|
736 |
+
"""Initialize the weights"""
|
737 |
+
if isinstance(module, nn.Linear):
|
738 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
739 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
740 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
741 |
+
if module.bias is not None:
|
742 |
+
module.bias.data.zero_()
|
743 |
+
elif isinstance(module, nn.Embedding):
|
744 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
745 |
+
if module.padding_idx is not None:
|
746 |
+
module.weight.data[module.padding_idx].zero_()
|
747 |
+
elif isinstance(module, nn.LayerNorm):
|
748 |
+
if module.bias is not None:
|
749 |
+
module.bias.data.zero_()
|
750 |
+
module.weight.data.fill_(1.0)
|
751 |
+
|
752 |
+
|
753 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
754 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
finetune/boolq/tokenizer_config.json
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"bos_token": {
|
4 |
+
"__type": "AddedToken",
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false
|
10 |
+
},
|
11 |
+
"cls_token": {
|
12 |
+
"__type": "AddedToken",
|
13 |
+
"content": "<s>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false
|
18 |
+
},
|
19 |
+
"eos_token": {
|
20 |
+
"__type": "AddedToken",
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false
|
26 |
+
},
|
27 |
+
"errors": "replace",
|
28 |
+
"mask_token": {
|
29 |
+
"__type": "AddedToken",
|
30 |
+
"content": "<mask>",
|
31 |
+
"lstrip": true,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false
|
35 |
+
},
|
36 |
+
"model_max_length": 512,
|
37 |
+
"name_or_path": "final_models/transformer_base_final_2",
|
38 |
+
"pad_token": {
|
39 |
+
"__type": "AddedToken",
|
40 |
+
"content": "<pad>",
|
41 |
+
"lstrip": false,
|
42 |
+
"normalized": true,
|
43 |
+
"rstrip": false,
|
44 |
+
"single_word": false
|
45 |
+
},
|
46 |
+
"sep_token": {
|
47 |
+
"__type": "AddedToken",
|
48 |
+
"content": "</s>",
|
49 |
+
"lstrip": false,
|
50 |
+
"normalized": true,
|
51 |
+
"rstrip": false,
|
52 |
+
"single_word": false
|
53 |
+
},
|
54 |
+
"special_tokens_map_file": null,
|
55 |
+
"tokenizer_class": "RobertaTokenizer",
|
56 |
+
"trim_offsets": true,
|
57 |
+
"unk_token": {
|
58 |
+
"__type": "AddedToken",
|
59 |
+
"content": "<unk>",
|
60 |
+
"lstrip": false,
|
61 |
+
"normalized": true,
|
62 |
+
"rstrip": false,
|
63 |
+
"single_word": false
|
64 |
+
}
|
65 |
+
}
|
finetune/boolq/train_results.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"train_loss": 0.33231258392333984,
|
4 |
+
"train_runtime": 67.9575,
|
5 |
+
"train_samples": 2072,
|
6 |
+
"train_samples_per_second": 304.896,
|
7 |
+
"train_steps_per_second": 2.649
|
8 |
+
}
|
finetune/boolq/trainer_state.json
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": null,
|
3 |
+
"best_model_checkpoint": null,
|
4 |
+
"epoch": 10.0,
|
5 |
+
"global_step": 180,
|
6 |
+
"is_hyper_param_search": false,
|
7 |
+
"is_local_process_zero": true,
|
8 |
+
"is_world_process_zero": true,
|
9 |
+
"log_history": [
|
10 |
+
{
|
11 |
+
"epoch": 10.0,
|
12 |
+
"step": 180,
|
13 |
+
"total_flos": 1363424481484800.0,
|
14 |
+
"train_loss": 0.33231258392333984,
|
15 |
+
"train_runtime": 67.9575,
|
16 |
+
"train_samples_per_second": 304.896,
|
17 |
+
"train_steps_per_second": 2.649
|
18 |
+
}
|
19 |
+
],
|
20 |
+
"max_steps": 180,
|
21 |
+
"num_train_epochs": 10,
|
22 |
+
"total_flos": 1363424481484800.0,
|
23 |
+
"trial_name": null,
|
24 |
+
"trial_params": null
|
25 |
+
}
|
finetune/boolq/training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5c49e448b89277e79f0f1ccaf10e6cae9a79ed2754e23dd1d3fde17f45d1945b
|
3 |
+
size 3503
|
finetune/boolq/vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/cola/all_results.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"eval_accuracy": 0.6712462902069092,
|
4 |
+
"eval_f1": 0.7777040477770405,
|
5 |
+
"eval_loss": 0.7399011254310608,
|
6 |
+
"eval_mcc": 0.15981907397227785,
|
7 |
+
"eval_runtime": 1.4236,
|
8 |
+
"eval_samples": 1019,
|
9 |
+
"eval_samples_per_second": 715.815,
|
10 |
+
"eval_steps_per_second": 89.916,
|
11 |
+
"train_loss": 0.42006420190783517,
|
12 |
+
"train_runtime": 267.2668,
|
13 |
+
"train_samples": 8164,
|
14 |
+
"train_samples_per_second": 305.463,
|
15 |
+
"train_steps_per_second": 2.582
|
16 |
+
}
|
finetune/cola/checkpoint-400/config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "final_models/transformer_base_final_2",
|
3 |
+
"architectures": [
|
4 |
+
"StructformerModelForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
9 |
+
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel",
|
10 |
+
"AutoModelForSequenceClassification": "structformer_as_hf_no_parser.StructformerModelForSequenceClassification"
|
11 |
+
},
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"classifier_dropout": null,
|
14 |
+
"conv_size": 9,
|
15 |
+
"dropatt": 0.1,
|
16 |
+
"dropout": 0.1,
|
17 |
+
"eos_token_id": 2,
|
18 |
+
"hidden_act": "gelu",
|
19 |
+
"hidden_dropout_prob": 0.1,
|
20 |
+
"hidden_size": 768,
|
21 |
+
"id2label": {
|
22 |
+
"0": 0,
|
23 |
+
"1": 1
|
24 |
+
},
|
25 |
+
"initializer_range": 0.02,
|
26 |
+
"intermediate_size": 3072,
|
27 |
+
"label2id": {
|
28 |
+
"0": 0,
|
29 |
+
"1": 1
|
30 |
+
},
|
31 |
+
"layer_norm_eps": 1e-05,
|
32 |
+
"max_position_embeddings": 514,
|
33 |
+
"model_type": "structformer",
|
34 |
+
"n_context_layers": 0,
|
35 |
+
"n_parser_layers": 0,
|
36 |
+
"nhead": 12,
|
37 |
+
"nlayers": 12,
|
38 |
+
"ntokens": 32000,
|
39 |
+
"num_attention_heads": 12,
|
40 |
+
"num_hidden_layers": 12,
|
41 |
+
"pad": 0,
|
42 |
+
"pad_token_id": 1,
|
43 |
+
"pos_emb": true,
|
44 |
+
"position_embedding_type": "absolute",
|
45 |
+
"problem_type": "single_label_classification",
|
46 |
+
"relations": [
|
47 |
+
"head",
|
48 |
+
"child"
|
49 |
+
],
|
50 |
+
"relative_bias": false,
|
51 |
+
"torch_dtype": "float32",
|
52 |
+
"transformers_version": "4.26.1",
|
53 |
+
"type_vocab_size": 1,
|
54 |
+
"use_cache": true,
|
55 |
+
"vocab_size": 32000,
|
56 |
+
"weight_act": "softmax"
|
57 |
+
}
|
finetune/cola/checkpoint-400/merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/cola/checkpoint-400/optimizer.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3ac0a55ab34318598e03256dc8115b7cdbe039ebcd7ae1e842a5c7b2add862cd
|
3 |
+
size 884981025
|
finetune/cola/checkpoint-400/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e347d72000b0f5a8771aa62eedeb5a78b9aa79669b5796edb1e66f8e21e832c
|
3 |
+
size 442624943
|
finetune/cola/checkpoint-400/rng_state.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:5769fda57687e02db326bee3281d32f541cdc788f737f37c47c3a91239b699cc
|
3 |
+
size 14503
|
finetune/cola/checkpoint-400/scheduler.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2b09db5c9b9264697e44562c70af05a12a67180f480992d419fa287214c7be7a
|
3 |
+
size 623
|
finetune/cola/checkpoint-400/special_tokens_map.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": "<s>",
|
3 |
+
"cls_token": "<s>",
|
4 |
+
"eos_token": "</s>",
|
5 |
+
"mask_token": {
|
6 |
+
"content": "<mask>",
|
7 |
+
"lstrip": true,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"pad_token": "<pad>",
|
13 |
+
"sep_token": "</s>",
|
14 |
+
"unk_token": "<unk>"
|
15 |
+
}
|
finetune/cola/checkpoint-400/structformer_as_hf_no_parser.py
ADDED
@@ -0,0 +1,754 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torch.nn import init
|
5 |
+
from transformers import PreTrainedModel
|
6 |
+
from transformers import PretrainedConfig
|
7 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
8 |
+
from typing import List
|
9 |
+
from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss
|
10 |
+
from transformers.modeling_outputs import (
|
11 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
12 |
+
BaseModelOutputWithPoolingAndCrossAttentions,
|
13 |
+
MaskedLMOutput,
|
14 |
+
SequenceClassifierOutput
|
15 |
+
)
|
16 |
+
|
17 |
+
##########################################
|
18 |
+
# HuggingFace Config
|
19 |
+
##########################################
|
20 |
+
class StructformerConfig(PretrainedConfig):
|
21 |
+
model_type = "structformer"
|
22 |
+
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
hidden_size=768,
|
26 |
+
n_context_layers=2,
|
27 |
+
nlayers=6,
|
28 |
+
ntokens=32000,
|
29 |
+
nhead=8,
|
30 |
+
dropout=0.1,
|
31 |
+
dropatt=0.1,
|
32 |
+
relative_bias=False,
|
33 |
+
pos_emb=False,
|
34 |
+
pad=0,
|
35 |
+
n_parser_layers=4,
|
36 |
+
conv_size=9,
|
37 |
+
relations=('head', 'child'),
|
38 |
+
weight_act='softmax',
|
39 |
+
**kwargs,
|
40 |
+
):
|
41 |
+
self.hidden_size = hidden_size
|
42 |
+
self.n_context_layers = n_context_layers
|
43 |
+
self.nlayers = nlayers
|
44 |
+
self.ntokens = ntokens
|
45 |
+
self.nhead = nhead
|
46 |
+
self.dropout = dropout
|
47 |
+
self.dropatt = dropatt
|
48 |
+
self.relative_bias = relative_bias
|
49 |
+
self.pos_emb = pos_emb
|
50 |
+
self.pad = pad
|
51 |
+
self.n_parser_layers = n_parser_layers
|
52 |
+
self.conv_size = conv_size
|
53 |
+
self.relations = relations
|
54 |
+
self.weight_act = weight_act
|
55 |
+
super().__init__(**kwargs)
|
56 |
+
|
57 |
+
##########################################
|
58 |
+
# Custom Layers
|
59 |
+
##########################################
|
60 |
+
def _get_activation_fn(activation):
|
61 |
+
"""Get specified activation function."""
|
62 |
+
if activation == "relu":
|
63 |
+
return nn.ReLU()
|
64 |
+
elif activation == "gelu":
|
65 |
+
return nn.GELU()
|
66 |
+
elif activation == "leakyrelu":
|
67 |
+
return nn.LeakyReLU()
|
68 |
+
|
69 |
+
raise RuntimeError(
|
70 |
+
"activation should be relu/gelu, not {}".format(activation))
|
71 |
+
|
72 |
+
class Conv1d(nn.Module):
|
73 |
+
"""1D convolution layer."""
|
74 |
+
|
75 |
+
def __init__(self, hidden_size, kernel_size, dilation=1):
|
76 |
+
"""Initialization.
|
77 |
+
|
78 |
+
Args:
|
79 |
+
hidden_size: dimension of input embeddings
|
80 |
+
kernel_size: convolution kernel size
|
81 |
+
dilation: the spacing between the kernel points
|
82 |
+
"""
|
83 |
+
super(Conv1d, self).__init__()
|
84 |
+
|
85 |
+
if kernel_size % 2 == 0:
|
86 |
+
padding = (kernel_size // 2) * dilation
|
87 |
+
self.shift = True
|
88 |
+
else:
|
89 |
+
padding = ((kernel_size - 1) // 2) * dilation
|
90 |
+
self.shift = False
|
91 |
+
self.conv = nn.Conv1d(
|
92 |
+
hidden_size,
|
93 |
+
hidden_size,
|
94 |
+
kernel_size,
|
95 |
+
padding=padding,
|
96 |
+
dilation=dilation)
|
97 |
+
|
98 |
+
def forward(self, x):
|
99 |
+
"""Compute convolution.
|
100 |
+
|
101 |
+
Args:
|
102 |
+
x: input embeddings
|
103 |
+
Returns:
|
104 |
+
conv_output: convolution results
|
105 |
+
"""
|
106 |
+
|
107 |
+
if self.shift:
|
108 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)[:, 1:]
|
109 |
+
else:
|
110 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)
|
111 |
+
|
112 |
+
class MultiheadAttention(nn.Module):
|
113 |
+
"""Multi-head self-attention layer."""
|
114 |
+
|
115 |
+
def __init__(self,
|
116 |
+
embed_dim,
|
117 |
+
num_heads,
|
118 |
+
dropout=0.,
|
119 |
+
bias=True,
|
120 |
+
v_proj=True,
|
121 |
+
out_proj=True,
|
122 |
+
relative_bias=True):
|
123 |
+
"""Initialization.
|
124 |
+
|
125 |
+
Args:
|
126 |
+
embed_dim: dimension of input embeddings
|
127 |
+
num_heads: number of self-attention heads
|
128 |
+
dropout: dropout rate
|
129 |
+
bias: bool, indicate whether include bias for linear transformations
|
130 |
+
v_proj: bool, indicate whether project inputs to new values
|
131 |
+
out_proj: bool, indicate whether project outputs to new values
|
132 |
+
relative_bias: bool, indicate whether use a relative position based
|
133 |
+
attention bias
|
134 |
+
"""
|
135 |
+
|
136 |
+
super(MultiheadAttention, self).__init__()
|
137 |
+
self.embed_dim = embed_dim
|
138 |
+
|
139 |
+
self.num_heads = num_heads
|
140 |
+
self.drop = nn.Dropout(dropout)
|
141 |
+
self.head_dim = embed_dim // num_heads
|
142 |
+
assert self.head_dim * num_heads == self.embed_dim, ("embed_dim must be "
|
143 |
+
"divisible by "
|
144 |
+
"num_heads")
|
145 |
+
|
146 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
147 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
148 |
+
if v_proj:
|
149 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
150 |
+
else:
|
151 |
+
self.v_proj = nn.Identity()
|
152 |
+
|
153 |
+
if out_proj:
|
154 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
155 |
+
else:
|
156 |
+
self.out_proj = nn.Identity()
|
157 |
+
|
158 |
+
if relative_bias:
|
159 |
+
self.relative_bias = nn.Parameter(torch.zeros((self.num_heads, 512)))
|
160 |
+
else:
|
161 |
+
self.relative_bias = None
|
162 |
+
|
163 |
+
self._reset_parameters()
|
164 |
+
|
165 |
+
def _reset_parameters(self):
|
166 |
+
"""Initialize attention parameters."""
|
167 |
+
|
168 |
+
init.xavier_uniform_(self.q_proj.weight)
|
169 |
+
init.constant_(self.q_proj.bias, 0.)
|
170 |
+
|
171 |
+
init.xavier_uniform_(self.k_proj.weight)
|
172 |
+
init.constant_(self.k_proj.bias, 0.)
|
173 |
+
|
174 |
+
if isinstance(self.v_proj, nn.Linear):
|
175 |
+
init.xavier_uniform_(self.v_proj.weight)
|
176 |
+
init.constant_(self.v_proj.bias, 0.)
|
177 |
+
|
178 |
+
if isinstance(self.out_proj, nn.Linear):
|
179 |
+
init.xavier_uniform_(self.out_proj.weight)
|
180 |
+
init.constant_(self.out_proj.bias, 0.)
|
181 |
+
|
182 |
+
def forward(self, query, key_padding_mask=None, attn_mask=None):
|
183 |
+
"""Compute multi-head self-attention.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
query: input embeddings
|
187 |
+
key_padding_mask: 3D mask that prevents attention to certain positions
|
188 |
+
attn_mask: 3D mask that rescale the attention weight at each position
|
189 |
+
Returns:
|
190 |
+
attn_output: self-attention output
|
191 |
+
"""
|
192 |
+
|
193 |
+
length, bsz, embed_dim = query.size()
|
194 |
+
assert embed_dim == self.embed_dim
|
195 |
+
|
196 |
+
head_dim = embed_dim // self.num_heads
|
197 |
+
assert head_dim * self.num_heads == embed_dim, ("embed_dim must be "
|
198 |
+
"divisible by num_heads")
|
199 |
+
scaling = float(head_dim)**-0.5
|
200 |
+
|
201 |
+
q = self.q_proj(query)
|
202 |
+
k = self.k_proj(query)
|
203 |
+
v = self.v_proj(query)
|
204 |
+
|
205 |
+
q = q * scaling
|
206 |
+
|
207 |
+
if attn_mask is not None:
|
208 |
+
assert list(attn_mask.size()) == [bsz * self.num_heads,
|
209 |
+
query.size(0), query.size(0)]
|
210 |
+
|
211 |
+
q = q.contiguous().view(length, bsz * self.num_heads,
|
212 |
+
head_dim).transpose(0, 1)
|
213 |
+
k = k.contiguous().view(length, bsz * self.num_heads,
|
214 |
+
head_dim).transpose(0, 1)
|
215 |
+
v = v.contiguous().view(length, bsz * self.num_heads,
|
216 |
+
head_dim).transpose(0, 1)
|
217 |
+
|
218 |
+
attn_output_weights = torch.bmm(q, k.transpose(1, 2))
|
219 |
+
assert list(
|
220 |
+
attn_output_weights.size()) == [bsz * self.num_heads, length, length]
|
221 |
+
|
222 |
+
if self.relative_bias is not None:
|
223 |
+
pos = torch.arange(length, device=query.device)
|
224 |
+
relative_pos = torch.abs(pos[:, None] - pos[None, :]) + 256
|
225 |
+
relative_pos = relative_pos[None, :, :].expand(bsz * self.num_heads, -1,
|
226 |
+
-1)
|
227 |
+
|
228 |
+
relative_bias = self.relative_bias.repeat_interleave(bsz, dim=0)
|
229 |
+
relative_bias = relative_bias[:, None, :].expand(-1, length, -1)
|
230 |
+
relative_bias = torch.gather(relative_bias, 2, relative_pos)
|
231 |
+
attn_output_weights = attn_output_weights + relative_bias
|
232 |
+
|
233 |
+
if key_padding_mask is not None:
|
234 |
+
attn_output_weights = attn_output_weights + key_padding_mask
|
235 |
+
|
236 |
+
if attn_mask is None:
|
237 |
+
attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
|
238 |
+
else:
|
239 |
+
attn_output_weights = torch.sigmoid(attn_output_weights) * attn_mask
|
240 |
+
|
241 |
+
attn_output_weights = self.drop(attn_output_weights)
|
242 |
+
|
243 |
+
attn_output = torch.bmm(attn_output_weights, v)
|
244 |
+
|
245 |
+
assert list(attn_output.size()) == [bsz * self.num_heads, length, head_dim]
|
246 |
+
attn_output = attn_output.transpose(0, 1).contiguous().view(
|
247 |
+
length, bsz, embed_dim)
|
248 |
+
attn_output = self.out_proj(attn_output)
|
249 |
+
|
250 |
+
return attn_output
|
251 |
+
|
252 |
+
class TransformerLayer(nn.Module):
|
253 |
+
"""TransformerEncoderLayer is made up of self-attn and feedforward network."""
|
254 |
+
|
255 |
+
def __init__(self,
|
256 |
+
d_model,
|
257 |
+
nhead,
|
258 |
+
dim_feedforward=2048,
|
259 |
+
dropout=0.1,
|
260 |
+
dropatt=0.1,
|
261 |
+
activation="leakyrelu",
|
262 |
+
relative_bias=True):
|
263 |
+
"""Initialization.
|
264 |
+
|
265 |
+
Args:
|
266 |
+
d_model: dimension of inputs
|
267 |
+
nhead: number of self-attention heads
|
268 |
+
dim_feedforward: dimension of hidden layer in feedforward layer
|
269 |
+
dropout: dropout rate
|
270 |
+
dropatt: drop attention rate
|
271 |
+
activation: activation function
|
272 |
+
relative_bias: bool, indicate whether use a relative position based
|
273 |
+
attention bias
|
274 |
+
"""
|
275 |
+
|
276 |
+
super(TransformerLayer, self).__init__()
|
277 |
+
|
278 |
+
self.self_attn = MultiheadAttention(
|
279 |
+
d_model, nhead, dropout=dropatt, relative_bias=relative_bias)
|
280 |
+
|
281 |
+
# Implementation of Feedforward model
|
282 |
+
self.feedforward = nn.Sequential(
|
283 |
+
nn.LayerNorm(d_model), nn.Linear(d_model, dim_feedforward),
|
284 |
+
_get_activation_fn(activation), nn.Dropout(dropout),
|
285 |
+
nn.Linear(dim_feedforward, d_model))
|
286 |
+
|
287 |
+
self.norm = nn.LayerNorm(d_model)
|
288 |
+
self.dropout1 = nn.Dropout(dropout)
|
289 |
+
self.dropout2 = nn.Dropout(dropout)
|
290 |
+
|
291 |
+
self.nhead = nhead
|
292 |
+
|
293 |
+
def forward(self, src, attn_mask=None, key_padding_mask=None):
|
294 |
+
"""Pass the input through the encoder layer.
|
295 |
+
|
296 |
+
Args:
|
297 |
+
src: the sequence to the encoder layer (required).
|
298 |
+
attn_mask: the mask for the src sequence (optional).
|
299 |
+
key_padding_mask: the mask for the src keys per batch (optional).
|
300 |
+
Returns:
|
301 |
+
src3: the output of transformer layer, share the same shape as src.
|
302 |
+
"""
|
303 |
+
src2 = self.self_attn(
|
304 |
+
self.norm(src), attn_mask=attn_mask, key_padding_mask=key_padding_mask)
|
305 |
+
src2 = src + self.dropout1(src2)
|
306 |
+
src3 = self.feedforward(src2)
|
307 |
+
src3 = src2 + self.dropout2(src3)
|
308 |
+
|
309 |
+
return src3
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
class RobertaClassificationHead(nn.Module):
|
314 |
+
"""Head for sentence-level classification tasks."""
|
315 |
+
|
316 |
+
def __init__(self, config):
|
317 |
+
super().__init__()
|
318 |
+
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
|
319 |
+
classifier_dropout = (
|
320 |
+
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
|
321 |
+
)
|
322 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
323 |
+
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
|
324 |
+
|
325 |
+
def forward(self, features, **kwargs):
|
326 |
+
x = features[:, 0, :] # take <s> token (equiv. to [CLS])
|
327 |
+
x = self.dropout(x)
|
328 |
+
x = self.dense(x)
|
329 |
+
x = torch.tanh(x)
|
330 |
+
x = self.dropout(x)
|
331 |
+
x = self.out_proj(x)
|
332 |
+
return x
|
333 |
+
|
334 |
+
|
335 |
+
##########################################
|
336 |
+
# Custom Models
|
337 |
+
##########################################
|
338 |
+
def cumprod(x, reverse=False, exclusive=False):
|
339 |
+
"""cumulative product."""
|
340 |
+
if reverse:
|
341 |
+
x = x.flip([-1])
|
342 |
+
|
343 |
+
if exclusive:
|
344 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=1)
|
345 |
+
|
346 |
+
cx = x.cumprod(-1)
|
347 |
+
|
348 |
+
if reverse:
|
349 |
+
cx = cx.flip([-1])
|
350 |
+
return cx
|
351 |
+
|
352 |
+
def cumsum(x, reverse=False, exclusive=False):
|
353 |
+
"""cumulative sum."""
|
354 |
+
bsz, _, length = x.size()
|
355 |
+
device = x.device
|
356 |
+
if reverse:
|
357 |
+
if exclusive:
|
358 |
+
w = torch.ones([bsz, length, length], device=device).tril(-1)
|
359 |
+
else:
|
360 |
+
w = torch.ones([bsz, length, length], device=device).tril(0)
|
361 |
+
cx = torch.bmm(x, w)
|
362 |
+
else:
|
363 |
+
if exclusive:
|
364 |
+
w = torch.ones([bsz, length, length], device=device).triu(1)
|
365 |
+
else:
|
366 |
+
w = torch.ones([bsz, length, length], device=device).triu(0)
|
367 |
+
cx = torch.bmm(x, w)
|
368 |
+
return cx
|
369 |
+
|
370 |
+
def cummin(x, reverse=False, exclusive=False, max_value=1e9):
|
371 |
+
"""cumulative min."""
|
372 |
+
if reverse:
|
373 |
+
if exclusive:
|
374 |
+
x = F.pad(x[:, :, 1:], (0, 1), value=max_value)
|
375 |
+
x = x.flip([-1]).cummin(-1)[0].flip([-1])
|
376 |
+
else:
|
377 |
+
if exclusive:
|
378 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=max_value)
|
379 |
+
x = x.cummin(-1)[0]
|
380 |
+
return x
|
381 |
+
|
382 |
+
class Transformer(nn.Module):
|
383 |
+
"""Transformer model."""
|
384 |
+
|
385 |
+
def __init__(self,
|
386 |
+
hidden_size,
|
387 |
+
nlayers,
|
388 |
+
ntokens,
|
389 |
+
nhead=8,
|
390 |
+
dropout=0.1,
|
391 |
+
dropatt=0.1,
|
392 |
+
relative_bias=True,
|
393 |
+
pos_emb=False,
|
394 |
+
pad=0):
|
395 |
+
"""Initialization.
|
396 |
+
|
397 |
+
Args:
|
398 |
+
hidden_size: dimension of inputs and hidden states
|
399 |
+
nlayers: number of layers
|
400 |
+
ntokens: number of output categories
|
401 |
+
nhead: number of self-attention heads
|
402 |
+
dropout: dropout rate
|
403 |
+
dropatt: drop attention rate
|
404 |
+
relative_bias: bool, indicate whether use a relative position based
|
405 |
+
attention bias
|
406 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
407 |
+
pad: pad token index
|
408 |
+
"""
|
409 |
+
|
410 |
+
super(Transformer, self).__init__()
|
411 |
+
|
412 |
+
self.drop = nn.Dropout(dropout)
|
413 |
+
|
414 |
+
self.emb = nn.Embedding(ntokens, hidden_size)
|
415 |
+
if pos_emb:
|
416 |
+
self.pos_emb = nn.Embedding(500, hidden_size)
|
417 |
+
|
418 |
+
self.layers = nn.ModuleList([
|
419 |
+
TransformerLayer(hidden_size, nhead, hidden_size * 4, dropout,
|
420 |
+
dropatt=dropatt, relative_bias=relative_bias)
|
421 |
+
for _ in range(nlayers)])
|
422 |
+
|
423 |
+
self.norm = nn.LayerNorm(hidden_size)
|
424 |
+
|
425 |
+
self.output_layer = nn.Linear(hidden_size, ntokens)
|
426 |
+
self.output_layer.weight = self.emb.weight
|
427 |
+
|
428 |
+
self.init_weights()
|
429 |
+
|
430 |
+
self.nlayers = nlayers
|
431 |
+
self.nhead = nhead
|
432 |
+
self.ntokens = ntokens
|
433 |
+
self.hidden_size = hidden_size
|
434 |
+
self.pad = pad
|
435 |
+
|
436 |
+
def init_weights(self):
|
437 |
+
"""Initialize token embedding and output bias."""
|
438 |
+
initrange = 0.1
|
439 |
+
self.emb.weight.data.uniform_(-initrange, initrange)
|
440 |
+
if hasattr(self, 'pos_emb'):
|
441 |
+
self.pos_emb.weight.data.uniform_(-initrange, initrange)
|
442 |
+
self.output_layer.bias.data.fill_(0)
|
443 |
+
|
444 |
+
def visibility(self, x, device):
|
445 |
+
"""Mask pad tokens."""
|
446 |
+
visibility = (x != self.pad).float()
|
447 |
+
visibility = visibility[:, None, :].expand(-1, x.size(1), -1)
|
448 |
+
visibility = torch.repeat_interleave(visibility, self.nhead, dim=0)
|
449 |
+
return visibility.log()
|
450 |
+
|
451 |
+
def encode(self, x, pos):
|
452 |
+
"""Standard transformer encode process."""
|
453 |
+
h = self.emb(x)
|
454 |
+
if hasattr(self, 'pos_emb'):
|
455 |
+
h = h + self.pos_emb(pos)
|
456 |
+
h_list = []
|
457 |
+
visibility = self.visibility(x, x.device)
|
458 |
+
|
459 |
+
for i in range(self.nlayers):
|
460 |
+
h_list.append(h)
|
461 |
+
h = self.layers[i](
|
462 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
463 |
+
|
464 |
+
output = h
|
465 |
+
h_array = torch.stack(h_list, dim=2)
|
466 |
+
|
467 |
+
return output, h_array
|
468 |
+
|
469 |
+
def forward(self, x, pos):
|
470 |
+
"""Pass the input through the encoder layer.
|
471 |
+
|
472 |
+
Args:
|
473 |
+
x: input tokens (required).
|
474 |
+
pos: position for each token (optional).
|
475 |
+
Returns:
|
476 |
+
output: probability distributions for missing tokens.
|
477 |
+
state_dict: parsing results and raw output
|
478 |
+
"""
|
479 |
+
|
480 |
+
batch_size, length = x.size()
|
481 |
+
|
482 |
+
raw_output, _ = self.encode(x, pos)
|
483 |
+
raw_output = self.norm(raw_output)
|
484 |
+
raw_output = self.drop(raw_output)
|
485 |
+
|
486 |
+
output = self.output_layer(raw_output)
|
487 |
+
return output.view(batch_size * length, -1), {'raw_output': raw_output,}
|
488 |
+
|
489 |
+
class StructFormer(Transformer):
|
490 |
+
"""StructFormer model."""
|
491 |
+
|
492 |
+
def __init__(self,
|
493 |
+
hidden_size,
|
494 |
+
n_context_layers,
|
495 |
+
nlayers,
|
496 |
+
ntokens,
|
497 |
+
nhead=8,
|
498 |
+
dropout=0.1,
|
499 |
+
dropatt=0.1,
|
500 |
+
relative_bias=False,
|
501 |
+
pos_emb=False,
|
502 |
+
pad=0,
|
503 |
+
n_parser_layers=4,
|
504 |
+
conv_size=9,
|
505 |
+
relations=('head', 'child'),
|
506 |
+
weight_act='softmax'):
|
507 |
+
"""Initialization.
|
508 |
+
|
509 |
+
Args:
|
510 |
+
hidden_size: dimension of inputs and hidden states
|
511 |
+
nlayers: number of layers
|
512 |
+
ntokens: number of output categories
|
513 |
+
nhead: number of self-attention heads
|
514 |
+
dropout: dropout rate
|
515 |
+
dropatt: drop attention rate
|
516 |
+
relative_bias: bool, indicate whether use a relative position based
|
517 |
+
attention bias
|
518 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
519 |
+
pad: pad token index
|
520 |
+
n_parser_layers: number of parsing layers
|
521 |
+
conv_size: convolution kernel size for parser
|
522 |
+
relations: relations that are used to compute self attention
|
523 |
+
weight_act: relations distribution activation function
|
524 |
+
"""
|
525 |
+
|
526 |
+
super(StructFormer, self).__init__(
|
527 |
+
hidden_size,
|
528 |
+
nlayers,
|
529 |
+
ntokens,
|
530 |
+
nhead=nhead,
|
531 |
+
dropout=dropout,
|
532 |
+
dropatt=dropatt,
|
533 |
+
relative_bias=relative_bias,
|
534 |
+
pos_emb=pos_emb,
|
535 |
+
pad=pad)
|
536 |
+
|
537 |
+
|
538 |
+
def encode(self, x, pos):
|
539 |
+
h = self.emb(x)
|
540 |
+
if hasattr(self, 'pos_emb'):
|
541 |
+
h = h + self.pos_emb(pos)
|
542 |
+
h_list = []
|
543 |
+
visibility = self.visibility(x, x.device)
|
544 |
+
|
545 |
+
for i in range(self.nlayers):
|
546 |
+
h_list.append(h)
|
547 |
+
h = self.layers[i](
|
548 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
549 |
+
|
550 |
+
output = h
|
551 |
+
h_array = torch.stack(h_list, dim=2)
|
552 |
+
|
553 |
+
return output
|
554 |
+
|
555 |
+
|
556 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
557 |
+
|
558 |
+
x = input_ids
|
559 |
+
batch_size, length = x.size()
|
560 |
+
|
561 |
+
if position_ids is None:
|
562 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
563 |
+
|
564 |
+
raw_output = self.encode(x, pos)
|
565 |
+
raw_output = self.norm(raw_output)
|
566 |
+
raw_output = self.drop(raw_output)
|
567 |
+
|
568 |
+
output = self.output_layer(raw_output)
|
569 |
+
|
570 |
+
loss = None
|
571 |
+
if labels is not None:
|
572 |
+
loss_fct = nn.CrossEntropyLoss()
|
573 |
+
loss = loss_fct(output.view(batch_size * length, -1), labels.reshape(-1))
|
574 |
+
|
575 |
+
return MaskedLMOutput(
|
576 |
+
loss=loss, # shape: 1
|
577 |
+
logits=output, # shape: (batch_size * length, ntokens)
|
578 |
+
hidden_states=None,
|
579 |
+
attentions=None,
|
580 |
+
)
|
581 |
+
|
582 |
+
##########################################
|
583 |
+
# HuggingFace Model
|
584 |
+
##########################################
|
585 |
+
class StructformerModel(PreTrainedModel):
|
586 |
+
config_class = StructformerConfig
|
587 |
+
|
588 |
+
def __init__(self, config):
|
589 |
+
super().__init__(config)
|
590 |
+
self.model = StructFormer(
|
591 |
+
hidden_size=config.hidden_size,
|
592 |
+
n_context_layers=config.n_context_layers,
|
593 |
+
nlayers=config.nlayers,
|
594 |
+
ntokens=config.ntokens,
|
595 |
+
nhead=config.nhead,
|
596 |
+
dropout=config.dropout,
|
597 |
+
dropatt=config.dropatt,
|
598 |
+
relative_bias=config.relative_bias,
|
599 |
+
pos_emb=config.pos_emb,
|
600 |
+
pad=config.pad,
|
601 |
+
n_parser_layers=config.n_parser_layers,
|
602 |
+
conv_size=config.conv_size,
|
603 |
+
relations=config.relations,
|
604 |
+
weight_act=config.weight_act
|
605 |
+
)
|
606 |
+
|
607 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
608 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
609 |
+
|
610 |
+
|
611 |
+
class StructFormerClassification(Transformer):
|
612 |
+
"""StructFormer model."""
|
613 |
+
|
614 |
+
def __init__(self,
|
615 |
+
hidden_size,
|
616 |
+
n_context_layers,
|
617 |
+
nlayers,
|
618 |
+
ntokens,
|
619 |
+
nhead=8,
|
620 |
+
dropout=0.1,
|
621 |
+
dropatt=0.1,
|
622 |
+
relative_bias=False,
|
623 |
+
pos_emb=False,
|
624 |
+
pad=0,
|
625 |
+
n_parser_layers=4,
|
626 |
+
conv_size=9,
|
627 |
+
relations=('head', 'child'),
|
628 |
+
weight_act='softmax',
|
629 |
+
config=None,
|
630 |
+
):
|
631 |
+
|
632 |
+
|
633 |
+
super(StructFormerClassification, self).__init__(
|
634 |
+
hidden_size,
|
635 |
+
nlayers,
|
636 |
+
ntokens,
|
637 |
+
nhead=nhead,
|
638 |
+
dropout=dropout,
|
639 |
+
dropatt=dropatt,
|
640 |
+
relative_bias=relative_bias,
|
641 |
+
pos_emb=pos_emb,
|
642 |
+
pad=pad)
|
643 |
+
|
644 |
+
self.num_labels = config.num_labels
|
645 |
+
self.config = config
|
646 |
+
|
647 |
+
self.classifier = RobertaClassificationHead(config)
|
648 |
+
|
649 |
+
def encode(self, x, pos):
|
650 |
+
h = self.emb(x)
|
651 |
+
if hasattr(self, 'pos_emb'):
|
652 |
+
h = h + self.pos_emb(pos)
|
653 |
+
h_list = []
|
654 |
+
visibility = self.visibility(x, x.device)
|
655 |
+
|
656 |
+
for i in range(self.nlayers):
|
657 |
+
h_list.append(h)
|
658 |
+
h = self.layers[i](
|
659 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
660 |
+
|
661 |
+
output = h
|
662 |
+
h_array = torch.stack(h_list, dim=2)
|
663 |
+
|
664 |
+
return output
|
665 |
+
|
666 |
+
|
667 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
668 |
+
|
669 |
+
x = input_ids
|
670 |
+
batch_size, length = x.size()
|
671 |
+
|
672 |
+
if position_ids is None:
|
673 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
674 |
+
|
675 |
+
raw_output = self.encode(x, pos)
|
676 |
+
raw_output = self.norm(raw_output)
|
677 |
+
raw_output = self.drop(raw_output)
|
678 |
+
|
679 |
+
#output = self.output_layer(raw_output)
|
680 |
+
logits = self.classifier(raw_output)
|
681 |
+
|
682 |
+
loss = None
|
683 |
+
if labels is not None:
|
684 |
+
if self.config.problem_type is None:
|
685 |
+
if self.num_labels == 1:
|
686 |
+
self.config.problem_type = "regression"
|
687 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
688 |
+
self.config.problem_type = "single_label_classification"
|
689 |
+
else:
|
690 |
+
self.config.problem_type = "multi_label_classification"
|
691 |
+
|
692 |
+
if self.config.problem_type == "regression":
|
693 |
+
loss_fct = MSELoss()
|
694 |
+
if self.num_labels == 1:
|
695 |
+
loss = loss_fct(logits.squeeze(), labels.squeeze())
|
696 |
+
else:
|
697 |
+
loss = loss_fct(logits, labels)
|
698 |
+
elif self.config.problem_type == "single_label_classification":
|
699 |
+
loss_fct = CrossEntropyLoss()
|
700 |
+
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
701 |
+
elif self.config.problem_type == "multi_label_classification":
|
702 |
+
loss_fct = BCEWithLogitsLoss()
|
703 |
+
loss = loss_fct(logits, labels)
|
704 |
+
|
705 |
+
|
706 |
+
return SequenceClassifierOutput(
|
707 |
+
loss=loss,
|
708 |
+
logits=logits,
|
709 |
+
hidden_states=None,
|
710 |
+
attentions=None,
|
711 |
+
)
|
712 |
+
|
713 |
+
|
714 |
+
class StructformerModelForSequenceClassification(PreTrainedModel):
|
715 |
+
config_class = StructformerConfig
|
716 |
+
def __init__(self, config):
|
717 |
+
super().__init__(config)
|
718 |
+
self.model = StructFormerClassification(
|
719 |
+
hidden_size=config.hidden_size,
|
720 |
+
n_context_layers=config.n_context_layers,
|
721 |
+
nlayers=config.nlayers,
|
722 |
+
ntokens=config.ntokens,
|
723 |
+
nhead=config.nhead,
|
724 |
+
dropout=config.dropout,
|
725 |
+
dropatt=config.dropatt,
|
726 |
+
relative_bias=config.relative_bias,
|
727 |
+
pos_emb=config.pos_emb,
|
728 |
+
pad=config.pad,
|
729 |
+
n_parser_layers=config.n_parser_layers,
|
730 |
+
conv_size=config.conv_size,
|
731 |
+
relations=config.relations,
|
732 |
+
weight_act=config.weight_act,
|
733 |
+
config=config)
|
734 |
+
|
735 |
+
def _init_weights(self, module):
|
736 |
+
"""Initialize the weights"""
|
737 |
+
if isinstance(module, nn.Linear):
|
738 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
739 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
740 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
741 |
+
if module.bias is not None:
|
742 |
+
module.bias.data.zero_()
|
743 |
+
elif isinstance(module, nn.Embedding):
|
744 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
745 |
+
if module.padding_idx is not None:
|
746 |
+
module.weight.data[module.padding_idx].zero_()
|
747 |
+
elif isinstance(module, nn.LayerNorm):
|
748 |
+
if module.bias is not None:
|
749 |
+
module.bias.data.zero_()
|
750 |
+
module.weight.data.fill_(1.0)
|
751 |
+
|
752 |
+
|
753 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
754 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
finetune/cola/checkpoint-400/tokenizer_config.json
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"bos_token": {
|
4 |
+
"__type": "AddedToken",
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false
|
10 |
+
},
|
11 |
+
"cls_token": {
|
12 |
+
"__type": "AddedToken",
|
13 |
+
"content": "<s>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false
|
18 |
+
},
|
19 |
+
"eos_token": {
|
20 |
+
"__type": "AddedToken",
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false
|
26 |
+
},
|
27 |
+
"errors": "replace",
|
28 |
+
"mask_token": {
|
29 |
+
"__type": "AddedToken",
|
30 |
+
"content": "<mask>",
|
31 |
+
"lstrip": true,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false
|
35 |
+
},
|
36 |
+
"model_max_length": 512,
|
37 |
+
"name_or_path": "final_models/transformer_base_final_2",
|
38 |
+
"pad_token": {
|
39 |
+
"__type": "AddedToken",
|
40 |
+
"content": "<pad>",
|
41 |
+
"lstrip": false,
|
42 |
+
"normalized": true,
|
43 |
+
"rstrip": false,
|
44 |
+
"single_word": false
|
45 |
+
},
|
46 |
+
"sep_token": {
|
47 |
+
"__type": "AddedToken",
|
48 |
+
"content": "</s>",
|
49 |
+
"lstrip": false,
|
50 |
+
"normalized": true,
|
51 |
+
"rstrip": false,
|
52 |
+
"single_word": false
|
53 |
+
},
|
54 |
+
"special_tokens_map_file": null,
|
55 |
+
"tokenizer_class": "RobertaTokenizer",
|
56 |
+
"trim_offsets": true,
|
57 |
+
"unk_token": {
|
58 |
+
"__type": "AddedToken",
|
59 |
+
"content": "<unk>",
|
60 |
+
"lstrip": false,
|
61 |
+
"normalized": true,
|
62 |
+
"rstrip": false,
|
63 |
+
"single_word": false
|
64 |
+
}
|
65 |
+
}
|
finetune/cola/checkpoint-400/trainer_state.json
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": 0.7777040477770405,
|
3 |
+
"best_model_checkpoint": "final_models/transformer_base_final_2/finetune/cola/checkpoint-400",
|
4 |
+
"epoch": 5.797101449275362,
|
5 |
+
"global_step": 400,
|
6 |
+
"is_hyper_param_search": false,
|
7 |
+
"is_local_process_zero": true,
|
8 |
+
"is_world_process_zero": true,
|
9 |
+
"log_history": [
|
10 |
+
{
|
11 |
+
"epoch": 5.8,
|
12 |
+
"eval_accuracy": 0.6712462902069092,
|
13 |
+
"eval_f1": 0.7777040477770405,
|
14 |
+
"eval_loss": 0.7399011254310608,
|
15 |
+
"eval_mcc": 0.15981907397227785,
|
16 |
+
"eval_runtime": 1.4063,
|
17 |
+
"eval_samples_per_second": 724.62,
|
18 |
+
"eval_steps_per_second": 91.022,
|
19 |
+
"step": 400
|
20 |
+
}
|
21 |
+
],
|
22 |
+
"max_steps": 690,
|
23 |
+
"num_train_epochs": 10,
|
24 |
+
"total_flos": 3120346955212800.0,
|
25 |
+
"trial_name": null,
|
26 |
+
"trial_params": null
|
27 |
+
}
|
finetune/cola/checkpoint-400/training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42f03300332c3f519b9e94f52c9e382ae77f93b0d20616ae0c3858d1e25c21dc
|
3 |
+
size 3503
|
finetune/cola/checkpoint-400/vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/cola/config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "final_models/transformer_base_final_2",
|
3 |
+
"architectures": [
|
4 |
+
"StructformerModelForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
9 |
+
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel",
|
10 |
+
"AutoModelForSequenceClassification": "structformer_as_hf_no_parser.StructformerModelForSequenceClassification"
|
11 |
+
},
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"classifier_dropout": null,
|
14 |
+
"conv_size": 9,
|
15 |
+
"dropatt": 0.1,
|
16 |
+
"dropout": 0.1,
|
17 |
+
"eos_token_id": 2,
|
18 |
+
"hidden_act": "gelu",
|
19 |
+
"hidden_dropout_prob": 0.1,
|
20 |
+
"hidden_size": 768,
|
21 |
+
"id2label": {
|
22 |
+
"0": 0,
|
23 |
+
"1": 1
|
24 |
+
},
|
25 |
+
"initializer_range": 0.02,
|
26 |
+
"intermediate_size": 3072,
|
27 |
+
"label2id": {
|
28 |
+
"0": 0,
|
29 |
+
"1": 1
|
30 |
+
},
|
31 |
+
"layer_norm_eps": 1e-05,
|
32 |
+
"max_position_embeddings": 514,
|
33 |
+
"model_type": "structformer",
|
34 |
+
"n_context_layers": 0,
|
35 |
+
"n_parser_layers": 0,
|
36 |
+
"nhead": 12,
|
37 |
+
"nlayers": 12,
|
38 |
+
"ntokens": 32000,
|
39 |
+
"num_attention_heads": 12,
|
40 |
+
"num_hidden_layers": 12,
|
41 |
+
"pad": 0,
|
42 |
+
"pad_token_id": 1,
|
43 |
+
"pos_emb": true,
|
44 |
+
"position_embedding_type": "absolute",
|
45 |
+
"problem_type": "single_label_classification",
|
46 |
+
"relations": [
|
47 |
+
"head",
|
48 |
+
"child"
|
49 |
+
],
|
50 |
+
"relative_bias": false,
|
51 |
+
"torch_dtype": "float32",
|
52 |
+
"transformers_version": "4.26.1",
|
53 |
+
"type_vocab_size": 1,
|
54 |
+
"use_cache": true,
|
55 |
+
"vocab_size": 32000,
|
56 |
+
"weight_act": "softmax"
|
57 |
+
}
|
finetune/cola/eval_results.json
ADDED
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"eval_accuracy": 0.6712462902069092,
|
4 |
+
"eval_f1": 0.7777040477770405,
|
5 |
+
"eval_loss": 0.7399011254310608,
|
6 |
+
"eval_mcc": 0.15981907397227785,
|
7 |
+
"eval_runtime": 1.4236,
|
8 |
+
"eval_samples": 1019,
|
9 |
+
"eval_samples_per_second": 715.815,
|
10 |
+
"eval_steps_per_second": 89.916
|
11 |
+
}
|
finet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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
index prediction
|
2 |
+
0 1
|
3 |
+
1 1
|
4 |
+
2 1
|
5 |
+
3 1
|
6 |
+
4 1
|
7 |
+
5 1
|
8 |
+
6 0
|
9 |
+
7 1
|
10 |
+
8 1
|
11 |
+
9 1
|
12 |
+
10 1
|
13 |
+
11 1
|
14 |
+
12 1
|
15 |
+
13 0
|
16 |
+
14 1
|
17 |
+
15 1
|
18 |
+
16 0
|
19 |
+
17 1
|
20 |
+
18 1
|
21 |
+
19 1
|
22 |
+
20 1
|
23 |
+
21 1
|
24 |
+
22 1
|
25 |
+
23 1
|
26 |
+
24 0
|
27 |
+
25 0
|
28 |
+
26 1
|
29 |
+
27 1
|
30 |
+
28 0
|
31 |
+
29 1
|
32 |
+
30 1
|
33 |
+
31 1
|
34 |
+
32 1
|
35 |
+
33 1
|
36 |
+
34 1
|
37 |
+
35 1
|
38 |
+
36 1
|
39 |
+
37 1
|
40 |
+
38 1
|
41 |
+
39 1
|
42 |
+
40 0
|
43 |
+
41 0
|
44 |
+
42 1
|
45 |
+
43 1
|
46 |
+
44 0
|
47 |
+
45 0
|
48 |
+
46 1
|
49 |
+
47 0
|
50 |
+
48 1
|
51 |
+
49 1
|
52 |
+
50 1
|
53 |
+
51 0
|
54 |
+
52 0
|
55 |
+
53 0
|
56 |
+
54 0
|
57 |
+
55 1
|
58 |
+
56 1
|
59 |
+
57 1
|
60 |
+
58 1
|
61 |
+
59 0
|
62 |
+
60 0
|
63 |
+
61 1
|
64 |
+
62 1
|
65 |
+
63 1
|
66 |
+
64 1
|
67 |
+
65 0
|
68 |
+
66 1
|
69 |
+
67 0
|
70 |
+
68 1
|
71 |
+
69 1
|
72 |
+
70 1
|
73 |
+
71 1
|
74 |
+
72 1
|
75 |
+
73 0
|
76 |
+
74 0
|
77 |
+
75 0
|
78 |
+
76 1
|
79 |
+
77 1
|
80 |
+
78 1
|
81 |
+
79 1
|
82 |
+
80 0
|
83 |
+
81 1
|
84 |
+
82 1
|
85 |
+
83 0
|
86 |
+
84 1
|
87 |
+
85 0
|
88 |
+
86 0
|
89 |
+
87 1
|
90 |
+
88 0
|
91 |
+
89 0
|
92 |
+
90 1
|
93 |
+
91 1
|
94 |
+
92 1
|
95 |
+
93 1
|
96 |
+
94 1
|
97 |
+
95 1
|
98 |
+
96 0
|
99 |
+
97 1
|
100 |
+
98 1
|
101 |
+
99 1
|
102 |
+
100 1
|
103 |
+
101 0
|
104 |
+
102 1
|
105 |
+
103 1
|
106 |
+
104 1
|
107 |
+
105 1
|
108 |
+
106 0
|
109 |
+
107 1
|
110 |
+
108 0
|
111 |
+
109 0
|
112 |
+
110 1
|
113 |
+
111 0
|
114 |
+
112 1
|
115 |
+
113 1
|
116 |
+
114 0
|
117 |
+
115 1
|
118 |
+
116 1
|
119 |
+
117 1
|
120 |
+
118 0
|
121 |
+
119 1
|
122 |
+
120 1
|
123 |
+
121 1
|
124 |
+
122 1
|
125 |
+
123 1
|
126 |
+
124 1
|
127 |
+
125 1
|
128 |
+
126 1
|
129 |
+
127 1
|
130 |
+
128 0
|
131 |
+
129 1
|
132 |
+
130 1
|
133 |
+
131 1
|
134 |
+
132 1
|
135 |
+
133 1
|
136 |
+
134 1
|
137 |
+
135 1
|
138 |
+
136 1
|
139 |
+
137 1
|
140 |
+
138 1
|
141 |
+
139 1
|
142 |
+
140 1
|
143 |
+
141 1
|
144 |
+
142 1
|
145 |
+
143 1
|
146 |
+
144 1
|
147 |
+
145 1
|
148 |
+
146 1
|
149 |
+
147 1
|
150 |
+
148 1
|
151 |
+
149 1
|
152 |
+
150 1
|
153 |
+
151 0
|
154 |
+
152 1
|
155 |
+
153 1
|
156 |
+
154 1
|
157 |
+
155 1
|
158 |
+
156 1
|
159 |
+
157 0
|
160 |
+
158 1
|
161 |
+
159 0
|
162 |
+
160 1
|
163 |
+
161 1
|
164 |
+
162 1
|
165 |
+
163 1
|
166 |
+
164 0
|
167 |
+
165 1
|
168 |
+
166 1
|
169 |
+
167 1
|
170 |
+
168 0
|
171 |
+
169 1
|
172 |
+
170 0
|
173 |
+
171 0
|
174 |
+
172 1
|
175 |
+
173 1
|
176 |
+
174 0
|
177 |
+
175 1
|
178 |
+
176 1
|
179 |
+
177 1
|
180 |
+
178 0
|
181 |
+
179 1
|
182 |
+
180 0
|
183 |
+
181 0
|
184 |
+
182 1
|
185 |
+
183 1
|
186 |
+
184 1
|
187 |
+
185 1
|
188 |
+
186 0
|
189 |
+
187 0
|
190 |
+
188 0
|
191 |
+
189 1
|
192 |
+
190 1
|
193 |
+
191 1
|
194 |
+
192 0
|
195 |
+
193 1
|
196 |
+
194 1
|
197 |
+
195 1
|
198 |
+
196 1
|
199 |
+
197 0
|
200 |
+
198 0
|
201 |
+
199 1
|
202 |
+
200 1
|
203 |
+
201 1
|
204 |
+
202 1
|
205 |
+
203 1
|
206 |
+
204 1
|
207 |
+
205 1
|
208 |
+
206 1
|
209 |
+
207 1
|
210 |
+
208 1
|
211 |
+
209 1
|
212 |
+
210 1
|
213 |
+
211 1
|
214 |
+
212 1
|
215 |
+
213 1
|
216 |
+
214 1
|
217 |
+
215 1
|
218 |
+
216 0
|
219 |
+
217 1
|
220 |
+
218 0
|
221 |
+
219 1
|
222 |
+
220 0
|
223 |
+
221 1
|
224 |
+
222 1
|
225 |
+
223 1
|
226 |
+
224 1
|
227 |
+
225 0
|
228 |
+
226 0
|
229 |
+
227 1
|
230 |
+
228 1
|
231 |
+
229 1
|
232 |
+
230 1
|
233 |
+
231 1
|
234 |
+
232 0
|
235 |
+
233 1
|
236 |
+
234 1
|
237 |
+
235 1
|
238 |
+
236 1
|
239 |
+
237 1
|
240 |
+
238 1
|
241 |
+
239 1
|
242 |
+
240 1
|
243 |
+
241 1
|
244 |
+
242 0
|
245 |
+
243 0
|
246 |
+
244 1
|
247 |
+
245 1
|
248 |
+
246 1
|
249 |
+
247 1
|
250 |
+
248 0
|
251 |
+
249 1
|
252 |
+
250 1
|
253 |
+
251 1
|
254 |
+
252 1
|
255 |
+
253 1
|
256 |
+
254 0
|
257 |
+
255 0
|
258 |
+
256 1
|
259 |
+
257 0
|
260 |
+
258 1
|
261 |
+
259 1
|
262 |
+
260 1
|
263 |
+
261 1
|
264 |
+
262 0
|
265 |
+
263 1
|
266 |
+
264 1
|
267 |
+
265 1
|
268 |
+
266 0
|
269 |
+
267 1
|
270 |
+
268 1
|
271 |
+
269 0
|
272 |
+
270 1
|
273 |
+
271 1
|
274 |
+
272 1
|
275 |
+
273 1
|
276 |
+
274 1
|
277 |
+
275 1
|
278 |
+
276 0
|
279 |
+
277 1
|
280 |
+
278 1
|
281 |
+
279 1
|
282 |
+
280 1
|
283 |
+
281 1
|
284 |
+
282 1
|
285 |
+
283 0
|
286 |
+
284 1
|
287 |
+
285 0
|
288 |
+
286 1
|
289 |
+
287 1
|
290 |
+
288 1
|
291 |
+
289 1
|
292 |
+
290 1
|
293 |
+
291 0
|
294 |
+
292 0
|
295 |
+
293 0
|
296 |
+
294 1
|
297 |
+
295 1
|
298 |
+
296 0
|
299 |
+
297 1
|
300 |
+
298 1
|
301 |
+
299 1
|
302 |
+
300 0
|
303 |
+
301 1
|
304 |
+
302 1
|
305 |
+
303 1
|
306 |
+
304 0
|
307 |
+
305 1
|
308 |
+
306 1
|
309 |
+
307 1
|
310 |
+
308 0
|
311 |
+
309 1
|
312 |
+
310 1
|
313 |
+
311 1
|
314 |
+
312 0
|
315 |
+
313 0
|
316 |
+
314 1
|
317 |
+
315 1
|
318 |
+
316 1
|
319 |
+
317 1
|
320 |
+
318 1
|
321 |
+
319 1
|
322 |
+
320 1
|
323 |
+
321 1
|
324 |
+
322 0
|
325 |
+
323 0
|
326 |
+
324 1
|
327 |
+
325 1
|
328 |
+
326 1
|
329 |
+
327 1
|
330 |
+
328 1
|
331 |
+
329 1
|
332 |
+
330 1
|
333 |
+
331 1
|
334 |
+
332 1
|
335 |
+
333 1
|
336 |
+
334 1
|
337 |
+
335 1
|
338 |
+
336 1
|
339 |
+
337 1
|
340 |
+
338 1
|
341 |
+
339 1
|
342 |
+
340 1
|
343 |
+
341 1
|
344 |
+
342 1
|
345 |
+
343 0
|
346 |
+
344 1
|
347 |
+
345 0
|
348 |
+
346 1
|
349 |
+
347 1
|
350 |
+
348 1
|
351 |
+
349 1
|
352 |
+
350 0
|
353 |
+
351 1
|
354 |
+
352 1
|
355 |
+
353 1
|
356 |
+
354 1
|
357 |
+
355 1
|
358 |
+
356 1
|
359 |
+
357 1
|
360 |
+
358 1
|
361 |
+
359 1
|
362 |
+
360 1
|
363 |
+
361 1
|
364 |
+
362 1
|
365 |
+
363 1
|
366 |
+
364 1
|
367 |
+
365 1
|
368 |
+
366 1
|
369 |
+
367 1
|
370 |
+
368 1
|
371 |
+
369 1
|
372 |
+
370 1
|
373 |
+
371 1
|
374 |
+
372 1
|
375 |
+
373 1
|
376 |
+
374 1
|
377 |
+
375 1
|
378 |
+
376 1
|
379 |
+
377 1
|
380 |
+
378 1
|
381 |
+
379 1
|
382 |
+
380 1
|
383 |
+
381 0
|
384 |
+
382 1
|
385 |
+
383 1
|
386 |
+
384 1
|
387 |
+
385 1
|
388 |
+
386 1
|
389 |
+
387 1
|
390 |
+
388 0
|
391 |
+
389 1
|
392 |
+
390 1
|
393 |
+
391 1
|
394 |
+
392 1
|
395 |
+
393 1
|
396 |
+
394 1
|
397 |
+
395 1
|
398 |
+
396 1
|
399 |
+
397 1
|
400 |
+
398 1
|
401 |
+
399 1
|
402 |
+
400 1
|
403 |
+
401 0
|
404 |
+
402 0
|
405 |
+
403 1
|
406 |
+
404 1
|
407 |
+
405 1
|
408 |
+
406 1
|
409 |
+
407 1
|
410 |
+
408 1
|
411 |
+
409 0
|
412 |
+
410 1
|
413 |
+
411 1
|
414 |
+
412 1
|
415 |
+
413 1
|
416 |
+
414 1
|
417 |
+
415 0
|
418 |
+
416 1
|
419 |
+
417 1
|
420 |
+
418 1
|
421 |
+
419 1
|
422 |
+
420 1
|
423 |
+
421 0
|
424 |
+
422 1
|
425 |
+
423 1
|
426 |
+
424 1
|
427 |
+
425 1
|
428 |
+
426 1
|
429 |
+
427 1
|
430 |
+
428 0
|
431 |
+
429 1
|
432 |
+
430 0
|
433 |
+
431 0
|
434 |
+
432 1
|
435 |
+
433 1
|
436 |
+
434 1
|
437 |
+
435 1
|
438 |
+
436 1
|
439 |
+
437 1
|
440 |
+
438 0
|
441 |
+
439 1
|
442 |
+
440 1
|
443 |
+
441 1
|
444 |
+
442 1
|
445 |
+
443 1
|
446 |
+
444 1
|
447 |
+
445 1
|
448 |
+
446 1
|
449 |
+
447 1
|
450 |
+
448 1
|
451 |
+
449 1
|
452 |
+
450 1
|
453 |
+
451 1
|
454 |
+
452 1
|
455 |
+
453 0
|
456 |
+
454 1
|
457 |
+
455 1
|
458 |
+
456 1
|
459 |
+
457 1
|
460 |
+
458 1
|
461 |
+
459 1
|
462 |
+
460 1
|
463 |
+
461 1
|
464 |
+
462 0
|
465 |
+
463 1
|
466 |
+
464 0
|
467 |
+
465 1
|
468 |
+
466 0
|
469 |
+
467 0
|
470 |
+
468 1
|
471 |
+
469 1
|
472 |
+
470 1
|
473 |
+
471 1
|
474 |
+
472 0
|
475 |
+
473 1
|
476 |
+
474 1
|
477 |
+
475 1
|
478 |
+
476 0
|
479 |
+
477 1
|
480 |
+
478 1
|
481 |
+
479 1
|
482 |
+
480 0
|
483 |
+
481 1
|
484 |
+
482 1
|
485 |
+
483 1
|
486 |
+
484 1
|
487 |
+
485 1
|
488 |
+
486 1
|
489 |
+
487 1
|
490 |
+
488 1
|
491 |
+
489 1
|
492 |
+
490 1
|
493 |
+
491 1
|
494 |
+
492 1
|
495 |
+
493 0
|
496 |
+
494 1
|
497 |
+
495 1
|
498 |
+
496 1
|
499 |
+
497 1
|
500 |
+
498 1
|
501 |
+
499 1
|
502 |
+
500 1
|
503 |
+
501 1
|
504 |
+
502 0
|
505 |
+
503 0
|
506 |
+
504 0
|
507 |
+
505 0
|
508 |
+
506 0
|
509 |
+
507 1
|
510 |
+
508 0
|
511 |
+
509 0
|
512 |
+
510 1
|
513 |
+
511 1
|
514 |
+
512 1
|
515 |
+
513 1
|
516 |
+
514 1
|
517 |
+
515 1
|
518 |
+
516 1
|
519 |
+
517 1
|
520 |
+
518 1
|
521 |
+
519 1
|
522 |
+
520 1
|
523 |
+
521 0
|
524 |
+
522 1
|
525 |
+
523 1
|
526 |
+
524 1
|
527 |
+
525 1
|
528 |
+
526 0
|
529 |
+
527 1
|
530 |
+
528 1
|
531 |
+
529 1
|
532 |
+
530 1
|
533 |
+
531 1
|
534 |
+
532 1
|
535 |
+
533 0
|
536 |
+
534 1
|
537 |
+
535 1
|
538 |
+
536 1
|
539 |
+
537 1
|
540 |
+
538 1
|
541 |
+
539 1
|
542 |
+
540 1
|
543 |
+
541 1
|
544 |
+
542 1
|
545 |
+
543 1
|
546 |
+
544 1
|
547 |
+
545 0
|
548 |
+
546 0
|
549 |
+
547 1
|
550 |
+
548 1
|
551 |
+
549 1
|
552 |
+
550 0
|
553 |
+
551 1
|
554 |
+
552 1
|
555 |
+
553 1
|
556 |
+
554 0
|
557 |
+
555 0
|
558 |
+
556 0
|
559 |
+
557 1
|
560 |
+
558 1
|
561 |
+
559 1
|
562 |
+
560 1
|
563 |
+
561 1
|
564 |
+
562 1
|
565 |
+
563 1
|
566 |
+
564 0
|
567 |
+
565 1
|
568 |
+
566 1
|
569 |
+
567 1
|
570 |
+
568 1
|
571 |
+
569 0
|
572 |
+
570 1
|
573 |
+
571 1
|
574 |
+
572 1
|
575 |
+
573 1
|
576 |
+
574 1
|
577 |
+
575 1
|
578 |
+
576 1
|
579 |
+
577 1
|
580 |
+
578 1
|
581 |
+
579 1
|
582 |
+
580 1
|
583 |
+
581 1
|
584 |
+
582 1
|
585 |
+
583 1
|
586 |
+
584 1
|
587 |
+
585 1
|
588 |
+
586 0
|
589 |
+
587 1
|
590 |
+
588 1
|
591 |
+
589 1
|
592 |
+
590 0
|
593 |
+
591 1
|
594 |
+
592 1
|
595 |
+
593 0
|
596 |
+
594 0
|
597 |
+
595 1
|
598 |
+
596 1
|
599 |
+
597 0
|
600 |
+
598 0
|
601 |
+
599 0
|
602 |
+
600 0
|
603 |
+
601 1
|
604 |
+
602 0
|
605 |
+
603 0
|
606 |
+
604 0
|
607 |
+
605 1
|
608 |
+
606 1
|
609 |
+
607 1
|
610 |
+
608 1
|
611 |
+
609 1
|
612 |
+
610 0
|
613 |
+
611 1
|
614 |
+
612 0
|
615 |
+
613 1
|
616 |
+
614 1
|
617 |
+
615 1
|
618 |
+
616 1
|
619 |
+
617 1
|
620 |
+
618 1
|
621 |
+
619 1
|
622 |
+
620 1
|
623 |
+
621 0
|
624 |
+
622 1
|
625 |
+
623 1
|
626 |
+
624 1
|
627 |
+
625 1
|
628 |
+
626 1
|
629 |
+
627 1
|
630 |
+
628 1
|
631 |
+
629 1
|
632 |
+
630 1
|
633 |
+
631 0
|
634 |
+
632 1
|
635 |
+
633 1
|
636 |
+
634 0
|
637 |
+
635 1
|
638 |
+
636 1
|
639 |
+
637 1
|
640 |
+
638 1
|
641 |
+
639 1
|
642 |
+
640 0
|
643 |
+
641 1
|
644 |
+
642 1
|
645 |
+
643 1
|
646 |
+
644 0
|
647 |
+
645 0
|
648 |
+
646 1
|
649 |
+
647 1
|
650 |
+
648 0
|
651 |
+
649 1
|
652 |
+
650 1
|
653 |
+
651 1
|
654 |
+
652 1
|
655 |
+
653 1
|
656 |
+
654 1
|
657 |
+
655 1
|
658 |
+
656 1
|
659 |
+
657 1
|
660 |
+
658 1
|
661 |
+
659 1
|
662 |
+
660 1
|
663 |
+
661 1
|
664 |
+
662 1
|
665 |
+
663 1
|
666 |
+
664 1
|
667 |
+
665 0
|
668 |
+
666 1
|
669 |
+
667 1
|
670 |
+
668 1
|
671 |
+
669 1
|
672 |
+
670 0
|
673 |
+
671 1
|
674 |
+
672 1
|
675 |
+
673 0
|
676 |
+
674 1
|
677 |
+
675 1
|
678 |
+
676 0
|
679 |
+
677 1
|
680 |
+
678 0
|
681 |
+
679 0
|
682 |
+
680 0
|
683 |
+
681 1
|
684 |
+
682 0
|
685 |
+
683 1
|
686 |
+
684 1
|
687 |
+
685 1
|
688 |
+
686 0
|
689 |
+
687 1
|
690 |
+
688 0
|
691 |
+
689 1
|
692 |
+
690 1
|
693 |
+
691 1
|
694 |
+
692 0
|
695 |
+
693 0
|
696 |
+
694 0
|
697 |
+
695 0
|
698 |
+
696 1
|
699 |
+
697 1
|
700 |
+
698 1
|
701 |
+
699 1
|
702 |
+
700 1
|
703 |
+
701 0
|
704 |
+
702 1
|
705 |
+
703 1
|
706 |
+
704 1
|
707 |
+
705 0
|
708 |
+
706 0
|
709 |
+
707 0
|
710 |
+
708 0
|
711 |
+
709 0
|
712 |
+
710 1
|
713 |
+
711 0
|
714 |
+
712 1
|
715 |
+
713 1
|
716 |
+
714 0
|
717 |
+
715 1
|
718 |
+
716 1
|
719 |
+
717 1
|
720 |
+
718 1
|
721 |
+
719 1
|
722 |
+
720 0
|
723 |
+
721 1
|
724 |
+
722 1
|
725 |
+
723 1
|
726 |
+
724 1
|
727 |
+
725 1
|
728 |
+
726 1
|
729 |
+
727 1
|
730 |
+
728 0
|
731 |
+
729 1
|
732 |
+
730 1
|
733 |
+
731 1
|
734 |
+
732 0
|
735 |
+
733 1
|
736 |
+
734 1
|
737 |
+
735 0
|
738 |
+
736 1
|
739 |
+
737 1
|
740 |
+
738 1
|
741 |
+
739 0
|
742 |
+
740 0
|
743 |
+
741 1
|
744 |
+
742 1
|
745 |
+
743 1
|
746 |
+
744 1
|
747 |
+
745 1
|
748 |
+
746 1
|
749 |
+
747 1
|
750 |
+
748 1
|
751 |
+
749 1
|
752 |
+
750 1
|
753 |
+
751 1
|
754 |
+
752 1
|
755 |
+
753 1
|
756 |
+
754 1
|
757 |
+
755 0
|
758 |
+
756 1
|
759 |
+
757 1
|
760 |
+
758 1
|
761 |
+
759 1
|
762 |
+
760 1
|
763 |
+
761 1
|
764 |
+
762 1
|
765 |
+
763 1
|
766 |
+
764 1
|
767 |
+
765 0
|
768 |
+
766 0
|
769 |
+
767 1
|
770 |
+
768 1
|
771 |
+
769 1
|
772 |
+
770 1
|
773 |
+
771 1
|
774 |
+
772 1
|
775 |
+
773 1
|
776 |
+
774 1
|
777 |
+
775 1
|
778 |
+
776 1
|
779 |
+
777 1
|
780 |
+
778 1
|
781 |
+
779 1
|
782 |
+
780 1
|
783 |
+
781 1
|
784 |
+
782 1
|
785 |
+
783 1
|
786 |
+
784 1
|
787 |
+
785 1
|
788 |
+
786 0
|
789 |
+
787 1
|
790 |
+
788 1
|
791 |
+
789 1
|
792 |
+
790 1
|
793 |
+
791 1
|
794 |
+
792 1
|
795 |
+
793 1
|
796 |
+
794 0
|
797 |
+
795 0
|
798 |
+
796 0
|
799 |
+
797 1
|
800 |
+
798 1
|
801 |
+
799 1
|
802 |
+
800 1
|
803 |
+
801 1
|
804 |
+
802 1
|
805 |
+
803 0
|
806 |
+
804 1
|
807 |
+
805 1
|
808 |
+
806 1
|
809 |
+
807 1
|
810 |
+
808 1
|
811 |
+
809 0
|
812 |
+
810 1
|
813 |
+
811 1
|
814 |
+
812 0
|
815 |
+
813 0
|
816 |
+
814 0
|
817 |
+
815 0
|
818 |
+
816 1
|
819 |
+
817 0
|
820 |
+
818 0
|
821 |
+
819 1
|
822 |
+
820 1
|
823 |
+
821 1
|
824 |
+
822 1
|
825 |
+
823 1
|
826 |
+
824 1
|
827 |
+
825 1
|
828 |
+
826 1
|
829 |
+
827 0
|
830 |
+
828 0
|
831 |
+
829 1
|
832 |
+
830 1
|
833 |
+
831 1
|
834 |
+
832 1
|
835 |
+
833 1
|
836 |
+
834 0
|
837 |
+
835 1
|
838 |
+
836 0
|
839 |
+
837 1
|
840 |
+
838 1
|
841 |
+
839 1
|
842 |
+
840 1
|
843 |
+
841 1
|
844 |
+
842 1
|
845 |
+
843 1
|
846 |
+
844 1
|
847 |
+
845 1
|
848 |
+
846 1
|
849 |
+
847 1
|
850 |
+
848 1
|
851 |
+
849 1
|
852 |
+
850 1
|
853 |
+
851 1
|
854 |
+
852 1
|
855 |
+
853 0
|
856 |
+
854 1
|
857 |
+
855 1
|
858 |
+
856 1
|
859 |
+
857 1
|
860 |
+
858 1
|
861 |
+
859 1
|
862 |
+
860 1
|
863 |
+
861 1
|
864 |
+
862 0
|
865 |
+
863 1
|
866 |
+
864 0
|
867 |
+
865 1
|
868 |
+
866 1
|
869 |
+
867 1
|
870 |
+
868 1
|
871 |
+
869 1
|
872 |
+
870 1
|
873 |
+
871 1
|
874 |
+
872 0
|
875 |
+
873 1
|
876 |
+
874 1
|
877 |
+
875 1
|
878 |
+
876 1
|
879 |
+
877 1
|
880 |
+
878 1
|
881 |
+
879 1
|
882 |
+
880 1
|
883 |
+
881 1
|
884 |
+
882 1
|
885 |
+
883 1
|
886 |
+
884 1
|
887 |
+
885 1
|
888 |
+
886 0
|
889 |
+
887 1
|
890 |
+
888 1
|
891 |
+
889 1
|
892 |
+
890 1
|
893 |
+
891 1
|
894 |
+
892 1
|
895 |
+
893 1
|
896 |
+
894 1
|
897 |
+
895 1
|
898 |
+
896 1
|
899 |
+
897 0
|
900 |
+
898 0
|
901 |
+
899 1
|
902 |
+
900 1
|
903 |
+
901 1
|
904 |
+
902 1
|
905 |
+
903 1
|
906 |
+
904 1
|
907 |
+
905 1
|
908 |
+
906 1
|
909 |
+
907 1
|
910 |
+
908 1
|
911 |
+
909 1
|
912 |
+
910 0
|
913 |
+
911 1
|
914 |
+
912 1
|
915 |
+
913 1
|
916 |
+
914 0
|
917 |
+
915 0
|
918 |
+
916 0
|
919 |
+
917 1
|
920 |
+
918 1
|
921 |
+
919 1
|
922 |
+
920 1
|
923 |
+
921 1
|
924 |
+
922 1
|
925 |
+
923 1
|
926 |
+
924 1
|
927 |
+
925 1
|
928 |
+
926 1
|
929 |
+
927 1
|
930 |
+
928 1
|
931 |
+
929 1
|
932 |
+
930 1
|
933 |
+
931 1
|
934 |
+
932 1
|
935 |
+
933 1
|
936 |
+
934 1
|
937 |
+
935 1
|
938 |
+
936 1
|
939 |
+
937 1
|
940 |
+
938 1
|
941 |
+
939 1
|
942 |
+
940 1
|
943 |
+
941 1
|
944 |
+
942 1
|
945 |
+
943 1
|
946 |
+
944 1
|
947 |
+
945 1
|
948 |
+
946 1
|
949 |
+
947 1
|
950 |
+
948 0
|
951 |
+
949 1
|
952 |
+
950 0
|
953 |
+
951 0
|
954 |
+
952 1
|
955 |
+
953 1
|
956 |
+
954 1
|
957 |
+
955 0
|
958 |
+
956 0
|
959 |
+
957 1
|
960 |
+
958 1
|
961 |
+
959 1
|
962 |
+
960 1
|
963 |
+
961 0
|
964 |
+
962 1
|
965 |
+
963 1
|
966 |
+
964 1
|
967 |
+
965 1
|
968 |
+
966 1
|
969 |
+
967 1
|
970 |
+
968 1
|
971 |
+
969 1
|
972 |
+
970 1
|
973 |
+
971 0
|
974 |
+
972 1
|
975 |
+
973 1
|
976 |
+
974 1
|
977 |
+
975 1
|
978 |
+
976 1
|
979 |
+
977 1
|
980 |
+
978 0
|
981 |
+
979 1
|
982 |
+
980 1
|
983 |
+
981 1
|
984 |
+
982 1
|
985 |
+
983 1
|
986 |
+
984 0
|
987 |
+
985 1
|
988 |
+
986 1
|
989 |
+
987 1
|
990 |
+
988 1
|
991 |
+
989 1
|
992 |
+
990 1
|
993 |
+
991 1
|
994 |
+
992 1
|
995 |
+
993 0
|
996 |
+
994 1
|
997 |
+
995 0
|
998 |
+
996 0
|
999 |
+
997 1
|
1000 |
+
998 1
|
1001 |
+
999 1
|
1002 |
+
1000 1
|
1003 |
+
1001 0
|
1004 |
+
1002 1
|
1005 |
+
1003 1
|
1006 |
+
1004 1
|
1007 |
+
1005 0
|
1008 |
+
1006 1
|
1009 |
+
1007 1
|
1010 |
+
1008 1
|
1011 |
+
1009 1
|
1012 |
+
1010 1
|
1013 |
+
1011 0
|
1014 |
+
1012 0
|
1015 |
+
1013 0
|
1016 |
+
1014 1
|
1017 |
+
1015 1
|
1018 |
+
1016 1
|
1019 |
+
1017 1
|
1020 |
+
1018 0
|
finetune/cola/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e347d72000b0f5a8771aa62eedeb5a78b9aa79669b5796edb1e66f8e21e832c
|
3 |
+
size 442624943
|
finetune/cola/special_tokens_map.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": "<s>",
|
3 |
+
"cls_token": "<s>",
|
4 |
+
"eos_token": "</s>",
|
5 |
+
"mask_token": {
|
6 |
+
"content": "<mask>",
|
7 |
+
"lstrip": true,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"pad_token": "<pad>",
|
13 |
+
"sep_token": "</s>",
|
14 |
+
"unk_token": "<unk>"
|
15 |
+
}
|
finetune/cola/structformer_as_hf_no_parser.py
ADDED
@@ -0,0 +1,754 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torch.nn import init
|
5 |
+
from transformers import PreTrainedModel
|
6 |
+
from transformers import PretrainedConfig
|
7 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
8 |
+
from typing import List
|
9 |
+
from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss
|
10 |
+
from transformers.modeling_outputs import (
|
11 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
12 |
+
BaseModelOutputWithPoolingAndCrossAttentions,
|
13 |
+
MaskedLMOutput,
|
14 |
+
SequenceClassifierOutput
|
15 |
+
)
|
16 |
+
|
17 |
+
##########################################
|
18 |
+
# HuggingFace Config
|
19 |
+
##########################################
|
20 |
+
class StructformerConfig(PretrainedConfig):
|
21 |
+
model_type = "structformer"
|
22 |
+
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
hidden_size=768,
|
26 |
+
n_context_layers=2,
|
27 |
+
nlayers=6,
|
28 |
+
ntokens=32000,
|
29 |
+
nhead=8,
|
30 |
+
dropout=0.1,
|
31 |
+
dropatt=0.1,
|
32 |
+
relative_bias=False,
|
33 |
+
pos_emb=False,
|
34 |
+
pad=0,
|
35 |
+
n_parser_layers=4,
|
36 |
+
conv_size=9,
|
37 |
+
relations=('head', 'child'),
|
38 |
+
weight_act='softmax',
|
39 |
+
**kwargs,
|
40 |
+
):
|
41 |
+
self.hidden_size = hidden_size
|
42 |
+
self.n_context_layers = n_context_layers
|
43 |
+
self.nlayers = nlayers
|
44 |
+
self.ntokens = ntokens
|
45 |
+
self.nhead = nhead
|
46 |
+
self.dropout = dropout
|
47 |
+
self.dropatt = dropatt
|
48 |
+
self.relative_bias = relative_bias
|
49 |
+
self.pos_emb = pos_emb
|
50 |
+
self.pad = pad
|
51 |
+
self.n_parser_layers = n_parser_layers
|
52 |
+
self.conv_size = conv_size
|
53 |
+
self.relations = relations
|
54 |
+
self.weight_act = weight_act
|
55 |
+
super().__init__(**kwargs)
|
56 |
+
|
57 |
+
##########################################
|
58 |
+
# Custom Layers
|
59 |
+
##########################################
|
60 |
+
def _get_activation_fn(activation):
|
61 |
+
"""Get specified activation function."""
|
62 |
+
if activation == "relu":
|
63 |
+
return nn.ReLU()
|
64 |
+
elif activation == "gelu":
|
65 |
+
return nn.GELU()
|
66 |
+
elif activation == "leakyrelu":
|
67 |
+
return nn.LeakyReLU()
|
68 |
+
|
69 |
+
raise RuntimeError(
|
70 |
+
"activation should be relu/gelu, not {}".format(activation))
|
71 |
+
|
72 |
+
class Conv1d(nn.Module):
|
73 |
+
"""1D convolution layer."""
|
74 |
+
|
75 |
+
def __init__(self, hidden_size, kernel_size, dilation=1):
|
76 |
+
"""Initialization.
|
77 |
+
|
78 |
+
Args:
|
79 |
+
hidden_size: dimension of input embeddings
|
80 |
+
kernel_size: convolution kernel size
|
81 |
+
dilation: the spacing between the kernel points
|
82 |
+
"""
|
83 |
+
super(Conv1d, self).__init__()
|
84 |
+
|
85 |
+
if kernel_size % 2 == 0:
|
86 |
+
padding = (kernel_size // 2) * dilation
|
87 |
+
self.shift = True
|
88 |
+
else:
|
89 |
+
padding = ((kernel_size - 1) // 2) * dilation
|
90 |
+
self.shift = False
|
91 |
+
self.conv = nn.Conv1d(
|
92 |
+
hidden_size,
|
93 |
+
hidden_size,
|
94 |
+
kernel_size,
|
95 |
+
padding=padding,
|
96 |
+
dilation=dilation)
|
97 |
+
|
98 |
+
def forward(self, x):
|
99 |
+
"""Compute convolution.
|
100 |
+
|
101 |
+
Args:
|
102 |
+
x: input embeddings
|
103 |
+
Returns:
|
104 |
+
conv_output: convolution results
|
105 |
+
"""
|
106 |
+
|
107 |
+
if self.shift:
|
108 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)[:, 1:]
|
109 |
+
else:
|
110 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)
|
111 |
+
|
112 |
+
class MultiheadAttention(nn.Module):
|
113 |
+
"""Multi-head self-attention layer."""
|
114 |
+
|
115 |
+
def __init__(self,
|
116 |
+
embed_dim,
|
117 |
+
num_heads,
|
118 |
+
dropout=0.,
|
119 |
+
bias=True,
|
120 |
+
v_proj=True,
|
121 |
+
out_proj=True,
|
122 |
+
relative_bias=True):
|
123 |
+
"""Initialization.
|
124 |
+
|
125 |
+
Args:
|
126 |
+
embed_dim: dimension of input embeddings
|
127 |
+
num_heads: number of self-attention heads
|
128 |
+
dropout: dropout rate
|
129 |
+
bias: bool, indicate whether include bias for linear transformations
|
130 |
+
v_proj: bool, indicate whether project inputs to new values
|
131 |
+
out_proj: bool, indicate whether project outputs to new values
|
132 |
+
relative_bias: bool, indicate whether use a relative position based
|
133 |
+
attention bias
|
134 |
+
"""
|
135 |
+
|
136 |
+
super(MultiheadAttention, self).__init__()
|
137 |
+
self.embed_dim = embed_dim
|
138 |
+
|
139 |
+
self.num_heads = num_heads
|
140 |
+
self.drop = nn.Dropout(dropout)
|
141 |
+
self.head_dim = embed_dim // num_heads
|
142 |
+
assert self.head_dim * num_heads == self.embed_dim, ("embed_dim must be "
|
143 |
+
"divisible by "
|
144 |
+
"num_heads")
|
145 |
+
|
146 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
147 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
148 |
+
if v_proj:
|
149 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
150 |
+
else:
|
151 |
+
self.v_proj = nn.Identity()
|
152 |
+
|
153 |
+
if out_proj:
|
154 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
155 |
+
else:
|
156 |
+
self.out_proj = nn.Identity()
|
157 |
+
|
158 |
+
if relative_bias:
|
159 |
+
self.relative_bias = nn.Parameter(torch.zeros((self.num_heads, 512)))
|
160 |
+
else:
|
161 |
+
self.relative_bias = None
|
162 |
+
|
163 |
+
self._reset_parameters()
|
164 |
+
|
165 |
+
def _reset_parameters(self):
|
166 |
+
"""Initialize attention parameters."""
|
167 |
+
|
168 |
+
init.xavier_uniform_(self.q_proj.weight)
|
169 |
+
init.constant_(self.q_proj.bias, 0.)
|
170 |
+
|
171 |
+
init.xavier_uniform_(self.k_proj.weight)
|
172 |
+
init.constant_(self.k_proj.bias, 0.)
|
173 |
+
|
174 |
+
if isinstance(self.v_proj, nn.Linear):
|
175 |
+
init.xavier_uniform_(self.v_proj.weight)
|
176 |
+
init.constant_(self.v_proj.bias, 0.)
|
177 |
+
|
178 |
+
if isinstance(self.out_proj, nn.Linear):
|
179 |
+
init.xavier_uniform_(self.out_proj.weight)
|
180 |
+
init.constant_(self.out_proj.bias, 0.)
|
181 |
+
|
182 |
+
def forward(self, query, key_padding_mask=None, attn_mask=None):
|
183 |
+
"""Compute multi-head self-attention.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
query: input embeddings
|
187 |
+
key_padding_mask: 3D mask that prevents attention to certain positions
|
188 |
+
attn_mask: 3D mask that rescale the attention weight at each position
|
189 |
+
Returns:
|
190 |
+
attn_output: self-attention output
|
191 |
+
"""
|
192 |
+
|
193 |
+
length, bsz, embed_dim = query.size()
|
194 |
+
assert embed_dim == self.embed_dim
|
195 |
+
|
196 |
+
head_dim = embed_dim // self.num_heads
|
197 |
+
assert head_dim * self.num_heads == embed_dim, ("embed_dim must be "
|
198 |
+
"divisible by num_heads")
|
199 |
+
scaling = float(head_dim)**-0.5
|
200 |
+
|
201 |
+
q = self.q_proj(query)
|
202 |
+
k = self.k_proj(query)
|
203 |
+
v = self.v_proj(query)
|
204 |
+
|
205 |
+
q = q * scaling
|
206 |
+
|
207 |
+
if attn_mask is not None:
|
208 |
+
assert list(attn_mask.size()) == [bsz * self.num_heads,
|
209 |
+
query.size(0), query.size(0)]
|
210 |
+
|
211 |
+
q = q.contiguous().view(length, bsz * self.num_heads,
|
212 |
+
head_dim).transpose(0, 1)
|
213 |
+
k = k.contiguous().view(length, bsz * self.num_heads,
|
214 |
+
head_dim).transpose(0, 1)
|
215 |
+
v = v.contiguous().view(length, bsz * self.num_heads,
|
216 |
+
head_dim).transpose(0, 1)
|
217 |
+
|
218 |
+
attn_output_weights = torch.bmm(q, k.transpose(1, 2))
|
219 |
+
assert list(
|
220 |
+
attn_output_weights.size()) == [bsz * self.num_heads, length, length]
|
221 |
+
|
222 |
+
if self.relative_bias is not None:
|
223 |
+
pos = torch.arange(length, device=query.device)
|
224 |
+
relative_pos = torch.abs(pos[:, None] - pos[None, :]) + 256
|
225 |
+
relative_pos = relative_pos[None, :, :].expand(bsz * self.num_heads, -1,
|
226 |
+
-1)
|
227 |
+
|
228 |
+
relative_bias = self.relative_bias.repeat_interleave(bsz, dim=0)
|
229 |
+
relative_bias = relative_bias[:, None, :].expand(-1, length, -1)
|
230 |
+
relative_bias = torch.gather(relative_bias, 2, relative_pos)
|
231 |
+
attn_output_weights = attn_output_weights + relative_bias
|
232 |
+
|
233 |
+
if key_padding_mask is not None:
|
234 |
+
attn_output_weights = attn_output_weights + key_padding_mask
|
235 |
+
|
236 |
+
if attn_mask is None:
|
237 |
+
attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
|
238 |
+
else:
|
239 |
+
attn_output_weights = torch.sigmoid(attn_output_weights) * attn_mask
|
240 |
+
|
241 |
+
attn_output_weights = self.drop(attn_output_weights)
|
242 |
+
|
243 |
+
attn_output = torch.bmm(attn_output_weights, v)
|
244 |
+
|
245 |
+
assert list(attn_output.size()) == [bsz * self.num_heads, length, head_dim]
|
246 |
+
attn_output = attn_output.transpose(0, 1).contiguous().view(
|
247 |
+
length, bsz, embed_dim)
|
248 |
+
attn_output = self.out_proj(attn_output)
|
249 |
+
|
250 |
+
return attn_output
|
251 |
+
|
252 |
+
class TransformerLayer(nn.Module):
|
253 |
+
"""TransformerEncoderLayer is made up of self-attn and feedforward network."""
|
254 |
+
|
255 |
+
def __init__(self,
|
256 |
+
d_model,
|
257 |
+
nhead,
|
258 |
+
dim_feedforward=2048,
|
259 |
+
dropout=0.1,
|
260 |
+
dropatt=0.1,
|
261 |
+
activation="leakyrelu",
|
262 |
+
relative_bias=True):
|
263 |
+
"""Initialization.
|
264 |
+
|
265 |
+
Args:
|
266 |
+
d_model: dimension of inputs
|
267 |
+
nhead: number of self-attention heads
|
268 |
+
dim_feedforward: dimension of hidden layer in feedforward layer
|
269 |
+
dropout: dropout rate
|
270 |
+
dropatt: drop attention rate
|
271 |
+
activation: activation function
|
272 |
+
relative_bias: bool, indicate whether use a relative position based
|
273 |
+
attention bias
|
274 |
+
"""
|
275 |
+
|
276 |
+
super(TransformerLayer, self).__init__()
|
277 |
+
|
278 |
+
self.self_attn = MultiheadAttention(
|
279 |
+
d_model, nhead, dropout=dropatt, relative_bias=relative_bias)
|
280 |
+
|
281 |
+
# Implementation of Feedforward model
|
282 |
+
self.feedforward = nn.Sequential(
|
283 |
+
nn.LayerNorm(d_model), nn.Linear(d_model, dim_feedforward),
|
284 |
+
_get_activation_fn(activation), nn.Dropout(dropout),
|
285 |
+
nn.Linear(dim_feedforward, d_model))
|
286 |
+
|
287 |
+
self.norm = nn.LayerNorm(d_model)
|
288 |
+
self.dropout1 = nn.Dropout(dropout)
|
289 |
+
self.dropout2 = nn.Dropout(dropout)
|
290 |
+
|
291 |
+
self.nhead = nhead
|
292 |
+
|
293 |
+
def forward(self, src, attn_mask=None, key_padding_mask=None):
|
294 |
+
"""Pass the input through the encoder layer.
|
295 |
+
|
296 |
+
Args:
|
297 |
+
src: the sequence to the encoder layer (required).
|
298 |
+
attn_mask: the mask for the src sequence (optional).
|
299 |
+
key_padding_mask: the mask for the src keys per batch (optional).
|
300 |
+
Returns:
|
301 |
+
src3: the output of transformer layer, share the same shape as src.
|
302 |
+
"""
|
303 |
+
src2 = self.self_attn(
|
304 |
+
self.norm(src), attn_mask=attn_mask, key_padding_mask=key_padding_mask)
|
305 |
+
src2 = src + self.dropout1(src2)
|
306 |
+
src3 = self.feedforward(src2)
|
307 |
+
src3 = src2 + self.dropout2(src3)
|
308 |
+
|
309 |
+
return src3
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
class RobertaClassificationHead(nn.Module):
|
314 |
+
"""Head for sentence-level classification tasks."""
|
315 |
+
|
316 |
+
def __init__(self, config):
|
317 |
+
super().__init__()
|
318 |
+
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
|
319 |
+
classifier_dropout = (
|
320 |
+
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
|
321 |
+
)
|
322 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
323 |
+
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
|
324 |
+
|
325 |
+
def forward(self, features, **kwargs):
|
326 |
+
x = features[:, 0, :] # take <s> token (equiv. to [CLS])
|
327 |
+
x = self.dropout(x)
|
328 |
+
x = self.dense(x)
|
329 |
+
x = torch.tanh(x)
|
330 |
+
x = self.dropout(x)
|
331 |
+
x = self.out_proj(x)
|
332 |
+
return x
|
333 |
+
|
334 |
+
|
335 |
+
##########################################
|
336 |
+
# Custom Models
|
337 |
+
##########################################
|
338 |
+
def cumprod(x, reverse=False, exclusive=False):
|
339 |
+
"""cumulative product."""
|
340 |
+
if reverse:
|
341 |
+
x = x.flip([-1])
|
342 |
+
|
343 |
+
if exclusive:
|
344 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=1)
|
345 |
+
|
346 |
+
cx = x.cumprod(-1)
|
347 |
+
|
348 |
+
if reverse:
|
349 |
+
cx = cx.flip([-1])
|
350 |
+
return cx
|
351 |
+
|
352 |
+
def cumsum(x, reverse=False, exclusive=False):
|
353 |
+
"""cumulative sum."""
|
354 |
+
bsz, _, length = x.size()
|
355 |
+
device = x.device
|
356 |
+
if reverse:
|
357 |
+
if exclusive:
|
358 |
+
w = torch.ones([bsz, length, length], device=device).tril(-1)
|
359 |
+
else:
|
360 |
+
w = torch.ones([bsz, length, length], device=device).tril(0)
|
361 |
+
cx = torch.bmm(x, w)
|
362 |
+
else:
|
363 |
+
if exclusive:
|
364 |
+
w = torch.ones([bsz, length, length], device=device).triu(1)
|
365 |
+
else:
|
366 |
+
w = torch.ones([bsz, length, length], device=device).triu(0)
|
367 |
+
cx = torch.bmm(x, w)
|
368 |
+
return cx
|
369 |
+
|
370 |
+
def cummin(x, reverse=False, exclusive=False, max_value=1e9):
|
371 |
+
"""cumulative min."""
|
372 |
+
if reverse:
|
373 |
+
if exclusive:
|
374 |
+
x = F.pad(x[:, :, 1:], (0, 1), value=max_value)
|
375 |
+
x = x.flip([-1]).cummin(-1)[0].flip([-1])
|
376 |
+
else:
|
377 |
+
if exclusive:
|
378 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=max_value)
|
379 |
+
x = x.cummin(-1)[0]
|
380 |
+
return x
|
381 |
+
|
382 |
+
class Transformer(nn.Module):
|
383 |
+
"""Transformer model."""
|
384 |
+
|
385 |
+
def __init__(self,
|
386 |
+
hidden_size,
|
387 |
+
nlayers,
|
388 |
+
ntokens,
|
389 |
+
nhead=8,
|
390 |
+
dropout=0.1,
|
391 |
+
dropatt=0.1,
|
392 |
+
relative_bias=True,
|
393 |
+
pos_emb=False,
|
394 |
+
pad=0):
|
395 |
+
"""Initialization.
|
396 |
+
|
397 |
+
Args:
|
398 |
+
hidden_size: dimension of inputs and hidden states
|
399 |
+
nlayers: number of layers
|
400 |
+
ntokens: number of output categories
|
401 |
+
nhead: number of self-attention heads
|
402 |
+
dropout: dropout rate
|
403 |
+
dropatt: drop attention rate
|
404 |
+
relative_bias: bool, indicate whether use a relative position based
|
405 |
+
attention bias
|
406 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
407 |
+
pad: pad token index
|
408 |
+
"""
|
409 |
+
|
410 |
+
super(Transformer, self).__init__()
|
411 |
+
|
412 |
+
self.drop = nn.Dropout(dropout)
|
413 |
+
|
414 |
+
self.emb = nn.Embedding(ntokens, hidden_size)
|
415 |
+
if pos_emb:
|
416 |
+
self.pos_emb = nn.Embedding(500, hidden_size)
|
417 |
+
|
418 |
+
self.layers = nn.ModuleList([
|
419 |
+
TransformerLayer(hidden_size, nhead, hidden_size * 4, dropout,
|
420 |
+
dropatt=dropatt, relative_bias=relative_bias)
|
421 |
+
for _ in range(nlayers)])
|
422 |
+
|
423 |
+
self.norm = nn.LayerNorm(hidden_size)
|
424 |
+
|
425 |
+
self.output_layer = nn.Linear(hidden_size, ntokens)
|
426 |
+
self.output_layer.weight = self.emb.weight
|
427 |
+
|
428 |
+
self.init_weights()
|
429 |
+
|
430 |
+
self.nlayers = nlayers
|
431 |
+
self.nhead = nhead
|
432 |
+
self.ntokens = ntokens
|
433 |
+
self.hidden_size = hidden_size
|
434 |
+
self.pad = pad
|
435 |
+
|
436 |
+
def init_weights(self):
|
437 |
+
"""Initialize token embedding and output bias."""
|
438 |
+
initrange = 0.1
|
439 |
+
self.emb.weight.data.uniform_(-initrange, initrange)
|
440 |
+
if hasattr(self, 'pos_emb'):
|
441 |
+
self.pos_emb.weight.data.uniform_(-initrange, initrange)
|
442 |
+
self.output_layer.bias.data.fill_(0)
|
443 |
+
|
444 |
+
def visibility(self, x, device):
|
445 |
+
"""Mask pad tokens."""
|
446 |
+
visibility = (x != self.pad).float()
|
447 |
+
visibility = visibility[:, None, :].expand(-1, x.size(1), -1)
|
448 |
+
visibility = torch.repeat_interleave(visibility, self.nhead, dim=0)
|
449 |
+
return visibility.log()
|
450 |
+
|
451 |
+
def encode(self, x, pos):
|
452 |
+
"""Standard transformer encode process."""
|
453 |
+
h = self.emb(x)
|
454 |
+
if hasattr(self, 'pos_emb'):
|
455 |
+
h = h + self.pos_emb(pos)
|
456 |
+
h_list = []
|
457 |
+
visibility = self.visibility(x, x.device)
|
458 |
+
|
459 |
+
for i in range(self.nlayers):
|
460 |
+
h_list.append(h)
|
461 |
+
h = self.layers[i](
|
462 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
463 |
+
|
464 |
+
output = h
|
465 |
+
h_array = torch.stack(h_list, dim=2)
|
466 |
+
|
467 |
+
return output, h_array
|
468 |
+
|
469 |
+
def forward(self, x, pos):
|
470 |
+
"""Pass the input through the encoder layer.
|
471 |
+
|
472 |
+
Args:
|
473 |
+
x: input tokens (required).
|
474 |
+
pos: position for each token (optional).
|
475 |
+
Returns:
|
476 |
+
output: probability distributions for missing tokens.
|
477 |
+
state_dict: parsing results and raw output
|
478 |
+
"""
|
479 |
+
|
480 |
+
batch_size, length = x.size()
|
481 |
+
|
482 |
+
raw_output, _ = self.encode(x, pos)
|
483 |
+
raw_output = self.norm(raw_output)
|
484 |
+
raw_output = self.drop(raw_output)
|
485 |
+
|
486 |
+
output = self.output_layer(raw_output)
|
487 |
+
return output.view(batch_size * length, -1), {'raw_output': raw_output,}
|
488 |
+
|
489 |
+
class StructFormer(Transformer):
|
490 |
+
"""StructFormer model."""
|
491 |
+
|
492 |
+
def __init__(self,
|
493 |
+
hidden_size,
|
494 |
+
n_context_layers,
|
495 |
+
nlayers,
|
496 |
+
ntokens,
|
497 |
+
nhead=8,
|
498 |
+
dropout=0.1,
|
499 |
+
dropatt=0.1,
|
500 |
+
relative_bias=False,
|
501 |
+
pos_emb=False,
|
502 |
+
pad=0,
|
503 |
+
n_parser_layers=4,
|
504 |
+
conv_size=9,
|
505 |
+
relations=('head', 'child'),
|
506 |
+
weight_act='softmax'):
|
507 |
+
"""Initialization.
|
508 |
+
|
509 |
+
Args:
|
510 |
+
hidden_size: dimension of inputs and hidden states
|
511 |
+
nlayers: number of layers
|
512 |
+
ntokens: number of output categories
|
513 |
+
nhead: number of self-attention heads
|
514 |
+
dropout: dropout rate
|
515 |
+
dropatt: drop attention rate
|
516 |
+
relative_bias: bool, indicate whether use a relative position based
|
517 |
+
attention bias
|
518 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
519 |
+
pad: pad token index
|
520 |
+
n_parser_layers: number of parsing layers
|
521 |
+
conv_size: convolution kernel size for parser
|
522 |
+
relations: relations that are used to compute self attention
|
523 |
+
weight_act: relations distribution activation function
|
524 |
+
"""
|
525 |
+
|
526 |
+
super(StructFormer, self).__init__(
|
527 |
+
hidden_size,
|
528 |
+
nlayers,
|
529 |
+
ntokens,
|
530 |
+
nhead=nhead,
|
531 |
+
dropout=dropout,
|
532 |
+
dropatt=dropatt,
|
533 |
+
relative_bias=relative_bias,
|
534 |
+
pos_emb=pos_emb,
|
535 |
+
pad=pad)
|
536 |
+
|
537 |
+
|
538 |
+
def encode(self, x, pos):
|
539 |
+
h = self.emb(x)
|
540 |
+
if hasattr(self, 'pos_emb'):
|
541 |
+
h = h + self.pos_emb(pos)
|
542 |
+
h_list = []
|
543 |
+
visibility = self.visibility(x, x.device)
|
544 |
+
|
545 |
+
for i in range(self.nlayers):
|
546 |
+
h_list.append(h)
|
547 |
+
h = self.layers[i](
|
548 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
549 |
+
|
550 |
+
output = h
|
551 |
+
h_array = torch.stack(h_list, dim=2)
|
552 |
+
|
553 |
+
return output
|
554 |
+
|
555 |
+
|
556 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
557 |
+
|
558 |
+
x = input_ids
|
559 |
+
batch_size, length = x.size()
|
560 |
+
|
561 |
+
if position_ids is None:
|
562 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
563 |
+
|
564 |
+
raw_output = self.encode(x, pos)
|
565 |
+
raw_output = self.norm(raw_output)
|
566 |
+
raw_output = self.drop(raw_output)
|
567 |
+
|
568 |
+
output = self.output_layer(raw_output)
|
569 |
+
|
570 |
+
loss = None
|
571 |
+
if labels is not None:
|
572 |
+
loss_fct = nn.CrossEntropyLoss()
|
573 |
+
loss = loss_fct(output.view(batch_size * length, -1), labels.reshape(-1))
|
574 |
+
|
575 |
+
return MaskedLMOutput(
|
576 |
+
loss=loss, # shape: 1
|
577 |
+
logits=output, # shape: (batch_size * length, ntokens)
|
578 |
+
hidden_states=None,
|
579 |
+
attentions=None,
|
580 |
+
)
|
581 |
+
|
582 |
+
##########################################
|
583 |
+
# HuggingFace Model
|
584 |
+
##########################################
|
585 |
+
class StructformerModel(PreTrainedModel):
|
586 |
+
config_class = StructformerConfig
|
587 |
+
|
588 |
+
def __init__(self, config):
|
589 |
+
super().__init__(config)
|
590 |
+
self.model = StructFormer(
|
591 |
+
hidden_size=config.hidden_size,
|
592 |
+
n_context_layers=config.n_context_layers,
|
593 |
+
nlayers=config.nlayers,
|
594 |
+
ntokens=config.ntokens,
|
595 |
+
nhead=config.nhead,
|
596 |
+
dropout=config.dropout,
|
597 |
+
dropatt=config.dropatt,
|
598 |
+
relative_bias=config.relative_bias,
|
599 |
+
pos_emb=config.pos_emb,
|
600 |
+
pad=config.pad,
|
601 |
+
n_parser_layers=config.n_parser_layers,
|
602 |
+
conv_size=config.conv_size,
|
603 |
+
relations=config.relations,
|
604 |
+
weight_act=config.weight_act
|
605 |
+
)
|
606 |
+
|
607 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
608 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
609 |
+
|
610 |
+
|
611 |
+
class StructFormerClassification(Transformer):
|
612 |
+
"""StructFormer model."""
|
613 |
+
|
614 |
+
def __init__(self,
|
615 |
+
hidden_size,
|
616 |
+
n_context_layers,
|
617 |
+
nlayers,
|
618 |
+
ntokens,
|
619 |
+
nhead=8,
|
620 |
+
dropout=0.1,
|
621 |
+
dropatt=0.1,
|
622 |
+
relative_bias=False,
|
623 |
+
pos_emb=False,
|
624 |
+
pad=0,
|
625 |
+
n_parser_layers=4,
|
626 |
+
conv_size=9,
|
627 |
+
relations=('head', 'child'),
|
628 |
+
weight_act='softmax',
|
629 |
+
config=None,
|
630 |
+
):
|
631 |
+
|
632 |
+
|
633 |
+
super(StructFormerClassification, self).__init__(
|
634 |
+
hidden_size,
|
635 |
+
nlayers,
|
636 |
+
ntokens,
|
637 |
+
nhead=nhead,
|
638 |
+
dropout=dropout,
|
639 |
+
dropatt=dropatt,
|
640 |
+
relative_bias=relative_bias,
|
641 |
+
pos_emb=pos_emb,
|
642 |
+
pad=pad)
|
643 |
+
|
644 |
+
self.num_labels = config.num_labels
|
645 |
+
self.config = config
|
646 |
+
|
647 |
+
self.classifier = RobertaClassificationHead(config)
|
648 |
+
|
649 |
+
def encode(self, x, pos):
|
650 |
+
h = self.emb(x)
|
651 |
+
if hasattr(self, 'pos_emb'):
|
652 |
+
h = h + self.pos_emb(pos)
|
653 |
+
h_list = []
|
654 |
+
visibility = self.visibility(x, x.device)
|
655 |
+
|
656 |
+
for i in range(self.nlayers):
|
657 |
+
h_list.append(h)
|
658 |
+
h = self.layers[i](
|
659 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
660 |
+
|
661 |
+
output = h
|
662 |
+
h_array = torch.stack(h_list, dim=2)
|
663 |
+
|
664 |
+
return output
|
665 |
+
|
666 |
+
|
667 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
668 |
+
|
669 |
+
x = input_ids
|
670 |
+
batch_size, length = x.size()
|
671 |
+
|
672 |
+
if position_ids is None:
|
673 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
674 |
+
|
675 |
+
raw_output = self.encode(x, pos)
|
676 |
+
raw_output = self.norm(raw_output)
|
677 |
+
raw_output = self.drop(raw_output)
|
678 |
+
|
679 |
+
#output = self.output_layer(raw_output)
|
680 |
+
logits = self.classifier(raw_output)
|
681 |
+
|
682 |
+
loss = None
|
683 |
+
if labels is not None:
|
684 |
+
if self.config.problem_type is None:
|
685 |
+
if self.num_labels == 1:
|
686 |
+
self.config.problem_type = "regression"
|
687 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
688 |
+
self.config.problem_type = "single_label_classification"
|
689 |
+
else:
|
690 |
+
self.config.problem_type = "multi_label_classification"
|
691 |
+
|
692 |
+
if self.config.problem_type == "regression":
|
693 |
+
loss_fct = MSELoss()
|
694 |
+
if self.num_labels == 1:
|
695 |
+
loss = loss_fct(logits.squeeze(), labels.squeeze())
|
696 |
+
else:
|
697 |
+
loss = loss_fct(logits, labels)
|
698 |
+
elif self.config.problem_type == "single_label_classification":
|
699 |
+
loss_fct = CrossEntropyLoss()
|
700 |
+
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
701 |
+
elif self.config.problem_type == "multi_label_classification":
|
702 |
+
loss_fct = BCEWithLogitsLoss()
|
703 |
+
loss = loss_fct(logits, labels)
|
704 |
+
|
705 |
+
|
706 |
+
return SequenceClassifierOutput(
|
707 |
+
loss=loss,
|
708 |
+
logits=logits,
|
709 |
+
hidden_states=None,
|
710 |
+
attentions=None,
|
711 |
+
)
|
712 |
+
|
713 |
+
|
714 |
+
class StructformerModelForSequenceClassification(PreTrainedModel):
|
715 |
+
config_class = StructformerConfig
|
716 |
+
def __init__(self, config):
|
717 |
+
super().__init__(config)
|
718 |
+
self.model = StructFormerClassification(
|
719 |
+
hidden_size=config.hidden_size,
|
720 |
+
n_context_layers=config.n_context_layers,
|
721 |
+
nlayers=config.nlayers,
|
722 |
+
ntokens=config.ntokens,
|
723 |
+
nhead=config.nhead,
|
724 |
+
dropout=config.dropout,
|
725 |
+
dropatt=config.dropatt,
|
726 |
+
relative_bias=config.relative_bias,
|
727 |
+
pos_emb=config.pos_emb,
|
728 |
+
pad=config.pad,
|
729 |
+
n_parser_layers=config.n_parser_layers,
|
730 |
+
conv_size=config.conv_size,
|
731 |
+
relations=config.relations,
|
732 |
+
weight_act=config.weight_act,
|
733 |
+
config=config)
|
734 |
+
|
735 |
+
def _init_weights(self, module):
|
736 |
+
"""Initialize the weights"""
|
737 |
+
if isinstance(module, nn.Linear):
|
738 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
739 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
740 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
741 |
+
if module.bias is not None:
|
742 |
+
module.bias.data.zero_()
|
743 |
+
elif isinstance(module, nn.Embedding):
|
744 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
745 |
+
if module.padding_idx is not None:
|
746 |
+
module.weight.data[module.padding_idx].zero_()
|
747 |
+
elif isinstance(module, nn.LayerNorm):
|
748 |
+
if module.bias is not None:
|
749 |
+
module.bias.data.zero_()
|
750 |
+
module.weight.data.fill_(1.0)
|
751 |
+
|
752 |
+
|
753 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
754 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
finetune/cola/tokenizer_config.json
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"bos_token": {
|
4 |
+
"__type": "AddedToken",
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false
|
10 |
+
},
|
11 |
+
"cls_token": {
|
12 |
+
"__type": "AddedToken",
|
13 |
+
"content": "<s>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false
|
18 |
+
},
|
19 |
+
"eos_token": {
|
20 |
+
"__type": "AddedToken",
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false
|
26 |
+
},
|
27 |
+
"errors": "replace",
|
28 |
+
"mask_token": {
|
29 |
+
"__type": "AddedToken",
|
30 |
+
"content": "<mask>",
|
31 |
+
"lstrip": true,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false
|
35 |
+
},
|
36 |
+
"model_max_length": 512,
|
37 |
+
"name_or_path": "final_models/transformer_base_final_2",
|
38 |
+
"pad_token": {
|
39 |
+
"__type": "AddedToken",
|
40 |
+
"content": "<pad>",
|
41 |
+
"lstrip": false,
|
42 |
+
"normalized": true,
|
43 |
+
"rstrip": false,
|
44 |
+
"single_word": false
|
45 |
+
},
|
46 |
+
"sep_token": {
|
47 |
+
"__type": "AddedToken",
|
48 |
+
"content": "</s>",
|
49 |
+
"lstrip": false,
|
50 |
+
"normalized": true,
|
51 |
+
"rstrip": false,
|
52 |
+
"single_word": false
|
53 |
+
},
|
54 |
+
"special_tokens_map_file": null,
|
55 |
+
"tokenizer_class": "RobertaTokenizer",
|
56 |
+
"trim_offsets": true,
|
57 |
+
"unk_token": {
|
58 |
+
"__type": "AddedToken",
|
59 |
+
"content": "<unk>",
|
60 |
+
"lstrip": false,
|
61 |
+
"normalized": true,
|
62 |
+
"rstrip": false,
|
63 |
+
"single_word": false
|
64 |
+
}
|
65 |
+
}
|
finetune/cola/train_results.json
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"train_loss": 0.42006420190783517,
|
4 |
+
"train_runtime": 267.2668,
|
5 |
+
"train_samples": 8164,
|
6 |
+
"train_samples_per_second": 305.463,
|
7 |
+
"train_steps_per_second": 2.582
|
8 |
+
}
|
finetune/cola/trainer_state.json
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": 0.7777040477770405,
|
3 |
+
"best_model_checkpoint": "final_models/transformer_base_final_2/finetune/cola/checkpoint-400",
|
4 |
+
"epoch": 10.0,
|
5 |
+
"global_step": 690,
|
6 |
+
"is_hyper_param_search": false,
|
7 |
+
"is_local_process_zero": true,
|
8 |
+
"is_world_process_zero": true,
|
9 |
+
"log_history": [
|
10 |
+
{
|
11 |
+
"epoch": 5.8,
|
12 |
+
"eval_accuracy": 0.6712462902069092,
|
13 |
+
"eval_f1": 0.7777040477770405,
|
14 |
+
"eval_loss": 0.7399011254310608,
|
15 |
+
"eval_mcc": 0.15981907397227785,
|
16 |
+
"eval_runtime": 1.4063,
|
17 |
+
"eval_samples_per_second": 724.62,
|
18 |
+
"eval_steps_per_second": 91.022,
|
19 |
+
"step": 400
|
20 |
+
},
|
21 |
+
{
|
22 |
+
"epoch": 7.25,
|
23 |
+
"learning_rate": 1.3768115942028985e-05,
|
24 |
+
"loss": 0.4934,
|
25 |
+
"step": 500
|
26 |
+
},
|
27 |
+
{
|
28 |
+
"epoch": 10.0,
|
29 |
+
"step": 690,
|
30 |
+
"total_flos": 5372103024537600.0,
|
31 |
+
"train_loss": 0.42006420190783517,
|
32 |
+
"train_runtime": 267.2668,
|
33 |
+
"train_samples_per_second": 305.463,
|
34 |
+
"train_steps_per_second": 2.582
|
35 |
+
}
|
36 |
+
],
|
37 |
+
"max_steps": 690,
|
38 |
+
"num_train_epochs": 10,
|
39 |
+
"total_flos": 5372103024537600.0,
|
40 |
+
"trial_name": null,
|
41 |
+
"trial_params": null
|
42 |
+
}
|
finetune/cola/training_args.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:42f03300332c3f519b9e94f52c9e382ae77f93b0d20616ae0c3858d1e25c21dc
|
3 |
+
size 3503
|
finetune/cola/vocab.json
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/control_raising_control/all_results.json
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"epoch": 10.0,
|
3 |
+
"eval_accuracy": 0.8665371537208557,
|
4 |
+
"eval_f1": 0.8797792137856758,
|
5 |
+
"eval_loss": 0.8952956199645996,
|
6 |
+
"eval_mcc": 0.7560620169097876,
|
7 |
+
"eval_runtime": 18.4787,
|
8 |
+
"eval_samples": 13382,
|
9 |
+
"eval_samples_per_second": 724.185,
|
10 |
+
"eval_steps_per_second": 90.537,
|
11 |
+
"train_loss": 0.04565125611526045,
|
12 |
+
"train_runtime": 234.6588,
|
13 |
+
"train_samples": 6570,
|
14 |
+
"train_samples_per_second": 279.981,
|
15 |
+
"train_steps_per_second": 2.344
|
16 |
+
}
|
finetune/control_raising_control/checkpoint-400/config.json
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"_name_or_path": "final_models/transformer_base_final_2",
|
3 |
+
"architectures": [
|
4 |
+
"StructformerModelForSequenceClassification"
|
5 |
+
],
|
6 |
+
"attention_probs_dropout_prob": 0.1,
|
7 |
+
"auto_map": {
|
8 |
+
"AutoConfig": "structformer_as_hf_no_parser.StructformerConfig",
|
9 |
+
"AutoModelForMaskedLM": "structformer_as_hf_no_parser.StructformerModel",
|
10 |
+
"AutoModelForSequenceClassification": "structformer_as_hf_no_parser.StructformerModelForSequenceClassification"
|
11 |
+
},
|
12 |
+
"bos_token_id": 0,
|
13 |
+
"classifier_dropout": null,
|
14 |
+
"conv_size": 9,
|
15 |
+
"dropatt": 0.1,
|
16 |
+
"dropout": 0.1,
|
17 |
+
"eos_token_id": 2,
|
18 |
+
"hidden_act": "gelu",
|
19 |
+
"hidden_dropout_prob": 0.1,
|
20 |
+
"hidden_size": 768,
|
21 |
+
"id2label": {
|
22 |
+
"0": 0,
|
23 |
+
"1": 1
|
24 |
+
},
|
25 |
+
"initializer_range": 0.02,
|
26 |
+
"intermediate_size": 3072,
|
27 |
+
"label2id": {
|
28 |
+
"0": 0,
|
29 |
+
"1": 1
|
30 |
+
},
|
31 |
+
"layer_norm_eps": 1e-05,
|
32 |
+
"max_position_embeddings": 514,
|
33 |
+
"model_type": "structformer",
|
34 |
+
"n_context_layers": 0,
|
35 |
+
"n_parser_layers": 0,
|
36 |
+
"nhead": 12,
|
37 |
+
"nlayers": 12,
|
38 |
+
"ntokens": 32000,
|
39 |
+
"num_attention_heads": 12,
|
40 |
+
"num_hidden_layers": 12,
|
41 |
+
"pad": 0,
|
42 |
+
"pad_token_id": 1,
|
43 |
+
"pos_emb": true,
|
44 |
+
"position_embedding_type": "absolute",
|
45 |
+
"problem_type": "single_label_classification",
|
46 |
+
"relations": [
|
47 |
+
"head",
|
48 |
+
"child"
|
49 |
+
],
|
50 |
+
"relative_bias": false,
|
51 |
+
"torch_dtype": "float32",
|
52 |
+
"transformers_version": "4.26.1",
|
53 |
+
"type_vocab_size": 1,
|
54 |
+
"use_cache": true,
|
55 |
+
"vocab_size": 32000,
|
56 |
+
"weight_act": "softmax"
|
57 |
+
}
|
finetune/control_raising_control/checkpoint-400/merges.txt
ADDED
The diff for this file is too large to render.
See raw diff
|
|
finetune/control_raising_control/checkpoint-400/optimizer.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:eb5abc7df98c1236620c1f8058c2ccdd643b47bf1e8151cda6472497757b7033
|
3 |
+
size 884981025
|
finetune/control_raising_control/checkpoint-400/pytorch_model.bin
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a17588df7b02a3563a195e3933f14f28429949d3cad166f41883869da4ce57a5
|
3 |
+
size 442624943
|
finetune/control_raising_control/checkpoint-400/rng_state.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:02576a68480d1bd556bf2545dbb91a6fcd2d751faf798e19f80054fd04bf34a7
|
3 |
+
size 14503
|
finetune/control_raising_control/checkpoint-400/scheduler.pt
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0c2403f14641b3caeb1b4d17bf70ec776358494ec9059cbe53a4c9c5a18c4c15
|
3 |
+
size 623
|
finetune/control_raising_control/checkpoint-400/special_tokens_map.json
ADDED
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"bos_token": "<s>",
|
3 |
+
"cls_token": "<s>",
|
4 |
+
"eos_token": "</s>",
|
5 |
+
"mask_token": {
|
6 |
+
"content": "<mask>",
|
7 |
+
"lstrip": true,
|
8 |
+
"normalized": false,
|
9 |
+
"rstrip": false,
|
10 |
+
"single_word": false
|
11 |
+
},
|
12 |
+
"pad_token": "<pad>",
|
13 |
+
"sep_token": "</s>",
|
14 |
+
"unk_token": "<unk>"
|
15 |
+
}
|
finetune/control_raising_control/checkpoint-400/structformer_as_hf_no_parser.py
ADDED
@@ -0,0 +1,754 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from torch.nn import init
|
5 |
+
from transformers import PreTrainedModel
|
6 |
+
from transformers import PretrainedConfig
|
7 |
+
from transformers.modeling_outputs import MaskedLMOutput
|
8 |
+
from typing import List
|
9 |
+
from torch.nn import CrossEntropyLoss, MSELoss, BCEWithLogitsLoss
|
10 |
+
from transformers.modeling_outputs import (
|
11 |
+
BaseModelOutputWithPastAndCrossAttentions,
|
12 |
+
BaseModelOutputWithPoolingAndCrossAttentions,
|
13 |
+
MaskedLMOutput,
|
14 |
+
SequenceClassifierOutput
|
15 |
+
)
|
16 |
+
|
17 |
+
##########################################
|
18 |
+
# HuggingFace Config
|
19 |
+
##########################################
|
20 |
+
class StructformerConfig(PretrainedConfig):
|
21 |
+
model_type = "structformer"
|
22 |
+
|
23 |
+
def __init__(
|
24 |
+
self,
|
25 |
+
hidden_size=768,
|
26 |
+
n_context_layers=2,
|
27 |
+
nlayers=6,
|
28 |
+
ntokens=32000,
|
29 |
+
nhead=8,
|
30 |
+
dropout=0.1,
|
31 |
+
dropatt=0.1,
|
32 |
+
relative_bias=False,
|
33 |
+
pos_emb=False,
|
34 |
+
pad=0,
|
35 |
+
n_parser_layers=4,
|
36 |
+
conv_size=9,
|
37 |
+
relations=('head', 'child'),
|
38 |
+
weight_act='softmax',
|
39 |
+
**kwargs,
|
40 |
+
):
|
41 |
+
self.hidden_size = hidden_size
|
42 |
+
self.n_context_layers = n_context_layers
|
43 |
+
self.nlayers = nlayers
|
44 |
+
self.ntokens = ntokens
|
45 |
+
self.nhead = nhead
|
46 |
+
self.dropout = dropout
|
47 |
+
self.dropatt = dropatt
|
48 |
+
self.relative_bias = relative_bias
|
49 |
+
self.pos_emb = pos_emb
|
50 |
+
self.pad = pad
|
51 |
+
self.n_parser_layers = n_parser_layers
|
52 |
+
self.conv_size = conv_size
|
53 |
+
self.relations = relations
|
54 |
+
self.weight_act = weight_act
|
55 |
+
super().__init__(**kwargs)
|
56 |
+
|
57 |
+
##########################################
|
58 |
+
# Custom Layers
|
59 |
+
##########################################
|
60 |
+
def _get_activation_fn(activation):
|
61 |
+
"""Get specified activation function."""
|
62 |
+
if activation == "relu":
|
63 |
+
return nn.ReLU()
|
64 |
+
elif activation == "gelu":
|
65 |
+
return nn.GELU()
|
66 |
+
elif activation == "leakyrelu":
|
67 |
+
return nn.LeakyReLU()
|
68 |
+
|
69 |
+
raise RuntimeError(
|
70 |
+
"activation should be relu/gelu, not {}".format(activation))
|
71 |
+
|
72 |
+
class Conv1d(nn.Module):
|
73 |
+
"""1D convolution layer."""
|
74 |
+
|
75 |
+
def __init__(self, hidden_size, kernel_size, dilation=1):
|
76 |
+
"""Initialization.
|
77 |
+
|
78 |
+
Args:
|
79 |
+
hidden_size: dimension of input embeddings
|
80 |
+
kernel_size: convolution kernel size
|
81 |
+
dilation: the spacing between the kernel points
|
82 |
+
"""
|
83 |
+
super(Conv1d, self).__init__()
|
84 |
+
|
85 |
+
if kernel_size % 2 == 0:
|
86 |
+
padding = (kernel_size // 2) * dilation
|
87 |
+
self.shift = True
|
88 |
+
else:
|
89 |
+
padding = ((kernel_size - 1) // 2) * dilation
|
90 |
+
self.shift = False
|
91 |
+
self.conv = nn.Conv1d(
|
92 |
+
hidden_size,
|
93 |
+
hidden_size,
|
94 |
+
kernel_size,
|
95 |
+
padding=padding,
|
96 |
+
dilation=dilation)
|
97 |
+
|
98 |
+
def forward(self, x):
|
99 |
+
"""Compute convolution.
|
100 |
+
|
101 |
+
Args:
|
102 |
+
x: input embeddings
|
103 |
+
Returns:
|
104 |
+
conv_output: convolution results
|
105 |
+
"""
|
106 |
+
|
107 |
+
if self.shift:
|
108 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)[:, 1:]
|
109 |
+
else:
|
110 |
+
return self.conv(x.transpose(1, 2)).transpose(1, 2)
|
111 |
+
|
112 |
+
class MultiheadAttention(nn.Module):
|
113 |
+
"""Multi-head self-attention layer."""
|
114 |
+
|
115 |
+
def __init__(self,
|
116 |
+
embed_dim,
|
117 |
+
num_heads,
|
118 |
+
dropout=0.,
|
119 |
+
bias=True,
|
120 |
+
v_proj=True,
|
121 |
+
out_proj=True,
|
122 |
+
relative_bias=True):
|
123 |
+
"""Initialization.
|
124 |
+
|
125 |
+
Args:
|
126 |
+
embed_dim: dimension of input embeddings
|
127 |
+
num_heads: number of self-attention heads
|
128 |
+
dropout: dropout rate
|
129 |
+
bias: bool, indicate whether include bias for linear transformations
|
130 |
+
v_proj: bool, indicate whether project inputs to new values
|
131 |
+
out_proj: bool, indicate whether project outputs to new values
|
132 |
+
relative_bias: bool, indicate whether use a relative position based
|
133 |
+
attention bias
|
134 |
+
"""
|
135 |
+
|
136 |
+
super(MultiheadAttention, self).__init__()
|
137 |
+
self.embed_dim = embed_dim
|
138 |
+
|
139 |
+
self.num_heads = num_heads
|
140 |
+
self.drop = nn.Dropout(dropout)
|
141 |
+
self.head_dim = embed_dim // num_heads
|
142 |
+
assert self.head_dim * num_heads == self.embed_dim, ("embed_dim must be "
|
143 |
+
"divisible by "
|
144 |
+
"num_heads")
|
145 |
+
|
146 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
147 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
148 |
+
if v_proj:
|
149 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
150 |
+
else:
|
151 |
+
self.v_proj = nn.Identity()
|
152 |
+
|
153 |
+
if out_proj:
|
154 |
+
self.out_proj = nn.Linear(embed_dim, embed_dim, bias=bias)
|
155 |
+
else:
|
156 |
+
self.out_proj = nn.Identity()
|
157 |
+
|
158 |
+
if relative_bias:
|
159 |
+
self.relative_bias = nn.Parameter(torch.zeros((self.num_heads, 512)))
|
160 |
+
else:
|
161 |
+
self.relative_bias = None
|
162 |
+
|
163 |
+
self._reset_parameters()
|
164 |
+
|
165 |
+
def _reset_parameters(self):
|
166 |
+
"""Initialize attention parameters."""
|
167 |
+
|
168 |
+
init.xavier_uniform_(self.q_proj.weight)
|
169 |
+
init.constant_(self.q_proj.bias, 0.)
|
170 |
+
|
171 |
+
init.xavier_uniform_(self.k_proj.weight)
|
172 |
+
init.constant_(self.k_proj.bias, 0.)
|
173 |
+
|
174 |
+
if isinstance(self.v_proj, nn.Linear):
|
175 |
+
init.xavier_uniform_(self.v_proj.weight)
|
176 |
+
init.constant_(self.v_proj.bias, 0.)
|
177 |
+
|
178 |
+
if isinstance(self.out_proj, nn.Linear):
|
179 |
+
init.xavier_uniform_(self.out_proj.weight)
|
180 |
+
init.constant_(self.out_proj.bias, 0.)
|
181 |
+
|
182 |
+
def forward(self, query, key_padding_mask=None, attn_mask=None):
|
183 |
+
"""Compute multi-head self-attention.
|
184 |
+
|
185 |
+
Args:
|
186 |
+
query: input embeddings
|
187 |
+
key_padding_mask: 3D mask that prevents attention to certain positions
|
188 |
+
attn_mask: 3D mask that rescale the attention weight at each position
|
189 |
+
Returns:
|
190 |
+
attn_output: self-attention output
|
191 |
+
"""
|
192 |
+
|
193 |
+
length, bsz, embed_dim = query.size()
|
194 |
+
assert embed_dim == self.embed_dim
|
195 |
+
|
196 |
+
head_dim = embed_dim // self.num_heads
|
197 |
+
assert head_dim * self.num_heads == embed_dim, ("embed_dim must be "
|
198 |
+
"divisible by num_heads")
|
199 |
+
scaling = float(head_dim)**-0.5
|
200 |
+
|
201 |
+
q = self.q_proj(query)
|
202 |
+
k = self.k_proj(query)
|
203 |
+
v = self.v_proj(query)
|
204 |
+
|
205 |
+
q = q * scaling
|
206 |
+
|
207 |
+
if attn_mask is not None:
|
208 |
+
assert list(attn_mask.size()) == [bsz * self.num_heads,
|
209 |
+
query.size(0), query.size(0)]
|
210 |
+
|
211 |
+
q = q.contiguous().view(length, bsz * self.num_heads,
|
212 |
+
head_dim).transpose(0, 1)
|
213 |
+
k = k.contiguous().view(length, bsz * self.num_heads,
|
214 |
+
head_dim).transpose(0, 1)
|
215 |
+
v = v.contiguous().view(length, bsz * self.num_heads,
|
216 |
+
head_dim).transpose(0, 1)
|
217 |
+
|
218 |
+
attn_output_weights = torch.bmm(q, k.transpose(1, 2))
|
219 |
+
assert list(
|
220 |
+
attn_output_weights.size()) == [bsz * self.num_heads, length, length]
|
221 |
+
|
222 |
+
if self.relative_bias is not None:
|
223 |
+
pos = torch.arange(length, device=query.device)
|
224 |
+
relative_pos = torch.abs(pos[:, None] - pos[None, :]) + 256
|
225 |
+
relative_pos = relative_pos[None, :, :].expand(bsz * self.num_heads, -1,
|
226 |
+
-1)
|
227 |
+
|
228 |
+
relative_bias = self.relative_bias.repeat_interleave(bsz, dim=0)
|
229 |
+
relative_bias = relative_bias[:, None, :].expand(-1, length, -1)
|
230 |
+
relative_bias = torch.gather(relative_bias, 2, relative_pos)
|
231 |
+
attn_output_weights = attn_output_weights + relative_bias
|
232 |
+
|
233 |
+
if key_padding_mask is not None:
|
234 |
+
attn_output_weights = attn_output_weights + key_padding_mask
|
235 |
+
|
236 |
+
if attn_mask is None:
|
237 |
+
attn_output_weights = torch.softmax(attn_output_weights, dim=-1)
|
238 |
+
else:
|
239 |
+
attn_output_weights = torch.sigmoid(attn_output_weights) * attn_mask
|
240 |
+
|
241 |
+
attn_output_weights = self.drop(attn_output_weights)
|
242 |
+
|
243 |
+
attn_output = torch.bmm(attn_output_weights, v)
|
244 |
+
|
245 |
+
assert list(attn_output.size()) == [bsz * self.num_heads, length, head_dim]
|
246 |
+
attn_output = attn_output.transpose(0, 1).contiguous().view(
|
247 |
+
length, bsz, embed_dim)
|
248 |
+
attn_output = self.out_proj(attn_output)
|
249 |
+
|
250 |
+
return attn_output
|
251 |
+
|
252 |
+
class TransformerLayer(nn.Module):
|
253 |
+
"""TransformerEncoderLayer is made up of self-attn and feedforward network."""
|
254 |
+
|
255 |
+
def __init__(self,
|
256 |
+
d_model,
|
257 |
+
nhead,
|
258 |
+
dim_feedforward=2048,
|
259 |
+
dropout=0.1,
|
260 |
+
dropatt=0.1,
|
261 |
+
activation="leakyrelu",
|
262 |
+
relative_bias=True):
|
263 |
+
"""Initialization.
|
264 |
+
|
265 |
+
Args:
|
266 |
+
d_model: dimension of inputs
|
267 |
+
nhead: number of self-attention heads
|
268 |
+
dim_feedforward: dimension of hidden layer in feedforward layer
|
269 |
+
dropout: dropout rate
|
270 |
+
dropatt: drop attention rate
|
271 |
+
activation: activation function
|
272 |
+
relative_bias: bool, indicate whether use a relative position based
|
273 |
+
attention bias
|
274 |
+
"""
|
275 |
+
|
276 |
+
super(TransformerLayer, self).__init__()
|
277 |
+
|
278 |
+
self.self_attn = MultiheadAttention(
|
279 |
+
d_model, nhead, dropout=dropatt, relative_bias=relative_bias)
|
280 |
+
|
281 |
+
# Implementation of Feedforward model
|
282 |
+
self.feedforward = nn.Sequential(
|
283 |
+
nn.LayerNorm(d_model), nn.Linear(d_model, dim_feedforward),
|
284 |
+
_get_activation_fn(activation), nn.Dropout(dropout),
|
285 |
+
nn.Linear(dim_feedforward, d_model))
|
286 |
+
|
287 |
+
self.norm = nn.LayerNorm(d_model)
|
288 |
+
self.dropout1 = nn.Dropout(dropout)
|
289 |
+
self.dropout2 = nn.Dropout(dropout)
|
290 |
+
|
291 |
+
self.nhead = nhead
|
292 |
+
|
293 |
+
def forward(self, src, attn_mask=None, key_padding_mask=None):
|
294 |
+
"""Pass the input through the encoder layer.
|
295 |
+
|
296 |
+
Args:
|
297 |
+
src: the sequence to the encoder layer (required).
|
298 |
+
attn_mask: the mask for the src sequence (optional).
|
299 |
+
key_padding_mask: the mask for the src keys per batch (optional).
|
300 |
+
Returns:
|
301 |
+
src3: the output of transformer layer, share the same shape as src.
|
302 |
+
"""
|
303 |
+
src2 = self.self_attn(
|
304 |
+
self.norm(src), attn_mask=attn_mask, key_padding_mask=key_padding_mask)
|
305 |
+
src2 = src + self.dropout1(src2)
|
306 |
+
src3 = self.feedforward(src2)
|
307 |
+
src3 = src2 + self.dropout2(src3)
|
308 |
+
|
309 |
+
return src3
|
310 |
+
|
311 |
+
|
312 |
+
|
313 |
+
class RobertaClassificationHead(nn.Module):
|
314 |
+
"""Head for sentence-level classification tasks."""
|
315 |
+
|
316 |
+
def __init__(self, config):
|
317 |
+
super().__init__()
|
318 |
+
self.dense = nn.Linear(config.hidden_size, config.hidden_size)
|
319 |
+
classifier_dropout = (
|
320 |
+
config.classifier_dropout if config.classifier_dropout is not None else config.hidden_dropout_prob
|
321 |
+
)
|
322 |
+
self.dropout = nn.Dropout(classifier_dropout)
|
323 |
+
self.out_proj = nn.Linear(config.hidden_size, config.num_labels)
|
324 |
+
|
325 |
+
def forward(self, features, **kwargs):
|
326 |
+
x = features[:, 0, :] # take <s> token (equiv. to [CLS])
|
327 |
+
x = self.dropout(x)
|
328 |
+
x = self.dense(x)
|
329 |
+
x = torch.tanh(x)
|
330 |
+
x = self.dropout(x)
|
331 |
+
x = self.out_proj(x)
|
332 |
+
return x
|
333 |
+
|
334 |
+
|
335 |
+
##########################################
|
336 |
+
# Custom Models
|
337 |
+
##########################################
|
338 |
+
def cumprod(x, reverse=False, exclusive=False):
|
339 |
+
"""cumulative product."""
|
340 |
+
if reverse:
|
341 |
+
x = x.flip([-1])
|
342 |
+
|
343 |
+
if exclusive:
|
344 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=1)
|
345 |
+
|
346 |
+
cx = x.cumprod(-1)
|
347 |
+
|
348 |
+
if reverse:
|
349 |
+
cx = cx.flip([-1])
|
350 |
+
return cx
|
351 |
+
|
352 |
+
def cumsum(x, reverse=False, exclusive=False):
|
353 |
+
"""cumulative sum."""
|
354 |
+
bsz, _, length = x.size()
|
355 |
+
device = x.device
|
356 |
+
if reverse:
|
357 |
+
if exclusive:
|
358 |
+
w = torch.ones([bsz, length, length], device=device).tril(-1)
|
359 |
+
else:
|
360 |
+
w = torch.ones([bsz, length, length], device=device).tril(0)
|
361 |
+
cx = torch.bmm(x, w)
|
362 |
+
else:
|
363 |
+
if exclusive:
|
364 |
+
w = torch.ones([bsz, length, length], device=device).triu(1)
|
365 |
+
else:
|
366 |
+
w = torch.ones([bsz, length, length], device=device).triu(0)
|
367 |
+
cx = torch.bmm(x, w)
|
368 |
+
return cx
|
369 |
+
|
370 |
+
def cummin(x, reverse=False, exclusive=False, max_value=1e9):
|
371 |
+
"""cumulative min."""
|
372 |
+
if reverse:
|
373 |
+
if exclusive:
|
374 |
+
x = F.pad(x[:, :, 1:], (0, 1), value=max_value)
|
375 |
+
x = x.flip([-1]).cummin(-1)[0].flip([-1])
|
376 |
+
else:
|
377 |
+
if exclusive:
|
378 |
+
x = F.pad(x[:, :, :-1], (1, 0), value=max_value)
|
379 |
+
x = x.cummin(-1)[0]
|
380 |
+
return x
|
381 |
+
|
382 |
+
class Transformer(nn.Module):
|
383 |
+
"""Transformer model."""
|
384 |
+
|
385 |
+
def __init__(self,
|
386 |
+
hidden_size,
|
387 |
+
nlayers,
|
388 |
+
ntokens,
|
389 |
+
nhead=8,
|
390 |
+
dropout=0.1,
|
391 |
+
dropatt=0.1,
|
392 |
+
relative_bias=True,
|
393 |
+
pos_emb=False,
|
394 |
+
pad=0):
|
395 |
+
"""Initialization.
|
396 |
+
|
397 |
+
Args:
|
398 |
+
hidden_size: dimension of inputs and hidden states
|
399 |
+
nlayers: number of layers
|
400 |
+
ntokens: number of output categories
|
401 |
+
nhead: number of self-attention heads
|
402 |
+
dropout: dropout rate
|
403 |
+
dropatt: drop attention rate
|
404 |
+
relative_bias: bool, indicate whether use a relative position based
|
405 |
+
attention bias
|
406 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
407 |
+
pad: pad token index
|
408 |
+
"""
|
409 |
+
|
410 |
+
super(Transformer, self).__init__()
|
411 |
+
|
412 |
+
self.drop = nn.Dropout(dropout)
|
413 |
+
|
414 |
+
self.emb = nn.Embedding(ntokens, hidden_size)
|
415 |
+
if pos_emb:
|
416 |
+
self.pos_emb = nn.Embedding(500, hidden_size)
|
417 |
+
|
418 |
+
self.layers = nn.ModuleList([
|
419 |
+
TransformerLayer(hidden_size, nhead, hidden_size * 4, dropout,
|
420 |
+
dropatt=dropatt, relative_bias=relative_bias)
|
421 |
+
for _ in range(nlayers)])
|
422 |
+
|
423 |
+
self.norm = nn.LayerNorm(hidden_size)
|
424 |
+
|
425 |
+
self.output_layer = nn.Linear(hidden_size, ntokens)
|
426 |
+
self.output_layer.weight = self.emb.weight
|
427 |
+
|
428 |
+
self.init_weights()
|
429 |
+
|
430 |
+
self.nlayers = nlayers
|
431 |
+
self.nhead = nhead
|
432 |
+
self.ntokens = ntokens
|
433 |
+
self.hidden_size = hidden_size
|
434 |
+
self.pad = pad
|
435 |
+
|
436 |
+
def init_weights(self):
|
437 |
+
"""Initialize token embedding and output bias."""
|
438 |
+
initrange = 0.1
|
439 |
+
self.emb.weight.data.uniform_(-initrange, initrange)
|
440 |
+
if hasattr(self, 'pos_emb'):
|
441 |
+
self.pos_emb.weight.data.uniform_(-initrange, initrange)
|
442 |
+
self.output_layer.bias.data.fill_(0)
|
443 |
+
|
444 |
+
def visibility(self, x, device):
|
445 |
+
"""Mask pad tokens."""
|
446 |
+
visibility = (x != self.pad).float()
|
447 |
+
visibility = visibility[:, None, :].expand(-1, x.size(1), -1)
|
448 |
+
visibility = torch.repeat_interleave(visibility, self.nhead, dim=0)
|
449 |
+
return visibility.log()
|
450 |
+
|
451 |
+
def encode(self, x, pos):
|
452 |
+
"""Standard transformer encode process."""
|
453 |
+
h = self.emb(x)
|
454 |
+
if hasattr(self, 'pos_emb'):
|
455 |
+
h = h + self.pos_emb(pos)
|
456 |
+
h_list = []
|
457 |
+
visibility = self.visibility(x, x.device)
|
458 |
+
|
459 |
+
for i in range(self.nlayers):
|
460 |
+
h_list.append(h)
|
461 |
+
h = self.layers[i](
|
462 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
463 |
+
|
464 |
+
output = h
|
465 |
+
h_array = torch.stack(h_list, dim=2)
|
466 |
+
|
467 |
+
return output, h_array
|
468 |
+
|
469 |
+
def forward(self, x, pos):
|
470 |
+
"""Pass the input through the encoder layer.
|
471 |
+
|
472 |
+
Args:
|
473 |
+
x: input tokens (required).
|
474 |
+
pos: position for each token (optional).
|
475 |
+
Returns:
|
476 |
+
output: probability distributions for missing tokens.
|
477 |
+
state_dict: parsing results and raw output
|
478 |
+
"""
|
479 |
+
|
480 |
+
batch_size, length = x.size()
|
481 |
+
|
482 |
+
raw_output, _ = self.encode(x, pos)
|
483 |
+
raw_output = self.norm(raw_output)
|
484 |
+
raw_output = self.drop(raw_output)
|
485 |
+
|
486 |
+
output = self.output_layer(raw_output)
|
487 |
+
return output.view(batch_size * length, -1), {'raw_output': raw_output,}
|
488 |
+
|
489 |
+
class StructFormer(Transformer):
|
490 |
+
"""StructFormer model."""
|
491 |
+
|
492 |
+
def __init__(self,
|
493 |
+
hidden_size,
|
494 |
+
n_context_layers,
|
495 |
+
nlayers,
|
496 |
+
ntokens,
|
497 |
+
nhead=8,
|
498 |
+
dropout=0.1,
|
499 |
+
dropatt=0.1,
|
500 |
+
relative_bias=False,
|
501 |
+
pos_emb=False,
|
502 |
+
pad=0,
|
503 |
+
n_parser_layers=4,
|
504 |
+
conv_size=9,
|
505 |
+
relations=('head', 'child'),
|
506 |
+
weight_act='softmax'):
|
507 |
+
"""Initialization.
|
508 |
+
|
509 |
+
Args:
|
510 |
+
hidden_size: dimension of inputs and hidden states
|
511 |
+
nlayers: number of layers
|
512 |
+
ntokens: number of output categories
|
513 |
+
nhead: number of self-attention heads
|
514 |
+
dropout: dropout rate
|
515 |
+
dropatt: drop attention rate
|
516 |
+
relative_bias: bool, indicate whether use a relative position based
|
517 |
+
attention bias
|
518 |
+
pos_emb: bool, indicate whether use a learnable positional embedding
|
519 |
+
pad: pad token index
|
520 |
+
n_parser_layers: number of parsing layers
|
521 |
+
conv_size: convolution kernel size for parser
|
522 |
+
relations: relations that are used to compute self attention
|
523 |
+
weight_act: relations distribution activation function
|
524 |
+
"""
|
525 |
+
|
526 |
+
super(StructFormer, self).__init__(
|
527 |
+
hidden_size,
|
528 |
+
nlayers,
|
529 |
+
ntokens,
|
530 |
+
nhead=nhead,
|
531 |
+
dropout=dropout,
|
532 |
+
dropatt=dropatt,
|
533 |
+
relative_bias=relative_bias,
|
534 |
+
pos_emb=pos_emb,
|
535 |
+
pad=pad)
|
536 |
+
|
537 |
+
|
538 |
+
def encode(self, x, pos):
|
539 |
+
h = self.emb(x)
|
540 |
+
if hasattr(self, 'pos_emb'):
|
541 |
+
h = h + self.pos_emb(pos)
|
542 |
+
h_list = []
|
543 |
+
visibility = self.visibility(x, x.device)
|
544 |
+
|
545 |
+
for i in range(self.nlayers):
|
546 |
+
h_list.append(h)
|
547 |
+
h = self.layers[i](
|
548 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
549 |
+
|
550 |
+
output = h
|
551 |
+
h_array = torch.stack(h_list, dim=2)
|
552 |
+
|
553 |
+
return output
|
554 |
+
|
555 |
+
|
556 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
557 |
+
|
558 |
+
x = input_ids
|
559 |
+
batch_size, length = x.size()
|
560 |
+
|
561 |
+
if position_ids is None:
|
562 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
563 |
+
|
564 |
+
raw_output = self.encode(x, pos)
|
565 |
+
raw_output = self.norm(raw_output)
|
566 |
+
raw_output = self.drop(raw_output)
|
567 |
+
|
568 |
+
output = self.output_layer(raw_output)
|
569 |
+
|
570 |
+
loss = None
|
571 |
+
if labels is not None:
|
572 |
+
loss_fct = nn.CrossEntropyLoss()
|
573 |
+
loss = loss_fct(output.view(batch_size * length, -1), labels.reshape(-1))
|
574 |
+
|
575 |
+
return MaskedLMOutput(
|
576 |
+
loss=loss, # shape: 1
|
577 |
+
logits=output, # shape: (batch_size * length, ntokens)
|
578 |
+
hidden_states=None,
|
579 |
+
attentions=None,
|
580 |
+
)
|
581 |
+
|
582 |
+
##########################################
|
583 |
+
# HuggingFace Model
|
584 |
+
##########################################
|
585 |
+
class StructformerModel(PreTrainedModel):
|
586 |
+
config_class = StructformerConfig
|
587 |
+
|
588 |
+
def __init__(self, config):
|
589 |
+
super().__init__(config)
|
590 |
+
self.model = StructFormer(
|
591 |
+
hidden_size=config.hidden_size,
|
592 |
+
n_context_layers=config.n_context_layers,
|
593 |
+
nlayers=config.nlayers,
|
594 |
+
ntokens=config.ntokens,
|
595 |
+
nhead=config.nhead,
|
596 |
+
dropout=config.dropout,
|
597 |
+
dropatt=config.dropatt,
|
598 |
+
relative_bias=config.relative_bias,
|
599 |
+
pos_emb=config.pos_emb,
|
600 |
+
pad=config.pad,
|
601 |
+
n_parser_layers=config.n_parser_layers,
|
602 |
+
conv_size=config.conv_size,
|
603 |
+
relations=config.relations,
|
604 |
+
weight_act=config.weight_act
|
605 |
+
)
|
606 |
+
|
607 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
608 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
609 |
+
|
610 |
+
|
611 |
+
class StructFormerClassification(Transformer):
|
612 |
+
"""StructFormer model."""
|
613 |
+
|
614 |
+
def __init__(self,
|
615 |
+
hidden_size,
|
616 |
+
n_context_layers,
|
617 |
+
nlayers,
|
618 |
+
ntokens,
|
619 |
+
nhead=8,
|
620 |
+
dropout=0.1,
|
621 |
+
dropatt=0.1,
|
622 |
+
relative_bias=False,
|
623 |
+
pos_emb=False,
|
624 |
+
pad=0,
|
625 |
+
n_parser_layers=4,
|
626 |
+
conv_size=9,
|
627 |
+
relations=('head', 'child'),
|
628 |
+
weight_act='softmax',
|
629 |
+
config=None,
|
630 |
+
):
|
631 |
+
|
632 |
+
|
633 |
+
super(StructFormerClassification, self).__init__(
|
634 |
+
hidden_size,
|
635 |
+
nlayers,
|
636 |
+
ntokens,
|
637 |
+
nhead=nhead,
|
638 |
+
dropout=dropout,
|
639 |
+
dropatt=dropatt,
|
640 |
+
relative_bias=relative_bias,
|
641 |
+
pos_emb=pos_emb,
|
642 |
+
pad=pad)
|
643 |
+
|
644 |
+
self.num_labels = config.num_labels
|
645 |
+
self.config = config
|
646 |
+
|
647 |
+
self.classifier = RobertaClassificationHead(config)
|
648 |
+
|
649 |
+
def encode(self, x, pos):
|
650 |
+
h = self.emb(x)
|
651 |
+
if hasattr(self, 'pos_emb'):
|
652 |
+
h = h + self.pos_emb(pos)
|
653 |
+
h_list = []
|
654 |
+
visibility = self.visibility(x, x.device)
|
655 |
+
|
656 |
+
for i in range(self.nlayers):
|
657 |
+
h_list.append(h)
|
658 |
+
h = self.layers[i](
|
659 |
+
h.transpose(0, 1), key_padding_mask=visibility).transpose(0, 1)
|
660 |
+
|
661 |
+
output = h
|
662 |
+
h_array = torch.stack(h_list, dim=2)
|
663 |
+
|
664 |
+
return output
|
665 |
+
|
666 |
+
|
667 |
+
def forward(self, input_ids, labels=None, position_ids=None, **kwargs):
|
668 |
+
|
669 |
+
x = input_ids
|
670 |
+
batch_size, length = x.size()
|
671 |
+
|
672 |
+
if position_ids is None:
|
673 |
+
pos = torch.arange(length, device=x.device).expand(batch_size, length)
|
674 |
+
|
675 |
+
raw_output = self.encode(x, pos)
|
676 |
+
raw_output = self.norm(raw_output)
|
677 |
+
raw_output = self.drop(raw_output)
|
678 |
+
|
679 |
+
#output = self.output_layer(raw_output)
|
680 |
+
logits = self.classifier(raw_output)
|
681 |
+
|
682 |
+
loss = None
|
683 |
+
if labels is not None:
|
684 |
+
if self.config.problem_type is None:
|
685 |
+
if self.num_labels == 1:
|
686 |
+
self.config.problem_type = "regression"
|
687 |
+
elif self.num_labels > 1 and (labels.dtype == torch.long or labels.dtype == torch.int):
|
688 |
+
self.config.problem_type = "single_label_classification"
|
689 |
+
else:
|
690 |
+
self.config.problem_type = "multi_label_classification"
|
691 |
+
|
692 |
+
if self.config.problem_type == "regression":
|
693 |
+
loss_fct = MSELoss()
|
694 |
+
if self.num_labels == 1:
|
695 |
+
loss = loss_fct(logits.squeeze(), labels.squeeze())
|
696 |
+
else:
|
697 |
+
loss = loss_fct(logits, labels)
|
698 |
+
elif self.config.problem_type == "single_label_classification":
|
699 |
+
loss_fct = CrossEntropyLoss()
|
700 |
+
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
|
701 |
+
elif self.config.problem_type == "multi_label_classification":
|
702 |
+
loss_fct = BCEWithLogitsLoss()
|
703 |
+
loss = loss_fct(logits, labels)
|
704 |
+
|
705 |
+
|
706 |
+
return SequenceClassifierOutput(
|
707 |
+
loss=loss,
|
708 |
+
logits=logits,
|
709 |
+
hidden_states=None,
|
710 |
+
attentions=None,
|
711 |
+
)
|
712 |
+
|
713 |
+
|
714 |
+
class StructformerModelForSequenceClassification(PreTrainedModel):
|
715 |
+
config_class = StructformerConfig
|
716 |
+
def __init__(self, config):
|
717 |
+
super().__init__(config)
|
718 |
+
self.model = StructFormerClassification(
|
719 |
+
hidden_size=config.hidden_size,
|
720 |
+
n_context_layers=config.n_context_layers,
|
721 |
+
nlayers=config.nlayers,
|
722 |
+
ntokens=config.ntokens,
|
723 |
+
nhead=config.nhead,
|
724 |
+
dropout=config.dropout,
|
725 |
+
dropatt=config.dropatt,
|
726 |
+
relative_bias=config.relative_bias,
|
727 |
+
pos_emb=config.pos_emb,
|
728 |
+
pad=config.pad,
|
729 |
+
n_parser_layers=config.n_parser_layers,
|
730 |
+
conv_size=config.conv_size,
|
731 |
+
relations=config.relations,
|
732 |
+
weight_act=config.weight_act,
|
733 |
+
config=config)
|
734 |
+
|
735 |
+
def _init_weights(self, module):
|
736 |
+
"""Initialize the weights"""
|
737 |
+
if isinstance(module, nn.Linear):
|
738 |
+
# Slightly different from the TF version which uses truncated_normal for initialization
|
739 |
+
# cf https://github.com/pytorch/pytorch/pull/5617
|
740 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
741 |
+
if module.bias is not None:
|
742 |
+
module.bias.data.zero_()
|
743 |
+
elif isinstance(module, nn.Embedding):
|
744 |
+
module.weight.data.normal_(mean=0.0, std=self.config.initializer_range)
|
745 |
+
if module.padding_idx is not None:
|
746 |
+
module.weight.data[module.padding_idx].zero_()
|
747 |
+
elif isinstance(module, nn.LayerNorm):
|
748 |
+
if module.bias is not None:
|
749 |
+
module.bias.data.zero_()
|
750 |
+
module.weight.data.fill_(1.0)
|
751 |
+
|
752 |
+
|
753 |
+
def forward(self, input_ids, labels=None, **kwargs):
|
754 |
+
return self.model(input_ids, labels=labels, **kwargs)
|
finetune/control_raising_control/checkpoint-400/tokenizer_config.json
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"add_prefix_space": false,
|
3 |
+
"bos_token": {
|
4 |
+
"__type": "AddedToken",
|
5 |
+
"content": "<s>",
|
6 |
+
"lstrip": false,
|
7 |
+
"normalized": true,
|
8 |
+
"rstrip": false,
|
9 |
+
"single_word": false
|
10 |
+
},
|
11 |
+
"cls_token": {
|
12 |
+
"__type": "AddedToken",
|
13 |
+
"content": "<s>",
|
14 |
+
"lstrip": false,
|
15 |
+
"normalized": true,
|
16 |
+
"rstrip": false,
|
17 |
+
"single_word": false
|
18 |
+
},
|
19 |
+
"eos_token": {
|
20 |
+
"__type": "AddedToken",
|
21 |
+
"content": "</s>",
|
22 |
+
"lstrip": false,
|
23 |
+
"normalized": true,
|
24 |
+
"rstrip": false,
|
25 |
+
"single_word": false
|
26 |
+
},
|
27 |
+
"errors": "replace",
|
28 |
+
"mask_token": {
|
29 |
+
"__type": "AddedToken",
|
30 |
+
"content": "<mask>",
|
31 |
+
"lstrip": true,
|
32 |
+
"normalized": false,
|
33 |
+
"rstrip": false,
|
34 |
+
"single_word": false
|
35 |
+
},
|
36 |
+
"model_max_length": 512,
|
37 |
+
"name_or_path": "final_models/transformer_base_final_2",
|
38 |
+
"pad_token": {
|
39 |
+
"__type": "AddedToken",
|
40 |
+
"content": "<pad>",
|
41 |
+
"lstrip": false,
|
42 |
+
"normalized": true,
|
43 |
+
"rstrip": false,
|
44 |
+
"single_word": false
|
45 |
+
},
|
46 |
+
"sep_token": {
|
47 |
+
"__type": "AddedToken",
|
48 |
+
"content": "</s>",
|
49 |
+
"lstrip": false,
|
50 |
+
"normalized": true,
|
51 |
+
"rstrip": false,
|
52 |
+
"single_word": false
|
53 |
+
},
|
54 |
+
"special_tokens_map_file": null,
|
55 |
+
"tokenizer_class": "RobertaTokenizer",
|
56 |
+
"trim_offsets": true,
|
57 |
+
"unk_token": {
|
58 |
+
"__type": "AddedToken",
|
59 |
+
"content": "<unk>",
|
60 |
+
"lstrip": false,
|
61 |
+
"normalized": true,
|
62 |
+
"rstrip": false,
|
63 |
+
"single_word": false
|
64 |
+
}
|
65 |
+
}
|
finetune/control_raising_control/checkpoint-400/trainer_state.json
ADDED
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"best_metric": 0.8797792137856758,
|
3 |
+
"best_model_checkpoint": "final_models/transformer_base_final_2/finetune/control_raising_control/checkpoint-400",
|
4 |
+
"epoch": 7.2727272727272725,
|
5 |
+
"global_step": 400,
|
6 |
+
"is_hyper_param_search": false,
|
7 |
+
"is_local_process_zero": true,
|
8 |
+
"is_world_process_zero": true,
|
9 |
+
"log_history": [
|
10 |
+
{
|
11 |
+
"epoch": 7.27,
|
12 |
+
"eval_accuracy": 0.8665371537208557,
|
13 |
+
"eval_f1": 0.8797792137856758,
|
14 |
+
"eval_loss": 0.8952956199645996,
|
15 |
+
"eval_mcc": 0.7560620169097876,
|
16 |
+
"eval_runtime": 18.3898,
|
17 |
+
"eval_samples_per_second": 727.687,
|
18 |
+
"eval_steps_per_second": 90.974,
|
19 |
+
"step": 400
|
20 |
+
}
|
21 |
+
],
|
22 |
+
"max_steps": 550,
|
23 |
+
"num_train_epochs": 10,
|
24 |
+
"total_flos": 3144693820953600.0,
|
25 |
+
"trial_name": null,
|
26 |
+
"trial_params": null
|
27 |
+
}
|