From 0aaf124fb934c314fe28fa9d1fa935c39d69f911 Mon Sep 17 00:00:00 2001 From: Taha Akbari <77528692+Taha1506@users.noreply.github.com> Date: Fri, 10 Jan 2025 18:53:54 +0330 Subject: [PATCH] Added error when sequence length is bigger than max_position_embeddings (#32156) * Added error when sequence length is bigger than max_position_embeddings * Fixed formatting * Fixed bug * Changed copies to match * Fixed bug * Applied suggestions * Removed redundant code * Fixed bugs * Bug fix * Bug fix * Added requested Changes * Fixed bug * Fixed unwanted change * Fixed unwanated changes * Fixed formatting --- src/transformers/models/blip/modeling_blip.py | 7 +++++++ src/transformers/models/clip/modeling_clip.py | 7 +++++++ src/transformers/models/clipseg/modeling_clipseg.py | 7 +++++++ src/transformers/models/groupvit/modeling_groupvit.py | 7 +++++++ src/transformers/models/siglip/modeling_siglip.py | 7 +++++++ src/transformers/models/x_clip/modeling_x_clip.py | 7 +++++++ 6 files changed, 42 insertions(+) diff --git a/src/transformers/models/blip/modeling_blip.py b/src/transformers/models/blip/modeling_blip.py index 27dbbee6c671ee..154d6eb9f8225d 100644 --- a/src/transformers/models/blip/modeling_blip.py +++ b/src/transformers/models/blip/modeling_blip.py @@ -309,6 +309,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length] diff --git a/src/transformers/models/clip/modeling_clip.py b/src/transformers/models/clip/modeling_clip.py index 0bd9c9c0abce2f..1818c6bb0963c0 100644 --- a/src/transformers/models/clip/modeling_clip.py +++ b/src/transformers/models/clip/modeling_clip.py @@ -277,6 +277,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length] diff --git a/src/transformers/models/clipseg/modeling_clipseg.py b/src/transformers/models/clipseg/modeling_clipseg.py index f3b963070dfb29..2d88746b771370 100644 --- a/src/transformers/models/clipseg/modeling_clipseg.py +++ b/src/transformers/models/clipseg/modeling_clipseg.py @@ -244,6 +244,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length] diff --git a/src/transformers/models/groupvit/modeling_groupvit.py b/src/transformers/models/groupvit/modeling_groupvit.py index c2f38ef3dcbe59..889b200552d51a 100644 --- a/src/transformers/models/groupvit/modeling_groupvit.py +++ b/src/transformers/models/groupvit/modeling_groupvit.py @@ -446,6 +446,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length] diff --git a/src/transformers/models/siglip/modeling_siglip.py b/src/transformers/models/siglip/modeling_siglip.py index 819f8c487f1d50..d8a317493a1013 100644 --- a/src/transformers/models/siglip/modeling_siglip.py +++ b/src/transformers/models/siglip/modeling_siglip.py @@ -340,6 +340,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length] diff --git a/src/transformers/models/x_clip/modeling_x_clip.py b/src/transformers/models/x_clip/modeling_x_clip.py index bce1ea02e7b9e0..4cf2af1da5c0ef 100644 --- a/src/transformers/models/x_clip/modeling_x_clip.py +++ b/src/transformers/models/x_clip/modeling_x_clip.py @@ -203,6 +203,13 @@ def forward( inputs_embeds: Optional[torch.FloatTensor] = None, ) -> torch.Tensor: seq_length = input_ids.shape[-1] if input_ids is not None else inputs_embeds.shape[-2] + max_position_embedding = self.position_embedding.weight.shape[0] + + if seq_length > max_position_embedding: + raise ValueError( + f"Sequence length must be less than max_position_embeddings (got `sequence length`: " + f"{seq_length} and max_position_embeddings: {max_position_embedding}" + ) if position_ids is None: position_ids = self.position_ids[:, :seq_length]