



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







							def __init__(				self   :			Optional[Any]      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			List[Any]=13      ,_UpperCAmelCase   :			Dict=7      ,_UpperCAmelCase   :			Tuple=True      ,_UpperCAmelCase   :			Union[str, Any]=True      ,_UpperCAmelCase   :			List[str]=True      ,_UpperCAmelCase   :			List[str]=True      ,_UpperCAmelCase   :			Tuple=99      ,_UpperCAmelCase   :			Union[str, Any]=32      ,_UpperCAmelCase   :			str=2      ,_UpperCAmelCase   :			List[str]=4      ,_UpperCAmelCase   :			Any=37      ,_UpperCAmelCase   :			Optional[Any]="gelu"      ,_UpperCAmelCase   :			Union[str, Any]=0.1      ,_UpperCAmelCase   :			Union[str, Any]=0.1      ,_UpperCAmelCase   :			Union[str, Any]=512      ,_UpperCAmelCase   :			int=16      ,_UpperCAmelCase   :			Union[str, Any]=2      ,_UpperCAmelCase   :			int=0.02      ,_UpperCAmelCase   :			List[Any]=3      ,_UpperCAmelCase   :			str=4      ,_UpperCAmelCase   :			Optional[Any]=None      ,):
									_a	: List[str]								=				parent
									_a	: List[Any]								=				13
									_a	: Tuple								=				7
									_a	: List[str]								=				True
									_a	: Tuple								=				True
									_a	: Optional[Any]								=				True
									_a	: List[Any]								=				True
									_a	: int								=				99
									_a	: Tuple								=				384
									_a	: str								=				2
									_a	: Optional[int]								=				4
									_a	: List[Any]								=				37
									_a	: str								=				'gelu'
									_a	: Tuple								=				0.1
									_a	: Union[str, Any]								=				0.1
									_a	: List[Any]								=				512
									_a	: Tuple								=				16
									_a	: int								=				2
									_a	: List[Any]								=				0.02
									_a	: Dict								=				3
									_a	: str								=				4
									_a	: List[Any]								=				128
									_a	: int								=				2
									_a	: Tuple								=				9
									_a	: Union[str, Any]								=				1
									_a	: str								=				None







							def 				__lowercase     (				self   :			Any						):
									_a	: List[Any]								=				ids_tensor([self.batch_size, self.seq_length]      ,self.vocab_size						)

									_a	: Tuple								=				None
									if self.use_input_mask:
											_a	: Union[str, Any]								=				random_attention_mask([self.batch_size, self.seq_length]						)

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

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

									_a	: 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=_UpperCAmelCase      ,)

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







							def 				__lowercase     (				self   :			int      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			Optional[Any]      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			List[Any]						):
									_a	: Dict								=				TFConvBertModel(config=_UpperCAmelCase						)
									_a	: int								=				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

									_a	: List[Any]								=				[input_ids, input_mask]
									_a	: int								=				model(_UpperCAmelCase						)

									_a	: int								=				model(_UpperCAmelCase						)

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







							def 				__lowercase     (				self   :			str      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			List[str]						):
									_a	: Optional[Any]								=				TFConvBertForMaskedLM(config=_UpperCAmelCase						)
									_a	: Optional[Any]								=				{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}
									_a	: List[str]								=				model(_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.vocab_size)						)







							def 				__lowercase     (				self   :			List[str]      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			List[Any]						):
									_a	: Tuple								=				self.num_labels
									_a	: int								=				TFConvBertForSequenceClassification(config=_UpperCAmelCase						)
									_a	: List[Any]								=				{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}

									_a	: Any								=				model(_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.num_labels)						)







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







							def 				__lowercase     (				self   :			int      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Union[str, Any]      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			List[Any]      ,_UpperCAmelCase   :			Dict						):
									_a	: str								=				self.num_labels
									_a	: Optional[int]								=				TFConvBertForTokenClassification(config=_UpperCAmelCase						)
									_a	: Optional[Any]								=				{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}
									_a	: Tuple								=				model(_UpperCAmelCase						)
									self.parent.assertEqual(result.logits.shape      ,(self.batch_size, self.seq_length, self.num_labels)						)







							def 				__lowercase     (				self   :			List[str]      ,_UpperCAmelCase   :			str      ,_UpperCAmelCase   :			List[str]      ,_UpperCAmelCase   :			Tuple      ,_UpperCAmelCase   :			Any      ,_UpperCAmelCase   :			int      ,_UpperCAmelCase   :			Optional[int]      ,_UpperCAmelCase   :			str						):
									_a	: Union[str, Any]								=				TFConvBertForQuestionAnswering(config=_UpperCAmelCase						)
									_a	: List[Any]								=				{
									    'input_ids': input_ids,
									    'attention_mask': input_mask,
									    'token_type_ids': token_type_ids,
									}

									_a	: List[str]								=				model(_UpperCAmelCase						)
									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 				__lowercase     (				self   :			Dict						):
									_a	: Optional[Any]								=				self.prepare_config_and_inputs()
									(
									    (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   (
									    _a
									)			,   
									)	: Dict								=				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       __magic_name__		(       _UpperCamelCase     ,		_UpperCamelCase     ,		unittest.TestCase  ):
							lowerCAmelCase			:       str  							=   (
							    (
							        TFConvBertModel,
							        TFConvBertForMaskedLM,
							        TFConvBertForQuestionAnswering,
							        TFConvBertForSequenceClassification,
							        TFConvBertForTokenClassification,
							        TFConvBertForMultipleChoice,
							    )
							    if is_tf_available()
							    else ()
							)
							lowerCAmelCase			:       Optional[Any]  							=   (
							    {
							        'feature-extraction': TFConvBertModel,
							        'fill-mask': TFConvBertForMaskedLM,
							        'question-answering': TFConvBertForQuestionAnswering,
							        'text-classification': TFConvBertForSequenceClassification,
							        'token-classification': TFConvBertForTokenClassification,
							        'zero-shot': TFConvBertForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							lowerCAmelCase			:       int  							=   False
							lowerCAmelCase			:       int  							=   False
							lowerCAmelCase			:       Tuple  							=   False







							def 				__lowercase     (				self   :			Dict						):
									_a	: List[Any]								=				TFConvBertModelTester(self						)
									_a	: str								=				ConfigTester(self      ,config_class=_UpperCAmelCase      ,hidden_size=37						)







							def 				__lowercase     (				self   :			str						):
									self.config_tester.run_common_tests()







							def 				__lowercase     (				self   :			Union[str, Any]						):
									_a	: Optional[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a	: Optional[int]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Any						):
									_a	: Optional[Any]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			List[str]						):
									_a	: Dict								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a	: List[str]								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase						)







							def 				__lowercase     (				self   :			List[str]						):
									_a	: Dict								=				self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase						)







							@slow
							def 				__lowercase     (				self   :			int						):
									_a			,   _a	: Union[str, Any]								=				self.model_tester.prepare_config_and_inputs_for_common()
									_a	: Optional[int]								=				True
									_a	: Optional[Any]								=				True

									if hasattr(_UpperCAmelCase      ,'use_cache'						):
											_a	: Optional[int]								=				True

									_a	: Union[str, Any]								=				getattr(self.model_tester      ,'encoder_seq_length'      ,self.model_tester.seq_length						)
									_a	: Optional[Any]								=				getattr(self.model_tester      ,'key_length'      ,_UpperCAmelCase						)

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

											with tempfile.TemporaryDirectory() as tmpdirname:
													model.save_pretrained(_UpperCAmelCase      ,saved_model=_UpperCAmelCase						)
													_a	: Dict								=				os.path.join(_UpperCAmelCase      ,'saved_model'      ,'1'						)
													_a	: Any								=				tf.keras.models.load_model(_UpperCAmelCase						)
													_a	: str								=				model(_UpperCAmelCase						)

													if self.is_encoder_decoder:
															_a	: List[Any]								=				outputs['encoder_hidden_states']
															_a	: Union[str, Any]								=				outputs['encoder_attentions']
													else:
															_a	: List[Any]								=				outputs['hidden_states']
															_a	: str								=				outputs['attentions']

													self.assertEqual(len(_UpperCAmelCase						)      ,_UpperCAmelCase						)

													_a	: List[Any]								=				getattr(
													    self.model_tester      ,'expected_num_hidden_layers'      ,self.model_tester.num_hidden_layers + 1						)

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

													self.assertEqual(len(_UpperCAmelCase						)      ,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 				__lowercase     (				self   :			int						):
									_a	: Any								=				TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
									self.assertIsNotNone(_UpperCAmelCase						)







							def 				__lowercase     (				self   :			Tuple						):
									_a			,   _a	: Any								=				self.model_tester.prepare_config_and_inputs_for_common()
									_a	: List[str]								=				True
									_a	: Union[str, Any]								=				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	: List[Any]								=				getattr(self.model_tester      ,'key_length'      ,_UpperCAmelCase						)
									_a	: int								=				getattr(self.model_tester      ,'key_length'      ,_UpperCAmelCase						)

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

											if self.is_encoder_decoder:
													_a	: List[str]								=				model_class(_UpperCAmelCase						)
													_a	: Union[str, Any]								=				model(self._prepare_for_class(_UpperCAmelCase      ,_UpperCAmelCase						)						)
													self.assertEqual(config.output_hidden_states      ,_UpperCAmelCase						)
													check_decoder_attentions_output(_UpperCAmelCase						)

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

											# Check attention is always last and order is fine
											_a	: Union[str, Any]								=				True
											_a	: int								=				True
											_a	: Optional[Any]								=				model_class(_UpperCAmelCase						)
											_a	: int								=				model(self._prepare_for_class(_UpperCAmelCase      ,_UpperCAmelCase						)						)

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

@require_tf
class       __magic_name__		(       unittest.TestCase  ):







							@slow
							def 				__lowercase     (				self   :			Dict						):
									_a	: int								=				TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
									_a	: Dict								=				tf.constant([[0, 1, 2, 3, 4, 5]]						)
									_a	: Union[str, Any]								=				model(_UpperCAmelCase						)[0]

									_a	: Optional[Any]								=				[1, 6, 768]
									self.assertEqual(output.shape      ,_UpperCAmelCase						)

									_a	: int								=				tf.constant(
									    [
									        [
									            [-0.03_47_54_93, -0.4_68_60_34, -0.30_63_88_32],
									            [0.22_63_72_48, -0.26_98_86_46, -0.7_42_34_24],
									            [0.10_32_48_68, -0.45_01_35_08, -0.58_28_07_84],
									        ]
									    ]						)
									tf.debugging.assert_near(output[:, :3, :3]      ,_UpperCAmelCase      ,atol=1E-4						)









'''simple docstring'''

import warnings

from ...utils import logging
from .image_processing_deit import DeiTImageProcessor


__lowerCAmelCase		     =  logging.get_logger(__name__)

class       __magic_name__		(       _UpperCamelCase  ):







							def __init__(				self   :			Tuple      ,*_UpperCAmelCase   :			str      ,**_UpperCAmelCase   :			int						):
									warnings.warn(
									    'The class DeiTFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
									    ' use DeiTImageProcessor instead.'      ,_UpperCAmelCase      ,)
									super().__init__(*_UpperCAmelCase      ,**_UpperCAmelCase						)




