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




      def __init__(							self   ,       lowercase_   ,       lowercase_=1_2   ,       lowercase_=7   ,       lowercase_=True   ,       lowercase_=True   ,       lowercase_=True   ,       lowercase_=9_9   ,       lowercase_=3_2   ,       lowercase_=3_2   ,       lowercase_=2   ,       lowercase_=4   ,       lowercase_=3_7   ,       lowercase_=0.1   ,       lowercase_=0.1   ,       lowercase_=5_1_2   ,       lowercase_=0.0_2   ,       lowercase_=0   ,       lowercase_=None   ,       )						->					str:
          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							)						->					Any:
          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(lowercase_							):
                  UpperCAmelCase									=					1
                  UpperCAmelCase									=					0

          UpperCAmelCase									=					self.get_config()

          return config, input_ids, tf.convert_to_tensor(lowercase_							)




      def     a_      (							self							)						->					Union[str, Any]:
          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   ,       lowercase_   ,       lowercase_   ,       lowercase_							)						->					Optional[int]:
          UpperCAmelCase									=					TFBlipTextModel(config=lowercase_							)
          UpperCAmelCase									=					model(lowercase_   ,       attention_mask=lowercase_   ,       training=lowercase_							)
          UpperCAmelCase									=					model(lowercase_   ,       training=lowercase_							)
          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							)						->					Optional[Any]:
          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      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_     ,			unittest.TestCase							):
      __SCREAMING_SNAKE_CASE   :							List[str]							      =	(TFBlipTextModel,) if is_tf_available() else ()
      __SCREAMING_SNAKE_CASE   :							Optional[Any]							      =	False
      __SCREAMING_SNAKE_CASE   :							Dict							      =	False
      __SCREAMING_SNAKE_CASE   :							Tuple							      =	False




      def     a_      (							self							)						->					Any:
          UpperCAmelCase									=					BlipTextModelTester(self							)
          UpperCAmelCase									=					ConfigTester(self   ,       config_class=lowercase_   ,       hidden_size=3_7							)




      def     a_      (							self							)						->					Dict:
          self.config_tester.run_common_tests()




      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowercase_							)




      def     a_      (							self							)						->					Optional[Any]:
          pass




      def     a_      (							self							)						->					int:
          pass




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




      @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'							)
      def     a_      (							self							)						->					Tuple:
          pass




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




      @slow
      def     a_      (							self							)						->					int:
          for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              UpperCAmelCase									=					TFBlipTextModel.from_pretrained(lowercase_							)
              self.assertIsNotNone(lowercase_							)




      def     a_      (							self   ,       lowercase_=True							)						->					Dict:
          super().test_pt_tf_model_equivalence(allow_missing_keys=lowercase_							)





"""simple docstring"""

import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_    					=						logging.get_logger(__name__)
class      _UpperCAmelCase							(		SCREAMING_SNAKE_CASE_							):
      __SCREAMING_SNAKE_CASE   :							Union[str, Any]							      =	"encoder-decoder"
      __SCREAMING_SNAKE_CASE   :							Any							      =	True




      def __init__(							self   ,       **lowercase_							)						->					str:
          super().__init__(**lowercase_							)
          assert (
              "encoder" in kwargs and "decoder" in kwargs
          ), "Config has to be initialized with encoder and decoder config"
          UpperCAmelCase									=					kwargs.pop('encoder'							)
          UpperCAmelCase									=					encoder_config.pop('model_type'							)
          UpperCAmelCase									=					kwargs.pop('decoder'							)
          UpperCAmelCase									=					decoder_config.pop('model_type'							)

          from ..auto.configuration_auto import AutoConfig

          UpperCAmelCase									=					AutoConfig.for_model(lowercase_   ,       **lowercase_							)
          UpperCAmelCase									=					AutoConfig.for_model(lowercase_   ,       **lowercase_							)
          UpperCAmelCase									=					True




      @classmethod
      def     a_      (							cls   ,       lowercase_   ,       lowercase_   ,       **lowercase_							)						->					PretrainedConfig:
          logger.info('Set `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config'							)
          UpperCAmelCase									=					True
          UpperCAmelCase									=					True

          return cls(encoder=encoder_config.to_dict()   ,       decoder=decoder_config.to_dict()   ,       **lowercase_							)




      def     a_      (							self							)						->					Optional[int]:
          UpperCAmelCase									=					copy.deepcopy(self.__dict__							)
          UpperCAmelCase									=					self.encoder.to_dict()
          UpperCAmelCase									=					self.decoder.to_dict()
          UpperCAmelCase									=					self.__class__.model_type
          return output



