'''simple docstring'''




from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
  import tensorflow as tf

  from transformers import TFBlipTextModel
  from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST




class     a__      :






     def __init__( self		:		List[str]  ,				a		:		Any  ,				a		:		Union[str, Any]=12  ,				a		:		List[Any]=7  ,				a		:		str=True  ,				a		:		str=True  ,				a		:		Dict=True  ,				a		:		Union[str, Any]=99  ,				a		:		Optional[Any]=32  ,				a		:		int=32  ,				a		:		int=2  ,				a		:		Optional[int]=4  ,				a		:		Dict=37  ,				a		:		Optional[Any]=0.1  ,				a		:		Dict=0.1  ,				a		:		Optional[int]=5_12  ,				a		:		List[Any]=0.02  ,				a		:		Union[str, Any]=0  ,				a		:		Any=None  ,				):





         """simple docstring"""



         __lowerCamelCase		=  parent
         __lowerCamelCase		=  batch_size
         __lowerCamelCase		=  seq_length
         __lowerCamelCase		=  is_training
         __lowerCamelCase		=  use_input_mask
         __lowerCamelCase		=  use_labels
         __lowerCamelCase		=  vocab_size
         __lowerCamelCase		=  hidden_size
         __lowerCamelCase		=  projection_dim
         __lowerCamelCase		=  num_hidden_layers
         __lowerCamelCase		=  num_attention_heads
         __lowerCamelCase		=  intermediate_size
         __lowerCamelCase		=  dropout
         __lowerCamelCase		=  attention_dropout
         __lowerCamelCase		=  max_position_embeddings
         __lowerCamelCase		=  initializer_range
         __lowerCamelCase		=  scope
         __lowerCamelCase		=  bos_token_id






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         __lowerCamelCase		=  ids_tensor([self.batch_size, self.seq_length]  ,				self.vocab_size    )

         __lowerCamelCase		=  None
         if self.use_input_mask:
             __lowerCamelCase		=  random_attention_mask([self.batch_size, self.seq_length]    )

         if input_mask is not None:
             __lowerCamelCase		=  input_mask.numpy()
             __lowerCamelCase	,  __lowerCamelCase		=  input_mask.shape
             __lowerCamelCase		=  np.random.randint(1  ,				seq_length - 1  ,				size=(batch_size,)    )
             for batch_idx, start_index in enumerate(a    ):
                 __lowerCamelCase		=  1
                 __lowerCamelCase		=  0

         __lowerCamelCase		=  self.get_config()

         return config, input_ids, tf.convert_to_tensor(a    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]    ):





         """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    SCREAMING_SNAKE_CASE__   ( self		:		Optional[Any]  ,				a		:		int  ,				a		:		Tuple  ,				a		:		Union[str, Any]    ):





         """simple docstring"""



         __lowerCamelCase		=  TFBlipTextModel(config=a    )
         __lowerCamelCase		=  model(a  ,				attention_mask=a  ,				training=a    )
         __lowerCamelCase		=  model(a  ,				training=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    SCREAMING_SNAKE_CASE__   ( self		:		Union[str, Any]    ):





         """simple docstring"""



         __lowerCamelCase		=  self.prepare_config_and_inputs()
         __lowerCamelCase	,  __lowerCamelCase	,  __lowerCamelCase		=  config_and_inputs
         __lowerCamelCase		=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
         return config, inputs_dict




@require_tf
class     a__      (   UpperCAmelCase__							,   unittest.TestCase ):
     lowerCamelCase  :     List[Any]							=(TFBlipTextModel,) if is_tf_available() else ()
     lowerCamelCase  :     Any							=False
     lowerCamelCase  :     List[str]							=False
     lowerCamelCase  :     int							=False






     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[int]    ):





         """simple docstring"""



         __lowerCamelCase		=  BlipTextModelTester(self    )
         __lowerCamelCase		=  ConfigTester(self  ,				config_class=a  ,				hidden_size=37    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         self.config_tester.run_common_tests()






     def    SCREAMING_SNAKE_CASE__   ( self		:		str    ):





         """simple docstring"""



         __lowerCamelCase		=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*a    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[int]    ):





         """simple docstring"""



         pass






     def    SCREAMING_SNAKE_CASE__   ( self		:		List[Any]    ):





         """simple docstring"""



         pass






     @unittest.skip(reason='''Blip does not use inputs_embeds'''    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[int]    ):





         """simple docstring"""



         pass






     @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[Any]    ):





         """simple docstring"""



         pass






     @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''    )
     def    SCREAMING_SNAKE_CASE__   ( self		:		List[str]    ):





         """simple docstring"""



         pass






     @slow
     def    SCREAMING_SNAKE_CASE__   ( self		:		Optional[int]    ):





         """simple docstring"""



         for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             __lowerCamelCase		=  TFBlipTextModel.from_pretrained(a    )
             self.assertIsNotNone(a    )






     def    SCREAMING_SNAKE_CASE__   ( self		:		int  ,				a		:		int=True    ):





         """simple docstring"""



         super().test_pt_tf_model_equivalence(allow_missing_keys=a    )




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_video_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import VivitImageProcessor
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				def __init__(       self			:Any			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Tuple=7			,			lowerCAmelCase__			:List[Any]=3			,			lowerCAmelCase__			:Any=10			,			lowerCAmelCase__			:Optional[int]=18			,			lowerCAmelCase__			:Dict=30			,			lowerCAmelCase__			:Tuple=400			,			lowerCAmelCase__			:List[Any]=True			,			lowerCAmelCase__			:Tuple=None			,			lowerCAmelCase__			:str=True			,			lowerCAmelCase__			:List[str]=[0.5, 0.5, 0.5]			,			lowerCAmelCase__			:List[str]=[0.5, 0.5, 0.5]			,			lowerCAmelCase__			:Optional[Any]=None			,			)		->			Optional[Any]:
										__SCREAMING_SNAKE_CASE						:     Dict					=		size if size is not None else {'''shortest_edge''': 18}
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		crop_size if crop_size is not None else {'''height''': 18, '''width''': 18}

										__SCREAMING_SNAKE_CASE						:     Tuple					=		parent
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		batch_size
										__SCREAMING_SNAKE_CASE						:     List[str]					=		num_channels
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		num_frames
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_size
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		min_resolution
										__SCREAMING_SNAKE_CASE						:     Any					=		max_resolution
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		do_resize
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		size
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		do_normalize
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		image_mean
										__SCREAMING_SNAKE_CASE						:     List[str]					=		image_std
										__SCREAMING_SNAKE_CASE						:     str					=		crop_size



				def 		__magic_name__(       self			:Tuple	)		->			Any:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "crop_size": self.crop_size,
										}




@require_torch
@require_vision
class        _lowercase      (			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			List[str]						=		VivitImageProcessor if is_vision_available() else None


				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     str					=		VivitImageProcessingTester(self	)


				@property
				def 		__magic_name__(       self			:int	)		->			Union[str, Any]:
										return self.image_processor_tester.prepare_image_processor_dict()


				def 		__magic_name__(       self			:List[str]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_mean'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''image_std'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_normalize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_resize'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''do_center_crop'''	)	)
										self.assertTrue(hasattr(lowerCAmelCase__			,			'''size'''	)	)


				def 		__magic_name__(       self			:Optional[Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		self.image_processing_class.from_dict(self.image_processor_dict	)
										self.assertEqual(image_processor.size			,			{'''shortest_edge''': 18}	)
										self.assertEqual(image_processor.crop_size			,			{'''height''': 18, '''width''': 18}	)

										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.image_processing_class.from_dict(self.image_processor_dict			,			size=42			,			crop_size=84	)
										self.assertEqual(image_processor.size			,			{'''shortest_edge''': 42}	)
										self.assertEqual(image_processor.crop_size			,			{'''height''': 84, '''width''': 84}	)


				def 		__magic_name__(       self			:List[Any]	)		->			Union[str, Any]:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Dict					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PIL videos
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			Image.Image	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     List[str]					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)


				def 		__magic_name__(       self			:str	)		->			int:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Any					=		self.image_processing_class(**self.image_processor_dict	)
										# create random numpy tensors
										__SCREAMING_SNAKE_CASE						:     List[str]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			numpify=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			np.ndarray	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Any					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)



				def 		__magic_name__(       self			:Any	)		->			List[str]:
										# Initialize image_processing
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.image_processing_class(**self.image_processor_dict	)
										# create random PyTorch tensors
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		prepare_video_inputs(self.image_processor_tester			,			equal_resolution=lowerCAmelCase__			,			torchify=lowerCAmelCase__	)
										for video in video_inputs:
																self.assertIsInstance(lowerCAmelCase__			,			lowerCAmelCase__	)
																self.assertIsInstance(video[0]			,			torch.Tensor	)

										# Test not batched input
										__SCREAMING_SNAKE_CASE						:     Tuple					=		image_processing(video_inputs[0]			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        1,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)

										# Test batched
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		image_processing(lowerCAmelCase__			,			return_tensors='''pt'''	).pixel_values
										self.assertEqual(
										    encoded_videos.shape			,			(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_frames,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.crop_size['''height'''],
										        self.image_processor_tester.crop_size['''width'''],
										    )			,			)
