



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







       '''simple docstring'''


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


             '''simple docstring'''




             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   lowercase__       (							self    :						List[Any]					)				->		str:


             '''simple docstring'''




             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_				,					UpperCAmelCase_    				=							input_mask.shape
                   UpperCAmelCase_    				=							np.random.randint(1	,       seq_length - 1	,       size=(batch_size,)					)
                   for batch_idx, start_index in enumerate(_UpperCAmelCase					):
                         UpperCAmelCase_    				=							1
                         UpperCAmelCase_    				=							0

             UpperCAmelCase_    				=							self.get_config()

             return config, input_ids, tf.convert_to_tensor(_UpperCAmelCase					)



       def   lowercase__       (							self    :						List[Any]					)				->		Optional[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   lowercase__       (							self    :						Union[str, Any]	,       _UpperCAmelCase    :						List[Any]	,       _UpperCAmelCase    :						Optional[Any]	,       _UpperCAmelCase    :						Tuple					)				->		int:


             '''simple docstring'''




             UpperCAmelCase_    				=							TFBlipTextModel(config=_UpperCAmelCase					)
             UpperCAmelCase_    				=							model(_UpperCAmelCase	,       attention_mask=_UpperCAmelCase	,       training=_UpperCAmelCase					)
             UpperCAmelCase_    				=							model(_UpperCAmelCase	,       training=_UpperCAmelCase					)
             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   lowercase__       (							self    :						Tuple					)				->		Tuple:


             '''simple docstring'''




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



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







       '''simple docstring'''


       UpperCamelCase		=  (TFBlipTextModel,) if is_tf_available() else ()
       UpperCamelCase		=  False
       UpperCamelCase		=  False
       UpperCamelCase		=  False



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


             '''simple docstring'''




             UpperCAmelCase_    				=							BlipTextModelTester(self					)
             UpperCAmelCase_    				=							ConfigTester(self	,       config_class=_UpperCAmelCase	,       hidden_size=37					)



       def   lowercase__       (							self    :						int					)				->		List[Any]:


             '''simple docstring'''




             self.config_tester.run_common_tests()



       def   lowercase__       (							self    :						int					)				->		str:


             '''simple docstring'''




             UpperCAmelCase_    				=							self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*_UpperCAmelCase					)



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


             '''simple docstring'''




             pass



       def   lowercase__       (							self    :						List[Any]					)				->		Optional[Any]:


             '''simple docstring'''




             pass



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


             '''simple docstring'''




             pass



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


             '''simple docstring'''




             pass



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


             '''simple docstring'''




             pass



       @slow
       def   lowercase__       (							self    :						Tuple					)				->		Union[str, Any]:


             '''simple docstring'''




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







       def   lowercase__       (							self    :						Tuple	,       _UpperCAmelCase    :						Union[str, Any]=True					)				->		Union[str, Any]:


             '''simple docstring'''




             super().test_pt_tf_model_equivalence(allow_missing_keys=_UpperCAmelCase					)









"""simple docstring"""





from __future__ import annotations

from decimal import Decimal

from numpy import array
def      a__			(   lowerCAmelCase__   ):
      UpperCAmelCase_    				=							Decimal

      # Check if the provided matrix has 2 rows and 2 columns
      # since this implementation only works for 2x2 matrices
      if len(lowerCAmelCase__   ) == 2 and len(matrix[0]   ) == 2 and len(matrix[1]   ) == 2:
            # Calculate the determinant of the matrix
            UpperCAmelCase_    				=							float(
                d(matrix[0][0]   ) * d(matrix[1][1]   ) - d(matrix[1][0]   ) * d(matrix[0][1]   )   )
            if determinant == 0:
                  raise ValueError("This matrix has no inverse."   )

            # Creates a copy of the matrix with swapped positions of the elements
            UpperCAmelCase_    				=							[[0.0, 0.0], [0.0, 0.0]]
            UpperCAmelCase_				,					UpperCAmelCase_    				=							matrix[1][1], matrix[0][0]
            UpperCAmelCase_				,					UpperCAmelCase_    				=							-matrix[1][0], -matrix[0][1]

            # Calculate the inverse of the matrix
            return [
                [(float(d(lowerCAmelCase__   )   ) / determinant) or 0.0 for n in row] for row in swapped_matrix
            ]
      elif (
          len(lowerCAmelCase__   ) == 3
          and len(matrix[0]   ) == 3
          and len(matrix[1]   ) == 3
          and len(matrix[2]   ) == 3
      ):
            # Calculate the determinant of the matrix using Sarrus rule
            UpperCAmelCase_    				=							float(
                (
                    (d(matrix[0][0]   ) * d(matrix[1][1]   ) * d(matrix[2][2]   ))
                    + (d(matrix[0][1]   ) * d(matrix[1][2]   ) * d(matrix[2][0]   ))
                    + (d(matrix[0][2]   ) * d(matrix[1][0]   ) * d(matrix[2][1]   ))
                )
                - (
                    (d(matrix[0][2]   ) * d(matrix[1][1]   ) * d(matrix[2][0]   ))
                    + (d(matrix[0][1]   ) * d(matrix[1][0]   ) * d(matrix[2][2]   ))
                    + (d(matrix[0][0]   ) * d(matrix[1][2]   ) * d(matrix[2][1]   ))
                )   )
            if determinant == 0:
                  raise ValueError("This matrix has no inverse."   )

            # Creating cofactor matrix
            UpperCAmelCase_    				=							[
                [d(0.0   ), d(0.0   ), d(0.0   )],
                [d(0.0   ), d(0.0   ), d(0.0   )],
                [d(0.0   ), d(0.0   ), d(0.0   )],
            ]
            UpperCAmelCase_    				=							(d(matrix[1][1]   ) * d(matrix[2][2]   )) - (
                d(matrix[1][2]   ) * d(matrix[2][1]   )
            )
            UpperCAmelCase_    				=							-(
                (d(matrix[1][0]   ) * d(matrix[2][2]   )) - (d(matrix[1][2]   ) * d(matrix[2][0]   ))
            )
            UpperCAmelCase_    				=							(d(matrix[1][0]   ) * d(matrix[2][1]   )) - (
                d(matrix[1][1]   ) * d(matrix[2][0]   )
            )
            UpperCAmelCase_    				=							-(
                (d(matrix[0][1]   ) * d(matrix[2][2]   )) - (d(matrix[0][2]   ) * d(matrix[2][1]   ))
            )
            UpperCAmelCase_    				=							(d(matrix[0][0]   ) * d(matrix[2][2]   )) - (
                d(matrix[0][2]   ) * d(matrix[2][0]   )
            )
            UpperCAmelCase_    				=							-(
                (d(matrix[0][0]   ) * d(matrix[2][1]   )) - (d(matrix[0][1]   ) * d(matrix[2][0]   ))
            )
            UpperCAmelCase_    				=							(d(matrix[0][1]   ) * d(matrix[1][2]   )) - (
                d(matrix[0][2]   ) * d(matrix[1][1]   )
            )
            UpperCAmelCase_    				=							-(
                (d(matrix[0][0]   ) * d(matrix[1][2]   )) - (d(matrix[0][2]   ) * d(matrix[1][0]   ))
            )
            UpperCAmelCase_    				=							(d(matrix[0][0]   ) * d(matrix[1][1]   )) - (
                d(matrix[0][1]   ) * d(matrix[1][0]   )
            )

            # Transpose the cofactor matrix (Adjoint matrix)
            UpperCAmelCase_    				=							array(lowerCAmelCase__   )
            for i in range(3   ):
                  for j in range(3   ):
                        UpperCAmelCase_    				=							cofactor_matrix[j][i]

        # Inverse of the matrix using the formula (1/determinant) * adjoint matrix
            UpperCAmelCase_    				=							array(lowerCAmelCase__   )
            for i in range(3   ):
                  for j in range(3   ):
                        inverse_matrix[i][j] /= d(lowerCAmelCase__   )

        # Calculate the inverse of the matrix
            return [[float(d(lowerCAmelCase__   )   ) or 0.0 for n in row] for row in inverse_matrix]
      raise ValueError("Please provide a matrix of size 2x2 or 3x3."   )




