




import copy
import os
from typing import Union

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


__a							     =					logging.get_logger(__name__)

__a							     =					{
    'BAAI/AltCLIP': 'https://huggingface.co/BAAI/AltCLIP/resolve/main/config.json',
    # See all AltCLIP models at https://huggingface.co/models?filter=altclip
}

class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''altclip_text_model'''





    def __init__( self  ,_SCREAMING_SNAKE_CASE=250_002  ,_SCREAMING_SNAKE_CASE=1_024  ,_SCREAMING_SNAKE_CASE=24  ,_SCREAMING_SNAKE_CASE=16  ,_SCREAMING_SNAKE_CASE=4_096  ,_SCREAMING_SNAKE_CASE="gelu"  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=0.1  ,_SCREAMING_SNAKE_CASE=514  ,_SCREAMING_SNAKE_CASE=1  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=1e-05  ,_SCREAMING_SNAKE_CASE=1  ,_SCREAMING_SNAKE_CASE=0  ,_SCREAMING_SNAKE_CASE=2  ,_SCREAMING_SNAKE_CASE="absolute"  ,_SCREAMING_SNAKE_CASE=True  ,_SCREAMING_SNAKE_CASE=768  ,**_SCREAMING_SNAKE_CASE  ,)					->						Optional[int]:
         super().__init__(pad_token_id=_SCREAMING_SNAKE_CASE  ,bos_token_id=_SCREAMING_SNAKE_CASE  ,eos_token_id=_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )

         UpperCAmelCase_     :				Union[str, Any]  =     vocab_size
         UpperCAmelCase_     :				Optional[Any]  =     hidden_size
         UpperCAmelCase_     :				List[Any]  =     num_hidden_layers
         UpperCAmelCase_     :				List[Any]  =     num_attention_heads
         UpperCAmelCase_     :				Any  =     hidden_act
         UpperCAmelCase_     :				Optional[Any]  =     intermediate_size
         UpperCAmelCase_     :				Optional[Any]  =     hidden_dropout_prob
         UpperCAmelCase_     :				Optional[Any]  =     attention_probs_dropout_prob
         UpperCAmelCase_     :				Any  =     max_position_embeddings
         UpperCAmelCase_     :				Any  =     type_vocab_size
         UpperCAmelCase_     :				Dict  =     initializer_range
         UpperCAmelCase_     :				Union[str, Any]  =     initializer_factor
         UpperCAmelCase_     :				Tuple  =     layer_norm_eps
         UpperCAmelCase_     :				Union[str, Any]  =     position_embedding_type
         UpperCAmelCase_     :				List[Any]  =     use_cache
         UpperCAmelCase_     :				str  =     project_dim


class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''altclip_vision_model'''





    def __init__( self  ,_SCREAMING_SNAKE_CASE=768  ,_SCREAMING_SNAKE_CASE=3_072  ,_SCREAMING_SNAKE_CASE=512  ,_SCREAMING_SNAKE_CASE=12  ,_SCREAMING_SNAKE_CASE=12  ,_SCREAMING_SNAKE_CASE=3  ,_SCREAMING_SNAKE_CASE=224  ,_SCREAMING_SNAKE_CASE=32  ,_SCREAMING_SNAKE_CASE="quick_gelu"  ,_SCREAMING_SNAKE_CASE=1e-5  ,_SCREAMING_SNAKE_CASE=0.0  ,_SCREAMING_SNAKE_CASE=0.02  ,_SCREAMING_SNAKE_CASE=1.0  ,**_SCREAMING_SNAKE_CASE  ,)					->						int:
         super().__init__(**_SCREAMING_SNAKE_CASE    )

         UpperCAmelCase_     :				Optional[Any]  =     hidden_size
         UpperCAmelCase_     :				Dict  =     intermediate_size
         UpperCAmelCase_     :				str  =     projection_dim
         UpperCAmelCase_     :				Tuple  =     num_hidden_layers
         UpperCAmelCase_     :				str  =     num_attention_heads
         UpperCAmelCase_     :				Optional[Any]  =     num_channels
         UpperCAmelCase_     :				Union[str, Any]  =     patch_size
         UpperCAmelCase_     :				Union[str, Any]  =     image_size
         UpperCAmelCase_     :				Any  =     initializer_range
         UpperCAmelCase_     :				Union[str, Any]  =     initializer_factor
         UpperCAmelCase_     :				Optional[Any]  =     attention_dropout
         UpperCAmelCase_     :				Any  =     layer_norm_eps
         UpperCAmelCase_     :				List[Any]  =     hidden_act





    @classmethod
    def 				a__ ( cls  ,_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )					->						"PretrainedConfig":
         cls._set_token_in_kwargs(_SCREAMING_SNAKE_CASE    )

         UpperCAmelCase_,       UpperCAmelCase_     :				Union[str, Any]  =     cls.get_config_dict(_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )

         # get the vision config dict if we are loading from AltCLIPConfig
         if config_dict.get('''model_type'''    ) == "altclip":
              UpperCAmelCase_     :				List[Any]  =     config_dict['''vision_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(_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )


class     __a(	_a  ):






    """simple docstring"""





    lowerCAmelCase         = '''altclip'''
    lowerCAmelCase         = True





    def __init__( self  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=None  ,_SCREAMING_SNAKE_CASE=768  ,_SCREAMING_SNAKE_CASE=2.65_92  ,**_SCREAMING_SNAKE_CASE    )					->						Dict:
         # If `_config_dict` exist, we use them for the backward compatibility.
         # We pop out these 2 attributes before calling `super().__init__` to avoid them being saved (which causes a lot
         # of confusion!).
         UpperCAmelCase_     :				Optional[int]  =     kwargs.pop('''text_config_dict'''  ,_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				List[str]  =     kwargs.pop('''vision_config_dict'''  ,_SCREAMING_SNAKE_CASE    )

         super().__init__(**_SCREAMING_SNAKE_CASE    )

         # Instead of simply assigning `[text|vision]_config_dict` to `[text|vision]_config`, we use the values in
         # `[text|vision]_config_dict` to update the values in `[text|vision]_config`. The values should be same in most
         # cases, but we don't want to break anything regarding `_config_dict` that existed before commit `8827e1b2`.
         if text_config_dict is not None:
              if text_config is None:
                   UpperCAmelCase_     :				Optional[Any]  =     {}

              # This is the complete result when using `text_config_dict`.
              UpperCAmelCase_     :				Optional[Any]  =     AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE    ).to_dict()

              # Give a warning if the values exist in both `_text_config_dict` and `text_config` but being different.
              for key, value in _text_config_dict.items():
                   if key in text_config and value != text_config[key] and key not in ["transformers_version"]:
                        # If specified in `text_config_dict`
                        if key in text_config_dict:
                             UpperCAmelCase_     :				Any  =     (
                                 f'''`{key}` is found in both `text_config_dict` and `text_config` but with different values. '''
                                 f'''The value `text_config_dict["{key}"]` will be used instead.'''
                             )
                        # If inferred from default argument values (just to be super careful)
                        else:
                             UpperCAmelCase_     :				Optional[int]  =     (
                                 f'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
                                 f'''value `text_config["{key}"]` will be overriden.'''
                             )
                        logger.warning(_SCREAMING_SNAKE_CASE    )

            # Update all values in `text_config` with the ones in `_text_config_dict`.
              text_config.update(_text_config_dict    )

         if vision_config_dict is not None:
              if vision_config is None:
                   UpperCAmelCase_     :				Dict  =     {}

              # This is the complete result when using `vision_config_dict`.
              UpperCAmelCase_     :				Tuple  =     AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE    ).to_dict()
              # convert keys to string instead of integer
              if "id2label" in _vision_config_dict:
                   UpperCAmelCase_     :				str  =     {
                       str(_SCREAMING_SNAKE_CASE    ): value for key, value in _vision_config_dict['''id2label'''].items()
                   }

              # Give a warning if the values exist in both `_vision_config_dict` and `vision_config` but being different.
              for key, value in _vision_config_dict.items():
                   if key in vision_config and value != vision_config[key] and key not in ["transformers_version"]:
                        # If specified in `vision_config_dict`
                        if key in vision_config_dict:
                             UpperCAmelCase_     :				Optional[int]  =     (
                                 f'''`{key}` is found in both `vision_config_dict` and `vision_config` but with different '''
                                 f'''values. The value `vision_config_dict["{key}"]` will be used instead.'''
                             )
                        # If inferred from default argument values (just to be super careful)
                        else:
                             UpperCAmelCase_     :				str  =     (
                                 f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
                                 f'''The value `vision_config["{key}"]` will be overriden.'''
                             )
                        logger.warning(_SCREAMING_SNAKE_CASE    )

            # Update all values in `vision_config` with the ones in `_vision_config_dict`.
              vision_config.update(_vision_config_dict    )

         if text_config is None:
              UpperCAmelCase_     :				Union[str, Any]  =     {}
              logger.info('''`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values.'''    )

         if vision_config is None:
              UpperCAmelCase_     :				List[Any]  =     {}
              logger.info('''`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values.'''    )

         UpperCAmelCase_     :				List[Any]  =     AltCLIPTextConfig(**_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Optional[int]  =     AltCLIPVisionConfig(**_SCREAMING_SNAKE_CASE    )

         UpperCAmelCase_     :				Optional[int]  =     projection_dim
         UpperCAmelCase_     :				List[str]  =     logit_scale_init_value
         UpperCAmelCase_     :				List[str]  =     1.0





    @classmethod
    def 				a__ ( cls  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,**_SCREAMING_SNAKE_CASE    )					->						Optional[Any]:
         return cls(text_config=text_config.to_dict()  ,vision_config=vision_config.to_dict()  ,**_SCREAMING_SNAKE_CASE    )





    def 				a__ ( self    )					->						Union[str, Any]:
         UpperCAmelCase_     :				Dict  =     copy.deepcopy(self.__dict__    )
         UpperCAmelCase_     :				Tuple  =     self.text_config.to_dict()
         UpperCAmelCase_     :				Optional[int]  =     self.vision_config.to_dict()
         UpperCAmelCase_     :				str  =     self.__class__.model_type
         return output





import unittest

import numpy as np







def lowerCamelCase__  (     _lowercase					,		_lowercase					,		_lowercase					,		_lowercase = None					,		):

     '''simple docstring'''


     UpperCAmelCase_     :				Dict  =     np.shape(_lowercase )
     UpperCAmelCase_     :				Optional[Any]  =     np.shape(_lowercase )
     UpperCAmelCase_     :				Tuple  =     np.shape(_lowercase )

     if shape_a[0] != shape_b[0]:
          UpperCAmelCase_     :				Tuple  =     (
              '''Expected the same number of rows for A and B. '''
              f'''Instead found A of size {shape_a} and B of size {shape_b}'''
          )
          raise ValueError(_lowercase )

     if shape_b[1] != shape_c[1]:
          UpperCAmelCase_     :				List[Any]  =     (
              '''Expected the same number of columns for B and C. '''
              f'''Instead found B of size {shape_b} and C of size {shape_c}'''
          )
          raise ValueError(_lowercase )

     UpperCAmelCase_     :				Dict  =     pseudo_inv
     if a_inv is None:
          try:
               UpperCAmelCase_     :				Any  =     np.linalg.inv(_lowercase )
          except np.linalg.LinAlgError:
               raise ValueError(
                   '''Input matrix A is not invertible. Cannot compute Schur complement.''' )

     return mat_c - mat_b.T @ a_inv @ mat_b

class     __a(	unittest.TestCase  ):






    """simple docstring"""





    def 				a__ ( self    )					->						None:
         UpperCAmelCase_     :				str  =     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]    )
         UpperCAmelCase_     :				Any  =     np.array([[0, 3], [3, 0], [2, 3]]    )
         UpperCAmelCase_     :				List[str]  =     np.array([[2, 1], [6, 3]]    )

         UpperCAmelCase_     :				Tuple  =     schur_complement(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )

         UpperCAmelCase_     :				List[Any]  =     np.block([[a, b], [b.T, c]]    )

         UpperCAmelCase_     :				List[Any]  =     np.linalg.det(_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				List[Any]  =     np.linalg.det(_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Any  =     np.linalg.det(_SCREAMING_SNAKE_CASE    )

         self.assertAlmostEqual(_SCREAMING_SNAKE_CASE  ,det_a * det_s    )





    def 				a__ ( self    )					->						None:
         UpperCAmelCase_     :				str  =     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]    )
         UpperCAmelCase_     :				Optional[int]  =     np.array([[0, 3], [3, 0], [2, 3]]    )
         UpperCAmelCase_     :				Optional[int]  =     np.array([[2, 1], [6, 3]]    )

         with self.assertRaises(_SCREAMING_SNAKE_CASE    ):
              schur_complement(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )





    def 				a__ ( self    )					->						None:
         UpperCAmelCase_     :				Optional[int]  =     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]    )
         UpperCAmelCase_     :				Optional[Any]  =     np.array([[0, 3], [3, 0], [2, 3]]    )
         UpperCAmelCase_     :				int  =     np.array([[2, 1, 3], [6, 3, 5]]    )

         with self.assertRaises(_SCREAMING_SNAKE_CASE    ):
              schur_complement(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     unittest.main()