



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







						def __init__(  self   :      Optional[int]				,    lowerCAmelCase__   :      Dict				,    lowerCAmelCase__   :      Optional[Any]=13				,    lowerCAmelCase__   :      Tuple=7				,    lowerCAmelCase__   :      Union[str, Any]=True				,    lowerCAmelCase__   :      Optional[int]=True				,    lowerCAmelCase__   :      int=True				,    lowerCAmelCase__   :      Any=True				,    lowerCAmelCase__   :      int=99				,    lowerCAmelCase__   :      Tuple=32				,    lowerCAmelCase__   :      str=2				,    lowerCAmelCase__   :      Optional[int]=4				,    lowerCAmelCase__   :      Optional[int]=37				,    lowerCAmelCase__   :      List[Any]="gelu"				,    lowerCAmelCase__   :      Tuple=0.1				,    lowerCAmelCase__   :      Union[str, Any]=0.1				,    lowerCAmelCase__   :      int=5_12				,    lowerCAmelCase__   :      Union[str, Any]=16				,    lowerCAmelCase__   :      Dict=2				,    lowerCAmelCase__   :      int=0.02				,    lowerCAmelCase__   :      Any=3				,    lowerCAmelCase__   :      Dict=4				,    lowerCAmelCase__   :      Union[str, Any]=None				,    ):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			parent
													SCREAMING_SNAKE_CASE					:      List[str]     					=			13
													SCREAMING_SNAKE_CASE					:      Dict     					=			7
													SCREAMING_SNAKE_CASE					:      str     					=			True
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			True
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			True
													SCREAMING_SNAKE_CASE					:      List[str]     					=			True
													SCREAMING_SNAKE_CASE					:      List[str]     					=			99
													SCREAMING_SNAKE_CASE					:      int     					=			3_84
													SCREAMING_SNAKE_CASE					:      Dict     					=			2
													SCREAMING_SNAKE_CASE					:      List[str]     					=			4
													SCREAMING_SNAKE_CASE					:      Any     					=			37
													SCREAMING_SNAKE_CASE					:      List[str]     					=			'''gelu'''
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			0.1
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			0.1
													SCREAMING_SNAKE_CASE					:      List[str]     					=			5_12
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			16
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			2
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			0.02
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			3
													SCREAMING_SNAKE_CASE					:      List[str]     					=			4
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			1_28
													SCREAMING_SNAKE_CASE					:      int     					=			2
													SCREAMING_SNAKE_CASE					:      int     					=			9
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			1
													SCREAMING_SNAKE_CASE					:      str     					=			None







						def 					__lowercase    (  self   :      List[str]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size				)

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

													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			None
													if self.use_token_type_ids:
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			ids_tensor([self.batch_size, self.seq_length]				,    self.type_vocab_size				)

													SCREAMING_SNAKE_CASE					:      Tuple     					=			None
													SCREAMING_SNAKE_CASE					:      int     					=			None
													SCREAMING_SNAKE_CASE					:      Tuple     					=			None
													if self.use_labels:
																				SCREAMING_SNAKE_CASE					:      Dict     					=			ids_tensor([self.batch_size]				,    self.type_sequence_label_size				)
																				SCREAMING_SNAKE_CASE					:      int     					=			ids_tensor([self.batch_size, self.seq_length]				,    self.num_labels				)
																				SCREAMING_SNAKE_CASE					:      int     					=			ids_tensor([self.batch_size]				,    self.num_choices				)

													SCREAMING_SNAKE_CASE					:      str     					=			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 					__lowercase    (  self   :      Optional[int]				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      Dict				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      Tuple				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			TFConvBertModel(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Any     					=			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			[input_ids, input_mask]
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			model(lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			model(lowerCAmelCase__				)

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







						def 					__lowercase    (  self   :      Optional[Any]				,    lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      Tuple				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			TFConvBertForMaskedLM(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      List[str]     					=			{
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			model(lowerCAmelCase__				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size)				)







						def 					__lowercase    (  self   :      List[Any]				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      Union[str, Any]				,    lowerCAmelCase__   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			self.num_labels
													SCREAMING_SNAKE_CASE					:      int     					=			TFConvBertForSequenceClassification(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      List[str]     					=			{
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}

													SCREAMING_SNAKE_CASE					:      Tuple     					=			model(lowerCAmelCase__				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels)				)







						def 					__lowercase    (  self   :      Optional[Any]				,    lowerCAmelCase__   :      Dict				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      List[Any]				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.num_choices
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			TFConvBertForMultipleChoice(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			tf.tile(tf.expand_dims(lowerCAmelCase__				,    1				)				,    (1, self.num_choices, 1)				)
													SCREAMING_SNAKE_CASE					:      str     					=			tf.tile(tf.expand_dims(lowerCAmelCase__				,    1				)				,    (1, self.num_choices, 1)				)
													SCREAMING_SNAKE_CASE					:      List[Any]     					=			tf.tile(tf.expand_dims(lowerCAmelCase__				,    1				)				,    (1, self.num_choices, 1)				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			{
													    '''input_ids''': multiple_choice_inputs_ids,
													    '''attention_mask''': multiple_choice_input_mask,
													    '''token_type_ids''': multiple_choice_token_type_ids,
													}
													SCREAMING_SNAKE_CASE					:      Dict     					=			model(lowerCAmelCase__				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_choices)				)







						def 					__lowercase    (  self   :      Optional[int]				,    lowerCAmelCase__   :      str				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      List[str]				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			self.num_labels
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			TFConvBertForTokenClassification(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			{
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}
													SCREAMING_SNAKE_CASE					:      str     					=			model(lowerCAmelCase__				)
													self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.num_labels)				)







						def 					__lowercase    (  self   :      int				,    lowerCAmelCase__   :      Union[str, Any]				,    lowerCAmelCase__   :      Union[str, Any]				,    lowerCAmelCase__   :      List[Any]				,    lowerCAmelCase__   :      Tuple				,    lowerCAmelCase__   :      int				,    lowerCAmelCase__   :      Any				,    lowerCAmelCase__   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			TFConvBertForQuestionAnswering(config=lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			{
													    '''input_ids''': input_ids,
													    '''attention_mask''': input_mask,
													    '''token_type_ids''': token_type_ids,
													}

													SCREAMING_SNAKE_CASE					:      List[Any]     					=			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 					__lowercase    (  self   :      Optional[int]				):

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


@require_tf
class 	lowerCamelCase_							(					snake_case_    ,				snake_case_    ,				unittest.TestCase			):
						_lowerCAmelCase     :   List[str]	     =				(
						    (
						        TFConvBertModel,
						        TFConvBertForMaskedLM,
						        TFConvBertForQuestionAnswering,
						        TFConvBertForSequenceClassification,
						        TFConvBertForTokenClassification,
						        TFConvBertForMultipleChoice,
						    )
						    if is_tf_available()
						    else ()
						)
						_lowerCAmelCase     :   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     :   Optional[Any]	     =				False
						_lowerCAmelCase     :   Optional[Any]	     =				False
						_lowerCAmelCase     :   str	     =				False







						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			TFConvBertModelTester(self				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			ConfigTester(self				,    config_class=lowerCAmelCase__				,    hidden_size=37				)







						def 					__lowercase    (  self   :      Dict				):

													"""simple docstring"""
													self.config_tester.run_common_tests()







						def 					__lowercase    (  self   :      int				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_model(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Any				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Tuple				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Dict     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_multiple_choice(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Any     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Optional[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__				)







						def 					__lowercase    (  self   :      Tuple				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Tuple     					=			self.model_tester.prepare_config_and_inputs()
													self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__				)







						@slow
						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      str     					=			self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE					:      List[str]     					=			True
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			True

													if hasattr(lowerCAmelCase__				,    '''use_cache'''				):
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			True

													SCREAMING_SNAKE_CASE					:      Dict     					=			getattr(self.model_tester				,    '''encoder_seq_length'''				,    self.model_tester.seq_length				)
													SCREAMING_SNAKE_CASE					:      Optional[int]     					=			getattr(self.model_tester				,    '''key_length'''				,    lowerCAmelCase__				)

													for model_class in self.all_model_classes:
																				SCREAMING_SNAKE_CASE					:      int     					=			self._prepare_for_class(lowerCAmelCase__				,    lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Tuple     					=			model_class(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			len(model(lowerCAmelCase__				)				)

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

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

																											self.assertEqual(len(lowerCAmelCase__				)				,    lowerCAmelCase__				)

																											SCREAMING_SNAKE_CASE					:      Dict     					=			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 					__lowercase    (  self   :      str				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''				)
													self.assertIsNotNone(lowerCAmelCase__				)







						def 					__lowercase    (  self   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      List[str]     					=			self.model_tester.prepare_config_and_inputs_for_common()
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			True
													SCREAMING_SNAKE_CASE					:      List[str]     					=			getattr(self.model_tester				,    '''decoder_seq_length'''				,    self.model_tester.seq_length				)
													SCREAMING_SNAKE_CASE					:      int     					=			getattr(self.model_tester				,    '''encoder_seq_length'''				,    self.model_tester.seq_length				)
													SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			getattr(self.model_tester				,    '''key_length'''				,    lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Any     					=			getattr(self.model_tester				,    '''key_length'''				,    lowerCAmelCase__				)

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

																				if self.is_encoder_decoder:
																											SCREAMING_SNAKE_CASE					:      Optional[int]     					=			model_class(lowerCAmelCase__				)
																											SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			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"]
																				SCREAMING_SNAKE_CASE					:      str     					=			True
																				SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			model_class(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			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
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			True
																				SCREAMING_SNAKE_CASE					:      Dict     					=			True
																				SCREAMING_SNAKE_CASE					:      str     					=			model_class(lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Tuple     					=			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 	lowerCamelCase_							(					unittest.TestCase			):







						@slow
						def 					__lowercase    (  self   :      Union[str, Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      int     					=			TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''				)
													SCREAMING_SNAKE_CASE					:      Any     					=			tf.constant([[0, 1, 2, 3, 4, 5]]				)
													SCREAMING_SNAKE_CASE					:      Any     					=			model(lowerCAmelCase__				)[0]

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

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









'''simple docstring'''



import importlib
import json
import os
from collections import OrderedDict
from typing import Dict, Optional, Union

# Build the list of all image processors
from ...configuration_utils import PretrainedConfig
from ...dynamic_module_utils import get_class_from_dynamic_module, resolve_trust_remote_code
from ...image_processing_utils import ImageProcessingMixin
from ...utils import CONFIG_NAME, IMAGE_PROCESSOR_NAME, get_file_from_repo, logging
from .auto_factory import _LazyAutoMapping
from .configuration_auto import (
    CONFIG_MAPPING_NAMES,
    AutoConfig,
    model_type_to_module_name,
    replace_list_option_in_docstrings,
)


lowerCAmelCase_     :       str								=						logging.get_logger(__name__)

lowerCAmelCase_     :       List[Any]								=						OrderedDict(
    [
        ('align', 'EfficientNetImageProcessor'),
        ('beit', 'BeitImageProcessor'),
        ('bit', 'BitImageProcessor'),
        ('blip', 'BlipImageProcessor'),
        ('blip-2', 'BlipImageProcessor'),
        ('bridgetower', 'BridgeTowerImageProcessor'),
        ('chinese_clip', 'ChineseCLIPImageProcessor'),
        ('clip', 'CLIPImageProcessor'),
        ('clipseg', 'ViTImageProcessor'),
        ('conditional_detr', 'ConditionalDetrImageProcessor'),
        ('convnext', 'ConvNextImageProcessor'),
        ('convnextv2', 'ConvNextImageProcessor'),
        ('cvt', 'ConvNextImageProcessor'),
        ('data2vec-vision', 'BeitImageProcessor'),
        ('deformable_detr', 'DeformableDetrImageProcessor'),
        ('deit', 'DeiTImageProcessor'),
        ('deta', 'DetaImageProcessor'),
        ('detr', 'DetrImageProcessor'),
        ('dinat', 'ViTImageProcessor'),
        ('donut-swin', 'DonutImageProcessor'),
        ('dpt', 'DPTImageProcessor'),
        ('efficientformer', 'EfficientFormerImageProcessor'),
        ('efficientnet', 'EfficientNetImageProcessor'),
        ('flava', 'FlavaImageProcessor'),
        ('focalnet', 'BitImageProcessor'),
        ('git', 'CLIPImageProcessor'),
        ('glpn', 'GLPNImageProcessor'),
        ('groupvit', 'CLIPImageProcessor'),
        ('imagegpt', 'ImageGPTImageProcessor'),
        ('instructblip', 'BlipImageProcessor'),
        ('layoutlmv2', 'LayoutLMv2ImageProcessor'),
        ('layoutlmv3', 'LayoutLMv3ImageProcessor'),
        ('levit', 'LevitImageProcessor'),
        ('mask2former', 'Mask2FormerImageProcessor'),
        ('maskformer', 'MaskFormerImageProcessor'),
        ('mgp-str', 'ViTImageProcessor'),
        ('mobilenet_v1', 'MobileNetV1ImageProcessor'),
        ('mobilenet_v2', 'MobileNetV2ImageProcessor'),
        ('mobilevit', 'MobileViTImageProcessor'),
        ('mobilevit', 'MobileViTImageProcessor'),
        ('mobilevitv2', 'MobileViTImageProcessor'),
        ('nat', 'ViTImageProcessor'),
        ('oneformer', 'OneFormerImageProcessor'),
        ('owlvit', 'OwlViTImageProcessor'),
        ('perceiver', 'PerceiverImageProcessor'),
        ('pix2struct', 'Pix2StructImageProcessor'),
        ('poolformer', 'PoolFormerImageProcessor'),
        ('regnet', 'ConvNextImageProcessor'),
        ('resnet', 'ConvNextImageProcessor'),
        ('sam', 'SamImageProcessor'),
        ('segformer', 'SegformerImageProcessor'),
        ('swiftformer', 'ViTImageProcessor'),
        ('swin', 'ViTImageProcessor'),
        ('swin2sr', 'Swin2SRImageProcessor'),
        ('swinv2', 'ViTImageProcessor'),
        ('table-transformer', 'DetrImageProcessor'),
        ('timesformer', 'VideoMAEImageProcessor'),
        ('tvlt', 'TvltImageProcessor'),
        ('upernet', 'SegformerImageProcessor'),
        ('van', 'ConvNextImageProcessor'),
        ('videomae', 'VideoMAEImageProcessor'),
        ('vilt', 'ViltImageProcessor'),
        ('vit', 'ViTImageProcessor'),
        ('vit_hybrid', 'ViTHybridImageProcessor'),
        ('vit_mae', 'ViTImageProcessor'),
        ('vit_msn', 'ViTImageProcessor'),
        ('xclip', 'CLIPImageProcessor'),
        ('yolos', 'YolosImageProcessor'),
    ]
)

lowerCAmelCase_     :       List[str]								=						_LazyAutoMapping(CONFIG_MAPPING_NAMES, IMAGE_PROCESSOR_MAPPING_NAMES)
def 					UpperCAmelCase   (						A  :				str				):
							for module_name, extractors in IMAGE_PROCESSOR_MAPPING_NAMES.items():
														if class_name in extractors:
																					SCREAMING_SNAKE_CASE					:      Tuple     					=			model_type_to_module_name(A				)

																					SCREAMING_SNAKE_CASE					:      List[Any]     					=			importlib.import_module(F""".{module_name}"""					,		'''transformers.models'''				)
																					try:
																												return getattr(A					,		A				)
																					except AttributeError:
																												continue

							for _, extractor in IMAGE_PROCESSOR_MAPPING._extra_content.items():
														if getattr(A					,		'''__name__'''					,		A				) == class_name:
																					return extractor

    # We did not fine the class, but maybe it's because a dep is missing. In that case, the class will be in the main
    # init and we return the proper dummy to get an appropriate error message.
							SCREAMING_SNAKE_CASE					:      List[str]     					=			importlib.import_module('''transformers'''				)
							if hasattr(A					,		A				):
														return getattr(A					,		A				)

							return None
def 					UpperCAmelCase   (						A  :				Union[str, os.PathLike]					,		A  :				Optional[Union[str, os.PathLike]] = None					,		A  :				bool = False					,		A  :				bool = False					,		A  :				Optional[Dict[str, str]] = None					,		A  :				Optional[Union[bool, str]] = None					,		A  :				Optional[str] = None					,		A  :				bool = False					,		**A  :				List[str]					,		):
							SCREAMING_SNAKE_CASE					:      List[Any]     					=			get_file_from_repo(
							    A					,		A					,		cache_dir=A					,		force_download=A					,		resume_download=A					,		proxies=A					,		use_auth_token=A					,		revision=A					,		local_files_only=A					,		)
							if resolved_config_file is None:
														logger.info(
														    '''Could not locate the image processor configuration file, will try to use the model config instead.'''				)
														return {}

							with open(A					,		encoding='''utf-8'''				) as reader:
														return json.load(A				)






class 	lowerCamelCase_							:







						def __init__(  self   :      Tuple				):

													"""simple docstring"""
													raise EnvironmentError(
													    '''AutoImageProcessor is designed to be instantiated '''
													    '''using the `AutoImageProcessor.from_pretrained(pretrained_model_name_or_path)` method.'''				)







						@classmethod
						@replace_list_option_in_docstrings(lowerCAmelCase__				)
						def 					__lowercase    (  cls   :      int				,    lowerCAmelCase__   :      Optional[int]				,    **lowerCAmelCase__   :      List[Any]				):

													"""simple docstring"""
													SCREAMING_SNAKE_CASE					:      Any     					=			kwargs.pop('''config'''				,    lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Tuple     					=			kwargs.pop('''trust_remote_code'''				,    lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      List[str]     					=			True

													SCREAMING_SNAKE_CASE					,SCREAMING_SNAKE_CASE					:      str     					=			ImageProcessingMixin.get_image_processor_dict(lowerCAmelCase__				,    **lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      Tuple     					=			config_dict.get('''image_processor_type'''				,    lowerCAmelCase__				)
													SCREAMING_SNAKE_CASE					:      str     					=			None
													if "AutoImageProcessor" in config_dict.get('''auto_map'''				,    {}				):
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			config_dict['''auto_map''']['''AutoImageProcessor''']

													# If we still don't have the image processor class, check if we're loading from a previous feature extractor config
													# and if so, infer the image processor class from there.
													if image_processor_class is None and image_processor_auto_map is None:
																				SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			config_dict.pop('''feature_extractor_type'''				,    lowerCAmelCase__				)
																				if feature_extractor_class is not None:
																											logger.warning(
																											    '''Could not find image processor class in the image processor config or the model config. Loading'''
																											    ''' based on pattern matching with the model\'s feature extractor configuration.'''				)
																											SCREAMING_SNAKE_CASE					:      Optional[Any]     					=			feature_extractor_class.replace('''FeatureExtractor'''				,    '''ImageProcessor'''				)
																				if "AutoFeatureExtractor" in config_dict.get('''auto_map'''				,    {}				):
																											SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			config_dict['''auto_map''']['''AutoFeatureExtractor''']
																											SCREAMING_SNAKE_CASE					:      str     					=			feature_extractor_auto_map.replace('''FeatureExtractor'''				,    '''ImageProcessor'''				)
																											logger.warning(
																											    '''Could not find image processor auto map in the image processor config or the model config.'''
																											    ''' Loading based on pattern matching with the model\'s feature extractor configuration.'''				)

        # If we don't find the image processor class in the image processor config, let's try the model config.
													if image_processor_class is None and image_processor_auto_map is None:
																				if not isinstance(lowerCAmelCase__				,    lowerCAmelCase__				):
																											SCREAMING_SNAKE_CASE					:      List[str]     					=			AutoConfig.from_pretrained(lowerCAmelCase__				,    **lowerCAmelCase__				)
																				# It could be in `config.image_processor_type``
																				SCREAMING_SNAKE_CASE					:      Any     					=			getattr(lowerCAmelCase__				,    '''image_processor_type'''				,    lowerCAmelCase__				)
																				if hasattr(lowerCAmelCase__				,    '''auto_map'''				) and "AutoImageProcessor" in config.auto_map:
																											SCREAMING_SNAKE_CASE					:      Tuple     					=			config.auto_map['''AutoImageProcessor''']

													if image_processor_class is not None:
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			image_processor_class_from_name(lowerCAmelCase__				)

													SCREAMING_SNAKE_CASE					:      Tuple     					=			image_processor_auto_map is not None
													SCREAMING_SNAKE_CASE					:      str     					=			image_processor_class is not None or type(lowerCAmelCase__				) in IMAGE_PROCESSOR_MAPPING
													SCREAMING_SNAKE_CASE					:      Union[str, Any]     					=			resolve_trust_remote_code(
													    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				)

													if has_remote_code and trust_remote_code:
																				SCREAMING_SNAKE_CASE					:      List[Any]     					=			get_class_from_dynamic_module(
																				    lowerCAmelCase__				,    lowerCAmelCase__				,    **lowerCAmelCase__				)
																				SCREAMING_SNAKE_CASE					:      Optional[int]     					=			kwargs.pop('''code_revision'''				,    lowerCAmelCase__				)
																				if os.path.isdir(lowerCAmelCase__				):
																											image_processor_class.register_for_auto_class()
																				return image_processor_class.from_dict(lowerCAmelCase__				,    **lowerCAmelCase__				)
													elif image_processor_class is not None:
																				return image_processor_class.from_dict(lowerCAmelCase__				,    **lowerCAmelCase__				)
													# Last try: we use the IMAGE_PROCESSOR_MAPPING.
													elif type(lowerCAmelCase__				) in IMAGE_PROCESSOR_MAPPING:
																				SCREAMING_SNAKE_CASE					:      List[str]     					=			IMAGE_PROCESSOR_MAPPING[type(lowerCAmelCase__				)]
																				return image_processor_class.from_dict(lowerCAmelCase__				,    **lowerCAmelCase__				)

													raise ValueError(
													    F"""Unrecognized image processor in {pretrained_model_name_or_path}. Should have a """
													    F"""`image_processor_type` key in its {IMAGE_PROCESSOR_NAME} of {CONFIG_NAME}, or one of the following """
													    F"""`model_type` keys in its {CONFIG_NAME}: {", ".join(c for c in IMAGE_PROCESSOR_MAPPING_NAMES.keys()				)}"""				)







						@staticmethod
						def 					__lowercase    (  lowerCAmelCase__   :      Optional[int]				,    lowerCAmelCase__   :      Optional[int]				):

													"""simple docstring"""
													IMAGE_PROCESSOR_MAPPING.register(lowerCAmelCase__				,    lowerCAmelCase__				)




