


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





							'''simple docstring'''






							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_=5_12   ,       lowerCamelCase_=16   ,       lowerCamelCase_=2   ,       lowerCamelCase_=0.02   ,       lowerCamelCase_=3   ,       lowerCamelCase_=4   ,       lowerCamelCase_=None   ,       )			->				Optional[int]:
														lowerCAmelCase__			  =      parent
														lowerCAmelCase__			  =      13
														lowerCAmelCase__			  =      7
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      99
														lowerCAmelCase__			  =      3_84
														lowerCAmelCase__			  =      2
														lowerCAmelCase__			  =      4
														lowerCAmelCase__			  =      37
														lowerCAmelCase__			  =      '''gelu'''
														lowerCAmelCase__			  =      0.1
														lowerCAmelCase__			  =      0.1
														lowerCAmelCase__			  =      5_12
														lowerCAmelCase__			  =      16
														lowerCAmelCase__			  =      2
														lowerCAmelCase__			  =      0.02
														lowerCAmelCase__			  =      3
														lowerCAmelCase__			  =      4
														lowerCAmelCase__			  =      1_28
														lowerCAmelCase__			  =      2
														lowerCAmelCase__			  =      9
														lowerCAmelCase__			  =      1
														lowerCAmelCase__			  =      None







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Any:
														lowerCAmelCase__			  =      ids_tensor([self.batch_size, self.seq_length]   ,       self.vocab_size  )

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

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

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

														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				int:
														lowerCAmelCase__			  =      TFConvBertModel(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

														lowerCAmelCase__			  =      [input_ids, input_mask]
														lowerCAmelCase__			  =      model(lowerCamelCase_  )

														lowerCAmelCase__			  =      model(lowerCamelCase_  )

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







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Optional[Any]:
														lowerCAmelCase__			  =      TFConvBertForMaskedLM(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.vocab_size)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Tuple:
														lowerCAmelCase__			  =      self.num_labels
														lowerCAmelCase__			  =      TFConvBertForSequenceClassification(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_labels)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.num_choices
														lowerCAmelCase__			  =      TFConvBertForMultipleChoice(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      tf.tile(tf.expand_dims(lowerCamelCase_   ,       1  )   ,       (1, self.num_choices, 1)  )
														lowerCAmelCase__			  =      tf.tile(tf.expand_dims(lowerCamelCase_   ,       1  )   ,       (1, self.num_choices, 1)  )
														lowerCAmelCase__			  =      tf.tile(tf.expand_dims(lowerCamelCase_   ,       1  )   ,       (1, self.num_choices, 1)  )
														lowerCAmelCase__			  =      {
														    '''input_ids''': multiple_choice_inputs_ids,
														    '''attention_mask''': multiple_choice_input_mask,
														    '''token_type_ids''': multiple_choice_token_type_ids,
														}
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.num_choices)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.num_labels
														lowerCAmelCase__			  =      TFConvBertForTokenClassification(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}
														lowerCAmelCase__			  =      model(lowerCamelCase_  )
														self.parent.assertEqual(result.logits.shape   ,       (self.batch_size, self.seq_length, self.num_labels)  )







							def     __SCREAMING_SNAKE_CASE				(						self   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_   ,       lowerCamelCase_  )			->				Any:
														lowerCAmelCase__			  =      TFConvBertForQuestionAnswering(config=lowerCamelCase_  )
														lowerCAmelCase__			  =      {
														    '''input_ids''': input_ids,
														    '''attention_mask''': input_mask,
														    '''token_type_ids''': token_type_ids,
														}

														lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.prepare_config_and_inputs()
														(
														    (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   (
														    lowerCAmelCase__
														)  ,   
														)			  =      config_and_inputs
														lowerCAmelCase__			  =      {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
														return config, inputs_dict




@require_tf
class 						a__							(				a__							,							a__							,							unittest.TestCase    ):





							'''simple docstring'''






							lowercase__     :  Tuple      		= (
							    (
							        TFConvBertModel,
							        TFConvBertForMaskedLM,
							        TFConvBertForQuestionAnswering,
							        TFConvBertForSequenceClassification,
							        TFConvBertForTokenClassification,
							        TFConvBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							lowercase__     :  int      		= (
							    {
							        "feature-extraction": TFConvBertModel,
							        "fill-mask": TFConvBertForMaskedLM,
							        "question-answering": TFConvBertForQuestionAnswering,
							        "text-classification": TFConvBertForSequenceClassification,
							        "token-classification": TFConvBertForTokenClassification,
							        "zero-shot": TFConvBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowercase__     :  Any      		= False
							lowercase__     :  Dict      		= False
							lowercase__     :  int      		= False







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      TFConvBertModelTester(self  )
														lowerCAmelCase__			  =      ConfigTester(self   ,       config_class=lowerCamelCase_   ,       hidden_size=37  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														self.config_tester.run_common_tests()







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				int:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_masked_lm(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[int]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_multiple_choice(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Optional[Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_question_answering(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_sequence_classification(*lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Dict:
														lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_token_classification(*lowerCamelCase_  )







							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				List[Any]:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      True

														if hasattr(lowerCamelCase_   ,       '''use_cache'''  ):
																					lowerCAmelCase__			  =      True

														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''encoder_seq_length'''   ,       self.model_tester.seq_length  )
														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''key_length'''   ,       lowerCamelCase_  )

														for model_class in self.all_model_classes:
																					lowerCAmelCase__			  =      self._prepare_for_class(lowerCamelCase_   ,       lowerCamelCase_  )
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					lowerCAmelCase__			  =      len(model(lowerCamelCase_  )  )

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

																												if self.is_encoder_decoder:
																																			lowerCAmelCase__			  =      outputs['''encoder_hidden_states''']
																																			lowerCAmelCase__			  =      outputs['''encoder_attentions''']
																												else:
																																			lowerCAmelCase__			  =      outputs['''hidden_states''']
																																			lowerCAmelCase__			  =      outputs['''attentions''']

																												self.assertEqual(len(lowerCamelCase_  )   ,       lowerCamelCase_  )

																												lowerCAmelCase__			  =      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     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__			  =      TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''  )
														self.assertIsNotNone(lowerCamelCase_  )







							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Tuple:
														lowerCAmelCase__  ,   lowerCAmelCase__			  =      self.model_tester.prepare_config_and_inputs_for_common()
														lowerCAmelCase__			  =      True
														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''decoder_seq_length'''   ,       self.model_tester.seq_length  )
														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''encoder_seq_length'''   ,       self.model_tester.seq_length  )
														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''key_length'''   ,       lowerCamelCase_  )
														lowerCAmelCase__			  =      getattr(self.model_tester   ,       '''key_length'''   ,       lowerCamelCase_  )

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

																					if self.is_encoder_decoder:
																												lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																												lowerCAmelCase__			  =      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"]
																					lowerCAmelCase__			  =      True
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					lowerCAmelCase__			  =      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
																					lowerCAmelCase__			  =      True
																					lowerCAmelCase__			  =      True
																					lowerCAmelCase__			  =      model_class(lowerCamelCase_  )
																					lowerCAmelCase__			  =      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 						a__							(				unittest.TestCase    ):





							'''simple docstring'''






							@slow
							def     __SCREAMING_SNAKE_CASE				(						self  )			->				Union[str, Any]:
														lowerCAmelCase__			  =      TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''  )
														lowerCAmelCase__			  =      tf.constant([[0, 1, 2, 3, 4, 5]]  )
														lowerCAmelCase__			  =      model(lowerCamelCase_  )[0]

														lowerCAmelCase__			  =      [1, 6, 7_68]
														self.assertEqual(output.shape   ,       lowerCamelCase_  )

														lowerCAmelCase__			  =      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]   ,       lowerCamelCase_   ,       atol=1e-4  )



'''simple docstring'''






import sys

from .dependency_versions_table import deps
from .utils.versions import require_version, require_version_core


# define which module versions we always want to check at run time
# (usually the ones defined in `install_requires` in setup.py)
#
# order specific notes:
# - tqdm must be checked before tokenizers

__UpperCAmelCase       							=				'''python tqdm regex requests packaging filelock numpy tokenizers'''.split()
if sys.version_info < (3, 7):
		pkgs_to_check_at_runtime.append('''dataclasses''')
if sys.version_info < (3, 8):
		pkgs_to_check_at_runtime.append('''importlib_metadata''')

for pkg in pkgs_to_check_at_runtime:
		if pkg in deps:
				if pkg == "tokenizers":
						# must be loaded here, or else tqdm check may fail
						from .utils import is_tokenizers_available

						if not is_tokenizers_available():
								continue  # not required, check version only if installed

				require_version_core(deps[pkg])
		else:
				raise ValueError(f"""can't find {pkg} in {deps.keys()}, check dependency_versions_table.py""")


def    _snake_case  (	A					,       A=None			)		->       Optional[Any]:
							require_version(deps[pkg]					,       A			)