



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     ,						lowerCAmelCase     ,						lowerCAmelCase=12     ,						lowerCAmelCase=7     ,						lowerCAmelCase=True     ,						lowerCAmelCase=True     ,						lowerCAmelCase=True     ,						lowerCAmelCase=99     ,						lowerCAmelCase=32     ,						lowerCAmelCase=32     ,						lowerCAmelCase=2     ,						lowerCAmelCase=4     ,						lowerCAmelCase=37     ,						lowerCAmelCase=0.1     ,						lowerCAmelCase=0.1     ,						lowerCAmelCase=512     ,						lowerCAmelCase=0.02     ,						lowerCAmelCase=0     ,						lowerCAmelCase=None     ,						):
							UpperCAmelCase_						       =					parent
							UpperCAmelCase_						       =					batch_size
							UpperCAmelCase_						       =					seq_length
							UpperCAmelCase_						       =					is_training
							UpperCAmelCase_						       =					use_input_mask
							UpperCAmelCase_						       =					use_labels
							UpperCAmelCase_						       =					vocab_size
							UpperCAmelCase_						       =					hidden_size
							UpperCAmelCase_						       =					projection_dim
							UpperCAmelCase_						       =					num_hidden_layers
							UpperCAmelCase_						       =					num_attention_heads
							UpperCAmelCase_						       =					intermediate_size
							UpperCAmelCase_						       =					dropout
							UpperCAmelCase_						       =					attention_dropout
							UpperCAmelCase_						       =					max_position_embeddings
							UpperCAmelCase_						       =					initializer_range
							UpperCAmelCase_						       =					scope
							UpperCAmelCase_						       =					bos_token_id



					def  A__		(      self  ):
							UpperCAmelCase_						       =					ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size  )

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

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

							UpperCAmelCase_						       =					self.get_config()

							return config, input_ids, tf.convert_to_tensor(A_  )



					def  A__		(      self  ):
							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  A__		(      self     ,						lowerCAmelCase     ,						lowerCAmelCase     ,						lowerCAmelCase  ):
							UpperCAmelCase_						       =					TFBlipTextModel(config=A_  )
							UpperCAmelCase_						       =					model(A_     ,						attention_mask=A_     ,						training=A_  )
							UpperCAmelCase_						       =					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  A__		(      self  ):
							UpperCAmelCase_						       =					self.prepare_config_and_inputs()
							UpperCAmelCase_						       =					config_and_inputs
							UpperCAmelCase_						       =					{"""input_ids""": input_ids, """attention_mask""": input_mask}
							return config, inputs_dict





@require_tf
class 						lowerCamelCase   (    lowercase__,  unittest.TestCase	):







					'''simple docstring'''







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



					def  A__		(      self  ):
							UpperCAmelCase_						       =					BlipTextModelTester(self  )
							UpperCAmelCase_						       =					ConfigTester(self     ,						config_class=A_     ,						hidden_size=37  )



					def  A__		(      self  ):
							self.config_tester.run_common_tests()



					def  A__		(      self  ):
							UpperCAmelCase_						       =					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*A_  )



					def  A__		(      self  ):
							pass



					def  A__		(      self  ):
							pass



					@unittest.skip(reason="Blip does not use inputs_embeds"  )
					def  A__		(      self  ):
							pass



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



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



					@slow
					def  A__		(      self  ):
							for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									UpperCAmelCase_						       =					TFBlipTextModel.from_pretrained(A_  )
									self.assertIsNotNone(A_  )



					def  A__		(      self     ,						lowerCAmelCase=True  ):
							super().test_pt_tf_model_equivalence(allow_missing_keys=A_  )





from random import randint, random




def    a__    (		snake_case__					:							int						,   snake_case__					:							int						,   snake_case__					:							int						,   snake_case__					:							bool = False						,   snake_case__					:							bool = False						,   snake_case__					:							int = 5						,   ):
  _UpperCAmelCase		:							Optional[int]							=     [[-1] * number_of_cells]  # Create a highway without any car
  _UpperCAmelCase		:							Optional[Any]							=     0
  _UpperCAmelCase		:							Dict							=     max(snake_case__						,   0     )
  while i < number_of_cells:
    _UpperCAmelCase		:							int							=     (
        randint(0						,   snake_case__     ) if random_speed else initial_speed
    )  # Place the cars
    i += (
        randint(1						,   max_speed * 2     ) if random_frequency else frequency
    )  # Arbitrary number, may need tuning
  return highway




def    a__    (		snake_case__					:							list						,   snake_case__					:							int     ):
  _UpperCAmelCase		:							List[Any]							=     0
  _UpperCAmelCase		:							int							=     highway_now[car_index + 1 :]
  for cell in range(len(snake_case__     )     ):  # May need a better name for this
    if cells[cell] != -1:  # If the cell is not empty then
      return distance  # we have the distance we wanted
    distance += 1
  # Here if the car is near the end of the highway
  return distance + get_distance(snake_case__						,   -1     )




def    a__    (		snake_case__					:							list						,   snake_case__					:							float						,   snake_case__					:							int     ):
  _UpperCAmelCase		:							Optional[Any]							=     len(snake_case__     )
  # Beforce calculations, the highway is empty
  _UpperCAmelCase		:							Dict							=     [-1] * number_of_cells

  for car_index in range(snake_case__     ):
    if highway_now[car_index] != -1:
      # Add 1 to the current speed of the car and cap the speed
      _UpperCAmelCase		:							Dict							=     min(highway_now[car_index] + 1						,   snake_case__     )
      # Number of empty cell before the next car
      _UpperCAmelCase		:							List[str]							=     get_distance(snake_case__						,   snake_case__     ) - 1
      # We can't have the car causing an accident
      _UpperCAmelCase		:							List[str]							=     min(next_highway[car_index]						,   snake_case__     )
      if random() < probability:
        # Randomly, a driver will slow down
        _UpperCAmelCase		:							Dict							=     max(next_highway[car_index] - 1						,   0     )
  return next_highway




def    a__    (		snake_case__					:							list						,   snake_case__					:							int						,   snake_case__					:							float						,   snake_case__					:							int     ):
  _UpperCAmelCase		:							Union[str, Any]							=     len(highway[0]     )

  for i in range(snake_case__     ):
    _UpperCAmelCase		:							Tuple							=     update(highway[i]						,   snake_case__						,   snake_case__     )
    _UpperCAmelCase		:							int							=     [-1] * number_of_cells

    for car_index in range(snake_case__     ):
      _UpperCAmelCase		:							List[Any]							=     next_speeds_calculated[car_index]
      if speed != -1:
        # Change the position based on the speed (with % to create the loop)
        _UpperCAmelCase		:							Optional[Any]							=     (car_index + speed) % number_of_cells
        # Commit the change of position
        _UpperCAmelCase		:							Optional[Any]							=     speed
    highway.append(snake_case__     )

  return highway


if __name__ == "__main__":
 import doctest

 doctest.testmod()





