# Adopted from https://github.com/haotian-liu/LLaVA. Below is the original copyright: # 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 import math from abc import ABC, abstractmethod import einops import torch import torch.distributed as dist import torch.nn as nn import numpy as np from ..constants import IGNORE_INDEX, MODAL_INDEX_MAP, NUM_FRAMES from .encoder import build_vision_encoder from .projector import build_vision_projector, load_mm_projector from .region_encoder import build_region_encoder from ..mm_utils import reshape_images_to_raw_grid def spatial_downsampling(features, grid_thws, strides): n, c = features.shape flatten_grid_thws = torch.cat([grid_thw for batch_grid_thws in grid_thws for grid_thw in batch_grid_thws]) split_sizes = [grid_thw.prod() for grid_thw in flatten_grid_thws] features = torch.split(features, split_sizes) flatten_strides = [stride for batch_strides in strides for stride in batch_strides] new_features = [] for feature, grid_thw, stride in zip(features, flatten_grid_thws, flatten_strides): # NOTE: adapted for reshape in image processor feature = feature.view(grid_thw[0], grid_thw[1] // stride, grid_thw[2] // stride, stride, stride, c).permute(0, 1, 3, 2, 4, 5) feature = feature.reshape(grid_thw[0], grid_thw[1], grid_thw[2], c).permute(0, 3, 1, 2) # NOTE: previous version model is align_corners=True new_feature = torch.nn.functional.interpolate(feature, (math.ceil(grid_thw[1] / stride), math.ceil(grid_thw[2] / stride)), mode='bilinear') # new_feature = nn.functional.avg_pool2d(feature, stride) # new_feature = nn.functional.max_pool2d(feature, stride) new_features.append(new_feature.permute(0, 2, 3, 1).view(-1, c)) new_features = torch.cat(new_features) return new_features class Videollama3MetaModel: def __init__(self, config): super(Videollama3MetaModel, self).__init__(config) if hasattr(config, "vision_encoder") or hasattr(config, "mm_vision_encoder"): self.vision_encoder = build_vision_encoder(config, delay_load=False) self.mm_projector = build_vision_projector(config) self.region_encoder = build_region_encoder(config, self.vision_encoder.hidden_size) def get_vision_encoder(self): vision_encoder = getattr(self, 'vision_encoder', None) if type(vision_encoder) is list: vision_encoder = vision_encoder[0] return vision_encoder def get_mm_projector(self): return self.mm_projector def initialize_vision_modules(self, model_args, fsdp=None): vision_encoder = model_args.vision_encoder mm_vision_select_layer = model_args.mm_vision_select_layer mm_vision_select_feature = model_args.mm_vision_select_feature pretrain_mm_projector = model_args.pretrain_mm_projector self.config.mm_vision_encoder = vision_encoder if self.get_vision_encoder() is None: vision_encoder = build_vision_encoder(model_args) if fsdp is not None and len(fsdp) > 0: self.vision_encoder = [vision_encoder] else: self.vision_encoder = vision_encoder else: if fsdp is not None and len(fsdp) > 0: vision_encoder = self.vision_encoder[0] else: vision_encoder = self.vision_encoder # NOTE: only compatible with delay_load encoder # vision_encoder.load_model(vision_encoder.cfg_only) self.config.use_mm_proj = True self.config.mm_projector_type = getattr(model_args, 'mm_projector_type', 'linear') self.config.mm_hidden_size = vision_encoder.hidden_size self.config.mm_vision_select_layer = mm_vision_select_layer self.config.mm_vision_select_feature = mm_vision_select_feature if getattr(self, 'mm_projector', None) is None: self.mm_projector = build_vision_projector(self.config) else: # In case it is frozen by LoRA for p in self.mm_projector.parameters(): p.requires_grad = True if pretrain_mm_projector is not None: if os.path.exists(pretrain_mm_projector): is_local = True if os.path.isdir(pretrain_mm_projector): mm_projector_weights = load_mm_projector(pretrain_mm_projector) else: mm_projector_weights = torch.load(pretrain_mm_projector, map_location='cpu') else: # Support loading projector weights from remote HuggingFace model hub is_local = False pretrain_mm_projector = pretrain_mm_projector.replace('mm_projector.bin', '') pretrain_mm_projector = pretrain_mm_projector.strip('/').strip('\\').strip() mm_projector_weights = load_mm_projector(pretrain_mm_projector) def get_w(weights, keyword): return {k.split(keyword + '.')[1]: v for k, v in weights.items() if keyword in k} # self.mm_projector.load_state_dict(get_w(mm_projector_weights, 'mm_projector')) # set strict=False to avoid missing key error regarding bert.embeddings.position_ids self.mm_projector.load_state_dict(get_w(mm_projector_weights, 'mm_projector'), strict=False) class Videollama3MetaForCausalLM(ABC): @abstractmethod def get_model(self): pass def num_frames(self): if hasattr(self.config, 'num_frames'): return self.config.num_frames else: return NUM_FRAMES def spatial_merge_size(self): if hasattr(self.config, 'spatial_merge_size'): return self.config.spatial_merge_size else: return 1 def get_vision_encoder(self): return self.get_model().get_vision_encoder() def get_mm_projector(self): return self.get_model().get_mm_projector() def encode_images(self,images, grid_thws, strides): """ images shape [b c h w] """ images_features = self.get_model().get_vision_encoder()(images, grid_thws=grid_thws, strides=strides) # images_features = spatial_downsampling(images_features, grid_thws, stride=self.config.spatial_merge_size) mm_features = spatial_downsampling(images_features, grid_thws, strides=strides) images_features = self.get_model().mm_projector(mm_features) return images_features def prepare_inputs_labels_for_multimodal( self, input_ids, attention_mask, past_key_values, labels, images, position_ids=None, masks=None, additional_images = None, ): if self.config.use_token_compression: return self.prepare_inputs_labels_for_multimodal_with_compression(input_ids, attention_mask, past_key_values, labels, images, position_ids, masks, additional_images) # # images shape (modal, tensor, flag) # vision_encoder = self.get_vision_encoder() # # NOTE: text-only situation # if vision_encoder is None or images is None or input_ids.shape[1] == 1: # return input_ids, attention_mask, past_key_values, None, labels, position_ids # # NOTE: Equvialent to the following code: # # images_tensor = [image for modal, image, image_flag, grid_thw in images] # # images_flag = [image_flag for modal, image, image_flag, grid_thw in images] # # grid_thws = [grid_thw for modal, image, image_flag, grid_thw in images] # modals, images, grid_thws = zip(*images) # images_flag = [] # strides = [] # for modal, grid_thw in zip(modals, grid_thws): # grid_thw = torch.cat(grid_thw) # stride = self.config.spatial_merge_size if modal == "video" else 1 # num_patches = grid_thw.prod(dim=-1).sum().div(stride**2).long() # image_flag = torch.full((num_patches, ), 0 if modal == 'text' else 1) # images_flag.append(image_flag) # strides.append([stride] * grid_thw.size(0)) # images_flag_tensor = torch.cat(images_flag) # mm_features = self.encode_images(images, grid_thws, strides) # mm_features = mm_features[images_flag_tensor.to(mm_features.device) == 1].to(input_ids.device) # additional_images_list = [] # additional_images_thw = [] # additional_images_strides = [] # for i in range(len(additional_images)): # additional_images_list.append(torch.from_numpy(np.array(additional_images[0][0])).to(mm_features.dtype).to(mm_features.device)) # additional_images_thw.append(torch.tensor(additional_images[0][1][0]).to(mm_features.device)) # additional_images_strides.append([1]*len(additional_images[0][1][0])) # image_selected = (input_ids == self.config.image_token_index) # audio_selected = (input_ids == MODAL_INDEX_MAP['