


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


		'''simple docstring'''



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

		def 			__UpperCAmelCase       (  self      ):
				__a           =			ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size      )

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

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

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

				__a           =			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 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			TFConvBertModel(config=_a      )
				__a           =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

				__a           =			[input_ids, input_mask]
				__a           =			model(_a      )

				__a           =			model(_a      )

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

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

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			self.num_labels
				__a           =			TFConvBertForSequenceClassification(config=_a      )
				__a           =			{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}

				__a           =			model(_a      )
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)      )

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

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

		def 			__UpperCAmelCase       (  self							, _a							, _a							, _a							, _a							, _a							, _a							, _a      ):
				__a           =			TFConvBertForQuestionAnswering(config=_a      )
				__a           =			{
				    '''input_ids''': input_ids,
				    '''attention_mask''': input_mask,
				    '''token_type_ids''': token_type_ids,
				}

				__a           =			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 			__UpperCAmelCase       (  self      ):
				__a           =			self.prepare_config_and_inputs()
				(
				    (
				    __a
				)      ,     (
				    __a
				)      ,     (
				    __a
				)      ,     (
				    __a
				)      ,     (
				    __a
				)      ,     (
				    __a
				)      ,     (
				    __a
				)      ,     
				)           =			config_and_inputs
				__a           =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
				return config, inputs_dict







@require_tf
class 					__lowerCAmelCase					(   __SCREAMING_SNAKE_CASE     ,			__SCREAMING_SNAKE_CASE     ,			unittest.TestCase       ):


		'''simple docstring'''



		__UpperCAmelCase		:			int										=				(
		    (
		        TFConvBertModel,
		        TFConvBertForMaskedLM,
		        TFConvBertForQuestionAnswering,
		        TFConvBertForSequenceClassification,
		        TFConvBertForTokenClassification,
		        TFConvBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		__UpperCAmelCase		:			str										=				(
		    {
		        'feature-extraction': TFConvBertModel,
		        'fill-mask': TFConvBertForMaskedLM,
		        'question-answering': TFConvBertForQuestionAnswering,
		        'text-classification': TFConvBertForSequenceClassification,
		        'token-classification': TFConvBertForTokenClassification,
		        'zero-shot': TFConvBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		__UpperCAmelCase		:			str										=				False
		__UpperCAmelCase		:			str										=				False
		__UpperCAmelCase		:			Optional[Any]										=				False

		def 			__UpperCAmelCase       (  self      ):
				__a           =			TFConvBertModelTester(self      )
				__a           =			ConfigTester(self							, config_class=_a							, hidden_size=37      )

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

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*_a      )

		def 			__UpperCAmelCase       (  self      ):
				__a           =			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*_a      )

		@slow
		def 			__UpperCAmelCase       (  self      ):
				__a      ,     __a           =			self.model_tester.prepare_config_and_inputs_for_common()
				__a           =			True
				__a           =			True

				if hasattr(_a							, '''use_cache'''      ):
						__a           =			True

				__a           =			getattr(self.model_tester							, '''encoder_seq_length'''							, self.model_tester.seq_length      )
				__a           =			getattr(self.model_tester							, '''key_length'''							, _a      )

				for model_class in self.all_model_classes:
						__a           =			self._prepare_for_class(_a							, _a      )
						__a           =			model_class(_a      )
						__a           =			len(model(_a      )      )

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

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

								self.assertEqual(len(_a      )							, _a      )

								__a           =			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 			__UpperCAmelCase       (  self      ):
				__a           =			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''      )
				self.assertIsNotNone(_a      )





		def 			__UpperCAmelCase       (  self      ):
				__a      ,     __a           =			self.model_tester.prepare_config_and_inputs_for_common()
				__a           =			True
				__a           =			getattr(self.model_tester							, '''decoder_seq_length'''							, self.model_tester.seq_length      )
				__a           =			getattr(self.model_tester							, '''encoder_seq_length'''							, self.model_tester.seq_length      )
				__a           =			getattr(self.model_tester							, '''key_length'''							, _a      )
				__a           =			getattr(self.model_tester							, '''key_length'''							, _a      )

				def check_decoder_attentions_output(_a      ):
						__a           =			len(_a      )
						self.assertEqual(out_len % 2							, 0      )
						__a           =			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      ):
						__a           =			[
						    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:
						__a           =			True
						__a           =			False
						__a           =			model_class(_a      )
						__a           =			model(self._prepare_for_class(_a							, _a      )      )
						__a           =			len(_a      )
						self.assertEqual(config.output_hidden_states							, _a      )
						check_encoder_attentions_output(_a      )

						if self.is_encoder_decoder:
								__a           =			model_class(_a      )
								__a           =			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"]
						__a           =			True
						__a           =			model_class(_a      )
						__a           =			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
						__a           =			True
						__a           =			True
						__a           =			model_class(_a      )
						__a           =			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 					__lowerCAmelCase					(   unittest.TestCase       ):


		'''simple docstring'''



		@slow
		def 			__UpperCAmelCase       (  self      ):
				__a           =			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''      )
				__a           =			tf.constant([[0, 1, 2, 3, 4, 5]]      )
				__a           =			model(_a      )[0]

				__a           =			[1, 6, 768]
				self.assertEqual(output.shape							, _a      )

				__a           =			tf.constant(
				    [
				        [
				            [-0.0347_5493, -0.468_6034, -0.3063_8832],
				            [0.2263_7248, -0.2698_8646, -0.742_3424],
				            [0.1032_4868, -0.4501_3508, -0.5828_0784],
				        ]
				    ]      )
				tf.debugging.assert_near(output[:, :3, :3]							, _a							, atol=1E-4      )










"""simple docstring"""




def        lowercase							(		lowerCAmelCase__ :     list			) ->      bool:
		if not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
				raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
		if len(lowerCAmelCase__			) == 0:
				raise ValueError('''Input list must be a non empty list'''			)
		if len(lowerCAmelCase__			) == 1:
				return True
		__a           =			series[1] - series[0]
		for index in range(len(lowerCAmelCase__			) - 1			):
				if series[index + 1] - series[index] != common_diff:
						return False
		return True

def        lowercase							(		lowerCAmelCase__ :     list			) ->      float:
		if not isinstance(lowerCAmelCase__    ,					lowerCAmelCase__			):
				raise ValueError('''Input series is not valid, valid series - [2, 4, 6]'''			)
		if len(lowerCAmelCase__			) == 0:
				raise ValueError('''Input list must be a non empty list'''			)
		__a           =			0
		for val in series:
				answer += val
		return answer / len(lowerCAmelCase__			)


if __name__ == "__main__":
				import doctest

				doctest.testmod()






