


'''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.models.esm.modeling_esmfold import EsmForProteinFolding
class     __lowerCAmelCase		:





      '''simple docstring'''






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

             '''simple docstring'''







             lowercase__      =					parent
             lowercase__      =					batch_size
             lowercase__      =					seq_length
             lowercase__      =					is_training
             lowercase__      =					use_input_mask
             lowercase__      =					use_token_type_ids
             lowercase__      =					use_labels
             lowercase__      =					vocab_size
             lowercase__      =					hidden_size
             lowercase__      =					num_hidden_layers
             lowercase__      =					num_attention_heads
             lowercase__      =					intermediate_size
             lowercase__      =					hidden_act
             lowercase__      =					hidden_dropout_prob
             lowercase__      =					attention_probs_dropout_prob
             lowercase__      =					max_position_embeddings
             lowercase__      =					type_vocab_size
             lowercase__      =					type_sequence_label_size
             lowercase__      =					initializer_range
             lowercase__      =					num_labels
             lowercase__      =					num_choices
             lowercase__      =					scope



      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.vocab_size					)

             lowercase__      =					None
             if self.use_input_mask:
                    lowercase__      =					random_attention_mask([self.batch_size, self.seq_length]					)

             lowercase__      =					None
             lowercase__      =					None
             lowercase__      =					None
             if self.use_labels:
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.type_sequence_label_size					)
                    lowercase__      =					ids_tensor([self.batch_size, self.seq_length]  ,		self.num_labels					)
                    lowercase__      =					ids_tensor([self.batch_size]  ,		self.num_choices					)

             lowercase__      =					self.get_config()

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



      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             lowercase__      =					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=UpperCamelCase  ,		esmfold_config={'''trunk''': {'''num_blocks''': 2}, '''fp16_esm''': False}  ,		)
             return config



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       Dict  ,		UpperCamelCase       :       Optional[Any]  ,		UpperCamelCase       :       Tuple  ,		UpperCamelCase       :       Optional[int]  ,		UpperCamelCase       :       int					):

             '''simple docstring'''







             lowercase__      =					EsmForProteinFolding(config=UpperCamelCase					).float()
             model.to(UpperCamelCase					)
             model.eval()
             lowercase__      =					model(UpperCamelCase  ,		attention_mask=UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase					)
             lowercase__      =					model(UpperCamelCase					)

             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  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             lowercase__      =					self.prepare_config_and_inputs()
             (
                 (
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,(
                 lowercase__
             )     ,
             )      =					config_and_inputs
             lowercase__      =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
             return config, inputs_dict






@require_torch
class     __lowerCAmelCase		(lowercase_    ,	lowercase_    ,	unittest.TestCase	):





      '''simple docstring'''






      lowerCAmelCase__   :	Any       =				False

      lowerCAmelCase__   :	Optional[Any]       =				(EsmForProteinFolding,) if is_torch_available() else ()
      lowerCAmelCase__   :	List[Any]       =				()
      lowerCAmelCase__   :	Optional[int]       =				{} if is_torch_available() else {}
      lowerCAmelCase__   :	Any       =				False



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             lowercase__      =					EsmFoldModelTester(self					)
             lowercase__      =					ConfigTester(self  ,		config_class=UpperCamelCase  ,		hidden_size=37					)



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             self.config_tester.run_common_tests()



      def  UpperCamelCase__						(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*UpperCamelCase					)



      @unittest.skip('''Does not support attention outputs'''					)
      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             pass



      @unittest.skip
      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             pass



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

             '''simple docstring'''







             pass



      @unittest.skip('''Esm does not support embedding resizing'''					)
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support passing input embeds!'''					)
      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support head pruning.'''					)
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support head pruning.'''					)
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support head pruning.'''					)
      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             pass



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

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support head pruning.'''					)
      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not output hidden states in the normal way.'''					)
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMfold does not output hidden states in the normal way.'''					)
      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold only has one output format.'''					)
      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             pass



      @unittest.skip('''This test doesn\'t work for ESMFold and doesn\'t test core functionality'''					)
      def  UpperCamelCase__						(self       :       Tuple					):

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold does not support input chunking.'''					)
      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             pass



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

             '''simple docstring'''







             pass



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

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold doesn\'t support torchscript compilation.'''					)
      def  UpperCamelCase__						(self       :       Optional[int]					):

             '''simple docstring'''







             pass



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

             '''simple docstring'''







             pass



      @unittest.skip('''ESMFold doesn\'t support data parallel.'''					)
      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             pass






      @unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''					)
      def  UpperCamelCase__						(self       :       Optional[Any]					):

             '''simple docstring'''







             pass






@require_torch
class     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      @slow
      def  UpperCamelCase__						(self       :       str					):

             '''simple docstring'''







             lowercase__      =					EsmForProteinFolding.from_pretrained('''facebook/esmfold_v1'''					).float()
             model.eval()
             lowercase__      =					torch.tensor([[0, 6, 4, 13, 5, 4, 16, 12, 11, 7, 2]]					)
             lowercase__      =					model(UpperCamelCase					)['''positions''']
             lowercase__      =					torch.tensor([2.58_28, 0.79_93, -10.93_34]  ,		dtype=torch.floataa					)
             self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]  ,		UpperCamelCase  ,		atol=1E-4					)					)








'''simple docstring'''

from __future__ import annotations

from collections import deque
class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       str  ,		UpperCamelCase       :       list[str]					):

             '''simple docstring'''







             lowercase__      =					[]
             self.adlist.append(
                 {'''value''': '''''', '''next_states''': [], '''fail_state''': 0, '''output''': []}					)

             for keyword in keywords:
                    self.add_keyword(UpperCamelCase					)
             self.set_fail_transitions()



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       str					):

             '''simple docstring'''







             for state in self.adlist[current_state]["next_states"]:
                    if char == self.adlist[state]["value"]:
                           return state
             return None



      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       str					):

             '''simple docstring'''







             lowercase__      =					0
             for character in keyword:
                    lowercase__      =					self.find_next_state(UpperCamelCase  ,		UpperCamelCase					)
                    if next_state is None:
                           self.adlist.append(
                               {
                                   '''value''': character,
                                   '''next_states''': [],
                                   '''fail_state''': 0,
                                   '''output''': [],
                               }					)
                           self.adlist[current_state]["next_states"].append(len(self.adlist					) - 1					)
                           lowercase__      =					len(self.adlist					) - 1
                    else:
                           lowercase__      =					next_state
             self.adlist[current_state]["output"].append(UpperCamelCase					)



      def  UpperCamelCase__						(self       :       List[str]					):

             '''simple docstring'''







             lowercase__      =					deque()
             for node in self.adlist[0]["next_states"]:
                    q.append(UpperCamelCase					)
                    lowercase__      =					0
             while q:
                    lowercase__      =					q.popleft()
                    for child in self.adlist[r]["next_states"]:
                           q.append(UpperCamelCase					)
                           lowercase__      =					self.adlist[r]['''fail_state''']
                           while (
                               self.find_next_state(UpperCamelCase  ,		self.adlist[child]['''value''']					) is None
                               and state != 0
                           ):
                                  lowercase__      =					self.adlist[state]['''fail_state''']
                           lowercase__      =					self.find_next_state(
                               UpperCamelCase  ,		self.adlist[child]['''value''']					)
                           if self.adlist[child]["fail_state"] is None:
                                  lowercase__      =					0
                           lowercase__      =					(
                               self.adlist[child]['''output''']
                               + self.adlist[self.adlist[child]['''fail_state''']]['''output''']
                           )






      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       str					):

             '''simple docstring'''







             lowercase__      =					{}  # returns a dict with keywords and list of its occurrences
             lowercase__      =					0
             for i in range(len(UpperCamelCase					)					):
                    while (
                        self.find_next_state(UpperCamelCase  ,		string[i]					) is None
                        and current_state != 0
                    ):
                           lowercase__      =					self.adlist[current_state]['''fail_state''']
                    lowercase__      =					self.find_next_state(UpperCamelCase  ,		string[i]					)
                    if next_state is None:
                           lowercase__      =					0
                    else:
                           lowercase__      =					next_state
                           for key in self.adlist[current_state]["output"]:
                                  if key not in result:
                                         lowercase__      =					[]
                                  result[key].append(i - len(UpperCamelCase					) + 1					)
             return result


if __name__ == "__main__":
 import doctest

 doctest.testmod()




