-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProntoVF.py
144 lines (111 loc) · 5.51 KB
/
ProntoVF.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
import torch
from transformers import AutoTokenizer, AutoModelForMaskedLM
import os
device = "cuda" if torch.cuda.is_available() else "cpu"
device = "mps" if torch.backends.mps.is_available() else device
class ProntoVF(torch.nn.Module):
def __init__(self,
template="[1] [MASK] [2].",
special_tokens=None, # ("[R1]", "[R2]")
pretrained_model="FacebookAI/roberta-large",
inner_dim=256,
*args, **kwargs):
super().__init__()
self.tokenizer = AutoTokenizer.from_pretrained(pretrained_model)
self.model = AutoModelForMaskedLM.from_pretrained(pretrained_model).to(device)
self.template = template.replace("[MASK]", self.tokenizer.mask_token)
self.pretrained_model = pretrained_model
if "roberta" in self.pretrained_model.lower():
self.plm_num_embeddings = self.model.roberta.embeddings.word_embeddings.num_embeddings
self.plm_embedding_dim = self.model.roberta.embeddings.word_embeddings.embedding_dim
else:
self.plm_num_embeddings = self.model.bert.embeddings.word_embeddings.num_embeddings
self.plm_embedding_dim = self.model.bert.embeddings.word_embeddings.embedding_dim
for param in self.model.parameters():
param.requires_grad = False
self.linear = torch.nn.Linear(self.plm_embedding_dim, 1)
self.special_tokens = None
self.inner_dim = inner_dim
self.vocab_extractor = torch.nn.Linear(self.plm_embedding_dim, self.inner_dim)
self.dropout = torch.nn.Dropout(0.1)
self.out_proj = torch.nn.Linear(self.inner_dim, 1)
# SPECIAL TOKENS
if special_tokens:
self.special_tokens = special_tokens
self.tokenizer.add_tokens(self.special_tokens, special_tokens=True)
self.special_tokens_ids = self.tokenizer.convert_tokens_to_ids(self.special_tokens) # converts tokens to tokenizer ids
self.soft_prompts = torch.nn.Parameter(torch.nn.init.xavier_uniform_(
torch.empty(len(self.special_tokens), self.plm_embedding_dim)), requires_grad=True)
def get_worst_tokens(self):
pass
def get_top_tokens(self):
pass
def generate_word_cloud(self, filename, top_tokens=50):
pass
def forward(self, x, prefixes=None, training=False):
# x: np.array, pairs of (child, parent) (str)
sentences = list(map(lambda pair: self.template.replace("[1]", pair[0]).replace("[2]", pair[1]), x))
# print(sentences)
if prefixes is not None:
assert len(prefixes) == len(sentences)
sentences = list(map(lambda x: x[1] + sentences[x[0]], enumerate(prefixes)))
#print(sentences[0])
tok = self.tokenizer(
sentences,
add_special_tokens=True,
padding=True,
return_tensors="pt",
)["input_ids"].to(device)
special_token_ids = []
attention_mask = 1 - tok.eq(self.tokenizer.pad_token_id).int()
if self.special_tokens:
for i, t in enumerate(self.special_tokens):
special_token_ids.append(torch.nonzero(
tok == self.special_tokens_ids[i], as_tuple=True
))
tok[special_token_ids[i]] = self.tokenizer.pad_token_id
# tok[special_token_index_2] = self.tokenizer.pad_token_id
if "roberta" in self.pretrained_model.lower():
inputs_embeds = self.model.roberta.embeddings.word_embeddings(tok.int())
else:
inputs_embeds = self.model.bert.embeddings.word_embeddings(tok.int())
# reparametrization trick on special tokens (soft prompts)
if self.special_tokens:
for i, t in enumerate(self.special_tokens):
inputs_embeds[special_token_ids[i]] = inputs_embeds[special_token_ids[i]] * 0 + self.soft_prompts[i]
# inputs_embeds[special_token_index_2] = inputs_embeds[special_token_index_2] * 0 + self.soft_prompts[1]
out = self.model(
input_ids=None,
attention_mask=attention_mask,
token_type_ids=None,
inputs_embeds=inputs_embeds,
output_hidden_states=True,
)
hidden_states = out.hidden_states[-1]
mask_token_index = torch.nonzero(
tok == self.tokenizer.mask_token_id, as_tuple=True
)
mask_hidden = hidden_states[mask_token_index]
x = self.dropout(mask_hidden)
x = self.vocab_extractor(x)
x = torch.tanh(x)
x = self.dropout(x)
x = self.out_proj(x)
out = torch.sigmoid(x).squeeze()
return out
def reg(self):
return 0
def save(self, filepath):
torch.save(self.out_proj, os.path.join(filepath, "W_c.pt"))
torch.save(self.vocab_extractor, os.path.join(filepath, "W_ve.pt"))
if self.special_tokens:
torch.save(self.soft_prompts, os.path.join(filepath, "soft_prompts.pt"))
#torch.save(self.filter, os.path.join(filepath, "filter.pt"))
@classmethod
def load(cls, filepath, *args, **kwargs):
model = cls(*args, **kwargs)
model.vocab_extractor = torch.load(os.path.join(filepath, "W_ve.pt"), map_location=device)
model.out_proj = torch.load(os.path.join(filepath, "W_c.pt"), map_location=device)
if os.path.exists(os.path.join(filepath, "soft_prompts.pt")):
model.soft_prompts = torch.load(os.path.join(filepath, "soft_prompts.pt"), map_location=device)
return model