





"""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 			__lowercase     :







					'''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=512		,     _UpperCAmelCase=16		,     _UpperCAmelCase=2		,     _UpperCAmelCase=0.0_2		,     _UpperCAmelCase=3		,     _UpperCAmelCase=4		,     _UpperCAmelCase=None		,     ):
									__a	:  str         =					parent
									__a	:  Dict         =					13
									__a	:  List[str]         =					7
									__a	:  Union[str, Any]         =					True
									__a	:  Any         =					True
									__a	:  Union[str, Any]         =					True
									__a	:  str         =					True
									__a	:  List[str]         =					99
									__a	:  Union[str, Any]         =					384
									__a	:  List[str]         =					2
									__a	:  Union[str, Any]         =					4
									__a	:  List[str]         =					37
									__a	:  Union[str, Any]         =					'''gelu'''
									__a	:  Union[str, Any]         =					0.1
									__a	:  Optional[Any]         =					0.1
									__a	:  Optional[int]         =					512
									__a	:  Dict         =					16
									__a	:  int         =					2
									__a	:  Optional[int]         =					0.0_2
									__a	:  str         =					3
									__a	:  Any         =					4
									__a	:  Any         =					128
									__a	:  List[str]         =					2
									__a	:  Tuple         =					9
									__a	:  List[str]         =					1
									__a	:  int         =					None







					def 	_lowerCamelCase						(  self       ):
									__a	:  Optional[int]         =					ids_tensor([self.batch_size, self.seq_length]		,     self.vocab_size       )

									__a	:  str         =					None
									if self.use_input_mask:
													__a	:  Optional[int]         =					random_attention_mask([self.batch_size, self.seq_length]       )

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

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

									__a	:  Union[str, Any]         =					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       ):
									__a	:  List[str]         =					TFConvBertModel(config=_UpperCAmelCase       )
									__a	:  Dict         =					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

									__a	:  str         =					[input_ids, input_mask]
									__a	:  int         =					model(_UpperCAmelCase       )

									__a	:  str         =					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       ):
									__a	:  str         =					TFConvBertForMaskedLM(config=_UpperCAmelCase       )
									__a	:  Tuple         =					{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									__a	:  int         =					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       ):
									__a	:  Union[str, Any]         =					self.num_labels
									__a	:  Tuple         =					TFConvBertForSequenceClassification(config=_UpperCAmelCase       )
									__a	:  Optional[Any]         =					{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

									__a	:  List[Any]         =					model(_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_labels)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  str         =					self.num_choices
									__a	:  Tuple         =					TFConvBertForMultipleChoice(config=_UpperCAmelCase       )
									__a	:  Any         =					tf.tile(tf.expand_dims(_UpperCAmelCase		,     1       )		,     (1, self.num_choices, 1)       )
									__a	:  str         =					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	:  int         =					{
									    '''input_ids''': multiple_choice_inputs_ids,
									    '''attention_mask''': multiple_choice_input_mask,
									    '''token_type_ids''': multiple_choice_token_type_ids,
									}
									__a	:  int         =					model(_UpperCAmelCase       )
									self.parent.assertEqual(result.logits.shape		,     (self.batch_size, self.num_choices)       )







					def 	_lowerCamelCase						(  self		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase		,     _UpperCAmelCase       ):
									__a	:  Any         =					self.num_labels
									__a	:  List[Any]         =					TFConvBertForTokenClassification(config=_UpperCAmelCase       )
									__a	:  str         =					{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}
									__a	:  str         =					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       ):
									__a	:  List[str]         =					TFConvBertForQuestionAnswering(config=_UpperCAmelCase       )
									__a	:  Any         =					{
									    '''input_ids''': input_ids,
									    '''attention_mask''': input_mask,
									    '''token_type_ids''': token_type_ids,
									}

									__a	:  List[Any]         =					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       ):
									__a	:  Any         =					self.prepare_config_and_inputs()
									(
									    (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  (
									    __a
									)    ,  
									)	:  Dict         =					config_and_inputs
									__a	:  int         =					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
									return config, inputs_dict






@require_tf
class 			__lowercase     (     _UpperCamelCase				, _UpperCamelCase				, 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       ):
									__a	:  Optional[Any]         =					TFConvBertModelTester(self       )
									__a	:  Any         =					ConfigTester(self		,     config_class=_UpperCAmelCase		,     hidden_size=37       )







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







					def 	_lowerCamelCase						(  self       ):
									__a	:  Union[str, Any]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Any         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  str         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  List[str]         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a	:  Dict         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_UpperCAmelCase       )







					@slow
					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  Dict         =					self.model_tester.prepare_config_and_inputs_for_common()
									__a	:  List[str]         =					True
									__a	:  Tuple         =					True

									if hasattr(_UpperCAmelCase		,     '''use_cache'''       ):
													__a	:  List[str]         =					True

									__a	:  int         =					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	:  Optional[int]         =					self._prepare_for_class(_UpperCAmelCase		,     _UpperCAmelCase       )
													__a	:  Tuple         =					model_class(_UpperCAmelCase       )
													__a	:  List[Any]         =					len(model(_UpperCAmelCase       )       )

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

																	if self.is_encoder_decoder:
																					__a	:  int         =					outputs['''encoder_hidden_states''']
																					__a	:  Optional[int]         =					outputs['''encoder_attentions''']
																	else:
																					__a	:  List[str]         =					outputs['''hidden_states''']
																					__a	:  str         =					outputs['''attentions''']

																	self.assertEqual(len(_UpperCAmelCase       )		,     _UpperCAmelCase       )

																	__a	:  Tuple         =					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       ):
									__a	:  Optional[int]         =					TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
									self.assertIsNotNone(_UpperCAmelCase       )







					def 	_lowerCamelCase						(  self       ):
									__a    ,  __a	:  Any         =					self.model_tester.prepare_config_and_inputs_for_common()
									__a	:  Dict         =					True
									__a	:  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	:  List[str]         =					getattr(self.model_tester		,     '''key_length'''		,     _UpperCAmelCase       )
									__a	:  str         =					getattr(self.model_tester		,     '''key_length'''		,     _UpperCAmelCase       )

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

													if self.is_encoder_decoder:
																	__a	:  Any         =					model_class(_UpperCAmelCase       )
																	__a	:  Tuple         =					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	:  List[str]         =					True
													__a	:  Tuple         =					model_class(_UpperCAmelCase       )
													__a	:  List[str]         =					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	:  str         =					True
													__a	:  Tuple         =					model_class(_UpperCAmelCase       )
													__a	:  str         =					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 			__lowercase     (     unittest.TestCase							):







					'''simple docstring'''




					@slow
					def 	_lowerCamelCase						(  self       ):
									__a	:  List[Any]         =					TFConvBertModel.from_pretrained('''YituTech/conv-bert-base'''       )
									__a	:  Optional[Any]         =					tf.constant([[0, 1, 2, 3, 4, 5]]       )
									__a	:  Tuple         =					model(_UpperCAmelCase       )[0]

									__a	:  List[str]         =					[1, 6, 768]
									self.assertEqual(output.shape		,     _UpperCAmelCase       )

									__a	:  List[str]         =					tf.constant(
									    [
									        [
									            [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
									            [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
									            [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
									        ]
									    ]       )
									tf.debugging.assert_near(output[:, :3, :3]		,     _UpperCAmelCase		,     atol=1e-4       )






"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


A	     =					{
    '''configuration_instructblip''': [
        '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''InstructBlipConfig''',
        '''InstructBlipQFormerConfig''',
        '''InstructBlipVisionConfig''',
    ],
    '''processing_instructblip''': ['''InstructBlipProcessor'''],
}

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		A	     =					[
		    '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''InstructBlipQFormerModel''',
		    '''InstructBlipPreTrainedModel''',
		    '''InstructBlipForConditionalGeneration''',
		    '''InstructBlipVisionModel''',
		]

if TYPE_CHECKING:
		from .configuration_instructblip import (
		    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    InstructBlipConfig,
		    InstructBlipQFormerConfig,
		    InstructBlipVisionConfig,
		)
		from .processing_instructblip import InstructBlipProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_instructblip import (
				    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    InstructBlipForConditionalGeneration,
				    InstructBlipPreTrainedModel,
				    InstructBlipQFormerModel,
				    InstructBlipVisionModel,
				)

else:
		import sys

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