




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    SCREAMING_SNAKE_CASE_				:



	"""simple docstring"""


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







						"""simple docstring"""







						__UpperCamelCase					:	Optional[Any]             =      parent
						__UpperCamelCase					:	Optional[int]             =      batch_size
						__UpperCamelCase					:	Union[str, Any]             =      seq_length
						__UpperCamelCase					:	Any             =      is_training
						__UpperCamelCase					:	Optional[int]             =      use_input_mask
						__UpperCamelCase					:	Dict             =      use_labels
						__UpperCamelCase					:	List[Any]             =      vocab_size
						__UpperCamelCase					:	Optional[int]             =      hidden_size
						__UpperCamelCase					:	Optional[int]             =      projection_dim
						__UpperCamelCase					:	Optional[int]             =      num_hidden_layers
						__UpperCamelCase					:	Optional[Any]             =      num_attention_heads
						__UpperCamelCase					:	Any             =      intermediate_size
						__UpperCamelCase					:	List[str]             =      dropout
						__UpperCamelCase					:	int             =      attention_dropout
						__UpperCamelCase					:	Optional[Any]             =      max_position_embeddings
						__UpperCamelCase					:	int             =      initializer_range
						__UpperCamelCase					:	Union[str, Any]             =      scope
						__UpperCamelCase					:	Optional[int]             =      bos_token_id






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      ids_tensor([self.batch_size, self.seq_length]					,     self.vocab_size	)

						__UpperCamelCase					:	Union[str, Any]             =      None
						if self.use_input_mask:
											__UpperCamelCase					:	Tuple             =      random_attention_mask([self.batch_size, self.seq_length]	)

						if input_mask is not None:
											__UpperCamelCase					:	List[str]             =      input_mask.numpy()
											__UpperCamelCase      ,			__UpperCamelCase					:	str             =      input_mask.shape
											__UpperCamelCase					:	Optional[int]             =      np.random.randint(1					,     seq_length - 1					,     size=(batch_size,)	)
											for batch_idx, start_index in enumerate(lowerCAmelCase	):
																__UpperCamelCase					:	Tuple             =      1
																__UpperCamelCase					:	Any             =      0

						__UpperCamelCase					:	Dict             =      self.get_config()

						return config, input_ids, tf.convert_to_tensor(lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       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 					lowerCamelCase__     (					self		:						Optional[Any]					,     lowerCAmelCase		:						Dict					,     lowerCAmelCase		:						List[str]					,     lowerCAmelCase		:						Optional[Any]	)	->       Optional[int]:







						"""simple docstring"""







						__UpperCamelCase					:	Any             =      TFBlipTextModel(config=lowerCAmelCase	)
						__UpperCamelCase					:	Optional[Any]             =      model(lowerCAmelCase					,     attention_mask=lowerCAmelCase					,     training=lowerCAmelCase	)
						__UpperCamelCase					:	Any             =      model(lowerCAmelCase					,     training=lowerCAmelCase	)
						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 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Dict:







						"""simple docstring"""







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






@require_tf
class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase	,  unittest.TestCase   ):



	"""simple docstring"""


	__magic_name__   :       Dict      	=		(TFBlipTextModel,) if is_tf_available() else ()
	__magic_name__   :       str      	=		False
	__magic_name__   :       Optional[int]      	=		False
	__magic_name__   :       List[Any]      	=		False






	def 					lowerCamelCase__     (					self		:						List[str]	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	List[str]             =      BlipTextModelTester(self	)
						__UpperCamelCase					:	List[Any]             =      ConfigTester(self					,     config_class=lowerCAmelCase					,     hidden_size=37	)






	def 					lowerCamelCase__     (					self		:						Optional[int]	)	->       Dict:







						"""simple docstring"""







						self.config_tester.run_common_tests()






	def 					lowerCamelCase__     (					self		:						int	)	->       str:







						"""simple docstring"""







						__UpperCamelCase					:	Tuple             =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						int	)	->       Union[str, Any]:







						"""simple docstring"""







						pass






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Union[str, Any]:







						"""simple docstring"""







						pass






	@unittest.skip(reason="""Blip does not use inputs_embeds"""	)
	def 					lowerCamelCase__     (					self		:						Union[str, Any]	)	->       Tuple:







						"""simple docstring"""







						pass






	@unittest.skip(reason="""BlipTextModel has no base class and is not available in MODEL_MAPPING"""	)
	def 					lowerCamelCase__     (					self		:						List[str]	)	->       Tuple:







						"""simple docstring"""







						pass






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







						"""simple docstring"""







						pass






	@slow
	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       Dict:







						"""simple docstring"""







						for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__UpperCamelCase					:	str             =      TFBlipTextModel.from_pretrained(lowerCAmelCase	)
											self.assertIsNotNone(lowerCAmelCase	)






	def 					lowerCamelCase__     (					self		:						Optional[int]					,     lowerCAmelCase		:						str=True	)	->       str:







						"""simple docstring"""







						super().test_pt_tf_model_equivalence(allow_missing_keys=lowerCAmelCase	)












import re
from pathlib import Path
from unittest import TestCase

import pytest


@pytest.mark.integration
class    SCREAMING_SNAKE_CASE_				(       _UpperCamelCase   ):



	"""simple docstring"""


	def 					lowerCamelCase__     (					self		:						Any					,     lowerCAmelCase		:						str	)	->       List[Any]:







						"""simple docstring"""







						with open(lowerCAmelCase					,     encoding="""utf-8"""	) as input_file:
											__UpperCamelCase					:	Union[str, Any]             =      re.compile(R"""(?!.*\b(?:encoding|rb|w|wb|w+|wb+|ab|ab+)\b)(?<=\s)(open)\((.*)\)"""	)
											__UpperCamelCase					:	str             =      input_file.read()
											__UpperCamelCase					:	Optional[int]             =      regexp.search(lowerCAmelCase	)

						return match






	def 					lowerCamelCase__     (					self		:						Optional[Any]					,     lowerCAmelCase		:						str	)	->       Union[str, Any]:







						"""simple docstring"""







						with open(lowerCAmelCase					,     encoding="""utf-8"""	) as input_file:
											__UpperCamelCase					:	Dict             =      re.compile(R"""#[^\r\n]*print\(|\"[^\r\n]*print\(|\"\"\".*?print\(.*?\"\"\"|(print\()"""					,     re.DOTALL	)
											__UpperCamelCase					:	str             =      input_file.read()
											# use `re.finditer` to handle the case where the ignored groups would be matched first by `re.search`
											__UpperCamelCase					:	List[Any]             =      regexp.finditer(lowerCAmelCase	)

						__UpperCamelCase					:	int             =      [match for match in matches if match is not None and match.group(1	) is not None]
						return matches[0] if matches else None






	def 					lowerCamelCase__     (					self		:						List[Any]	)	->       List[str]:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      Path("""./datasets"""	)
						__UpperCamelCase					:	Optional[int]             =      list(dataset_paths.absolute().glob("""**/*.py"""	)	)

						for dataset in dataset_files:
											if self._no_encoding_on_file_open(str(lowerCAmelCase	)	):
																raise AssertionError(F'''open(...) must use utf-8 encoding in {dataset}'''	)






	def 					lowerCamelCase__     (					self		:						Optional[Any]	)	->       Any:







						"""simple docstring"""







						__UpperCamelCase					:	Optional[int]             =      Path("""./datasets"""	)
						__UpperCamelCase					:	Optional[int]             =      list(dataset_paths.absolute().glob("""**/*.py"""	)	)

						for dataset in dataset_files:
											if self._no_print_statements(str(lowerCAmelCase	)	):
																raise AssertionError(F'''print statement found in {dataset}. Use datasets.logger/logging instead.'''	)






