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        lowercase_   :



							def __init__(						self				,    lowercase_				,    lowercase_=13				,    lowercase_=7				,    lowercase_=True				,    lowercase_=True				,    lowercase_=True				,    lowercase_=True				,    lowercase_=99				,    lowercase_=32				,    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_=None				,    ):
													_snake_case						:					Any    =   parent
													_snake_case						:					Tuple    =   13
													_snake_case						:					Optional[int]    =   7
													_snake_case						:					Optional[int]    =   True
													_snake_case						:					Any    =   True
													_snake_case						:					Dict    =   True
													_snake_case						:					Optional[int]    =   True
													_snake_case						:					int    =   99
													_snake_case						:					int    =   384
													_snake_case						:					Tuple    =   2
													_snake_case						:					int    =   4
													_snake_case						:					Tuple    =   37
													_snake_case						:					List[Any]    =   "gelu"
													_snake_case						:					Tuple    =   0.1
													_snake_case						:					Tuple    =   0.1
													_snake_case						:					Optional[Any]    =   512
													_snake_case						:					List[Any]    =   16
													_snake_case						:					Dict    =   2
													_snake_case						:					Union[str, Any]    =   0.02
													_snake_case						:					str    =   3
													_snake_case						:					Union[str, Any]    =   4
													_snake_case						:					Dict    =   128
													_snake_case						:					Any    =   2
													_snake_case						:					Optional[Any]    =   9
													_snake_case						:					Any    =   1
													_snake_case						:					int    =   None



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[str]    =   ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size    )

													_snake_case						:					Dict    =   None
													if self.use_input_mask:
																			_snake_case						:					int    =   random_attention_mask([self.batch_size, self.seq_length]    )

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

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

													_snake_case						:					int    =   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=lowercase_				,    )

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



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Optional[Any]    =   TFConvBertModel(config=lowercase_    )
													_snake_case						:					Union[str, Any]    =   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

													_snake_case						:					int    =   [input_ids, input_mask]
													_snake_case						:					Dict    =   model(lowercase_    )

													_snake_case						:					Tuple    =   model(lowercase_    )

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



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[str]    =   TFConvBertForMaskedLM(config=lowercase_    )
													_snake_case						:					List[Any]    =   {
													    "input_ids": input_ids,
													    "attention_mask": input_mask,
													    "token_type_ids": token_type_ids,
													}
													_snake_case						:					Optional[Any]    =   model(lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					Tuple    =   self.num_labels
													_snake_case						:					Tuple    =   TFConvBertForSequenceClassification(config=lowercase_    )
													_snake_case						:					Union[str, Any]    =   {
													    "input_ids": input_ids,
													    "attention_mask": input_mask,
													    "token_type_ids": token_type_ids,
													}

													_snake_case						:					Optional[Any]    =   model(lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)    )



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



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[str]    =   self.num_labels
													_snake_case						:					Dict    =   TFConvBertForTokenClassification(config=lowercase_    )
													_snake_case						:					Union[str, Any]    =   {
													    "input_ids": input_ids,
													    "attention_mask": input_mask,
													    "token_type_ids": token_type_ids,
													}
													_snake_case						:					Union[str, Any]    =   model(lowercase_    )
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)    )



							def 	UpperCamelCase	(						self				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_				,    lowercase_    ):
													_snake_case						:					List[Any]    =   TFConvBertForQuestionAnswering(config=lowercase_    )
													_snake_case						:					Optional[Any]    =   {
													    "input_ids": input_ids,
													    "attention_mask": input_mask,
													    "token_type_ids": token_type_ids,
													}

													_snake_case						:					List[str]    =   model(lowercase_    )
													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    ):
													_snake_case						:					List[str]    =   self.prepare_config_and_inputs()
													(
													    _snake_case
													)						:					Optional[int]    =   config_and_inputs
													_snake_case						:					int    =   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
													return config, inputs_dict




@require_tf
class        lowercase_   (					__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    ):
													_snake_case						:					Dict    =   TFConvBertModelTester(self    )
													_snake_case						:					List[str]    =   ConfigTester(self				,    config_class=lowercase_				,    hidden_size=37    )



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



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Dict    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					List[Any]    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Any    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Tuple    =   self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*lowercase_    )



							@slow
							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Union[str, Any]    =   self.model_tester.prepare_config_and_inputs_for_common()
													_snake_case						:					List[str]    =   True
													_snake_case						:					int    =   True

													if hasattr(lowercase_				,    "use_cache"    ):
																			_snake_case						:					int    =   True

													_snake_case						:					Union[str, Any]    =   getattr(self.model_tester				,    "encoder_seq_length"				,    self.model_tester.seq_length    )
													_snake_case						:					int    =   getattr(self.model_tester				,    "key_length"				,    lowercase_    )

													for model_class in self.all_model_classes:
																			_snake_case						:					int    =   self._prepare_for_class(lowercase_				,    lowercase_    )
																			_snake_case						:					List[Any]    =   model_class(lowercase_    )
																			_snake_case						:					str    =   len(model(lowercase_    )    )

																			with tempfile.TemporaryDirectory() as tmpdirname:
																									model.save_pretrained(lowercase_				,    saved_model=lowercase_    )
																									_snake_case						:					Any    =   os.path.join(lowercase_				,    "saved_model"				,    "1"    )
																									_snake_case						:					List[str]    =   tf.keras.models.load_model(lowercase_    )
																									_snake_case						:					Any    =   model(lowercase_    )

																									if self.is_encoder_decoder:
																															_snake_case						:					Union[str, Any]    =   outputs["encoder_hidden_states"]
																															_snake_case						:					Tuple    =   outputs["encoder_attentions"]
																									else:
																															_snake_case						:					List[str]    =   outputs["hidden_states"]
																															_snake_case						:					Any    =   outputs["attentions"]

																									self.assertEqual(len(lowercase_    )				,    lowercase_    )

																									_snake_case						:					Tuple    =   getattr(
																									    self.model_tester				,    "expected_num_hidden_layers"				,    self.model_tester.num_hidden_layers + 1    )

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

																									self.assertEqual(len(lowercase_    )				,    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    ):
													_snake_case						:					str    =   TFConvBertModel.from_pretrained("YituTech/conv-bert-base"    )
													self.assertIsNotNone(lowercase_    )



							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[int]    =   self.model_tester.prepare_config_and_inputs_for_common()
													_snake_case						:					Optional[Any]    =   True
													_snake_case						:					Dict    =   getattr(self.model_tester				,    "decoder_seq_length"				,    self.model_tester.seq_length    )
													_snake_case						:					Union[str, Any]    =   getattr(self.model_tester				,    "encoder_seq_length"				,    self.model_tester.seq_length    )
													_snake_case						:					str    =   getattr(self.model_tester				,    "key_length"				,    lowercase_    )
													_snake_case						:					Dict    =   getattr(self.model_tester				,    "key_length"				,    lowercase_    )

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

																			if self.is_encoder_decoder:
																									_snake_case						:					Any    =   model_class(lowercase_    )
																									_snake_case						:					Optional[Any]    =   model(self._prepare_for_class(lowercase_				,    lowercase_    )    )
																									self.assertEqual(config.output_hidden_states				,    lowercase_    )
																									check_decoder_attentions_output(lowercase_    )

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

																			# Check attention is always last and order is fine
																			_snake_case						:					Optional[Any]    =   True
																			_snake_case						:					List[str]    =   True
																			_snake_case						:					Tuple    =   model_class(lowercase_    )
																			_snake_case						:					str    =   model(self._prepare_for_class(lowercase_				,    lowercase_    )    )

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




@require_tf
class        lowercase_   (					unittest.TestCase		):



							@slow
							def 	UpperCamelCase	(						self    ):
													_snake_case						:					Optional[Any]    =   TFConvBertModel.from_pretrained("YituTech/conv-bert-base"    )
													_snake_case						:					Dict    =   tf.constant([[0, 1, 2, 3, 4, 5]]    )
													_snake_case						:					Union[str, Any]    =   model(lowercase_    )[0]

													_snake_case						:					str    =   [1, 6, 768]
													self.assertEqual(output.shape				,    lowercase_    )

													_snake_case						:					Dict    =   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]				,    lowercase_				,    atol=1e-4    )
def  snake_case						(__lowercase ,      __lowercase						)	->	str:

						'''simple docstring'''



						_snake_case						:					Tuple    =   ""
						for word_or_phrase in separated:
												if not isinstance(__lowercase ,      __lowercase						):
																		raise Exception("join() accepts only strings to be joined"						)
												joined += word_or_phrase + separator
						return joined.strip(__lowercase						)


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

				testmod()