andrewqian123 commited on
Commit
4ca6160
·
verified ·
1 Parent(s): 21f37b3

Update modeling_minicpmv.py

Browse files
Files changed (1) hide show
  1. modeling_minicpmv.py +16 -17
modeling_minicpmv.py CHANGED
@@ -297,25 +297,24 @@ class MiniCPMV(MiniCPMVPreTrainedModel):
297
  vector_reshaped = pad_embedding_vector.view(1, 1, 4096)
298
 
299
  for place, tensor in enumerate(batch):
300
- to_add = []
301
- for pl in range(0, max_x - tensor.shape[1]):
302
- to_add.append(0)
303
- bound = 0
304
-
305
- for pl in range(max_x - tensor.shape[1], max_x):
306
- if bound == 0:
307
- bound = pl
308
-
309
- #print("here")
310
- tensor = torch.cat((vector_reshaped, tensor), dim=1)
311
- to_add.append(1)
312
- # tensor = tensor.to(self.device)
313
- # if bound != 0:
314
- # print(tensor[0,bound-1], " BOUND")
315
- # print(tensor[0,bound], " OTHERSIDE")
316
- # print(pad_embedding_vector, " VECTOR")
317
  print(tensor.shape, "UPDATED_SHAPE")
 
 
318
  batch[place] = tensor
 
 
319
  attention_mask.append(to_add)
320
 
321
  attention_mask = torch.tensor(attention_mask)
 
297
  vector_reshaped = pad_embedding_vector.view(1, 1, 4096)
298
 
299
  for place, tensor in enumerate(batch):
300
+ # Calculate how much padding is needed on the left
301
+ padding_needed = max_x - tensor.shape[1]
302
+
303
+ # Create the list for the attention mask, marking the padded tokens
304
+ to_add = [0] * padding_needed + [1] * tensor.shape[1]
305
+
306
+ # Create the padding tensor of the correct size
307
+ padding_tensor = vector_reshaped.expand(tensor.shape[0], padding_needed, tensor.shape[2])
308
+
309
+ # Concatenate the padding tensor to the left of the original tensor
310
+ tensor = torch.cat((padding_tensor, tensor), dim=1)
311
+
 
 
 
 
 
312
  print(tensor.shape, "UPDATED_SHAPE")
313
+
314
+ # Update the batch with the padded tensor
315
  batch[place] = tensor
316
+
317
+ # Append the attention mask for this tensor
318
  attention_mask.append(to_add)
319
 
320
  attention_mask = torch.tensor(attention_mask)