Spaces:
Running
on
Zero
Running
on
Zero
File size: 32,030 Bytes
e19aac6 |
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 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 |
# Copyright 2023 Haotian Liu
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os, sys, os.path as osp
import warnings
from abc import ABC, abstractmethod
import torch, logging
from transformers import (
AutoTokenizer,
AutoModel,
AutoModelForCausalLM,
AutoConfig,
BitsAndBytesConfig,
PretrainedConfig,
PreTrainedModel,
)
from .constants import (
DEFAULT_IM_END_TOKEN,
DEFAULT_IM_START_TOKEN,
DEFAULT_IMAGE_PATCH_TOKEN,
IGNORE_INDEX,
IMAGE_TOKEN_INDEX,
MASK_TOKEN_INDEX,
)
from collections import OrderedDict
from .utils import get_model_config
from .language_model.builder import build_llm_and_tokenizer
from .multimodal_encoder.builder import build_vision_tower, build_context_provider
from .multimodal_projector.builder import build_mm_projector
from .configuration_llava import LlavaConfig
from transformers.modeling_utils import ContextManagers, no_init_weights
## TODO decide whether should we use metaclass
class LlavaMetaModel(ABC):
def init_vlm(self, config: PreTrainedModel = None, *args, **kwargs):
# TODO(ligeng): figure out how from_config and from_pretrained works in HF implementation.
if hasattr(self, "llm") or hasattr(self, "vision_tower") or hasattr(self, "mm_projector"):
# already initialized, skipped
return
model_dtype = getattr(config, "model_dtype", "torch.float16")
if not hasattr(config, "model_dtype"):
warnings.warn("model_dtype not found in config, defaulting to torch.float16.")
config.model_dtype = model_dtype
# print("init_vlm(): config", config); input("DEBUG init_vlm")
cfgs = get_model_config(config)
# Only the first three are required. Others are optional.
llm_cfg, vision_tower_cfg, mm_projector_cfg, mask_encoder_cfg, context_provider_cfg = cfgs
if llm_cfg is None or vision_tower_cfg is None or mm_projector_cfg is None:
raise ValueError("`llm_cfg` `mm_projector_cfg` `vision_tower_cfg` not found in the config.")
# print("init_vlm():", cfgs); input("DEBUG init_vlm")
# print(llm_cfg, vision_tower_cfg, mm_projector_cfg); input("DEBUG init_vlm")
self.llm, self.tokenizer = build_llm_and_tokenizer(llm_cfg, config, *args, **kwargs)
self.vision_tower = build_vision_tower(vision_tower_cfg, config)
self.mm_projector = build_mm_projector(mm_projector_cfg, config)
self.context_provider = build_context_provider(context_provider_cfg, config) if context_provider_cfg is not None else None
self.post_config()
self.is_loaded = True
assert (
self.llm is not None or self.vision_tower is not None or self.mm_projector is not None
), "At least one of the components must be instantiated."
@classmethod
def load_from_config(cls, model_path_or_config, *args, **kwargs):
pass
## FIXME we will use this function to load model in the future
@classmethod
def load_pretrained(cls, model_path_or_config, *args, **kwargs):
kwargs.pop("config", None)
if isinstance(model_path_or_config, str):
config = AutoConfig.from_pretrained(model_path_or_config)
elif isinstance(model_path_or_config, LlavaConfig):
config = model_path_or_config
else:
raise NotImplementedError(f"wrong type, {type(model_path_or_config)} \
{isinstance(model_path_or_config, LlavaConfig)}")
model_dtype = getattr(config, "model_dtype", "torch.float16")
if not hasattr(config, "model_dtype"):
warnings.warn("model_dtype not found in config, defaulting to torch.float16.")
config.model_dtype = model_dtype
cfgs = get_model_config(config)
# Only the first three are required. Others are optional.
llm_cfg, vision_tower_cfg, mm_projector_cfg, mask_encoder_cfg, context_provider_cfg = cfgs
if llm_cfg is None or vision_tower_cfg is None or mm_projector_cfg is None:
raise ValueError("`llm_cfg` `mm_projector_cfg` `vision_tower_cfg` not found in the config.")
# print(llm_cfg, vision_tower_cfg, mm_projector_cfg); input("DEBUG load_pretrained")
with ContextManagers([no_init_weights(_enable=True),]):
vlm = cls(config, *args, **kwargs)
# print(llm_cfg, vision_tower_cfg, mm_projector_cfg); input("DEBUG load_pretrained finish")
if hasattr(vlm, "llm") or hasattr(vlm, "vision_tower") or hasattr(vlm, "mm_projector"):
if vlm.is_loaded:
return vlm
vlm.llm, vlm.tokenizer = build_llm_and_tokenizer(llm_cfg, config, *args, **kwargs)
vlm.vision_tower = build_vision_tower(vision_tower_cfg, config)
vlm.mm_projector = build_mm_projector(mm_projector_cfg, config)
if mask_encoder_cfg is not None:
raise NotImplementedError("Mask encoder is not supported.")
vlm.context_provider = build_context_provider(context_provider_cfg, config) if context_provider_cfg is not None else None
self.post_config()
self.is_loaded = True
# FIXME(ligeng, yunhao): llm should never be none here.
assert (
vlm.llm is not None or vlm.vision_tower is not None or vlm.mm_projector is not None
), "At least one of the components must be instantiated."
return vlm
## FIXME we will use this function to save the model in the future
def save_pretrained(self, output_dir, state_dict=None):
if state_dict is None:
# other wise fetch from deepspeed
# state_dict = accelerator.get_state_dict(is_deepspeed_enabled)
state_dict = self.state_dict()
if getattr(self, "tokenizer", None):
self.tokenizer.save_pretrained(osp.join(output_dir, "llm"))
if self.get_llm():
print(f"saving llm to {osp.join(output_dir, 'llm')}")
self.llm.config._name_or_path = osp.join(output_dir, "llm")
llm_state_dict = OrderedDict({k.split("llm.")[-1]: v for k, v in state_dict.items() if "llm" in k})
self.llm.save_pretrained(os.path.join(output_dir, "llm"), state_dict=llm_state_dict)
self.config.llm_cfg = self.llm.config
if self.get_vision_tower() and "radio" not in self.get_vision_tower().__class__.__name__.lower():
print(f"saving vision_tower to {osp.join(output_dir, 'vision_tower')}")
self.vision_tower.config._name_or_path = osp.join(output_dir, "vision_tower")
vision_tower_state_dict = OrderedDict(
{k.split("vision_tower.vision_tower.")[-1]: v for k, v in state_dict.items() if "vision_tower" in k}
)
self.vision_tower.vision_tower.save_pretrained(
os.path.join(output_dir, "vision_tower"),
state_dict=vision_tower_state_dict,
)
self.vision_tower.image_processor.save_pretrained(os.path.join(output_dir, "vision_tower"))
self.config.vision_tower_cfg = self.vision_tower.config
if hasattr(self.config.vision_tower_cfg, 'auto_map'):
delattr(self.config.vision_tower_cfg, 'auto_map')
if self.get_mm_projector():
print(f"saving mm_projector to {osp.join(output_dir, 'mm_projector')}")
self.mm_projector.config._name_or_path = osp.join(output_dir, "mm_projector")
mm_projector_state_dict = OrderedDict(
{k.split("mm_projector.")[-1]: v for k, v in state_dict.items() if "mm_projector" in k}
)
self.mm_projector.save_pretrained(
os.path.join(output_dir, "mm_projector"),
state_dict=mm_projector_state_dict,
)
self.config.mm_projector_cfg = self.mm_projector.config
if self.get_context_provider():
print(f"saving context_provider to {osp.join(output_dir, 'context_provider')}")
self.context_provider.config._name_or_path = osp.join(output_dir, "context_provider")
context_provider_state_dict = OrderedDict(
{k.split("context_provider.")[-1]: v for k, v in state_dict.items() if "context_provider" in k}
)
self.context_provider.save_pretrained(
os.path.join(output_dir, "context_provider"),
state_dict=context_provider_state_dict,
)
self.config.context_provider_cfg = self.context_provider.config
## update and save top-level config
self.config._name_or_path = output_dir
self.config.architectures = [self.__class__.__name__]
self.config.save_pretrained(output_dir)
def get_llm(self):
llm = getattr(self, "llm", None)
if type(llm) is list:
llm = llm[0]
return llm
def get_lm_head(self):
lm_head = getattr(self.get_llm(), "lm_head", None)
return lm_head
def get_vision_tower(self):
vision_tower = getattr(self, "vision_tower", None)
if type(vision_tower) is list:
vision_tower = vision_tower[0]
return vision_tower
def get_mm_projector(self):
mm_projector = getattr(self, "mm_projector", None)
if type(mm_projector) is list:
mm_projector = mm_projector[0]
return mm_projector
def get_context_provider(self):
context_provider = getattr(self, "context_provider", None)
return context_provider
def post_config(self):
self.training = self.get_llm().training
## configuration
if getattr(self.config, "llm_cfg", None) is None:
self.config.llm_cfg = self.llm.config
if getattr(self.config, "vision_tower_cfg", None) is None:
self.config.vision_tower_cfg = self.vision_tower.config
if getattr(self.config, "mm_projector_cfg", None) is None:
self.config.mm_projector_cfg = self.mm_projector.config
if getattr(self.config, "context_provider_cfg", None) is None and self.context_provider is not None:
self.config.context_provider_cfg = self.context_provider.config
def freezed_module_patch(self):
'''
Huggingface will call model.train() at each training_step. To ensure the expected behaviors for modules like dropout, batchnorm, etc., we need to call model.eval() for the freezed modules.
'''
if self.training:
if self.get_llm() and not getattr(self.config, "tune_language_model", False):
logging.warning("Caution: Your LLM is currently in training mode, ensuring accurate gradient computation. Please be vigilant, particularly regarding BatchNorm and Dropout operations.")
if self.get_vision_tower() and not getattr(self.config, "tune_vision_tower", False):
self.get_vision_tower().eval()
if self.get_mm_projector() and not getattr(self.config, "tune_mm_projector", False):
self.get_mm_projector().eval()
if self.get_context_provider() and not getattr(self.config, "tune_context_provider", False):
self.get_context_provider().eval()
def encode_images(self, images):
image_features = self.get_vision_tower()(images)
image_features = self.get_mm_projector()(image_features)
return image_features
def encode_images_with_context(self, images):
context_provider = self.get_context_provider()
# If the channels completely match, they are cimage (image with context).
cimage_mask = torch.any((images[:, :4, ...] != images[:, 4:, ...]).flatten(start_dim=1), dim=1)
if context_provider.treat_image_as_cimage:
# If the context provider treats the image as cimage, then all images are cimage.
cimage_mask[:] = True
if context_provider.context_image_as_queries:
# Swap the crop image and full image since the model uses the full image as queries by default
images = torch.cat((images[:, 4:, ...], images[:, :4, ...]), dim=1)
# Process the first 4 channels for all images: for image it's the image, for cimage it's the full image
vision_tower = self.get_vision_tower()
# Encode context images (full images)
image_features = vision_tower(images[:, :4, ...]).to(self.device)
# Each cimage has 8 channels (full and crop concatenated)
cimage_concatenated = images[cimage_mask]
cimage_full_features = image_features[cimage_mask]
if context_provider.context_provider_type == "cross_attn_end_to_all":
cimage_features = self.context_provider(
cimage_full_features=cimage_full_features,
cimage_concatenated=cimage_concatenated,
vision_tower=vision_tower
).to(self.device)
elif context_provider.context_provider_type == "concat":
# Full features of cimages are computed but not used.
cimage_features = self.context_provider(
cimage_concatenated=cimage_concatenated,
vision_tower=vision_tower
).to(self.device)
else:
raise NotImplementedError(f"Context provider type {context_provider.context_provider_type} not implemented.")
# Put cimage_features into image_features
image_features[cimage_mask] = cimage_features
# Project to the llm space
image_features = self.get_mm_projector()(image_features)
return image_features
## @yunhao: is there a better way to handle function call and attributes for llm?
## support beam search
def _temporary_reorder_cache(self, past_key_values, sorted_idx):
return self.get_llm()._temporary_reorder_cache(past_key_values, sorted_idx)
def get_input_embeddings(self):
return self.get_llm().get_input_embeddings()
def get_output_embeddings(self):
return self.get_llm().get_output_embeddings()
def resize_token_embeddings(self, embed_size):
self.get_llm().resize_token_embeddings(embed_size)
class LlavaMetaForCausalLM(ABC):
"""This class is originally implemented by the LLaVA team and
modified by Haotian Tang and Jason Lu based on Ji Lin's implementation
to support multiple images and input packing."""
## TODO move the forward function here if there is no need to override it
def prepare_inputs_labels_for_multimodal(
self, input_ids, position_ids, attention_mask, past_key_values, labels, images
):
vision_tower = self.get_vision_tower()
if vision_tower is None or images is None or input_ids.shape[1] == 1:
if (
past_key_values is not None
and vision_tower is not None
and images is not None
and input_ids.shape[1] == 1
):
target_shape = past_key_values[-1][-1].shape[-2] + 1
attention_mask = torch.cat(
(
attention_mask,
torch.ones(
(
attention_mask.shape[0],
target_shape - attention_mask.shape[1],
),
dtype=attention_mask.dtype,
device=attention_mask.device,
),
),
dim=1,
)
position_ids = torch.sum(attention_mask, dim=1).unsqueeze(-1) - 1
return (
input_ids,
position_ids,
attention_mask,
past_key_values,
None,
labels,
)
# handle different image dtypes for packing
if type(images) is list:
images = torch.cat(images, dim=0)
elif images.ndim == 5: # batch_size x seq_len x image_channels
images = images.flatten(0, 1)
if getattr(self, "context_provider", None):
image_features = self.encode_images_with_context(images)
else:
# Since we slice it with index below, turning it into a list splits things by the first index which does not result in data copy or degrade performance.
# Example dimension: [1, 196, 2560]
assert images.shape[1] <= 4, f"images have more than 4 channels, but context provider is not included"
image_features = self.encode_images(images).to(self.device)
# Note (kentang-mit@): image start / end is not implemented here to support pretraining.
if getattr(self.config, "turn_mm_projector", False) and getattr(self.config, "mm_use_im_start_end", False):
raise NotImplementedError
# Let's just add dummy tensors if they do not exist,
# it is a headache to deal with None all the time.
# But it is not ideal, and if you have a better idea,
# please open an issue / submit a PR, thanks.
_labels = labels
_position_ids = position_ids
_attention_mask = attention_mask
if attention_mask is None:
attention_mask = torch.ones_like(input_ids, dtype=torch.bool)
else:
attention_mask = attention_mask.bool()
if position_ids is None:
position_ids = torch.arange(0, input_ids.shape[1], dtype=torch.long, device=input_ids.device)
if labels is None:
labels = torch.full_like(input_ids, IGNORE_INDEX)
# remove the padding using attention_mask
input_ids_copy = input_ids.clone()
# kentang-mit@: Otherwise tokenizer out of bounds. Embeddings of image tokens will not be used.
input_ids_copy[input_ids_copy == IMAGE_TOKEN_INDEX] = 0
input_embeds = self.llm.model.embed_tokens(input_ids_copy)
input_ids = [
cur_input_ids[cur_attention_mask] for cur_input_ids, cur_attention_mask in zip(input_ids, attention_mask)
]
input_embeds_1 = [
cur_input_embeds[cur_attention_mask]
for cur_input_embeds, cur_attention_mask in zip(input_embeds, attention_mask)
]
labels = [cur_labels[cur_attention_mask] for cur_labels, cur_attention_mask in zip(labels, attention_mask)]
new_input_embeds = []
new_labels = []
cur_image_idx = 0
# print("BEFORE BATCH LOOP:", len(input_ids), input_ids[0].shape, input_ids[0].device, [(x == IMAGE_TOKEN_INDEX).sum() for x in input_ids])
# kentang-mit@: If some part of the model is executed in the loop, the the loop length needs to be a constant.
for batch_idx, cur_input_ids in enumerate(input_ids):
cur_input_ids = input_ids[batch_idx]
num_images = (cur_input_ids == IMAGE_TOKEN_INDEX).sum()
if num_images == 0:
cur_image_features = image_features[0]
# cur_input_embeds_1 = self.get_llm().embed_tokens(cur_input_ids)
cur_input_embeds_1 = input_embeds_1[batch_idx]
cur_input_embeds = torch.cat([cur_input_embeds_1, cur_image_features[0:0]], dim=0)
new_input_embeds.append(cur_input_embeds)
new_labels.append(labels[batch_idx])
# kenang-mit@: we do not have placeholdr image for text-only data now.
# cur_image_idx += 1
continue
cur_input_embeds = input_embeds_1[batch_idx]
image_token_indices = (
[-1] + torch.where(cur_input_ids == IMAGE_TOKEN_INDEX)[0].tolist() + [cur_input_ids.shape[0]]
)
cur_input_ids_noim = []
cur_labels = labels[batch_idx]
cur_labels_noim = []
cur_input_embeds_no_im = []
for i in range(len(image_token_indices) - 1):
cur_input_ids_noim.append(cur_input_ids[image_token_indices[i] + 1 : image_token_indices[i + 1]])
cur_labels_noim.append(cur_labels[image_token_indices[i] + 1 : image_token_indices[i + 1]])
cur_input_embeds_no_im.append(cur_input_embeds[image_token_indices[i] + 1 : image_token_indices[i + 1]])
split_sizes = [x.shape[0] for x in cur_labels_noim]
# cur_input_embeds = self.get_llm().embed_tokens(torch.cat(cur_input_ids_noim))
# cur_input_embeds_no_im = torch.split(cur_input_embeds, split_sizes, dim=0)
cur_new_input_embeds = []
cur_new_labels = []
for i in range(num_images + 1):
cur_new_input_embeds.append(cur_input_embeds_no_im[i])
cur_new_labels.append(cur_labels_noim[i])
if i < num_images:
cur_image_features = image_features[cur_image_idx]
cur_image_idx += 1
cur_new_input_embeds.append(cur_image_features)
cur_new_labels.append(
torch.full(
(cur_image_features.shape[0],),
IGNORE_INDEX,
device=cur_labels.device,
dtype=cur_labels.dtype,
)
)
cur_new_input_embeds = torch.cat(cur_new_input_embeds)
cur_new_labels = torch.cat(cur_new_labels)
new_input_embeds.append(cur_new_input_embeds)
new_labels.append(cur_new_labels)
# Truncate sequences to max length as image embeddings can make the sequence longer
tokenizer_model_max_length = getattr(self.llm.config, "tokenizer_model_max_length", None)
if tokenizer_model_max_length is not None:
if any(len(x) > tokenizer_model_max_length for x in new_input_embeds):
warnings.warn("Inputs truncated!")
new_input_embeds = [x[:tokenizer_model_max_length] for x in new_input_embeds]
new_labels = [x[:tokenizer_model_max_length] for x in new_labels]
# Combine them
max_len = max(x.shape[0] for x in new_input_embeds)
batch_size = len(new_input_embeds)
new_input_embeds_padded = []
new_labels_padded = torch.full(
(batch_size, max_len),
IGNORE_INDEX,
dtype=new_labels[0].dtype,
device=new_labels[0].device,
)
attention_mask = torch.zeros(
(batch_size, max_len),
dtype=attention_mask.dtype,
device=attention_mask.device,
)
position_ids = torch.zeros((batch_size, max_len), dtype=position_ids.dtype, device=position_ids.device)
for i, (cur_new_embed, cur_new_labels) in enumerate(zip(new_input_embeds, new_labels)):
cur_len = cur_new_embed.shape[0]
if getattr(self.llm.config, "tokenizer_padding_side", "right") == "left":
new_input_embeds_padded.append(
torch.cat(
(
torch.zeros(
(max_len - cur_len, cur_new_embed.shape[1]),
dtype=cur_new_embed.dtype,
device=cur_new_embed.device,
),
cur_new_embed,
),
dim=0,
)
)
if cur_len > 0:
new_labels_padded[i, -cur_len:] = cur_new_labels
attention_mask[i, -cur_len:] = True
position_ids[i, -cur_len:] = torch.arange(
0, cur_len, dtype=position_ids.dtype, device=position_ids.device
)
else:
new_input_embeds_padded.append(
torch.cat(
(
cur_new_embed,
torch.zeros(
(max_len - cur_len, cur_new_embed.shape[1]),
dtype=cur_new_embed.dtype,
device=cur_new_embed.device,
),
),
dim=0,
)
)
if cur_len > 0:
new_labels_padded[i, :cur_len] = cur_new_labels
attention_mask[i, :cur_len] = True
position_ids[i, :cur_len] = torch.arange(
0, cur_len, dtype=position_ids.dtype, device=position_ids.device
)
new_input_embeds = torch.stack(new_input_embeds_padded, dim=0)
if _labels is None:
new_labels = None
else:
new_labels = new_labels_padded
if _attention_mask is None:
attention_mask = None
else:
attention_mask = attention_mask.to(dtype=_attention_mask.dtype)
if _position_ids is None:
position_ids = None
return (
None,
position_ids,
attention_mask,
past_key_values,
new_input_embeds,
new_labels,
)
def repack_multimodal_data(
self,
input_ids,
position_ids,
attention_mask,
past_key_values,
inputs_embeds,
labels,
):
# kentang-mit@: reorder and repack (reduce computation overhead)
# requires transformers replacement.
new_inputs_embeds = []
new_position_ids = []
new_labels = []
seqlens_in_batch = attention_mask.sum(dim=-1, dtype=torch.int32)
sorted_seqlens_in_batch, sorted_idx = torch.sort(seqlens_in_batch, descending=True)
# print(sorted_seqlens_in_batch)
max_seqlen = inputs_embeds.shape[1]
cur_inputs_embeds = []
cur_position_ids = []
cur_labels = []
cur_batch_len = 0
# print(sorted_seqlens_in_batch.device, len(sorted_seqlens_in_batch), max_seqlen)
for i in range(len(sorted_seqlens_in_batch)):
cur_seqlen = sorted_seqlens_in_batch[i].item()
if cur_seqlen + cur_batch_len <= max_seqlen:
cur_batch_len += cur_seqlen
# each item: num_tokens x num_channels
# remove padding on-the-fly
cur_inputs_embeds.append(inputs_embeds[sorted_idx[i]][attention_mask[sorted_idx[i]]])
# each item: num_tokens
cur_position_ids.append(
torch.arange(
cur_inputs_embeds[-1].shape[0],
device=cur_inputs_embeds[-1].device,
)
)
# each item: num_tokens
# remove padding on-the-fly
cur_labels.append(labels[sorted_idx[i]][attention_mask[sorted_idx[i]]])
else:
new_inputs_embeds.append(torch.cat(cur_inputs_embeds, 0))
new_position_ids.append(torch.cat(cur_position_ids, 0))
new_labels.append(torch.cat(cur_labels, 0))
# The current batch is too long. We will start a new batch.
cur_batch_len = cur_seqlen
cur_inputs_embeds = [inputs_embeds[sorted_idx[i]][attention_mask[sorted_idx[i]]]]
cur_position_ids = [
torch.arange(
cur_inputs_embeds[-1].shape[0],
device=cur_inputs_embeds[-1].device,
)
]
cur_labels = [labels[sorted_idx[i]][attention_mask[sorted_idx[i]]]]
if len(cur_inputs_embeds):
new_inputs_embeds.append(torch.cat(cur_inputs_embeds, 0))
new_position_ids.append(torch.cat(cur_position_ids, 0))
new_labels.append(torch.cat(cur_labels, 0))
# print(new_position_ids[0].device, [x.shape for x in new_inputs_embeds], [x.shape for x in new_labels], [x.shape for x in new_position_ids])
# assert 0
new_inputs_embeds = torch.nn.utils.rnn.pad_sequence(
new_inputs_embeds, batch_first=True, padding_value=self.llm.pad_token_id
)
new_position_ids = torch.nn.utils.rnn.pad_sequence(new_position_ids, batch_first=True, padding_value=-1)
new_labels = torch.nn.utils.rnn.pad_sequence(new_labels, batch_first=True, padding_value=IGNORE_INDEX)
## yunhao: it's currently a workaround to avoid errors for seq_len < 100
new_attention_mask = new_position_ids.ne(-1)
# sanity check
assert new_attention_mask.sum() == attention_mask.sum()
# print(new_inputs_embeds.shape, (new_attention_mask.sum(1)))
# print(sorted_seqlens_in_batch.device, sorted_seqlens_in_batch, new_attention_mask.sum(1))
# return None, position_ids, attention_mask, past_key_values, new_input_embeds, new_labels
return (
None,
new_position_ids,
new_attention_mask,
past_key_values,
new_inputs_embeds,
new_labels,
sorted_seqlens_in_batch,
)
def initialize_vision_tokenizer(self, model_args, tokenizer):
if model_args.mm_use_im_patch_token:
tokenizer.add_tokens([DEFAULT_IMAGE_PATCH_TOKEN], special_tokens=True)
self.resize_token_embeddings(len(tokenizer))
if model_args.mm_use_im_start_end:
num_new_tokens = tokenizer.add_tokens([DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN], special_tokens=True)
self.resize_token_embeddings(len(tokenizer))
if num_new_tokens > 0:
input_embeddings = self.get_input_embeddings().weight.data
output_embeddings = self.get_output_embeddings().weight.data
input_embeddings_avg = input_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
output_embeddings_avg = output_embeddings[:-num_new_tokens].mean(dim=0, keepdim=True)
input_embeddings[-num_new_tokens:] = input_embeddings_avg
output_embeddings[-num_new_tokens:] = output_embeddings_avg
## TODO yunhao: handle cases for <im_st> <im_end>
if model_args.pretrain_mm_mlp_adapter:
mm_projector_weights = torch.load(model_args.pretrain_mm_mlp_adapter, map_location="cpu")
embed_tokens_weight = mm_projector_weights["model.embed_tokens.weight"]
assert num_new_tokens == 2
if input_embeddings.shape == embed_tokens_weight.shape:
input_embeddings[-num_new_tokens:] = embed_tokens_weight[-num_new_tokens:]
elif embed_tokens_weight.shape[0] == num_new_tokens:
input_embeddings[-num_new_tokens:] = embed_tokens_weight
else:
raise ValueError(
f"Unexpected embed_tokens_weight shape. Pretrained: {embed_tokens_weight.shape}. Current: {input_embeddings.shape}. Numer of new tokens: {num_new_tokens}."
)
elif model_args.mm_use_im_patch_token:
if model_args.mm_projector:
for p in self.get_input_embeddings().parameters():
p.requires_grad = False
for p in self.get_output_embeddings().parameters():
p.requires_grad = False
|