

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 SCREAMING_SNAKE_CASE  :






			"""simple docstring"""




			def __init__(    self ,				__UpperCamelCase ,				__UpperCamelCase=13 ,				__UpperCamelCase=7 ,				__UpperCamelCase=True ,				__UpperCamelCase=True ,				__UpperCamelCase=True ,				__UpperCamelCase=True ,				__UpperCamelCase=99 ,				__UpperCamelCase=32 ,				__UpperCamelCase=2 ,				__UpperCamelCase=4 ,				__UpperCamelCase=37 ,				__UpperCamelCase="gelu" ,				__UpperCamelCase=0.1 ,				__UpperCamelCase=0.1 ,				__UpperCamelCase=5_12 ,				__UpperCamelCase=16 ,				__UpperCamelCase=2 ,				__UpperCamelCase=0.02 ,				__UpperCamelCase=3 ,				__UpperCamelCase=4 ,				__UpperCamelCase=None ,				):

				"""simple docstring"""


				snake_case_      		= parent
				snake_case_      		= 13
				snake_case_      		= 7
				snake_case_      		= True
				snake_case_      		= True
				snake_case_      		= True
				snake_case_      		= True
				snake_case_      		= 99
				snake_case_      		= 3_84
				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_      		= 1_28
				snake_case_      		= 2
				snake_case_      		= 9
				snake_case_      		= 1
				snake_case_      		= None


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= ids_tensor([self.batch_size, self.seq_length] ,				self.vocab_size							)

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

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

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

				snake_case_      		= 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 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= TFConvBertModel(config=__UpperCamelCase							)
				snake_case_      		= {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

				snake_case_      		= [input_ids, input_mask]
				snake_case_      		= model(__UpperCamelCase							)

				snake_case_      		= model(__UpperCamelCase							)

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


			def 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= TFConvBertForMaskedLM(config=__UpperCamelCase							)
				snake_case_      		= {
				    'input_ids': input_ids,
				    'attention_mask': input_mask,
				    'token_type_ids': token_type_ids,
				}
				snake_case_      		= model(__UpperCamelCase							)
				self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.vocab_size)							)


			def 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= self.num_labels
				snake_case_      		= TFConvBertForSequenceClassification(config=__UpperCamelCase							)
				snake_case_      		= {
				    'input_ids': input_ids,
				    'attention_mask': input_mask,
				    'token_type_ids': token_type_ids,
				}

				snake_case_      		= model(__UpperCamelCase							)
				self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_labels)							)


			def 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= self.num_choices
				snake_case_      		= TFConvBertForMultipleChoice(config=__UpperCamelCase							)
				snake_case_      		= tf.tile(tf.expand_dims(__UpperCamelCase ,				1							) ,				(1, self.num_choices, 1)							)
				snake_case_      		= tf.tile(tf.expand_dims(__UpperCamelCase ,				1							) ,				(1, self.num_choices, 1)							)
				snake_case_      		= tf.tile(tf.expand_dims(__UpperCamelCase ,				1							) ,				(1, self.num_choices, 1)							)
				snake_case_      		= {
				    'input_ids': multiple_choice_inputs_ids,
				    'attention_mask': multiple_choice_input_mask,
				    'token_type_ids': multiple_choice_token_type_ids,
				}
				snake_case_      		= model(__UpperCamelCase							)
				self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.num_choices)							)


			def 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= self.num_labels
				snake_case_      		= TFConvBertForTokenClassification(config=__UpperCamelCase							)
				snake_case_      		= {
				    'input_ids': input_ids,
				    'attention_mask': input_mask,
				    'token_type_ids': token_type_ids,
				}
				snake_case_      		= model(__UpperCamelCase							)
				self.parent.assertEqual(result.logits.shape ,				(self.batch_size, self.seq_length, self.num_labels)							)


			def 			__lowerCAmelCase (    self ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase ,				__UpperCamelCase							):

				"""simple docstring"""


				snake_case_      		= TFConvBertForQuestionAnswering(config=__UpperCamelCase							)
				snake_case_      		= {
				    'input_ids': input_ids,
				    'attention_mask': input_mask,
				    'token_type_ids': token_type_ids,
				}

				snake_case_      		= 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 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.prepare_config_and_inputs()
				(
				    (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       (
				    snake_case_
				)		,       
				)      		= config_and_inputs
				snake_case_      		= {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
				return config, inputs_dict







@require_tf
class SCREAMING_SNAKE_CASE  (		__snake_case							,						__snake_case							,						unittest.TestCase							):






			"""simple docstring"""




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


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= TFConvBertModelTester(self							)
				snake_case_      		= ConfigTester(self ,				config_class=__UpperCamelCase ,				hidden_size=37							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				self.config_tester.run_common_tests()


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*__UpperCamelCase							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*__UpperCamelCase							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_multiple_choice(*__UpperCamelCase							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_question_answering(*__UpperCamelCase							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_sequence_classification(*__UpperCamelCase							)


			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_token_classification(*__UpperCamelCase							)


			@slow
			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_		,       snake_case_      		= self.model_tester.prepare_config_and_inputs_for_common()
				snake_case_      		= True
				snake_case_      		= True

				if hasattr(__UpperCamelCase ,				'use_cache'							):
					snake_case_      		= True

				snake_case_      		= getattr(self.model_tester ,				'encoder_seq_length' ,				self.model_tester.seq_length							)
				snake_case_      		= getattr(self.model_tester ,				'key_length' ,				__UpperCamelCase							)

				for model_class in self.all_model_classes:
					snake_case_      		= self._prepare_for_class(__UpperCamelCase ,				__UpperCamelCase							)
					snake_case_      		= model_class(__UpperCamelCase							)
					snake_case_      		= len(model(__UpperCamelCase							)							)

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

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

						self.assertEqual(len(__UpperCamelCase							) ,				__UpperCamelCase							)

						snake_case_      		= 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 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= TFConvBertModel.from_pretrained('YituTech/conv-bert-base'							)
				self.assertIsNotNone(__UpperCamelCase							)







			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_		,       snake_case_      		= self.model_tester.prepare_config_and_inputs_for_common()
				snake_case_      		= True
				snake_case_      		= getattr(self.model_tester ,				'decoder_seq_length' ,				self.model_tester.seq_length							)
				snake_case_      		= getattr(self.model_tester ,				'encoder_seq_length' ,				self.model_tester.seq_length							)
				snake_case_      		= getattr(self.model_tester ,				'key_length' ,				__UpperCamelCase							)
				snake_case_      		= getattr(self.model_tester ,				'key_length' ,				__UpperCamelCase							)

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

					if self.is_encoder_decoder:
						snake_case_      		= model_class(__UpperCamelCase							)
						snake_case_      		= 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"]
					snake_case_      		= True
					snake_case_      		= model_class(__UpperCamelCase							)
					snake_case_      		= 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
					snake_case_      		= True
					snake_case_      		= True
					snake_case_      		= model_class(__UpperCamelCase							)
					snake_case_      		= 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 SCREAMING_SNAKE_CASE  (		unittest.TestCase							):






			"""simple docstring"""




			@slow
			def 			__lowerCAmelCase (    self							):

				"""simple docstring"""


				snake_case_      		= TFConvBertModel.from_pretrained('YituTech/conv-bert-base'							)
				snake_case_      		= tf.constant([[0, 1, 2, 3, 4, 5]]							)
				snake_case_      		= model(__UpperCamelCase							)[0]

				snake_case_      		= [1, 6, 7_68]
				self.assertEqual(output.shape ,				__UpperCamelCase							)

				snake_case_      		= 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] ,				__UpperCamelCase ,				atol=1E-4							)



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tokenizers_available,
    is_torch_available,
    is_vision_available,
)


A											=     {
    'configuration_perceiver': ['PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'PerceiverConfig', 'PerceiverOnnxConfig'],
    'tokenization_perceiver': ['PerceiverTokenizer'],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A											=     ['PerceiverFeatureExtractor']
		A											=     ['PerceiverImageProcessor']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A											=     [
		    'PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'PerceiverForImageClassificationConvProcessing',
		    'PerceiverForImageClassificationFourier',
		    'PerceiverForImageClassificationLearned',
		    'PerceiverForMaskedLM',
		    'PerceiverForMultimodalAutoencoding',
		    'PerceiverForOpticalFlow',
		    'PerceiverForSequenceClassification',
		    'PerceiverLayer',
		    'PerceiverModel',
		    'PerceiverPreTrainedModel',
		]


if TYPE_CHECKING:
		from .configuration_perceiver import PERCEIVER_PRETRAINED_CONFIG_ARCHIVE_MAP, PerceiverConfig, PerceiverOnnxConfig
		from .tokenization_perceiver import PerceiverTokenizer

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_perceiver import PerceiverFeatureExtractor
				from .image_processing_perceiver import PerceiverImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_perceiver import (
				    PERCEIVER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    PerceiverForImageClassificationConvProcessing,
				    PerceiverForImageClassificationFourier,
				    PerceiverForImageClassificationLearned,
				    PerceiverForMaskedLM,
				    PerceiverForMultimodalAutoencoding,
				    PerceiverForOpticalFlow,
				    PerceiverForSequenceClassification,
				    PerceiverLayer,
				    PerceiverModel,
				    PerceiverPreTrainedModel,
				)

else:
		import sys

		A											=     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)
