


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     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE=13     ,       _SCREAMING_SNAKE_CASE=7     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=True     ,       _SCREAMING_SNAKE_CASE=99     ,       _SCREAMING_SNAKE_CASE=32     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=4     ,       _SCREAMING_SNAKE_CASE=37     ,       _SCREAMING_SNAKE_CASE="gelu"     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=0.1     ,       _SCREAMING_SNAKE_CASE=512     ,       _SCREAMING_SNAKE_CASE=16     ,       _SCREAMING_SNAKE_CASE=2     ,       _SCREAMING_SNAKE_CASE=0.02     ,       _SCREAMING_SNAKE_CASE=3     ,       _SCREAMING_SNAKE_CASE=4     ,       _SCREAMING_SNAKE_CASE=None     ,       )					->			Union[str, Any]:




									"""simple docstring"""





									_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       UpperCAmelCase__							(     self		)					->			Any:




									"""simple docstring"""





									_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=_SCREAMING_SNAKE_CASE     ,       )

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Tuple:




									"""simple docstring"""





									_UpperCAmelCase											=     TFConvBertModel(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

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

									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)

									self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			Dict:




									"""simple docstring"""





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




									"""simple docstring"""





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

									_UpperCAmelCase											=     model(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_labels)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			List[str]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.num_choices
									_UpperCAmelCase											=     TFConvBertForMultipleChoice(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE     ,       1		)     ,       (1, self.num_choices, 1)		)
									_UpperCAmelCase											=     tf.tile(tf.expand_dims(_SCREAMING_SNAKE_CASE     ,       1		)     ,       (1, self.num_choices, 1)		)
									_UpperCAmelCase											=     tf.tile(tf.expand_dims(_SCREAMING_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(_SCREAMING_SNAKE_CASE		)
									self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.num_choices)		)
		def       UpperCAmelCase__							(     self     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)					->			int:




									"""simple docstring"""





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




									"""simple docstring"""





									_UpperCAmelCase											=     TFConvBertForQuestionAnswering(config=_SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     {
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}

									_UpperCAmelCase											=     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       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





									_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 						__a (							UpperCAmelCase       , UpperCAmelCase       , unittest.TestCase     ):
		_a      :    Optional[int]           =      (
		    (
		        TFConvBertModel,
		        TFConvBertForMaskedLM,
		        TFConvBertForQuestionAnswering,
		        TFConvBertForSequenceClassification,
		        TFConvBertForTokenClassification,
		        TFConvBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		_a      :    Optional[Any]           =      (
		    {
		        'feature-extraction': TFConvBertModel,
		        'fill-mask': TFConvBertForMaskedLM,
		        'question-answering': TFConvBertForQuestionAnswering,
		        'text-classification': TFConvBertForSequenceClassification,
		        'token-classification': TFConvBertForTokenClassification,
		        'zero-shot': TFConvBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		_a      :    Tuple           =      False
		_a      :    Any           =      False
		_a      :    Optional[int]           =      False
		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     TFConvBertModelTester(self		)
									_UpperCAmelCase											=     ConfigTester(self     ,       config_class=_SCREAMING_SNAKE_CASE     ,       hidden_size=37		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





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




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_SCREAMING_SNAKE_CASE		)
		def       UpperCAmelCase__							(     self		)					->			Optional[int]:




									"""simple docstring"""





									_UpperCAmelCase											=     self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_SCREAMING_SNAKE_CASE		)
		@slow
		def       UpperCAmelCase__							(     self		)					->			Dict:




									"""simple docstring"""





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

									if hasattr(_SCREAMING_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'     ,       _SCREAMING_SNAKE_CASE		)

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

																with tempfile.TemporaryDirectory() as tmpdirname:
																							model.save_pretrained(_SCREAMING_SNAKE_CASE     ,       saved_model=_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase											=     os.path.join(_SCREAMING_SNAKE_CASE     ,       'saved_model'     ,       '1'		)
																							_UpperCAmelCase											=     tf.keras.models.load_model(_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase											=     model(_SCREAMING_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(_SCREAMING_SNAKE_CASE		)     ,       _SCREAMING_SNAKE_CASE		)

																							_UpperCAmelCase											=     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       UpperCAmelCase__							(     self		)					->			List[Any]:




									"""simple docstring"""





									_UpperCAmelCase											=     TFConvBertModel.from_pretrained('YituTech/conv-bert-base'		)
									self.assertIsNotNone(_SCREAMING_SNAKE_CASE		)






		def       UpperCAmelCase__							(     self		)					->			Union[str, Any]:




									"""simple docstring"""





									_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'     ,       _SCREAMING_SNAKE_CASE		)
									_UpperCAmelCase											=     getattr(self.model_tester     ,       'key_length'     ,       _SCREAMING_SNAKE_CASE		)

									def check_decoder_attentions_output(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase											=     len(_SCREAMING_SNAKE_CASE		)
																self.assertEqual(out_len % 2     ,       0		)
																_UpperCAmelCase											=     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(_SCREAMING_SNAKE_CASE		):
																_UpperCAmelCase											=     [
																    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:
																_UpperCAmelCase											=     True
																_UpperCAmelCase											=     False
																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     model(self._prepare_for_class(_SCREAMING_SNAKE_CASE     ,       _SCREAMING_SNAKE_CASE		)		)
																_UpperCAmelCase											=     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:
																							_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																							_UpperCAmelCase											=     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"]
																_UpperCAmelCase											=     True
																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     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
																_UpperCAmelCase											=     True
																_UpperCAmelCase											=     True
																_UpperCAmelCase											=     model_class(_SCREAMING_SNAKE_CASE		)
																_UpperCAmelCase											=     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       UpperCAmelCase__							(     self		)					->			str:




									"""simple docstring"""





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

									_UpperCAmelCase											=     [1, 6, 768]
									self.assertEqual(output.shape     ,       _SCREAMING_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]     ,       _SCREAMING_SNAKE_CASE     ,       atol=1e-4		)




def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return x if y == 0 else greatest_common_divisor(a__     ,    x % y			)

def 			lowerCAmelCase__					(     a__:						int     ,    a__:						int			)    ->						int:


							'''simple docstring'''





							return (x * y) // greatest_common_divisor(a__     ,    a__			)

def 			lowerCAmelCase__					(     a__:						int = 2_0			)    ->						int:


							'''simple docstring'''





							_UpperCAmelCase											=     1
							for i in range(1     ,    n + 1			):
														_UpperCAmelCase											=     lcm(a__     ,    a__			)
							return g


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