

"""simple docstring"""






import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask


if is_torch_available():
						import torch

						from transformers import BlipTextModel
						from transformers.models.blip.modeling_blip import BLIP_PRETRAINED_MODEL_ARCHIVE_LIST






class     _UpperCAmelCase			:
			def __init__(		self      ,		_A      ,		_A=12      ,		_A=7      ,		_A=True      ,		_A=True      ,		_A=True      ,		_A=99      ,		_A=32      ,		_A=32      ,		_A=5      ,		_A=4      ,		_A=37      ,		_A=0.1      ,		_A=0.1      ,		_A=5_12      ,		_A=0.02      ,		_A=0      ,		_A=None      ,		)    ->    List[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							List[Any]									=					parent
				_UpperCAmelCase    :							Optional[int]									=					batch_size
				_UpperCAmelCase    :							Dict									=					seq_length
				_UpperCAmelCase    :							List[Any]									=					is_training
				_UpperCAmelCase    :							str									=					use_input_mask
				_UpperCAmelCase    :							Any									=					use_labels
				_UpperCAmelCase    :							List[Any]									=					vocab_size
				_UpperCAmelCase    :							Union[str, Any]									=					hidden_size
				_UpperCAmelCase    :							Dict									=					projection_dim
				_UpperCAmelCase    :							Tuple									=					num_hidden_layers
				_UpperCAmelCase    :							List[Any]									=					num_attention_heads
				_UpperCAmelCase    :							Dict									=					intermediate_size
				_UpperCAmelCase    :							str									=					dropout
				_UpperCAmelCase    :							List[str]									=					attention_dropout
				_UpperCAmelCase    :							Dict									=					max_position_embeddings
				_UpperCAmelCase    :							Optional[int]									=					initializer_range
				_UpperCAmelCase    :							List[str]									=					scope
				_UpperCAmelCase    :							Optional[Any]									=					bos_token_id
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					ids_tensor([self.batch_size, self.seq_length]      ,		self.vocab_size     )

				_UpperCAmelCase    :							Any									=					None
				if self.use_input_mask:
					_UpperCAmelCase    :							Optional[Any]									=					random_attention_mask([self.batch_size, self.seq_length]     )

				if input_mask is not None:
					_UpperCAmelCase						,   _UpperCAmelCase    :							str									=					input_mask.shape
					_UpperCAmelCase    :							List[Any]									=					np.random.randint(1      ,		seq_length - 1      ,		size=(batch_size,)     )
					for batch_idx, start_index in enumerate(_A     ):
						_UpperCAmelCase    :							str									=					1
						_UpperCAmelCase    :							List[Any]									=					0

				_UpperCAmelCase    :							List[str]									=					self.get_config()

				return config, input_ids, input_mask
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				return BlipTextConfig(
				    vocab_size=self.vocab_size      ,		hidden_size=self.hidden_size      ,		projection_dim=self.projection_dim      ,		num_hidden_layers=self.num_hidden_layers      ,		num_attention_heads=self.num_attention_heads      ,		intermediate_size=self.intermediate_size      ,		dropout=self.dropout      ,		attention_dropout=self.attention_dropout      ,		max_position_embeddings=self.max_position_embeddings      ,		initializer_range=self.initializer_range      ,		bos_token_id=self.bos_token_id      ,		)
			def 							__snake_case (		self      ,		_A      ,		_A      ,		_A     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Any									=					BlipTextModel(config=_A     )
				model.to(_A     )
				model.eval()
				with torch.no_grad():
					_UpperCAmelCase    :							Optional[int]									=					model(_A      ,		attention_mask=_A     )
					_UpperCAmelCase    :							Any									=					model(_A     )
				self.parent.assertEqual(result.last_hidden_state.shape      ,		(self.batch_size, self.seq_length, self.hidden_size)     )
				self.parent.assertEqual(result.pooler_output.shape      ,		(self.batch_size, self.hidden_size)     )
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					self.prepare_config_and_inputs()
				_UpperCAmelCase						,   _UpperCAmelCase						,   _UpperCAmelCase    :							Optional[int]									=					config_and_inputs
				_UpperCAmelCase    :							int									=					{"""input_ids""": input_ids, """attention_mask""": input_mask}
				return config, inputs_dict






@require_torch
class     _UpperCAmelCase			(		__a    ,     unittest.TestCase):
			__a :       List[Any]									=							(BlipTextModel,) if is_torch_available() else ()
			__a :       Union[str, Any]									=							False
			__a :       Tuple									=							False
			__a :       Dict									=							False
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[Any]									=					BlipTextModelTester(self     )
				_UpperCAmelCase    :							List[str]									=					ConfigTester(self      ,		config_class=_A      ,		hidden_size=37     )
			def 							__snake_case (		self     )    ->    int:






				'''simple docstring'''







				self.config_tester.run_common_tests()
			def 							__snake_case (		self     )    ->    Tuple:






				'''simple docstring'''







				_UpperCAmelCase    :							Dict									=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_A     )
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				pass
			@unittest.skip(reason="""Blip does not use inputs_embeds"""     )
			def 							__snake_case (		self     )    ->    Optional[int]:






				'''simple docstring'''







				pass
			@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""     )
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				pass
			@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""     )
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				pass
			@slow
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				for model_name in BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					_UpperCAmelCase    :							Any									=					BlipTextModel.from_pretrained(_A     )
					self.assertIsNotNone(_A     )
			def 							__snake_case (		self     )    ->    Union[str, Any]:






				'''simple docstring'''







				super().test_pt_tf_model_equivalence(allow_missing_keys=_A     )








"""simple docstring"""






import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import ViTImageProcessor






class     _UpperCAmelCase			(		unittest.TestCase):
			def __init__(		self      ,		_A      ,		_A=7      ,		_A=3      ,		_A=18      ,		_A=30      ,		_A=4_00      ,		_A=True      ,		_A=None      ,		_A=True      ,		_A=[0.5, 0.5, 0.5]      ,		_A=[0.5, 0.5, 0.5]      ,		)    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[Any]									=					size if size is not None else {"""height""": 18, """width""": 18}
				_UpperCAmelCase    :							Tuple									=					parent
				_UpperCAmelCase    :							Tuple									=					batch_size
				_UpperCAmelCase    :							Optional[int]									=					num_channels
				_UpperCAmelCase    :							Any									=					image_size
				_UpperCAmelCase    :							str									=					min_resolution
				_UpperCAmelCase    :							Dict									=					max_resolution
				_UpperCAmelCase    :							Optional[Any]									=					do_resize
				_UpperCAmelCase    :							str									=					size
				_UpperCAmelCase    :							Optional[int]									=					do_normalize
				_UpperCAmelCase    :							Optional[Any]									=					image_mean
				_UpperCAmelCase    :							Any									=					image_std
			def 							__snake_case (		self     )    ->    List[Any]:






				'''simple docstring'''







				return {
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_normalize": self.do_normalize,
				    "do_resize": self.do_resize,
				    "size": self.size,
				}






@require_torch
@require_vision
class     _UpperCAmelCase			(		__a    ,     unittest.TestCase):
			__a :       List[str]									=							ViTImageProcessor if is_vision_available() else None
			def 							__snake_case (		self     )    ->    Any:






				'''simple docstring'''







				_UpperCAmelCase    :							Tuple									=					ViTImageProcessingTester(self     )
			@property
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				return self.image_processor_tester.prepare_image_processor_dict()
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							int									=					self.image_processing_class(**self.image_processor_dict     )
				self.assertTrue(hasattr(_A      ,		"""image_mean"""     )     )
				self.assertTrue(hasattr(_A      ,		"""image_std"""     )     )
				self.assertTrue(hasattr(_A      ,		"""do_normalize"""     )     )
				self.assertTrue(hasattr(_A      ,		"""do_resize"""     )     )
				self.assertTrue(hasattr(_A      ,		"""size"""     )     )
			def 							__snake_case (		self     )    ->    Dict:






				'''simple docstring'''







				_UpperCAmelCase    :							Optional[int]									=					self.image_processing_class.from_dict(self.image_processor_dict     )
				self.assertEqual(image_processor.size      ,		{"""height""": 18, """width""": 18}     )

				_UpperCAmelCase    :							Any									=					self.image_processing_class.from_dict(self.image_processor_dict      ,		size=42     )
				self.assertEqual(image_processor.size      ,		{"""height""": 42, """width""": 42}     )
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				pass
			def 							__snake_case (		self     )    ->    Optional[Any]:






				'''simple docstring'''







				_UpperCAmelCase    :							Dict									=					self.image_processing_class(**self.image_processor_dict     )
				# create random PIL images
				_UpperCAmelCase    :							Dict									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		Image.Image     )

				# Test not batched input
				_UpperCAmelCase    :							Optional[Any]									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							Union[str, Any]									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)
			def 							__snake_case (		self     )    ->    str:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.image_processing_class(**self.image_processor_dict     )
				# create random numpy tensors
				_UpperCAmelCase    :							List[Any]									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A      ,		numpify=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		np.ndarray     )

				# Test not batched input
				_UpperCAmelCase    :							Any									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							int									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)
			def 							__snake_case (		self     )    ->    List[str]:






				'''simple docstring'''







				_UpperCAmelCase    :							Union[str, Any]									=					self.image_processing_class(**self.image_processor_dict     )
				# create random PyTorch tensors
				_UpperCAmelCase    :							Tuple									=					prepare_image_inputs(self.image_processor_tester      ,		equal_resolution=_A      ,		torchify=_A     )
				for image in image_inputs:
					self.assertIsInstance(_A      ,		torch.Tensor     )

				# Test not batched input
				_UpperCAmelCase    :							Optional[int]									=					image_processing(image_inputs[0]      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        1,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)

				# Test batched
				_UpperCAmelCase    :							Tuple									=					image_processing(_A      ,		return_tensors="""pt"""     ).pixel_values
				self.assertEqual(
				    encoded_images.shape      ,		(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        self.image_processor_tester.size["""height"""],
				        self.image_processor_tester.size["""width"""],
				    )      ,		)





