-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
168 lines (158 loc) · 8.02 KB
/
models.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
import numpy as np
from tqdm import tqdm
import sys
import os
import pdb
from typing import Tuple, Optional, Union
from peft import LoraConfig, get_peft_model,get_peft_config,PeftModelForCausalLM,TaskType,PrefixTuningConfig, PromptEncoderConfig, PromptTuningConfig
import torch
import torch.nn as nn
from torch.nn import functional as nnf
import transformers
from transformers import set_seed, GPT2Config, GPT2Tokenizer, GPT2LMHeadModel
from transformers.models.biogpt import BioGptForCausalLM, BioGptTokenizer, BioGptConfig
from transformers import AutoTokenizer,AutoModelForCausalLM,AutoConfig
from prefix_mappers import MLP, TransformerMapper
class VQAmedModel(nn.Module):
def forward(self, prefix, labels, tokens, mask, q_len, batch_size):
prefix_projections = self.clip_project(prefix).view(-1, self.prefix_length, self.gpt_embedding_size)
if self.gpttype=='microsoft/biogpt':
embedding = self.gpt.transformer.embed_tokens(tokens)
else:
embedding = self.gpt.transformer.wte(tokens)
for b in range(batch_size):
# insert the visual prefix after the question
embedding[b,q_len[b]:q_len[b]+self.prefix_length,:] = prefix_projections[b]
return self.gpt(inputs_embeds=embedding, attention_mask=mask)
def generate(self, prefix, labels, tokens, mask, q_len):
prefix_projections = self.clip_project(prefix.view(1, -1)).view(self.prefix_length, self.gpt_embedding_size)
if self.gpttype=='microsoft/biogpt':
embedding_txt = self.gpt.transformer.embed_tokens(tokens)
else:
embedding_txt = self.gpt.transformer.wte(tokens)
embedding_txt[q_len:q_len+self.prefix_length,:] = prefix_projections
return embedding_txt
def __init__(
self,
prefix_length=2,
clip_length=2,
prefix_size=512,
num_layers=8,
setting="lora",
mapping_type="MLP",
args=None,
):
super(VQAmedModel, self).__init__()
gpttype = args.model_type
self.gpttype = gpttype
self.setting = setting
self.prefix_length = prefix_length
self.gpt = AutoModelForCausalLM.from_pretrained(gpttype,load_in_8bit=True,device_map='auto')
# load the relevant fine-tuning strategy
if setting == "lora":
peft_config = LoraConfig(task_type=TaskType.CAUSAL_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="prefixtuning":
peft_config = PrefixTuningConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="p_tuning":
peft_config = PromptEncoderConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="prompttuning":
peft_config = PromptTuningConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=='frozen':
for param in self.gpt.transformer.parameters():
param.requires_grad = False
self.tokenizer = GPT2Tokenizer.from_pretrained(gpttype)
self.gpt_embedding_size = self.gpt.transformer.wte.weight.shape[1]
if mapping_type == "MLP":
self.clip_project = MLP((
prefix_size,
(self.gpt_embedding_size * prefix_length) // 2,
self.gpt_embedding_size * prefix_length,
self.gpt_embedding_size * prefix_length))
elif mapping_type == "Transformer":
self.clip_project = TransformerMapper(
prefix_size,
self.gpt_embedding_size,
prefix_length,
clip_length,
num_layers)
else:
raise ValueError("select valid mapping type: MLP or Transformer")
# adaptation of VQAmedModel for ablation studies
class VQAmedModel_abl(nn.Module):
def forward(self, prefix, labels, tokens, mask, q_len, batch_size,abl):
embeddings = self.gpt.transformer.wte(tokens)
if abl=="replace_visual":
for b in range(batch_size):
embeddings[b,q_len[b]:q_len[b]+self.prefix_length,:] = self.nv_tokens[b]
elif abl=="remove_question":
prefix_projections = self.clip_project(prefix).view(-1, self.prefix_length, self.gpt_embedding_size)
embeddings[:,q_len[0]:q_len[0]+self.prefix_length,:] = prefix_projections
elif abl=="swap":
prefix_projections = self.clip_project(prefix).view(-1, self.prefix_length, self.gpt_embedding_size)
embeddings[:,q_len[0]:q_len[0]+self.prefix_length,:] = prefix_projections
return self.gpt(inputs_embeds=embeddings, attention_mask=mask)
def generate(self, prefix, labels, tokens, mask, q_len,abl):
prefix_projections = self.clip_project(prefix.view(1, -1)).view(self.prefix_length, self.gpt_embedding_size)
embeddings = self.gpt.transformer.wte(tokens)
if abl=="replace_visual":
embeddings[q_len:q_len+self.prefix_length,:] = self.nv_tokens[0]
elif abl=="remove_question":
prefix_projections = self.clip_project(prefix.view(1, -1)).view(self.prefix_length, self.gpt_embedding_size)
embeddings[q_len:q_len+self.prefix_length,:] = prefix_projections
elif abl=="swap":
prefix_projections = self.clip_project(prefix.view(1, -1)).view(self.prefix_length, self.gpt_embedding_size)
embeddings[q_len:q_len+self.prefix_length,:] = prefix_projections
return embeddings
def __init__(
self,
prefix_length=2,
clip_length=2,
prefix_size=512,
num_layers=8,
setting="frozen",
mapping_type="MLP",
args=None,
):
super(VQAmedModel_abl, self).__init__()
gpttype = "gpt2-xl"
self.model_type = gpttype
self.setting = setting
self.prefix_length = prefix_length
self.gpt = GPT2LMHeadModel.from_pretrained(gpttype,load_in_8bit=True,device_map='auto')
if setting == "lora":
peft_config = LoraConfig(task_type=TaskType.CAUSAL_LM, inference_mode=False, r=8, lora_alpha=32, lora_dropout=0.1)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="prefixtuning":
peft_config = PrefixTuningConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="p_tuning":
peft_config = PromptEncoderConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=="prompttuning":
peft_config = PromptTuningConfig(task_type=TaskType.CAUSAL_LM, num_virtual_tokens=30)
self.gpt = get_peft_model(self.gpt,peft_config)
elif setting=='frozen':
for param in self.gpt.transformer.parameters():
param.requires_grad = False
self.tokenizer = GPT2Tokenizer.from_pretrained(gpttype)
self.gpt_embedding_size = self.gpt.transformer.wte.weight.shape[1]
# for the replace_visual ablation study we replace the visual tokens with learnable parameters
self.nv_tokens = torch.nn.Parameter(torch.randn(args.batch_size,prefix_length,self.gpt_embedding_size),requires_grad=True).cuda()
if mapping_type == "MLP":
self.clip_project = MLP((prefix_size,
(self.gpt_embedding_size * prefix_length) // 2,
self.gpt_embedding_size * prefix_length,
self.gpt_embedding_size * prefix_length))
elif mapping_type == "Transformer":
self.clip_project = TransformerMapper(
prefix_size,
self.gpt_embedding_size,
prefix_length,
clip_length,
num_layers)
else:
raise ValueError("select valid mapping type: MLP or Transformer")