

'''simple docstring'''







from dataclasses import asdict, dataclass
from typing import Optional

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


__lowerCamelCase						    =      logging.get_logger(__name__)

# TODO Update this
__lowerCamelCase						    =      {
    '''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     ):
    lowercase		   =						"esm"



    def __init__(   self						,			UpperCamelCase__=None						,			UpperCamelCase__=None						,			UpperCamelCase__=None						,			UpperCamelCase__=768						,			UpperCamelCase__=12						,			UpperCamelCase__=12						,			UpperCamelCase__=3072						,			UpperCamelCase__=0.1						,			UpperCamelCase__=0.1						,			UpperCamelCase__=1026						,			UpperCamelCase__=0.02						,			UpperCamelCase__=1e-1_2						,			UpperCamelCase__="absolute"						,			UpperCamelCase__=True						,			UpperCamelCase__=None						,			UpperCamelCase__=False						,			UpperCamelCase__=False						,			UpperCamelCase__=None						,			UpperCamelCase__=None						,			**UpperCamelCase__						,			)  -> Any:





           '''simple docstring'''
           super().__init__(pad_token_id=UpperCamelCase__						,			mask_token_id=UpperCamelCase__						,			**UpperCamelCase__    )

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







    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = super().to_dict()
           if isinstance(self.esmfold_config						,			UpperCamelCase__    ):
                  A_		   = self.esmfold_config.to_dict()
           return output


@dataclass
class    A__ :
    lowercase		   =						None
    lowercase		   =						True
    lowercase		   =						False
    lowercase		   =						False
    lowercase		   =						False
    lowercase		   =						0

    lowercase		   =						True
    lowercase		   =						False

    lowercase		   =						128
    lowercase		   =						None



    def    snake_case_     (   self    )  -> Union[str, Any]:





           '''simple docstring'''
           if self.trunk is None:
                  A_		   = TrunkConfig()
           elif isinstance(self.trunk						,			UpperCamelCase__    ):
                  A_		   = TrunkConfig(**self.trunk    )







    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           A_		   = asdict(self    )
           A_		   = self.trunk.to_dict()
           return output


@dataclass
class    A__ :
    lowercase		   =						48
    lowercase		   =						1_024
    lowercase		   =						128
    lowercase		   =						32
    lowercase		   =						32
    lowercase		   =						32
    lowercase		   =						0
    lowercase		   =						0
    lowercase		   =						False
    lowercase		   =						4
    lowercase		   =						128
    lowercase		   =						None



    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           if self.structure_module is None:
                  A_		   = StructureModuleConfig()
           elif isinstance(self.structure_module						,			UpperCamelCase__    ):
                  A_		   = 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}.'''    )

           A_		   = self.sequence_state_dim // self.sequence_head_width
           A_		   = 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    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           A_		   = asdict(self    )
           A_		   = self.structure_module.to_dict()
           return output


@dataclass
class    A__ :
    lowercase		   =						384
    lowercase		   =						128
    lowercase		   =						16
    lowercase		   =						128
    lowercase		   =						12
    lowercase		   =						4
    lowercase		   =						8
    lowercase		   =						0.1
    lowercase		   =						8
    lowercase		   =						1
    lowercase		   =						2
    lowercase		   =						7
    lowercase		   =						10
    lowercase		   =						1e-8
    lowercase		   =						1e5



    def    snake_case_     (   self    )  -> List[str]:





           '''simple docstring'''
           return asdict(self    )

def  UpperCAmelCase__      ( )     ->     Dict:
       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>",
       )



'''simple docstring'''







from tempfile import TemporaryDirectory
from unittest import TestCase
from unittest.mock import MagicMock, patch

from transformers import AutoModel, TFAutoModel
from transformers.onnx import FeaturesManager
from transformers.testing_utils import SMALL_MODEL_IDENTIFIER, require_tf, require_torch


@require_torch
@require_tf
class    A__ ( _snake_case     ):



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = SMALL_MODEL_IDENTIFIER
           A_		   = """pt"""
           A_		   = """tf"""



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> Tuple:





           '''simple docstring'''
           A_		   = AutoModel.from_pretrained(self.test_model    )
           model_pt.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self						,			UpperCamelCase__    )  -> List[str]:





           '''simple docstring'''
           A_		   = TFAutoModel.from_pretrained(self.test_model						,			from_pt=UpperCamelCase__    )
           model_tf.save_pretrained(UpperCamelCase__    )



    def    snake_case_     (   self    )  -> Dict:





           '''simple docstring'''
           A_		   = """mock_framework"""

           # Framework provided - return whatever the user provides
           A_		   = FeaturesManager.determine_framework(self.test_model						,			UpperCamelCase__    )
           self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # Local checkpoint and framework provided - return provided framework
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__						,			UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			UpperCamelCase__    )



    def    snake_case_     (   self    )  -> int:





           '''simple docstring'''
           # PyTorch checkpoint
           with TemporaryDirectory() as local_pt_ckpt:
                  self._setup_pt_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # TensorFlow checkpoint
           with TemporaryDirectory() as local_tf_ckpt:
                  self._setup_tf_ckpt(UpperCamelCase__    )
                  A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Invalid local checkpoint
           with TemporaryDirectory() as local_invalid_ckpt:
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(UpperCamelCase__    )







    def    snake_case_     (   self    )  -> List[Any]:





           '''simple docstring'''
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # PyTorch not in environment -> use TensorFlow
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_tf    )

           # Both in environment -> use PyTorch
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  A_		   = FeaturesManager.determine_framework(self.test_model    )
                  self.assertEqual(UpperCamelCase__						,			self.framework_pt    )

           # Both not in environment -> raise error
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           A_		   = MagicMock(return_value=UpperCamelCase__    )
           with patch("""transformers.onnx.features.is_tf_available"""						,			UpperCamelCase__    ), patch(
               """transformers.onnx.features.is_torch_available"""						,			UpperCamelCase__    ):
                  with self.assertRaises(UpperCamelCase__    ):
                         A_		   = FeaturesManager.determine_framework(self.test_model    )
