



import re
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin


class 	__snake_case   ( SCREAMING_SNAKE_CASE ):
		SCREAMING_SNAKE_CASE__								=	['image_processor', 'tokenizer']
		SCREAMING_SNAKE_CASE__								=	'AutoImageProcessor'
		SCREAMING_SNAKE_CASE__								=	'AutoTokenizer'
		def __init__(							self    ,a_=None    ,a_=None    ,**a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  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__		  =  kwargs.pop('feature_extractor'						)

						lowerCAmelCase__		  =  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__		  =  self.image_processor
						lowerCAmelCase__		  =  False
		def __call__(							self    ,*a_    ,**a_						):







						"""simple docstring"""
						# For backward compatibility
						if self._in_target_context_manager:
										return self.current_processor(*a_    ,**a_						)

						lowerCAmelCase__		  =  kwargs.pop('images'    ,a_						)
						lowerCAmelCase__		  =  kwargs.pop('text'    ,a_						)
						if len(a_						) > 0:
										lowerCAmelCase__		  =  args[0]
										lowerCAmelCase__		  =  args[1:]

						if images is None and text is None:
										raise ValueError('You need to specify either an `images` or `text` input to process.'						)

						if images is not None:
										lowerCAmelCase__		  =  self.image_processor(a_    ,*a_    ,**a_						)
						if text is not None:
										lowerCAmelCase__		  =  self.tokenizer(a_    ,**a_						)

						if text is None:
										return inputs
						elif images is None:
										return encodings
						else:
										lowerCAmelCase__		  =  encodings['input_ids']
										return inputs
		def 						SCREAMING_SNAKE_CASE_					(							self    ,*a_    ,**a_						):







						"""simple docstring"""
						return self.tokenizer.batch_decode(*a_    ,**a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,*a_    ,**a_						):







						"""simple docstring"""
						return self.tokenizer.decode(*a_    ,**a_						)
		@contextmanager
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						warnings.warn(
						    '`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your '
						    'labels by using the argument `text` of the regular `__call__` method (either in the same call as '
						    'your images inputs, or in a separate call.'						)
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  self.tokenizer
						yield
						lowerCAmelCase__		  =  self.image_processor
						lowerCAmelCase__		  =  False
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_=False    ,a_=None						):







						"""simple docstring"""
						if added_vocab is None:
										lowerCAmelCase__		  =  self.tokenizer.get_added_vocab()

						lowerCAmelCase__		  =  {}

						while tokens:
										lowerCAmelCase__		  =  re.search(r'<s_(.*?)>'    ,a_    ,re.IGNORECASE						)
										if start_token is None:
														break
										lowerCAmelCase__		  =  start_token.group(1						)
										lowerCAmelCase__		  =  re.search(rf'</s_{key}>'    ,a_    ,re.IGNORECASE						)
										lowerCAmelCase__		  =  start_token.group()
										if end_token is None:
														lowerCAmelCase__		  =  tokens.replace(a_    ,''						)
										else:
														lowerCAmelCase__		  =  end_token.group()
														lowerCAmelCase__		  =  re.escape(a_						)
														lowerCAmelCase__		  =  re.escape(a_						)
														lowerCAmelCase__		  =  re.search(f'{start_token_escaped}(.*?){end_token_escaped}'    ,a_    ,re.IGNORECASE						)
														if content is not None:
																		lowerCAmelCase__		  =  content.group(1						).strip()
																		if r"<s_" in content and r"</s_" in content:  # non-leaf node
																						lowerCAmelCase__		  =  self.tokenajson(a_    ,is_inner_value=a_    ,added_vocab=a_						)
																						if value:
																										if len(a_						) == 1:
																														lowerCAmelCase__		  =  value[0]
																										lowerCAmelCase__		  =  value
																		else:  # leaf nodes
																						lowerCAmelCase__		  =  []
																						for leaf in content.split(r'<sep/>'						):
																										lowerCAmelCase__		  =  leaf.strip()
																										if leaf in added_vocab and leaf[0] == "<" and leaf[-2:] == "/>":
																														lowerCAmelCase__		  =  leaf[1:-2]  # for categorical special tokens
																										output[key].append(a_						)
																						if len(output[key]						) == 1:
																										lowerCAmelCase__		  =  output[key][0]

														lowerCAmelCase__		  =  tokens[tokens.find(a_						) + len(a_						) :].strip()
														if tokens[:6] == r"<sep/>":  # non-leaf nodes
																		return [output] + self.tokenajson(tokens[6:]    ,is_inner_value=a_    ,added_vocab=a_						)

						if len(a_						):
										return [output] if is_inner_value else output
						else:
										return [] if is_inner_value else {"text_sequence": tokens}
		@property
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						warnings.warn(
						    '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'    ,a_    ,)
						return self.image_processor_class



		@property
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						warnings.warn(
						    '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'    ,a_    ,)
						return self.image_processor





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 	__snake_case   :
		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    ,):







						"""simple docstring"""
						lowerCAmelCase__		  =  parent
						lowerCAmelCase__		  =  batch_size
						lowerCAmelCase__		  =  seq_length
						lowerCAmelCase__		  =  is_training
						lowerCAmelCase__		  =  use_input_mask
						lowerCAmelCase__		  =  use_token_type_ids
						lowerCAmelCase__		  =  use_labels
						lowerCAmelCase__		  =  vocab_size
						lowerCAmelCase__		  =  hidden_size
						lowerCAmelCase__		  =  num_hidden_layers
						lowerCAmelCase__		  =  num_attention_heads
						lowerCAmelCase__		  =  intermediate_size
						lowerCAmelCase__		  =  hidden_act
						lowerCAmelCase__		  =  hidden_dropout_prob
						lowerCAmelCase__		  =  attention_probs_dropout_prob
						lowerCAmelCase__		  =  max_position_embeddings
						lowerCAmelCase__		  =  type_vocab_size
						lowerCAmelCase__		  =  type_sequence_label_size
						lowerCAmelCase__		  =  initializer_range
						lowerCAmelCase__		  =  num_labels
						lowerCAmelCase__		  =  num_choices
						lowerCAmelCase__		  =  scope
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size						)

						lowerCAmelCase__		  =  None
						if self.use_input_mask:
										lowerCAmelCase__		  =  random_attention_mask([self.batch_size, self.seq_length]						)

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

						lowerCAmelCase__		  =  self.get_config()

						return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""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 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  EsmModel(config=a_						)
						model.to(a_						)
						model.eval()
						lowerCAmelCase__		  =  model(a_    ,attention_mask=a_						)
						lowerCAmelCase__		  =  model(a_						)
						lowerCAmelCase__		  =  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 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  EsmForMaskedLM(config=a_						)
						model.to(a_						)
						model.eval()
						lowerCAmelCase__		  =  model(a_    ,attention_mask=a_    ,labels=a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.num_labels
						lowerCAmelCase__		  =  EsmForTokenClassification(config=a_						)
						model.to(a_						)
						model.eval()
						lowerCAmelCase__		  =  model(a_    ,attention_mask=a_    ,labels=a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.num_labels)						)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.prepare_config_and_inputs()
						(
						    (
						    lowerCAmelCase__
						)  ,   (
						    lowerCAmelCase__
						)  ,   (
						    lowerCAmelCase__
						)  ,   (
						    lowerCAmelCase__
						)  ,   (
						    lowerCAmelCase__
						)  ,   (
						    lowerCAmelCase__
						)  ,   
						)		  =  config_and_inputs
						lowerCAmelCase__		  =  {'input_ids': input_ids, 'attention_mask': input_mask}
						return config, inputs_dict



@require_torch
class 	__snake_case   ( SCREAMING_SNAKE_CASE							, SCREAMING_SNAKE_CASE							, unittest.TestCase ):
		SCREAMING_SNAKE_CASE__								=	False

		SCREAMING_SNAKE_CASE__								=	(
		    (
		        EsmForMaskedLM,
		        EsmModel,
		        EsmForSequenceClassification,
		        EsmForTokenClassification,
		    )
		    if is_torch_available()
		    else ()
		)
		SCREAMING_SNAKE_CASE__								=	()
		SCREAMING_SNAKE_CASE__								=	(
		    {
		        'feature-extraction': EsmModel,
		        'fill-mask': EsmForMaskedLM,
		        'text-classification': EsmForSequenceClassification,
		        'token-classification': EsmForTokenClassification,
		        'zero-shot': EsmForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		SCREAMING_SNAKE_CASE__								=	True
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  EsmModelTester(self						)
						lowerCAmelCase__		  =  ConfigTester(self    ,config_class=a_    ,hidden_size=37						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						self.config_tester.run_common_tests()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
										lowerCAmelCase__		  =  type
										self.model_tester.create_and_check_model(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*a_						)
		@slow
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowerCAmelCase__		  =  EsmModel.from_pretrained(a_						)
										self.assertIsNotNone(a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()[0]
						lowerCAmelCase__		  =  EsmEmbeddings(config=a_						)

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







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()[0]
						lowerCAmelCase__		  =  EsmEmbeddings(config=a_						)

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







						"""simple docstring"""
						pass
		@unittest.skip('Esm does not support embedding resizing'						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						pass



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







						"""simple docstring"""
						pass



@require_torch
class 	__snake_case   ( SCREAMING_SNAKE_CASE ):
		@slow
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						with torch.no_grad():
										lowerCAmelCase__		  =  EsmForMaskedLM.from_pretrained('facebook/esm2_t6_8M_UR50D'						)
										model.eval()
										lowerCAmelCase__		  =  torch.tensor([[0, 1, 2, 3, 4, 5]]						)
										lowerCAmelCase__		  =  model(a_						)[0]

										lowerCAmelCase__		  =  33

										lowerCAmelCase__		  =  torch.Size((1, 6, vocab_size)						)
										self.assertEqual(output.shape    ,a_						)

										lowerCAmelCase__		  =  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 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						with torch.no_grad():
										lowerCAmelCase__		  =  EsmModel.from_pretrained('facebook/esm2_t6_8M_UR50D'						)
										model.eval()

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