



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





	"""simple docstring"""




	def __init__(self		,       lowerCamelCase_		,       lowerCamelCase_=13		,       lowerCamelCase_=7		,       lowerCamelCase_=False		,       lowerCamelCase_=True		,       lowerCamelCase_=False		,       lowerCamelCase_=True		,       lowerCamelCase_=33		,       lowerCamelCase_=32		,       lowerCamelCase_=5		,       lowerCamelCase_=4		,       lowerCamelCase_=37		,       lowerCamelCase_="gelu"		,       lowerCamelCase_=0.1		,       lowerCamelCase_=0.1		,       lowerCamelCase_=512		,       lowerCamelCase_=16		,       lowerCamelCase_=2		,       lowerCamelCase_=0.02		,       lowerCamelCase_=3		,       lowerCamelCase_=4		,       lowerCamelCase_=None		,       ):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   ids_tensor([self.batch_size, self.seq_length]		,       self.vocab_size		)

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

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

						a					     =   self.get_config()

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




	def      UpperCamelCase_       (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      UpperCamelCase_       (self		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		):
						"""simple docstring"""

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

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




	def      UpperCamelCase_       (self		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		,       lowerCamelCase_		):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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




@require_torch
class 	_lowercase							(   lowerCAmelCase,      lowerCAmelCase,      unittest.TestCase	):





	"""simple docstring"""
	__A							=							False

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   EsmModelTester(self		)
						a					     =   ConfigTester(self		,       config_class=lowerCamelCase_		,       hidden_size=37		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						self.config_tester.run_common_tests()




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_		)




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_		)




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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




	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						a					     =   self.model_tester.prepare_config_and_inputs()[0]
						a					     =   EsmEmbeddings(config=lowerCamelCase_		)

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

						a					     =   self.model_tester.prepare_config_and_inputs()[0]
						a					     =   EsmEmbeddings(config=lowerCamelCase_		)

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

						pass




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

						pass




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

						pass




@require_torch
class 	_lowercase							(   lowerCAmelCase	):





	"""simple docstring"""




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

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

											a					     =   33

											a					     =   torch.Size((1, 6, vocab_size)		)
											self.assertEqual(output.shape		,       lowerCamelCase_		)

											a					     =   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]		,       lowerCamelCase_		,       atol=1E-4		)		)




	@slow
	def      UpperCamelCase_       (self		):
						"""simple docstring"""

						with torch.no_grad():
											a					     =   EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"		)
											model.eval()

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





from typing import List, Union

from ..utils import (
    add_end_docstrings,
    is_tf_available,
    is_torch_available,
    is_vision_available,
    logging,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, Pipeline


if is_vision_available():
				from PIL import Image

				from ..image_utils import load_image

if is_tf_available():
				from ..models.auto.modeling_tf_auto import TF_MODEL_FOR_VISION_2_SEQ_MAPPING

if is_torch_available():
				import torch

				from ..models.auto.modeling_auto import MODEL_FOR_VISION_2_SEQ_MAPPING

_lowercase: Union[str, Any]      			=		logging.get_logger(__name__)



@add_end_docstrings(lowerCAmelCase	)
class 	_lowercase							(   lowerCAmelCase	):





	"""simple docstring"""




	def __init__(self		,       *lowerCamelCase_		,       **lowerCamelCase_		):
						"""simple docstring"""

						super().__init__(*lowerCamelCase_		,       **lowerCamelCase_		)
						requires_backends(self		,       "vision"		)
						self.check_model_type(
						    TF_MODEL_FOR_VISION_2_SEQ_MAPPING if self.framework == "tf" else MODEL_FOR_VISION_2_SEQ_MAPPING		)




	def      UpperCamelCase_       (self		,       lowerCamelCase_=None		,       lowerCamelCase_=None		,       lowerCamelCase_=None		):
						"""simple docstring"""

						a					     =   {}
						a					     =   {}

						if prompt is not None:
											a					     =   prompt

						if generate_kwargs is not None:
											a					     =   generate_kwargs
						if max_new_tokens is not None:
											if "generate_kwargs" not in forward_kwargs:
																a					     =   {}
											if "max_new_tokens" in forward_kwargs["generate_kwargs"]:
																raise ValueError(
																    "'max_new_tokens' is defined twice, once in 'generate_kwargs' and once as a direct parameter,"
																    " please use only one"		)
											a					     =   max_new_tokens
						return preprocess_params, forward_kwargs, {}




	def __call__(self		,       lowerCamelCase_		,       **lowerCamelCase_		):
						"""simple docstring"""

						return super().__call__(lowerCamelCase_		,       **lowerCamelCase_		)




	def      UpperCamelCase_       (self		,       lowerCamelCase_		,       lowerCamelCase_=None		):
						"""simple docstring"""

						a					     =   load_image(lowerCamelCase_		)

						if prompt is not None:
											if not isinstance(lowerCamelCase_		,       lowerCamelCase_		):
																raise ValueError(
																    F'''Received an invalid text input, got - {type(lowerCamelCase_		)} - but expected a single string. '''
																    "Note also that one single text can be provided for conditional image to text generation."		)

											a					     =   self.model.config.model_type

											if model_type == "git":
																a					     =   self.image_processor(images=lowerCamelCase_		,       return_tensors=self.framework		)
																a					     =   self.tokenizer(text=lowerCamelCase_		,       add_special_tokens=lowerCamelCase_		).input_ids
																a					     =   [self.tokenizer.cls_token_id] + input_ids
																a					     =   torch.tensor(lowerCamelCase_		).unsqueeze(0		)
																model_inputs.update({"input_ids": input_ids}		)

											elif model_type == "pix2struct":
																a					     =   self.image_processor(images=lowerCamelCase_		,       header_text=lowerCamelCase_		,       return_tensors=self.framework		)

											elif model_type != "vision-encoder-decoder":
																# vision-encoder-decoder does not support conditional generation
																a					     =   self.image_processor(images=lowerCamelCase_		,       return_tensors=self.framework		)
																a					     =   self.tokenizer(lowerCamelCase_		,       return_tensors=self.framework		)
																model_inputs.update(lowerCamelCase_		)

											else:
																raise ValueError(F'''Model type {model_type} does not support conditional text generation'''		)

						else:
											a					     =   self.image_processor(images=lowerCamelCase_		,       return_tensors=self.framework		)

						if self.model.config.model_type == "git" and prompt is None:
											a					     =   None

						return model_inputs




	def      UpperCamelCase_       (self		,       lowerCamelCase_		,       lowerCamelCase_=None		):
						"""simple docstring"""

						if (
						    "input_ids" in model_inputs
						    and isinstance(model_inputs["input_ids"]		,       lowerCamelCase_		)
						    and all(x is None for x in model_inputs["input_ids"]		)
						):
											a					     =   None

						if generate_kwargs is None:
											a					     =   {}
						# FIXME: We need to pop here due to a difference in how `generation.py` and `generation.tf_utils.py`
						#  parse inputs. In the Tensorflow version, `generate` raises an error if we don't use `input_ids` whereas
						#  the PyTorch version matches it with `self.model.main_input_name` or `self.model.encoder.main_input_name`
						#  in the `_prepare_model_inputs` method.
						a					     =   model_inputs.pop(self.model.main_input_name		)
						a					     =   self.model.generate(lowerCamelCase_		,       **lowerCamelCase_		,       **lowerCamelCase_		)
						return model_outputs




	def      UpperCamelCase_       (self		,       lowerCamelCase_		):
						"""simple docstring"""

						a					     =   []
						for output_ids in model_outputs:
											a					     =   {
											    "generated_text": self.tokenizer.decode(
											        lowerCamelCase_		,       skip_special_tokens=lowerCamelCase_		,       )
											}
											records.append(lowerCamelCase_		)
						return records
