xla modeling
Browse files- modeling_internvl_chat.py +163 -92
modeling_internvl_chat.py
CHANGED
|
@@ -10,8 +10,7 @@ import torch.utils.checkpoint
|
|
| 10 |
import transformers
|
| 11 |
from torch import nn
|
| 12 |
from torch.nn import CrossEntropyLoss
|
| 13 |
-
from transformers import
|
| 14 |
-
LlamaTokenizer)
|
| 15 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 16 |
from transformers.modeling_utils import PreTrainedModel
|
| 17 |
from transformers.utils import ModelOutput, logging
|
|
@@ -21,41 +20,58 @@ from .conversation import get_conv_template
|
|
| 21 |
from .modeling_intern_vit import InternVisionModel, has_flash_attn
|
| 22 |
from .modeling_internlm2 import InternLM2ForCausalLM
|
| 23 |
|
|
|
|
|
|
|
| 24 |
logger = logging.get_logger(__name__)
|
| 25 |
|
| 26 |
|
| 27 |
-
def version_cmp(v1, v2, op=
|
| 28 |
import operator
|
| 29 |
|
| 30 |
from packaging import version
|
|
|
|
| 31 |
op_func = getattr(operator, op)
|
| 32 |
return op_func(version.parse(v1), version.parse(v2))
|
| 33 |
|
| 34 |
|
| 35 |
class InternVLChatModel(PreTrainedModel):
|
| 36 |
config_class = InternVLChatConfig
|
| 37 |
-
main_input_name =
|
| 38 |
_supports_flash_attn_2 = True
|
| 39 |
-
_no_split_modules = [
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
super().__init__(config)
|
| 43 |
|
| 44 |
-
assert version_cmp(transformers.__version__,
|
| 45 |
image_size = config.force_image_size or config.vision_config.image_size
|
| 46 |
patch_size = config.vision_config.patch_size
|
| 47 |
self.patch_size = patch_size
|
| 48 |
self.select_layer = config.select_layer
|
| 49 |
self.template = config.template
|
| 50 |
-
self.num_image_token = int(
|
|
|
|
|
|
|
| 51 |
self.downsample_ratio = config.downsample_ratio
|
| 52 |
self.ps_version = config.ps_version
|
| 53 |
use_flash_attn = use_flash_attn if has_flash_attn else False
|
| 54 |
config.vision_config.use_flash_attn = True if use_flash_attn else False
|
| 55 |
-
config.llm_config.attn_implementation =
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
logger.info(f
|
| 58 |
-
logger.info(f
|
| 59 |
if vision_model is not None:
|
| 60 |
self.vision_model = vision_model
|
| 61 |
else:
|
|
@@ -63,21 +79,25 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 63 |
if language_model is not None:
|
| 64 |
self.language_model = language_model
|
| 65 |
else:
|
| 66 |
-
if config.llm_config.architectures[0] ==
|
| 67 |
self.language_model = LlamaForCausalLM(config.llm_config)
|
| 68 |
-
elif config.llm_config.architectures[0] ==
|
| 69 |
self.language_model = InternLM2ForCausalLM(config.llm_config)
|
| 70 |
else:
|
| 71 |
-
raise NotImplementedError(
|
|
|
|
|
|
|
| 72 |
|
| 73 |
vit_hidden_size = config.vision_config.hidden_size
|
| 74 |
llm_hidden_size = config.llm_config.hidden_size
|
| 75 |
|
| 76 |
self.mlp1 = nn.Sequential(
|
| 77 |
nn.LayerNorm(vit_hidden_size * int(1 / self.downsample_ratio) ** 2),
|
| 78 |
-
nn.Linear(
|
|
|
|
|
|
|
| 79 |
nn.GELU(),
|
| 80 |
-
nn.Linear(llm_hidden_size, llm_hidden_size)
|
| 81 |
)
|
| 82 |
|
| 83 |
self.img_context_token_id = None
|
|
@@ -85,20 +105,22 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 85 |
self.system_message = self.conv_template.system_message
|
| 86 |
|
| 87 |
def forward(
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 101 |
-
return_dict =
|
|
|
|
|
|
|
| 102 |
|
| 103 |
image_flags = image_flags.squeeze(-1)
|
| 104 |
input_embeds = self.language_model.get_input_embeddings()(input_ids)
|
|
@@ -111,16 +133,22 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 111 |
input_embeds = input_embeds.reshape(B * N, C)
|
| 112 |
|
| 113 |
if torch.distributed.get_rank() == 0:
|
| 114 |
-
print(
|
|
|
|
|
|
|
| 115 |
|
| 116 |
input_ids = input_ids.reshape(B * N)
|
| 117 |
-
selected =
|
| 118 |
try:
|
| 119 |
-
input_embeds[selected] = input_embeds[selected] * 0.0 + vit_embeds.reshape(
|
|
|
|
|
|
|
| 120 |
except Exception as e:
|
| 121 |
vit_embeds = vit_embeds.reshape(-1, C)
|
| 122 |
-
print(
|
| 123 |
-
|
|
|
|
|
|
|
| 124 |
n_token = selected.sum()
|
| 125 |
input_embeds[selected] = input_embeds[selected] * 0.0 + vit_embeds[:n_token]
|
| 126 |
|
|
@@ -170,11 +198,17 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 170 |
# N, W, H * scale, C // scale --> N, H * scale, W, C // scale
|
| 171 |
x = x.permute(0, 2, 1, 3).contiguous()
|
| 172 |
# N, H * scale, W, C // scale --> N, H * scale, W * scale, C // (scale ** 2)
|
| 173 |
-
x = x.view(
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 178 |
else:
|
| 179 |
x = x.permute(0, 2, 1, 3).contiguous()
|
| 180 |
return x
|
|
@@ -182,14 +216,12 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 182 |
def extract_feature(self, pixel_values):
|
| 183 |
if self.select_layer == -1:
|
| 184 |
vit_embeds = self.vision_model(
|
| 185 |
-
pixel_values=pixel_values,
|
| 186 |
-
|
| 187 |
-
return_dict=True).last_hidden_state
|
| 188 |
else:
|
| 189 |
vit_embeds = self.vision_model(
|
| 190 |
-
pixel_values=pixel_values,
|
| 191 |
-
|
| 192 |
-
return_dict=True).hidden_states[self.select_layer]
|
| 193 |
vit_embeds = vit_embeds[:, 1:, :]
|
| 194 |
|
| 195 |
h = w = int(vit_embeds.shape[1] ** 0.5)
|
|
@@ -199,64 +231,95 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 199 |
vit_embeds = self.mlp1(vit_embeds)
|
| 200 |
return vit_embeds
|
| 201 |
|
| 202 |
-
def batch_chat(
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
if history is not None or return_history:
|
| 206 |
-
print(
|
| 207 |
raise NotImplementedError
|
| 208 |
|
| 209 |
if image_counts is not None:
|
| 210 |
num_patches_list = image_counts
|
| 211 |
-
print(
|
|
|
|
|
|
|
| 212 |
|
| 213 |
img_context_token_id = tokenizer.convert_tokens_to_ids(IMG_CONTEXT_TOKEN)
|
| 214 |
self.img_context_token_id = img_context_token_id
|
| 215 |
|
| 216 |
if verbose and pixel_values is not None:
|
| 217 |
image_bs = pixel_values.shape[0]
|
| 218 |
-
print(f
|
| 219 |
|
| 220 |
queries = []
|
| 221 |
for idx, num_patches in enumerate(num_patches_list):
|
| 222 |
question = questions[idx]
|
| 223 |
-
if pixel_values is not None and
|
| 224 |
-
question =
|
| 225 |
template = get_conv_template(self.template)
|
| 226 |
template.system_message = self.system_message
|
| 227 |
template.append_message(template.roles[0], question)
|
| 228 |
template.append_message(template.roles[1], None)
|
| 229 |
query = template.get_prompt()
|
| 230 |
|
| 231 |
-
image_tokens =
|
| 232 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 233 |
queries.append(query)
|
| 234 |
|
| 235 |
-
tokenizer.padding_side =
|
| 236 |
-
model_inputs = tokenizer(queries, return_tensors=
|
| 237 |
-
input_ids = model_inputs[
|
| 238 |
-
attention_mask = model_inputs[
|
| 239 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 240 |
-
generation_config[
|
| 241 |
generation_output = self.generate(
|
| 242 |
pixel_values=pixel_values,
|
| 243 |
input_ids=input_ids,
|
| 244 |
attention_mask=attention_mask,
|
| 245 |
-
**generation_config
|
| 246 |
)
|
| 247 |
responses = tokenizer.batch_decode(generation_output, skip_special_tokens=True)
|
| 248 |
responses = [response.split(template.sep)[0].strip() for response in responses]
|
| 249 |
return responses
|
| 250 |
|
| 251 |
-
def chat(
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
if num_patches_list is None:
|
| 259 |
-
num_patches_list =
|
|
|
|
|
|
|
| 260 |
assert pixel_values is None or len(pixel_values) == sum(num_patches_list)
|
| 261 |
|
| 262 |
img_context_token_id = tokenizer.convert_tokens_to_ids(IMG_CONTEXT_TOKEN)
|
|
@@ -267,7 +330,7 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 267 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 268 |
|
| 269 |
history = [] if history is None else history
|
| 270 |
-
for
|
| 271 |
template.append_message(template.roles[0], old_question)
|
| 272 |
template.append_message(template.roles[1], old_answer)
|
| 273 |
template.append_message(template.roles[0], question)
|
|
@@ -276,45 +339,53 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 276 |
|
| 277 |
if verbose and pixel_values is not None:
|
| 278 |
image_bs = pixel_values.shape[0]
|
| 279 |
-
print(f
|
| 280 |
|
| 281 |
for num_patches in num_patches_list:
|
| 282 |
-
image_tokens =
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 289 |
generation_output = self.generate(
|
| 290 |
pixel_values=pixel_values,
|
| 291 |
input_ids=input_ids,
|
| 292 |
attention_mask=attention_mask,
|
| 293 |
-
**generation_config
|
| 294 |
)
|
| 295 |
-
response = tokenizer.batch_decode(generation_output, skip_special_tokens=True)[
|
|
|
|
|
|
|
| 296 |
response = response.split(template.sep)[0].strip()
|
| 297 |
history.append((question, response))
|
| 298 |
if return_history:
|
| 299 |
return response, history
|
| 300 |
else:
|
| 301 |
-
query_to_print = query.replace(IMG_CONTEXT_TOKEN,
|
| 302 |
-
query_to_print = query_to_print.replace(
|
|
|
|
|
|
|
| 303 |
if verbose:
|
| 304 |
print(query_to_print, response)
|
| 305 |
return response
|
| 306 |
|
| 307 |
@torch.no_grad()
|
| 308 |
def generate(
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
) -> torch.LongTensor:
|
| 319 |
|
| 320 |
assert self.img_context_token_id is not None
|
|
@@ -328,7 +399,7 @@ class InternVLChatModel(PreTrainedModel):
|
|
| 328 |
input_embeds = input_embeds.reshape(B * N, C)
|
| 329 |
|
| 330 |
input_ids = input_ids.reshape(B * N)
|
| 331 |
-
selected =
|
| 332 |
assert selected.sum() != 0
|
| 333 |
input_embeds[selected] = vit_embeds.reshape(-1, C).to(input_embeds.device)
|
| 334 |
|
|
|
|
| 10 |
import transformers
|
| 11 |
from torch import nn
|
| 12 |
from torch.nn import CrossEntropyLoss
|
| 13 |
+
from transformers import AutoModel, GenerationConfig, LlamaForCausalLM, LlamaTokenizer
|
|
|
|
| 14 |
from transformers.modeling_outputs import CausalLMOutputWithPast
|
| 15 |
from transformers.modeling_utils import PreTrainedModel
|
| 16 |
from transformers.utils import ModelOutput, logging
|
|
|
|
| 20 |
from .modeling_intern_vit import InternVisionModel, has_flash_attn
|
| 21 |
from .modeling_internlm2 import InternLM2ForCausalLM
|
| 22 |
|
| 23 |
+
import torch_xla.core.xla_model as xm
|
| 24 |
+
|
| 25 |
logger = logging.get_logger(__name__)
|
| 26 |
|
| 27 |
|
| 28 |
+
def version_cmp(v1, v2, op="eq"):
|
| 29 |
import operator
|
| 30 |
|
| 31 |
from packaging import version
|
| 32 |
+
|
| 33 |
op_func = getattr(operator, op)
|
| 34 |
return op_func(version.parse(v1), version.parse(v2))
|
| 35 |
|
| 36 |
|
| 37 |
class InternVLChatModel(PreTrainedModel):
|
| 38 |
config_class = InternVLChatConfig
|
| 39 |
+
main_input_name = "pixel_values"
|
| 40 |
_supports_flash_attn_2 = True
|
| 41 |
+
_no_split_modules = [
|
| 42 |
+
"InternVisionModel",
|
| 43 |
+
"LlamaDecoderLayer",
|
| 44 |
+
"InternLM2DecoderLayer",
|
| 45 |
+
]
|
| 46 |
+
|
| 47 |
+
def __init__(
|
| 48 |
+
self,
|
| 49 |
+
config: InternVLChatConfig,
|
| 50 |
+
vision_model=None,
|
| 51 |
+
language_model=None,
|
| 52 |
+
use_flash_attn=True,
|
| 53 |
+
):
|
| 54 |
super().__init__(config)
|
| 55 |
|
| 56 |
+
assert version_cmp(transformers.__version__, "4.36.2", "ge")
|
| 57 |
image_size = config.force_image_size or config.vision_config.image_size
|
| 58 |
patch_size = config.vision_config.patch_size
|
| 59 |
self.patch_size = patch_size
|
| 60 |
self.select_layer = config.select_layer
|
| 61 |
self.template = config.template
|
| 62 |
+
self.num_image_token = int(
|
| 63 |
+
(image_size // patch_size) ** 2 * (config.downsample_ratio**2)
|
| 64 |
+
)
|
| 65 |
self.downsample_ratio = config.downsample_ratio
|
| 66 |
self.ps_version = config.ps_version
|
| 67 |
use_flash_attn = use_flash_attn if has_flash_attn else False
|
| 68 |
config.vision_config.use_flash_attn = True if use_flash_attn else False
|
| 69 |
+
config.llm_config.attn_implementation = (
|
| 70 |
+
"flash_attention_2" if use_flash_attn else "eager"
|
| 71 |
+
)
|
| 72 |
|
| 73 |
+
logger.info(f"num_image_token: {self.num_image_token}")
|
| 74 |
+
logger.info(f"ps_version: {self.ps_version}")
|
| 75 |
if vision_model is not None:
|
| 76 |
self.vision_model = vision_model
|
| 77 |
else:
|
|
|
|
| 79 |
if language_model is not None:
|
| 80 |
self.language_model = language_model
|
| 81 |
else:
|
| 82 |
+
if config.llm_config.architectures[0] == "LlamaForCausalLM":
|
| 83 |
self.language_model = LlamaForCausalLM(config.llm_config)
|
| 84 |
+
elif config.llm_config.architectures[0] == "InternLM2ForCausalLM":
|
| 85 |
self.language_model = InternLM2ForCausalLM(config.llm_config)
|
| 86 |
else:
|
| 87 |
+
raise NotImplementedError(
|
| 88 |
+
f"{config.llm_config.architectures[0]} is not implemented."
|
| 89 |
+
)
|
| 90 |
|
| 91 |
vit_hidden_size = config.vision_config.hidden_size
|
| 92 |
llm_hidden_size = config.llm_config.hidden_size
|
| 93 |
|
| 94 |
self.mlp1 = nn.Sequential(
|
| 95 |
nn.LayerNorm(vit_hidden_size * int(1 / self.downsample_ratio) ** 2),
|
| 96 |
+
nn.Linear(
|
| 97 |
+
vit_hidden_size * int(1 / self.downsample_ratio) ** 2, llm_hidden_size
|
| 98 |
+
),
|
| 99 |
nn.GELU(),
|
| 100 |
+
nn.Linear(llm_hidden_size, llm_hidden_size),
|
| 101 |
)
|
| 102 |
|
| 103 |
self.img_context_token_id = None
|
|
|
|
| 105 |
self.system_message = self.conv_template.system_message
|
| 106 |
|
| 107 |
def forward(
|
| 108 |
+
self,
|
| 109 |
+
pixel_values: torch.FloatTensor,
|
| 110 |
+
input_ids: torch.LongTensor = None,
|
| 111 |
+
attention_mask: Optional[torch.Tensor] = None,
|
| 112 |
+
position_ids: Optional[torch.LongTensor] = None,
|
| 113 |
+
image_flags: Optional[torch.LongTensor] = None,
|
| 114 |
+
past_key_values: Optional[List[torch.FloatTensor]] = None,
|
| 115 |
+
labels: Optional[torch.LongTensor] = None,
|
| 116 |
+
use_cache: Optional[bool] = None,
|
| 117 |
+
output_attentions: Optional[bool] = None,
|
| 118 |
+
output_hidden_states: Optional[bool] = None,
|
| 119 |
+
return_dict: Optional[bool] = None,
|
| 120 |
) -> Union[Tuple, CausalLMOutputWithPast]:
|
| 121 |
+
return_dict = (
|
| 122 |
+
return_dict if return_dict is not None else self.config.use_return_dict
|
| 123 |
+
)
|
| 124 |
|
| 125 |
image_flags = image_flags.squeeze(-1)
|
| 126 |
input_embeds = self.language_model.get_input_embeddings()(input_ids)
|
|
|
|
| 133 |
input_embeds = input_embeds.reshape(B * N, C)
|
| 134 |
|
| 135 |
if torch.distributed.get_rank() == 0:
|
| 136 |
+
print(
|
| 137 |
+
f"dynamic ViT batch size: {vit_batch_size}, images per sample: {vit_batch_size / B}, dynamic token length: {N}"
|
| 138 |
+
)
|
| 139 |
|
| 140 |
input_ids = input_ids.reshape(B * N)
|
| 141 |
+
selected = input_ids == self.img_context_token_id
|
| 142 |
try:
|
| 143 |
+
input_embeds[selected] = input_embeds[selected] * 0.0 + vit_embeds.reshape(
|
| 144 |
+
-1, C
|
| 145 |
+
)
|
| 146 |
except Exception as e:
|
| 147 |
vit_embeds = vit_embeds.reshape(-1, C)
|
| 148 |
+
print(
|
| 149 |
+
f"warning: {e}, input_embeds[selected].shape={input_embeds[selected].shape}, "
|
| 150 |
+
f"vit_embeds.shape={vit_embeds.shape}"
|
| 151 |
+
)
|
| 152 |
n_token = selected.sum()
|
| 153 |
input_embeds[selected] = input_embeds[selected] * 0.0 + vit_embeds[:n_token]
|
| 154 |
|
|
|
|
| 198 |
# N, W, H * scale, C // scale --> N, H * scale, W, C // scale
|
| 199 |
x = x.permute(0, 2, 1, 3).contiguous()
|
| 200 |
# N, H * scale, W, C // scale --> N, H * scale, W * scale, C // (scale ** 2)
|
| 201 |
+
x = x.view(
|
| 202 |
+
n,
|
| 203 |
+
int(h * scale_factor),
|
| 204 |
+
int(w * scale_factor),
|
| 205 |
+
int(c / (scale_factor * scale_factor)),
|
| 206 |
+
)
|
| 207 |
+
if self.ps_version == "v1":
|
| 208 |
+
warnings.warn(
|
| 209 |
+
"In ps_version 'v1', the height and width have not been swapped back, "
|
| 210 |
+
"which results in a transposed image."
|
| 211 |
+
)
|
| 212 |
else:
|
| 213 |
x = x.permute(0, 2, 1, 3).contiguous()
|
| 214 |
return x
|
|
|
|
| 216 |
def extract_feature(self, pixel_values):
|
| 217 |
if self.select_layer == -1:
|
| 218 |
vit_embeds = self.vision_model(
|
| 219 |
+
pixel_values=pixel_values, output_hidden_states=False, return_dict=True
|
| 220 |
+
).last_hidden_state
|
|
|
|
| 221 |
else:
|
| 222 |
vit_embeds = self.vision_model(
|
| 223 |
+
pixel_values=pixel_values, output_hidden_states=True, return_dict=True
|
| 224 |
+
).hidden_states[self.select_layer]
|
|
|
|
| 225 |
vit_embeds = vit_embeds[:, 1:, :]
|
| 226 |
|
| 227 |
h = w = int(vit_embeds.shape[1] ** 0.5)
|
|
|
|
| 231 |
vit_embeds = self.mlp1(vit_embeds)
|
| 232 |
return vit_embeds
|
| 233 |
|
| 234 |
+
def batch_chat(
|
| 235 |
+
self,
|
| 236 |
+
tokenizer,
|
| 237 |
+
pixel_values,
|
| 238 |
+
questions,
|
| 239 |
+
generation_config,
|
| 240 |
+
num_patches_list=None,
|
| 241 |
+
history=None,
|
| 242 |
+
return_history=False,
|
| 243 |
+
IMG_START_TOKEN="<img>",
|
| 244 |
+
IMG_END_TOKEN="</img>",
|
| 245 |
+
IMG_CONTEXT_TOKEN="<IMG_CONTEXT>",
|
| 246 |
+
verbose=False,
|
| 247 |
+
image_counts=None,
|
| 248 |
+
):
|
| 249 |
if history is not None or return_history:
|
| 250 |
+
print("Now multi-turn chat is not supported in batch_chat.")
|
| 251 |
raise NotImplementedError
|
| 252 |
|
| 253 |
if image_counts is not None:
|
| 254 |
num_patches_list = image_counts
|
| 255 |
+
print(
|
| 256 |
+
"Warning: `image_counts` is deprecated. Please use `num_patches_list` instead."
|
| 257 |
+
)
|
| 258 |
|
| 259 |
img_context_token_id = tokenizer.convert_tokens_to_ids(IMG_CONTEXT_TOKEN)
|
| 260 |
self.img_context_token_id = img_context_token_id
|
| 261 |
|
| 262 |
if verbose and pixel_values is not None:
|
| 263 |
image_bs = pixel_values.shape[0]
|
| 264 |
+
print(f"dynamic ViT batch size: {image_bs}")
|
| 265 |
|
| 266 |
queries = []
|
| 267 |
for idx, num_patches in enumerate(num_patches_list):
|
| 268 |
question = questions[idx]
|
| 269 |
+
if pixel_values is not None and "<image>" not in question:
|
| 270 |
+
question = "<image>\n" + question
|
| 271 |
template = get_conv_template(self.template)
|
| 272 |
template.system_message = self.system_message
|
| 273 |
template.append_message(template.roles[0], question)
|
| 274 |
template.append_message(template.roles[1], None)
|
| 275 |
query = template.get_prompt()
|
| 276 |
|
| 277 |
+
image_tokens = (
|
| 278 |
+
IMG_START_TOKEN
|
| 279 |
+
+ IMG_CONTEXT_TOKEN * self.num_image_token * num_patches
|
| 280 |
+
+ IMG_END_TOKEN
|
| 281 |
+
)
|
| 282 |
+
query = query.replace("<image>", image_tokens, 1)
|
| 283 |
queries.append(query)
|
| 284 |
|
| 285 |
+
tokenizer.padding_side = "left"
|
| 286 |
+
model_inputs = tokenizer(queries, return_tensors="pt", padding=True)
|
| 287 |
+
input_ids = model_inputs["input_ids"].to(xm.xla_device())
|
| 288 |
+
attention_mask = model_inputs["attention_mask"].to(xm.xla_device())
|
| 289 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 290 |
+
generation_config["eos_token_id"] = eos_token_id
|
| 291 |
generation_output = self.generate(
|
| 292 |
pixel_values=pixel_values,
|
| 293 |
input_ids=input_ids,
|
| 294 |
attention_mask=attention_mask,
|
| 295 |
+
**generation_config,
|
| 296 |
)
|
| 297 |
responses = tokenizer.batch_decode(generation_output, skip_special_tokens=True)
|
| 298 |
responses = [response.split(template.sep)[0].strip() for response in responses]
|
| 299 |
return responses
|
| 300 |
|
| 301 |
+
def chat(
|
| 302 |
+
self,
|
| 303 |
+
tokenizer,
|
| 304 |
+
pixel_values,
|
| 305 |
+
question,
|
| 306 |
+
generation_config,
|
| 307 |
+
history=None,
|
| 308 |
+
return_history=False,
|
| 309 |
+
num_patches_list=None,
|
| 310 |
+
IMG_START_TOKEN="<img>",
|
| 311 |
+
IMG_END_TOKEN="</img>",
|
| 312 |
+
IMG_CONTEXT_TOKEN="<IMG_CONTEXT>",
|
| 313 |
+
verbose=False,
|
| 314 |
+
):
|
| 315 |
+
|
| 316 |
+
if history is None and pixel_values is not None and "<image>" not in question:
|
| 317 |
+
question = "<image>\n" + question
|
| 318 |
|
| 319 |
if num_patches_list is None:
|
| 320 |
+
num_patches_list = (
|
| 321 |
+
[pixel_values.shape[0]] if pixel_values is not None else []
|
| 322 |
+
)
|
| 323 |
assert pixel_values is None or len(pixel_values) == sum(num_patches_list)
|
| 324 |
|
| 325 |
img_context_token_id = tokenizer.convert_tokens_to_ids(IMG_CONTEXT_TOKEN)
|
|
|
|
| 330 |
eos_token_id = tokenizer.convert_tokens_to_ids(template.sep)
|
| 331 |
|
| 332 |
history = [] if history is None else history
|
| 333 |
+
for old_question, old_answer in history:
|
| 334 |
template.append_message(template.roles[0], old_question)
|
| 335 |
template.append_message(template.roles[1], old_answer)
|
| 336 |
template.append_message(template.roles[0], question)
|
|
|
|
| 339 |
|
| 340 |
if verbose and pixel_values is not None:
|
| 341 |
image_bs = pixel_values.shape[0]
|
| 342 |
+
print(f"dynamic ViT batch size: {image_bs}")
|
| 343 |
|
| 344 |
for num_patches in num_patches_list:
|
| 345 |
+
image_tokens = (
|
| 346 |
+
IMG_START_TOKEN
|
| 347 |
+
+ IMG_CONTEXT_TOKEN * self.num_image_token * num_patches
|
| 348 |
+
+ IMG_END_TOKEN
|
| 349 |
+
)
|
| 350 |
+
query = query.replace("<image>", image_tokens, 1)
|
| 351 |
+
|
| 352 |
+
model_inputs = tokenizer(query, return_tensors="pt")
|
| 353 |
+
input_ids = model_inputs["input_ids"].to(xm.xla_device())
|
| 354 |
+
attention_mask = model_inputs["attention_mask"].to(xm.xla_device())
|
| 355 |
+
generation_config["eos_token_id"] = eos_token_id
|
| 356 |
generation_output = self.generate(
|
| 357 |
pixel_values=pixel_values,
|
| 358 |
input_ids=input_ids,
|
| 359 |
attention_mask=attention_mask,
|
| 360 |
+
**generation_config,
|
| 361 |
)
|
| 362 |
+
response = tokenizer.batch_decode(generation_output, skip_special_tokens=True)[
|
| 363 |
+
0
|
| 364 |
+
]
|
| 365 |
response = response.split(template.sep)[0].strip()
|
| 366 |
history.append((question, response))
|
| 367 |
if return_history:
|
| 368 |
return response, history
|
| 369 |
else:
|
| 370 |
+
query_to_print = query.replace(IMG_CONTEXT_TOKEN, "")
|
| 371 |
+
query_to_print = query_to_print.replace(
|
| 372 |
+
f"{IMG_START_TOKEN}{IMG_END_TOKEN}", "<image>"
|
| 373 |
+
)
|
| 374 |
if verbose:
|
| 375 |
print(query_to_print, response)
|
| 376 |
return response
|
| 377 |
|
| 378 |
@torch.no_grad()
|
| 379 |
def generate(
|
| 380 |
+
self,
|
| 381 |
+
pixel_values: Optional[torch.FloatTensor] = None,
|
| 382 |
+
input_ids: Optional[torch.FloatTensor] = None,
|
| 383 |
+
attention_mask: Optional[torch.LongTensor] = None,
|
| 384 |
+
visual_features: Optional[torch.FloatTensor] = None,
|
| 385 |
+
generation_config: Optional[GenerationConfig] = None,
|
| 386 |
+
output_hidden_states: Optional[bool] = None,
|
| 387 |
+
return_dict: Optional[bool] = None,
|
| 388 |
+
**generate_kwargs,
|
| 389 |
) -> torch.LongTensor:
|
| 390 |
|
| 391 |
assert self.img_context_token_id is not None
|
|
|
|
| 399 |
input_embeds = input_embeds.reshape(B * N, C)
|
| 400 |
|
| 401 |
input_ids = input_ids.reshape(B * N)
|
| 402 |
+
selected = input_ids == self.img_context_token_id
|
| 403 |
assert selected.sum() != 0
|
| 404 |
input_embeds[selected] = vit_embeds.reshape(-1, C).to(input_embeds.device)
|
| 405 |
|