



import numpy as np
from numpy import ndarray
from scipy.optimize import Bounds, LinearConstraint, minimize




def      __UpperCamelCase     (  _lowerCAmelCase		)	->  float:



    """simple docstring"""






    return np.dot(_lowerCAmelCase				,					_lowerCAmelCase		)






class SCREAMING_SNAKE_CASE__					:




    '''simple docstring'''






    def __init__(	self,		*,
        lowerCamelCase__ = np.inf,		lowerCamelCase__ = "linear",		lowerCamelCase__ = 0.0,		):
        A			:	Optional[Any]										=       regularization
        A			:	List[str]										=       gamma
        if kernel == "linear":
            A			:	List[Any]										=       self.__linear
        elif kernel == "rbf":
            if self.gamma == 0:
                raise ValueError("""rbf kernel requires gamma"""		)
            if not isinstance(self.gamma,		(float, int)		):
                raise ValueError("""gamma must be float or int"""		)
            if not self.gamma > 0:
                raise ValueError("""gamma must be > 0"""		)
            A			:	List[Any]										=       self.__rbf
            # in the future, there could be a default value like in sklearn
            # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
            # previously it was 1/(n_features)
        else:
            A			:	Any										=       f'''Unknown kernel: {kernel}'''
            raise ValueError(lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        return np.dot(lowerCamelCase__,		lowerCamelCase__		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        return np.exp(-(self.gamma * norm_squared(vectora - vectora		))		)






    def       _lowerCAmelCase (	self,		lowerCamelCase__,		lowerCamelCase__		):
        A			:	int										=       observations
        A			:	Union[str, Any]										=       classes

        # using Wolfe's Dual to calculate w.
        # Primal problem: minimize 1/2*norm_squared(w)
        #   constraint: yn(w . xn + b) >= 1
        #
        # With l a vector
        # Dual problem: maximize sum_n(ln) -
        #       1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm))
        #   constraint: self.C >= ln >= 0
        #           and sum_n(ln*yn) = 0
        # Then we get w using w = sum_n(ln*yn*xn)
        # At the end we can get b ~= mean(yn - w . xn)
        #
        # Since we use kernels, we only need l_star to calculate b
        # and to classify observations

        ((A)					,							)			:	Optional[int]										=       np.shape(lowerCamelCase__		)

        def to_minimize(lowerCamelCase__		) -> float:
            A			:	List[Any]										=       0
            ((A)					,							)			:	Tuple										=       np.shape(lowerCamelCase__		)
            for i in range(lowerCamelCase__		):
                for j in range(lowerCamelCase__		):
                    s += (
                        candidate[i]
                        * candidate[j]
                        * classes[i]
                        * classes[j]
                        * self.kernel(observations[i],		observations[j]		)
                    )
            return 1 / 2 * s - sum(lowerCamelCase__		)

        A			:	str										=       LinearConstraint(lowerCamelCase__,		0,		0		)
        A			:	Union[str, Any]										=       Bounds(0,		self.regularization		)

        A			:	List[str]										=       minimize(
            lowerCamelCase__,		np.ones(lowerCamelCase__		),		bounds=lowerCamelCase__,		constraints=[ly_contraint]		).x
        A			:	str										=       l_star

        # calculating mean offset of separation plane to points
        A			:	Any										=       0
        for i in range(lowerCamelCase__		):
            for j in range(lowerCamelCase__		):
                s += classes[i] - classes[i] * self.optimum[i] * self.kernel(
                    observations[i],		observations[j]		)
        A			:	List[str]										=       s / n






    def       _lowerCAmelCase (	self,		lowerCamelCase__		):
        A			:	Optional[int]										=       sum(
            self.optimum[n]
            * self.classes[n]
            * self.kernel(self.observations[n],		lowerCamelCase__		)
            for n in range(len(self.classes		)		)		)
        return 1 if s + self.offset >= 0 else -1


if __name__ == "__main__":
     import doctest

     doctest.testmod()







import copy
import os
from typing import Union

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


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

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






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			int =				"altclip_text_model"






    def __init__(	self,		lowerCamelCase__=25_0002,		lowerCamelCase__=1024,		lowerCamelCase__=24,		lowerCamelCase__=16,		lowerCamelCase__=4096,		lowerCamelCase__="gelu",		lowerCamelCase__=0.1,		lowerCamelCase__=0.1,		lowerCamelCase__=514,		lowerCamelCase__=1,		lowerCamelCase__=0.02,		lowerCamelCase__=0.02,		lowerCamelCase__=1e-05,		lowerCamelCase__=1,		lowerCamelCase__=0,		lowerCamelCase__=2,		lowerCamelCase__="absolute",		lowerCamelCase__=True,		lowerCamelCase__=768,		**lowerCamelCase__,		):
        super().__init__(pad_token_id=lowerCamelCase__,		bos_token_id=lowerCamelCase__,		eos_token_id=lowerCamelCase__,		**lowerCamelCase__		)

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






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			Optional[Any] =				"altclip_vision_model"






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

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






    @classmethod
    def       _lowerCAmelCase (	cls,		lowerCamelCase__,		**lowerCamelCase__		):
        cls._set_token_in_kwargs(lowerCamelCase__		)

        A					,							A			:	Optional[Any]										=       cls.get_config_dict(lowerCamelCase__,		**lowerCamelCase__		)

        # get the vision config dict if we are loading from AltCLIPConfig
        if config_dict.get("""model_type"""		) == "altclip":
            A			:	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(lowerCamelCase__,		**lowerCamelCase__		)






class SCREAMING_SNAKE_CASE__					(		SCREAMING_SNAKE_CASE__   ):




    '''simple docstring'''






    __lowerCamelCase      :			List[Any] =				"altclip"
    __lowerCamelCase      :			List[Any] =				True






    def __init__(	self,		lowerCamelCase__=None,		lowerCamelCase__=None,		lowerCamelCase__=768,		lowerCamelCase__=2.6592,		**lowerCamelCase__		):
        # 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!).
        A			:	Dict										=       kwargs.pop("""text_config_dict""",		lowerCamelCase__		)
        A			:	str										=       kwargs.pop("""vision_config_dict""",		lowerCamelCase__		)

        super().__init__(**lowerCamelCase__		)

        # 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:
                A			:	Dict										=       {}

            # This is the complete result when using `text_config_dict`.
            A			:	str										=       AltCLIPTextConfig(**lowerCamelCase__		).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:
                        A			:	Optional[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:
                        A			:	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(lowerCamelCase__		)

            # 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:
                A			:	int										=       {}

            # This is the complete result when using `vision_config_dict`.
            A			:	Union[str, Any]										=       AltCLIPVisionConfig(**lowerCamelCase__		).to_dict()
            # convert keys to string instead of integer
            if "id2label" in _vision_config_dict:
                A			:	Optional[int]										=       {
                    str(lowerCamelCase__		): 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:
                        A			:	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:
                        A			:	Any										=       (
                            f'''`vision_config_dict` is provided which will be used to initialize `AltCLIPVisionConfig`. '''
                            f'''The value `vision_config["{key}"]` will be overriden.'''
                        )
                    logger.warning(lowerCamelCase__		)

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

        if text_config is None:
            A			:	Tuple										=       {}
            logger.info("""`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."""		)

        if vision_config is None:
            A			:	Union[str, Any]										=       {}
            logger.info("""`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."""		)

        A			:	Dict										=       AltCLIPTextConfig(**lowerCamelCase__		)
        A			:	Optional[int]										=       AltCLIPVisionConfig(**lowerCamelCase__		)

        A			:	List[str]										=       projection_dim
        A			:	Any										=       logit_scale_init_value
        A			:	Tuple										=       1.0






    @classmethod
    def       _lowerCAmelCase (	cls,		lowerCamelCase__,		lowerCamelCase__,		**lowerCamelCase__		):
        return cls(text_config=text_config.to_dict(),		vision_config=vision_config.to_dict(),		**lowerCamelCase__		)






    def       _lowerCAmelCase (	self		):
        A			:	str										=       copy.deepcopy(self.__dict__		)
        A			:	Any										=       self.text_config.to_dict()
        A			:	List[str]										=       self.vision_config.to_dict()
        A			:	Union[str, Any]										=       self.__class__.model_type
        return output


