





'''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		,      a_		,      a_=13		,      a_=7		,      a_=False		,      a_=True		,      a_=False		,      a_=True		,      a_=33		,      a_=32		,      a_=5		,      a_=4		,      a_=37		,      a_="gelu"		,      a_=0.1		,      a_=0.1		,      a_=512		,      a_=16		,      a_=2		,      a_=0.02		,      a_=3		,      a_=4		,      a_=None		,      ):
			lowerCAmelCase     :    Union[str, Any]						=   parent
			lowerCAmelCase     :    List[str]						=   batch_size
			lowerCAmelCase     :    Any						=   seq_length
			lowerCAmelCase     :    List[Any]						=   is_training
			lowerCAmelCase     :    Optional[Any]						=   use_input_mask
			lowerCAmelCase     :    Tuple						=   use_token_type_ids
			lowerCAmelCase     :    Optional[int]						=   use_labels
			lowerCAmelCase     :    Tuple						=   vocab_size
			lowerCAmelCase     :    Optional[int]						=   hidden_size
			lowerCAmelCase     :    List[str]						=   num_hidden_layers
			lowerCAmelCase     :    List[str]						=   num_attention_heads
			lowerCAmelCase     :    Any						=   intermediate_size
			lowerCAmelCase     :    Dict						=   hidden_act
			lowerCAmelCase     :    int						=   hidden_dropout_prob
			lowerCAmelCase     :    Tuple						=   attention_probs_dropout_prob
			lowerCAmelCase     :    Any						=   max_position_embeddings
			lowerCAmelCase     :    Any						=   type_vocab_size
			lowerCAmelCase     :    List[Any]						=   type_sequence_label_size
			lowerCAmelCase     :    List[str]						=   initializer_range
			lowerCAmelCase     :    Optional[Any]						=   num_labels
			lowerCAmelCase     :    Union[str, Any]						=   num_choices
			lowerCAmelCase     :    Tuple						=   scope



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   ids_tensor([self.batch_size, self.seq_length]		,      self.vocab_size	)

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

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

			lowerCAmelCase     :    Union[str, Any]						=   self.get_config()

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



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



	def   _lowerCamelCase    (   self		,      a_		,      a_		,      a_		,      a_		,      a_		,      a_	):
			lowerCAmelCase     :    int						=   self.num_labels
			lowerCAmelCase     :    int						=   EsmForTokenClassification(config=a_	)
			model.to(a_	)
			model.eval()
			lowerCAmelCase     :    Tuple						=   model(a_		,      attention_mask=a_		,      labels=a_	)
			self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.seq_length, self.num_labels)	)





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






@require_torch
class 		lowerCamelCase  (      _A      ,   _A      ,   unittest.TestCase       ):
	snake_case_      =   False

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



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Dict						=   EsmModelTester(self	)
			lowerCAmelCase     :    str						=   ConfigTester(self		,      config_class=a_		,      hidden_size=37	)



	def   _lowerCamelCase    (   self	):
			self.config_tester.run_common_tests()



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Dict						=   self.model_tester.prepare_config_and_inputs()
			for type in ["absolute", "relative_key", "relative_key_query"]:
					lowerCAmelCase     :    Optional[int]						=   type
					self.model_tester.create_and_check_model(*a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*a_	)



	@slow
	def   _lowerCamelCase    (   self	):
			for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					lowerCAmelCase     :    List[Any]						=   EsmModel.from_pretrained(a_	)
					self.assertIsNotNone(a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   self.model_tester.prepare_config_and_inputs()[0]
			lowerCAmelCase     :    List[Any]						=   EsmEmbeddings(config=a_	)

			lowerCAmelCase     :    List[Any]						=   torch.as_tensor([[12, 31, 13, model.padding_idx]]	)
			lowerCAmelCase     :    Dict						=   torch.as_tensor(
			    [
			        [
			            0 + model.padding_idx + 1,
			            1 + model.padding_idx + 1,
			            2 + model.padding_idx + 1,
			            model.padding_idx,
			        ]
			    ]	)
			lowerCAmelCase     :    List[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   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   self.model_tester.prepare_config_and_inputs()[0]
			lowerCAmelCase     :    Any						=   EsmEmbeddings(config=a_	)

			lowerCAmelCase     :    int						=   torch.empty(2		,      4		,      30	)
			lowerCAmelCase     :    Union[str, Any]						=   [
			    0 + embeddings.padding_idx + 1,
			    1 + embeddings.padding_idx + 1,
			    2 + embeddings.padding_idx + 1,
			    3 + embeddings.padding_idx + 1,
			]
			lowerCAmelCase     :    Tuple						=   torch.as_tensor([expected_single_positions, expected_single_positions]	)
			lowerCAmelCase     :    Tuple						=   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   _lowerCamelCase    (   self	):
			pass



	@unittest.skip("Esm does not support embedding resizing"	)
	def   _lowerCamelCase    (   self	):
			pass





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






@require_torch
class 		lowerCamelCase  (      _A       ):



	@slow
	def   _lowerCamelCase    (   self	):
			with torch.no_grad():
					lowerCAmelCase     :    Dict						=   EsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"	)
					model.eval()
					lowerCAmelCase     :    List[str]						=   torch.tensor([[0, 1, 2, 3, 4, 5]]	)
					lowerCAmelCase     :    str						=   model(a_	)[0]

					lowerCAmelCase     :    int						=   33

					lowerCAmelCase     :    List[str]						=   torch.Size((1, 6, vocab_size)	)
					self.assertEqual(output.shape		,      a_	)

					lowerCAmelCase     :    Optional[int]						=   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]		,      a_		,      atol=1e-4	)	)





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

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










'''simple docstring'''






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class 		lowerCamelCase  (      _A       ):
	snake_case_      =   ["image_processor", "tokenizer"]
	snake_case_      =   "ChineseCLIPImageProcessor"
	snake_case_      =   ("BertTokenizer", "BertTokenizerFast")



	def __init__(   self		,      a_=None		,      a_=None		,      **a_	):
			lowerCAmelCase     :    Any						=   None
			if "feature_extractor" in kwargs:
					warnings.warn(
					    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
					    " instead."		,      a_		,      )
					lowerCAmelCase     :    Optional[Any]						=   kwargs.pop("feature_extractor"	)

			lowerCAmelCase     :    int						=   image_processor if image_processor is not None else feature_extractor
			if image_processor is None:
					raise ValueError("You need to specify an `image_processor`."	)
			if tokenizer is None:
					raise ValueError("You need to specify a `tokenizer`."	)

			super().__init__(a_		,      a_	)
			lowerCAmelCase     :    Dict						=   self.image_processor



	def __call__(   self		,      a_=None		,      a_=None		,      a_=None		,      **a_	):

			if text is None and images is None:
					raise ValueError("You have to specify either text or images. Both cannot be none."	)

			if text is not None:
					lowerCAmelCase     :    List[str]						=   self.tokenizer(a_		,      return_tensors=a_		,      **a_	)

			if images is not None:
					lowerCAmelCase     :    List[Any]						=   self.image_processor(a_		,      return_tensors=a_		,      **a_	)

			if text is not None and images is not None:
					lowerCAmelCase     :    str						=   image_features.pixel_values
					return encoding
			elif text is not None:
					return encoding
			else:
					return BatchEncoding(data=dict(**a_	)		,      tensor_type=a_	)



	def   _lowerCamelCase    (   self		,      *a_		,      **a_	):
			return self.tokenizer.batch_decode(*a_		,      **a_	)



	def   _lowerCamelCase    (   self		,      *a_		,      **a_	):
			return self.tokenizer.decode(*a_		,      **a_	)



	@property
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   self.tokenizer.model_input_names
			lowerCAmelCase     :    List[Any]						=   self.image_processor.model_input_names
			return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names	)	)





	@property
	def   _lowerCamelCase    (   self	):
			warnings.warn(
			    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."		,      a_		,      )
			return self.image_processor_class



