
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       ,   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       ,   )						->      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 )						->      List[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[int]:
				"""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_ )						->      Any:
				"""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_ )						->      List[str]:
				"""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_ )						->      Tuple:
				"""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_ )						->      Optional[Any]:
				"""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 )						->      str:
				"""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        lowercase      (	_SCREAMING_SNAKE_CASE						,     _SCREAMING_SNAKE_CASE						,     unittest.TestCase     ):
		__lowercase : Optional[Any]			  =       (
		    (
		        TFConvBertModel,
		        TFConvBertForMaskedLM,
		        TFConvBertForQuestionAnswering,
		        TFConvBertForSequenceClassification,
		        TFConvBertForTokenClassification,
		        TFConvBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		__lowercase : 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 : Tuple			  =       False
		__lowercase : Any			  =       False
		__lowercase : Optional[int]			  =       False



		def __UpperCamelCase				(				self )						->      Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase					=  TFConvBertModelTester(self )
				UpperCamelCase					=  ConfigTester(self       ,   config_class=A_       ,   hidden_size=37 )



		def __UpperCamelCase				(				self )						->      Optional[Any]:
				"""simple docstring"""
				self.config_tester.run_common_tests()



		def __UpperCamelCase				(				self )						->      Dict:
				"""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 )						->      str:
				"""simple docstring"""
				UpperCamelCase					=  self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*A_ )



		def __UpperCamelCase				(				self )						->      Any:
				"""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 )						->      int:
				"""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 )						->      Optional[int]:
				"""simple docstring"""
				UpperCamelCase					=  TFConvBertModel.from_pretrained('YituTech/conv-bert-base' )
				self.assertIsNotNone(A_ )



		def __UpperCamelCase				(				self )						->      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        lowercase      (	unittest.TestCase     ):



		@slow
		def __UpperCamelCase				(				self )						->      List[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.0347_5493, -0.468_6034, -0.3063_8832],
				            [0.2263_7248, -0.2698_8646, -0.742_3424],
				            [0.1032_4868, -0.4501_3508, -0.5828_0784],
				        ]
				    ] )
				tf.debugging.assert_near(output[:, :3, :3]       ,   A_       ,   atol=1e-4 )


from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_UpperCAmelCase      :				Tuple          = logging.get_logger(__name__)

_UpperCAmelCase      :				Union[str, Any]          = {
    "facebook/data2vec-text-base": "https://huggingface.co/data2vec/resolve/main/config.json",
}


class        lowercase      (	_SCREAMING_SNAKE_CASE     ):
		__lowercase : Dict			  =       "data2vec-text"



		def __init__(				self       ,   A_=30_522       ,   A_=768       ,   A_=12       ,   A_=12       ,   A_=3_072       ,   A_="gelu"       ,   A_=0.1       ,   A_=0.1       ,   A_=512       ,   A_=2       ,   A_=0.02       ,   A_=1e-12       ,   A_=1       ,   A_=0       ,   A_=2       ,   A_="absolute"       ,   A_=True       ,   A_=None       ,   **A_       ,   )						->      Any:
				"""simple docstring"""
				super().__init__(pad_token_id=A_       ,   bos_token_id=A_       ,   eos_token_id=A_       ,   **A_ )

				UpperCamelCase						=  vocab_size
				UpperCamelCase						=  hidden_size
				UpperCamelCase						=  num_hidden_layers
				UpperCamelCase						=  num_attention_heads
				UpperCamelCase						=  hidden_act
				UpperCamelCase						=  intermediate_size
				UpperCamelCase						=  hidden_dropout_prob
				UpperCamelCase						=  attention_probs_dropout_prob
				UpperCamelCase						=  max_position_embeddings
				UpperCamelCase						=  type_vocab_size
				UpperCamelCase						=  initializer_range
				UpperCamelCase						=  layer_norm_eps
				UpperCamelCase						=  position_embedding_type
				UpperCamelCase						=  use_cache
				UpperCamelCase						=  classifier_dropout






class        lowercase      (	_SCREAMING_SNAKE_CASE     ):



		@property
		def __UpperCamelCase				(				self )						->      Mapping[str, Mapping[int, str]]:
				"""simple docstring"""
				if self.task == "multiple-choice":
						UpperCamelCase						=  {0: 'batch', 1: 'choice', 2: 'sequence'}
				else:
						UpperCamelCase						=  {0: 'batch', 1: 'sequence'}
				return OrderedDict(
				    [
				        ('input_ids', dynamic_axis),
				        ('attention_mask', dynamic_axis),
				    ] )
