
'''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						,				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						,				)->		List[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     )->		str:







											'''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=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_     )->		Optional[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     TFConvBertModel(config=A_     )
											UpperCamelCase   			=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

											UpperCamelCase   			=     [input_ids, input_mask]
											UpperCamelCase   			=     model(A_     )

											UpperCamelCase   			=     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_     )->		str:







											'''simple docstring'''






											UpperCamelCase   			=     TFConvBertForMaskedLM(config=A_     )
											UpperCamelCase   			=     {
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}
											UpperCamelCase   			=     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_     )->		int:







											'''simple docstring'''






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

											UpperCamelCase   			=     model(A_     )
											self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_labels)     )







				def 						UpperCAmelCase_    (       self						,				A_						,				A_						,				A_						,				A_						,				A_						,				A_						,				A_     )->		Any:







											'''simple docstring'''






											UpperCamelCase   			=     self.num_choices
											UpperCamelCase   			=     TFConvBertForMultipleChoice(config=A_     )
											UpperCamelCase   			=     tf.tile(tf.expand_dims(A_						,				1     )						,				(1, self.num_choices, 1)     )
											UpperCamelCase   			=     tf.tile(tf.expand_dims(A_						,				1     )						,				(1, self.num_choices, 1)     )
											UpperCamelCase   			=     tf.tile(tf.expand_dims(A_						,				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(A_     )
											self.parent.assertEqual(result.logits.shape						,				(self.batch_size, self.num_choices)     )







				def 						UpperCAmelCase_    (       self						,				A_						,				A_						,				A_						,				A_						,				A_						,				A_						,				A_     )->		str:







											'''simple docstring'''






											UpperCamelCase   			=     self.num_labels
											UpperCamelCase   			=     TFConvBertForTokenClassification(config=A_     )
											UpperCamelCase   			=     {
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}
											UpperCamelCase   			=     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_     )->		List[str]:







											'''simple docstring'''






											UpperCamelCase   			=     TFConvBertForQuestionAnswering(config=A_     )
											UpperCamelCase   			=     {
											    'input_ids': input_ids,
											    'attention_mask': input_mask,
											    'token_type_ids': token_type_ids,
											}

											UpperCamelCase   			=     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     )->		List[Any]:







											'''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 			SCREAMING_SNAKE_CASE__      (     snake_case_							,       snake_case_							,       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 						UpperCAmelCase_    (       self     )->		str:







											'''simple docstring'''






											UpperCamelCase   			=     TFConvBertModelTester(self     )
											UpperCamelCase   			=     ConfigTester(self						,				config_class=A_						,				hidden_size=37     )







				def 						UpperCAmelCase_    (       self     )->		List[Any]:







											'''simple docstring'''






											self.config_tester.run_common_tests()







				def 						UpperCAmelCase_    (       self     )->		str:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*A_     )







				def 						UpperCAmelCase_    (       self     )->		Tuple:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_lm(*A_     )







				def 						UpperCAmelCase_    (       self     )->		List[Any]:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_multiple_choice(*A_     )







				def 						UpperCAmelCase_    (       self     )->		List[str]:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_question_answering(*A_     )







				def 						UpperCAmelCase_    (       self     )->		int:







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_sequence_classification(*A_     )







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







											'''simple docstring'''






											UpperCamelCase   			=     self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_token_classification(*A_     )







				@slow
				def 						UpperCAmelCase_    (       self     )->		str:







											'''simple docstring'''






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

											if hasattr(A_						,				'use_cache'     ):
																		UpperCamelCase   			=     True

											UpperCamelCase   			=     getattr(self.model_tester						,				'encoder_seq_length'						,				self.model_tester.seq_length     )
											UpperCamelCase   			=     getattr(self.model_tester						,				'key_length'						,				A_     )

											for model_class in self.all_model_classes:
																		UpperCamelCase   			=     self._prepare_for_class(A_						,				A_     )
																		UpperCamelCase   			=     model_class(A_     )
																		UpperCamelCase   			=     len(model(A_     )     )

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

																									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(A_     )						,				A_     )

																									UpperCamelCase   			=     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     )->		int:







											'''simple docstring'''






											UpperCamelCase   			=     TFConvBertModel.from_pretrained('YituTech/conv-bert-base'     )
											self.assertIsNotNone(A_     )







				def 						UpperCAmelCase_    (       self     )->		Optional[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'						,				A_     )
											UpperCamelCase   			=     getattr(self.model_tester						,				'key_length'						,				A_     )

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

																		if self.is_encoder_decoder:
																									UpperCamelCase   			=     model_class(A_     )
																									UpperCamelCase   			=     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"]
																		UpperCamelCase   			=     True
																		UpperCamelCase   			=     model_class(A_     )
																		UpperCamelCase   			=     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
																		UpperCamelCase   			=     True
																		UpperCamelCase   			=     True
																		UpperCamelCase   			=     model_class(A_     )
																		UpperCamelCase   			=     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 			SCREAMING_SNAKE_CASE__      (     unittest.TestCase):







				@slow
				def 						UpperCAmelCase_    (       self     )->		Union[str, Any]:







											'''simple docstring'''






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

											UpperCamelCase   			=     [1, 6, 768]
											self.assertEqual(output.shape						,				A_     )

											UpperCamelCase   			=     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 string import ascii_lowercase, ascii_uppercase







def 		A_(     A						:		str):
							if not sentence:
														return ""
							UpperCamelCase   			=     dict(zip(A		,     A))
							return lower_to_upper.get(sentence[0]		,     sentence[0]) + sentence[1:]


if __name__ == "__main__":
						from doctest import testmod

						testmod()




