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		:


				'''simple docstring'''






				def __init__(		self						,      lowercase						,      lowercase=1_3						,      lowercase=7						,      lowercase=True						,      lowercase=True						,      lowercase=True						,      lowercase=True						,      lowercase=9_9						,      lowercase=3_2						,      lowercase=2						,      lowercase=4						,      lowercase=3_7						,      lowercase="gelu"						,      lowercase=0.1						,      lowercase=0.1						,      lowercase=5_1_2						,      lowercase=1_6						,      lowercase=2						,      lowercase=0.02						,      lowercase=3						,      lowercase=4						,      lowercase=None						,      ):



						"""simple docstring"""





						A_						:							List[str]      = parent
						A_						:							str      = 1_3
						A_						:							int      = 7
						A_						:							Optional[Any]      = True
						A_						:							int      = True
						A_						:							Any      = True
						A_						:							Optional[Any]      = True
						A_						:							List[Any]      = 9_9
						A_						:							Optional[int]      = 3_8_4
						A_						:							Any      = 2
						A_						:							List[str]      = 4
						A_						:							Optional[Any]      = 3_7
						A_						:							Any      = 'gelu'
						A_						:							str      = 0.1
						A_						:							str      = 0.1
						A_						:							Optional[int]      = 5_1_2
						A_						:							str      = 1_6
						A_						:							Optional[Any]      = 2
						A_						:							Dict      = 0.02
						A_						:							List[Any]      = 3
						A_						:							Any      = 4
						A_						:							Union[str, Any]      = 1_2_8
						A_						:							List[str]      = 2
						A_						:							Optional[Any]      = 9
						A_						:							Optional[Any]      = 1
						A_						:							int      = None
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Dict      = ids_tensor([self.batch_size, self.seq_length]						,      self.vocab_size   )

						A_						:							Any      = None
						if self.use_input_mask:
								A_						:							List[Any]      = random_attention_mask([self.batch_size, self.seq_length]   )

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

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

						A_						:							Dict      = 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     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





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

						A_						:							int      = [input_ids, input_mask]
						A_						:							int      = model(lowercase   )

						A_						:							Optional[Any]      = model(lowercase   )

						self.parent.assertEqual(result.last_hidden_state.shape						,      (self.batch_size, self.seq_length, self.hidden_size)   )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





						A_						:							Optional[int]      = TFConvBertForMaskedLM(config=lowercase   )
						A_						:							int      = {
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						    'token_type_ids': token_type_ids,
						}
						A_						:							int      = model(lowercase   )
						self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.vocab_size)   )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





						A_						:							str      = self.num_labels
						A_						:							List[Any]      = TFConvBertForSequenceClassification(config=lowercase   )
						A_						:							List[Any]      = {
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						    'token_type_ids': token_type_ids,
						}

						A_						:							str      = model(lowercase   )
						self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_labels)   )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





						A_						:							Dict      = self.num_choices
						A_						:							Optional[Any]      = TFConvBertForMultipleChoice(config=lowercase   )
						A_						:							Optional[Any]      = tf.tile(tf.expand_dims(lowercase						,      1   )						,      (1, self.num_choices, 1)   )
						A_						:							Optional[int]      = tf.tile(tf.expand_dims(lowercase						,      1   )						,      (1, self.num_choices, 1)   )
						A_						:							Optional[Any]      = tf.tile(tf.expand_dims(lowercase						,      1   )						,      (1, self.num_choices, 1)   )
						A_						:							List[Any]      = {
						    'input_ids': multiple_choice_inputs_ids,
						    'attention_mask': multiple_choice_input_mask,
						    'token_type_ids': multiple_choice_token_type_ids,
						}
						A_						:							Optional[Any]      = model(lowercase   )
						self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.num_choices)   )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





						A_						:							str      = self.num_labels
						A_						:							Tuple      = TFConvBertForTokenClassification(config=lowercase   )
						A_						:							int      = {
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						    'token_type_ids': token_type_ids,
						}
						A_						:							int      = model(lowercase   )
						self.parent.assertEqual(result.logits.shape						,      (self.batch_size, self.seq_length, self.num_labels)   )
				def     lowerCAmelCase_  (		self						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase						,      lowercase   ):



						"""simple docstring"""





						A_						:							Tuple      = TFConvBertForQuestionAnswering(config=lowercase   )
						A_						:							List[str]      = {
						    'input_ids': input_ids,
						    'attention_mask': input_mask,
						    'token_type_ids': token_type_ids,
						}

						A_						:							List[Any]      = 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     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							str      = self.prepare_config_and_inputs()
						(
						    (
						    A_
						)  ,     (
						    A_
						)  ,     (
						    A_
						)  ,     (
						    A_
						)  ,     (
						    A_
						)  ,     (
						    A_
						)  ,     (
						    A_
						)  ,     
						)						:							Optional[int]      = config_and_inputs
						A_						:							List[Any]      = {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
						return config, inputs_dict





@require_tf
class UpperCAmelCase		(   __A   ,			__A   ,			unittest.TestCase    ):


				'''simple docstring'''






				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     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							str      = TFConvBertModelTester(self   )
						A_						:							Optional[int]      = ConfigTester(self						,      config_class=lowercase						,      hidden_size=3_7   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





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



						"""simple docstring"""





						A_						:							Union[str, Any]      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*lowercase   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Dict      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*lowercase   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Tuple      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*lowercase   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							List[str]      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*lowercase   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Dict      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*lowercase   )
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Union[str, Any]      = self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*lowercase   )
				@slow
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_  ,     A_						:							str      = self.model_tester.prepare_config_and_inputs_for_common()
						A_						:							Optional[int]      = True
						A_						:							Dict      = True

						if hasattr(lowercase						,      'use_cache'   ):
								A_						:							int      = True

						A_						:							List[Any]      = getattr(self.model_tester						,      'encoder_seq_length'						,      self.model_tester.seq_length   )
						A_						:							Optional[Any]      = getattr(self.model_tester						,      'key_length'						,      lowercase   )

						for model_class in self.all_model_classes:
								A_						:							int      = self._prepare_for_class(lowercase						,      lowercase   )
								A_						:							Optional[int]      = model_class(lowercase   )
								A_						:							Union[str, Any]      = len(model(lowercase   )   )

								with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(lowercase						,      saved_model=lowercase   )
										A_						:							int      = os.path.join(lowercase						,      'saved_model'						,      '1'   )
										A_						:							Union[str, Any]      = tf.keras.models.load_model(lowercase   )
										A_						:							List[Any]      = model(lowercase   )

										if self.is_encoder_decoder:
												A_						:							Optional[Any]      = outputs['encoder_hidden_states']
												A_						:							List[str]      = outputs['encoder_attentions']
										else:
												A_						:							List[str]      = outputs['hidden_states']
												A_						:							Dict      = outputs['attentions']

										self.assertEqual(len(lowercase   )						,      lowercase   )

										A_						:							List[str]      = 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     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





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







				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_  ,     A_						:							Union[str, Any]      = self.model_tester.prepare_config_and_inputs_for_common()
						A_						:							Dict      = True
						A_						:							Optional[int]      = getattr(self.model_tester						,      'decoder_seq_length'						,      self.model_tester.seq_length   )
						A_						:							int      = getattr(self.model_tester						,      'encoder_seq_length'						,      self.model_tester.seq_length   )
						A_						:							Any      = getattr(self.model_tester						,      'key_length'						,      lowercase   )
						A_						:							str      = getattr(self.model_tester						,      'key_length'						,      lowercase   )

						def check_decoder_attentions_output(lowercase   ):
								A_						:							str      = len(lowercase   )
								self.assertEqual(out_len % 2						,      0   )
								A_						:							Dict      = 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   ):
								A_						:							Optional[int]      = [
								    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:
								A_						:							Tuple      = True
								A_						:							Optional[int]      = False
								A_						:							Optional[Any]      = model_class(lowercase   )
								A_						:							Union[str, Any]      = model(self._prepare_for_class(lowercase						,      lowercase   )   )
								A_						:							List[Any]      = len(lowercase   )
								self.assertEqual(config.output_hidden_states						,      lowercase   )
								check_encoder_attentions_output(lowercase   )

								if self.is_encoder_decoder:
										A_						:							Any      = model_class(lowercase   )
										A_						:							Tuple      = 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"]
								A_						:							Union[str, Any]      = True
								A_						:							Union[str, Any]      = model_class(lowercase   )
								A_						:							List[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
								A_						:							List[str]      = True
								A_						:							List[Any]      = True
								A_						:							List[str]      = model_class(lowercase   )
								A_						:							int      = 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 UpperCAmelCase		(   unittest.TestCase    ):


				'''simple docstring'''






				@slow
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						A_						:							Tuple      = TFConvBertModel.from_pretrained('YituTech/conv-bert-base'   )
						A_						:							str      = tf.constant([[0, 1, 2, 3, 4, 5]]   )
						A_						:							Optional[Any]      = model(lowercase   )[0]

						A_						:							Optional[Any]      = [1, 6, 7_6_8]
						self.assertEqual(output.shape						,      lowercase   )

						A_						:							Optional[int]      = 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]						,      lowercase						,      atol=1E-4   )







from collections import OrderedDict
from typing import Mapping

from packaging import version

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


_UpperCAmelCase          =						logging.get_logger(__name__)

_UpperCAmelCase          =						{
    """facebook/deit-base-distilled-patch16-224""": (
        """https://huggingface.co/facebook/deit-base-patch16-224/resolve/main/config.json"""
    ),
    # See all DeiT models at https://huggingface.co/models?filter=deit
}





class UpperCAmelCase		(   __A    ):


				'''simple docstring'''






				lowerCamelCase_        =   '''deit'''
				def __init__(		self						,      lowercase=7_6_8						,      lowercase=1_2						,      lowercase=1_2						,      lowercase=3_0_7_2						,      lowercase="gelu"						,      lowercase=0.0						,      lowercase=0.0						,      lowercase=0.02						,      lowercase=1E-12						,      lowercase=2_2_4						,      lowercase=1_6						,      lowercase=3						,      lowercase=True						,      lowercase=1_6						,      **lowercase						,      ):



						"""simple docstring"""





						super().__init__(**lowercase   )

						A_						:							Dict      = hidden_size
						A_						:							List[Any]      = num_hidden_layers
						A_						:							Optional[int]      = num_attention_heads
						A_						:							List[str]      = intermediate_size
						A_						:							int      = hidden_act
						A_						:							Optional[int]      = hidden_dropout_prob
						A_						:							str      = attention_probs_dropout_prob
						A_						:							List[str]      = initializer_range
						A_						:							List[Any]      = layer_norm_eps
						A_						:							List[str]      = image_size
						A_						:							str      = patch_size
						A_						:							str      = num_channels
						A_						:							Dict      = qkv_bias
						A_						:							Optional[Any]      = encoder_stride





class UpperCAmelCase		(   __A    ):


				'''simple docstring'''






				lowerCamelCase_        =   version.parse('''1.11'''    )
				@property
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						return OrderedDict(
						    [
						        ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
						    ]   )







				@property
				def     lowerCAmelCase_  (		self   ):



						"""simple docstring"""





						return 1E-4






