



"""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 ,		lowercase__ ,		lowercase__=13 ,		lowercase__=7 ,		lowercase__=True ,		lowercase__=True ,		lowercase__=True ,		lowercase__=True ,		lowercase__=99 ,		lowercase__=32 ,		lowercase__=2 ,		lowercase__=4 ,		lowercase__=37 ,		lowercase__="gelu" ,		lowercase__=0.1 ,		lowercase__=0.1 ,		lowercase__=5_12 ,		lowercase__=16 ,		lowercase__=2 ,		lowercase__=0.02 ,		lowercase__=3 ,		lowercase__=4 ,		lowercase__=None ,		):
											snake_case_       :		Optional[Any]			       =       parent
											snake_case_       :		List[str]			       =       13
											snake_case_       :		Optional[Any]			       =       7
											snake_case_       :		Any			       =       True
											snake_case_       :		Any			       =       True
											snake_case_       :		Tuple			       =       True
											snake_case_       :		Tuple			       =       True
											snake_case_       :		int			       =       99
											snake_case_       :		str			       =       3_84
											snake_case_       :		Any			       =       2
											snake_case_       :		Union[str, Any]			       =       4
											snake_case_       :		Optional[Any]			       =       37
											snake_case_       :		Optional[int]			       =       """gelu"""
											snake_case_       :		Tuple			       =       0.1
											snake_case_       :		Dict			       =       0.1
											snake_case_       :		Dict			       =       5_12
											snake_case_       :		str			       =       16
											snake_case_       :		str			       =       2
											snake_case_       :		Tuple			       =       0.02
											snake_case_       :		Optional[int]			       =       3
											snake_case_       :		Optional[int]			       =       4
											snake_case_       :		Dict			       =       1_28
											snake_case_       :		str			       =       2
											snake_case_       :		List[str]			       =       9
											snake_case_       :		Tuple			       =       1
											snake_case_       :		Dict			       =       None







						def     __UpperCamelCase				(self							):
											snake_case_       :		List[Any]			       =       ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size							)

											snake_case_       :		Tuple			       =       None
											if self.use_input_mask:
																snake_case_       :		int			       =       random_attention_mask([self.batch_size, self.seq_length]							)

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

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

											snake_case_       :		Dict			       =       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=lowerCamelCase_ ,		)

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







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Any			       =       TFConvBertModel(config=lowerCamelCase_							)
											snake_case_       :		List[Any]			       =       {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

											snake_case_       :		int			       =       [input_ids, input_mask]
											snake_case_       :		Dict			       =       model(lowerCamelCase_							)

											snake_case_       :		Optional[Any]			       =       model(lowerCamelCase_							)

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







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Tuple			       =       TFConvBertForMaskedLM(config=lowerCamelCase_							)
											snake_case_       :		List[Any]			       =       {
											    """input_ids""": input_ids,
											    """attention_mask""": input_mask,
											    """token_type_ids""": token_type_ids,
											}
											snake_case_       :		Optional[int]			       =       model(lowerCamelCase_							)
											self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.vocab_size)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Union[str, Any]			       =       self.num_labels
											snake_case_       :		Tuple			       =       TFConvBertForSequenceClassification(config=lowerCamelCase_							)
											snake_case_       :		int			       =       {
											    """input_ids""": input_ids,
											    """attention_mask""": input_mask,
											    """token_type_ids""": token_type_ids,
											}

											snake_case_       :		List[str]			       =       model(lowerCamelCase_							)
											self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_labels)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Tuple			       =       self.num_choices
											snake_case_       :		List[Any]			       =       TFConvBertForMultipleChoice(config=lowerCamelCase_							)
											snake_case_       :		Tuple			       =       tf.tile(tf.expand_dims(lowerCamelCase_ ,		1							) ,		(1, self.num_choices, 1)							)
											snake_case_       :		Optional[Any]			       =       tf.tile(tf.expand_dims(lowerCamelCase_ ,		1							) ,		(1, self.num_choices, 1)							)
											snake_case_       :		str			       =       tf.tile(tf.expand_dims(lowerCamelCase_ ,		1							) ,		(1, self.num_choices, 1)							)
											snake_case_       :		Optional[Any]			       =       {
											    """input_ids""": multiple_choice_inputs_ids,
											    """attention_mask""": multiple_choice_input_mask,
											    """token_type_ids""": multiple_choice_token_type_ids,
											}
											snake_case_       :		Any			       =       model(lowerCamelCase_							)
											self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.num_choices)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		List[Any]			       =       self.num_labels
											snake_case_       :		str			       =       TFConvBertForTokenClassification(config=lowerCamelCase_							)
											snake_case_       :		Dict			       =       {
											    """input_ids""": input_ids,
											    """attention_mask""": input_mask,
											    """token_type_ids""": token_type_ids,
											}
											snake_case_       :		List[str]			       =       model(lowerCamelCase_							)
											self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.num_labels)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Dict			       =       TFConvBertForQuestionAnswering(config=lowerCamelCase_							)
											snake_case_       :		List[str]			       =       {
											    """input_ids""": input_ids,
											    """attention_mask""": input_mask,
											    """token_type_ids""": token_type_ids,
											}

											snake_case_       :		List[Any]			       =       model(lowerCamelCase_							)
											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     __UpperCamelCase				(self							):
											snake_case_       :		List[str]			       =       self.prepare_config_and_inputs()
											(
											    (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,      (
											    snake_case_   
											)       ,         
											)       :		str			       =       config_and_inputs
											snake_case_       :		Dict			       =       {"""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"""







						_A		:				Tuple											=		(
						    (
						        TFConvBertModel,
						        TFConvBertForMaskedLM,
						        TFConvBertForQuestionAnswering,
						        TFConvBertForSequenceClassification,
						        TFConvBertForTokenClassification,
						        TFConvBertForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						_A		:				int											=		(
						    {
						        "feature-extraction": TFConvBertModel,
						        "fill-mask": TFConvBertForMaskedLM,
						        "question-answering": TFConvBertForQuestionAnswering,
						        "text-classification": TFConvBertForSequenceClassification,
						        "token-classification": TFConvBertForTokenClassification,
						        "zero-shot": TFConvBertForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						_A		:				Any											=		False
						_A		:				Dict											=		False
						_A		:				int											=		False







						def     __UpperCamelCase				(self							):
											snake_case_       :		Union[str, Any]			       =       TFConvBertModelTester(self							)
											snake_case_       :		List[Any]			       =       ConfigTester(self ,		config_class=lowerCamelCase_ ,		hidden_size=37							)







						def     __UpperCamelCase				(self							):
											self.config_tester.run_common_tests()







						def     __UpperCamelCase				(self							):
											snake_case_       :		str			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Tuple			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Union[str, Any]			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Optional[int]			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		List[Any]			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Optional[int]			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_							)







						@slow
						def     __UpperCamelCase				(self							):
											snake_case_       ,      snake_case_       :		Dict			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		int			       =       True
											snake_case_       :		Optional[Any]			       =       True

											if hasattr(lowerCamelCase_ ,		"""use_cache"""							):
																snake_case_       :		Optional[int]			       =       True

											snake_case_       :		Any			       =       getattr(self.model_tester ,		"""encoder_seq_length""" ,		self.model_tester.seq_length							)
											snake_case_       :		Any			       =       getattr(self.model_tester ,		"""key_length""" ,		lowerCamelCase_							)

											for model_class in self.all_model_classes:
																snake_case_       :		List[str]			       =       self._prepare_for_class(lowerCamelCase_ ,		lowerCamelCase_							)
																snake_case_       :		Dict			       =       model_class(lowerCamelCase_							)
																snake_case_       :		Dict			       =       len(model(lowerCamelCase_							)							)

																with tempfile.TemporaryDirectory() as tmpdirname:
																					model.save_pretrained(lowerCamelCase_ ,		saved_model=lowerCamelCase_							)
																					snake_case_       :		Optional[int]			       =       os.path.join(lowerCamelCase_ ,		"""saved_model""" ,		"""1"""							)
																					snake_case_       :		List[Any]			       =       tf.keras.models.load_model(lowerCamelCase_							)
																					snake_case_       :		int			       =       model(lowerCamelCase_							)

																					if self.is_encoder_decoder:
																										snake_case_       :		Optional[Any]			       =       outputs["""encoder_hidden_states"""]
																										snake_case_       :		List[str]			       =       outputs["""encoder_attentions"""]
																					else:
																										snake_case_       :		Dict			       =       outputs["""hidden_states"""]
																										snake_case_       :		Union[str, Any]			       =       outputs["""attentions"""]

																					self.assertEqual(len(lowerCamelCase_							) ,		lowerCamelCase_							)

																					snake_case_       :		Any			       =       getattr(
																					    self.model_tester ,		"""expected_num_hidden_layers""" ,		self.model_tester.num_hidden_layers + 1							)

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

																					self.assertEqual(len(lowerCamelCase_							) ,		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     __UpperCamelCase				(self							):
											snake_case_       :		Any			       =       TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
											self.assertIsNotNone(lowerCamelCase_							)







						def     __UpperCamelCase				(self							):
											snake_case_       ,      snake_case_       :		Tuple			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		Optional[Any]			       =       True
											snake_case_       :		Tuple			       =       getattr(self.model_tester ,		"""decoder_seq_length""" ,		self.model_tester.seq_length							)
											snake_case_       :		Union[str, Any]			       =       getattr(self.model_tester ,		"""encoder_seq_length""" ,		self.model_tester.seq_length							)
											snake_case_       :		Optional[Any]			       =       getattr(self.model_tester ,		"""key_length""" ,		lowerCamelCase_							)
											snake_case_       :		Optional[Any]			       =       getattr(self.model_tester ,		"""key_length""" ,		lowerCamelCase_							)

											def check_decoder_attentions_output(lowercase__							):
																snake_case_       :		Optional[int]			       =       len(lowerCamelCase_							)
																self.assertEqual(out_len % 2 ,		0							)
																snake_case_       :		Union[str, Any]			       =       outputs.decoder_attentions
																self.assertEqual(len(lowerCamelCase_							) ,		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(lowercase__							):
																snake_case_       :		int			       =       [
																    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
																]
																self.assertEqual(len(lowerCamelCase_							) ,		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:
																snake_case_       :		Tuple			       =       True
																snake_case_       :		Optional[int]			       =       False
																snake_case_       :		Any			       =       model_class(lowerCamelCase_							)
																snake_case_       :		Optional[Any]			       =       model(self._prepare_for_class(lowerCamelCase_ ,		lowerCamelCase_							)							)
																snake_case_       :		Any			       =       len(lowerCamelCase_							)
																self.assertEqual(config.output_hidden_states ,		lowerCamelCase_							)
																check_encoder_attentions_output(lowerCamelCase_							)

																if self.is_encoder_decoder:
																					snake_case_       :		List[Any]			       =       model_class(lowerCamelCase_							)
																					snake_case_       :		Optional[Any]			       =       model(self._prepare_for_class(lowerCamelCase_ ,		lowerCamelCase_							)							)
																					self.assertEqual(config.output_hidden_states ,		lowerCamelCase_							)
																					check_decoder_attentions_output(lowerCamelCase_							)

																# Check that output attentions can also be changed via the config
																del inputs_dict["output_attentions"]
																snake_case_       :		Optional[Any]			       =       True
																snake_case_       :		Tuple			       =       model_class(lowerCamelCase_							)
																snake_case_       :		Optional[int]			       =       model(self._prepare_for_class(lowerCamelCase_ ,		lowerCamelCase_							)							)
																self.assertEqual(config.output_hidden_states ,		lowerCamelCase_							)
																check_encoder_attentions_output(lowerCamelCase_							)

																# Check attention is always last and order is fine
																snake_case_       :		List[str]			       =       True
																snake_case_       :		Union[str, Any]			       =       True
																snake_case_       :		Optional[Any]			       =       model_class(lowerCamelCase_							)
																snake_case_       :		Optional[int]			       =       model(self._prepare_for_class(lowerCamelCase_ ,		lowerCamelCase_							)							)

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







@require_tf
class  __lowercase  (       unittest.TestCase):







						"""simple docstring"""







						@slow
						def     __UpperCamelCase				(self							):
											snake_case_       :		Dict			       =       TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""							)
											snake_case_       :		Tuple			       =       tf.constant([[0, 1, 2, 3, 4, 5]]							)
											snake_case_       :		Union[str, Any]			       =       model(lowerCamelCase_							)[0]

											snake_case_       :		List[str]			       =       [1, 6, 7_68]
											self.assertEqual(output.shape ,		lowerCamelCase_							)

											snake_case_       :		List[str]			       =       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] ,		lowerCamelCase_ ,		atol=1e-4							)








'''simple docstring'''






from __future__ import annotations


def    _snake_case  (	A			)		->       bool:
							lowerCAmelCase__			  =      str(A			)
							return len(A			) == 9 and set(A			) == set('''123456789'''			)


def    _snake_case  (	)		->       int | None:
							for base_num in range(9999					,       4999					,       -1			):
														lowerCAmelCase__			  =      100002 * base_num
														if is_9_pandigital(A			):
																					return candidate

							for base_num in range(333					,       99					,       -1			):
														lowerCAmelCase__			  =      1002003 * base_num
														if is_9_pandigital(A			):
																					return candidate

							return None


if __name__ == "__main__":
		print(f"""{solution() = }""")