Repository navigation
Expand file tree
/
Copy pathmodel_loader.py
More file actions
210 lines (172 loc) · 6.67 KB
/
Copy pathmodel_loader.py
File metadata and controls
210 lines (172 loc) · 6.67 KB
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
import os
from collections import namedtuple
import torch
import yaml
from CFG import CFGLogits
from constants import (
DEFAULT_IMAGE_PATCH_TOKEN,
IMAGE_TOKEN_INDEX,
IMAGE_TOKEN_LENGTH,
SHIKRA_IMAGE_TOKEN_LENGTH,
SHIKRA_IMG_END_TOKEN,
SHIKRA_IMG_START_TOKEN,
)
from llava.mm_utils import get_model_name_from_path
from llava.model.builder import load_pretrained_model
from mllm.models import load_pretrained
def load_model_args_from_yaml(yaml_path):
with open(yaml_path, "r") as file:
data = yaml.safe_load(file)
ModelArgs = namedtuple("ModelArgs", data["ModelArgs"].keys())
TrainingArgs = namedtuple("TrainingArgs", data["TrainingArgs"].keys())
model_args = ModelArgs(**data["ModelArgs"])
training_args = TrainingArgs(**data["TrainingArgs"])
return model_args, training_args
def load_llava_model(model_path):
model_name = get_model_name_from_path(model_path)
model_base = None
tokenizer, model, image_processor, context_len = load_pretrained_model(
model_path, model_base, model_name
)
return tokenizer, model, image_processor, model
def load_shikra_model(yaml_path):
model_args, training_args = load_model_args_from_yaml(yaml_path)
model, preprocessor = load_pretrained(model_args, training_args)
return (
preprocessor["text"],
model.to("cuda"),
preprocessor["image"],
model.to("cuda"),
)
def prepare_llava_inputs(template, query, image, tokenizer):
image_tensor = image["pixel_values"][0]
qu = [template.replace("<question>", q) for q in query]
batch_size = len(query)
chunks = [q.split("<ImageHere>") for q in qu]
chunk_before = [chunk[0] for chunk in chunks]
chunk_after = [chunk[1] for chunk in chunks]
token_before = (
tokenizer(
chunk_before,
return_tensors="pt",
padding="longest",
add_special_tokens=False,
)
.to("cuda")
.input_ids
)
token_after = (
tokenizer(
chunk_after,
return_tensors="pt",
padding="longest",
add_special_tokens=False,
)
.to("cuda")
.input_ids
)
bos = (
torch.ones([batch_size, 1], dtype=torch.int64, device="cuda")
* tokenizer.bos_token_id
)
img_start_idx = len(token_before[0]) + 1
img_end_idx = img_start_idx + IMAGE_TOKEN_LENGTH
image_token = (
torch.ones([batch_size, 1], dtype=torch.int64, device="cuda")
* IMAGE_TOKEN_INDEX
)
input_ids = torch.cat([bos, token_before, image_token, token_after], dim=1)
kwargs = {}
kwargs["images"] = image_tensor.half()
kwargs["input_ids"] = input_ids
return qu, img_start_idx, img_end_idx, kwargs
def prepare_shikra_inputs(template, query, image, tokenizer):
image_tensor = image["pixel_values"][0]
replace_token = DEFAULT_IMAGE_PATCH_TOKEN * SHIKRA_IMAGE_TOKEN_LENGTH
qu = [template.replace("<question>", q) for q in query]
qu = [p.replace("<ImageHere>", replace_token) for p in qu]
input_tokens = tokenizer(
qu, return_tensors="pt", padding="longest", add_special_tokens=False
).to("cuda")
bs = len(query)
bos = torch.ones([bs, 1], dtype=torch.int64, device="cuda") * tokenizer.bos_token_id
input_ids = torch.cat([bos, input_tokens.input_ids], dim=1)
img_start_idx = torch.where(input_ids == SHIKRA_IMG_START_TOKEN)[1][0].item()
img_end_idx = torch.where(input_ids == SHIKRA_IMG_END_TOKEN)[1][0].item()
kwargs = {}
kwargs["input_ids"] = input_ids
kwargs["images"] = image_tensor.to("cuda")
return qu, img_start_idx, img_end_idx, kwargs
# Example usage:
# prepare_inputs_for_model(args, image, model, tokenizer, kwargs)
class ModelLoader:
def __init__(self, model_name):
self.model_name = model_name
self.tokenizer = None
self.vlm_model = None
self.llm_model = None
self.image_processor = None
self.load_model()
def load_model(self):
if self.model_name == "llava-1.5":
model_path = os.path.expanduser("/root/autodl-tmp/llava-v1.5-7b")
self.tokenizer, self.vlm_model, self.image_processor, self.llm_model = (
load_llava_model(model_path)
)
else:
raise ValueError(f"Unknown model: {self.model}")
def prepare_inputs_for_model(self, template, query, image):
if self.model_name == "llava-1.5":
questions, img_start_idx, img_end_idx, kwargs = prepare_llava_inputs(
template, query, image, self.tokenizer
)
else:
raise ValueError(f"Unknown model: {self.model_name}")
self.img_start_idx = img_start_idx
self.img_end_idx = img_end_idx
return questions, kwargs
def init_cfg_processor(self, questions, gamma=1.1, beam=1, start_layer=0, end_layer=32):
if self.model_name == "llava-1.5":
chunks = [q.split("<ImageHere>") for q in questions]
else:
raise ValueError(f"Unknown model: {self.model_name}")
chunk_before = [chunk[0] for chunk in chunks]
chunk_after = [chunk[1] for chunk in chunks]
token_before = self.tokenizer(
chunk_before,
return_tensors="pt",
padding="longest",
add_special_tokens=False,
).input_ids.to("cuda")
token_after = self.tokenizer(
chunk_after,
return_tensors="pt",
padding="longest",
add_special_tokens=False,
).input_ids.to("cuda")
batch_size = len(questions)
bos = (
torch.ones(
[batch_size, 1], dtype=token_before.dtype, device=token_before.device
)
* self.tokenizer.bos_token_id
)
neg_promt = torch.cat([bos, token_before, token_after], dim=1)
neg_promt = neg_promt.repeat(beam, 1)
logits_processor = CFGLogits(gamma, neg_promt.to("cuda"), self.llm_model, start_layer=start_layer, end_layer=end_layer)
return logits_processor
def decode(self, output_ids):
# get outputs
if self.model_name == "llava-1.5":
# replace image token by pad token
output_ids = output_ids.clone()
output_ids[output_ids == IMAGE_TOKEN_INDEX] = torch.tensor(
0, dtype=output_ids.dtype, device=output_ids.device
)
output_text = self.tokenizer.batch_decode(
output_ids, skip_special_tokens=True
)
output_text = [text.split("ASSISTANT:")[-1].strip() for text in output_text]
else:
raise ValueError(f"Unknown model: {self.model_name}")
return output_text