



'''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 ,				_lowerCAmelCase ,				_lowerCAmelCase=13 ,				_lowerCAmelCase=7 ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=True ,				_lowerCAmelCase=99 ,				_lowerCAmelCase=32 ,				_lowerCAmelCase=2 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=37 ,				_lowerCAmelCase="gelu" ,				_lowerCAmelCase=0.1 ,				_lowerCAmelCase=0.1 ,				_lowerCAmelCase=512 ,				_lowerCAmelCase=16 ,				_lowerCAmelCase=2 ,				_lowerCAmelCase=0.02 ,				_lowerCAmelCase=3 ,				_lowerCAmelCase=4 ,				_lowerCAmelCase=None ,				)    ->    int:
							'''simple docstring'''






							lowercase        =							parent
							lowercase        =							13
							lowercase        =							7
							lowercase        =							True
							lowercase        =							True
							lowercase        =							True
							lowercase        =							True
							lowercase        =							99
							lowercase        =							384
							lowercase        =							2
							lowercase        =							4
							lowercase        =							37
							lowercase        =							"""gelu"""
							lowercase        =							0.1
							lowercase        =							0.1
							lowercase        =							512
							lowercase        =							16
							lowercase        =							2
							lowercase        =							0.02
							lowercase        =							3
							lowercase        =							4
							lowercase        =							128
							lowercase        =							2
							lowercase        =							9
							lowercase        =							1
							lowercase        =							None







	def   _a   (    self						)    ->    Any:
							'''simple docstring'''






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

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

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

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

							lowercase        =							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   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Optional[Any]:
							'''simple docstring'''






							lowercase        =							TFConvBertModel(config=_lowerCAmelCase						)
							lowercase        =							{"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

							lowercase        =							[input_ids, input_mask]
							lowercase        =							model(_lowerCAmelCase						)

							lowercase        =							model(_lowerCAmelCase						)

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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Dict:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							self.num_labels
							lowercase        =							TFConvBertForSequenceClassification(config=_lowerCAmelCase						)
							lowercase        =							{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}

							lowercase        =							model(_lowerCAmelCase						)
							self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)						)







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    int:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    Dict:
							'''simple docstring'''






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







	def   _a   (    self ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase ,				_lowerCAmelCase						)    ->    List[str]:
							'''simple docstring'''






							lowercase        =							TFConvBertForQuestionAnswering(config=_lowerCAmelCase						)
							lowercase        =							{
							    """input_ids""": input_ids,
							    """attention_mask""": input_mask,
							    """token_type_ids""": token_type_ids,
							}

							lowercase        =							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   _a   (    self						)    ->    Tuple:
							'''simple docstring'''






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




@require_tf
class      __UpperCamelCase       (_UpperCAmelCase			,   _UpperCAmelCase			,   unittest.TestCase			):
	__A			  =					(
	    (
	        TFConvBertModel,
	        TFConvBertForMaskedLM,
	        TFConvBertForQuestionAnswering,
	        TFConvBertForSequenceClassification,
	        TFConvBertForTokenClassification,
	        TFConvBertForMultipleChoice,
	    )
	    if is_tf_available()
	    else ()
	)
	__A			  =					(
	    {
	        '''feature-extraction''': TFConvBertModel,
	        '''fill-mask''': TFConvBertForMaskedLM,
	        '''question-answering''': TFConvBertForQuestionAnswering,
	        '''text-classification''': TFConvBertForSequenceClassification,
	        '''token-classification''': TFConvBertForTokenClassification,
	        '''zero-shot''': TFConvBertForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	__A			  =					False
	__A			  =					False
	__A			  =					False







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							lowercase        =							TFConvBertModelTester(self						)
							lowercase        =							ConfigTester(self ,				config_class=_lowerCAmelCase ,				hidden_size=37						)







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							self.config_tester.run_common_tests()







	def   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    Any:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    List[Any]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    int:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase						)







	def   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase						)







	@slow
	def   _a   (    self						)    ->    Optional[Any]:
							'''simple docstring'''






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

							if hasattr(_lowerCAmelCase ,				"""use_cache"""						):
													lowercase        =							True

							lowercase        =							getattr(self.model_tester ,				"""encoder_seq_length""" ,				self.model_tester.seq_length						)
							lowercase        =							getattr(self.model_tester ,				"""key_length""" ,				_lowerCAmelCase						)

							for model_class in self.all_model_classes:
													lowercase        =							self._prepare_for_class(_lowerCAmelCase ,				_lowerCAmelCase						)
													lowercase        =							model_class(_lowerCAmelCase						)
													lowercase        =							len(model(_lowerCAmelCase						)						)

													with tempfile.TemporaryDirectory() as tmpdirname:
																			model.save_pretrained(_lowerCAmelCase ,				saved_model=_lowerCAmelCase						)
																			lowercase        =							os.path.join(_lowerCAmelCase ,				"""saved_model""" ,				"""1"""						)
																			lowercase        =							tf.keras.models.load_model(_lowerCAmelCase						)
																			lowercase        =							model(_lowerCAmelCase						)

																			if self.is_encoder_decoder:
																									lowercase        =							outputs["""encoder_hidden_states"""]
																									lowercase        =							outputs["""encoder_attentions"""]
																			else:
																									lowercase        =							outputs["""hidden_states"""]
																									lowercase        =							outputs["""attentions"""]

																			self.assertEqual(len(_lowerCAmelCase						) ,				_lowerCAmelCase						)

																			lowercase        =							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   _a   (    self						)    ->    Dict:
							'''simple docstring'''






							lowercase        =							TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""						)
							self.assertIsNotNone(_lowerCAmelCase						)







	def   _a   (    self						)    ->    Optional[int]:
							'''simple docstring'''






							lowercase     ,   lowercase        =							self.model_tester.prepare_config_and_inputs_for_common()
							lowercase        =							True
							lowercase        =							getattr(self.model_tester ,				"""decoder_seq_length""" ,				self.model_tester.seq_length						)
							lowercase        =							getattr(self.model_tester ,				"""encoder_seq_length""" ,				self.model_tester.seq_length						)
							lowercase        =							getattr(self.model_tester ,				"""key_length""" ,				_lowerCAmelCase						)
							lowercase        =							getattr(self.model_tester ,				"""key_length""" ,				_lowerCAmelCase						)

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

													if self.is_encoder_decoder:
																			lowercase        =							model_class(_lowerCAmelCase						)
																			lowercase        =							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"]
													lowercase        =							True
													lowercase        =							model_class(_lowerCAmelCase						)
													lowercase        =							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
													lowercase        =							True
													lowercase        =							True
													lowercase        =							model_class(_lowerCAmelCase						)
													lowercase        =							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      __UpperCamelCase       (unittest.TestCase			):







	@slow
	def   _a   (    self						)    ->    Union[str, Any]:
							'''simple docstring'''






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

							lowercase        =							[1, 6, 768]
							self.assertEqual(output.shape ,				_lowerCAmelCase						)

							lowercase        =							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] ,				_lowerCAmelCase ,				atol=1E-4						)







'''simple docstring'''


from transformers import HfArgumentParser, TensorFlowBenchmark, TensorFlowBenchmarkArguments







def      SCREAMING_SNAKE_CASE				(				):
						lowercase        =							HfArgumentParser(lowercase_						)
						lowercase        =							parser.parse_args_into_dataclasses()[0]
						lowercase        =							TensorFlowBenchmark(args=lowercase_						)
						try:
												lowercase        =							parser.parse_args_into_dataclasses()[0]
						except ValueError as e:
												lowercase        =							"""Arg --no_{0} is no longer used, please use --no-{0} instead."""
												lowercase        =							""" """.join(str(lowercase_						).split(""" """						)[:-1]						)
												lowercase        =							""""""
												lowercase        =							eval(str(lowercase_						).split(""" """						)[-1]						)
												lowercase        =							[]
												for arg in depreciated_args:
																		# arg[2:] removes '--'
																		if arg[2:] in TensorFlowBenchmark.deprecated_args:
																								# arg[5:] removes '--no_'
																								full_error_msg += arg_error_msg.format(arg[5:]						)
																		else:
																								wrong_args.append(lowercase_						)
												if len(lowercase_						) > 0:
																		lowercase        =							full_error_msg + begin_error_msg + str(lowercase_						)
												raise ValueError(lowercase_						)
						benchmark.run()


if __name__ == "__main__":
						main()


