


"""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 import EsmForMaskedLM, EsmForSequenceClassification, EsmForTokenClassification, EsmModel
		from transformers.models.esm.modeling_esm import (
		    ESM_PRETRAINED_MODEL_ARCHIVE_LIST,
		    EsmEmbeddings,
		    create_position_ids_from_input_ids,
		)




class  UpperCamelCase_	:

	"""simple docstring"""





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




	def   UpperCAmelCase_      ( self			:				str   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size   )

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

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

						__SCREAMING_SNAKE_CASE						   =				self.get_config()

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




	def   UpperCAmelCase_      ( self			:				Union[str, Any]   )       ->	Dict:
						return EsmConfig(
						    vocab_size=self.vocab_size			,			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			,			)




	def   UpperCAmelCase_      ( self			:				List[str]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				Union[str, Any]			,			UpperCAmelCase__			:				Optional[int]			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				Dict   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				EsmModel(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__   )
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__   )

						self.parent.assertEqual(result.last_hidden_state.shape			,			(self.batch_size, self.seq_length, self.hidden_size)   )
						self.parent.assertEqual(result.pooler_output.shape			,			(self.batch_size, self.hidden_size)   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Union[str, Any]			,			UpperCAmelCase__			:				Optional[Any]			,			UpperCAmelCase__			:				int			,			UpperCAmelCase__			:				List[str]			,			UpperCAmelCase__			:				List[str]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				EsmForMaskedLM(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__			,			labels=UpperCAmelCase__   )
						self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.vocab_size)   )




	def   UpperCAmelCase_      ( self			:				List[str]			,			UpperCAmelCase__			:				str			,			UpperCAmelCase__			:				Any			,			UpperCAmelCase__			:				Optional[int]			,			UpperCAmelCase__			:				str			,			UpperCAmelCase__			:				Optional[int]			,			UpperCAmelCase__			:				Any   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				self.num_labels
						__SCREAMING_SNAKE_CASE						   =				EsmForTokenClassification(config=UpperCAmelCase__   )
						model.to(UpperCAmelCase__   )
						model.eval()
						__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__			,			attention_mask=UpperCAmelCase__			,			labels=UpperCAmelCase__   )
						self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.num_labels)   )




	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				self.prepare_config_and_inputs()
						(
						    (
						    __SCREAMING_SNAKE_CASE
						)   ,  (
						    __SCREAMING_SNAKE_CASE
						)   ,  (
						    __SCREAMING_SNAKE_CASE
						)   ,  (
						    __SCREAMING_SNAKE_CASE
						)   ,  (
						    __SCREAMING_SNAKE_CASE
						)   ,  (
						    __SCREAMING_SNAKE_CASE
						)   ,  
						)						   =				config_and_inputs
						__SCREAMING_SNAKE_CASE						   =				{"input_ids": input_ids, "attention_mask": input_mask}
						return config, inputs_dict




@require_torch
class  UpperCamelCase_	( UpperCamelCase			,  UpperCamelCase			,  unittest.TestCase):

	"""simple docstring"""





	snake_case__				:  int    					=			False

	snake_case__				:  List[str]    					=			(
	    (
	        EsmForMaskedLM,
	        EsmModel,
	        EsmForSequenceClassification,
	        EsmForTokenClassification,
	    )
	    if is_torch_available()
	    else ()
	)
	snake_case__				:  str    					=			()
	snake_case__				:  List[str]    					=			(
	    {
	        "feature-extraction": EsmModel,
	        "fill-mask": EsmForMaskedLM,
	        "text-classification": EsmForSequenceClassification,
	        "token-classification": EsmForTokenClassification,
	        "zero-shot": EsmForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	snake_case__				:  List[str]    					=			True




	def   UpperCAmelCase_      ( self			:				int   )       ->	Union[str, Any]:
						__SCREAMING_SNAKE_CASE						   =				EsmModelTester(self   )
						__SCREAMING_SNAKE_CASE						   =				ConfigTester(self			,			config_class=UpperCAmelCase__			,			hidden_size=3_7   )




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	str:
						self.config_tester.run_common_tests()




	def   UpperCAmelCase_      ( self			:				int   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Tuple:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						for type in ["absolute", "relative_key", "relative_key_query"]:
											__SCREAMING_SNAKE_CASE						   =				type
											self.model_tester.create_and_check_model(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	List[str]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	List[Any]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__   )




	@slow
	def   UpperCAmelCase_      ( self			:				Optional[Any]   )       ->	List[str]:
						for model_name in ESM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__SCREAMING_SNAKE_CASE						   =				EsmModel.from_pretrained(UpperCAmelCase__   )
											self.assertIsNotNone(UpperCAmelCase__   )




	def   UpperCAmelCase_      ( self			:				int   )       ->	Optional[Any]:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()[0]
						__SCREAMING_SNAKE_CASE						   =				EsmEmbeddings(config=UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				torch.as_tensor([[1_2, 3_1, 1_3, model.padding_idx]]   )
						__SCREAMING_SNAKE_CASE						   =				torch.as_tensor(
						    [
						        [
						            0 + model.padding_idx + 1,
						            1 + model.padding_idx + 1,
						            2 + model.padding_idx + 1,
						            model.padding_idx,
						        ]
						    ]   )
						__SCREAMING_SNAKE_CASE						   =				create_position_ids_from_input_ids(UpperCAmelCase__			,			model.padding_idx   )
						self.assertEqual(position_ids.shape			,			expected_positions.shape   )
						self.assertTrue(torch.all(torch.eq(UpperCAmelCase__			,			UpperCAmelCase__   )   )   )




	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Any:
						__SCREAMING_SNAKE_CASE						   =				self.model_tester.prepare_config_and_inputs()[0]
						__SCREAMING_SNAKE_CASE						   =				EsmEmbeddings(config=UpperCAmelCase__   )

						__SCREAMING_SNAKE_CASE						   =				torch.empty(2			,			4			,			3_0   )
						__SCREAMING_SNAKE_CASE						   =				[
						    0 + embeddings.padding_idx + 1,
						    1 + embeddings.padding_idx + 1,
						    2 + embeddings.padding_idx + 1,
						    3 + embeddings.padding_idx + 1,
						]
						__SCREAMING_SNAKE_CASE						   =				torch.as_tensor([expected_single_positions, expected_single_positions]   )
						__SCREAMING_SNAKE_CASE						   =				embeddings.create_position_ids_from_inputs_embeds(UpperCAmelCase__   )
						self.assertEqual(position_ids.shape			,			expected_positions.shape   )
						self.assertTrue(torch.all(torch.eq(UpperCAmelCase__			,			UpperCAmelCase__   )   )   )




	@unittest.skip("Esm does not support embedding resizing"   )
	def   UpperCAmelCase_      ( self			:				List[Any]   )       ->	Union[str, Any]:
						pass




	@unittest.skip("Esm does not support embedding resizing"   )
	def   UpperCAmelCase_      ( self			:				Optional[int]   )       ->	Any:
						pass




	@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
	def   UpperCAmelCase_      ( self			:				Dict   )       ->	Tuple:
						pass




@require_torch
class  UpperCamelCase_	( UpperCamelCase):

	"""simple docstring"""





	@slow
	def   UpperCAmelCase_      ( self			:				Dict   )       ->	Dict:
						with torch.no_grad():
											__SCREAMING_SNAKE_CASE						   =				EsmForMaskedLM.from_pretrained("facebook/esm2_t6_8M_UR50D"   )
											model.eval()
											__SCREAMING_SNAKE_CASE						   =				torch.tensor([[0, 1, 2, 3, 4, 5]]   )
											__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__   )[0]

											__SCREAMING_SNAKE_CASE						   =				3_3

											__SCREAMING_SNAKE_CASE						   =				torch.Size((1, 6, vocab_size)   )
											self.assertEqual(output.shape			,			UpperCAmelCase__   )

											__SCREAMING_SNAKE_CASE						   =				torch.tensor(
											    [[[8.9_215, -10.5_898, -6.4_671], [-6.3_967, -13.9_114, -1.1_212], [-7.7_812, -13.9_516, -3.7_406]]]   )
											self.assertTrue(torch.allclose(output[:, :3, :3]			,			UpperCAmelCase__			,			atol=1E-4   )   )




	@slow
	def   UpperCAmelCase_      ( self			:				Tuple   )       ->	Optional[Any]:
						with torch.no_grad():
											__SCREAMING_SNAKE_CASE						   =				EsmModel.from_pretrained("facebook/esm2_t6_8M_UR50D"   )
											model.eval()

											__SCREAMING_SNAKE_CASE						   =				torch.tensor([[0, 6, 4, 1_3, 5, 4, 1_6, 1_2, 1_1, 7, 2]]   )
											__SCREAMING_SNAKE_CASE						   =				model(UpperCAmelCase__   )[0]
											# compare the actual values for a slice.
											__SCREAMING_SNAKE_CASE						   =				torch.tensor(
											    [[[0.1_444, 0.5_413, 0.3_248], [0.3_034, 0.0_053, 0.3_108], [0.3_228, -0.2_499, 0.3_415]]]   )
											self.assertTrue(torch.allclose(output[:, :3, :3]			,			UpperCAmelCase__			,			atol=1E-4   )   )




"""simple docstring"""







def    UpperCAmelCase__       (lowerCAmelCase_   ):




					'''simple docstring'''


					if upper_limit < 0:
										raise ValueError("Limit for the Catalan sequence must be ≥ 0"   )

					__SCREAMING_SNAKE_CASE						   =				[0] * (upper_limit + 1)

					# Base case: C(0) = C(1) = 1
					__SCREAMING_SNAKE_CASE						   =				1
					if upper_limit > 0:
										__SCREAMING_SNAKE_CASE						   =				1

					# Recurrence relation: C(i) = sum(C(j).C(i-j-1)), from j = 0 to i
					for i in range(2  ,						upper_limit + 1   ):
										for j in range(lowerCAmelCase_   ):
															catalan_list[i] += catalan_list[j] * catalan_list[i - j - 1]

					return catalan_list


if __name__ == "__main__":
		print('''\n********* Catalan Numbers Using Dynamic Programming ************\n''')
		print('''\n*** Enter -1 at any time to quit ***''')
		print('''\nEnter the upper limit (≥ 0) for the Catalan number sequence: ''', end='''''')
		try:
				while True:
						a__				:							List[str]						=     int(input().strip())
						if N < 0:
								print('''\n********* Goodbye!! ************''')
								break
						else:
								print(F"The Catalan numbers from 0 through {N} are:")
								print(catalan_numbers(N))
								print('''Try another upper limit for the sequence: ''', end='''''')
		except (NameError, ValueError):
				print('''\n********* Invalid input, goodbye! ************\n''')

		import doctest

		doctest.testmod()
