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 ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_=13 ,					SCREAMING_SNAKE_CASE_=7 ,					SCREAMING_SNAKE_CASE_=False ,					SCREAMING_SNAKE_CASE_=True ,					SCREAMING_SNAKE_CASE_=False ,					SCREAMING_SNAKE_CASE_=True ,					SCREAMING_SNAKE_CASE_=33 ,					SCREAMING_SNAKE_CASE_=32 ,					SCREAMING_SNAKE_CASE_=5 ,					SCREAMING_SNAKE_CASE_=4 ,					SCREAMING_SNAKE_CASE_=37 ,					SCREAMING_SNAKE_CASE_="gelu" ,					SCREAMING_SNAKE_CASE_=0.1 ,					SCREAMING_SNAKE_CASE_=0.1 ,					SCREAMING_SNAKE_CASE_=512 ,					SCREAMING_SNAKE_CASE_=16 ,					SCREAMING_SNAKE_CASE_=2 ,					SCREAMING_SNAKE_CASE_=0.02 ,					SCREAMING_SNAKE_CASE_=3 ,					SCREAMING_SNAKE_CASE_=4 ,					SCREAMING_SNAKE_CASE_=None ,					)		->   List[str]:





       '''simple docstring'''





       UpperCamelCase__					    =   parent
       UpperCamelCase__					    =   batch_size
       UpperCamelCase__					    =   seq_length
       UpperCamelCase__					    =   is_training
       UpperCamelCase__					    =   use_input_mask
       UpperCamelCase__					    =   use_token_type_ids
       UpperCamelCase__					    =   use_labels
       UpperCamelCase__					    =   vocab_size
       UpperCamelCase__					    =   hidden_size
       UpperCamelCase__					    =   num_hidden_layers
       UpperCamelCase__					    =   num_attention_heads
       UpperCamelCase__					    =   intermediate_size
       UpperCamelCase__					    =   hidden_act
       UpperCamelCase__					    =   hidden_dropout_prob
       UpperCamelCase__					    =   attention_probs_dropout_prob
       UpperCamelCase__					    =   max_position_embeddings
       UpperCamelCase__					    =   type_vocab_size
       UpperCamelCase__					    =   type_sequence_label_size
       UpperCamelCase__					    =   initializer_range
       UpperCamelCase__					    =   num_labels
       UpperCamelCase__					    =   num_choices
       UpperCamelCase__					    =   scope






  def 		_a       (self						)		->   Dict:





       '''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]						)

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

       UpperCamelCase__					    =   self.get_config()

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






  def 		_a       (self						)		->   Optional[Any]:





       '''simple docstring'''





       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 		_a       (self ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_						)		->   Optional[int]:





       '''simple docstring'''





       UpperCamelCase__					    =   EsmModel(config=__UpperCamelCase						)
       model.to(__UpperCamelCase						)
       model.eval()
       UpperCamelCase__					    =   model(__UpperCamelCase ,					attention_mask=__UpperCamelCase						)
       UpperCamelCase__					    =   model(__UpperCamelCase						)
       UpperCamelCase__					    =   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 		_a       (self ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_						)		->   int:





       '''simple docstring'''





       UpperCamelCase__					    =   EsmForMaskedLM(config=__UpperCamelCase						)
       model.to(__UpperCamelCase						)
       model.eval()
       UpperCamelCase__					    =   model(__UpperCamelCase ,					attention_mask=__UpperCamelCase ,					labels=__UpperCamelCase						)
       self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.vocab_size)						)






  def 		_a       (self ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_ ,					SCREAMING_SNAKE_CASE_						)		->   List[Any]:





       '''simple docstring'''





       UpperCamelCase__					    =   self.num_labels
       UpperCamelCase__					    =   EsmForTokenClassification(config=__UpperCamelCase						)
       model.to(__UpperCamelCase						)
       model.eval()
       UpperCamelCase__					    =   model(__UpperCamelCase ,					attention_mask=__UpperCamelCase ,					labels=__UpperCamelCase						)
       self.parent.assertEqual(result.logits.shape ,					(self.batch_size, self.seq_length, self.num_labels)						)






  def 		_a       (self						)		->   Optional[int]:





       '''simple docstring'''





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



@require_torch
class      _A (     a_						,			a_						,			unittest.TestCase				):
  SCREAMING_SNAKE_CASE_						:						str    =False

  SCREAMING_SNAKE_CASE_						:						Optional[Any]    =(
      (
          EsmForMaskedLM,
          EsmModel,
          EsmForSequenceClassification,
          EsmForTokenClassification,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE_						:						Union[str, Any]    =()
  SCREAMING_SNAKE_CASE_						:						str    =(
      {
          "feature-extraction": EsmModel,
          "fill-mask": EsmForMaskedLM,
          "text-classification": EsmForSequenceClassification,
          "token-classification": EsmForTokenClassification,
          "zero-shot": EsmForSequenceClassification,
      }
      if is_torch_available()
      else {}
  )
  SCREAMING_SNAKE_CASE_						:						Union[str, Any]    =True






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





       '''simple docstring'''





       UpperCamelCase__					    =   EsmModelTester(self						)
       UpperCamelCase__					    =   ConfigTester(self ,					config_class=__UpperCamelCase ,					hidden_size=37						)






  def 		_a       (self						)		->   Dict:





       '''simple docstring'''





       self.config_tester.run_common_tests()






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





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*__UpperCamelCase						)






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





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()
       for type in ["absolute", "relative_key", "relative_key_query"]:
            UpperCamelCase__					    =   type
            self.model_tester.create_and_check_model(*__UpperCamelCase						)






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





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase						)






  def 		_a       (self						)		->   Dict:





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase						)






  @slow
  def 		_a       (self						)		->   List[str]:





       '''simple docstring'''





       for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCamelCase__					    =   EsmModel.from_pretrained(__UpperCamelCase						)
            self.assertIsNotNone(__UpperCamelCase						)






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





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()[0]
       UpperCamelCase__					    =   EsmEmbeddings(config=__UpperCamelCase						)

       UpperCamelCase__					    =   torch.as_tensor([[12, 31, 13, model.padding_idx]]						)
       UpperCamelCase__					    =   torch.as_tensor(
           [
               [
                   0 + model.padding_idx + 1,
                   1 + model.padding_idx + 1,
                   2 + model.padding_idx + 1,
                   model.padding_idx,
               ]
           ]						)
       UpperCamelCase__					    =   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 		_a       (self						)		->   List[str]:





       '''simple docstring'''





       UpperCamelCase__					    =   self.model_tester.prepare_config_and_inputs()[0]
       UpperCamelCase__					    =   EsmEmbeddings(config=__UpperCamelCase						)

       UpperCamelCase__					    =   torch.empty(2 ,					4 ,					30						)
       UpperCamelCase__					    =   [
           0 + embeddings.padding_idx + 1,
           1 + embeddings.padding_idx + 1,
           2 + embeddings.padding_idx + 1,
           3 + embeddings.padding_idx + 1,
       ]
       UpperCamelCase__					    =   torch.as_tensor([expected_single_positions, expected_single_positions]						)
       UpperCamelCase__					    =   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 		_a       (self						)		->   Optional[int]:





       '''simple docstring'''





       pass






  @unittest.skip('''Esm does not support embedding resizing'''						)
  def 		_a       (self						)		->   Any:





       '''simple docstring'''





       pass






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





       '''simple docstring'''





       pass



@require_torch
class      _A (     a_				):






  @slow
  def 		_a       (self						)		->   List[Any]:





       '''simple docstring'''





       with torch.no_grad():
            UpperCamelCase__					    =   EsmForMaskedLM.from_pretrained('''facebook/esm2_t6_8M_UR50D'''						)
            model.eval()
            UpperCamelCase__					    =   torch.tensor([[0, 1, 2, 3, 4, 5]]						)
            UpperCamelCase__					    =   model(__UpperCamelCase						)[0]

            UpperCamelCase__					    =   33

            UpperCamelCase__					    =   torch.Size((1, 6, vocab_size)						)
            self.assertEqual(output.shape ,					__UpperCamelCase						)

            UpperCamelCase__					    =   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 		_a       (self						)		->   Tuple:





       '''simple docstring'''





       with torch.no_grad():
            UpperCamelCase__					    =   EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''						)
            model.eval()

            UpperCamelCase__					    =   torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]						)
            UpperCamelCase__					    =   model(__UpperCamelCase						)[0]
            # compare the actual values for a slice.
            UpperCamelCase__					    =   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						)						)











import itertools
import string
from collections.abc import Generator, Iterable





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case							):
     __lowerCAmelCase             =       iter(__snake_case							)
     while True:
          __lowerCAmelCase             =       tuple(itertools.islice(__snake_case       ,			__snake_case							)							)
          if not chunk:
               return
          yield chunk





def    __lowerCAmelCase   (    __snake_case							):
     __lowerCAmelCase             =       "".join([c.upper() for c in dirty if c in string.ascii_letters]							)
     __lowerCAmelCase             =       ""

     if len(__snake_case							) < 2:
          return dirty

     for i in range(len(__snake_case							) - 1							):
          clean += dirty[i]

          if dirty[i] == dirty[i + 1]:
               clean += "X"

     clean += dirty[-1]

     if len(__snake_case							) & 1:
          clean += "X"

     return clean





def    __lowerCAmelCase   (    __snake_case							):
     # I and J are used interchangeably to allow
     # us to use a 5x5 table (25 letters)
     __lowerCAmelCase             =       "ABCDEFGHIKLMNOPQRSTUVWXYZ"
     # we're using a list instead of a '2d' array because it makes the math
     # for setting up the table and doing the actual encoding/decoding simpler
     __lowerCAmelCase             =       []

     # copy key chars into the table if they are in `alphabet` ignoring duplicates
     for char in key.upper():
          if char not in table and char in alphabet:
               table.append(__snake_case							)

    # fill the rest of the table in with the remaining alphabet chars
     for char in alphabet:
          if char not in table:
               table.append(__snake_case							)

     return table





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case							):
     __lowerCAmelCase             =       generate_table(__snake_case							)
     __lowerCAmelCase             =       prepare_input(__snake_case							)
     __lowerCAmelCase             =       ""

     # https://en.wikipedia.org/wiki/Playfair_cipher#Description
     for chara, chara in chunker(__snake_case       ,			2							):
          __lowerCAmelCase      ,			__lowerCAmelCase             =       divmod(table.index(__snake_case							)       ,			5							)
          __lowerCAmelCase      ,			__lowerCAmelCase             =       divmod(table.index(__snake_case							)       ,			5							)

          if rowa == rowa:
               ciphertext += table[rowa * 5 + (cola + 1) % 5]
               ciphertext += table[rowa * 5 + (cola + 1) % 5]
          elif cola == cola:
               ciphertext += table[((rowa + 1) % 5) * 5 + cola]
               ciphertext += table[((rowa + 1) % 5) * 5 + cola]
          else:  # rectangle
               ciphertext += table[rowa * 5 + cola]
               ciphertext += table[rowa * 5 + cola]

     return ciphertext





def    __lowerCAmelCase   (    __snake_case       ,			__snake_case							):
     __lowerCAmelCase             =       generate_table(__snake_case							)
     __lowerCAmelCase             =       ""

     # https://en.wikipedia.org/wiki/Playfair_cipher#Description
     for chara, chara in chunker(__snake_case       ,			2							):
          __lowerCAmelCase      ,			__lowerCAmelCase             =       divmod(table.index(__snake_case							)       ,			5							)
          __lowerCAmelCase      ,			__lowerCAmelCase             =       divmod(table.index(__snake_case							)       ,			5							)

          if rowa == rowa:
               plaintext += table[rowa * 5 + (cola - 1) % 5]
               plaintext += table[rowa * 5 + (cola - 1) % 5]
          elif cola == cola:
               plaintext += table[((rowa - 1) % 5) * 5 + cola]
               plaintext += table[((rowa - 1) % 5) * 5 + cola]
          else:  # rectangle
               plaintext += table[rowa * 5 + cola]
               plaintext += table[rowa * 5 + cola]

     return plaintext






