-
Notifications
You must be signed in to change notification settings - Fork 3
/
finetuning.py
360 lines (279 loc) · 13.9 KB
/
finetuning.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
import numpy as np
from transformers import AutoModelForCausalLM, AutoTokenizer
from transformers import pipeline
import pandas as pd
import sys
import json
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
from torch.utils.data._utils.collate import default_collate
#from data import TranslationDataset
from transformers import BertTokenizerFast, BertTokenizer
from transformers import BertModel, BertForMaskedLM, BertConfig, EncoderDecoderModel, BertLMHeadModel, AutoModelForSequenceClassification
from sklearn.metrics import roc_auc_score
import sys
import torch
import torch.utils.data as data
from torch.nn.utils.rnn import pad_sequence
import os
from transformers.models.bert.modeling_bert import BertPreTrainedModel, BertOnlyMLMHead, SequenceClassifierOutput
from torch.nn import MSELoss, CrossEntropyLoss, BCEWithLogitsLoss
from typing import List, Optional, Tuple, Union
from transformers.modeling_outputs import ModelOutput
from transformers import PretrainedConfig
from transformers.modeling_outputs import BaseModelOutput, Seq2SeqLMOutput
from transformers.modeling_utils import PreTrainedModel
from transformers.utils import add_start_docstrings, add_start_docstrings_to_model_forward, logging, replace_return_docstrings
from transformers.models.encoder_decoder.configuration_encoder_decoder import EncoderDecoderConfig
import warnings
from torch.profiler import profile, record_function, ProfilerActivity
import wandb
import argparse
from src.multiTrans import TulipPetal, TCRDataset, BertLastPooler, unsupervised_auc, train_unsupervised, eval_unsupervised, MyMasking, Tulip, get_auc_mi
from wandb_osh.hooks import TriggerWandbSyncHook
wandb.login()
torch.manual_seed(0)
def main():
trigger_sync = TriggerWandbSyncHook()
parser = argparse.ArgumentParser()
# Required parameters
parser.add_argument(
"--train_dir",
default=None,
type=str,
required=True,
help="The train data dir. Should contain the .csv files (or other data files) for the task.",
)
parser.add_argument(
"--test_dir",
default=None,
type=str,
required=True,
help="The test data dir. Should contain the .fasta files (or other data files) for the task.",
)
parser.add_argument(
"--modelconfig",
type=str,
help="path to json including the config of the model" ,
)
parser.add_argument(
"--load",
default=None,
type=str,
help="path to the model pretrained to load" ,
)
parser.add_argument(
"--save",
default=None,
type=str,
help="path to save the model" ,
)
parser.add_argument(
"--batch_size",
default=512,
type=int,
help="batch_size" ,
)
parser.add_argument(
"--masking_proba",
default=0.0,
type=float,
help="masking_proba" ,
)
parser.add_argument(
"--num_epochs",
default=300,
type=int,
help="numbers of epochs" ,
)
parser.add_argument(
"--weight_decay",
default=0.0,
type=float,
help="weight decay" ,
)
parser.add_argument(
"--lr",
default=0.0001,
type=float,
help="learning rate" ,
)
parser.add_argument("--freeze", action="store_true", help="Whether to run training.")
parser.add_argument("--skipMiss", action="store_true", help="Whether to run training.")
args = parser.parse_args()
with open(args.modelconfig, "r") as read_file:
print("loading hyperparameter")
modelconfig = json.load(read_file)
wandb.login()
torch.manual_seed(0)
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
print("Using device:", device)
test_path = args.test_dir
train_path = args.train_dir
tokenizer = AutoTokenizer.from_pretrained("aatok/")
if tokenizer.pad_token is None:
tokenizer.add_special_tokens({'pad_token': '<PAD>'})
if tokenizer.sep_token is None:
tokenizer.add_special_tokens({'sep_token': '<MIS>'})
if tokenizer.cls_token is None:
tokenizer.add_special_tokens({'cls_token': '<CLS>'})
if tokenizer.eos_token is None:
tokenizer.add_special_tokens({'eos_token': '<EOS>'})
if tokenizer.mask_token is None:
tokenizer.add_special_tokens({'mask_token': '<MASK>'})
from tokenizers.processors import TemplateProcessing
tokenizer._tokenizer.post_processor = TemplateProcessing(
single="<CLS> $A <EOS>",
pair="<CLS> $A <MIS> $B:1 <EOS>:1",
special_tokens=[
("<EOS>", 2),
("<CLS>", 3),
("<MIS>", 4),
],
)
mhctok = AutoTokenizer.from_pretrained("mhctok/")
vocabsize = len(tokenizer._tokenizer.get_vocab())
mhcvocabsize = len(mhctok._tokenizer.get_vocab())
print("Loading models ..")
max_length = 50
encoder_config_pep = BertConfig(vocab_size = vocabsize,
max_position_embeddings = max_length, # this shuold be some large value
num_attention_heads = modelconfig["num_attn_heads_encoder_pep"],
num_hidden_layers = modelconfig["num_hidden_layers_encoder_pep"],
hidden_size = modelconfig["hidden_size"],
type_vocab_size = 1,
pad_token_id = tokenizer.pad_token_id)
encoder_config_cdr = BertConfig(vocab_size = vocabsize,
max_position_embeddings = max_length, # this shuold be some large value
num_attention_heads = modelconfig["num_attn_heads_encoder_cdr"],
num_hidden_layers = modelconfig["num_hidden_layers_encoder_cdr"],
hidden_size = modelconfig["hidden_size"],
type_vocab_size = 1,
pad_token_id = tokenizer.pad_token_id)
encoder_config_pep.mhc_vocab_size = mhcvocabsize
encoder_config_cdr.mhc_vocab_size = mhcvocabsize
encoderA = BertModel(config=encoder_config_cdr)
encoderB = BertModel(config=encoder_config_cdr)
encoderE = BertModel(config=encoder_config_pep)
max_length = 50
decoder_config_pep = BertConfig(vocab_size = vocabsize,
max_position_embeddings = max_length, # this shuold be some large value
num_attention_heads = modelconfig["num_attn_heads_decoder_pep"],
num_hidden_layers = modelconfig["num_hidden_layers_decoder_pep"],
hidden_size = modelconfig["hidden_size"],
type_vocab_size = 1,
is_decoder=True,
pad_token_id = tokenizer.pad_token_id) # Very Important
decoder_config_cdr = BertConfig(vocab_size = vocabsize,
max_position_embeddings = max_length, # this shuold be some large value
num_attention_heads = modelconfig["num_attn_heads_decoder_cdr"],
num_hidden_layers = modelconfig["num_hidden_layers_decoder_cdr"],
hidden_size = modelconfig["hidden_size"],
type_vocab_size = 1,
is_decoder=True,
pad_token_id = tokenizer.pad_token_id) # Very Important
decoder_config_cdr.add_cross_attention=True
decoder_config_pep.add_cross_attention=True
decoderA = TulipPetal(config=decoder_config_cdr) #BertForMaskedLM
decoderA.pooler = BertLastPooler(config=decoder_config_cdr)
decoderB = TulipPetal(config=decoder_config_cdr) #BertForMaskedLM
decoderB.pooler = BertLastPooler(config=decoder_config_cdr)
decoderE = TulipPetal(config=decoder_config_pep) #BertForMaskedLM
decoderE.pooler = BertLastPooler(config=decoder_config_pep)
# Define encoder decoder model
model = Tulip(encoderA=encoderA,encoderB=encoderB,encoderE=encoderE, decoderA=decoderA, decoderB=decoderB, decoderE=decoderE)
if args.skipMiss:
model.skipMiss=True
else:
model.skipMiss=False
def count_parameters(mdl):
return sum(p.numel() for p in mdl.parameters() if p.requires_grad)
print(f'The model has {count_parameters(model):,} trainable parameters')
for p in model.parameters():
if p.dim() > 1:
nn.init.xavier_normal_(p)
if args.load:
checkpoint = torch.load(args.load+"/pytorch_model.bin")
model.load_state_dict(checkpoint)
print("loaded")
model.to(device)
if args.freeze:
for param in model.mhc_embeddings.parameters(): #params have requires_grad=True by default
param.requires_grad = False
for param in model.encoderA.parameters(): #params have requires_grad=True by default
param.requires_grad = False
for param in model.encoderB.parameters(): #params have requires_grad=True by default
param.requires_grad = False
for param in model.encoderE.parameters(): #params have requires_grad=True by default
param.requires_grad = False
optimizer = optim.AdamW(model.parameters(), lr=args.lr, weight_decay=args.weight_decay)
print("pti",tokenizer.pad_token_id)
criterion = nn.NLLLoss(ignore_index=tokenizer.pad_token_id, reduction='sum')
datasetTrainFull = TCRDataset(train_path, tokenizer, device, mhctok=mhctok)
datasetTrainFull.set_chain_masking_proba(proba=args.masking_proba)
train_dataloaderFull = torch.utils.data.DataLoader(dataset=datasetTrainFull, batch_size=args.batch_size, shuffle=True, collate_fn=datasetTrainFull.all2allmhc_collate_function)
datasetValidFinal = TCRDataset(test_path, tokenizer, device, mhctok=mhctok)
valid_dataloaderFinal = torch.utils.data.DataLoader(dataset=datasetValidFinal, batch_size=args.batch_size, shuffle=True, collate_fn=datasetValidFinal.all2allmhc_collate_function)
datasetValidFinal_true = TCRDataset(test_path, tokenizer, device,target_binder=True, mhctok=mhctok)
valid_dataloaderFinal_true = torch.utils.data.DataLoader(dataset=datasetValidFinal_true, batch_size=args.batch_size, shuffle=True, collate_fn=datasetValidFinal.all2allmhc_collate_function)
masker = MyMasking(tokenizer, mlm_probability = 0.15)
config_dict = {
'batch_size': args.batch_size,
'dropout': 0.1,
'lr': args.lr,
'weight_decay': args.weight_decay,
'skipMiss': args.skipMiss,
'masking_proba':args.masking_proba,
'starting_point': args.load,
'freeze':args.freeze,
}
config_dict.update(modelconfig)
wandb.init(project="finetune_reviews", entity="barthelemymp", config=config_dict)
trigger_sync()
wandb.config.update(config_dict)
trigger_sync()
target_peptidesFinal = pd.read_csv(test_path)["peptide"].unique()
target_peptidesFinal = pd.read_csv(test_path)["peptide"].value_counts().index
target_peptidesFinal_top = pd.read_csv(test_path)["peptide"].value_counts().index[:20]
for epoch in range(0, args.num_epochs+1):
if epoch%20==0:
aucelist = []
aucalist = []
aucblist = []
aucmialist = []
aucmiblist = []
for target_peptide in target_peptidesFinal:
datasetPetideSpecific= TCRDataset(test_path, tokenizer, device, target_peptide=target_peptide, mhctok=mhctok)
dataloaderPetideSpecific = torch.utils.data.DataLoader(dataset=datasetPetideSpecific, batch_size=1, shuffle=True, collate_fn=datasetValidFinal.all2allmhc_collate_function)
print(target_peptide)
sys.stdout.flush()
auca, aucb, auce = unsupervised_auc(model, dataloaderPetideSpecific, tokenizer.pad_token_id)
aucami, aucbmi = get_auc_mi(model, datasetPetideSpecific, mask_mhc=True, mask_peptide=True, mask_paired=False)
wandb.log({target_peptide+"_a":auca, target_peptide+"_b":aucb,target_peptide+"_mia":aucami, target_peptide+"_mib":aucbmi,target_peptide+"_e":auce, "epochT":epoch})
aucelist.append(auce)
aucalist.append(auca)
aucblist.append(aucb)
aucmialist.append(aucami)
aucmiblist.append(aucbmi)
wandb.log({"avg_e":np.mean(aucelist), "avg_a":np.mean(aucalist),"avg_b":np.mean(aucblist), "avg_mia":np.mean(aucmialist),"avg_mib":np.mean(aucmiblist),"epochT":epoch})
trigger_sync()
print("Starting epoch", epoch+1, file=sys.stdout)
sys.stdout.flush()
epoch_lm_lossA, epoch_lm_lossB, epoch_lm_lossE, epoch_mlm_lossA, epoch_mlm_lossB, epoch_mlm_lossE = train_unsupervised(model, optimizer, masker, train_dataloaderFull, criterion)
print(epoch_lm_lossA, epoch_lm_lossB, epoch_lm_lossE, epoch_mlm_lossA, epoch_mlm_lossB, epoch_mlm_lossE,file=sys.stdout)
wandb.log({"epoch_lm_lossAu": epoch_lm_lossA, "epoch_lm_lossBu":epoch_lm_lossB ,"epoch_lm_lossEu":epoch_lm_lossE ,"epoch_mlm_lossAu":epoch_mlm_lossA ,"epoch_mlm_lossBu":epoch_mlm_lossB ,"epoch_mlm_lossEu":epoch_mlm_lossE, "epochT":epoch})
trigger_sync()
if epoch%20==0:
with torch.no_grad():
epoch_lm_lossA, epoch_lm_lossB, epoch_lm_lossE, epoch_mlm_lossA, epoch_mlm_lossB, epoch_mlm_lossE = eval_unsupervised(model, masker, valid_dataloaderFinal_true, criterion)
print(epoch_lm_lossA, epoch_lm_lossB, epoch_lm_lossE, epoch_mlm_lossA, epoch_mlm_lossB, epoch_mlm_lossE,file=sys.stdout)
sys.stdout.flush()
wandb.log({"epoch_lm_lossAu_val": epoch_lm_lossA, "epoch_lm_lossBu_val":epoch_lm_lossB ,"epoch_lm_lossEu_val":epoch_lm_lossE ,"epoch_mlm_lossAu_val":epoch_mlm_lossA ,"epoch_mlm_lossBu_val":epoch_mlm_lossB ,"epoch_mlm_lossEu_val":epoch_mlm_lossE, "epochT":epoch})
if epoch%10==0:
if args.save:
print('saving model at ', args.save + str(epoch))
model.save_pretrained(args.save + str(epoch))
if __name__ == "__main__":
main()