'''simple docstring'''




from __future__ import annotations

import unittest

from transformers import EsmConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
   import numpy
   import tensorflow as tf

   from transformers.models.esm.modeling_tf_esm import (
       TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
       TFEsmForMaskedLM,
       TFEsmForSequenceClassification,
       TFEsmForTokenClassification,
       TFEsmModel,
   )


class       UpperCAmelCase__  :






   """simple docstring"""
   def __init__(self       ,				_a       ,				)    ->     str:
        lowercase_					:       Dict     				=					parent
        lowercase_					:       str     				=					13
        lowercase_					:       int     				=					7
        lowercase_					:       List[str]     				=					True
        lowercase_					:       str     				=					True
        lowercase_					:       Optional[Any]     				=					True
        lowercase_					:       Optional[int]     				=					99
        lowercase_					:       str     				=					32
        lowercase_					:       List[str]     				=					2
        lowercase_					:       Optional[Any]     				=					4
        lowercase_					:       Optional[Any]     				=					37
        lowercase_					:       Union[str, Any]     				=					'gelu'
        lowercase_					:       Optional[int]     				=					0.1
        lowercase_					:       List[Any]     				=					0.1
        lowercase_					:       Optional[Any]     				=					512
        lowercase_					:       Optional[int]     				=					16
        lowercase_					:       Any     				=					2
        lowercase_					:       List[Any]     				=					0.02
        lowercase_					:       str     				=					3
        lowercase_					:       Tuple     				=					4
        lowercase_					:       Dict     				=					None
   def 		_lowerCamelCase (self				)    ->     Union[str, Any]:
        lowercase_					:       Optional[Any]     				=					ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size				)

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

        lowercase_					:       Optional[Any]     				=					None
        lowercase_					:       Tuple     				=					None
        lowercase_					:       int     				=					None
        if self.use_labels:
             lowercase_					:       Any     				=					ids_tensor([self.batch_size]       ,				self.type_sequence_label_size				)
             lowercase_					:       str     				=					ids_tensor([self.batch_size, self.seq_length]       ,				self.num_labels				)
             lowercase_					:       Dict     				=					ids_tensor([self.batch_size]       ,				self.num_choices				)

        lowercase_					:       List[str]     				=					EsmConfig(
            vocab_size=self.vocab_size       ,				hidden_size=self.hidden_size       ,				num_hidden_layers=self.num_hidden_layers       ,				pad_token_id=1       ,				num_attention_heads=self.num_attention_heads       ,				intermediate_size=self.intermediate_size       ,				hidden_act=self.hidden_act       ,				hidden_dropout_prob=self.hidden_dropout_prob       ,				attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,				max_position_embeddings=self.max_position_embeddings       ,				type_vocab_size=self.type_vocab_size       ,				initializer_range=self.initializer_range       ,				)

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 		_lowerCamelCase (self				)    ->     Union[str, Any]:
        (
            (
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,			
        )					:       List[str]     				=					self.prepare_config_and_inputs()

        lowercase_					:       int     				=					True
        lowercase_					:       str     				=					floats_tensor([self.batch_size, self.seq_length, self.hidden_size]				)
        lowercase_					:       List[Any]     				=					ids_tensor([self.batch_size, self.seq_length]       ,				vocab_size=2				)

        return (
            config,
            input_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
            encoder_hidden_states,
            encoder_attention_mask,
        )
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a				)    ->     str:
        lowercase_					:       Optional[int]     				=					TFEsmModel(config=_lowerCAmelCase				)
        lowercase_					:       Any     				=					{'input_ids': input_ids, 'attention_mask': input_mask}
        lowercase_					:       Dict     				=					model(_lowerCAmelCase				)

        lowercase_					:       Optional[Any]     				=					[input_ids, input_mask]
        lowercase_					:       Optional[int]     				=					model(_lowerCAmelCase				)

        lowercase_					:       List[Any]     				=					model(_lowerCAmelCase				)

        self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, self.seq_length, self.hidden_size)				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				)    ->     int:
        lowercase_					:       str     				=					True

        lowercase_					:       Union[str, Any]     				=					TFEsmModel(config=_lowerCAmelCase				)
        lowercase_					:       int     				=					{
            'input_ids': input_ids,
            'attention_mask': input_mask,
            'encoder_hidden_states': encoder_hidden_states,
            'encoder_attention_mask': encoder_attention_mask,
        }
        lowercase_					:       List[Any]     				=					model(_lowerCAmelCase				)

        lowercase_					:       Tuple     				=					[input_ids, input_mask]
        lowercase_					:       List[Any]     				=					model(_lowerCAmelCase       ,				encoder_hidden_states=_lowerCAmelCase				)

        # Also check the case where encoder outputs are not passed
        lowercase_					:       Dict     				=					model(_lowerCAmelCase       ,				attention_mask=_lowerCAmelCase				)

        self.parent.assertEqual(result.last_hidden_state.shape       ,				(self.batch_size, self.seq_length, self.hidden_size)				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a				)    ->     List[Any]:
        lowercase_					:       Union[str, Any]     				=					TFEsmForMaskedLM(config=_lowerCAmelCase				)
        lowercase_					:       List[str]     				=					model([input_ids, input_mask]				)
        self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.vocab_size)				)
   def 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a       ,				_a				)    ->     Union[str, Any]:
        lowercase_					:       Optional[int]     				=					self.num_labels
        lowercase_					:       Dict     				=					TFEsmForTokenClassification(config=_lowerCAmelCase				)
        lowercase_					:       Optional[int]     				=					{'input_ids': input_ids, 'attention_mask': input_mask}
        lowercase_					:       Any     				=					model(_lowerCAmelCase				)
        self.parent.assertEqual(result.logits.shape       ,				(self.batch_size, self.seq_length, self.num_labels)				)





   def 		_lowerCamelCase (self				)    ->     List[Any]:
        lowercase_					:       Tuple     				=					self.prepare_config_and_inputs()
        (
            (
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,(
            lowercase_			
        )				,			
        )					:       int     				=					config_and_inputs
        lowercase_					:       str     				=					{'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict



@require_tf
class       UpperCAmelCase__  (       _snake_case					,     _snake_case					,     unittest.TestCase			):






   """simple docstring"""
   A							:       Union[str, Any]     =    (
       (
           TFEsmModel,
           TFEsmForMaskedLM,
           TFEsmForSequenceClassification,
           TFEsmForTokenClassification,
       )
       if is_tf_available()
       else ()
   )
   A							:       Optional[Any]     =    (
       {
           '''feature-extraction''': TFEsmModel,
           '''fill-mask''': TFEsmForMaskedLM,
           '''text-classification''': TFEsmForSequenceClassification,
           '''token-classification''': TFEsmForTokenClassification,
           '''zero-shot''': TFEsmForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A							:       List[Any]     =    False
   A							:       Optional[Any]     =    False
   def 		_lowerCamelCase (self				)    ->     str:
        lowercase_					:       Tuple     				=					TFEsmModelTester(self				)
        lowercase_					:       Dict     				=					ConfigTester(self       ,				config_class=_lowerCAmelCase       ,				hidden_size=37				)
   def 		_lowerCamelCase (self				)    ->     Tuple:
        self.config_tester.run_common_tests()
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_					:       Optional[Any]     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_lowerCAmelCase				)
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        lowercase_					:       List[str]     				=					self.model_tester.prepare_config_and_inputs_for_decoder()
        self.model_tester.create_and_check_model_as_decoder(*_lowerCAmelCase				)
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_					:       Optional[Any]     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase				)
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_					:       Tuple     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase				)
   @slow
   def 		_lowerCamelCase (self				)    ->     Tuple:
        for model_name in TF_ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_					:       Union[str, Any]     				=					TFEsmModel.from_pretrained(_lowerCAmelCase				)
             self.assertIsNotNone(_lowerCAmelCase				)
   @unittest.skip('Protein models do not support embedding resizing.'				)
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        pass
   @unittest.skip('Protein models do not support embedding resizing.'				)
   def 		_lowerCamelCase (self				)    ->     Any:
        pass





   def 		_lowerCamelCase (self				)    ->     Any:
        lowercase_				,lowercase_					:       str     				=					self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_					:       Tuple     				=					model_class(_lowerCAmelCase				)
             assert isinstance(model.get_input_embeddings()       ,				tf.keras.layers.Layer				)
             if model_class is TFEsmForMaskedLM:
                  # Output embedding test differs from the main test because they're a matrix, not a layer
                  lowercase_					:       int     				=					model.get_bias()
                  assert isinstance(_lowerCAmelCase       ,				_lowerCAmelCase				)
                  for k, v in name.items():
                       assert isinstance(_lowerCAmelCase       ,				tf.Variable				)
             else:
                  lowercase_					:       str     				=					model.get_output_embeddings()
                  assert x is None
                  lowercase_					:       Any     				=					model.get_bias()
                  assert name is None



@require_tf
class       UpperCAmelCase__  (       unittest.TestCase			):






   """simple docstring"""
   @slow
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        lowercase_					:       Optional[int]     				=					TFEsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'				)

        lowercase_					:       Union[str, Any]     				=					tf.constant([[0, 1, 2, 3, 4, 5]]				)
        lowercase_					:       Dict     				=					model(_lowerCAmelCase				)[0]
        lowercase_					:       Union[str, Any]     				=					[1, 6, 33]
        self.assertEqual(list(output.numpy().shape				)       ,				_lowerCAmelCase				)
        # compare the actual values for a slice.
        lowercase_					:       List[Any]     				=					tf.constant(
            [
                [
                    [8.92_15_18, -10.58_98_14, -6.4_67_13_07],
                    [-6.3_96_71_56, -13.91_13_77, -1.1_21_19_15],
                    [-7.78_12_47, -13.95_15_57, -3.74_05_92],
                ]
            ]				)
        self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()       ,				expected_slice.numpy()       ,				atol=1e-2				)				)





   @slow
   def 		_lowerCamelCase (self				)    ->     Dict:
        lowercase_					:       int     				=					TFEsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'				)

        lowercase_					:       int     				=					tf.constant([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]				)
        lowercase_					:       Dict     				=					model(_lowerCAmelCase				)[0]
        # compare the actual values for a slice.
        lowercase_					:       Optional[Any]     				=					tf.constant(
            [
                [
                    [0.14_44_30_92, 0.54_12_53_27, 0.3_24_77_39],
                    [0.30_34_04_84, 0.00_52_66_76, 0.31_07_77_22],
                    [0.32_27_80_43, -0.24_98_70_96, 0.3_41_46_28],
                ]
            ]				)
        self.assertTrue(numpy.allclose(output[:, :3, :3].numpy()       ,				expected_slice.numpy()       ,				atol=1e-4				)				)





'''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__  :






   """simple docstring"""
   def __init__(self       ,				_a       ,				_a=12       ,				_a=7       ,				_a=True       ,				_a=True       ,				_a=True       ,				_a=99       ,				_a=32       ,				_a=32       ,				_a=2       ,				_a=4       ,				_a=37       ,				_a=0.1       ,				_a=0.1       ,				_a=512       ,				_a=0.02       ,				_a=0       ,				_a=None       ,				)    ->     Optional[Any]:
        lowercase_					:       List[Any]     				=					parent
        lowercase_					:       List[Any]     				=					batch_size
        lowercase_					:       Optional[Any]     				=					seq_length
        lowercase_					:       Optional[Any]     				=					is_training
        lowercase_					:       Optional[int]     				=					use_input_mask
        lowercase_					:       Any     				=					use_labels
        lowercase_					:       Union[str, Any]     				=					vocab_size
        lowercase_					:       Union[str, Any]     				=					hidden_size
        lowercase_					:       str     				=					projection_dim
        lowercase_					:       str     				=					num_hidden_layers
        lowercase_					:       List[str]     				=					num_attention_heads
        lowercase_					:       List[str]     				=					intermediate_size
        lowercase_					:       Optional[Any]     				=					dropout
        lowercase_					:       Tuple     				=					attention_dropout
        lowercase_					:       Optional[Any]     				=					max_position_embeddings
        lowercase_					:       Any     				=					initializer_range
        lowercase_					:       List[str]     				=					scope
        lowercase_					:       Optional[Any]     				=					bos_token_id
   def 		_lowerCamelCase (self				)    ->     Tuple:
        lowercase_					:       Optional[Any]     				=					ids_tensor([self.batch_size, self.seq_length]       ,				self.vocab_size				)

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

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

        lowercase_					:       List[Any]     				=					self.get_config()

        return config, input_ids, tf.convert_to_tensor(_a				)
   def 		_lowerCamelCase (self				)    ->     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 		_lowerCamelCase (self       ,				_a       ,				_a       ,				_a				)    ->     List[Any]:
        lowercase_					:       List[Any]     				=					TFBlipTextModel(config=_a				)
        lowercase_					:       List[str]     				=					model(_a       ,				attention_mask=_a       ,				training=_a				)
        lowercase_					:       Union[str, Any]     				=					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				)    ->     Dict:
        lowercase_					:       Optional[Any]     				=					self.prepare_config_and_inputs()
        lowercase_				,lowercase_				,lowercase_					:       Dict     				=					config_and_inputs
        lowercase_					:       Optional[int]     				=					{'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict



@require_tf
class       UpperCAmelCase__  (       _snake_case					,     unittest.TestCase			):






   """simple docstring"""
   A							:       Any     =    (TFBlipTextModel,) if is_tf_available() else ()
   A							:       Union[str, Any]     =    False
   A							:       int     =    False
   A							:       str     =    False
   def 		_lowerCamelCase (self				)    ->     int:
        lowercase_					:       Tuple     				=					BlipTextModelTester(self				)
        lowercase_					:       str     				=					ConfigTester(self       ,				config_class=_a       ,				hidden_size=37				)
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        self.config_tester.run_common_tests()
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        lowercase_					:       str     				=					self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_a				)
   def 		_lowerCamelCase (self				)    ->     Dict:
        pass
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        pass
   @unittest.skip(reason='Blip does not use inputs_embeds'				)
   def 		_lowerCamelCase (self				)    ->     List[Any]:
        pass
   @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'				)
   def 		_lowerCamelCase (self				)    ->     Any:
        pass
   @unittest.skip(reason='BlipTextModel has no base class and is not available in MODEL_MAPPING'				)
   def 		_lowerCamelCase (self				)    ->     Optional[int]:
        pass
   @slow
   def 		_lowerCamelCase (self				)    ->     Optional[Any]:
        for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_					:       List[str]     				=					TFBlipTextModel.from_pretrained(_a				)
             self.assertIsNotNone(_a				)





   def 		_lowerCamelCase (self       ,				_a=True				)    ->     Optional[int]:
        super().test_pt_tf_model_equivalence(allow_missing_keys=_a				)




