

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



   def __init__(							self							:     Optional[Any],		__A							:     Dict,		__A							:     Tuple=1_3,		__A							:     int=7,		__A							:     Dict=False,		__A							:     Dict=True,		__A							:     Optional[int]=False,		__A							:     List[Any]=False,		__A							:     Union[str, Any]=1_9,		__A							:     Dict=3_2,		__A							:     Optional[Any]=5,		__A							:     Any=4,		__A							:     int=3_7,		__A							:     Union[str, Any]="gelu",		__A							:     List[Any]=0.1,		__A							:     Tuple=0.1,		__A							:     Any=5_1_2,		__A							:     Tuple=1_6,		__A							:     Optional[Any]=2,		__A							:     Optional[Any]=0.0_2,		__A							:     str=3,		__A							:     Optional[int]=4,		__A							:     Tuple=None,		):
     UpperCAmelCase       :							Tuple  = parent
     UpperCAmelCase       :							List[str]  = batch_size
     UpperCAmelCase       :							Union[str, Any]  = seq_length
     UpperCAmelCase       :							List[str]  = is_training
     UpperCAmelCase       :							Optional[int]  = use_input_mask
     UpperCAmelCase       :							int  = use_token_type_ids
     UpperCAmelCase       :							int  = use_labels
     UpperCAmelCase       :							Any  = vocab_size
     UpperCAmelCase       :							Optional[Any]  = hidden_size
     UpperCAmelCase       :							Optional[Any]  = num_hidden_layers
     UpperCAmelCase       :							Union[str, Any]  = num_attention_heads
     UpperCAmelCase       :							Optional[Any]  = intermediate_size
     UpperCAmelCase       :							int  = hidden_act
     UpperCAmelCase       :							List[str]  = hidden_dropout_prob
     UpperCAmelCase       :							List[Any]  = attention_probs_dropout_prob
     UpperCAmelCase       :							Optional[Any]  = max_position_embeddings
     UpperCAmelCase       :							str  = type_vocab_size
     UpperCAmelCase       :							List[str]  = type_sequence_label_size
     UpperCAmelCase       :							Dict  = initializer_range
     UpperCAmelCase       :							Any  = num_labels
     UpperCAmelCase       :							int  = num_choices
     UpperCAmelCase       :							Tuple  = scope



   def        __magic_name__    (							self							:     List[str]						):
     UpperCAmelCase       :							Tuple  = ids_tensor([self.batch_size, self.seq_length],		self.vocab_size						)

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

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

     UpperCAmelCase       :							Optional[Any]  = self.get_config()

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



   def        __magic_name__    (							self							:     Union[str, Any]						):
     UpperCAmelCase       :							Dict  = EsmConfig(
         vocab_size=3_3,		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=UpperCAmelCase_,		esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False},		)
     return config



   def        __magic_name__    (							self							:     Optional[Any],		__A							:     int,		__A							:     List[str],		__A							:     str,		__A							:     List[str],		__A							:     List[str],		__A							:     str						):
     UpperCAmelCase       :							str  = EsmForProteinFolding(config=UpperCAmelCase_						).float()
     model.to(UpperCAmelCase_						)
     model.eval()
     UpperCAmelCase       :							str  = model(UpperCAmelCase_,		attention_mask=UpperCAmelCase_						)
     UpperCAmelCase       :							Optional[int]  = model(UpperCAmelCase_						)
     UpperCAmelCase       :							Any  = model(UpperCAmelCase_						)

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





   def        __magic_name__    (							self							:     List[Any]						):
     UpperCAmelCase       :							Optional[Any]  = self.prepare_config_and_inputs()
     (
         UpperCAmelCase					
     )       :							Optional[Any]  = config_and_inputs
     UpperCAmelCase       :							Any  = {"input_ids": input_ids, "attention_mask": input_mask}
     return config, inputs_dict






@require_torch
class 					__UpperCAmelCase     (       __SCREAMING_SNAKE_CASE			,  __SCREAMING_SNAKE_CASE			,  unittest.TestCase						):
   UpperCamelCase        =     False

   UpperCamelCase        =     (EsmForProteinFolding,) if is_torch_available() else ()
   UpperCamelCase        =     ()
   UpperCamelCase        =     {} if is_torch_available() else {}
   UpperCamelCase        =     False



   def        __magic_name__    (							self							:     Optional[Any]						):
     UpperCAmelCase       :							Optional[Any]  = EsmFoldModelTester(self						)
     UpperCAmelCase       :							str  = ConfigTester(self,		config_class=UpperCAmelCase_,		hidden_size=3_7						)



   def        __magic_name__    (							self							:     Union[str, Any]						):
     self.config_tester.run_common_tests()



   def        __magic_name__    (							self							:     List[str]						):
     UpperCAmelCase       :							Any  = self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*UpperCAmelCase_						)



   @unittest.skip('''Does not support attention outputs'''						)
   def        __magic_name__    (							self							:     List[Any]						):
     pass



   @unittest.skip
   def        __magic_name__    (							self							:     int						):
     pass



   @unittest.skip('''Esm does not support embedding resizing'''						)
   def        __magic_name__    (							self							:     str						):
     pass



   @unittest.skip('''Esm does not support embedding resizing'''						)
   def        __magic_name__    (							self							:     Optional[int]						):
     pass



   @unittest.skip('''ESMFold does not support passing input embeds!'''						)
   def        __magic_name__    (							self							:     Dict						):
     pass



   @unittest.skip('''ESMFold does not support head pruning.'''						)
   def        __magic_name__    (							self							:     Optional[Any]						):
     pass



   @unittest.skip('''ESMFold does not support head pruning.'''						)
   def        __magic_name__    (							self							:     str						):
     pass



   @unittest.skip('''ESMFold does not support head pruning.'''						)
   def        __magic_name__    (							self							:     Any						):
     pass



   @unittest.skip('''ESMFold does not support head pruning.'''						)
   def        __magic_name__    (							self							:     Optional[Any]						):
     pass



   @unittest.skip('''ESMFold does not support head pruning.'''						)
   def        __magic_name__    (							self							:     Union[str, Any]						):
     pass



   @unittest.skip('''ESMFold does not output hidden states in the normal way.'''						)
   def        __magic_name__    (							self							:     List[str]						):
     pass



   @unittest.skip('''ESMfold does not output hidden states in the normal way.'''						)
   def        __magic_name__    (							self							:     List[str]						):
     pass



   @unittest.skip('''ESMFold only has one output format.'''						)
   def        __magic_name__    (							self							:     Dict						):
     pass



   @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality'''						)
   def        __magic_name__    (							self							:     List[str]						):
     pass



   @unittest.skip('''ESMFold does not support input chunking.'''						)
   def        __magic_name__    (							self							:     Dict						):
     pass



   @unittest.skip('''ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.'''						)
   def        __magic_name__    (							self							:     int						):
     pass



   @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''						)
   def        __magic_name__    (							self							:     str						):
     pass



   @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''						)
   def        __magic_name__    (							self							:     Any						):
     pass



   @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''						)
   def        __magic_name__    (							self							:     List[Any]						):
     pass



   @unittest.skip('''ESMFold doesn\'t support data parallel.'''						)
   def        __magic_name__    (							self							:     Dict						):
     pass





   @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''						)
   def        __magic_name__    (							self							:     Union[str, Any]						):
     pass






@require_torch
class 					__UpperCAmelCase     (       __SCREAMING_SNAKE_CASE						):



   @slow
   def        __magic_name__    (							self							:     Any						):
     UpperCAmelCase       :							Optional[Any]  = EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1'''						).float()
     model.eval()
     UpperCAmelCase       :							Any  = torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]						)
     UpperCAmelCase       :							int  = model(UpperCAmelCase_						)["positions"]
     UpperCAmelCase       :							Tuple  = torch.tensor([2.5_8_2_8, 0.7_9_9_3, -1_0.9_3_3_4],		dtype=torch.floataa						)
     self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0],		UpperCAmelCase_,		atol=1E-4						)						)












import collections
import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

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


__A									=				logging.get_logger(__name__)

__A									=				"▁"

__A									=				{"vocab_file": "prophetnet.tokenizer"}

__A									=				{
    "vocab_file": {
        "microsoft/xprophetnet-large-wiki100-cased": (
            "https://huggingface.co/microsoft/xprophetnet-large-wiki100-cased/resolve/main/prophetnet.tokenizer"
        ),
    }
}

__A									=				{
    "microsoft/xprophetnet-large-wiki100-cased": {"do_lower_case": False},
}

__A									=				{
    "microsoft/xprophetnet-large-wiki100-cased": 512,
}





def      lowerCAmelCase_ (       __a		)     ->		int:







  """simple docstring"""





  lowerCamelCase__:      Optional[Any]			  =collections.OrderedDict()
  with open(__a	,    "r"	,    encoding="utf-8"		) as reader:
    lowerCamelCase__:      int			  =reader.readlines()
  for index, token in enumerate(__a		):
    lowerCamelCase__:      List[str]			  =token.rstrip("\n"		)
    lowerCamelCase__:      List[Any]			  =index
  return vocab






class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''
 lowercase_		=			VOCAB_FILES_NAMES
 lowercase_		=			PRETRAINED_VOCAB_FILES_MAP
 lowercase_		=			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowercase_		=			["input_ids", "attention_mask"]

 def __init__(self				:   Union[str, Any]       , UpperCAmelCase_				:   Union[str, Any]       , UpperCAmelCase_				:   Optional[Any]="[SEP]"       , UpperCAmelCase_				:   List[Any]="[SEP]"       , UpperCAmelCase_				:   Optional[Any]="[SEP]"       , UpperCAmelCase_				:   int="[UNK]"       , UpperCAmelCase_				:   Optional[Any]="[PAD]"       , UpperCAmelCase_				:   Dict="[CLS]"       , UpperCAmelCase_				:   Dict="[MASK]"       , UpperCAmelCase_				:   Optional[Dict[str, Any]] = None       , **UpperCAmelCase_				:   Tuple       , )       ->None:



   '''simple docstring'''



   lowerCamelCase__:      int			  ={} if sp_model_kwargs is None else sp_model_kwargs

   super().__init__(
       bos_token=UpperCAmelCase_       , eos_token=UpperCAmelCase_       , sep_token=UpperCAmelCase_       , unk_token=UpperCAmelCase_       , pad_token=UpperCAmelCase_       , cls_token=UpperCAmelCase_       , mask_token=UpperCAmelCase_       , sp_model_kwargs=self.sp_model_kwargs       , **UpperCAmelCase_       , )

   try:
     import sentencepiece as spm
   except ImportError:
     logger.warning(
         "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"
         " pip install sentencepiece")
     raise

   lowerCamelCase__:      Optional[int]			  =spm.SentencePieceProcessor(**self.sp_model_kwargs)
   self.sp_model.Load(str(UpperCAmelCase_))
   lowerCamelCase__:      Optional[int]			  =vocab_file

   # Original fairseq vocab and spm vocab must be "aligned":
   # Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
   # -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
   # fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
   # spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

   # put special tokens and [unused] tokens into the vocab
   lowerCamelCase__:      Optional[int]			  ={"[PAD]": 0, "[CLS]": 1, "[SEP]": 2, "[UNK]": 3, "[MASK]": 4}

   for i in range(10):
     lowerCamelCase__:      Optional[int]			  =F"""[unused{i}]"""
     lowerCamelCase__:      int			  =5 + i

   # The first "real" token "," has position 15 in the embedding vocab and position 3 in the spm vocab
   lowerCamelCase__:      int			  =12
   lowerCamelCase__:      Optional[Any]			  ={v: k for k, v in self.fairseq_tokens_to_ids.items()}
   for k in self.fairseq_tokens_to_ids.keys():
     self.unique_no_split_tokens.append(UpperCAmelCase_)

 def __getstate__(self				:   List[str])       ->Dict:



   '''simple docstring'''



   lowerCamelCase__:      Optional[int]			  =self.__dict__.copy()
   lowerCamelCase__:      Dict			  =None
   return state

 def __setstate__(self				:   List[str]       , UpperCAmelCase_				:   Union[str, Any])       ->Dict:



   '''simple docstring'''



   lowerCamelCase__:      Tuple			  =d
   try:
     import sentencepiece as spm
   except ImportError:
     logger.warning(
         "You need to install SentencePiece to use XLMRobertaTokenizer: https://github.com/google/sentencepiece"
         " pip install sentencepiece")
     raise

   # for backward compatibility
   if not hasattr(self       , "sp_model_kwargs"):
     lowerCamelCase__:      Dict			  ={}

   lowerCamelCase__:      Tuple			  =spm.SentencePieceProcessor(**self.sp_model_kwargs)
   self.sp_model.Load(self.vocab_file)

 def 							SCREAMING_SNAKE_CASE_					(self				:   List[str]       , UpperCAmelCase_				:   List[int]       , UpperCAmelCase_				:   Optional[List[int]] = None       , UpperCAmelCase_				:   bool = False)       ->List[int]:



   '''simple docstring'''



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

   if token_ids_a is None:
     return ([0] * len(UpperCAmelCase_)) + [1]
   return ([0] * len(UpperCAmelCase_)) + [1] + ([0] * len(UpperCAmelCase_)) + [1]

 def 							SCREAMING_SNAKE_CASE_					(self				:   Dict       , UpperCAmelCase_				:   List[int]       , UpperCAmelCase_				:   Optional[List[int]] = None)       ->List[int]:



   '''simple docstring'''



   lowerCamelCase__:      Any			  =[self.sep_token_id]

   if token_ids_a is None:
     return len(token_ids_a + sep) * [0]
   return len(token_ids_a + sep + sep + token_ids_a + sep) * [0]

 @property
 def 							SCREAMING_SNAKE_CASE_					(self				:   str)       ->Dict:



   '''simple docstring'''



   return len(self.sp_model) + self.fairseq_offset

 def 							SCREAMING_SNAKE_CASE_					(self				:   List[str])       ->Tuple:



   '''simple docstring'''



   lowerCamelCase__:      str			  ={self.convert_ids_to_tokens(UpperCAmelCase_): i for i in range(self.vocab_size)}
   vocab.update(self.added_tokens_encoder)
   return vocab

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



   '''simple docstring'''



   return self.sp_model.encode(UpperCAmelCase_       , out_type=UpperCAmelCase_)

 def 							SCREAMING_SNAKE_CASE_					(self				:   List[Any]       , UpperCAmelCase_				:   List[Any])       ->str:



   '''simple docstring'''



   if token in self.fairseq_tokens_to_ids:
     return self.fairseq_tokens_to_ids[token]
   lowerCamelCase__:      str			  =self.sp_model.PieceToId(UpperCAmelCase_)

   # Need to return unknown token if the SP model returned 0
   return spm_id + self.fairseq_offset if spm_id else self.unk_token_id

 def 							SCREAMING_SNAKE_CASE_					(self				:   str       , UpperCAmelCase_				:   Optional[Any])       ->Optional[int]:



   '''simple docstring'''



   if index in self.fairseq_ids_to_tokens:
     return self.fairseq_ids_to_tokens[index]
   return self.sp_model.IdToPiece(index - self.fairseq_offset)

 def 							SCREAMING_SNAKE_CASE_					(self				:   int       , UpperCAmelCase_				:   Optional[Any])       ->Optional[Any]:



   '''simple docstring'''



   lowerCamelCase__:      Union[str, Any]			  ="".join(UpperCAmelCase_).replace(UpperCAmelCase_       , " ").strip()
   return out_string

 def 							SCREAMING_SNAKE_CASE_					(self				:   Union[str, Any]       , UpperCAmelCase_				:   str       , UpperCAmelCase_				:   Optional[str] = None)       ->Tuple[str]:



   '''simple docstring'''



   if not os.path.isdir(UpperCAmelCase_):
     logger.error(F"""Vocabulary path ({save_directory}) should be a directory""")
     return
   lowerCamelCase__:      List[str]			  =os.path.join(
       UpperCAmelCase_       , (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"])

   if os.path.abspath(self.vocab_file) != os.path.abspath(UpperCAmelCase_) and os.path.isfile(self.vocab_file):
     copyfile(self.vocab_file       , UpperCAmelCase_)
   elif not os.path.isfile(self.vocab_file):
     with open(UpperCAmelCase_       , "wb") as fi:
       lowerCamelCase__:      Dict			  =self.sp_model.serialized_model_proto()
       fi.write(UpperCAmelCase_)

   return (out_vocab_file,)







 def 							SCREAMING_SNAKE_CASE_					(self				:   List[str]       , UpperCAmelCase_				:   List[int]       , UpperCAmelCase_				:   Optional[List[int]] = None)       ->List[int]:



   '''simple docstring'''



   if token_ids_a is None:
     return token_ids_a + [self.sep_token_id]
   lowerCamelCase__:      Union[str, Any]			  =[self.sep_token_id]
   return token_ids_a + sep + token_ids_a + sep

