

'''simple docstring'''

import inspect
import unittest

from transformers import ViTHybridConfig
from transformers.testing_utils import require_accelerate, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch
				from torch import nn

				from transformers import ViTHybridForImageClassification, ViTHybridImageProcessor, ViTHybridModel
				from transformers.models.vit_hybrid.modeling_vit_hybrid import VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image







class   UpperCamelCase__    :


		"""simple docstring"""

		def __init__( self    ,			snake_case    ,			snake_case=1_3    ,			snake_case=6_4    ,			snake_case=2    ,			snake_case=3    ,			snake_case=True    ,			snake_case=True    ,			snake_case=3_2    ,			snake_case=5    ,			snake_case=4    ,			snake_case=3_7    ,			snake_case="gelu"    ,			snake_case=0.1    ,			snake_case=0.1    ,			snake_case=1_0    ,			snake_case=0.02    ,			snake_case=[1, 1_6, 4, 4]    ,			snake_case=None    ,			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]   					=   parent
							UpperCAmelCase		:							Union[str, Any]   					=   batch_size
							UpperCAmelCase		:							Any   					=   image_size
							UpperCAmelCase		:							List[str]   					=   patch_size
							UpperCAmelCase		:							Dict   					=   num_channels
							UpperCAmelCase		:							int   					=   is_training
							UpperCAmelCase		:							List[str]   					=   use_labels
							UpperCAmelCase		:							Optional[int]   					=   hidden_size
							UpperCAmelCase		:							Tuple   					=   num_hidden_layers
							UpperCAmelCase		:							Any   					=   num_attention_heads
							UpperCAmelCase		:							Optional[int]   					=   intermediate_size
							UpperCAmelCase		:							List[Any]   					=   hidden_act
							UpperCAmelCase		:							Union[str, Any]   					=   hidden_dropout_prob
							UpperCAmelCase		:							List[Any]   					=   attention_probs_dropout_prob
							UpperCAmelCase		:							Optional[int]   					=   type_sequence_label_size
							UpperCAmelCase		:							Optional[Any]   					=   initializer_range
							UpperCAmelCase		:							Dict   					=   scope
							UpperCAmelCase		:							Union[str, Any]   					=   backbone_featmap_shape

							# in ViT hybrid, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
							# the number of patches is based on the feature map of the backbone, which by default uses an output stride
							# of 32, which means that the feature map has a spatial resolution of 1/32 of the input image size
							UpperCAmelCase		:							List[str]   					=   (self.image_size // 3_2) ** 2
							UpperCAmelCase		:							Optional[Any]   					=   num_patches + 1







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Union[str, Any]   					=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

							UpperCAmelCase		:							List[str]   					=   None
							if self.use_labels:
												UpperCAmelCase		:							Dict   					=   ids_tensor([self.batch_size]    ,			self.type_sequence_label_size			)

							UpperCAmelCase		:							Tuple   					=   self.get_config()

							return config, pixel_values, labels







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   {
							    "global_padding": "same",
							    "layer_type": "bottleneck",
							    "depths": [3, 4, 9],
							    "out_features": ["stage1", "stage2", "stage3"],
							    "embedding_dynamic_padding": True,
							    "hidden_sizes": [4, 8, 1_6, 3_2],
							    "num_groups": 2,
							}

							return ViTHybridConfig(
							    image_size=self.image_size    ,			patch_size=self.patch_size    ,			num_channels=self.num_channels    ,			hidden_size=self.hidden_size    ,			num_hidden_layers=self.num_hidden_layers    ,			num_attention_heads=self.num_attention_heads    ,			intermediate_size=self.intermediate_size    ,			hidden_act=self.hidden_act    ,			hidden_dropout_prob=self.hidden_dropout_prob    ,			attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,			is_decoder=_lowercase    ,			initializer_range=self.initializer_range    ,			backbone_featmap_shape=self.backbone_featmap_shape    ,			backbone_config=_lowercase    ,			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   ViTHybridModel(config=_lowercase			)
							model.to(_lowercase			)
							model.eval()
							UpperCAmelCase		:							Union[str, Any]   					=   model(_lowercase			)
							self.parent.assertEqual(result.last_hidden_state.shape    ,			(self.batch_size, self.seq_length, self.hidden_size)			)







		def       A_       ( self    ,			snake_case    ,			snake_case    ,			snake_case			):


							'''simple docstring'''

							UpperCAmelCase		:							str   					=   self.type_sequence_label_size
							UpperCAmelCase		:							List[Any]   					=   ViTHybridForImageClassification(_lowercase			)
							model.to(_lowercase			)
							model.eval()
							UpperCAmelCase		:							Union[str, Any]   					=   model(_lowercase    ,			labels=_lowercase			)
							self.parent.assertEqual(result.logits.shape    ,			(self.batch_size, self.type_sequence_label_size)			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   self.prepare_config_and_inputs()
							UpperCAmelCase					, UpperCAmelCase					, UpperCAmelCase		:							Union[str, Any]   					=   config_and_inputs
							UpperCAmelCase		:							int   					=   {"pixel_values": pixel_values}
							return config, inputs_dict







@require_torch
class   UpperCamelCase__    (   lowerCamelCase_							,							lowerCamelCase_							,							unittest.TestCase   ):


		"""simple docstring"""

		SCREAMING_SNAKE_CASE__  :	List[Any]   =							(ViTHybridModel, ViTHybridForImageClassification) if is_torch_available() else ()
		SCREAMING_SNAKE_CASE__  :	Tuple   =							(
		    {"feature-extraction": ViTHybridModel, "image-classification": ViTHybridForImageClassification}
		    if is_torch_available()
		    else {}
		)
		SCREAMING_SNAKE_CASE__  :	Any   =							False
		SCREAMING_SNAKE_CASE__  :	Optional[int]   =							False
		SCREAMING_SNAKE_CASE__  :	int   =							False







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[int]   					=   ViTHybridModelTester(self			)
							UpperCAmelCase		:							Any   					=   ConfigTester(self    ,			config_class=_lowercase    ,			has_text_modality=_lowercase    ,			hidden_size=3_7			)







		def       A_       ( self			):


							'''simple docstring'''

							self.config_tester.run_common_tests()







		@unittest.skip(reason="ViT does not use inputs_embeds"			)
		def       A_       ( self			):


							'''simple docstring'''

							pass







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							Dict   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Any   					=   model_class(_lowercase			)
												self.assertIsInstance(model.get_input_embeddings()    ,			(nn.Module)			)
												UpperCAmelCase		:							List[str]   					=   model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(_lowercase    ,			nn.Linear			)			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							Tuple   					=   self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
												UpperCAmelCase		:							int   					=   model_class(_lowercase			)
												UpperCAmelCase		:							Any   					=   inspect.signature(model.forward			)
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												UpperCAmelCase		:							Optional[Any]   					=   [*signature.parameters.keys()]

												UpperCAmelCase		:							str   					=   ["pixel_values"]
												self.assertListEqual(arg_names[:1]    ,			_lowercase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							List[str]   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowercase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Dict   					=   self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_image_classification(*_lowercase			)







		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase					, UpperCAmelCase		:							Optional[int]   					=   self.model_tester.prepare_config_and_inputs_for_common()

							UpperCAmelCase		:							int   					=   _config_zero_init(_lowercase			)
							for model_class in self.all_model_classes:
												UpperCAmelCase		:							Union[str, Any]   					=   model_class(config=_lowercase			)
												# Skip the check for the backbone
												for name, module in model.named_modules():
																	if module.__class__.__name__ == "ViTHybridPatchEmbeddings":
																						UpperCAmelCase		:							Optional[Any]   					=   [f"{name}.{key}" for key in module.state_dict().keys()]
																						break

												for name, param in model.named_parameters():
																	if param.requires_grad:
																						if name in backbone_params:
																											continue
																						self.assertIn(
																						    ((param.data.mean() * 1e9).round() / 1e9).item()    ,			[0.0, 1.0]    ,			msg=f"Parameter {name} of model {model_class} seems not properly initialized"    ,			)







		@slow
		def       A_       ( self			):


							'''simple docstring'''

							for model_name in VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												UpperCAmelCase		:							str   					=   ViTHybridModel.from_pretrained(_lowercase			)
												self.assertIsNotNone(_lowercase			)





def       lowercase					(     ):



					'''simple docstring'''







					UpperCAmelCase		:							Dict   					=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
					return image







@require_torch
@require_vision
class   UpperCamelCase__    (   unittest.TestCase   ):


		"""simple docstring"""

		@cached_property
		def       A_       ( self			):


							'''simple docstring'''

							return (
							    ViTHybridImageProcessor.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]			)
							    if is_vision_available()
							    else None
							)







		@slow
		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							int   					=   ViTHybridForImageClassification.from_pretrained(VIT_HYBRID_PRETRAINED_MODEL_ARCHIVE_LIST[0]			).to(
							    _lowercase			)

							UpperCAmelCase		:							Any   					=   self.default_image_processor
							UpperCAmelCase		:							Union[str, Any]   					=   prepare_img()
							UpperCAmelCase		:							Tuple   					=   image_processor(images=_lowercase    ,			return_tensors="pt"			).to(_lowercase			)

							# forward pass
							with torch.no_grad():
												UpperCAmelCase		:							Any   					=   model(**_lowercase			)

							# verify the logits
							UpperCAmelCase		:							Tuple   					=   torch.Size((1, 1_0_0_0)			)
							self.assertEqual(outputs.logits.shape    ,			_lowercase			)

							UpperCAmelCase		:							Optional[Any]   					=   torch.tensor([-1.9090, -0.4993, -0.2389]			).to(_lowercase			)

							self.assertTrue(torch.allclose(outputs.logits[0, :3]    ,			_lowercase    ,			atol=1e-4			)			)







		@slow
		@require_accelerate
		def       A_       ( self			):


							'''simple docstring'''

							UpperCAmelCase		:							Optional[Any]   					=   ViTHybridImageProcessor.from_pretrained("google/vit-hybrid-base-bit-384"			)
							UpperCAmelCase		:							List[Any]   					=   ViTHybridForImageClassification.from_pretrained("google/vit-hybrid-base-bit-384"    ,			device_map="auto"			)

							UpperCAmelCase		:							List[str]   					=   prepare_img()

							UpperCAmelCase		:							Any   					=   image_processor(images=_lowercase    ,			return_tensors="pt"			)
							UpperCAmelCase		:							Any   					=   model(**_lowercase			)
							UpperCAmelCase		:							List[Any]   					=   outputs.logits
							# model predicts one of the 1000 ImageNet classes
							UpperCAmelCase		:							Dict   					=   logits.argmax(-1			).item()

							self.assertTrue(model.config.idalabel[predicted_class_idx]    ,			"tabby, tabby cat"			)

# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ..models.auto import AutoModelForVisionaSeq
from ..utils import requires_backends
from .base import PipelineTool


if TYPE_CHECKING:
   from PIL import Image







class __snake_case				(						lowerCamelCase_		):
   lowerCAmelCase_														=							"Salesforce/blip-image-captioning-base"
   lowerCAmelCase_														=							(
       "This is a tool that generates a description of an image. It takes an input named `image` which should be the "
       "image to caption, and returns a text that contains the description in English."
   )
   lowerCAmelCase_														=							"image_captioner"
   lowerCAmelCase_														=							AutoModelForVisionaSeq

   lowerCAmelCase_														=							["image"]
   lowerCAmelCase_														=							["text"]




   def __init__(    self			:				List[Any]       ,	*_lowercase			:				Optional[int]       ,	**_lowercase			:				Union[str, Any]      ):



     """simple docstring"""






     requires_backends(self       ,	["""vision"""]      )
     super().__init__(*_lowercase       ,	**_lowercase      )




   def 	__a						(    self			:				Tuple       ,	_lowercase			:				"Image"      ):



     """simple docstring"""






     return self.pre_processor(images=_lowercase       ,	return_tensors="""pt"""      )




   def 	__a						(    self			:				Union[str, Any]       ,	_lowercase			:				Optional[int]      ):



     """simple docstring"""






     return self.model.generate(**_lowercase      )




   def 	__a						(    self			:				int       ,	_lowercase			:				Any      ):



     """simple docstring"""






     return self.pre_processor.batch_decode(_lowercase       ,	skip_special_tokens=_lowercase      )[0].strip()
