


'''simple docstring'''
import unittest

from transformers import EsmConfig, is_torch_available
from transformers.testing_utils import TestCasePlus, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
    from transformers.models.esm.modeling_esm import (
        ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
        EsmEmbeddings,
        create_position_ids_from_input_ids,
    )






class        lowerCAmelCase_  :






       def __init__( self			,     _UpperCamelCase			,     _UpperCamelCase=13			,     _UpperCamelCase=7			,     _UpperCamelCase=False			,     _UpperCamelCase=True			,     _UpperCamelCase=False			,     _UpperCamelCase=True			,     _UpperCamelCase=33			,     _UpperCamelCase=32			,     _UpperCamelCase=5			,     _UpperCamelCase=4			,     _UpperCamelCase=37			,     _UpperCamelCase="gelu"			,     _UpperCamelCase=0.1			,     _UpperCamelCase=0.1			,     _UpperCamelCase=512			,     _UpperCamelCase=16			,     _UpperCamelCase=2			,     _UpperCamelCase=0.02			,     _UpperCamelCase=3			,     _UpperCamelCase=4			,     _UpperCamelCase=None			,     )->      List[str]:
            _A				=							parent
            _A				=							batch_size
            _A				=							seq_length
            _A				=							is_training
            _A				=							use_input_mask
            _A				=							use_token_type_ids
            _A				=							use_labels
            _A				=							vocab_size
            _A				=							hidden_size
            _A				=							num_hidden_layers
            _A				=							num_attention_heads
            _A				=							intermediate_size
            _A				=							hidden_act
            _A				=							hidden_dropout_prob
            _A				=							attention_probs_dropout_prob
            _A				=							max_position_embeddings
            _A				=							type_vocab_size
            _A				=							type_sequence_label_size
            _A				=							initializer_range
            _A				=							num_labels
            _A				=							num_choices
            _A				=							scope






       def   UpperCamelCase						( self   )->      Dict:
            _A				=							ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size   )

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

            _A				=							None
            _A				=							None
            _A				=							None
            if self.use_labels:
                 _A				=							ids_tensor([self.batch_size]			,     self.type_sequence_label_size   )
                 _A				=							ids_tensor([self.batch_size, self.seq_length]			,     self.num_labels   )
                 _A				=							ids_tensor([self.batch_size]			,     self.num_choices   )

            _A				=							self.get_config()

            return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






       def   UpperCamelCase						( self   )->      List[str]:
            return EsmConfig(
                vocab_size=self.vocab_size			,     hidden_size=self.hidden_size			,     pad_token_id=1			,     num_hidden_layers=self.num_hidden_layers			,     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			,     )






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Tuple:
            _A				=							EsmModel(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							model(_UpperCamelCase			,     attention_mask=_UpperCamelCase   )
            _A				=							model(_UpperCamelCase   )
            _A				=							model(_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   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      List[Any]:
            _A				=							EsmForMaskedLM(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							model(_UpperCamelCase			,     attention_mask=_UpperCamelCase			,     labels=_UpperCamelCase   )
            self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)   )






       def   UpperCamelCase						( self			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase			,     _UpperCamelCase   )->      Optional[Any]:
            _A				=							self.num_labels
            _A				=							EsmForTokenClassification(config=_UpperCamelCase   )
            model.to(_UpperCamelCase   )
            model.eval()
            _A				=							model(_UpperCamelCase			,     attention_mask=_UpperCamelCase			,     labels=_UpperCamelCase   )
            self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.num_labels)   )






       def   UpperCamelCase						( self   )->      Optional[int]:
            _A				=							self.prepare_config_and_inputs()
            (
                (
                _A
            )		,				(
                _A
            )		,				(
                _A
            )		,				(
                _A
            )		,				(
                _A
            )		,				(
                _A
            )		,				
            )				=							config_and_inputs
            _A				=							{'input_ids': input_ids, 'attention_mask': input_mask}
            return config, inputs_dict






@require_torch
class        lowerCAmelCase_  (     UpperCAmelCase      ,							UpperCAmelCase      ,							unittest.TestCase	):
       __UpperCAmelCase					=False

       __UpperCAmelCase					=(
           (
               EsmForMaskedLM,
               EsmModel,
               EsmForSequenceClassification,
               EsmForTokenClassification,
           )
           if is_torch_available()
           else ()
       )
       __UpperCAmelCase					=()
       __UpperCAmelCase					=(
           {
               'feature-extraction': EsmModel,
               'fill-mask': EsmForMaskedLM,
               'text-classification': EsmForSequenceClassification,
               'token-classification': EsmForTokenClassification,
               'zero-shot': EsmForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       __UpperCAmelCase					=True






       def   UpperCamelCase						( self   )->      Optional[int]:
            _A				=							EsmModelTester(self   )
            _A				=							ConfigTester(self			,     config_class=_UpperCamelCase			,     hidden_size=37   )






       def   UpperCamelCase						( self   )->      Optional[Any]:
            self.config_tester.run_common_tests()






       def   UpperCamelCase						( self   )->      int:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      List[str]:
            _A				=							self.model_tester.prepare_config_and_inputs()
            for type in ["absolute", "relative_key", "relative_key_query"]:
                 _A				=							type
                 self.model_tester.create_and_check_model(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      Dict:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      List[str]:
            _A				=							self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*_UpperCamelCase   )






       @slow
       def   UpperCamelCase						( self   )->      Optional[int]:
            for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A				=							EsmModel.from_pretrained(_UpperCamelCase   )
                 self.assertIsNotNone(_UpperCamelCase   )






       def   UpperCamelCase						( self   )->      List[str]:
            _A				=							self.model_tester.prepare_config_and_inputs()[0]
            _A				=							EsmEmbeddings(config=_UpperCamelCase   )

            _A				=							torch.as_tensor([[12, 31, 13, model.padding_idx]]   )
            _A				=							torch.as_tensor(
                [
                    [
                        0 + model.padding_idx + 1,
                        1 + model.padding_idx + 1,
                        2 + model.padding_idx + 1,
                        model.padding_idx,
                    ]
                ]   )
            _A				=							create_position_ids_from_input_ids(_UpperCamelCase			,     model.padding_idx   )
            self.assertEqual(position_ids.shape			,     expected_positions.shape   )
            self.assertTrue(torch.all(torch.eq(_UpperCamelCase			,     _UpperCamelCase   )   )   )






       def   UpperCamelCase						( self   )->      Tuple:
            _A				=							self.model_tester.prepare_config_and_inputs()[0]
            _A				=							EsmEmbeddings(config=_UpperCamelCase   )

            _A				=							torch.empty(2			,     4			,     30   )
            _A				=							[
                0 + embeddings.padding_idx + 1,
                1 + embeddings.padding_idx + 1,
                2 + embeddings.padding_idx + 1,
                3 + embeddings.padding_idx + 1,
            ]
            _A				=							torch.as_tensor([expected_single_positions, expected_single_positions]   )
            _A				=							embeddings.create_position_ids_from_inputs_embeds(_UpperCamelCase   )
            self.assertEqual(position_ids.shape			,     expected_positions.shape   )
            self.assertTrue(torch.all(torch.eq(_UpperCamelCase			,     _UpperCamelCase   )   )   )






       @unittest.skip('Esm does not support embedding resizing'   )
       def   UpperCamelCase						( self   )->      Dict:
            pass






       @unittest.skip('Esm does not support embedding resizing'   )
       def   UpperCamelCase						( self   )->      int:
            pass






       @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'   )
       def   UpperCamelCase						( self   )->      Optional[Any]:
            pass






@require_torch
class        lowerCAmelCase_  (     UpperCAmelCase	):






       @slow
       def   UpperCamelCase						( self   )->      int:
            with torch.no_grad():
                 _A				=							EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'   )
                 model.eval()
                 _A				=							torch.tensor([[0, 1, 2, 3, 4, 5]]   )
                 _A				=							model(_UpperCamelCase   )[0]

                 _A				=							33

                 _A				=							torch.Size((1, 6, vocab_size)   )
                 self.assertEqual(output.shape			,     _UpperCamelCase   )

                 _A				=							torch.tensor(
                     [[[8.9215, -10.5898, -6.4671], [-6.3967, -13.9114, -1.1212], [-7.7812, -13.9516, -3.7406]]]   )
                 self.assertTrue(torch.allclose(output[:, :3, :3]			,     _UpperCamelCase			,     atol=1e-4   )   )






       @slow
       def   UpperCamelCase						( self   )->      Union[str, Any]:
            with torch.no_grad():
                 _A				=							EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'   )
                 model.eval()

                 _A				=							torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]   )
                 _A				=							model(_UpperCamelCase   )[0]
                 # compare the actual values for a slice.
                 _A				=							torch.tensor(
                     [[[0.1444, 0.5413, 0.3248], [0.3034, 0.0053, 0.3108], [0.3228, -0.2499, 0.3415]]]   )
                 self.assertTrue(torch.allclose(output[:, :3, :3]			,     _UpperCamelCase			,     atol=1e-4   )   )









'''simple docstring'''
def 					lowerCamelCase_ ( __UpperCamelCase					:       list							,    __UpperCamelCase					:       int							,    __UpperCamelCase					:       int = 0							,    __UpperCamelCase					:       int = 0		)					->	int:







     """simple docstring"""
     _A				=							right or len(__UpperCamelCase		) - 1
     if left > right:
          return -1
     elif list_data[left] == key:
          return left
     elif list_data[right] == key:
          return right
     else:
          return search(__UpperCamelCase							,    __UpperCamelCase							,    left + 1							,    right - 1		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()





