


"""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    __a						:







				def __init__( self      ,	UpperCAmelCase      ,	UpperCAmelCase=13      ,	UpperCAmelCase=7      ,	UpperCAmelCase=True      ,	UpperCAmelCase=True      ,	UpperCAmelCase=True      ,	UpperCAmelCase=True      ,	UpperCAmelCase=99      ,	UpperCAmelCase=32      ,	UpperCAmelCase=2      ,	UpperCAmelCase=4      ,	UpperCAmelCase=37      ,	UpperCAmelCase="gelu"      ,	UpperCAmelCase=0.1      ,	UpperCAmelCase=0.1      ,	UpperCAmelCase=512      ,	UpperCAmelCase=16      ,	UpperCAmelCase=2      ,	UpperCAmelCase=0.0_2      ,	UpperCAmelCase=3      ,	UpperCAmelCase=4      ,	UpperCAmelCase=None      ,	):

					'''simple docstring'''






					lowerCAmelCase_      							=			parent
					lowerCAmelCase_      							=			13
					lowerCAmelCase_      							=			7
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			99
					lowerCAmelCase_      							=			384
					lowerCAmelCase_      							=			2
					lowerCAmelCase_      							=			4
					lowerCAmelCase_      							=			37
					lowerCAmelCase_      							=			'''gelu'''
					lowerCAmelCase_      							=			0.1
					lowerCAmelCase_      							=			0.1
					lowerCAmelCase_      							=			512
					lowerCAmelCase_      							=			16
					lowerCAmelCase_      							=			2
					lowerCAmelCase_      							=			0.0_2
					lowerCAmelCase_      							=			3
					lowerCAmelCase_      							=			4
					lowerCAmelCase_      							=			128
					lowerCAmelCase_      							=			2
					lowerCAmelCase_      							=			9
					lowerCAmelCase_      							=			1
					lowerCAmelCase_      							=			None







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size   )

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

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

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

					lowerCAmelCase_      							=			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=__SCREAMING_SNAKE_CASE      ,	)

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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertModel(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

					lowerCAmelCase_      							=			[input_ids, input_mask]
					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )

					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )

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







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertForMaskedLM(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			{
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					}
					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )
					self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.seq_length, self.vocab_size)   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.num_labels
					lowerCAmelCase_      							=			TFConvBertForSequenceClassification(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			{
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					}

					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )
					self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.num_labels)   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.num_choices
					lowerCAmelCase_      							=			TFConvBertForMultipleChoice(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE      ,	1   )      ,	(1, self.num_choices, 1)   )
					lowerCAmelCase_      							=			tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE      ,	1   )      ,	(1, self.num_choices, 1)   )
					lowerCAmelCase_      							=			tf.tile(tf.expand_dims(__SCREAMING_SNAKE_CASE      ,	1   )      ,	(1, self.num_choices, 1)   )
					lowerCAmelCase_      							=			{
					    '''input_ids''': multiple_choice_inputs_ids,
					    '''attention_mask''': multiple_choice_input_mask,
					    '''token_type_ids''': multiple_choice_token_type_ids,
					}
					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )
					self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.num_choices)   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.num_labels
					lowerCAmelCase_      							=			TFConvBertForTokenClassification(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			{
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					}
					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )
					self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.seq_length, self.num_labels)   )







				def      lowerCamelCase_     ( self      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase      ,	UpperCAmelCase   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertForQuestionAnswering(config=__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			{
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					}

					lowerCAmelCase_      							=			model(__SCREAMING_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   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.prepare_config_and_inputs()
					(
					    (
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	(
					    lowerCAmelCase_     
					)							,	     
					)      							=			config_and_inputs
					lowerCAmelCase_      							=			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
					return config, inputs_dict







@require_tf
class    __a						( __lowerCAmelCase					,			__lowerCAmelCase					,			unittest.TestCase							):
				lowerCamelCase     :       str			       =(
				    (
				        TFConvBertModel,
				        TFConvBertForMaskedLM,
				        TFConvBertForQuestionAnswering,
				        TFConvBertForSequenceClassification,
				        TFConvBertForTokenClassification,
				        TFConvBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				lowerCamelCase     :       Optional[int]			       =(
				    {
				        '''feature-extraction''': TFConvBertModel,
				        '''fill-mask''': TFConvBertForMaskedLM,
				        '''question-answering''': TFConvBertForQuestionAnswering,
				        '''text-classification''': TFConvBertForSequenceClassification,
				        '''token-classification''': TFConvBertForTokenClassification,
				        '''zero-shot''': TFConvBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				lowerCamelCase     :       Optional[Any]			       =False
				lowerCamelCase     :       List[str]			       =False
				lowerCamelCase     :       Union[str, Any]			       =False







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertModelTester(self   )
					lowerCAmelCase_      							=			ConfigTester(self      ,	config_class=__SCREAMING_SNAKE_CASE      ,	hidden_size=37   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					self.config_tester.run_common_tests()







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE   )







				@slow
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			True

					if hasattr(__SCREAMING_SNAKE_CASE      ,	'''use_cache'''   ):
						lowerCAmelCase_      							=			True

					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''encoder_seq_length'''      ,	self.model_tester.seq_length   )
					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''key_length'''      ,	__SCREAMING_SNAKE_CASE   )

					for model_class in self.all_model_classes:
						lowerCAmelCase_      							=			self._prepare_for_class(__SCREAMING_SNAKE_CASE      ,	__SCREAMING_SNAKE_CASE   )
						lowerCAmelCase_      							=			model_class(__SCREAMING_SNAKE_CASE   )
						lowerCAmelCase_      							=			len(model(__SCREAMING_SNAKE_CASE   )   )

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

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

							self.assertEqual(len(__SCREAMING_SNAKE_CASE   )      ,	__SCREAMING_SNAKE_CASE   )

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

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

							self.assertEqual(len(__SCREAMING_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   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''   )
					self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )







				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_							,	lowerCAmelCase_      							=			self.model_tester.prepare_config_and_inputs_for_common()
					lowerCAmelCase_      							=			True
					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''decoder_seq_length'''      ,	self.model_tester.seq_length   )
					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''encoder_seq_length'''      ,	self.model_tester.seq_length   )
					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''key_length'''      ,	__SCREAMING_SNAKE_CASE   )
					lowerCAmelCase_      							=			getattr(self.model_tester      ,	'''key_length'''      ,	__SCREAMING_SNAKE_CASE   )

					def check_decoder_attentions_output(UpperCAmelCase   ):
						lowerCAmelCase_      							=			len(__SCREAMING_SNAKE_CASE   )
						self.assertEqual(out_len % 2      ,	0   )
						lowerCAmelCase_      							=			outputs.decoder_attentions
						self.assertEqual(len(__SCREAMING_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(UpperCAmelCase   ):
						lowerCAmelCase_      							=			[
						    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
						]
						self.assertEqual(len(__SCREAMING_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:
						lowerCAmelCase_      							=			True
						lowerCAmelCase_      							=			False
						lowerCAmelCase_      							=			model_class(__SCREAMING_SNAKE_CASE   )
						lowerCAmelCase_      							=			model(self._prepare_for_class(__SCREAMING_SNAKE_CASE      ,	__SCREAMING_SNAKE_CASE   )   )
						lowerCAmelCase_      							=			len(__SCREAMING_SNAKE_CASE   )
						self.assertEqual(config.output_hidden_states      ,	__SCREAMING_SNAKE_CASE   )
						check_encoder_attentions_output(__SCREAMING_SNAKE_CASE   )

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

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

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

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







@require_tf
class    __a						( unittest.TestCase							):







				@slow
				def      lowerCamelCase_     ( self   ):

					'''simple docstring'''






					lowerCAmelCase_      							=			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''   )
					lowerCAmelCase_      							=			tf.constant([[0, 1, 2, 3, 4, 5]]   )
					lowerCAmelCase_      							=			model(__SCREAMING_SNAKE_CASE   )[0]

					lowerCAmelCase_      							=			[1, 6, 768]
					self.assertEqual(output.shape      ,	__SCREAMING_SNAKE_CASE   )

					lowerCAmelCase_      							=			tf.constant(
					    [
					        [
					            [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
					            [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
					            [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
					        ]
					    ]   )
					tf.debugging.assert_near(output[:, :3, :3]      ,	__SCREAMING_SNAKE_CASE      ,	atol=1E-4   )





'''simple docstring'''



def       _UpperCamelCase			(_lowerCamelCase				:  int						)-> int:




     '''simple docstring'''





     if divisor % 5 == 0 or divisor % 2 == 0:
          return 0
     __snake_case            =	1
     __snake_case            =	1
     while repunit:
          __snake_case            =	(10 * repunit + 1) % divisor
          repunit_index += 1
     return repunit_index







def       _UpperCamelCase			(_lowerCamelCase				:  int = 1_00_00_00						)-> int:




     '''simple docstring'''





     __snake_case            =	limit - 1
     if divisor % 2 == 0:
          divisor += 1
     while least_divisible_repunit(_lowerCamelCase						) <= limit:
          divisor += 2
     return divisor


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






