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



				def __init__(self       :   Dict			,						_A       :   Dict			,						_A       :   Dict=13			,						_A       :   Optional[Any]=7			,						_A       :   Tuple=True			,						_A       :   Optional[int]=True			,						_A       :   Dict=True			,						_A       :   Dict=True			,						_A       :   int=99			,						_A       :   str=32			,						_A       :   Dict=2			,						_A       :   Any=4			,						_A       :   Any=37			,						_A       :   Optional[Any]="gelu"			,						_A       :   Any=0.1			,						_A       :   Optional[int]=0.1			,						_A       :   Any=5_12			,						_A       :   int=16			,						_A       :   Union[str, Any]=2			,						_A       :   List[str]=0.02			,						_A       :   Dict=3			,						_A       :   Tuple=4			,						_A       :   Tuple=None			,						)					->			List[Any]:
					__snake_case    :							Tuple   	=				parent
					__snake_case    :							Optional[Any]   	=				13
					__snake_case    :							int   	=				7
					__snake_case    :							List[Any]   	=				True
					__snake_case    :							Dict   	=				True
					__snake_case    :							Optional[Any]   	=				True
					__snake_case    :							List[str]   	=				True
					__snake_case    :							Optional[int]   	=				99
					__snake_case    :							str   	=				3_84
					__snake_case    :							Any   	=				2
					__snake_case    :							str   	=				4
					__snake_case    :							Dict   	=				37
					__snake_case    :							Optional[int]   	=				'gelu'
					__snake_case    :							Any   	=				0.1
					__snake_case    :							int   	=				0.1
					__snake_case    :							Any   	=				5_12
					__snake_case    :							Tuple   	=				16
					__snake_case    :							Union[str, Any]   	=				2
					__snake_case    :							int   	=				0.02
					__snake_case    :							Optional[Any]   	=				3
					__snake_case    :							List[str]   	=				4
					__snake_case    :							Any   	=				1_28
					__snake_case    :							Union[str, Any]   	=				2
					__snake_case    :							Any   	=				9
					__snake_case    :							List[Any]   	=				1
					__snake_case    :							List[str]   	=				None



				def       _lowercase  (self       :   Union[str, Any])					->			Optional[int]:
					__snake_case    :							List[Any]   	=				ids_tensor([self.batch_size, self.seq_length]			,						self.vocab_size)

					__snake_case    :							Optional[Any]   	=				None
					if self.use_input_mask:
						__snake_case    :							Tuple   	=				random_attention_mask([self.batch_size, self.seq_length])

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

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

					__snake_case    :							Union[str, Any]   	=				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=_A			,						)

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



				def       _lowercase  (self       :   List[Any]			,						_A       :   Dict			,						_A       :   Any			,						_A       :   Optional[int]			,						_A       :   Optional[Any]			,						_A       :   Union[str, Any]			,						_A       :   Optional[int]			,						_A       :   int)					->			Union[str, Any]:
					__snake_case    :							int   	=				TFConvBertModel(config=_A)
					__snake_case    :							List[Any]   	=				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

					__snake_case    :							Dict   	=				[input_ids, input_mask]
					__snake_case    :							str   	=				model(_A)

					__snake_case    :							Union[str, Any]   	=				model(_A)

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



				def       _lowercase  (self       :   Any			,						_A       :   List[str]			,						_A       :   List[Any]			,						_A       :   Optional[Any]			,						_A       :   List[str]			,						_A       :   Union[str, Any]			,						_A       :   List[Any]			,						_A       :   Any)					->			Union[str, Any]:
					__snake_case    :							Dict   	=				TFConvBertForMaskedLM(config=_A)
					__snake_case    :							Optional[int]   	=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					__snake_case    :							List[str]   	=				model(_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.vocab_size))



				def       _lowercase  (self       :   List[Any]			,						_A       :   Optional[Any]			,						_A       :   Optional[Any]			,						_A       :   Tuple			,						_A       :   Any			,						_A       :   List[Any]			,						_A       :   int			,						_A       :   List[str])					->			Any:
					__snake_case    :							List[str]   	=				self.num_labels
					__snake_case    :							List[str]   	=				TFConvBertForSequenceClassification(config=_A)
					__snake_case    :							Dict   	=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}

					__snake_case    :							Dict   	=				model(_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_labels))



				def       _lowercase  (self       :   str			,						_A       :   int			,						_A       :   Optional[int]			,						_A       :   List[Any]			,						_A       :   int			,						_A       :   Any			,						_A       :   Any			,						_A       :   List[str])					->			List[Any]:
					__snake_case    :							Dict   	=				self.num_choices
					__snake_case    :							int   	=				TFConvBertForMultipleChoice(config=_A)
					__snake_case    :							Union[str, Any]   	=				tf.tile(tf.expand_dims(_A			,						1)			,						(1, self.num_choices, 1))
					__snake_case    :							List[Any]   	=				tf.tile(tf.expand_dims(_A			,						1)			,						(1, self.num_choices, 1))
					__snake_case    :							Tuple   	=				tf.tile(tf.expand_dims(_A			,						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    :							Optional[Any]   	=				model(_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.num_choices))



				def       _lowercase  (self       :   Optional[int]			,						_A       :   Optional[int]			,						_A       :   Dict			,						_A       :   int			,						_A       :   Optional[Any]			,						_A       :   List[Any]			,						_A       :   str			,						_A       :   Tuple)					->			str:
					__snake_case    :							List[Any]   	=				self.num_labels
					__snake_case    :							Union[str, Any]   	=				TFConvBertForTokenClassification(config=_A)
					__snake_case    :							List[str]   	=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}
					__snake_case    :							Dict   	=				model(_A)
					self.parent.assertEqual(result.logits.shape			,						(self.batch_size, self.seq_length, self.num_labels))



				def       _lowercase  (self       :   List[str]			,						_A       :   Union[str, Any]			,						_A       :   int			,						_A       :   List[str]			,						_A       :   Union[str, Any]			,						_A       :   int			,						_A       :   List[Any]			,						_A       :   Optional[int])					->			Union[str, Any]:
					__snake_case    :							Optional[Any]   	=				TFConvBertForQuestionAnswering(config=_A)
					__snake_case    :							Optional[Any]   	=				{
					    'input_ids': input_ids,
					    'attention_mask': input_mask,
					    'token_type_ids': token_type_ids,
					}

					__snake_case    :							str   	=				model(_A)
					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       _lowercase  (self       :   Optional[int])					->			List[str]:
					__snake_case    :							str   	=				self.prepare_config_and_inputs()
					(
					    (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       (
					    __snake_case
					)	,       
					)    :							Tuple   	=				config_and_inputs
					__snake_case    :							Union[str, Any]   	=				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
					return config, inputs_dict







@require_tf
class 	UpperCamelCase    (					lowercase   ,			lowercase   ,			unittest.TestCase		):
				UpperCAmelCase       :   List[str]					=	(
				    (
				        TFConvBertModel,
				        TFConvBertForMaskedLM,
				        TFConvBertForQuestionAnswering,
				        TFConvBertForSequenceClassification,
				        TFConvBertForTokenClassification,
				        TFConvBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				UpperCAmelCase       :   Union[str, Any]					=	(
				    {
				        """feature-extraction""": TFConvBertModel,
				        """fill-mask""": TFConvBertForMaskedLM,
				        """question-answering""": TFConvBertForQuestionAnswering,
				        """text-classification""": TFConvBertForSequenceClassification,
				        """token-classification""": TFConvBertForTokenClassification,
				        """zero-shot""": TFConvBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				UpperCAmelCase       :   Union[str, Any]					=	False
				UpperCAmelCase       :   str					=	False
				UpperCAmelCase       :   Dict					=	False



				def       _lowercase  (self       :   Union[str, Any])					->			str:
					__snake_case    :							List[str]   	=				TFConvBertModelTester(self)
					__snake_case    :							str   	=				ConfigTester(self			,						config_class=_A			,						hidden_size=37)



				def       _lowercase  (self       :   Optional[int])					->			Union[str, Any]:
					self.config_tester.run_common_tests()



				def       _lowercase  (self       :   Dict)					->			Dict:
					__snake_case    :							List[Any]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_A)



				def       _lowercase  (self       :   Union[str, Any])					->			int:
					__snake_case    :							Any   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_masked_lm(*_A)



				def       _lowercase  (self       :   Tuple)					->			Optional[Any]:
					__snake_case    :							Any   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_multiple_choice(*_A)



				def       _lowercase  (self       :   Optional[int])					->			Tuple:
					__snake_case    :							Optional[Any]   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_question_answering(*_A)



				def       _lowercase  (self       :   Optional[Any])					->			Union[str, Any]:
					__snake_case    :							Tuple   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_sequence_classification(*_A)



				def       _lowercase  (self       :   List[str])					->			int:
					__snake_case    :							str   	=				self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_token_classification(*_A)



				@slow
				def       _lowercase  (self       :   Optional[Any])					->			Any:
					__snake_case	,       __snake_case    :							Tuple   	=				self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case    :							Optional[Any]   	=				True
					__snake_case    :							int   	=				True

					if hasattr(_A			,						'use_cache'):
						__snake_case    :							List[str]   	=				True

					__snake_case    :							Optional[Any]   	=				getattr(self.model_tester			,						'encoder_seq_length'			,						self.model_tester.seq_length)
					__snake_case    :							Any   	=				getattr(self.model_tester			,						'key_length'			,						_A)

					for model_class in self.all_model_classes:
						__snake_case    :							Optional[int]   	=				self._prepare_for_class(_A			,						_A)
						__snake_case    :							List[Any]   	=				model_class(_A)
						__snake_case    :							int   	=				len(model(_A))

						with tempfile.TemporaryDirectory() as tmpdirname:
							model.save_pretrained(_A			,						saved_model=_A)
							__snake_case    :							str   	=				os.path.join(_A			,						'saved_model'			,						'1')
							__snake_case    :							Optional[Any]   	=				tf.keras.models.load_model(_A)
							__snake_case    :							Optional[Any]   	=				model(_A)

							if self.is_encoder_decoder:
								__snake_case    :							Tuple   	=				outputs['encoder_hidden_states']
								__snake_case    :							int   	=				outputs['encoder_attentions']
							else:
								__snake_case    :							Optional[int]   	=				outputs['hidden_states']
								__snake_case    :							str   	=				outputs['attentions']

							self.assertEqual(len(_A)			,						_A)

							__snake_case    :							Optional[Any]   	=				getattr(
							    self.model_tester			,						'expected_num_hidden_layers'			,						self.model_tester.num_hidden_layers + 1)

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

							self.assertEqual(len(_A)			,						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       _lowercase  (self       :   Dict)					->			List[Any]:
					__snake_case    :							Dict   	=				TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
					self.assertIsNotNone(_A)






				def       _lowercase  (self       :   List[Any])					->			Tuple:
					__snake_case	,       __snake_case    :							Dict   	=				self.model_tester.prepare_config_and_inputs_for_common()
					__snake_case    :							List[str]   	=				True
					__snake_case    :							List[Any]   	=				getattr(self.model_tester			,						'decoder_seq_length'			,						self.model_tester.seq_length)
					__snake_case    :							List[str]   	=				getattr(self.model_tester			,						'encoder_seq_length'			,						self.model_tester.seq_length)
					__snake_case    :							Tuple   	=				getattr(self.model_tester			,						'key_length'			,						_A)
					__snake_case    :							Dict   	=				getattr(self.model_tester			,						'key_length'			,						_A)

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

						if self.is_encoder_decoder:
							__snake_case    :							str   	=				model_class(_A)
							__snake_case    :							Dict   	=				model(self._prepare_for_class(_A			,						_A))
							self.assertEqual(config.output_hidden_states			,						_A)
							check_decoder_attentions_output(_A)

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

						# Check attention is always last and order is fine
						__snake_case    :							Optional[int]   	=				True
						__snake_case    :							str   	=				True
						__snake_case    :							Tuple   	=				model_class(_A)
						__snake_case    :							str   	=				model(self._prepare_for_class(_A			,						_A))

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







@require_tf
class 	UpperCamelCase    (					unittest.TestCase		):



				@slow
				def       _lowercase  (self       :   List[Any])					->			List[str]:
					__snake_case    :							Optional[Any]   	=				TFConvBertModel.from_pretrained('YituTech/conv-bert-base')
					__snake_case    :							Tuple   	=				tf.constant([[0, 1, 2, 3, 4, 5]])
					__snake_case    :							str   	=				model(_A)[0]

					__snake_case    :							str   	=				[1, 6, 7_68]
					self.assertEqual(output.shape			,						_A)

					__snake_case    :							Tuple   	=				tf.constant(
					    [
					        [
					            [-0.03_475_493, -0.4_686_034, -0.30_638_832],
					            [0.22_637_248, -0.26_988_646, -0.7_423_424],
					            [0.10_324_868, -0.45_013_508, -0.58_280_784],
					        ]
					    ])
					tf.debugging.assert_near(output[:, :3, :3]			,						_A			,						atol=1E-4)






"""simple docstring"""







from abc import ABC, abstractmethod
from argparse import ArgumentParser

class 	UpperCamelCase    (					lowercase		):



				@staticmethod
				@abstractmethod
				def       _lowercase  (_A       :   ArgumentParser)					->			Tuple:
					raise NotImplementedError()






				@abstractmethod
				def       _lowercase  (self       :   Any)					->			Optional[Any]:
					raise NotImplementedError()





