






"""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 		__lowerCamelCase				:





		'''simple docstring'''






		def __init__(   self							:	List[Any]						,			a_							:	Dict						,			a_							:	Optional[int]=12						,			a_							:	str=7						,			a_							:	str=True						,			a_							:	Tuple=True						,			a_							:	Optional[Any]=True						,			a_							:	str=99						,			a_							:	List[str]=32						,			a_							:	Tuple=32						,			a_							:	Tuple=2						,			a_							:	List[Any]=4						,			a_							:	Optional[int]=37						,			a_							:	Dict=0.1						,			a_							:	Dict=0.1						,			a_							:	Tuple=5_12						,			a_							:	Any=0.02						,			a_							:	int=0						,			a_							:	int=None						,			):
				lowerCAmelCase_   :							Optional[Any]									=			parent
				lowerCAmelCase_   :							Optional[Any]									=			batch_size
				lowerCAmelCase_   :							List[Any]									=			seq_length
				lowerCAmelCase_   :							List[Any]									=			is_training
				lowerCAmelCase_   :							List[str]									=			use_input_mask
				lowerCAmelCase_   :							Optional[int]									=			use_labels
				lowerCAmelCase_   :							Union[str, Any]									=			vocab_size
				lowerCAmelCase_   :							Any									=			hidden_size
				lowerCAmelCase_   :							int									=			projection_dim
				lowerCAmelCase_   :							int									=			num_hidden_layers
				lowerCAmelCase_   :							Tuple									=			num_attention_heads
				lowerCAmelCase_   :							Optional[Any]									=			intermediate_size
				lowerCAmelCase_   :							Dict									=			dropout
				lowerCAmelCase_   :							List[Any]									=			attention_dropout
				lowerCAmelCase_   :							List[str]									=			max_position_embeddings
				lowerCAmelCase_   :							Union[str, Any]									=			initializer_range
				lowerCAmelCase_   :							Tuple									=			scope
				lowerCAmelCase_   :							Any									=			bos_token_id





		def    lowerCamelCase       (   self							:	Optional[int]	):
				lowerCAmelCase_   :							List[Any]									=			ids_tensor([self.batch_size, self.seq_length]						,			self.vocab_size	)

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

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

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

				return config, input_ids, tf.convert_to_tensor(a_	)





		def    lowerCamelCase       (   self							:	Tuple	):
				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							:	Dict						,			a_							:	int						,			a_							:	int						,			a_							:	Dict	):
				lowerCAmelCase_   :							List[Any]									=			TFBlipTextModel(config=a_	)
				lowerCAmelCase_   :							str									=			model(a_						,			attention_mask=a_						,			training=a_	)
				lowerCAmelCase_   :							str									=			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    lowerCamelCase       (   self							:	str	):
				lowerCAmelCase_   :							List[str]									=			self.prepare_config_and_inputs()
				lowerCAmelCase_				,	lowerCAmelCase_				,	lowerCAmelCase_   :							str									=			config_and_inputs
				lowerCAmelCase_   :							Optional[Any]									=			{"input_ids": input_ids, "attention_mask": input_mask}
				return config, inputs_dict







@require_tf
class 		__lowerCamelCase				( SCREAMING_SNAKE_CASE__ ,							unittest.TestCase			):





		'''simple docstring'''






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





		def    lowerCamelCase       (   self							:	Union[str, Any]	):
				lowerCAmelCase_   :							Optional[int]									=			BlipTextModelTester(self	)
				lowerCAmelCase_   :							List[Any]									=			ConfigTester(self						,			config_class=a_						,			hidden_size=37	)





		def    lowerCamelCase       (   self							:	Union[str, Any]	):
				self.config_tester.run_common_tests()





		def    lowerCamelCase       (   self							:	List[str]	):
				lowerCAmelCase_   :							List[Any]									=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*a_	)





		def    lowerCamelCase       (   self							:	str	):
				pass





		def    lowerCamelCase       (   self							:	str	):
				pass





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





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





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





		@slow
		def    lowerCamelCase       (   self							:	str	):
				for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						lowerCAmelCase_   :							List[str]									=			TFBlipTextModel.from_pretrained(a_	)
						self.assertIsNotNone(a_	)





		def    lowerCamelCase       (   self							:	List[Any]						,			a_							:	List[Any]=True	):
				super().test_pt_tf_model_equivalence(allow_missing_keys=a_	)

def    a__       (						__UpperCamelCase ):
      if not head:
            return True
      # split the list to two parts
      SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     head.next, head
      while fast and fast.next:
            SCREAMING_SNAKE_CASE_      =     fast.next.next
            SCREAMING_SNAKE_CASE_      =     slow.next
      SCREAMING_SNAKE_CASE_      =     slow.next
      SCREAMING_SNAKE_CASE_      =     None  # Don't forget here! But forget still works!
      # reverse the second part
      SCREAMING_SNAKE_CASE_      =     None
      while second:
            SCREAMING_SNAKE_CASE_      =     second.next
            SCREAMING_SNAKE_CASE_      =     node
            SCREAMING_SNAKE_CASE_      =     second
            SCREAMING_SNAKE_CASE_      =     nxt
      # compare two parts
      # second part has the same or one less node
      while node:
            if node.val != head.val:
                  return False
            SCREAMING_SNAKE_CASE_      =     node.next
            SCREAMING_SNAKE_CASE_      =     head.next
      return True


def    a__       (						__UpperCamelCase ):
      if not head or not head.next:
            return True

      # 1. Get the midpoint (slow)
      SCREAMING_SNAKE_CASE_      =     SCREAMING_SNAKE_CASE_      =     SCREAMING_SNAKE_CASE_      =     head
      while fast and fast.next:
            SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     fast.next.next, slow.next

      # 2. Push the second half into the stack
      SCREAMING_SNAKE_CASE_      =     [slow.val]
      while slow.next:
            SCREAMING_SNAKE_CASE_      =     slow.next
            stack.append(slow.val )

      # 3. Comparison
      while stack:
            if stack.pop() != cur.val:
                  return False
            SCREAMING_SNAKE_CASE_      =     cur.next

      return True





def    a__       (						__UpperCamelCase ):
      if not head or not head.next:
            return True
      SCREAMING_SNAKE_CASE_      =     {}
      SCREAMING_SNAKE_CASE_      =     0
      while head:
            if head.val in d:
                  d[head.val].append(__UpperCamelCase )
            else:
                  SCREAMING_SNAKE_CASE_      =     [pos]
            SCREAMING_SNAKE_CASE_      =     head.next
            pos += 1
      SCREAMING_SNAKE_CASE_      =     pos - 1
      SCREAMING_SNAKE_CASE_      =     0
      for v in d.values():
            if len(__UpperCamelCase ) % 2 != 0:
                  middle += 1
            else:
                  SCREAMING_SNAKE_CASE_      =     0
                  for i in range(0   ,					len(__UpperCamelCase ) ):
                        if v[i] + v[len(__UpperCamelCase ) - 1 - step] != checksum:
                              return False
                        step += 1
            if middle > 1:
                  return False
      return True


