




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






			'''simple docstring'''



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

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

							_lowerCamelCase     :			str    =    None
							_lowerCamelCase     :			List[str]    =    None
							_lowerCamelCase     :			Tuple    =    None
							if self.use_labels:
											_lowerCamelCase     :			str    =    ids_tensor([self.batch_size]  ,      self.type_sequence_label_size  )
											_lowerCamelCase     :			List[Any]    =    ids_tensor([self.batch_size, self.seq_length]  ,      self.num_labels  )
											_lowerCamelCase     :			Union[str, Any]    =    ids_tensor([self.batch_size]  ,      self.num_choices  )

							_lowerCamelCase     :			List[str]    =    self.get_config()

							return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
			def 					A_    (  self  ):
							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  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			str    =    EsmModel(config=__a  )
							model.to(__a  )
							model.eval()
							_lowerCamelCase     :			List[Any]    =    model(__a  ,      attention_mask=__a  )
							_lowerCamelCase     :			Dict    =    model(__a  )
							_lowerCamelCase     :			Optional[int]    =    model(__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 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[str]    =    EsmForMaskedLM(config=__a  )
							model.to(__a  )
							model.eval()
							_lowerCamelCase     :			Any    =    model(__a  ,      attention_mask=__a  ,      labels=__a  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.vocab_size)  )
			def 					A_    (  self  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ,      lowercase  ):
							_lowerCamelCase     :			List[Any]    =    self.num_labels
							_lowerCamelCase     :			str    =    EsmForTokenClassification(config=__a  )
							model.to(__a  )
							model.eval()
							_lowerCamelCase     :			int    =    model(__a  ,      attention_mask=__a  ,      labels=__a  )
							self.parent.assertEqual(result.logits.shape  ,      (self.batch_size, self.seq_length, self.num_labels)  )






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






@require_torch
class lowerCAmelCase__	(     UpperCamelCase__,	UpperCamelCase__,	unittest.TestCase						):






			'''simple docstring'''



			lowerCamelCase__      =			False

			lowerCamelCase__      =			(
			    (
			        EsmForMaskedLM,
			        EsmModel,
			        EsmForSequenceClassification,
			        EsmForTokenClassification,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCamelCase__      =			()
			lowerCamelCase__      =			(
			    {
			        """feature-extraction""": EsmModel,
			        """fill-mask""": EsmForMaskedLM,
			        """text-classification""": EsmForSequenceClassification,
			        """token-classification""": EsmForTokenClassification,
			        """zero-shot""": EsmForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase__      =			True
			def 					A_    (  self  ):
							_lowerCamelCase     :			Optional[Any]    =    EsmModelTester(self  )
							_lowerCamelCase     :			Optional[int]    =    ConfigTester(self  ,      config_class=__a  ,      hidden_size=37  )
			def 					A_    (  self  ):
							self.config_tester.run_common_tests()
			def 					A_    (  self  ):
							_lowerCamelCase     :			str    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__a  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Tuple    =    self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
											_lowerCamelCase     :			str    =    type
											self.model_tester.create_and_check_model(*__a  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			int    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*__a  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			Dict    =    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*__a  )
			@slow
			def 					A_    (  self  ):
							for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											_lowerCamelCase     :			Tuple    =    EsmModel.from_pretrained(__a  )
											self.assertIsNotNone(__a  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    self.model_tester.prepare_config_and_inputs()[0]
							_lowerCamelCase     :			Optional[Any]    =    EsmEmbeddings(config=__a  )

							_lowerCamelCase     :			List[Any]    =    torch.as_tensor([[12, 31, 13, model.padding_idx]]  )
							_lowerCamelCase     :			int    =    torch.as_tensor(
							    [
							        [
							            0 + model.padding_idx + 1,
							            1 + model.padding_idx + 1,
							            2 + model.padding_idx + 1,
							            model.padding_idx,
							        ]
							    ]  )
							_lowerCamelCase     :			Any    =    create_position_ids_from_input_ids(__a  ,      model.padding_idx  )
							self.assertEqual(position_ids.shape  ,      expected_positions.shape  )
							self.assertTrue(torch.all(torch.eq(__a  ,      __a  )  )  )
			def 					A_    (  self  ):
							_lowerCamelCase     :			List[Any]    =    self.model_tester.prepare_config_and_inputs()[0]
							_lowerCamelCase     :			Tuple    =    EsmEmbeddings(config=__a  )

							_lowerCamelCase     :			Optional[int]    =    torch.empty(2  ,      4  ,      30  )
							_lowerCamelCase     :			Tuple    =    [
							    0 + embeddings.padding_idx + 1,
							    1 + embeddings.padding_idx + 1,
							    2 + embeddings.padding_idx + 1,
							    3 + embeddings.padding_idx + 1,
							]
							_lowerCamelCase     :			Any    =    torch.as_tensor([expected_single_positions, expected_single_positions]  )
							_lowerCamelCase     :			Any    =    embeddings.create_position_ids_from_inputs_embeds(__a  )
							self.assertEqual(position_ids.shape  ,      expected_positions.shape  )
							self.assertTrue(torch.all(torch.eq(__a  ,      __a  )  )  )
			@unittest.skip('Esm does not support embedding resizing'  )
			def 					A_    (  self  ):
							pass
			@unittest.skip('Esm does not support embedding resizing'  )
			def 					A_    (  self  ):
							pass






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






@require_torch
class lowerCAmelCase__	(     UpperCamelCase__						):






			'''simple docstring'''



			@slow
			def 					A_    (  self  ):
							with torch.no_grad():
											_lowerCamelCase     :			List[str]    =    EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'  )
											model.eval()
											_lowerCamelCase     :			List[str]    =    torch.tensor([[0, 1, 2, 3, 4, 5]]  )
											_lowerCamelCase     :			Optional[int]    =    model(__a  )[0]

											_lowerCamelCase     :			Optional[Any]    =    33

											_lowerCamelCase     :			str    =    torch.Size((1, 6, vocab_size)  )
											self.assertEqual(output.shape  ,      __a  )

											_lowerCamelCase     :			List[Any]    =    torch.tensor(
											    [[[8.92_15, -10.58_98, -6.46_71], [-6.39_67, -13.91_14, -1.12_12], [-7.78_12, -13.95_16, -3.74_06]]]  )
											self.assertTrue(torch.allclose(output[:, :3, :3]  ,      __a  ,      atol=1E-4  )  )






			@slow
			def 					A_    (  self  ):
							with torch.no_grad():
											_lowerCamelCase     :			Dict    =    EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'  )
											model.eval()

											_lowerCamelCase     :			str    =    torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]  )
											_lowerCamelCase     :			Tuple    =    model(__a  )[0]
											# compare the actual values for a slice.
											_lowerCamelCase     :			Tuple    =    torch.tensor(
											    [[[0.14_44, 0.54_13, 0.32_48], [0.30_34, 0.00_53, 0.31_08], [0.32_28, -0.24_99, 0.34_15]]]  )
											self.assertTrue(torch.allclose(output[:, :3, :3]  ,      __a  ,      atol=1E-4  )  )
'''simple docstring'''





from __future__ import annotations





def 	lowerCAmelCase_				(					snake_case_       : list					,    snake_case_       : int | None = None					,    snake_case_       : int | None = None			)    ->      None:


     '''simple docstring'''




     if start is None:
          UpperCAmelCase_           =   0

     if end is None:
          UpperCAmelCase_           =   len(snake_case_			) - 1

     if start >= end:
          return

     UpperCAmelCase_           =   (start + end) // 2

     slowsort(snake_case_					,    snake_case_					,    snake_case_			)
     slowsort(snake_case_					,    mid + 1					,    snake_case_			)

     if sequence[end] < sequence[mid]:
          UpperCAmelCase_    , UpperCAmelCase_           =   sequence[mid], sequence[end]

     slowsort(snake_case_					,    snake_case_					,    end - 1			)


if __name__ == "__main__":
    from doctest import testmod

    testmod()




