Update modeling_minicpmv.py
Browse files101 unmodified lines1021031041051061071082 unmodified lines11111211311411511611752 unmodified lines17017117217317417517617 unmodified lines19419519619719819920078 unmodified lines27928028128228328428523 unmodified lines3093103113123133143152 unmodified lines318319320321322323324325326327328329330331332333334101 unmodified linespadding_value=0.0)B, L, _ = all_pixel_values.shapeall_pixel_values = all_pixel_values.permute(0, 2, 1).reshape(B, 3, -1, L)print(B, "BATCH")patch_attn_mask = torch.zeros((B, 1, max_patches), dtype=torch.bool, device=device)for i in range(B):patch_attn_mask[i, :tgt_sizes[i][0] * tgt_sizes[i][1]] = True2 unmodified linesvision_embedding = self.resampler(vision_embedding, tgt_sizes)else:# get vision_embedding foreachprint("HERE, NOT BATCH")vision_embedding = []for single_tgt_size, single_pixel_values in zip(tgt_sizes, all_pixel_values):single_pixel_values = single_pixel_values.unsqueeze(0)52 unmodified lineselif self.training:cur_vllm_emb += cur_vs_hs[0].mean() * 0print(vllm_embedding.shape)return vllm_embedding, vision_hidden_statesdef forward(self, data, **kwargs):17 unmodified linesresult = result[1:]if result[-1] == tokenizer.eos_id or result[-1] == tokenizer.eot_id:result = result[:-1]print(result)result_text.append(tokenizer.decode(result).strip())return result_text78 unmodified linesinput_embeds,vision_hidden_states_dummy,) = self.get_vllm_embedding(model_inputs)print(input_embeds.shape, f"INPUT_EMBEDS {counter}")counter += 1batch.append(input_embeds)23 unmodified lines# Concatenate the padding tensor to the left of the original tensortensor = torch.cat((padding_tensor, tensor), dim=1)print(tensor.shape, "UPDATED_SHAPE")# Update the batch with the padded tensorbatch[place] = tensor2 unmodified linesattention_mask.append(to_add)attention_mask = torch.tensor(attention_mask)print(attention_mask.shape)print(attention_mask, "ATTENTION")# attention_mask = attention_mask.to(self.device)# padded_tensors = [torch.nn.functional.pad(tensor, (0, 0, 0, max_x - tensor.shape[1])) for tensor in batch]# Step 3: Stack the padded tensors into a single batchfor stuff in batch:print(stuff.shape, "SHAPE")batch = torch.cat(batch, dim=0)print(batch.shape)print(batch)# output_ids = self._decode(input_embeds, tokenizer, **kwargs)if stream:kwargs.pop("decode_text")101 unmodified lines1021031041051061071082 unmodified lines11111211311411511611752 unmodified lines17017117217317417517617 unmodified lines19419519619719819920078 unmodified lines27928028128228328428523 unmodified lines3093103113123133143152 unmodified lines318319320321322323324325326327328329330331332333334101 unmodified linespadding_value=0.0)B, L, _ = all_pixel_values.shapeall_pixel_values = all_pixel_values.permute(0, 2, 1).reshape(B, 3, -1, L)# print(B, "BATCH")patch_attn_mask = torch.zeros((B, 1, max_patches), dtype=torch.bool, device=device)for i in range(B):patch_attn_mask[i, :tgt_sizes[i][0] * tgt_sizes[i][1]] = True2 unmodified linesvision_embedding = self.resampler(vision_embedding, tgt_sizes)else:# get vision_embedding foreach# print("HERE, NOT BATCH")vision_embedding = []for single_tgt_size, single_pixel_values in zip(tgt_sizes, all_pixel_values):single_pixel_values = single_pixel_values.unsqueeze(0)52 unmodified lineselif self.training:cur_vllm_emb += cur_vs_hs[0].mean() * 0# print(vllm_embedding.shape)return vllm_embedding, vision_hidden_statesdef forward(self, data, **kwargs):17 unmodified linesresult = result[1:]if result[-1] == tokenizer.eos_id or result[-1] == tokenizer.eot_id:result = result[:-1]# print(result)result_text.append(tokenizer.decode(result).strip())return result_text78 unmodified linesinput_embeds,vision_hidden_states_dummy,) = self.get_vllm_embedding(model_inputs)# print(input_embeds.shape, f"INPUT_EMBEDS {counter}")counter += 1batch.append(input_embeds)23 unmodified lines# Concatenate the padding tensor to the left of the original tensortensor = torch.cat((padding_tensor, tensor), dim=1)# print(tensor.shape, "UPDATED_SHAPE")# Update the batch with the padded tensorbatch[place] = tensor2 unmodified linesattention_mask.append(to_add)attention_mask = torch.tensor(attention_mask)# print(attention_mask.shape)# print(attention_mask, "ATTENTION")# attention_mask = attention_mask.to(self.device)# padded_tensors = [torch.nn.functional.pad(tensor, (0, 0, 0, max_x - tensor.shape[1])) for tensor in batch]# Step 3: Stack the padded tensors into a single batch# for stuff in batch:# print(stuff.shape, "SHAPE")batch = torch.cat(batch, dim=0)# print(batch.shape)# print(batch)# output_ids = self._decode(input_embeds, tokenizer, **kwargs)if stream:kwargs.pop("decode_text")
modeling_minicpmv.py+12-12