
from dataclasses import asdict, dataclass
from typing import Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase        =  logging.get_logger(__name__)

# TODO Update this
lowercase        =  {
    '''facebook/esm-1b''': '''https://huggingface.co/facebook/esm-1b/resolve/main/config.json''',
    # See all ESM models at https://huggingface.co/models?filter=esm
}





class       __A(				snake_case__       ):
					SCREAMING_SNAKE_CASE			    =    '''esm'''

					def __init__(	self					:     Dict   ,					__UpperCamelCase					:     Any=None   ,					__UpperCamelCase					:     Union[str, Any]=None   ,					__UpperCamelCase					:     Optional[Any]=None   ,					__UpperCamelCase					:     Optional[int]=7_6_8   ,					__UpperCamelCase					:     Optional[int]=1_2   ,					__UpperCamelCase					:     Dict=1_2   ,					__UpperCamelCase					:     Optional[int]=3_0_7_2   ,					__UpperCamelCase					:     Union[str, Any]=0.1   ,					__UpperCamelCase					:     str=0.1   ,					__UpperCamelCase					:     int=1_0_2_6   ,					__UpperCamelCase					:     Dict=0.02   ,					__UpperCamelCase					:     Tuple=1E-12   ,					__UpperCamelCase					:     Any="absolute"   ,					__UpperCamelCase					:     Tuple=True   ,					__UpperCamelCase					:     Dict=None   ,					__UpperCamelCase					:     int=False   ,					__UpperCamelCase					:     Optional[Any]=False   ,					__UpperCamelCase					:     Dict=None   ,					__UpperCamelCase					:     List[Any]=None   ,					**__UpperCamelCase					:     List[str]   ,					):
												super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE   ,					mask_token_id=_SCREAMING_SNAKE_CASE   ,					**_SCREAMING_SNAKE_CASE   )

												lowerCamelCase_							  =					vocab_size
												lowerCamelCase_							  =					hidden_size
												lowerCamelCase_							  =					num_hidden_layers
												lowerCamelCase_							  =					num_attention_heads
												lowerCamelCase_							  =					intermediate_size
												lowerCamelCase_							  =					hidden_dropout_prob
												lowerCamelCase_							  =					attention_probs_dropout_prob
												lowerCamelCase_							  =					max_position_embeddings
												lowerCamelCase_							  =					initializer_range
												lowerCamelCase_							  =					layer_norm_eps
												lowerCamelCase_							  =					position_embedding_type
												lowerCamelCase_							  =					use_cache
												lowerCamelCase_							  =					emb_layer_norm_before
												lowerCamelCase_							  =					token_dropout
												lowerCamelCase_							  =					is_folding_model
												if is_folding_model:
																			if esmfold_config is None:
																										logger.info("""No esmfold_config supplied for folding model, using default values."""   )
																										lowerCamelCase_							  =					EsmFoldConfig()
																			elif isinstance(_SCREAMING_SNAKE_CASE   ,					_SCREAMING_SNAKE_CASE   ):
																										lowerCamelCase_							  =					EsmFoldConfig(**_SCREAMING_SNAKE_CASE   )
																			lowerCamelCase_							  =					esmfold_config
																			if vocab_list is None:
																										logger.warning("""No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!"""   )
																										lowerCamelCase_							  =					get_default_vocab_list()
																			else:
																										lowerCamelCase_							  =					vocab_list
												else:
																			lowerCamelCase_							  =					None
																			lowerCamelCase_							  =					None
												if self.esmfold_config is not None and getattr(self.esmfold_config   ,					"""use_esm_attn_map"""   ,					_SCREAMING_SNAKE_CASE   ):
																			raise ValueError("""The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!"""   )



					def    lowercase__      (	self					:     Tuple   ):
												lowerCamelCase_							  =					super().to_dict()
												if isinstance(self.esmfold_config   ,					_SCREAMING_SNAKE_CASE   ):
																			lowerCamelCase_							  =					self.esmfold_config.to_dict()
												return output





@dataclass
class       __A:
					SCREAMING_SNAKE_CASE			    =    None
					SCREAMING_SNAKE_CASE			    =    True
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    0

					SCREAMING_SNAKE_CASE			    =    True
					SCREAMING_SNAKE_CASE			    =    False

					SCREAMING_SNAKE_CASE			    =    1_2_8
					SCREAMING_SNAKE_CASE			    =    None

					def    lowercase__      (	self					:     Tuple   ):
												if self.trunk is None:
																			lowerCamelCase_							  =					TrunkConfig()
												elif isinstance(self.trunk   ,					_SCREAMING_SNAKE_CASE   ):
																			lowerCamelCase_							  =					TrunkConfig(**self.trunk   )



					def    lowercase__      (	self					:     Optional[int]   ):
												lowerCamelCase_							  =					asdict(self   )
												lowerCamelCase_							  =					self.trunk.to_dict()
												return output





@dataclass
class       __A:
					SCREAMING_SNAKE_CASE			    =    4_8
					SCREAMING_SNAKE_CASE			    =    1_0_2_4
					SCREAMING_SNAKE_CASE			    =    1_2_8
					SCREAMING_SNAKE_CASE			    =    3_2
					SCREAMING_SNAKE_CASE			    =    3_2
					SCREAMING_SNAKE_CASE			    =    3_2
					SCREAMING_SNAKE_CASE			    =    0
					SCREAMING_SNAKE_CASE			    =    0
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    4
					SCREAMING_SNAKE_CASE			    =    1_2_8
					SCREAMING_SNAKE_CASE			    =    None

					def    lowercase__      (	self					:     str   ):
												if self.structure_module is None:
																			lowerCamelCase_							  =					StructureModuleConfig()
												elif isinstance(self.structure_module   ,					_SCREAMING_SNAKE_CASE   ):
																			lowerCamelCase_							  =					StructureModuleConfig(**self.structure_module   )

												if self.max_recycles <= 0:
																			raise ValueError(F'''`max_recycles` should be positive, got {self.max_recycles}.'''   )
												if self.sequence_state_dim % self.sequence_state_dim != 0:
																			raise ValueError(
																			    """`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got"""
																			    F''' {self.sequence_state_dim} and {self.sequence_state_dim}.'''   )
												if self.pairwise_state_dim % self.pairwise_state_dim != 0:
																			raise ValueError(
																			    """`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got"""
																			    F''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.'''   )

												lowerCamelCase_							  =					self.sequence_state_dim // self.sequence_head_width
												lowerCamelCase_							  =					self.pairwise_state_dim // self.pairwise_head_width

												if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
																			raise ValueError(
																			    """`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got"""
																			    F''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.'''   )
												if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
																			raise ValueError(
																			    """`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got"""
																			    F''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.'''   )
												if self.pairwise_state_dim % 2 != 0:
																			raise ValueError(F'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.'''   )

												if self.dropout >= 0.4:
																			raise ValueError(F'''`dropout` should not be greater than 0.4, got {self.dropout}.'''   )



					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					asdict(self   )
												lowerCamelCase_							  =					self.structure_module.to_dict()
												return output





@dataclass
class       __A:
					SCREAMING_SNAKE_CASE			    =    3_8_4
					SCREAMING_SNAKE_CASE			    =    1_2_8
					SCREAMING_SNAKE_CASE			    =    1_6
					SCREAMING_SNAKE_CASE			    =    1_2_8
					SCREAMING_SNAKE_CASE			    =    1_2
					SCREAMING_SNAKE_CASE			    =    4
					SCREAMING_SNAKE_CASE			    =    8
					SCREAMING_SNAKE_CASE			    =    0.1
					SCREAMING_SNAKE_CASE			    =    8
					SCREAMING_SNAKE_CASE			    =    1
					SCREAMING_SNAKE_CASE			    =    2
					SCREAMING_SNAKE_CASE			    =    7
					SCREAMING_SNAKE_CASE			    =    1_0
					SCREAMING_SNAKE_CASE			    =    1e-8
					SCREAMING_SNAKE_CASE			    =    1e5

					def    lowercase__      (	self					:     Tuple   ):
												return asdict(self   )




def      __lowerCAmelCase						(							)  ->    int:
							return (
							    "<cls>",
							    "<pad>",
							    "<eos>",
							    "<unk>",
							    "L",
							    "A",
							    "G",
							    "V",
							    "S",
							    "E",
							    "R",
							    "T",
							    "I",
							    "D",
							    "P",
							    "K",
							    "Q",
							    "N",
							    "F",
							    "Y",
							    "M",
							    "H",
							    "W",
							    "C",
							    "X",
							    "B",
							    "U",
							    "Z",
							    "O",
							    ".",
							    "-",
							    "<null_1>",
							    "<mask>",
							)







from __future__ import annotations

import unittest

from transformers import is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import numpy
		import tensorflow as tf

		from transformers import (
		    TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    BertConfig,
		    DPRConfig,
		    TFDPRContextEncoder,
		    TFDPRQuestionEncoder,
		    TFDPRReader,
		)





class       __A:

					def __init__(	self					:     str   ,					__UpperCamelCase					:     Dict   ,					__UpperCamelCase					:     Any=1_3   ,					__UpperCamelCase					:     Dict=7   ,					__UpperCamelCase					:     int=True   ,					__UpperCamelCase					:     Optional[Any]=True   ,					__UpperCamelCase					:     Optional[Any]=True   ,					__UpperCamelCase					:     Dict=True   ,					__UpperCamelCase					:     Dict=9_9   ,					__UpperCamelCase					:     Optional[int]=3_2   ,					__UpperCamelCase					:     List[Any]=2   ,					__UpperCamelCase					:     Optional[int]=4   ,					__UpperCamelCase					:     List[Any]=3_7   ,					__UpperCamelCase					:     Optional[Any]="gelu"   ,					__UpperCamelCase					:     List[str]=0.1   ,					__UpperCamelCase					:     Any=0.1   ,					__UpperCamelCase					:     int=5_1_2   ,					__UpperCamelCase					:     List[Any]=1_6   ,					__UpperCamelCase					:     List[Any]=2   ,					__UpperCamelCase					:     List[str]=0.02   ,					__UpperCamelCase					:     Any=3   ,					__UpperCamelCase					:     int=4   ,					__UpperCamelCase					:     Optional[Any]=None   ,					__UpperCamelCase					:     Union[str, Any]=0   ,					):
												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
												lowerCamelCase_							  =					projection_dim

					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					ids_tensor([self.batch_size, self.seq_length]   ,					self.vocab_size   )

												lowerCamelCase_							  =					None
												if self.use_input_mask:
																			# follow test_modeling_tf_ctrl.py
																			lowerCamelCase_							  =					random_attention_mask([self.batch_size, self.seq_length]   )

												lowerCamelCase_							  =					None
												if self.use_token_type_ids:
																			lowerCamelCase_							  =					ids_tensor([self.batch_size, self.seq_length]   ,					self.type_vocab_size   )

												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_							  =					BertConfig(
												    vocab_size=self.vocab_size   ,					hidden_size=self.hidden_size   ,					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   ,					is_decoder=__UpperCamelCase   ,					initializer_range=self.initializer_range   ,					)
												lowerCamelCase_							  =					DPRConfig(projection_dim=self.projection_dim   ,					**config.to_dict()   )

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

					def    lowercase__      (	self					:     Optional[Any]   ,					__UpperCamelCase					:     int   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     List[str]   ,					__UpperCamelCase					:     Union[str, Any]   ):
												lowerCamelCase_							  =					TFDPRContextEncoder(config=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					token_type_ids=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					token_type_ids=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   )
												self.parent.assertEqual(result.pooler_output.shape   ,					(self.batch_size, self.projection_dim or self.hidden_size)   )

					def    lowercase__      (	self					:     str   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     Union[str, Any]   ,					__UpperCamelCase					:     List[Any]   ,					__UpperCamelCase					:     Union[str, Any]   ):
												lowerCamelCase_							  =					TFDPRQuestionEncoder(config=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   ,					token_type_ids=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					token_type_ids=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   )
												self.parent.assertEqual(result.pooler_output.shape   ,					(self.batch_size, self.projection_dim or self.hidden_size)   )

					def    lowercase__      (	self					:     str   ,					__UpperCamelCase					:     Optional[Any]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Optional[int]   ,					__UpperCamelCase					:     Tuple   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     Any   ,					__UpperCamelCase					:     List[str]   ):
												lowerCamelCase_							  =					TFDPRReader(config=__UpperCamelCase   )
												lowerCamelCase_							  =					model(__UpperCamelCase   ,					attention_mask=__UpperCamelCase   )

												self.parent.assertEqual(result.start_logits.shape   ,					(self.batch_size, self.seq_length)   )
												self.parent.assertEqual(result.end_logits.shape   ,					(self.batch_size, self.seq_length)   )
												self.parent.assertEqual(result.relevance_logits.shape   ,					(self.batch_size,)   )



					def    lowercase__      (	self					:     Dict   ):
												lowerCamelCase_							  =					self.prepare_config_and_inputs()
												(
												    (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   (
												    lowerCamelCase_
												)  ,   
												)							  =					config_and_inputs
												lowerCamelCase_							  =					{"""input_ids""": input_ids}
												return config, inputs_dict





@require_tf
class       __A(				UpperCAmelCase     ,		UpperCAmelCase     ,		unittest.TestCase       ):
					SCREAMING_SNAKE_CASE			    =    (
					    (
					        TFDPRContextEncoder,
					        TFDPRQuestionEncoder,
					        TFDPRReader,
					    )
					    if is_tf_available()
					    else ()
					)
					SCREAMING_SNAKE_CASE			    =    {'''feature-extraction''': TFDPRQuestionEncoder} if is_tf_available() else {}

					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False
					SCREAMING_SNAKE_CASE			    =    False

					def    lowercase__      (	self					:     Dict   ):
												lowerCamelCase_							  =					TFDPRModelTester(self   )
												lowerCamelCase_							  =					ConfigTester(self   ,					config_class=__UpperCamelCase   ,					hidden_size=3_7   )

					def    lowercase__      (	self					:     Optional[Any]   ):
												self.config_tester.run_common_tests()

					def    lowercase__      (	self					:     Any   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_context_encoder(*__UpperCamelCase   )

					def    lowercase__      (	self					:     Dict   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_question_encoder(*__UpperCamelCase   )

					def    lowercase__      (	self					:     List[str]   ):
												lowerCamelCase_							  =					self.model_tester.prepare_config_and_inputs()
												self.model_tester.create_and_check_dpr_reader(*__UpperCamelCase   )



					@slow
					def    lowercase__      (	self					:     Optional[int]   ):
												for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			lowerCamelCase_							  =					TFDPRContextEncoder.from_pretrained(__UpperCamelCase   )
																			self.assertIsNotNone(__UpperCamelCase   )

												for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			lowerCamelCase_							  =					TFDPRContextEncoder.from_pretrained(__UpperCamelCase   )
																			self.assertIsNotNone(__UpperCamelCase   )

												for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			lowerCamelCase_							  =					TFDPRQuestionEncoder.from_pretrained(__UpperCamelCase   )
																			self.assertIsNotNone(__UpperCamelCase   )

												for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																			lowerCamelCase_							  =					TFDPRReader.from_pretrained(__UpperCamelCase   )
																			self.assertIsNotNone(__UpperCamelCase   )





@require_tf
class       __A(				unittest.TestCase       ):

					@slow
					def    lowercase__      (	self					:     Union[str, Any]   ):
												lowerCamelCase_							  =					TFDPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""   )

												lowerCamelCase_							  =					tf.constant(
												    [[1_0_1, 7_5_9_2, 1_0_1_0, 2_0_0_3, 2_0_2_6, 3_8_9_9, 1_0_1_4_0, 1_0_2_9, 1_0_2]]   )  # [CLS] hello, is my dog cute? [SEP]
												lowerCamelCase_							  =					model(__UpperCamelCase   )[0]  # embedding shape = (1, 768)
												# compare the actual values for a slice.
												lowerCamelCase_							  =					tf.constant(
												    [
												        [
												            0.03236253,
												            0.12753335,
												            0.16818509,
												            0.00279786,
												            0.3896933,
												            0.24264945,
												            0.2178971,
												            -0.02335227,
												            -0.08481959,
												            -0.14324117,
												        ]
												    ]   )
												self.assertTrue(numpy.allclose(output[:, :1_0].numpy()   ,					expected_slice.numpy()   ,					atol=1E-4   )   )





