



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

     """simple docstring"""






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


     def 		lowercase   ( self :	Any ):
      snake_case__				:							Optional[Any]      							= ids_tensor([self.batch_size, self.seq_length]   ,			self.vocab_size )

      snake_case__				:							List[Any]      							= None
      if self.use_input_mask:
       snake_case__				:							List[str]      							= random_attention_mask([self.batch_size, self.seq_length] )

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

      snake_case__				:							Union[str, Any]      							= self.get_config()

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


     def 		lowercase   ( self :	Union[str, Any] ):
      snake_case__				:							Optional[Any]      							= 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 		lowercase   ( self :	Union[str, Any]   ,			__a :	Union[str, Any]   ,			__a :	Any   ,			__a :	Optional[int]   ,			__a :	Optional[Any]   ,			__a :	Union[str, Any]   ,			__a :	Tuple ):
      snake_case__				:							List[Any]      							= EsmForProteinFolding(config=__UpperCamelCase ).float()
      model.to(__UpperCamelCase )
      model.eval()
      snake_case__				:							str      							= model(__UpperCamelCase   ,			attention_mask=__UpperCamelCase )
      snake_case__				:							List[Any]      							= model(__UpperCamelCase )
      snake_case__				:							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 		lowercase   ( self :	Any ):
      snake_case__				:							str      							= self.prepare_config_and_inputs()
      (
          (
          snake_case__   
      )							,		(
          snake_case__   
      )							,		(
          snake_case__   
      )							,		(
          snake_case__   
      )							,		(
          snake_case__   
      )							,		(
          snake_case__   
      )							,		   
      )				:							int      							= config_and_inputs
      snake_case__				:							Tuple      							= {"""input_ids""": input_ids, """attention_mask""": input_mask}
      return config, inputs_dict



@require_torch
class 						lowercase__      (_lowerCamelCase						,      _lowerCamelCase						,      unittest.TestCase						):

     """simple docstring"""






     __UpperCamelCase    :	Dict		=       False

     __UpperCamelCase    :	List[Any]		=       (EsmForProteinFolding,) if is_torch_available() else ()
     __UpperCamelCase    :	Tuple		=       ()
     __UpperCamelCase    :	List[Any]		=       {} if is_torch_available() else {}
     __UpperCamelCase    :	str		=       False


     def 		lowercase   ( self :	List[Any] ):
      snake_case__				:							int      							= EsmFoldModelTester(self )
      snake_case__				:							Optional[int]      							= ConfigTester(self   ,			config_class=__UpperCamelCase   ,			hidden_size=3_7 )


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


     def 		lowercase   ( self :	List[str] ):
      snake_case__				:							Union[str, Any]      							= self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__UpperCamelCase )


     @unittest.skip("""Does not support attention outputs""" )
     def 		lowercase   ( self :	Optional[int] ):
      pass


     @unittest.skip
     def 		lowercase   ( self :	Dict ):
      pass


     @unittest.skip("""Esm does not support embedding resizing""" )
     def 		lowercase   ( self :	List[str] ):
      pass


     @unittest.skip("""Esm does not support embedding resizing""" )
     def 		lowercase   ( self :	List[str] ):
      pass


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


     @unittest.skip("""ESMFold does not support head pruning.""" )
     def 		lowercase   ( self :	Tuple ):
      pass


     @unittest.skip("""ESMFold does not support head pruning.""" )
     def 		lowercase   ( self :	Optional[int] ):
      pass


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


     @unittest.skip("""ESMFold does not support head pruning.""" )
     def 		lowercase   ( self :	Optional[int] ):
      pass


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


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


     @unittest.skip("""ESMfold does not output hidden states in the normal way.""" )
     def 		lowercase   ( self :	Any ):
      pass


     @unittest.skip("""ESMFold only has one output format.""" )
     def 		lowercase   ( self :	Optional[int] ):
      pass


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


     @unittest.skip("""ESMFold does not support input chunking.""" )
     def 		lowercase   ( self :	Any ):
      pass


     @unittest.skip("""ESMFold doesn\'t respect you and it certainly doesn\'t respect your initialization arguments.""" )
     def 		lowercase   ( self :	Optional[Any] ):
      pass


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


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


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


     @unittest.skip("""ESMFold doesn\'t support data parallel.""" )
     def 		lowercase   ( self :	List[Any] ):
      pass


     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests.""" )
     def 		lowercase   ( self :	List[Any] ):
      pass



@require_torch
class 						lowercase__      (_lowerCamelCase						):

     """simple docstring"""






     @slow
     def 		lowercase   ( self :	Union[str, Any] ):
      snake_case__				:							Optional[int]      							= EsmForProteinFolding.from_pretrained("""facebook/esmfold_v1""" ).float()
      model.eval()
      snake_case__				:							Optional[Any]      							= torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]] )
      snake_case__				:							Optional[int]      							= model(__UpperCamelCase )["""positions"""]
      snake_case__				:							Tuple      							= torch.tensor([2.5828, 0.7993, -1_0.9_3_3_4]   ,			dtype=torch.floataa )
      self.assertTrue(torch.allclose(position_outputs[0, 0, 0, 0]   ,			__UpperCamelCase   ,			atol=1e-4 ) )









__snake_case     :Any			       =256
# Modulus to hash a string
__snake_case     :Optional[int]			       =1000003






def  lowerCamelCase_	(      lowerCAmelCase__	:	str			,	lowerCAmelCase__	:	str				)       ->							bool:

							'''simple docstring'''






							A      						=    len(lowerCAmelCase__				)
							A      						=    len(lowerCAmelCase__				)
							if p_len > t_len:
														return False

							A      						=    0
							A      						=    0
							A      						=    1

							# Calculating the hash of pattern and substring of text
							for i in range(lowerCAmelCase__				):
														A      						=    (ord(pattern[i]				) + p_hash * alphabet_size) % modulus
														A      						=    (ord(text[i]				) + text_hash * alphabet_size) % modulus
														if i == p_len - 1:
																					continue
														A      						=    (modulus_power * alphabet_size) % modulus

							for i in range(0			,	t_len - p_len + 1				):
														if text_hash == p_hash and text[i : i + p_len] == pattern:
																					return True
														if i == t_len - p_len:
																					continue
														# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
														A      						=    (
														    (text_hash - ord(text[i]				) * modulus_power) * alphabet_size
														    + ord(text[i + p_len]				)
														) % modulus
							return False






def  lowerCamelCase_	(      )       ->							None:

							'''simple docstring'''






							A      						=    'abc1abc12'
							A      						=    'alskfjaldsabc1abc1abc12k23adsfabcabc'
							A      						=    'alskfjaldsk23adsfabcabc'
							assert rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				) and not rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)

							# Test 2)
							A      						=    'ABABX'
							A      						=    'ABABZABABYABABX'
							assert rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)

							# Test 3)
							A      						=    'AAAB'
							A      						=    'ABAAAAAB'
							assert rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)

							# Test 4)
							A      						=    'abcdabcy'
							A      						=    'abcxabcdabxabcdabcdabcy'
							assert rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)

							# Test 5)
							A      						=    'Lü'
							A      						=    'Lüsai'
							assert rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)
							A      						=    'Lue'
							assert not rabin_karp(lowerCAmelCase__			,	lowerCAmelCase__				)
							print('Success.'				)


if __name__ == "__main__":
				test_rabin_karp()