


"""simple docstring"""





from __future__ import annotations

import unittest

import numpy as np

from transformers import BlipTextConfig
from transformers.testing_utils import require_tf, slow
from transformers.utils import is_tf_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask


if is_tf_available():
 import tensorflow as tf

 from transformers import TFBlipTextModel
 from transformers.models.blip.modeling_tf_blip import TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST



class 		snake_case_:






     def __init__(	self  :					Union[str, Any]					,				UpperCamelCase_  :					int					,				UpperCamelCase_  :					Optional[Any]=1_2					,				UpperCamelCase_  :					List[Any]=7					,				UpperCamelCase_  :					Optional[Any]=True					,				UpperCamelCase_  :					Dict=True					,				UpperCamelCase_  :					Optional[int]=True					,				UpperCamelCase_  :					List[Any]=9_9					,				UpperCamelCase_  :					Tuple=3_2					,				UpperCamelCase_  :					int=3_2					,				UpperCamelCase_  :					str=2					,				UpperCamelCase_  :					List[str]=4					,				UpperCamelCase_  :					List[str]=3_7					,				UpperCamelCase_  :					Tuple=0.1					,				UpperCamelCase_  :					Tuple=0.1					,				UpperCamelCase_  :					List[Any]=5_1_2					,				UpperCamelCase_  :					Union[str, Any]=0.02					,				UpperCamelCase_  :					Optional[int]=0					,				UpperCamelCase_  :					List[str]=None					,				):
           lowerCAmelCase					:	int       =   parent
           lowerCAmelCase					:	Union[str, Any]       =   batch_size
           lowerCAmelCase					:	str       =   seq_length
           lowerCAmelCase					:	Any       =   is_training
           lowerCAmelCase					:	Tuple       =   use_input_mask
           lowerCAmelCase					:	List[Any]       =   use_labels
           lowerCAmelCase					:	List[str]       =   vocab_size
           lowerCAmelCase					:	Optional[Any]       =   hidden_size
           lowerCAmelCase					:	Any       =   projection_dim
           lowerCAmelCase					:	List[str]       =   num_hidden_layers
           lowerCAmelCase					:	Tuple       =   num_attention_heads
           lowerCAmelCase					:	str       =   intermediate_size
           lowerCAmelCase					:	List[str]       =   dropout
           lowerCAmelCase					:	int       =   attention_dropout
           lowerCAmelCase					:	str       =   max_position_embeddings
           lowerCAmelCase					:	Union[str, Any]       =   initializer_range
           lowerCAmelCase					:	List[str]       =   scope
           lowerCAmelCase					:	List[str]       =   bos_token_id






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

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

           if input_mask is not None:
                 lowerCAmelCase					:	Optional[int]       =   input_mask.numpy()
                 lowerCAmelCase,      lowerCAmelCase					:	str       =   input_mask.shape
                 lowerCAmelCase					:	List[Any]       =   np.random.randint(1					,				seq_length - 1					,				size=(batch_size,)				)
                 for batch_idx, start_index in enumerate(UpperCamelCase_				):
                       lowerCAmelCase					:	List[Any]       =   1
                       lowerCAmelCase					:	List[str]       =   0

           lowerCAmelCase					:	Tuple       =   self.get_config()

           return config, input_ids, tf.convert_to_tensor(UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           return BlipTextConfig(
               vocab_size=self.vocab_size					,				hidden_size=self.hidden_size					,				projection_dim=self.projection_dim					,				num_hidden_layers=self.num_hidden_layers					,				num_attention_heads=self.num_attention_heads					,				intermediate_size=self.intermediate_size					,				dropout=self.dropout					,				attention_dropout=self.attention_dropout					,				max_position_embeddings=self.max_position_embeddings					,				initializer_range=self.initializer_range					,				bos_token_id=self.bos_token_id					,				)






     def      lowerCamelCase__     (	self  :					Union[str, Any]					,				UpperCamelCase_  :					Tuple					,				UpperCamelCase_  :					Optional[Any]					,				UpperCamelCase_  :					List[str]				):
           lowerCAmelCase					:	List[Any]       =   TFBlipTextModel(config=UpperCamelCase_				)
           lowerCAmelCase					:	Tuple       =   model(UpperCamelCase_					,				attention_mask=UpperCamelCase_					,				training=UpperCamelCase_				)
           lowerCAmelCase					:	Any       =   model(UpperCamelCase_					,				training=UpperCamelCase_				)
           self.parent.assertEqual(result.last_hidden_state.shape					,				(self.batch_size, self.seq_length, self.hidden_size)				)
           self.parent.assertEqual(result.pooler_output.shape					,				(self.batch_size, self.hidden_size)				)






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



@require_tf
class 		snake_case_(       a__							,					unittest.TestCase			):
     __UpperCamelCase  =					(TFBlipTextModel,) if is_tf_available() else ()
     __UpperCamelCase  =					False
     __UpperCamelCase  =					False
     __UpperCamelCase  =					False






     def      lowerCamelCase__     (	self  :					List[str]				):
           lowerCAmelCase					:	List[str]       =   BlipTextModelTester(self				)
           lowerCAmelCase					:	int       =   ConfigTester(self					,				config_class=UpperCamelCase_					,				hidden_size=3_7				)






     def      lowerCamelCase__     (	self  :					List[str]				):
           self.config_tester.run_common_tests()






     def      lowerCamelCase__     (	self  :					Tuple				):
           lowerCAmelCase					:	List[str]       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					Dict				):
           pass






     def      lowerCamelCase__     (	self  :					int				):
           pass






     @unittest.skip(reason='''Blip does not use inputs_embeds'''				)
     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           pass






     @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''				)
     def      lowerCamelCase__     (	self  :					Tuple				):
           pass






     @unittest.skip(reason='''BlipTextModel has no base class and is not available in MODEL_MAPPING'''				)
     def      lowerCamelCase__     (	self  :					List[Any]				):
           pass






     @slow
     def      lowerCamelCase__     (	self  :					Tuple				):
           for model_name in TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase					:	str       =   TFBlipTextModel.from_pretrained(UpperCamelCase_				)
                 self.assertIsNotNone(UpperCamelCase_				)






     def      lowerCamelCase__     (	self  :					str					,				UpperCamelCase_  :					Optional[int]=True				):
           super().test_pt_tf_model_equivalence(allow_missing_keys=UpperCamelCase_				)






"""simple docstring"""





from collections import OrderedDict
from typing import Mapping

from packaging import version

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


snake_case__		:     Tuple    				=				logging.get_logger(__name__)

snake_case__		:     int    				=				{
    '''facebook/levit-128S''': '''https://huggingface.co/facebook/levit-128S/resolve/main/config.json''',
    # See all LeViT models at https://huggingface.co/models?filter=levit
}



class 		snake_case_(       a__			):
     __UpperCamelCase  =					'''levit'''






     def __init__(	self  :					str					,				UpperCamelCase_  :					Union[str, Any]=2_2_4					,				UpperCamelCase_  :					Union[str, Any]=3					,				UpperCamelCase_  :					Union[str, Any]=3					,				UpperCamelCase_  :					int=2					,				UpperCamelCase_  :					Union[str, Any]=1					,				UpperCamelCase_  :					Tuple=1_6					,				UpperCamelCase_  :					Dict=[1_2_8, 2_5_6, 3_8_4]					,				UpperCamelCase_  :					Optional[Any]=[4, 8, 1_2]					,				UpperCamelCase_  :					Dict=[4, 4, 4]					,				UpperCamelCase_  :					Any=[1_6, 1_6, 1_6]					,				UpperCamelCase_  :					str=0					,				UpperCamelCase_  :					int=[2, 2, 2]					,				UpperCamelCase_  :					Optional[Any]=[2, 2, 2]					,				UpperCamelCase_  :					str=0.02					,				**UpperCamelCase_  :					List[str]					,				):
           super().__init__(**UpperCamelCase_				)
           lowerCAmelCase					:	Tuple       =   image_size
           lowerCAmelCase					:	int       =   num_channels
           lowerCAmelCase					:	Optional[int]       =   kernel_size
           lowerCAmelCase					:	Dict       =   stride
           lowerCAmelCase					:	List[Any]       =   padding
           lowerCAmelCase					:	Dict       =   hidden_sizes
           lowerCAmelCase					:	List[str]       =   num_attention_heads
           lowerCAmelCase					:	Tuple       =   depths
           lowerCAmelCase					:	Dict       =   key_dim
           lowerCAmelCase					:	Union[str, Any]       =   drop_path_rate
           lowerCAmelCase					:	List[Any]       =   patch_size
           lowerCAmelCase					:	Tuple       =   attention_ratio
           lowerCAmelCase					:	Optional[int]       =   mlp_ratio
           lowerCAmelCase					:	Union[str, Any]       =   initializer_range
           lowerCAmelCase					:	List[str]       =   [
               ['''Subsample''', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
               ['''Subsample''', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
           ]



class 		snake_case_(       a__			):
     __UpperCamelCase  =					version.parse('''1.11'''			)






     @property
     def      lowerCamelCase__     (	self  :					Tuple				):
           return OrderedDict(
               [
                   ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
               ]				)






     @property
     def      lowerCamelCase__     (	self  :					Optional[Any]				):
           return 1E-4


