Update modeling_minicpmo.py

#18
Files changed (1) hide show
  1. modeling_minicpmo.py +31 -9
modeling_minicpmo.py CHANGED
@@ -762,17 +762,39 @@ class MiniCPMO(MiniCPMOPreTrainedModel):
762
  for i in range(bs):
763
  audio_embs = audio_embeddings[i]
764
  bounds = audio_bounds[i]
 
 
 
 
 
 
 
765
  for embs, bound in zip(audio_embs, bounds):
766
- audio_indices = torch.arange(bound[0], bound[1], dtype=torch.long).to(
767
- input_embeddings.device
 
768
  )
769
-
770
- if embs.shape[0] != len(audio_indices):
771
- raise ValueError(
772
- f"Shape mismatch: Trying to assign embeddings of shape {embs.shape} "
773
- f"to input indices of length {len(audio_indices)}"
774
- )
775
- input_embeddings[i, audio_indices] = embs.to(input_embeddings.dtype)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
776
  elif self.training:
777
  for i in range(bs):
778
  # dummy audio_embedings
 
762
  for i in range(bs):
763
  audio_embs = audio_embeddings[i]
764
  bounds = audio_bounds[i]
765
+
766
+ one_to_one_match = len(audio_embs) == len(bounds) and all(
767
+ embs.shape[0] == int(bound[1] - bound[0])
768
+ for embs, bound in zip(audio_embs, bounds)
769
+ )
770
+
771
+ if one_to_one_match:
772
  for embs, bound in zip(audio_embs, bounds):
773
+ input_embeddings[i, bound[0] : bound[1]] = embs.to(
774
+ device=input_embeddings.device,
775
+ dtype=input_embeddings.dtype,
776
  )
777
+ else:
778
+ flat_audio_embs = torch.cat(audio_embs, dim=0).to(
779
+ device=input_embeddings.device,
780
+ dtype=input_embeddings.dtype,
781
+ )
782
+
783
+ total_bound_len = sum(int(bound[1] - bound[0]) for bound in bounds)
784
+
785
+ if flat_audio_embs.shape[0] != total_bound_len:
786
+ raise ValueError(
787
+ f"Audio total length mismatch: {flat_audio_embs.shape[0]} != {total_bound_len}"
788
+ )
789
+
790
+ offset = 0
791
+ for bound in bounds:
792
+ audio_len = int(bound[1] - bound[0])
793
+ input_embeddings[i, bound[0] : bound[1]] = flat_audio_embs[
794
+ offset : offset + audio_len
795
+ ]
796
+ offset += audio_len
797
+
798
  elif self.training:
799
  for i in range(bs):
800
  # dummy audio_embedings