"docs/vscode:/vscode.git/clone" did not exist on "eedaba682fcf0fdbc24816fbae54768b809571b3"
Unverified Commit 12b6880c authored by Sai-Suraj-27's avatar Sai-Suraj-27 Committed by GitHub
Browse files

fix: Fixed raising `TypeError` instead of `ValueError` for invalid type (#32111)

* Raised TypeError instead of ValueError for invalid types.

* Updated formatting using ruff.

* Retrieved few changes.

* Retrieved few changes.

* Updated tests accordingly.
parent d1ec36b9
...@@ -1928,13 +1928,13 @@ class ClapModel(ClapPreTrainedModel): ...@@ -1928,13 +1928,13 @@ class ClapModel(ClapPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, ClapTextConfig): if not isinstance(config.text_config, ClapTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type ClapTextConfig but is of type" "config.text_config is expected to be of type ClapTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.audio_config, ClapAudioConfig): if not isinstance(config.audio_config, ClapAudioConfig):
raise ValueError( raise TypeError(
"config.audio_config is expected to be of type ClapAudioConfig but is of type" "config.audio_config is expected to be of type ClapAudioConfig but is of type"
f" {type(config.audio_config)}." f" {type(config.audio_config)}."
) )
......
...@@ -1119,13 +1119,13 @@ class CLIPModel(CLIPPreTrainedModel): ...@@ -1119,13 +1119,13 @@ class CLIPModel(CLIPPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, CLIPTextConfig): if not isinstance(config.text_config, CLIPTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type CLIPTextConfig but is of type" "config.text_config is expected to be of type CLIPTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, CLIPVisionConfig): if not isinstance(config.vision_config, CLIPVisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type CLIPVisionConfig but is of type" "config.vision_config is expected to be of type CLIPVisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
...@@ -825,13 +825,13 @@ class TFCLIPMainLayer(keras.layers.Layer): ...@@ -825,13 +825,13 @@ class TFCLIPMainLayer(keras.layers.Layer):
super().__init__(**kwargs) super().__init__(**kwargs)
if not isinstance(config.text_config, CLIPTextConfig): if not isinstance(config.text_config, CLIPTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type CLIPTextConfig but is of type" "config.text_config is expected to be of type CLIPTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, CLIPVisionConfig): if not isinstance(config.vision_config, CLIPVisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type CLIPVisionConfig but is of type" "config.vision_config is expected to be of type CLIPVisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
...@@ -924,13 +924,13 @@ class CLIPSegModel(CLIPSegPreTrainedModel): ...@@ -924,13 +924,13 @@ class CLIPSegModel(CLIPSegPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, CLIPSegTextConfig): if not isinstance(config.text_config, CLIPSegTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type CLIPSegTextConfig but is of type" "config.text_config is expected to be of type CLIPSegTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, CLIPSegVisionConfig): if not isinstance(config.vision_config, CLIPSegVisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type CLIPSegVisionConfig but is of type" "config.vision_config is expected to be of type CLIPSegVisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
...@@ -1513,19 +1513,19 @@ class ClvpModelForConditionalGeneration(ClvpPreTrainedModel): ...@@ -1513,19 +1513,19 @@ class ClvpModelForConditionalGeneration(ClvpPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, ClvpEncoderConfig): if not isinstance(config.text_config, ClvpEncoderConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type `ClvpEncoderConfig` but is of type" "config.text_config is expected to be of type `ClvpEncoderConfig` but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.speech_config, ClvpEncoderConfig): if not isinstance(config.speech_config, ClvpEncoderConfig):
raise ValueError( raise TypeError(
"config.speech_config is expected to be of type `ClvpEncoderConfig` but is of type" "config.speech_config is expected to be of type `ClvpEncoderConfig` but is of type"
f" {type(config.speech_config)}." f" {type(config.speech_config)}."
) )
if not isinstance(config.decoder_config, ClvpDecoderConfig): if not isinstance(config.decoder_config, ClvpDecoderConfig):
raise ValueError( raise TypeError(
"config.decoder_config is expected to be of type `ClvpDecoderConfig` but is of type" "config.decoder_config is expected to be of type `ClvpDecoderConfig` but is of type"
f" {type(config.decoder_config)}." f" {type(config.decoder_config)}."
) )
......
...@@ -518,4 +518,4 @@ def convert_to_unicode(text): ...@@ -518,4 +518,4 @@ def convert_to_unicode(text):
elif isinstance(text, bytes): elif isinstance(text, bytes):
return text.decode("utf-8", "ignore") return text.decode("utf-8", "ignore")
else: else:
raise ValueError(f"Unsupported string type: {type(text)}") raise TypeError(f"Unsupported string type: {type(text)}")
...@@ -298,7 +298,7 @@ class DepthAnythingNeck(nn.Module): ...@@ -298,7 +298,7 @@ class DepthAnythingNeck(nn.Module):
List of hidden states from the backbone. List of hidden states from the backbone.
""" """
if not isinstance(hidden_states, (tuple, list)): if not isinstance(hidden_states, (tuple, list)):
raise ValueError("hidden_states should be a tuple or list of tensors") raise TypeError("hidden_states should be a tuple or list of tensors")
if len(hidden_states) != len(self.config.neck_hidden_sizes): if len(hidden_states) != len(self.config.neck_hidden_sizes):
raise ValueError("The number of hidden states should be equal to the number of neck hidden sizes.") raise ValueError("The number of hidden states should be equal to the number of neck hidden sizes.")
......
...@@ -1002,7 +1002,7 @@ class DPTNeck(nn.Module): ...@@ -1002,7 +1002,7 @@ class DPTNeck(nn.Module):
List of hidden states from the backbone. List of hidden states from the backbone.
""" """
if not isinstance(hidden_states, (tuple, list)): if not isinstance(hidden_states, (tuple, list)):
raise ValueError("hidden_states should be a tuple or list of tensors") raise TypeError("hidden_states should be a tuple or list of tensors")
if len(hidden_states) != len(self.config.neck_hidden_sizes): if len(hidden_states) != len(self.config.neck_hidden_sizes):
raise ValueError("The number of hidden states should be equal to the number of neck hidden sizes.") raise ValueError("The number of hidden states should be equal to the number of neck hidden sizes.")
......
...@@ -32,7 +32,7 @@ def _fetch_dims(tree: Union[dict, list, tuple, torch.Tensor]) -> List[Tuple[int, ...@@ -32,7 +32,7 @@ def _fetch_dims(tree: Union[dict, list, tuple, torch.Tensor]) -> List[Tuple[int,
elif isinstance(tree, torch.Tensor): elif isinstance(tree, torch.Tensor):
shapes.append(tree.shape) shapes.append(tree.shape)
else: else:
raise ValueError("Not supported") raise TypeError("Not supported")
return shapes return shapes
...@@ -302,7 +302,7 @@ def chunk_layer( ...@@ -302,7 +302,7 @@ def chunk_layer(
else: else:
out[i : i + chunk_size] = output_chunk out[i : i + chunk_size] = output_chunk
else: else:
raise ValueError("Not supported") raise TypeError("Not supported")
i += chunk_size i += chunk_size
......
...@@ -394,7 +394,7 @@ def map_structure_with_atom_order(in_list: list, first_call: bool = True) -> lis ...@@ -394,7 +394,7 @@ def map_structure_with_atom_order(in_list: list, first_call: bool = True) -> lis
elif isinstance(in_list[i], str): elif isinstance(in_list[i], str):
in_list[i] = atom_order[in_list[i]] in_list[i] = atom_order[in_list[i]]
else: else:
raise ValueError("Unexpected type when mapping nested lists!") raise TypeError("Unexpected type when mapping nested lists!")
return in_list return in_list
......
...@@ -134,7 +134,7 @@ def tree_map(fn, tree, leaf_type): ...@@ -134,7 +134,7 @@ def tree_map(fn, tree, leaf_type):
return fn(tree) return fn(tree)
else: else:
print(type(tree)) print(type(tree))
raise ValueError("Not supported") raise TypeError("Not supported")
tensor_tree_map = partial(tree_map, leaf_type=torch.Tensor) tensor_tree_map = partial(tree_map, leaf_type=torch.Tensor)
...@@ -1181,19 +1181,19 @@ class FlavaModel(FlavaPreTrainedModel): ...@@ -1181,19 +1181,19 @@ class FlavaModel(FlavaPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, FlavaTextConfig): if not isinstance(config.text_config, FlavaTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type FlavaTextConfig but is of type" "config.text_config is expected to be of type FlavaTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.image_config, FlavaImageConfig): if not isinstance(config.image_config, FlavaImageConfig):
raise ValueError( raise TypeError(
"config.image_config is expected to be of type FlavaImageConfig but is of type" "config.image_config is expected to be of type FlavaImageConfig but is of type"
f" {type(config.image_config)}." f" {type(config.image_config)}."
) )
if not isinstance(config.multimodal_config, FlavaMultimodalConfig): if not isinstance(config.multimodal_config, FlavaMultimodalConfig):
raise ValueError( raise TypeError(
"config.multimodal_config is expected to be of type FlavaMultimodalConfig but " "config.multimodal_config is expected to be of type FlavaMultimodalConfig but "
+ f"is of type {type(config.multimodal_config)}." + f"is of type {type(config.multimodal_config)}."
) )
......
...@@ -1302,13 +1302,13 @@ class GroupViTModel(GroupViTPreTrainedModel): ...@@ -1302,13 +1302,13 @@ class GroupViTModel(GroupViTPreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, GroupViTTextConfig): if not isinstance(config.text_config, GroupViTTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type GroupViTTextConfig but is of type" "config.text_config is expected to be of type GroupViTTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, GroupViTVisionConfig): if not isinstance(config.vision_config, GroupViTVisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type GroupViTVisionConfig but is of type" "config.vision_config is expected to be of type GroupViTVisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
...@@ -1443,13 +1443,13 @@ class TFGroupViTMainLayer(keras.layers.Layer): ...@@ -1443,13 +1443,13 @@ class TFGroupViTMainLayer(keras.layers.Layer):
super().__init__(**kwargs) super().__init__(**kwargs)
if not isinstance(config.text_config, GroupViTTextConfig): if not isinstance(config.text_config, GroupViTTextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type GroupViTTextConfig but is of type" "config.text_config is expected to be of type GroupViTTextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, GroupViTVisionConfig): if not isinstance(config.vision_config, GroupViTVisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type GroupViTVisionConfig but is of type" "config.vision_config is expected to be of type GroupViTVisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
...@@ -513,7 +513,7 @@ class LlavaNextImageProcessor(BaseImageProcessor): ...@@ -513,7 +513,7 @@ class LlavaNextImageProcessor(BaseImageProcessor):
List[np.array]: A list of NumPy arrays containing the processed image patches. List[np.array]: A list of NumPy arrays containing the processed image patches.
""" """
if not isinstance(grid_pinpoints, list): if not isinstance(grid_pinpoints, list):
raise ValueError("grid_pinpoints must be a list of possible resolutions.") raise TypeError("grid_pinpoints must be a list of possible resolutions.")
possible_resolutions = grid_pinpoints possible_resolutions = grid_pinpoints
......
...@@ -60,12 +60,12 @@ def get_anyres_image_grid_shape(image_size, grid_pinpoints, patch_size): ...@@ -60,12 +60,12 @@ def get_anyres_image_grid_shape(image_size, grid_pinpoints, patch_size):
tuple: The shape of the image patch grid in the format (width, height). tuple: The shape of the image patch grid in the format (width, height).
""" """
if not isinstance(grid_pinpoints, list): if not isinstance(grid_pinpoints, list):
raise ValueError("grid_pinpoints should be a list of tuples or lists") raise TypeError("grid_pinpoints should be a list of tuples or lists")
# ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate # ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate
if not isinstance(image_size, (list, tuple)): if not isinstance(image_size, (list, tuple)):
if not isinstance(image_size, (torch.Tensor, np.ndarray)): if not isinstance(image_size, (torch.Tensor, np.ndarray)):
raise ValueError( raise TypeError(
f"image_size invalid type: {type(image_size)} not valid, should be either list, tuple, np.ndarray or tensor" f"image_size invalid type: {type(image_size)} not valid, should be either list, tuple, np.ndarray or tensor"
) )
image_size = image_size.tolist() image_size = image_size.tolist()
...@@ -91,12 +91,12 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int): ...@@ -91,12 +91,12 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int):
int: the number of patches int: the number of patches
""" """
if not isinstance(grid_pinpoints, list): if not isinstance(grid_pinpoints, list):
raise ValueError("grid_pinpoints should be a list of tuples or lists") raise TypeError("grid_pinpoints should be a list of tuples or lists")
# ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate # ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate
if not isinstance(image_size, (list, tuple)): if not isinstance(image_size, (list, tuple)):
if not isinstance(image_size, (torch.Tensor, np.ndarray)): if not isinstance(image_size, (torch.Tensor, np.ndarray)):
raise ValueError(f"image_size invalid type {type(image_size)} with value {image_size}") raise TypeError(f"image_size invalid type {type(image_size)} with value {image_size}")
image_size = image_size.tolist() image_size = image_size.tolist()
best_resolution = select_best_resolution(image_size, grid_pinpoints) best_resolution = select_best_resolution(image_size, grid_pinpoints)
......
...@@ -66,12 +66,12 @@ def get_anyres_image_grid_shape(image_size, grid_pinpoints, patch_size): ...@@ -66,12 +66,12 @@ def get_anyres_image_grid_shape(image_size, grid_pinpoints, patch_size):
tuple: The shape of the image patch grid in the format (width, height). tuple: The shape of the image patch grid in the format (width, height).
""" """
if not isinstance(grid_pinpoints, list): if not isinstance(grid_pinpoints, list):
raise ValueError("grid_pinpoints should be a list of tuples or lists") raise TypeError("grid_pinpoints should be a list of tuples or lists")
# ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate # ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate
if not isinstance(image_size, (list, tuple)): if not isinstance(image_size, (list, tuple)):
if not isinstance(image_size, (torch.Tensor, np.ndarray)): if not isinstance(image_size, (torch.Tensor, np.ndarray)):
raise ValueError( raise TypeError(
f"image_size invalid type: {type(image_size)} not valid, should be either list, tuple, np.ndarray or tensor" f"image_size invalid type: {type(image_size)} not valid, should be either list, tuple, np.ndarray or tensor"
) )
image_size = image_size.tolist() image_size = image_size.tolist()
...@@ -97,12 +97,12 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int): ...@@ -97,12 +97,12 @@ def image_size_to_num_patches(image_size, grid_pinpoints, patch_size: int):
int: the number of patches int: the number of patches
""" """
if not isinstance(grid_pinpoints, list): if not isinstance(grid_pinpoints, list):
raise ValueError("grid_pinpoints should be a list of tuples or lists") raise TypeError("grid_pinpoints should be a list of tuples or lists")
# ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate # ! VERY IMPORTANT if image_size is tensor, must convert to into tuple, otherwise it will cause wrong calculate
if not isinstance(image_size, (list, tuple)): if not isinstance(image_size, (list, tuple)):
if not isinstance(image_size, (torch.Tensor, np.ndarray)): if not isinstance(image_size, (torch.Tensor, np.ndarray)):
raise ValueError(f"image_size invalid type {type(image_size)} with value {image_size}") raise TypeError(f"image_size invalid type {type(image_size)} with value {image_size}")
image_size = image_size.tolist() image_size = image_size.tolist()
best_resolution = select_best_resolution(image_size, grid_pinpoints) best_resolution = select_best_resolution(image_size, grid_pinpoints)
......
...@@ -889,7 +889,7 @@ class LukeTokenizer(PreTrainedTokenizer): ...@@ -889,7 +889,7 @@ class LukeTokenizer(PreTrainedTokenizer):
def _check_entity_input_format(self, entities: Optional[EntityInput], entity_spans: Optional[EntitySpanInput]): def _check_entity_input_format(self, entities: Optional[EntityInput], entity_spans: Optional[EntitySpanInput]):
if not isinstance(entity_spans, list): if not isinstance(entity_spans, list):
raise ValueError("entity_spans should be given as a list") raise TypeError("entity_spans should be given as a list")
elif len(entity_spans) > 0 and not isinstance(entity_spans[0], tuple): elif len(entity_spans) > 0 and not isinstance(entity_spans[0], tuple):
raise ValueError( raise ValueError(
"entity_spans should be given as a list of tuples containing the start and end character indices" "entity_spans should be given as a list of tuples containing the start and end character indices"
......
...@@ -721,7 +721,7 @@ class MLukeTokenizer(PreTrainedTokenizer): ...@@ -721,7 +721,7 @@ class MLukeTokenizer(PreTrainedTokenizer):
# Copied from transformers.models.luke.tokenization_luke.LukeTokenizer._check_entity_input_format # Copied from transformers.models.luke.tokenization_luke.LukeTokenizer._check_entity_input_format
def _check_entity_input_format(self, entities: Optional[EntityInput], entity_spans: Optional[EntitySpanInput]): def _check_entity_input_format(self, entities: Optional[EntityInput], entity_spans: Optional[EntitySpanInput]):
if not isinstance(entity_spans, list): if not isinstance(entity_spans, list):
raise ValueError("entity_spans should be given as a list") raise TypeError("entity_spans should be given as a list")
elif len(entity_spans) > 0 and not isinstance(entity_spans[0], tuple): elif len(entity_spans) > 0 and not isinstance(entity_spans[0], tuple):
raise ValueError( raise ValueError(
"entity_spans should be given as a list of tuples containing the start and end character indices" "entity_spans should be given as a list of tuples containing the start and end character indices"
......
...@@ -1015,13 +1015,13 @@ class Owlv2Model(Owlv2PreTrainedModel): ...@@ -1015,13 +1015,13 @@ class Owlv2Model(Owlv2PreTrainedModel):
super().__init__(config) super().__init__(config)
if not isinstance(config.text_config, Owlv2TextConfig): if not isinstance(config.text_config, Owlv2TextConfig):
raise ValueError( raise TypeError(
"config.text_config is expected to be of type Owlv2TextConfig but is of type" "config.text_config is expected to be of type Owlv2TextConfig but is of type"
f" {type(config.text_config)}." f" {type(config.text_config)}."
) )
if not isinstance(config.vision_config, Owlv2VisionConfig): if not isinstance(config.vision_config, Owlv2VisionConfig):
raise ValueError( raise TypeError(
"config.vision_config is expected to be of type Owlv2VisionConfig but is of type" "config.vision_config is expected to be of type Owlv2VisionConfig but is of type"
f" {type(config.vision_config)}." f" {type(config.vision_config)}."
) )
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment