-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
180 lines (159 loc) · 6.74 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import torch.nn as nn
# from dataloader.default_split import DEFAULT_SPLIT
import json
import os
import torch
def label_smoothed_nll_loss(lprobs, target, epsilon=0.1, ignore_index=-100):
"""From fairseq"""
if target.dim() == lprobs.dim() - 1:
target = target.unsqueeze(-1)
nll_loss = -lprobs.gather(dim=-1, index=target)
smooth_loss = -lprobs.sum(dim=-1, keepdim=True)
if ignore_index is not None:
pad_mask = target.eq(ignore_index)
nll_loss.masked_fill_(pad_mask, 0.0)
smooth_loss.masked_fill_(pad_mask, 0.0)
else:
nll_loss = nll_loss.squeeze(-1)
smooth_loss = smooth_loss.squeeze(-1)
nll_loss = nll_loss.sum()
smooth_loss = smooth_loss.sum()
eps_i = epsilon / lprobs.size(-1)
loss = (1.0 - epsilon) * nll_loss + eps_i * smooth_loss
return loss, nll_loss
def freeze_params(model: nn.Module):
"""Set requires_grad=False for each of model.parameters()"""
for n, par in model.named_parameters():
if 'prompt' in n:
continue
par.requires_grad = False
def freeze_prompt_blend_embeds(args, model):
"""Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
freeze_params(model.model)
# freeze_params(model.all_prompt_parameters)
if not args.do_tune_bert:
freeze_params(model.blender.word_encoder)
def freeze_blend_intrinsic_embeds(args, model):
"""Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
freeze_params(model.model)
# freeze_params(model.all_prompt_parameters)
if not args.do_tune_bert:
freeze_params(model.blender.word_encoder)
for n, par in model.named_parameters():
if 'prompt_W' in n and 'mapping' not in n:
par.requires_grad = False
else:
continue
def freeze_intrinsic_mlp_embeds(args, model):
"""Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
freeze_params(model.model)
# freeze_params(model.all_prompt_parameters)
if not args.do_tune_bert:
freeze_params(model.blender)
for n, par in model.named_parameters():
if 'prompt_W' in n and 'mapping' not in n:
par.requires_grad = False
else:
continue
def freeze_bert_of_blend(model):
freeze_params(model.word_encoder)
# def freeze_embeds(model):
# """Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
# model_type = model.config.model_type
# if model_type == "t5":
# freeze_params(model.shared)
# for d in [model.encoder, model.decoder]:
# freeze_params(d.embed_tokens)
# elif model_type == "fsmt":
# for d in [model.model.encoder, model.model.decoder]:
# freeze_params(d.embed_positions)
# freeze_params(d.embed_tokens)
# else:
# freeze_params(model.model.shared)
# for d in [model.model.encoder, model.model.decoder]:
# freeze_params(d.embed_positions)
# freeze_params(d.embed_tokens)
def freeze_embeds(model, AE_recover=False, AE_recover_stage_two=False):
"""Freeze token embeddings and positional embeddings for bart, just token embeddings for t5."""
if AE_recover:
for n, p in model.named_parameters():
if not AE_recover_stage_two:
if 'prompt_task' not in n:
p.requires_grad = False
else:
if 'prompt_embeddings' not in n:
p.requires_grad = False
else:
freeze_params(model.model)
def trim_batch(
input_ids,
pad_token_id,
attention_mask=None,
):
"""Remove columns that are populated exclusively by pad_token_id"""
keep_column_mask = input_ids.ne(pad_token_id).any(dim=0)
if attention_mask is None:
return input_ids[:, keep_column_mask]
else:
return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask])
def get_tasks_list(filename, split_name):
with open(filename, "r") as fin:
split_dict = json.load(fin)
return sorted(split_dict[split_name])
def load_prompt_parameters(args, prompt_path, train_tasks):
train_tasks_split = sorted(train_tasks.split(" "))
prompt_weight = torch.tensor([])
for task in train_tasks_split:
task_dir = os.path.join(args.data_dir, task)
files = sorted(os.listdir(task_dir))
prefixes = []
for filename in files:
if not filename.endswith(".tsv"):
continue
prefix = "_".join(filename.split("_")[:-1])
if prefix not in prefixes:
prefixes.append(prefix)
# 固定seed100
prefix = prefixes[0]
task_prompt_weight_path = os.path.join(prompt_path, "singletask-"+task, "prompt_weight")
if prefix+"_best.pt" in os.listdir(task_prompt_weight_path):
task_prompt_weight = torch.load(os.path.join(task_prompt_weight_path, prefix+"_best.pt"))
elif prefix+"_lr_1e-05_bsz_2.pt" in os.listdir(task_prompt_weight_path):
task_prompt_weight = torch.load(os.path.join(task_prompt_weight_path, prefix+"_lr_1e-05_bsz_2.pt"))
else:
task_prompt_weight = torch.rand(100, 768)
prompt_weight = torch.cat((prompt_weight, task_prompt_weight.unsqueeze(0)),0)
return prompt_weight
def load_intrinsic_parameters(args, intrinsic_path, train_tasks):
train_tasks_split = sorted(train_tasks.split(" "))
intrinsic_weight = torch.tensor([])
for task in train_tasks_split:
task_dir = os.path.join(args.data_dir, task)
files = sorted(os.listdir(task_dir))
prefixes = []
for filename in files:
if not filename.endswith(".tsv"):
continue
prefix = "_".join(filename.split("_")[:-1])
if prefix not in prefixes:
prefixes.append(prefix)
# 固定seed100
prefix = prefixes[0]
task_intrinsic_weight_path = os.path.join(intrinsic_path, task, '100/1e-05_4/best-ckpt.pt')
if os.path.exists(task_intrinsic_weight_path):
task_intrinsic_weight = torch.load(task_intrinsic_weight_path)['model']['prompt_task.weight']
else:
print('Intrinsic weight of {} doesnot exist', task)
intrinsic_weight = torch.cat((intrinsic_weight, task_intrinsic_weight.unsqueeze(0)),0) # [100,1,3]
return intrinsic_weight
def get_prefixes(task_dir, task):
task_dir = os.path.join(task_dir, task)
files = sorted(os.listdir(task_dir))
prefixes = []
for filename in files:
if not filename.endswith(".tsv"):
continue
prefix = "_".join(filename.split("_")[:-1])
if prefix not in prefixes:
prefixes.append(prefix)
return prefixes