

'''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 		__SCREAMING_SNAKE_CASE       :







				def __init__(					self    :		int		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		str=13		,				UpperCAmelCase__    :		Tuple=7		,				UpperCAmelCase__    :		List[str]=True		,				UpperCAmelCase__    :		List[Any]=True		,				UpperCAmelCase__    :		Dict=True		,				UpperCAmelCase__    :		Any=True		,				UpperCAmelCase__    :		int=99		,				UpperCAmelCase__    :		Dict=32		,				UpperCAmelCase__    :		Tuple=2		,				UpperCAmelCase__    :		Tuple=4		,				UpperCAmelCase__    :		Optional[int]=37		,				UpperCAmelCase__    :		Optional[int]="gelu"		,				UpperCAmelCase__    :		List[str]=0.1		,				UpperCAmelCase__    :		Union[str, Any]=0.1		,				UpperCAmelCase__    :		List[str]=512		,				UpperCAmelCase__    :		Tuple=16		,				UpperCAmelCase__    :		Tuple=2		,				UpperCAmelCase__    :		Dict=0.02		,				UpperCAmelCase__    :		Optional[int]=3		,				UpperCAmelCase__    :		List[Any]=4		,				UpperCAmelCase__    :		Union[str, Any]=None		,				):
										'''simple docstring'''
										lowercase   :       Any								=parent
										lowercase   :       Tuple								=13
										lowercase   :       List[Any]								=7
										lowercase   :       Any								=True
										lowercase   :       Optional[int]								=True
										lowercase   :       Optional[Any]								=True
										lowercase   :       Optional[Any]								=True
										lowercase   :       int								=99
										lowercase   :       Dict								=384
										lowercase   :       List[str]								=2
										lowercase   :       Any								=4
										lowercase   :       int								=37
										lowercase   :       int								='''gelu'''
										lowercase   :       List[Any]								=0.1
										lowercase   :       Any								=0.1
										lowercase   :       Dict								=512
										lowercase   :       List[Any]								=16
										lowercase   :       Optional[int]								=2
										lowercase   :       Optional[Any]								=0.02
										lowercase   :       Dict								=3
										lowercase   :       List[str]								=4
										lowercase   :       int								=128
										lowercase   :       List[str]								=2
										lowercase   :       int								=9
										lowercase   :       Tuple								=1
										lowercase   :       str								=None







				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       str								=ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size      )

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

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

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

										lowercase   :       Dict								=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=UpperCAmelCase__		,				)

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







				def    lowerCamelCase_  (					self    :		Union[str, Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Any      ):
										'''simple docstring'''
										lowercase   :       Tuple								=TFConvBertModel(config=UpperCAmelCase__      )
										lowercase   :       List[Any]								={'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

										lowercase   :       Dict								=[input_ids, input_mask]
										lowercase   :       Tuple								=model(UpperCAmelCase__      )

										lowercase   :       Dict								=model(UpperCAmelCase__      )

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







				def    lowerCamelCase_  (					self    :		str		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Any		,				UpperCAmelCase__    :		Optional[Any]		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       Dict								=TFConvBertForMaskedLM(config=UpperCAmelCase__      )
										lowercase   :       str								={
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										}
										lowercase   :       int								=model(UpperCAmelCase__      )
										self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.vocab_size)      )







				def    lowerCamelCase_  (					self    :		Dict		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Optional[int]		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Any      ):
										'''simple docstring'''
										lowercase   :       str								=self.num_labels
										lowercase   :       Optional[Any]								=TFConvBertForSequenceClassification(config=UpperCAmelCase__      )
										lowercase   :       Optional[int]								={
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										}

										lowercase   :       Optional[int]								=model(UpperCAmelCase__      )
										self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels)      )







				def    lowerCamelCase_  (					self    :		Optional[int]		,				UpperCAmelCase__    :		str		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Optional[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		str      ):
										'''simple docstring'''
										lowercase   :       Optional[Any]								=self.num_choices
										lowercase   :       List[Any]								=TFConvBertForMultipleChoice(config=UpperCAmelCase__      )
										lowercase   :       Optional[int]								=tf.tile(tf.expand_dims(UpperCAmelCase__		,				1      )		,				(1, self.num_choices, 1)      )
										lowercase   :       List[Any]								=tf.tile(tf.expand_dims(UpperCAmelCase__		,				1      )		,				(1, self.num_choices, 1)      )
										lowercase   :       Optional[Any]								=tf.tile(tf.expand_dims(UpperCAmelCase__		,				1      )		,				(1, self.num_choices, 1)      )
										lowercase   :       str								={
										    '''input_ids''': multiple_choice_inputs_ids,
										    '''attention_mask''': multiple_choice_input_mask,
										    '''token_type_ids''': multiple_choice_token_type_ids,
										}
										lowercase   :       List[str]								=model(UpperCAmelCase__      )
										self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_choices)      )







				def    lowerCamelCase_  (					self    :		int		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Optional[Any]		,				UpperCAmelCase__    :		List[str]		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		Dict      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=self.num_labels
										lowercase   :       Optional[Any]								=TFConvBertForTokenClassification(config=UpperCAmelCase__      )
										lowercase   :       Optional[Any]								={
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										}
										lowercase   :       Optional[Any]								=model(UpperCAmelCase__      )
										self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.num_labels)      )







				def    lowerCamelCase_  (					self    :		List[str]		,				UpperCAmelCase__    :		int		,				UpperCAmelCase__    :		Optional[Any]		,				UpperCAmelCase__    :		Dict		,				UpperCAmelCase__    :		Union[str, Any]		,				UpperCAmelCase__    :		List[Any]		,				UpperCAmelCase__    :		Tuple		,				UpperCAmelCase__    :		int      ):
										'''simple docstring'''
										lowercase   :       int								=TFConvBertForQuestionAnswering(config=UpperCAmelCase__      )
										lowercase   :       Any								={
										    '''input_ids''': input_ids,
										    '''attention_mask''': input_mask,
										    '''token_type_ids''': token_type_ids,
										}

										lowercase   :       Any								=model(UpperCAmelCase__      )
										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    :		Optional[int]      ):
										'''simple docstring'''
										lowercase   :       Any								=self.prepare_config_and_inputs()
										(
										    (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  (
										    lowercase
										)		,  
										)   :       str								=config_and_inputs
										lowercase   :       Dict								={'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict





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







				def    lowerCamelCase_  (					self    :		str      ):
										'''simple docstring'''
										lowercase   :       Tuple								=TFConvBertModelTester(self      )
										lowercase   :       Any								=ConfigTester(self		,				config_class=UpperCAmelCase__		,				hidden_size=37      )







				def    lowerCamelCase_  (					self    :		Union[str, Any]      ):
										'''simple docstring'''
										self.config_tester.run_common_tests()







				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       Dict								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       Optional[Any]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		List[str]      ):
										'''simple docstring'''
										lowercase   :       Optional[Any]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_multiple_choice(*UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       List[str]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Any      ):
										'''simple docstring'''
										lowercase   :       Optional[Any]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Dict      ):
										'''simple docstring'''
										lowercase   :       Optional[int]								=self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__      )







				@slow
				def    lowerCamelCase_  (					self    :		Tuple      ):
										'''simple docstring'''
										lowercase		,  lowercase   :       Tuple								=self.model_tester.prepare_config_and_inputs_for_common()
										lowercase   :       List[str]								=True
										lowercase   :       str								=True

										if hasattr(UpperCAmelCase__		,				'''use_cache'''      ):
																lowercase   :       List[Any]								=True

										lowercase   :       Tuple								=getattr(self.model_tester		,				'''encoder_seq_length'''		,				self.model_tester.seq_length      )
										lowercase   :       Dict								=getattr(self.model_tester		,				'''key_length'''		,				UpperCAmelCase__      )

										for model_class in self.all_model_classes:
																lowercase   :       Tuple								=self._prepare_for_class(UpperCAmelCase__		,				UpperCAmelCase__      )
																lowercase   :       List[str]								=model_class(UpperCAmelCase__      )
																lowercase   :       int								=len(model(UpperCAmelCase__      )      )

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

																						if self.is_encoder_decoder:
																												lowercase   :       List[Any]								=outputs['''encoder_hidden_states''']
																												lowercase   :       Optional[Any]								=outputs['''encoder_attentions''']
																						else:
																												lowercase   :       Dict								=outputs['''hidden_states''']
																												lowercase   :       Union[str, Any]								=outputs['''attentions''']

																						self.assertEqual(len(UpperCAmelCase__      )		,				UpperCAmelCase__      )

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

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

																						self.assertEqual(len(UpperCAmelCase__      )		,				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    :		Any      ):
										'''simple docstring'''
										lowercase   :       Union[str, Any]								=TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''      )
										self.assertIsNotNone(UpperCAmelCase__      )







				def    lowerCamelCase_  (					self    :		Optional[int]      ):
										'''simple docstring'''
										lowercase		,  lowercase   :       List[str]								=self.model_tester.prepare_config_and_inputs_for_common()
										lowercase   :       Dict								=True
										lowercase   :       Optional[int]								=getattr(self.model_tester		,				'''decoder_seq_length'''		,				self.model_tester.seq_length      )
										lowercase   :       Optional[Any]								=getattr(self.model_tester		,				'''encoder_seq_length'''		,				self.model_tester.seq_length      )
										lowercase   :       Dict								=getattr(self.model_tester		,				'''key_length'''		,				UpperCAmelCase__      )
										lowercase   :       List[Any]								=getattr(self.model_tester		,				'''key_length'''		,				UpperCAmelCase__      )

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

																if self.is_encoder_decoder:
																						lowercase   :       Tuple								=model_class(UpperCAmelCase__      )
																						lowercase   :       Union[str, Any]								=model(self._prepare_for_class(UpperCAmelCase__		,				UpperCAmelCase__      )      )
																						self.assertEqual(config.output_hidden_states		,				UpperCAmelCase__      )
																						check_decoder_attentions_output(UpperCAmelCase__      )

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

																# Check attention is always last and order is fine
																lowercase   :       Any								=True
																lowercase   :       Dict								=True
																lowercase   :       Optional[Any]								=model_class(UpperCAmelCase__      )
																lowercase   :       Any								=model(self._prepare_for_class(UpperCAmelCase__		,				UpperCAmelCase__      )      )

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





@require_tf
class 		__SCREAMING_SNAKE_CASE       ( unittest.TestCase			):







				@slow
				def    lowerCamelCase_  (					self    :		Optional[Any]      ):
										'''simple docstring'''
										lowercase   :       List[str]								=TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''      )
										lowercase   :       Optional[Any]								=tf.constant([[0, 1, 2, 3, 4, 5]]      )
										lowercase   :       List[str]								=model(UpperCAmelCase__      )[0]

										lowercase   :       Any								=[1, 6, 768]
										self.assertEqual(output.shape		,				UpperCAmelCase__      )

										lowercase   :       int								=tf.constant(
										    [
										        [
										            [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
										            [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
										            [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
										        ]
										    ]      )
										tf.debugging.assert_near(output[:, :3, :3]		,				UpperCAmelCase__		,				atol=1E-4      )








'''simple docstring'''





from datetime import datetime

import matplotlib.pyplot as plt
import torch






def 							_lowerCAmelCase (      __magic_name__	:    Dict	)							->     Dict:
						for param in module.parameters():
												lowercase   :       List[str]								=False






def 							_lowerCAmelCase (      )							->     List[str]:
						lowercase   :       Dict								='''cuda''' if torch.cuda.is_available() else '''cpu'''
						if torch.backends.mps.is_available() and torch.backends.mps.is_built():
												lowercase   :       Optional[int]								='''mps'''
						if device == "mps":
												print(
												    '''WARNING: MPS currently doesn\'t seem to work, and messes up backpropagation without any visible torch'''
												    ''' errors. I recommend using CUDA on a colab notebook or CPU instead if you\'re facing inexplicable issues'''
												    ''' with generations.'''	)
						return device






def 							_lowerCAmelCase (      __magic_name__	:    Union[str, Any]	)							->     str:
						lowercase   :       Optional[int]								=plt.imshow(__magic_name__	)
						fig.axes.get_xaxis().set_visible(__magic_name__	)
						fig.axes.get_yaxis().set_visible(__magic_name__	)
						plt.show()







def 							_lowerCAmelCase (      )							->     List[Any]:
						lowercase   :       Any								=datetime.now()
						lowercase   :       Dict								=current_time.strftime('''%H:%M:%S'''	)
						return timestamp





