






import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A_		:		Optional[int]		     =			logging.get_logger(__name__)

A_		:		str		     =			{
    'BridgeTower/bridgetower-base': 'https://huggingface.co/BridgeTower/bridgetower-base/blob/main/config.json',
    'BridgeTower/bridgetower-base-itm-mlm': (
        'https://huggingface.co/BridgeTower/bridgetower-base-itm-mlm/blob/main/config.json'
    ),
}




class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		List[Any]									='''bridgetower_vision_model'''





       def __init__( self  ,      _lowerCamelCase=7_6_8  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=3  ,      _lowerCamelCase=1_6  ,      _lowerCamelCase=2_8_8  ,      _lowerCamelCase=1  ,      _lowerCamelCase=1e-05  ,      _lowerCamelCase=False  ,      _lowerCamelCase=True  ,      _lowerCamelCase=False  ,      **_lowerCamelCase  ,      ):
         super().__init__(**_lowerCamelCase							)
         UpperCamelCase_: Any   				=    hidden_size
         UpperCamelCase_: Union[str, Any]   				=    num_hidden_layers
         UpperCamelCase_: List[str]   				=    num_channels
         UpperCamelCase_: int   				=    patch_size
         UpperCamelCase_: List[Any]   				=    image_size
         UpperCamelCase_: str   				=    initializer_factor
         UpperCamelCase_: str   				=    layer_norm_eps
         UpperCamelCase_: int   				=    stop_gradient
         UpperCamelCase_: int   				=    share_layernorm
         UpperCamelCase_: Union[str, Any]   				=    remove_last_layer





       @classmethod
       def       _a   ( cls  ,      _lowerCamelCase  ,      **_lowerCamelCase							):
         UpperCamelCase_: Optional[Any]   				=    cls.get_config_dict(_lowerCamelCase  ,      **_lowerCamelCase							)

         if config_dict.get('model_type'							) == "bridgetower":
           UpperCamelCase_: int   				=    config_dict['text_config']

         if "model_type" in config_dict and hasattr(cls  ,      'model_type'							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(_lowerCamelCase  ,      **_lowerCamelCase							)







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		List[str]									='''bridgetower_text_model'''





       def __init__( self  ,      _lowerCamelCase=5_0_2_6_5  ,      _lowerCamelCase=7_6_8  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=1  ,      _lowerCamelCase=3_0_7_2  ,      _lowerCamelCase="gelu"  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=0.1  ,      _lowerCamelCase=5_1_4  ,      _lowerCamelCase=1  ,      _lowerCamelCase=1e-05  ,      _lowerCamelCase=1  ,      _lowerCamelCase=0  ,      _lowerCamelCase=2  ,      _lowerCamelCase="absolute"  ,      _lowerCamelCase=True  ,      **_lowerCamelCase  ,      ):
         super().__init__(**_lowerCamelCase							)

         UpperCamelCase_: Tuple   				=    vocab_size
         UpperCamelCase_: str   				=    hidden_size
         UpperCamelCase_: List[str]   				=    num_hidden_layers
         UpperCamelCase_: Tuple   				=    num_attention_heads
         UpperCamelCase_: Optional[Any]   				=    hidden_act
         UpperCamelCase_: int   				=    initializer_factor
         UpperCamelCase_: Optional[int]   				=    intermediate_size
         UpperCamelCase_: Optional[Any]   				=    hidden_dropout_prob
         UpperCamelCase_: Dict   				=    attention_probs_dropout_prob
         UpperCamelCase_: int   				=    max_position_embeddings
         UpperCamelCase_: int   				=    type_vocab_size
         UpperCamelCase_: Optional[Any]   				=    layer_norm_eps
         UpperCamelCase_: Any   				=    position_embedding_type
         UpperCamelCase_: int   				=    use_cache
         UpperCamelCase_: Optional[Any]   				=    pad_token_id
         UpperCamelCase_: Optional[Any]   				=    bos_token_id
         UpperCamelCase_: List[str]   				=    eos_token_id





       @classmethod
       def       _a   ( cls  ,      _lowerCamelCase  ,      **_lowerCamelCase							):
         UpperCamelCase_: int   				=    cls.get_config_dict(_lowerCamelCase  ,      **_lowerCamelCase							)

         if config_dict.get('model_type'							) == "bridgetower":
           UpperCamelCase_: Dict   				=    config_dict['text_config']

         if "model_type" in config_dict and hasattr(cls  ,      'model_type'							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict['model_type']} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(_lowerCamelCase  ,      **_lowerCamelCase							)







class     _lowerCAmelCase(	UpperCAmelCase_ ):





       """simple docstring"""







       a    :		Optional[Any]									='''bridgetower'''





       def __init__( self  ,      _lowerCamelCase=True  ,      _lowerCamelCase="gelu"  ,      _lowerCamelCase=7_6_8  ,      _lowerCamelCase=1  ,      _lowerCamelCase=1e-05  ,      _lowerCamelCase=False  ,      _lowerCamelCase="add"  ,      _lowerCamelCase=1_2  ,      _lowerCamelCase=6  ,      _lowerCamelCase=False  ,      _lowerCamelCase=False  ,      _lowerCamelCase=None  ,      _lowerCamelCase=None  ,      **_lowerCamelCase  ,      ):
         # TODO: remove this once the Hub files are updated.
         UpperCamelCase_: Optional[int]   				=    kwargs.pop('text_config_dict'  ,      _lowerCamelCase							)
         UpperCamelCase_: Union[str, Any]   				=    kwargs.pop('vision_config_dict'  ,      _lowerCamelCase							)

         super().__init__(**_lowerCamelCase							)
         UpperCamelCase_: List[Any]   				=    share_cross_modal_transformer_layers
         UpperCamelCase_: List[str]   				=    hidden_act
         UpperCamelCase_: Optional[Any]   				=    hidden_size
         UpperCamelCase_: Optional[Any]   				=    initializer_factor
         UpperCamelCase_: Any   				=    layer_norm_eps
         UpperCamelCase_: Dict   				=    share_link_tower_layers
         UpperCamelCase_: int   				=    link_tower_type
         UpperCamelCase_: Union[str, Any]   				=    num_attention_heads
         UpperCamelCase_: Optional[int]   				=    num_hidden_layers
         UpperCamelCase_: Tuple   				=    tie_word_embeddings
         UpperCamelCase_: Optional[int]   				=    init_layernorm_from_vision_encoder

         if text_config is None:
           UpperCamelCase_: Any   				=    {}
           logger.info('`text_config` is `None`. Initializing the `BridgeTowerTextConfig` with default values.'							)

         if vision_config is None:
           UpperCamelCase_: Any   				=    {}
           logger.info('`vision_config` is `None`. Initializing the `BridgeTowerVisionConfig` with default values.'							)

         UpperCamelCase_: Optional[Any]   				=    BridgeTowerTextConfig(**_lowerCamelCase							)
         UpperCamelCase_: Optional[Any]   				=    BridgeTowerVisionConfig(**_lowerCamelCase							)





       @classmethod
       def       _a   ( cls  ,      _lowerCamelCase  ,      _lowerCamelCase  ,      **_lowerCamelCase							):
         return cls(text_config=text_config.to_dict()  ,      vision_config=vision_config.to_dict()  ,      **_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: Tuple   				=    copy.deepcopy(self.__dict__							)
         UpperCamelCase_: Optional[Any]   				=    self.text_config.to_dict()
         UpperCamelCase_: Union[str, Any]   				=    self.vision_config.to_dict()
         UpperCamelCase_: Tuple   				=    self.__class__.model_type
         return output









import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
   import torch

   from transformers.activations import gelu_new, gelu_python, get_activation




@require_torch
class     _lowerCAmelCase(	unittest.TestCase ):





       """simple docstring"""







       def       _a   ( self							):
         UpperCamelCase_: Any   				=    torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0]							)
         UpperCamelCase_: List[str]   				=    get_activation('gelu'							)
         self.assertTrue(torch.allclose(gelu_python(_lowerCamelCase							)  ,      torch_builtin(_lowerCamelCase							)							)							)
         self.assertFalse(torch.allclose(gelu_python(_lowerCamelCase							)  ,      gelu_new(_lowerCamelCase							)							)							)





       def       _a   ( self							):
         UpperCamelCase_: Optional[Any]   				=    torch.tensor([-1_0_0, -1, -0.1, 0, 0.1, 1.0, 1_0_0]							)
         UpperCamelCase_: Union[str, Any]   				=    get_activation('gelu'							)
         UpperCamelCase_: int   				=    get_activation('gelu_10'							)

         UpperCamelCase_: Union[str, Any]   				=    torch_builtin(_lowerCamelCase							)
         UpperCamelCase_: List[str]   				=    geluaa(_lowerCamelCase							)

         UpperCamelCase_: Dict   				=    torch.where(y_gelu_aa < 1_0.0  ,      1  ,      0							)

         self.assertTrue(torch.max(_lowerCamelCase							).item() == 1_0.0							)
         self.assertTrue(torch.allclose(y_gelu * clipped_mask  ,      y_gelu_aa * clipped_mask							)							)





       def       _a   ( self							):
         get_activation('gelu'							)
         get_activation('gelu_10'							)
         get_activation('gelu_fast'							)
         get_activation('gelu_new'							)
         get_activation('gelu_python'							)
         get_activation('gelu_pytorch_tanh'							)
         get_activation('linear'							)
         get_activation('mish'							)
         get_activation('quick_gelu'							)
         get_activation('relu'							)
         get_activation('sigmoid'							)
         get_activation('silu'							)
         get_activation('swish'							)
         get_activation('tanh'							)
         with self.assertRaises(_lowerCamelCase							):
           get_activation('bogus'							)
         with self.assertRaises(_lowerCamelCase							):
           get_activation(_lowerCamelCase							)





       def       _a   ( self							):
         UpperCamelCase_: str   				=    get_activation('gelu'							)
         UpperCamelCase_: str   				=    1
         UpperCamelCase_: int   				=    get_activation('gelu'							)
         self.assertEqual(acta.a  ,      1							)
         with self.assertRaises(_lowerCamelCase							):
           UpperCamelCase_: Tuple   				=    acta.a