



import unittest

from transformers import SqueezeBertConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, 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 (
							    SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    SqueezeBertForMaskedLM,
							    SqueezeBertForMultipleChoice,
							    SqueezeBertForQuestionAnswering,
							    SqueezeBertForSequenceClassification,
							    SqueezeBertForTokenClassification,
							    SqueezeBertModel,
							)
class 				_snake_case			(					lowercase__):




				def __init__(						self					:						Dict,					__lowercase					:						Tuple,					__lowercase					:						Optional[Any]=13,					__lowercase					:						int=7,					__lowercase					:						Union[str, Any]=True,					__lowercase					:						List[Any]=True,					__lowercase					:						Optional[int]=False,					__lowercase					:						Dict=True,					__lowercase					:						List[Any]=99,					__lowercase					:						str=32,					__lowercase					:						int=5,					__lowercase					:						int=4,					__lowercase					:						Any=64,					__lowercase					:						str="gelu",					__lowercase					:						List[Any]=0.1,					__lowercase					:						Optional[Any]=0.1,					__lowercase					:						Any=512,					__lowercase					:						List[str]=16,					__lowercase					:						int=2,					__lowercase					:						str=0.02,					__lowercase					:						Optional[int]=3,					__lowercase					:						str=4,					__lowercase					:						List[Any]=None,					__lowercase					:						str=2,					__lowercase					:						Dict=2,					__lowercase					:						Any=2,					__lowercase					:						Dict=2,					__lowercase					:						str=4,					__lowercase					:						Union[str, Any]=1,					):
							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
							lowercase__					  =				q_groups
							lowercase__					  =				k_groups
							lowercase__					  =				v_groups
							lowercase__					  =				post_attention_groups
							lowercase__					  =				intermediate_groups
							lowercase__					  =				output_groups




				def 							A__      (						self					:						List[Any]						):
							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 							A__      (						self					:						Optional[int]						):
							return SqueezeBertConfig(
							    embedding_size=self.hidden_size,					vocab_size=self.vocab_size,					hidden_size=self.hidden_size,					num_hidden_layers=self.num_hidden_layers,					num_attention_heads=self.num_attention_heads,					intermediate_size=self.intermediate_size,					hidden_act=self.hidden_act,					attention_probs_dropout_prob=self.hidden_dropout_prob,					attention_dropout=self.attention_probs_dropout_prob,					max_position_embeddings=self.max_position_embeddings,					initializer_range=self.initializer_range,					q_groups=self.q_groups,					k_groups=self.k_groups,					v_groups=self.v_groups,					post_attention_groups=self.post_attention_groups,					intermediate_groups=self.intermediate_groups,					output_groups=self.output_groups,					)




				def 							A__      (						self					:						Optional[Any],					__lowercase					:						Optional[Any],					__lowercase					:						Optional[int],					__lowercase					:						List[str],					__lowercase					:						List[Any],					__lowercase					:						Tuple,					__lowercase					:						int						):
							lowercase__					  =				SqueezeBertModel(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					__lowercase						)
							lowercase__					  =				model(__lowercase						)
							self.parent.assertEqual(result.last_hidden_state.shape,					(self.batch_size, self.seq_length, self.hidden_size)						)




				def 							A__      (						self					:						List[str],					__lowercase					:						Optional[int],					__lowercase					:						Optional[Any],					__lowercase					:						List[Any],					__lowercase					:						Tuple,					__lowercase					:						Optional[Any],					__lowercase					:						Optional[int]						):
							lowercase__					  =				SqueezeBertForMaskedLM(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					attention_mask=__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.vocab_size)						)




				def 							A__      (						self					:						List[Any],					__lowercase					:						int,					__lowercase					:						Tuple,					__lowercase					:						int,					__lowercase					:						str,					__lowercase					:						Union[str, Any],					__lowercase					:						List[Any]						):
							lowercase__					  =				SqueezeBertForQuestionAnswering(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(
							    __lowercase,					attention_mask=__lowercase,					start_positions=__lowercase,					end_positions=__lowercase						)
							self.parent.assertEqual(result.start_logits.shape,					(self.batch_size, self.seq_length)						)
							self.parent.assertEqual(result.end_logits.shape,					(self.batch_size, self.seq_length)						)




				def 							A__      (						self					:						Tuple,					__lowercase					:						Optional[int],					__lowercase					:						str,					__lowercase					:						Any,					__lowercase					:						List[Any],					__lowercase					:						Dict,					__lowercase					:						Optional[Any]						):
							lowercase__					  =				self.num_labels
							lowercase__					  =				SqueezeBertForSequenceClassification(__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				model(__lowercase,					attention_mask=__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_labels)						)




				def 							A__      (						self					:						int,					__lowercase					:						str,					__lowercase					:						Tuple,					__lowercase					:						Any,					__lowercase					:						Union[str, Any],					__lowercase					:						Any,					__lowercase					:						Union[str, Any]						):
							lowercase__					  =				self.num_labels
							lowercase__					  =				SqueezeBertForTokenClassification(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()

							lowercase__					  =				model(__lowercase,					attention_mask=__lowercase,					labels=__lowercase						)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.num_labels)						)




				def 							A__      (						self					:						int,					__lowercase					:						Optional[Any],					__lowercase					:						Tuple,					__lowercase					:						Optional[int],					__lowercase					:						List[str],					__lowercase					:						List[str],					__lowercase					:						Dict						):
							lowercase__					  =				self.num_choices
							lowercase__					  =				SqueezeBertForMultipleChoice(config=__lowercase						)
							model.to(__lowercase						)
							model.eval()
							lowercase__					  =				input_ids.unsqueeze(1						).expand(-1,					self.num_choices,					-1						).contiguous()
							lowercase__					  =				input_mask.unsqueeze(1						).expand(-1,					self.num_choices,					-1						).contiguous()
							lowercase__					  =				model(
							    __lowercase,					attention_mask=__lowercase,					labels=__lowercase,					)
							self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.num_choices)						)




				def 							A__      (						self					:						Union[str, Any]						):
							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 				_snake_case			(					lowercase__    ,	lowercase__    ,	unittest.TestCase):
				UpperCamelCase__				:					Tuple     		=(
				    (
				        SqueezeBertModel,
				        SqueezeBertForMaskedLM,
				        SqueezeBertForMultipleChoice,
				        SqueezeBertForQuestionAnswering,
				        SqueezeBertForSequenceClassification,
				        SqueezeBertForTokenClassification,
				    )
				    if is_torch_available()
				    else None
				)
				UpperCamelCase__				:					List[str]     		=(
				    {
				        """feature-extraction""": SqueezeBertModel,
				        """fill-mask""": SqueezeBertForMaskedLM,
				        """question-answering""": SqueezeBertForQuestionAnswering,
				        """text-classification""": SqueezeBertForSequenceClassification,
				        """token-classification""": SqueezeBertForTokenClassification,
				        """zero-shot""": SqueezeBertForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)
				UpperCamelCase__				:					Tuple     		=False
				UpperCamelCase__				:					Dict     		=True
				UpperCamelCase__				:					Union[str, Any]     		=False




				def 							A__      (						self					:						Any						):
							lowercase__					  =				SqueezeBertModelTester(self						)
							lowercase__					  =				ConfigTester(self,					config_class=__lowercase,					dim=37						)




				def 							A__      (						self					:						Tuple						):
							self.config_tester.run_common_tests()




				def 							A__      (						self					:						str						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_model(*__lowercase						)




				def 							A__      (						self					:						Any						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_masked_lm(*__lowercase						)




				def 							A__      (						self					:						int						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_question_answering(*__lowercase						)




				def 							A__      (						self					:						Dict						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_sequence_classification(*__lowercase						)




				def 							A__      (						self					:						Union[str, Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_token_classification(*__lowercase						)




				def 							A__      (						self					:						Optional[Any]						):
							lowercase__					  =				self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_squeezebert_for_multiple_choice(*__lowercase						)




				@slow
				def 							A__      (						self					:						Dict						):
							for model_name in SQUEEZEBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										lowercase__					  =				SqueezeBertModel.from_pretrained(__lowercase						)
										self.assertIsNotNone(__lowercase						)
@require_sentencepiece
@require_tokenizers
@require_torch
class 				_snake_case			(					unittest.TestCase):




				@slow
				def 							A__      (						self					:						List[Any]						):
							lowercase__					  =				SqueezeBertForSequenceClassification.from_pretrained("squeezebert/squeezebert-mnli"						)

							lowercase__					  =				torch.tensor([[1, 2_9414, 232, 328, 740, 1140, 1_2695, 69, 13, 1588, 2]]						)
							lowercase__					  =				model(__lowercase						)[0]
							lowercase__					  =				torch.Size((1, 3)						)
							self.assertEqual(output.shape,					__lowercase						)
							lowercase__					  =				torch.tensor([[0.6401, -0.0349, -0.6041]]						)
							self.assertTrue(torch.allclose(__lowercase,					__lowercase,					atol=1e-4						)						)









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

			lowercase__					  =				[0] * (upper_limit + 1)

			# Base case: C(0) = C(1) = 1
			lowercase__					  =				1
			if upper_limit > 0:
						lowercase__					  =				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(SCREAMING_SNAKE_CASE_  ):
									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:
																					lowercase_								=     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()




