



"""simple docstring"""





from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import (
					    TFConvBertForMaskedLM,
					    TFConvBertForMultipleChoice,
					    TFConvBertForQuestionAnswering,
					    TFConvBertForSequenceClassification,
					    TFConvBertForTokenClassification,
					    TFConvBertModel,
					)




class 			lowercase__			:
							'''simple docstring'''







							def __init__(  self	,       snake_case	,       snake_case=13	,       snake_case=7	,       snake_case=True	,       snake_case=True	,       snake_case=True	,       snake_case=True	,       snake_case=99	,       snake_case=32	,       snake_case=2	,       snake_case=4	,       snake_case=37	,       snake_case="gelu"	,       snake_case=0.1	,       snake_case=0.1	,       snake_case=512	,       snake_case=16	,       snake_case=2	,       snake_case=0.02	,       snake_case=3	,       snake_case=4	,       snake_case=None	,       )			->							Dict:
									_UpperCAmelCase							 =		parent
									_UpperCAmelCase							 =		13
									_UpperCAmelCase							 =		7
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		99
									_UpperCAmelCase							 =		384
									_UpperCAmelCase							 =		2
									_UpperCAmelCase							 =		4
									_UpperCAmelCase							 =		37
									_UpperCAmelCase							 =		'gelu'
									_UpperCAmelCase							 =		0.1
									_UpperCAmelCase							 =		0.1
									_UpperCAmelCase							 =		512
									_UpperCAmelCase							 =		16
									_UpperCAmelCase							 =		2
									_UpperCAmelCase							 =		0.02
									_UpperCAmelCase							 =		3
									_UpperCAmelCase							 =		4
									_UpperCAmelCase							 =		128
									_UpperCAmelCase							 =		2
									_UpperCAmelCase							 =		9
									_UpperCAmelCase							 =		1
									_UpperCAmelCase							 =		None






							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.seq_length]	,       self.vocab_size						)

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

									_UpperCAmelCase							 =		None
									if self.use_token_type_ids:
											_UpperCAmelCase							 =		ids_tensor([self.batch_size, self.seq_length]	,       self.type_vocab_size						)

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

									_UpperCAmelCase							 =		ConvBertConfig(
									    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	,       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	,       return_dict=snake_case	,       )

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






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Any:
									_UpperCAmelCase							 =		TFConvBertModel(config=snake_case						)
									_UpperCAmelCase							 =		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

									_UpperCAmelCase							 =		[input_ids, input_mask]
									_UpperCAmelCase							 =		model(snake_case						)

									_UpperCAmelCase							 =		model(snake_case						)

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






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							List[str]:
									_UpperCAmelCase							 =		TFConvBertForMaskedLM(config=snake_case						)
									_UpperCAmelCase							 =		{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}
									_UpperCAmelCase							 =		model(snake_case						)
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.seq_length, self.vocab_size)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							str:
									_UpperCAmelCase							 =		self.num_labels
									_UpperCAmelCase							 =		TFConvBertForSequenceClassification(config=snake_case						)
									_UpperCAmelCase							 =		{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}

									_UpperCAmelCase							 =		model(snake_case						)
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.num_labels)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							int:
									_UpperCAmelCase							 =		self.num_choices
									_UpperCAmelCase							 =		TFConvBertForMultipleChoice(config=snake_case						)
									_UpperCAmelCase							 =		tf.tile(tf.expand_dims(snake_case	,       1						)	,       (1, self.num_choices, 1)						)
									_UpperCAmelCase							 =		tf.tile(tf.expand_dims(snake_case	,       1						)	,       (1, self.num_choices, 1)						)
									_UpperCAmelCase							 =		tf.tile(tf.expand_dims(snake_case	,       1						)	,       (1, self.num_choices, 1)						)
									_UpperCAmelCase							 =		{
									    'input_ids': multiple_choice_inputs_ids,
									    'attention_mask': multiple_choice_input_mask,
									    'token_type_ids': multiple_choice_token_type_ids,
									}
									_UpperCAmelCase							 =		model(snake_case						)
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.num_choices)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							List[Any]:
									_UpperCAmelCase							 =		self.num_labels
									_UpperCAmelCase							 =		TFConvBertForTokenClassification(config=snake_case						)
									_UpperCAmelCase							 =		{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}
									_UpperCAmelCase							 =		model(snake_case						)
									self.parent.assertEqual(result.logits.shape	,       (self.batch_size, self.seq_length, self.num_labels)						)






							def        lowerCamelCase_   (  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case						)			->							Optional[int]:
									_UpperCAmelCase							 =		TFConvBertForQuestionAnswering(config=snake_case						)
									_UpperCAmelCase							 =		{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}

									_UpperCAmelCase							 =		model(snake_case						)
									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        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		self.prepare_config_and_inputs()
									(
									    (
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					(
									    _UpperCAmelCase
									) ,					
									)							 =		config_and_inputs
									_UpperCAmelCase							 =		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
									return config, inputs_dict







@require_tf
class 			lowercase__			(      A,					A,					unittest.TestCase    ):
							'''simple docstring'''







							_UpperCAmelCase      				=					(
							    (
							        TFConvBertModel,
							        TFConvBertForMaskedLM,
							        TFConvBertForQuestionAnswering,
							        TFConvBertForSequenceClassification,
							        TFConvBertForTokenClassification,
							        TFConvBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							_UpperCAmelCase      				=					(
							    {
							        '''feature-extraction''': TFConvBertModel,
							        '''fill-mask''': TFConvBertForMaskedLM,
							        '''question-answering''': TFConvBertForQuestionAnswering,
							        '''text-classification''': TFConvBertForSequenceClassification,
							        '''token-classification''': TFConvBertForTokenClassification,
							        '''zero-shot''': TFConvBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							_UpperCAmelCase      				=					False
							_UpperCAmelCase      				=					False
							_UpperCAmelCase      				=					False






							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		TFConvBertModelTester(self						)
									_UpperCAmelCase							 =		ConfigTester(self	,       config_class=snake_case	,       hidden_size=37						)






							def        lowerCamelCase_   (  self						)			->							int:
									self.config_tester.run_common_tests()






							def        lowerCamelCase_   (  self						)			->							List[Any]:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*snake_case						)






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*snake_case						)






							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*snake_case						)






							def        lowerCamelCase_   (  self						)			->							Dict:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*snake_case						)






							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*snake_case						)






							def        lowerCamelCase_   (  self						)			->							Union[str, Any]:
									_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*snake_case						)






							@slow
							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase ,					_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs_for_common()
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		True

									if hasattr(snake_case	,       'use_cache'						):
											_UpperCAmelCase							 =		True

									_UpperCAmelCase							 =		getattr(self.model_tester	,       'encoder_seq_length'	,       self.model_tester.seq_length						)
									_UpperCAmelCase							 =		getattr(self.model_tester	,       'key_length'	,       snake_case						)

									for model_class in self.all_model_classes:
											_UpperCAmelCase							 =		self._prepare_for_class(snake_case	,       snake_case						)
											_UpperCAmelCase							 =		model_class(snake_case						)
											_UpperCAmelCase							 =		len(model(snake_case						)						)

											with tempfile.TemporaryDirectory() as tmpdirname:
													model.save_pretrained(snake_case	,       saved_model=snake_case						)
													_UpperCAmelCase							 =		os.path.join(snake_case	,       'saved_model'	,       '1'						)
													_UpperCAmelCase							 =		tf.keras.models.load_model(snake_case						)
													_UpperCAmelCase							 =		model(snake_case						)

													if self.is_encoder_decoder:
															_UpperCAmelCase							 =		outputs['encoder_hidden_states']
															_UpperCAmelCase							 =		outputs['encoder_attentions']
													else:
															_UpperCAmelCase							 =		outputs['hidden_states']
															_UpperCAmelCase							 =		outputs['attentions']

													self.assertEqual(len(snake_case						)	,       snake_case						)

													_UpperCAmelCase							 =		getattr(
													    self.model_tester	,       'expected_num_hidden_layers'	,       self.model_tester.num_hidden_layers + 1						)

													self.assertEqual(len(snake_case						)	,       snake_case						)
													self.assertListEqual(
													    list(output_hidden_states[0].shape[-2:]						)	,       [self.model_tester.seq_length, self.model_tester.hidden_size]	,       )

													self.assertEqual(len(snake_case						)	,       self.model_tester.num_hidden_layers						)
													self.assertListEqual(
													    list(output_attentions[0].shape[-3:]						)	,       [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]	,       )






							@slow
							def        lowerCamelCase_   (  self						)			->							Optional[Any]:
									_UpperCAmelCase							 =		TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
									self.assertIsNotNone(snake_case						)






							def        lowerCamelCase_   (  self						)			->							Tuple:
									_UpperCAmelCase ,					_UpperCAmelCase							 =		self.model_tester.prepare_config_and_inputs_for_common()
									_UpperCAmelCase							 =		True
									_UpperCAmelCase							 =		getattr(self.model_tester	,       'decoder_seq_length'	,       self.model_tester.seq_length						)
									_UpperCAmelCase							 =		getattr(self.model_tester	,       'encoder_seq_length'	,       self.model_tester.seq_length						)
									_UpperCAmelCase							 =		getattr(self.model_tester	,       'key_length'	,       snake_case						)
									_UpperCAmelCase							 =		getattr(self.model_tester	,       'key_length'	,       snake_case						)

									def check_decoder_attentions_output(snake_case						):
											_UpperCAmelCase							 =		len(snake_case						)
											self.assertEqual(out_len % 2	,       0						)
											_UpperCAmelCase							 =		outputs.decoder_attentions
											self.assertEqual(len(snake_case						)	,       self.model_tester.num_hidden_layers						)
											self.assertListEqual(
											    list(decoder_attentions[0].shape[-3:]						)	,       [self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]	,       )

									def check_encoder_attentions_output(snake_case						):
											_UpperCAmelCase							 =		[
											    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
											]
											self.assertEqual(len(snake_case						)	,       self.model_tester.num_hidden_layers						)
											self.assertListEqual(
											    list(attentions[0].shape[-3:]						)	,       [self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]	,       )

									for model_class in self.all_model_classes:
											_UpperCAmelCase							 =		True
											_UpperCAmelCase							 =		False
											_UpperCAmelCase							 =		model_class(snake_case						)
											_UpperCAmelCase							 =		model(self._prepare_for_class(snake_case	,       snake_case						)						)
											_UpperCAmelCase							 =		len(snake_case						)
											self.assertEqual(config.output_hidden_states	,       snake_case						)
											check_encoder_attentions_output(snake_case						)

											if self.is_encoder_decoder:
													_UpperCAmelCase							 =		model_class(snake_case						)
													_UpperCAmelCase							 =		model(self._prepare_for_class(snake_case	,       snake_case						)						)
													self.assertEqual(config.output_hidden_states	,       snake_case						)
													check_decoder_attentions_output(snake_case						)

											# Check that output attentions can also be changed via the config
											del inputs_dict["output_attentions"]
											_UpperCAmelCase							 =		True
											_UpperCAmelCase							 =		model_class(snake_case						)
											_UpperCAmelCase							 =		model(self._prepare_for_class(snake_case	,       snake_case						)						)
											self.assertEqual(config.output_hidden_states	,       snake_case						)
											check_encoder_attentions_output(snake_case						)

											# Check attention is always last and order is fine
											_UpperCAmelCase							 =		True
											_UpperCAmelCase							 =		True
											_UpperCAmelCase							 =		model_class(snake_case						)
											_UpperCAmelCase							 =		model(self._prepare_for_class(snake_case	,       snake_case						)						)

											self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)	,       len(snake_case						)						)
											self.assertEqual(model.config.output_hidden_states	,       snake_case						)
											check_encoder_attentions_output(snake_case						)







@require_tf
class 			lowercase__			(      unittest.TestCase    ):
							'''simple docstring'''







							@slow
							def        lowerCamelCase_   (  self						)			->							List[str]:
									_UpperCAmelCase							 =		TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
									_UpperCAmelCase							 =		tf.constant([[0, 1, 2, 3, 4, 5]]						)
									_UpperCAmelCase							 =		model(snake_case						)[0]

									_UpperCAmelCase							 =		[1, 6, 768]
									self.assertEqual(output.shape	,       snake_case						)

									_UpperCAmelCase							 =		tf.constant(
									    [
									        [
									            [-0.03475493, -0.4686034, -0.30638832],
									            [0.22637248, -0.26988646, -0.7423424],
									            [0.10324868, -0.45013508, -0.58280784],
									        ]
									    ]						)
									tf.debugging.assert_near(output[:, :3, :3]	,       snake_case	,       atol=1E-4						)







"""simple docstring"""





def 	UpperCAmelCase     (		A	:						int       ):

		'''simple docstring'''


		_UpperCAmelCase							 =		[[0 for _ in range(A       )] for _ in range(m + 1       )]
		for i in range(m + 1       ):
				_UpperCAmelCase							 =		1

		for n in range(m + 1       ):
				for k in range(1     ,				A       ):
						memo[n][k] += memo[n][k - 1]
						if n - k > 0:
								memo[n][k] += memo[n - k - 1][k]

		return memo[m][m - 1]


if __name__ == "__main__":
					import sys

					if len(sys.argv) == 1:
										try:
															lowercase  							=					int(input('''Enter a number: ''').strip())
															print(partition(n))
										except ValueError:
															print('''Please enter a number.''')
					else:
										try:
															lowercase  							=					int(sys.argv[1])
															print(partition(n))
										except ValueError:
															print('''Please pass a number.''')


