
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 		__magic_name__ :





		def __init__(				self     :						Tuple    ,	lowerCamelCase__     :						Dict    ,	lowerCamelCase__     :						Dict=1_3    ,	lowerCamelCase__     :						str=7    ,	lowerCamelCase__     :						Tuple=False    ,	lowerCamelCase__     :						Tuple=True    ,	lowerCamelCase__     :						str=False    ,	lowerCamelCase__     :						Optional[int]=True    ,	lowerCamelCase__     :						Optional[int]=3_3    ,	lowerCamelCase__     :						List[str]=3_2    ,	lowerCamelCase__     :						Union[str, Any]=5    ,	lowerCamelCase__     :						List[str]=4    ,	lowerCamelCase__     :						Dict=3_7    ,	lowerCamelCase__     :						Union[str, Any]="gelu"    ,	lowerCamelCase__     :						Dict=0.1    ,	lowerCamelCase__     :						Optional[Any]=0.1    ,	lowerCamelCase__     :						List[str]=5_1_2    ,	lowerCamelCase__     :						Union[str, Any]=1_6    ,	lowerCamelCase__     :						Optional[Any]=2    ,	lowerCamelCase__     :						List[Any]=0.0_2    ,	lowerCamelCase__     :						str=3    ,	lowerCamelCase__     :						Any=4    ,	lowerCamelCase__     :						List[Any]=None    ,	):
							lowerCAmelCase		:       Union[str, Any]							=  parent
							lowerCAmelCase		:       Optional[Any]							=  batch_size
							lowerCAmelCase		:       List[Any]							=  seq_length
							lowerCAmelCase		:       str							=  is_training
							lowerCAmelCase		:       Dict							=  use_input_mask
							lowerCAmelCase		:       List[str]							=  use_token_type_ids
							lowerCAmelCase		:       Optional[Any]							=  use_labels
							lowerCAmelCase		:       Dict							=  vocab_size
							lowerCAmelCase		:       Tuple							=  hidden_size
							lowerCAmelCase		:       Dict							=  num_hidden_layers
							lowerCAmelCase		:       Dict							=  num_attention_heads
							lowerCAmelCase		:       Optional[Any]							=  intermediate_size
							lowerCAmelCase		:       Dict							=  hidden_act
							lowerCAmelCase		:       Optional[Any]							=  hidden_dropout_prob
							lowerCAmelCase		:       Union[str, Any]							=  attention_probs_dropout_prob
							lowerCAmelCase		:       Dict							=  max_position_embeddings
							lowerCAmelCase		:       Dict							=  type_vocab_size
							lowerCAmelCase		:       int							=  type_sequence_label_size
							lowerCAmelCase		:       List[Any]							=  initializer_range
							lowerCAmelCase		:       Any							=  num_labels
							lowerCAmelCase		:       int							=  num_choices
							lowerCAmelCase		:       Any							=  scope





		def    _A				(				self     :						Union[str, Any]					):
							lowerCAmelCase		:       Optional[int]							=  ids_tensor([self.batch_size, self.seq_length]    ,	self.vocab_size					)

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

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

							lowerCAmelCase		:       str							=  self.get_config()

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





		def    _A				(				self     :						Tuple					):
							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     :						Tuple    ,	lowerCamelCase__     :						List[Any]    ,	lowerCamelCase__     :						Any    ,	lowerCamelCase__     :						str    ,	lowerCamelCase__     :						Union[str, Any]    ,	lowerCamelCase__     :						List[Any]    ,	lowerCamelCase__     :						Tuple					):
							lowerCAmelCase		:       List[Any]							=  EsmModel(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							lowerCAmelCase		:       int							=  model(lowerCamelCase__    ,	attention_mask=lowerCamelCase__					)
							lowerCAmelCase		:       Dict							=  model(lowerCamelCase__					)
							lowerCAmelCase		:       int							=  model(lowerCamelCase__					)

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





		def    _A				(				self     :						Union[str, Any]    ,	lowerCamelCase__     :						int    ,	lowerCamelCase__     :						Dict    ,	lowerCamelCase__     :						Tuple    ,	lowerCamelCase__     :						Any    ,	lowerCamelCase__     :						Any    ,	lowerCamelCase__     :						Tuple					):
							lowerCAmelCase		:       Optional[Any]							=  self.num_labels
							lowerCAmelCase		:       Optional[int]							=  EsmForTokenClassification(config=lowerCamelCase__					)
							model.to(lowerCamelCase__					)
							model.eval()
							lowerCAmelCase		:       Any							=  model(lowerCamelCase__    ,	attention_mask=lowerCamelCase__    ,	labels=lowerCamelCase__					)
							self.parent.assertEqual(result.logits.shape    ,	(self.batch_size, self.seq_length, self.num_labels)					)





		def    _A				(				self     :						Dict					):
							lowerCAmelCase		:       Any							=  self.prepare_config_and_inputs()
							(
							    (
							    lowerCAmelCase      
							)						,				(
							    lowerCAmelCase      
							)						,				(
							    lowerCAmelCase      
							)						,				(
							    lowerCAmelCase      
							)						,				(
							    lowerCAmelCase      
							)						,				(
							    lowerCAmelCase      
							)						,				      
							)		:       Optional[Any]							=  config_and_inputs
							lowerCAmelCase		:       List[Any]							=  {'''input_ids''': input_ids, '''attention_mask''': input_mask}
							return config, inputs_dict



@require_torch
class 		__magic_name__ (    _lowerCAmelCase,					_lowerCAmelCase,					unittest.TestCase     ):
		_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     :						Tuple					):
							lowerCAmelCase		:       Dict							=  EsmModelTester(self					)
							lowerCAmelCase		:       Optional[int]							=  ConfigTester(self    ,	config_class=lowerCamelCase__    ,	hidden_size=3_7					)





		def    _A				(				self     :						List[str]					):
							self.config_tester.run_common_tests()





		def    _A				(				self     :						Optional[int]					):
							lowerCAmelCase		:       int							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*lowerCamelCase__					)





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       List[Any]							=  self.model_tester.prepare_config_and_inputs()
							for type in ["absolute", "relative_key", "relative_key_query"]:
												lowerCAmelCase		:       int							=  type
												self.model_tester.create_and_check_model(*lowerCamelCase__					)





		def    _A				(				self     :						Dict					):
							lowerCAmelCase		:       Tuple							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase__					)





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       Union[str, Any]							=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*lowerCamelCase__					)





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





		def    _A				(				self     :						Optional[Any]					):
							lowerCAmelCase		:       List[str]							=  self.model_tester.prepare_config_and_inputs()[0]
							lowerCAmelCase		:       int							=  EsmEmbeddings(config=lowerCamelCase__					)

							lowerCAmelCase		:       List[str]							=  torch.as_tensor([[1_2, 3_1, 1_3, model.padding_idx]]					)
							lowerCAmelCase		:       str							=  torch.as_tensor(
							    [
							        [
							            0 + model.padding_idx + 1,
							            1 + model.padding_idx + 1,
							            2 + model.padding_idx + 1,
							            model.padding_idx,
							        ]
							    ]					)
							lowerCAmelCase		:       int							=  create_position_ids_from_input_ids(lowerCamelCase__    ,	model.padding_idx					)
							self.assertEqual(position_ids.shape    ,	expected_positions.shape					)
							self.assertTrue(torch.all(torch.eq(lowerCamelCase__    ,	lowerCamelCase__					)					)					)





		def    _A				(				self     :						List[Any]					):
							lowerCAmelCase		:       Optional[Any]							=  self.model_tester.prepare_config_and_inputs()[0]
							lowerCAmelCase		:       Any							=  EsmEmbeddings(config=lowerCamelCase__					)

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





		@unittest.skip('''Esm does not support embedding resizing'''					)
		def    _A				(				self     :						Union[str, Any]					):
							pass





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





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



@require_torch
class 		__magic_name__ (    _lowerCAmelCase     ):





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

												lowerCAmelCase		:       List[str]							=  3_3

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

												lowerCAmelCase		:       int							=  torch.tensor(
												    [[[8.9_2_1_5, -1_0.5_8_9_8, -6.4_6_7_1], [-6.3_9_6_7, -1_3.9_1_1_4, -1.1_2_1_2], [-7.7_8_1_2, -1_3.9_5_1_6, -3.7_4_0_6]]]					)
												self.assertTrue(torch.allclose(output[:, :3, :3]    ,	lowerCamelCase__    ,	atol=1E-4					)					)





		@slow
		def    _A				(				self     :						List[Any]					):
							with torch.no_grad():
												lowerCAmelCase		:       Tuple							=  EsmModel.from_pretrained('''facebook/esm2_t6_8M_UR50D'''					)
												model.eval()

												lowerCAmelCase		:       Tuple							=  torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]					)
												lowerCAmelCase		:       str							=  model(lowerCamelCase__					)[0]
												# compare the actual values for a slice.
												lowerCAmelCase		:       Optional[Any]							=  torch.tensor(
												    [[[0.1_4_4_4, 0.5_4_1_3, 0.3_2_4_8], [0.3_0_3_4, 0.0_0_5_3, 0.3_1_0_8], [0.3_2_2_8, -0.2_4_9_9, 0.3_4_1_5]]]					)
												self.assertTrue(torch.allclose(output[:, :3, :3]    ,	lowerCamelCase__    ,	atol=1E-4					)					)








'''simple docstring'''



import copy
import random

from transformers import CLIPTokenizer
class 				__SCREAMING_SNAKE_CASE							(				_lowerCAmelCase  ):




      def __init__(	self			, *lowerCamelCase			, **lowerCamelCase   )				->Union[str, Any]:



         '''simple docstring'''
         super().__init__(*lowerCamelCase			, **lowerCamelCase   )
         __a					  =     {}




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )				->Dict:



         '''simple docstring'''
         __a					  =     super().add_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
         if num_added_tokens == 0:
            raise ValueError(
                F"""The tokenizer already contains the token {placeholder_token}. Please pass a different"""
                ' `placeholder_token` that is not already in the tokenizer.'   )




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=1			, **lowerCamelCase   )				->List[str]:



         '''simple docstring'''
         __a					  =     []
         if num_vec_per_token == 1:
            self.try_adding_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
            output.append(lowerCamelCase   )
         else:
            __a					  =     []
            for i in range(lowerCamelCase   ):
               __a					  =     placeholder_token + F"""_{i}"""
               self.try_adding_tokens(lowerCamelCase			, *lowerCamelCase			, **lowerCamelCase   )
               output.append(lowerCamelCase   )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
         for token in self.token_map:
            if token in placeholder_token:
               raise ValueError(
                   F"""The tokenizer already has placeholder token {token} that can get confused with"""
                   F""" {placeholder_token}keep placeholder tokens independent"""   )
         __a					  =     output




      def 			__UpperCamelCase				(	self			, lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0   )				->int:



         '''simple docstring'''
         if isinstance(lowerCamelCase			, lowerCamelCase   ):
            __a					  =     []
            for i in range(len(lowerCamelCase   )   ):
               output.append(self.replace_placeholder_tokens_in_text(text[i]			, vector_shuffle=lowerCamelCase   )   )
            return output
         for placeholder_token in self.token_map:
            if placeholder_token in text:
               __a					  =     self.token_map[placeholder_token]
               __a					  =     tokens[: 1 + int(len(lowerCamelCase   ) * prop_tokens_to_load   )]
               if vector_shuffle:
                  __a					  =     copy.copy(lowerCamelCase   )
                  random.shuffle(lowerCamelCase   )
               __a					  =     text.replace(lowerCamelCase			, ' '.join(lowerCamelCase   )   )
         return text




      def __call__(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0			, **lowerCamelCase   )				->Optional[int]:



         '''simple docstring'''
         return super().__call__(
             self.replace_placeholder_tokens_in_text(
                 lowerCamelCase			, vector_shuffle=lowerCamelCase			, prop_tokens_to_load=lowerCamelCase   )			, *lowerCamelCase			, **lowerCamelCase			, )






      def 			__UpperCamelCase				(	self			, lowerCamelCase			, *lowerCamelCase			, lowerCamelCase=False			, lowerCamelCase=1.0			, **lowerCamelCase   )				->List[Any]:



         '''simple docstring'''
         return super().encode(
             self.replace_placeholder_tokens_in_text(
                 lowerCamelCase			, vector_shuffle=lowerCamelCase			, prop_tokens_to_load=lowerCamelCase   )			, *lowerCamelCase			, **lowerCamelCase			, )