


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						:		Optional[Any]		, _SCREAMING_SNAKE_CASE						:		Tuple		, _SCREAMING_SNAKE_CASE						:		Tuple=12		, _SCREAMING_SNAKE_CASE						:		List[Any]=7		, _SCREAMING_SNAKE_CASE						:		int=True		, _SCREAMING_SNAKE_CASE						:		Any=True		, _SCREAMING_SNAKE_CASE						:		List[str]=True		, _SCREAMING_SNAKE_CASE						:		int=99		, _SCREAMING_SNAKE_CASE						:		List[Any]=32		, _SCREAMING_SNAKE_CASE						:		Tuple=32		, _SCREAMING_SNAKE_CASE						:		Optional[Any]=2		, _SCREAMING_SNAKE_CASE						:		int=4		, _SCREAMING_SNAKE_CASE						:		Optional[int]=37		, _SCREAMING_SNAKE_CASE						:		Any=0.1		, _SCREAMING_SNAKE_CASE						:		int=0.1		, _SCREAMING_SNAKE_CASE						:		int=512		, _SCREAMING_SNAKE_CASE						:		Dict=0.02		, _SCREAMING_SNAKE_CASE						:		Dict=0		, _SCREAMING_SNAKE_CASE						:		Dict=None		, )->	Dict:
			lowerCAmelCase__       :      str   					= parent
			lowerCAmelCase__       :      Union[str, Any]   					= batch_size
			lowerCAmelCase__       :      Any   					= seq_length
			lowerCAmelCase__       :      Tuple   					= is_training
			lowerCAmelCase__       :      Optional[int]   					= use_input_mask
			lowerCAmelCase__       :      str   					= use_labels
			lowerCAmelCase__       :      Optional[int]   					= vocab_size
			lowerCAmelCase__       :      Tuple   					= hidden_size
			lowerCAmelCase__       :      str   					= projection_dim
			lowerCAmelCase__       :      List[Any]   					= num_hidden_layers
			lowerCAmelCase__       :      Union[str, Any]   					= num_attention_heads
			lowerCAmelCase__       :      Tuple   					= intermediate_size
			lowerCAmelCase__       :      Optional[int]   					= dropout
			lowerCAmelCase__       :      Union[str, Any]   					= attention_dropout
			lowerCAmelCase__       :      Optional[int]   					= max_position_embeddings
			lowerCAmelCase__       :      Tuple   					= initializer_range
			lowerCAmelCase__       :      List[Any]   					= scope
			lowerCAmelCase__       :      Optional[Any]   					= bos_token_id




	def 							UpperCAmelCase__( self						:		int		)->	List[str]:
			lowerCAmelCase__       :      Any   					= ids_tensor([self.batch_size, self.seq_length]		, self.vocab_size		)

			lowerCAmelCase__       :      Dict   					= None
			if self.use_input_mask:
					lowerCAmelCase__       :      List[Any]   					= random_attention_mask([self.batch_size, self.seq_length]		)

			if input_mask is not None:
					lowerCAmelCase__       :      Dict   					= input_mask.numpy()
					lowerCAmelCase__					,     lowerCAmelCase__       :      Any   					= input_mask.shape
					lowerCAmelCase__       :      Any   					= np.random.randint(1		, seq_length - 1		, size=(batch_size,)		)
					for batch_idx, start_index in enumerate(_SCREAMING_SNAKE_CASE		):
							lowerCAmelCase__       :      int   					= 1
							lowerCAmelCase__       :      Dict   					= 0

			lowerCAmelCase__       :      int   					= self.get_config()

			return config, input_ids, tf.convert_to_tensor(_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		int		)->	List[str]:
			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 							UpperCAmelCase__( self						:		int		, _SCREAMING_SNAKE_CASE						:		str		, _SCREAMING_SNAKE_CASE						:		str		, _SCREAMING_SNAKE_CASE						:		Union[str, Any]		)->	int:
			lowerCAmelCase__       :      Any   					= TFBlipTextModel(config=_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      Union[str, Any]   					= model(_SCREAMING_SNAKE_CASE		, attention_mask=_SCREAMING_SNAKE_CASE		, training=_SCREAMING_SNAKE_CASE		)
			lowerCAmelCase__       :      int   					= model(_SCREAMING_SNAKE_CASE		, training=_SCREAMING_SNAKE_CASE		)
			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 							UpperCAmelCase__( self						:		List[str]		)->	List[Any]:
			lowerCAmelCase__       :      int   					= self.prepare_config_and_inputs()
			lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__       :      Tuple   					= config_and_inputs
			lowerCAmelCase__       :      Union[str, Any]   					= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
			return config, inputs_dict





@require_tf
class  _a							(    _lowercase					,						unittest.TestCase):
	_a  :  Dict				   =							(TFBlipTextModel,) if is_tf_available() else ()
	_a  :  Union[str, Any]				   =							False
	_a  :  List[str]				   =							False
	_a  :  List[str]				   =							False




	def 							UpperCAmelCase__( self						:		List[Any]		)->	Optional[Any]:
			lowerCAmelCase__       :      List[Any]   					= BlipTextModelTester(self		)
			lowerCAmelCase__       :      Optional[Any]   					= ConfigTester(self		, config_class=_SCREAMING_SNAKE_CASE		, hidden_size=37		)




	def 							UpperCAmelCase__( self						:		Dict		)->	str:
			self.config_tester.run_common_tests()




	def 							UpperCAmelCase__( self						:		List[str]		)->	List[str]:
			lowerCAmelCase__       :      Any   					= self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		int		)->	Tuple:
			pass




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




	@unittest.skip(reason='''Blip does not use inputs_embeds'''		)
	def 							UpperCAmelCase__( self						:		str		)->	Tuple:
			pass




	@unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''		)
	def 							UpperCAmelCase__( self						:		Any		)->	Union[str, Any]:
			pass




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




	@slow
	def 							UpperCAmelCase__( self						:		List[str]		)->	Union[str, Any]:
			for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase__       :      Tuple   					= TFBlipTextModel.from_pretrained(_SCREAMING_SNAKE_CASE		)
					self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)




	def 							UpperCAmelCase__( self						:		Optional[int]		, _SCREAMING_SNAKE_CASE						:		Tuple=True		)->	Dict:
			super().test_pt_tf_model_equivalence(allow_missing_keys=_SCREAMING_SNAKE_CASE		)








from __future__ import annotations






def 					lowerCamelCase_   (	_a     ,       _a     ,       _a     ,       _a	):  # noqa: E741




		"""simple docstring"""






		while r - l > 1:
				lowerCAmelCase__       :      Any   					= (l + r) // 2
				if v[m] >= key:
						lowerCAmelCase__       :      int   					= m
				else:
						lowerCAmelCase__       :      Tuple   					= m  # noqa: E741
		return r






def 					lowerCamelCase_   (	_a	):




		"""simple docstring"""






		if len(_a	) == 0:
				return 0

		lowerCAmelCase__       :      Optional[int]   					= [0] * len(_a	)
		lowerCAmelCase__       :      List[Any]   					= 1

		lowerCAmelCase__       :      int   					= v[0]

		for i in range(1     ,       len(_a	)	):
				if v[i] < tail[0]:
						lowerCAmelCase__       :      str   					= v[i]
				elif v[i] > tail[length - 1]:
						lowerCAmelCase__       :      Any   					= v[i]
						length += 1
				else:
						lowerCAmelCase__       :      int   					= v[i]

		return length


if __name__ == "__main__":
		import doctest

		doctest.testmod()




