



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







    def __init__( self					:    Union[str, Any]		,     __snake_case					:    str		,     __snake_case					:    Tuple=13		,     __snake_case					:    Union[str, Any]=7		,     __snake_case					:    str=False		,     __snake_case					:    Optional[int]=True		,     __snake_case					:    str=False		,     __snake_case					:    List[str]=True		,     __snake_case					:    Union[str, Any]=33		,     __snake_case					:    List[Any]=32		,     __snake_case					:    List[Any]=5		,     __snake_case					:    Tuple=4		,     __snake_case					:    Tuple=37		,     __snake_case					:    Tuple="gelu"		,     __snake_case					:    int=0.1		,     __snake_case					:    int=0.1		,     __snake_case					:    Dict=5_12		,     __snake_case					:    Tuple=16		,     __snake_case					:    Tuple=2		,     __snake_case					:    str=0.02		,     __snake_case					:    Optional[int]=3		,     __snake_case					:    str=4		,     __snake_case					:    List[Any]=None		,     ):
      a		:   List[str]	  =	parent
      a		:   List[str]	  =	batch_size
      a		:   List[str]	  =	seq_length
      a		:   int	  =	is_training
      a		:   Union[str, Any]	  =	use_input_mask
      a		:   int	  =	use_token_type_ids
      a		:   Tuple	  =	use_labels
      a		:   List[str]	  =	vocab_size
      a		:   Union[str, Any]	  =	hidden_size
      a		:   Optional[int]	  =	num_hidden_layers
      a		:   str	  =	num_attention_heads
      a		:   Tuple	  =	intermediate_size
      a		:   Optional[int]	  =	hidden_act
      a		:   Union[str, Any]	  =	hidden_dropout_prob
      a		:   int	  =	attention_probs_dropout_prob
      a		:   Dict	  =	max_position_embeddings
      a		:   Optional[int]	  =	type_vocab_size
      a		:   Union[str, Any]	  =	type_sequence_label_size
      a		:   int	  =	initializer_range
      a		:   int	  =	num_labels
      a		:   List[str]	  =	num_choices
      a		:   Optional[int]	  =	scope







    def      lowercase_			( self					:    Dict      ):
      a		:   str	  =	ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size      )

      a		:   str	  =	None
      if self.use_input_mask:
        a		:   Optional[Any]	  =	random_attention_mask([self.batch_size, self.seq_length]      )

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

      a		:   Tuple	  =	self.get_config()

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







    def      lowercase_			( 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      lowercase_			( self					:    int		,     __snake_case					:    Tuple		,     __snake_case					:    List[Any]		,     __snake_case					:    str		,     __snake_case					:    str		,     __snake_case					:    List[Any]		,     __snake_case					:    str      ):
      a		:   Optional[int]	  =	EsmModel(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Any	  =	model(__snake_case		,     attention_mask=__snake_case      )
      a		:   List[Any]	  =	model(__snake_case      )
      a		:   Any	  =	model(__snake_case      )

      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					:    Dict		,     __snake_case					:    int		,     __snake_case					:    Dict		,     __snake_case					:    Dict		,     __snake_case					:    Any		,     __snake_case					:    List[Any]		,     __snake_case					:    Union[str, Any]      ):
      a		:   List[str]	  =	EsmForMaskedLM(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   Union[str, Any]	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.vocab_size)      )







    def      lowercase_			( self					:    Union[str, Any]		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    str		,     __snake_case					:    str		,     __snake_case					:    int		,     __snake_case					:    Union[str, Any]		,     __snake_case					:    int      ):
      a		:   Any	  =	self.num_labels
      a		:   List[str]	  =	EsmForTokenClassification(config=__snake_case      )
      model.to(__snake_case      )
      model.eval()
      a		:   str	  =	model(__snake_case		,     attention_mask=__snake_case		,     labels=__snake_case      )
      self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.seq_length, self.num_labels)      )







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

@require_torch
class 				a__(   lowerCamelCase__						,					lowerCamelCase__						,					unittest.TestCase      ):
    lowercase__					=       False

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







    def      lowercase_			( self					:    str      ):
      a		:   Optional[Any]	  =	EsmModelTester(self      )
      a		:   Dict	  =	ConfigTester(self		,     config_class=__snake_case		,     hidden_size=37      )







    def      lowercase_			( self					:    Tuple      ):
      self.config_tester.run_common_tests()







    def      lowercase_			( self					:    Union[str, Any]      ):
      a		:   List[str]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__snake_case      )







    def      lowercase_			( self					:    List[str]      ):
      a		:   Union[str, Any]	  =	self.model_tester.prepare_config_and_inputs()
      for type in ["absolute", "relative_key", "relative_key_query"]:
        a		:   Any	  =	type
        self.model_tester.create_and_check_model(*__snake_case      )







    def      lowercase_			( self					:    Optional[int]      ):
      a		:   List[Any]	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_lm(*__snake_case      )







    def      lowercase_			( self					:    Tuple      ):
      a		:   str	  =	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_token_classification(*__snake_case      )







    @slow
    def      lowercase_			( self					:    Union[str, Any]      ):
      for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        a		:   List[str]	  =	EsmModel.from_pretrained(__snake_case      )
        self.assertIsNotNone(__snake_case      )







    def      lowercase_			( self					:    Optional[Any]      ):
      a		:   int	  =	self.model_tester.prepare_config_and_inputs()[0]
      a		:   Tuple	  =	EsmEmbeddings(config=__snake_case      )

      a		:   Union[str, Any]	  =	torch.as_tensor([[12, 31, 13, model.padding_idx]]      )
      a		:   int	  =	torch.as_tensor(
          [
              [
                  0 + model.padding_idx + 1,
                  1 + model.padding_idx + 1,
                  2 + model.padding_idx + 1,
                  model.padding_idx,
              ]
          ]      )
      a		:   List[str]	  =	create_position_ids_from_input_ids(__snake_case		,     model.padding_idx      )
      self.assertEqual(position_ids.shape		,     expected_positions.shape      )
      self.assertTrue(torch.all(torch.eq(__snake_case		,     __snake_case      )      )      )







    def      lowercase_			( self					:    str      ):
      a		:   Tuple	  =	self.model_tester.prepare_config_and_inputs()[0]
      a		:   Tuple	  =	EsmEmbeddings(config=__snake_case      )

      a		:   Optional[int]	  =	torch.empty(2		,     4		,     30      )
      a		:   Tuple	  =	[
          0 + embeddings.padding_idx + 1,
          1 + embeddings.padding_idx + 1,
          2 + embeddings.padding_idx + 1,
          3 + embeddings.padding_idx + 1,
      ]
      a		:   Union[str, Any]	  =	torch.as_tensor([expected_single_positions, expected_single_positions]      )
      a		:   Optional[Any]	  =	embeddings.create_position_ids_from_inputs_embeds(__snake_case      )
      self.assertEqual(position_ids.shape		,     expected_positions.shape      )
      self.assertTrue(torch.all(torch.eq(__snake_case		,     __snake_case      )      )      )







    @unittest.skip('Esm does not support embedding resizing'      )
    def      lowercase_			( self					:    Tuple      ):
      pass







    @unittest.skip('Esm does not support embedding resizing'      )
    def      lowercase_			( self					:    Optional[int]      ):
      pass







    @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'      )
    def      lowercase_			( self					:    Dict      ):
      pass

@require_torch
class 				a__(   lowerCamelCase__      ):







    @slow
    def      lowercase_			( self					:    Any      ):
      with torch.no_grad():
        a		:   Any	  =	EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'      )
        model.eval()
        a		:   List[Any]	  =	torch.tensor([[0, 1, 2, 3, 4, 5]]      )
        a		:   List[Any]	  =	model(__snake_case      )[0]

        a		:   Optional[int]	  =	33

        a		:   int	  =	torch.Size((1, 6, vocab_size)      )
        self.assertEqual(output.shape		,     __snake_case      )

        a		:   List[Any]	  =	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]		,     __snake_case		,     atol=1e-4      )      )







    @slow
    def      lowercase_			( self					:    str      ):
      with torch.no_grad():
        a		:   str	  =	EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'      )
        model.eval()

        a		:   List[str]	  =	torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]      )
        a		:   Optional[int]	  =	model(__snake_case      )[0]
        # compare the actual values for a slice.
        a		:   str	  =	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]		,     __snake_case		,     atol=1e-4      )      )




'''simple docstring'''






from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


lowerCAmelCase:   List[Any]					=		{'configuration_reformer': ['REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'ReformerConfig']}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   Dict					=		['ReformerTokenizer']

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   Tuple					=		['ReformerTokenizerFast']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     lowerCAmelCase:   List[Any]					=		[
         'REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
         'ReformerAttention',
         'ReformerForMaskedLM',
         'ReformerForQuestionAnswering',
         'ReformerForSequenceClassification',
         'ReformerLayer',
         'ReformerModel',
         'ReformerModelWithLMHead',
         'ReformerPreTrainedModel',
     ]


if TYPE_CHECKING:
     from .configuration_reformer import REFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, ReformerConfig

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_reformer import ReformerTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_reformer_fast import ReformerTokenizerFast

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_reformer import (
              REFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
              ReformerAttention,
              ReformerForMaskedLM,
              ReformerForQuestionAnswering,
              ReformerForSequenceClassification,
              ReformerLayer,
              ReformerModel,
              ReformerModelWithLMHead,
              ReformerPreTrainedModel,
          )

else:
     import sys

     lowerCAmelCase:   str					=		_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)