



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.models.esm.modeling_esmfold import EsmForProteinFolding



class 							lowerCAmelCase_						:
       def __init__(     self,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_=13,     SCREAMING_SNAKE_CASE_=7,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=True,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=False,     SCREAMING_SNAKE_CASE_=19,     SCREAMING_SNAKE_CASE_=32,     SCREAMING_SNAKE_CASE_=5,     SCREAMING_SNAKE_CASE_=4,     SCREAMING_SNAKE_CASE_=37,     SCREAMING_SNAKE_CASE_="gelu",     SCREAMING_SNAKE_CASE_=0.1,     SCREAMING_SNAKE_CASE_=0.1,     SCREAMING_SNAKE_CASE_=512,     SCREAMING_SNAKE_CASE_=16,     SCREAMING_SNAKE_CASE_=2,     SCREAMING_SNAKE_CASE_=0.02,     SCREAMING_SNAKE_CASE_=3,     SCREAMING_SNAKE_CASE_=4,     SCREAMING_SNAKE_CASE_=None,     )	->						int:
          UpperCamelCase       :    Any		      =      parent
          UpperCamelCase       :    Tuple		      =      batch_size
          UpperCamelCase       :    List[str]		      =      seq_length
          UpperCamelCase       :    Tuple		      =      is_training
          UpperCamelCase       :    Optional[Any]		      =      use_input_mask
          UpperCamelCase       :    Tuple		      =      use_token_type_ids
          UpperCamelCase       :    Any		      =      use_labels
          UpperCamelCase       :    Any		      =      vocab_size
          UpperCamelCase       :    int		      =      hidden_size
          UpperCamelCase       :    Dict		      =      num_hidden_layers
          UpperCamelCase       :    Dict		      =      num_attention_heads
          UpperCamelCase       :    List[Any]		      =      intermediate_size
          UpperCamelCase       :    Optional[Any]		      =      hidden_act
          UpperCamelCase       :    Any		      =      hidden_dropout_prob
          UpperCamelCase       :    Union[str, Any]		      =      attention_probs_dropout_prob
          UpperCamelCase       :    List[str]		      =      max_position_embeddings
          UpperCamelCase       :    Union[str, Any]		      =      type_vocab_size
          UpperCamelCase       :    List[str]		      =      type_sequence_label_size
          UpperCamelCase       :    Dict		      =      initializer_range
          UpperCamelCase       :    Any		      =      num_labels
          UpperCamelCase       :    int		      =      num_choices
          UpperCamelCase       :    List[str]		      =      scope
       def    snake_case_			(     self			)	->						Union[str, Any]:
          UpperCamelCase       :    List[str]		      =      ids_tensor([self.batch_size, self.seq_length],     self.vocab_size			)

          UpperCamelCase       :    Union[str, Any]		      =      None
          if self.use_input_mask:
             UpperCamelCase       :    Optional[int]		      =      random_attention_mask([self.batch_size, self.seq_length]			)

          UpperCamelCase       :    Union[str, Any]		      =      None
          UpperCamelCase       :    str		      =      None
          UpperCamelCase       :    Union[str, Any]		      =      None
          if self.use_labels:
             UpperCamelCase       :    Any		      =      ids_tensor([self.batch_size],     self.type_sequence_label_size			)
             UpperCamelCase       :    Any		      =      ids_tensor([self.batch_size, self.seq_length],     self.num_labels			)
             UpperCamelCase       :    int		      =      ids_tensor([self.batch_size],     self.num_choices			)

          UpperCamelCase       :    Dict		      =      self.get_config()

          return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
       def    snake_case_			(     self			)	->						Optional[Any]:
          UpperCamelCase       :    Any		      =      EsmConfig(
              vocab_size=33,     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,     is_folding_model=_lowerCamelCase,     esmfold_config={'trunk': {'num_blocks': 2}, 'fp16_esm': False},     )
          return config
       def    snake_case_			(     self,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_,     SCREAMING_SNAKE_CASE_			)	->						Any:
          UpperCamelCase       :    Union[str, Any]		      =      EsmForProteinFolding(config=_lowerCamelCase			).float()
          model.to(_lowerCamelCase			)
          model.eval()
          UpperCamelCase       :    Tuple		      =      model(_lowerCamelCase,     attention_mask=_lowerCamelCase			)
          UpperCamelCase       :    str		      =      model(_lowerCamelCase			)
          UpperCamelCase       :    Union[str, Any]		      =      model(_lowerCamelCase			)

          self.parent.assertEqual(result.positions.shape,     (8, self.batch_size, self.seq_length, 14, 3)			)
          self.parent.assertEqual(result.angles.shape,     (8, self.batch_size, self.seq_length, 7, 2)			)






       def    snake_case_			(     self			)	->						Tuple:
          UpperCamelCase       :    Any		      =      self.prepare_config_and_inputs()
          (
              (
              UpperCamelCase					
          )      ,	(
              UpperCamelCase					
          )      ,	(
              UpperCamelCase					
          )      ,	(
              UpperCamelCase					
          )      ,	(
              UpperCamelCase					
          )      ,	(
              UpperCamelCase					
          )      ,						
          )       :    Optional[int]		      =      config_and_inputs
          UpperCamelCase       :    Any		      =      {'input_ids': input_ids, 'attention_mask': input_mask}
          return config, inputs_dict






@require_torch
class 							lowerCAmelCase_						(    __a			,       __a			,       unittest.TestCase				):
       UpperCAmelCase__							:   Tuple  	=    False

       UpperCAmelCase__							:   str  	=    (EsmForProteinFolding,) if is_torch_available() else ()
       UpperCAmelCase__							:   Optional[int]  	=    ()
       UpperCAmelCase__							:   Union[str, Any]  	=    {} if is_torch_available() else {}
       UpperCAmelCase__							:   Any  	=    False
       def    snake_case_			(     self			)	->						Tuple:
          UpperCamelCase       :    Optional[int]		      =      EsmFoldModelTester(self			)
          UpperCamelCase       :    Any		      =      ConfigTester(self,     config_class=_lowerCamelCase,     hidden_size=37			)
       def    snake_case_			(     self			)	->						Dict:
          self.config_tester.run_common_tests()
       def    snake_case_			(     self			)	->						List[str]:
          UpperCamelCase       :    Optional[Any]		      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_lowerCamelCase			)
       @unittest.skip('Does not support attention outputs'			)
       def    snake_case_			(     self			)	->						int:
          pass
       @unittest.skip
       def    snake_case_			(     self			)	->						Dict:
          pass
       @unittest.skip('Esm does not support embedding resizing'			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('Esm does not support embedding resizing'			)
       def    snake_case_			(     self			)	->						List[Any]:
          pass
       @unittest.skip('ESMFold does not support passing input embeds!'			)
       def    snake_case_			(     self			)	->						List[Any]:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						str:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						Tuple:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						Any:
          pass
       @unittest.skip('ESMFold does not support head pruning.'			)
       def    snake_case_			(     self			)	->						List[str]:
          pass
       @unittest.skip('ESMFold does not output hidden states in the normal way.'			)
       def    snake_case_			(     self			)	->						Any:
          pass
       @unittest.skip('ESMfold does not output hidden states in the normal way.'			)
       def    snake_case_			(     self			)	->						List[str]:
          pass
       @unittest.skip('ESMFold only has one output format.'			)
       def    snake_case_			(     self			)	->						Tuple:
          pass
       @unittest.skip('This test doesn\'t work for ESMFold and doesn\'t test core functionality'			)
       def    snake_case_			(     self			)	->						Union[str, Any]:
          pass
       @unittest.skip('ESMFold does not support input chunking.'			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'			)
       def    snake_case_			(     self			)	->						int:
          pass
       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'			)
       def    snake_case_			(     self			)	->						Optional[Any]:
          pass
       @unittest.skip('ESMFold doesn\'t support torchscript compilation.'			)
       def    snake_case_			(     self			)	->						str:
          pass
       @unittest.skip('ESMFold doesn\'t support data parallel.'			)
       def    snake_case_			(     self			)	->						Optional[int]:
          pass






       @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.'			)
       def    snake_case_			(     self			)	->						Optional[int]:
          pass






@require_torch
class 							lowerCAmelCase_						(    __a				):
       @slow
       def    snake_case_			(     self			)	->						Any:
          UpperCamelCase       :    Dict		      =      EsmForProteinFolding.from_pretrained('facebook/esmfold_v1'			).float()
          model.eval()
          UpperCamelCase       :    Optional[int]		      =      torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]			)
          UpperCamelCase       :    List[str]		      =      model(_lowerCamelCase			)['positions']
          UpperCamelCase       :    int		      =      torch.tensor([2.58_28, 0.79_93, -10.93_34],     dtype=torch.floataa			)
          self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0],     _lowerCamelCase,     atol=1e-4			)			)





"""simple docstring"""






import os
import re
from shutil import copyfile
from typing import List, Optional, Tuple

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


_lowercase							=				logging.get_logger(__name__)

_lowercase							=				{
    '''vocab_file''': '''vocab.txt''',
    '''merges_file''': '''bpe.codes''',
}

_lowercase							=				{
    '''vocab_file''': {
        '''vinai/phobert-base''': '''https://huggingface.co/vinai/phobert-base/resolve/main/vocab.txt''',
        '''vinai/phobert-large''': '''https://huggingface.co/vinai/phobert-large/resolve/main/vocab.txt''',
    },
    '''merges_file''': {
        '''vinai/phobert-base''': '''https://huggingface.co/vinai/phobert-base/resolve/main/bpe.codes''',
        '''vinai/phobert-large''': '''https://huggingface.co/vinai/phobert-large/resolve/main/bpe.codes''',
    },
}

_lowercase							=				{
    '''vinai/phobert-base''': 256,
    '''vinai/phobert-large''': 256,
}



def      lowerCAmelCase__			(		__magic_name__      )						->Optional[int]:
			__lowercase       =       set()
			__lowercase       =       word[0]
			for char in word[1:]:
						pairs.add((prev_char, char)      )
						__lowercase       =       char

			__lowercase       =       set(__magic_name__      )
			return pairs







class 					__a     ( __a       ):



							'''simple docstring'''







							_lowerCamelCase					:			int          =   VOCAB_FILES_NAMES
							_lowerCamelCase					:			List[str]          =   PRETRAINED_VOCAB_FILES_MAP
							_lowerCamelCase					:			Optional[Any]          =   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							def __init__(     self  ,    _lowerCamelCase  ,    _lowerCamelCase  ,    _lowerCamelCase="<s>"  ,    _lowerCamelCase="</s>"  ,    _lowerCamelCase="</s>"  ,    _lowerCamelCase="<s>"  ,    _lowerCamelCase="<unk>"  ,    _lowerCamelCase="<pad>"  ,    _lowerCamelCase="<mask>"  ,    **_lowerCamelCase  ,    )			->							Tuple:




										'''simple docstring'''
										super().__init__(
										    bos_token=_lowerCamelCase  ,    eos_token=_lowerCamelCase  ,    unk_token=_lowerCamelCase  ,    sep_token=_lowerCamelCase  ,    cls_token=_lowerCamelCase  ,    pad_token=_lowerCamelCase  ,    mask_token=_lowerCamelCase  ,    **_lowerCamelCase  ,    )

										__lowercase       =       vocab_file
										__lowercase       =       merges_file

										__lowercase       =       {}
										__lowercase       =       0
										__lowercase       =       1
										__lowercase       =       2
										__lowercase       =       3

										self.add_from_file(_lowerCamelCase       )

										__lowercase       =       {v: k for k, v in self.encoder.items()}

										with open(_lowerCamelCase  ,    encoding="utf-8"       ) as merges_handle:
													__lowercase       =       merges_handle.read().split("\n"       )[:-1]
										__lowercase       =       [tuple(merge.split()[:-1]       ) for merge in merges]
										__lowercase       =       dict(zip(_lowerCamelCase  ,    range(len(_lowerCamelCase       )       )       )       )
										__lowercase       =       {}
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase = None       )			->							List[int]:




										'''simple docstring'''

										if token_ids_a is None:
													return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
										__lowercase       =       [self.cls_token_id]
										__lowercase       =       [self.sep_token_id]
										return cls + token_ids_a + sep + sep + token_ids_a + sep
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase = None  ,    _lowerCamelCase = False       )			->							List[int]:




										'''simple docstring'''

										if already_has_special_tokens:
													return super().get_special_tokens_mask(
													    token_ids_a=_lowerCamelCase  ,    token_ids_a=_lowerCamelCase  ,    already_has_special_tokens=_lowerCamelCase       )

										if token_ids_a is None:
													return [1] + ([0] * len(_lowerCamelCase       )) + [1]
										return [1] + ([0] * len(_lowerCamelCase       )) + [1, 1] + ([0] * len(_lowerCamelCase       )) + [1]
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase = None       )			->							List[int]:




										'''simple docstring'''
										__lowercase       =       [self.sep_token_id]
										__lowercase       =       [self.cls_token_id]

										if token_ids_a is None:
													return len(cls + token_ids_a + sep       ) * [0]
										return len(cls + token_ids_a + sep + sep + token_ids_a + sep       ) * [0]
							@property
							def   SCREAMING_SNAKE_CASE						(     self       )			->							Optional[Any]:




										'''simple docstring'''
										return len(self.encoder       )
							def   SCREAMING_SNAKE_CASE						(     self       )			->							str:




										'''simple docstring'''
										return dict(self.encoder  ,    **self.added_tokens_encoder       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							List[Any]:




										'''simple docstring'''
										if token in self.cache:
													return self.cache[token]
										__lowercase       =       tuple(_lowerCamelCase       )
										__lowercase       =       tuple(list(word[:-1]       ) + [word[-1] + "</w>"]       )
										__lowercase       =       get_pairs(_lowerCamelCase       )

										if not pairs:
													return token

										while True:
													__lowercase       =       min(_lowerCamelCase  ,    key=lambda _lowerCamelCase       : self.bpe_ranks.get(_lowerCamelCase  ,    float("inf"       )       )       )
													if bigram not in self.bpe_ranks:
																break
													__lowercase   ,					__lowercase       =       bigram
													__lowercase       =       []
													__lowercase       =       0
													while i < len(_lowerCamelCase       ):
																try:
																			__lowercase       =       word.index(_lowerCamelCase  ,    _lowerCamelCase       )
																except ValueError:
																			new_word.extend(word[i:]       )
																			break
																else:
																			new_word.extend(word[i:j]       )
																			__lowercase       =       j

																if word[i] == first and i < len(_lowerCamelCase       ) - 1 and word[i + 1] == second:
																			new_word.append(first + second       )
																			i += 2
																else:
																			new_word.append(word[i]       )
																			i += 1
													__lowercase       =       tuple(_lowerCamelCase       )
													__lowercase       =       new_word
													if len(_lowerCamelCase       ) == 1:
																break
													else:
																__lowercase       =       get_pairs(_lowerCamelCase       )
										__lowercase       =       "@@ ".join(_lowerCamelCase       )
										__lowercase       =       word[:-4]
										__lowercase       =       word
										return word
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							str:




										'''simple docstring'''
										__lowercase       =       []

										__lowercase       =       re.findall(R"\S+\n?"  ,    _lowerCamelCase       )

										for token in words:
													split_tokens.extend(list(self.bpe(_lowerCamelCase       ).split(" "       )       )       )
										return split_tokens
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							Optional[Any]:




										'''simple docstring'''
										return self.encoder.get(_lowerCamelCase  ,    self.encoder.get(self.unk_token       )       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							Optional[int]:




										'''simple docstring'''
										return self.decoder.get(_lowerCamelCase  ,    self.unk_token       )
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							Tuple:




										'''simple docstring'''
										__lowercase       =       " ".join(_lowerCamelCase       ).replace("@@ "  ,    ""       ).strip()
										return out_string
							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase  ,    _lowerCamelCase = None       )			->							Tuple[str]:




										'''simple docstring'''
										if not os.path.isdir(_lowerCamelCase       ):
													logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''       )
													return
										__lowercase       =       os.path.join(
										    _lowerCamelCase  ,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]       )
										__lowercase       =       os.path.join(
										    _lowerCamelCase  ,    (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["merges_file"]       )

										if os.path.abspath(self.vocab_file       ) != os.path.abspath(_lowerCamelCase       ):
													copyfile(self.vocab_file  ,    _lowerCamelCase       )

										if os.path.abspath(self.merges_file       ) != os.path.abspath(_lowerCamelCase       ):
													copyfile(self.merges_file  ,    _lowerCamelCase       )

										return out_vocab_file, out_merge_file





							def   SCREAMING_SNAKE_CASE						(     self  ,    _lowerCamelCase       )			->							Dict:




										'''simple docstring'''
										if isinstance(_lowerCamelCase  ,    _lowerCamelCase       ):
													try:
																with open(_lowerCamelCase  ,    "r"  ,    encoding="utf-8"       ) as fd:
																			self.add_from_file(_lowerCamelCase       )
													except FileNotFoundError as fnfe:
																raise fnfe
													except UnicodeError:
																raise Exception(f'''Incorrect encoding detected in {f}, please rebuild the dataset'''       )
													return

										__lowercase       =       f.readlines()
										for lineTmp in lines:
													__lowercase       =       lineTmp.strip()
													__lowercase       =       line.rfind(" "       )
													if idx == -1:
																raise ValueError("Incorrect dictionary format, expected '<token> <cnt>'"       )
													__lowercase       =       line[:idx]
													__lowercase       =       len(self.encoder       )





