




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







 '''simple docstring'''





 def __init__(				self:		List[Any],      a_:		Optional[Any],      a_:		List[Any]=13,      a_:		List[Any]=7,      a_:		Tuple=False,      a_:		str=True,      a_:		str=False,      a_:		List[str]=True,      a_:		Dict=33,      a_:		Any=32,      a_:		Tuple=5,      a_:		List[Any]=4,      a_:		Any=37,      a_:		str="gelu",      a_:		Tuple=0.1,      a_:		Union[str, Any]=0.1,      a_:		Dict=512,      a_:		str=16,      a_:		str=2,      a_:		Tuple=0.02,      a_:		Optional[int]=3,      a_:		str=4,      a_:		Any=None,      ):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  parent
   _snake_case							:		Optional[Any]								=  batch_size
   _snake_case							:		int								=  seq_length
   _snake_case							:		Optional[int]								=  is_training
   _snake_case							:		List[str]								=  use_input_mask
   _snake_case							:		List[Any]								=  use_token_type_ids
   _snake_case							:		Any								=  use_labels
   _snake_case							:		List[Any]								=  vocab_size
   _snake_case							:		Any								=  hidden_size
   _snake_case							:		int								=  num_hidden_layers
   _snake_case							:		str								=  num_attention_heads
   _snake_case							:		Optional[int]								=  intermediate_size
   _snake_case							:		Union[str, Any]								=  hidden_act
   _snake_case							:		Tuple								=  hidden_dropout_prob
   _snake_case							:		Union[str, Any]								=  attention_probs_dropout_prob
   _snake_case							:		Optional[Any]								=  max_position_embeddings
   _snake_case							:		Union[str, Any]								=  type_vocab_size
   _snake_case							:		Any								=  type_sequence_label_size
   _snake_case							:		str								=  initializer_range
   _snake_case							:		Tuple								=  num_labels
   _snake_case							:		Any								=  num_choices
   _snake_case							:		Optional[int]								=  scope


 def  UpperCamelCase_   (				self:		Any		):




   '''simple docstring'''



   _snake_case							:		Dict								=  ids_tensor([self.batch_size, self.seq_length],      self.vocab_size		)

   _snake_case							:		Any								=  None
   if self.use_input_mask:
     _snake_case							:		Tuple								=  random_attention_mask([self.batch_size, self.seq_length]		)

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

   _snake_case							:		Any								=  self.get_config()

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


 def  UpperCamelCase_   (				self:		Union[str, Any]		):




   '''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:		int,      a_:		Union[str, Any],      a_:		Dict,      a_:		List[str],      a_:		Any,      a_:		Any,      a_:		Any		):




   '''simple docstring'''



   _snake_case							:		Dict								=  EsmModel(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		List[str]								=  model(a_,      attention_mask=a_		)
   _snake_case							:		Any								=  model(a_		)
   _snake_case							:		Optional[Any]								=  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  UpperCamelCase_   (				self:		Tuple,      a_:		int,      a_:		Dict,      a_:		Optional[Any],      a_:		str,      a_:		Union[str, Any],      a_:		List[str]		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  EsmForMaskedLM(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		Optional[Any]								=  model(a_,      attention_mask=a_,      labels=a_		)
   self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.vocab_size)		)


 def  UpperCamelCase_   (				self:		List[Any],      a_:		Optional[Any],      a_:		Any,      a_:		Any,      a_:		str,      a_:		Union[str, Any],      a_:		Tuple		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.num_labels
   _snake_case							:		Tuple								=  EsmForTokenClassification(config=a_		)
   model.to(a_		)
   model.eval()
   _snake_case							:		List[Any]								=  model(a_,      attention_mask=a_,      labels=a_		)
   self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.num_labels)		)


 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  self.prepare_config_and_inputs()
   (
       (
       _snake_case
   )		,					(
       _snake_case
   )		,					(
       _snake_case
   )		,					(
       _snake_case
   )		,					(
       _snake_case
   )		,					(
       _snake_case
   )		,					
   )							:		str								=  config_and_inputs
   _snake_case							:		Optional[Any]								=  {"""input_ids""": input_ids, """attention_mask""": input_mask}
   return config, inputs_dict







@require_torch
class 					lowercase(     __a       ,			__a       ,			unittest.TestCase ):







 '''simple docstring'''





 lowercase__ 					=					False

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


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		str								=  EsmModelTester(self		)
   _snake_case							:		Union[str, Any]								=  ConfigTester(self,      config_class=a_,      hidden_size=37		)


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   self.config_tester.run_common_tests()


 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*a_		)


 def  UpperCamelCase_   (				self:		Tuple		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  self.model_tester.prepare_config_and_inputs()
   for type in ["absolute", "relative_key", "relative_key_query"]:
     _snake_case							:		str								=  type
     self.model_tester.create_and_check_model(*a_		)


 def  UpperCamelCase_   (				self:		str		):




   '''simple docstring'''



   _snake_case							:		List[str]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*a_		)


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Optional[int]								=  self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_token_classification(*a_		)


 @slow
 def  UpperCamelCase_   (				self:		Optional[Any]		):




   '''simple docstring'''



   for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:		Tuple								=  EsmModel.from_pretrained(a_		)
     self.assertIsNotNone(a_		)


 def  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Any								=  self.model_tester.prepare_config_and_inputs()[0]
   _snake_case							:		Union[str, Any]								=  EsmEmbeddings(config=a_		)

   _snake_case							:		Union[str, Any]								=  torch.as_tensor([[12, 31, 13, model.padding_idx]]		)
   _snake_case							:		int								=  torch.as_tensor(
       [
           [
               0 + model.padding_idx + 1,
               1 + model.padding_idx + 1,
               2 + model.padding_idx + 1,
               model.padding_idx,
           ]
       ]		)
   _snake_case							:		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  UpperCamelCase_   (				self:		Dict		):




   '''simple docstring'''



   _snake_case							:		Any								=  self.model_tester.prepare_config_and_inputs()[0]
   _snake_case							:		Optional[int]								=  EsmEmbeddings(config=a_		)

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




   '''simple docstring'''



   pass


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




   '''simple docstring'''



   pass


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




   '''simple docstring'''



   pass







@require_torch
class 					lowercase(     __a ):







 '''simple docstring'''





 @slow
 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



   with torch.no_grad():
     _snake_case							:		Optional[Any]								=  EsmForMaskedLM.from_pretrained("""facebook/esm2_t6_8M_UR50D"""		)
     model.eval()
     _snake_case							:		Union[str, Any]								=  torch.tensor([[0, 1, 2, 3, 4, 5]]		)
     _snake_case							:		Optional[int]								=  model(a_		)[0]

     _snake_case							:		Tuple								=  33

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

     _snake_case							:		str								=  torch.tensor(
         [[[8.9_215, -10.5_898, -6.4_671], [-6.3_967, -13.9_114, -1.1_212], [-7.7_812, -13.9_516, -3.7_406]]]		)
     self.assertTrue(torch.allclose(output[:, :3, :3],      a_,      atol=1E-4		)		)


 @slow
 def  UpperCamelCase_   (				self:		Optional[int]		):




   '''simple docstring'''



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

     _snake_case							:		Any								=  torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]		)
     _snake_case							:		Optional[int]								=  model(a_		)[0]
     # compare the actual values for a slice.
     _snake_case							:		Union[str, Any]								=  torch.tensor(
         [[[0.1_444, 0.5_413, 0.3_248], [0.3_034, 0.0_053, 0.3_108], [0.3_228, -0.2_499, 0.3_415]]]		)
     self.assertTrue(torch.allclose(output[:, :3, :3],      a_,      atol=1E-4		)		)










"""simple docstring"""
import copy
from collections import OrderedDict
from typing import Dict, Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


lowerCamelCase_					=      logging.get_logger(__name__)

lowerCamelCase_					=      {
    '''facebook/detr-resnet-50''': '''https://huggingface.co/facebook/detr-resnet-50/resolve/main/config.json''',
    # See all DETR models at https://huggingface.co/models?filter=detr
}







class 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				'''detr'''
	__magic_name__	      =				['''past_key_values''']
	__magic_name__	      =				{
	    '''hidden_size''': '''d_model''',
	    '''num_attention_heads''': '''encoder_attention_heads''',
	}





	def __init__(      self					: Dict	,	lowerCAmelCase_					: str=True	,	lowerCAmelCase_					: Union[str, Any]=None	,	lowerCAmelCase_					: Dict=3	,	lowerCAmelCase_					: List[Any]=100	,	lowerCAmelCase_					: int=6	,	lowerCAmelCase_					: Union[str, Any]=2_048	,	lowerCAmelCase_					: int=8	,	lowerCAmelCase_					: int=6	,	lowerCAmelCase_					: Union[str, Any]=2_048	,	lowerCAmelCase_					: Any=8	,	lowerCAmelCase_					: Dict=0.0	,	lowerCAmelCase_					: Union[str, Any]=0.0	,	lowerCAmelCase_					: Dict=True	,	lowerCAmelCase_					: List[Any]="relu"	,	lowerCAmelCase_					: List[Any]=256	,	lowerCAmelCase_					: Tuple=0.1	,	lowerCAmelCase_					: Optional[Any]=0.0	,	lowerCAmelCase_					: List[Any]=0.0	,	lowerCAmelCase_					: List[Any]=0.0_2	,	lowerCAmelCase_					: Optional[int]=1.0	,	lowerCAmelCase_					: str=False	,	lowerCAmelCase_					: Tuple="sine"	,	lowerCAmelCase_					: str="resnet50"	,	lowerCAmelCase_					: List[Any]=True	,	lowerCAmelCase_					: int=False	,	lowerCAmelCase_					: Optional[Any]=1	,	lowerCAmelCase_					: int=5	,	lowerCAmelCase_					: Optional[Any]=2	,	lowerCAmelCase_					: List[Any]=1	,	lowerCAmelCase_					: List[Any]=1	,	lowerCAmelCase_					: Dict=5	,	lowerCAmelCase_					: Optional[Any]=2	,	lowerCAmelCase_					: Union[str, Any]=0.1	,	**lowerCAmelCase_					: Dict	,	)						->			int:
				if backbone_config is not None and use_timm_backbone:
							raise ValueError("You can't specify both `backbone_config` and `use_timm_backbone`."						)

				if not use_timm_backbone:
							if backbone_config is None:
										logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."						)
										UpperCAmelCase_     : Tuple			     =					CONFIG_MAPPING["resnet"](out_features=["stage4"]						)
							elif isinstance(lowerCAmelCase_	,	lowerCAmelCase_						):
										UpperCAmelCase_     : Dict			     =					backbone_config.get("model_type"						)
										UpperCAmelCase_     : Dict			     =					CONFIG_MAPPING[backbone_model_type]
										UpperCAmelCase_     : Tuple			     =					config_class.from_dict(lowerCAmelCase_						)
							# set timm attributes to None
							UpperCAmelCase_     ,	UpperCAmelCase_     ,	UpperCAmelCase_     : Optional[Any]			     =					None, None, None

				UpperCAmelCase_     : str			     =					use_timm_backbone
				UpperCAmelCase_     : Optional[Any]			     =					backbone_config
				UpperCAmelCase_     : Tuple			     =					num_channels
				UpperCAmelCase_     : Dict			     =					num_queries
				UpperCAmelCase_     : str			     =					d_model
				UpperCAmelCase_     : Any			     =					encoder_ffn_dim
				UpperCAmelCase_     : Union[str, Any]			     =					encoder_layers
				UpperCAmelCase_     : Optional[int]			     =					encoder_attention_heads
				UpperCAmelCase_     : List[str]			     =					decoder_ffn_dim
				UpperCAmelCase_     : Tuple			     =					decoder_layers
				UpperCAmelCase_     : Optional[int]			     =					decoder_attention_heads
				UpperCAmelCase_     : List[Any]			     =					dropout
				UpperCAmelCase_     : Union[str, Any]			     =					attention_dropout
				UpperCAmelCase_     : int			     =					activation_dropout
				UpperCAmelCase_     : List[str]			     =					activation_function
				UpperCAmelCase_     : Optional[int]			     =					init_std
				UpperCAmelCase_     : Union[str, Any]			     =					init_xavier_std
				UpperCAmelCase_     : List[str]			     =					encoder_layerdrop
				UpperCAmelCase_     : Tuple			     =					decoder_layerdrop
				UpperCAmelCase_     : str			     =					encoder_layers
				UpperCAmelCase_     : Any			     =					auxiliary_loss
				UpperCAmelCase_     : Optional[int]			     =					position_embedding_type
				UpperCAmelCase_     : List[str]			     =					backbone
				UpperCAmelCase_     : int			     =					use_pretrained_backbone
				UpperCAmelCase_     : Any			     =					dilation
				# Hungarian matcher
				UpperCAmelCase_     : str			     =					class_cost
				UpperCAmelCase_     : Any			     =					bbox_cost
				UpperCAmelCase_     : int			     =					giou_cost
				# Loss coefficients
				UpperCAmelCase_     : List[str]			     =					mask_loss_coefficient
				UpperCAmelCase_     : Dict			     =					dice_loss_coefficient
				UpperCAmelCase_     : Any			     =					bbox_loss_coefficient
				UpperCAmelCase_     : Union[str, Any]			     =					giou_loss_coefficient
				UpperCAmelCase_     : int			     =					eos_coefficient
				super().__init__(is_encoder_decoder=lowerCAmelCase_	,	**lowerCAmelCase_						)





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: List[Any]						)						->			int:
				return self.encoder_attention_heads





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: int						)						->			int:
				return self.d_model





	@classmethod
	def      _SCREAMING_SNAKE_CASE      (      cls					: Union[str, Any]	,	lowerCAmelCase_					: PretrainedConfig	,	**lowerCAmelCase_					: Tuple						)						->			List[Any]:
				return cls(backbone_config=lowerCAmelCase_	,	**lowerCAmelCase_						)





	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Dict[str, any]:
				UpperCAmelCase_     : Tuple			     =					copy.deepcopy(self.__dict__						)
				if output["backbone_config"] is not None:
							UpperCAmelCase_     : Union[str, Any]			     =					self.backbone_config.to_dict()
				UpperCAmelCase_     : Any			     =					self.__class__.model_type
				return output







class 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				version.parse('''1.11'''     )





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Mapping[str, Mapping[int, str]]:
				return OrderedDict(
				    [
				        ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
				        ("pixel_mask", {0: "batch"}),
				    ]						)





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			float:
				return 1e-5





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Union[str, Any]						)						->			int:
				return 12



