
"""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						,    snake_case_						,    snake_case_=13						,    snake_case_=7						,    snake_case_=True						,    snake_case_=True						,    snake_case_=True						,    snake_case_=True						,    snake_case_=99						,    snake_case_=32						,    snake_case_=2						,    snake_case_=4						,    snake_case_=37						,    snake_case_="gelu"						,    snake_case_=0.1						,    snake_case_=0.1						,    snake_case_=5_12						,    snake_case_=16						,    snake_case_=2						,    snake_case_=0.02						,    snake_case_=3						,    snake_case_=4						,    snake_case_=None						,    ):
						_snake_case     :						str						=   parent
						_snake_case     :						List[Any]						=   13
						_snake_case     :						Any						=   7
						_snake_case     :						Union[str, Any]						=   True
						_snake_case     :						str						=   True
						_snake_case     :						Optional[int]						=   True
						_snake_case     :						str						=   True
						_snake_case     :						List[str]						=   99
						_snake_case     :						int						=   3_84
						_snake_case     :						Optional[int]						=   2
						_snake_case     :						Optional[Any]						=   4
						_snake_case     :						Optional[int]						=   37
						_snake_case     :						int						=   "gelu"
						_snake_case     :						Any						=   0.1
						_snake_case     :						Any						=   0.1
						_snake_case     :						Tuple						=   5_12
						_snake_case     :						Optional[int]						=   16
						_snake_case     :						Tuple						=   2
						_snake_case     :						Tuple						=   0.02
						_snake_case     :						str						=   3
						_snake_case     :						Any						=   4
						_snake_case     :						str						=   1_28
						_snake_case     :						Union[str, Any]						=   2
						_snake_case     :						Optional[int]						=   9
						_snake_case     :						List[str]						=   1
						_snake_case     :						List[str]						=   None
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						List[Any]						=   ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size     )

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

						_snake_case     :						Union[str, Any]						=   None
						if self.use_token_type_ids:
								_snake_case     :						Dict						=   ids_tensor([self.batch_size, self.seq_length]						,    self.type_vocab_size     )

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

						_snake_case     :						Optional[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=snake_case_						,    )

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						Dict						=   TFConvBertModel(config=snake_case_     )
						_snake_case     :						List[Any]						=   {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

						_snake_case     :						Union[str, Any]						=   [input_ids, input_mask]
						_snake_case     :						Dict						=   model(snake_case_     )

						_snake_case     :						Optional[Any]						=   model(snake_case_     )

						self.parent.assertEqual(result.last_hidden_state.shape						,    (self.batch_size, self.seq_length, self.hidden_size)     )
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						List[Any]						=   TFConvBertForMaskedLM(config=snake_case_     )
						_snake_case     :						str						=   {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}
						_snake_case     :						int						=   model(snake_case_     )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)     )
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						Optional[Any]						=   self.num_labels
						_snake_case     :						Any						=   TFConvBertForSequenceClassification(config=snake_case_     )
						_snake_case     :						int						=   {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}

						_snake_case     :						int						=   model(snake_case_     )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)     )
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						Dict						=   self.num_choices
						_snake_case     :						int						=   TFConvBertForMultipleChoice(config=snake_case_     )
						_snake_case     :						Any						=   tf.tile(tf.expand_dims(snake_case_						,    1     )						,    (1, self.num_choices, 1)     )
						_snake_case     :						int						=   tf.tile(tf.expand_dims(snake_case_						,    1     )						,    (1, self.num_choices, 1)     )
						_snake_case     :						List[str]						=   tf.tile(tf.expand_dims(snake_case_						,    1     )						,    (1, self.num_choices, 1)     )
						_snake_case     :						Union[str, 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(snake_case_     )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_choices)     )
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						Optional[Any]						=   self.num_labels
						_snake_case     :						Any						=   TFConvBertForTokenClassification(config=snake_case_     )
						_snake_case     :						List[str]						=   {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}
						_snake_case     :						List[str]						=   model(snake_case_     )
						self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.num_labels)     )
				def 					lowerCamelCase__	(    self						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_						,    snake_case_     ):
						_snake_case     :						List[str]						=   TFConvBertForQuestionAnswering(config=snake_case_     )
						_snake_case     :						Optional[Any]						=   {
						    "input_ids": input_ids,
						    "attention_mask": input_mask,
						    "token_type_ids": token_type_ids,
						}

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






@require_tf
class      _UpperCAmelCase				(					_snake_case		,					_snake_case		,					unittest.TestCase):
				__lowercase							: Optional[int]  =					(
				    (
				        TFConvBertModel,
				        TFConvBertForMaskedLM,
				        TFConvBertForQuestionAnswering,
				        TFConvBertForSequenceClassification,
				        TFConvBertForTokenClassification,
				        TFConvBertForMultipleChoice,
				    )
				    if is_tf_available()
				    else ()
				)
				__lowercase							: List[Any]  =					(
				    {
				        """feature-extraction""": TFConvBertModel,
				        """fill-mask""": TFConvBertForMaskedLM,
				        """question-answering""": TFConvBertForQuestionAnswering,
				        """text-classification""": TFConvBertForSequenceClassification,
				        """token-classification""": TFConvBertForTokenClassification,
				        """zero-shot""": TFConvBertForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__lowercase							: Optional[int]  =					False
				__lowercase							: Tuple  =					False
				__lowercase							: Optional[Any]  =					False
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						List[Any]						=   TFConvBertModelTester(self     )
						_snake_case     :						Dict						=   ConfigTester(self						,    config_class=snake_case_						,    hidden_size=37     )
				def 					lowerCamelCase__	(    self     ):
						self.config_tester.run_common_tests()
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Union[str, Any]						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*snake_case_     )
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Any						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*snake_case_     )
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Dict						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*snake_case_     )
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						List[str]						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*snake_case_     )
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Any						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*snake_case_     )
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Optional[int]						=   self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*snake_case_     )
				@slow
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						List[str]						=   self.model_tester.prepare_config_and_inputs_for_common()
						_snake_case     :						List[Any]						=   True
						_snake_case     :						Dict						=   True

						if hasattr(snake_case_						,    "use_cache"     ):
								_snake_case     :						Dict						=   True

						_snake_case     :						str						=   getattr(self.model_tester						,    "encoder_seq_length"						,    self.model_tester.seq_length     )
						_snake_case     :						List[Any]						=   getattr(self.model_tester						,    "key_length"						,    snake_case_     )

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

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

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

										self.assertEqual(len(snake_case_     )						,    snake_case_     )

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

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

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


				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						Optional[Any]						=   self.model_tester.prepare_config_and_inputs_for_common()
						_snake_case     :						Optional[Any]						=   True
						_snake_case     :						Tuple						=   getattr(self.model_tester						,    "decoder_seq_length"						,    self.model_tester.seq_length     )
						_snake_case     :						Any						=   getattr(self.model_tester						,    "encoder_seq_length"						,    self.model_tester.seq_length     )
						_snake_case     :						List[str]						=   getattr(self.model_tester						,    "key_length"						,    snake_case_     )
						_snake_case     :						List[str]						=   getattr(self.model_tester						,    "key_length"						,    snake_case_     )

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

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

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

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

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






@require_tf
class      _UpperCAmelCase				(					unittest.TestCase):
				@slow
				def 					lowerCamelCase__	(    self     ):
						_snake_case     :						str						=   TFConvBertModel.from_pretrained("YituTech/conv-bert-base"     )
						_snake_case     :						Optional[int]						=   tf.constant([[0, 1, 2, 3, 4, 5]]     )
						_snake_case     :						int						=   model(snake_case_     )[0]

						_snake_case     :						List[str]						=   [1, 6, 7_68]
						self.assertEqual(output.shape						,    snake_case_     )

						_snake_case     :						List[Any]						=   tf.constant(
						    [
						        [
						            [-0.03475493, -0.4686034, -0.30638832],
						            [0.22637248, -0.26988646, -0.7423424],
						            [0.10324868, -0.45013508, -0.58280784],
						        ]
						    ]     )
						tf.debugging.assert_near(output[:, :3, :3]						,    snake_case_						,    atol=1E-4     )



"""simple docstring"""
def 				a__   (   a   :				list       ,     a   :				int       ,     a   :				int = 0       ,     a   :				int = 0		):

  """simple docstring"""







  _snake_case     :						Optional[int]						=   right or len(a		) - 1
  if left > right:
    return -1
  elif list_data[left] == key:
    return left
  elif list_data[right] == key:
    return right
  else:
    return search(a       ,     a       ,     left + 1       ,     right - 1		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()

