





"""simple docstring"""


import copy
import os
from typing import Union

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


lowerCamelCase__				  =   logging.get_logger(__name__)

lowerCamelCase__				  =   {
    "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    (						_UpperCamelCase   ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:List[str]	     =					"altclip_text_model"

  def __init__(		self   :       List[Any]					,   __a   :       Union[str, Any]=25_0002					,   __a   :       Optional[int]=1024					,   __a   :       str=24					,   __a   :       Any=16					,   __a   :       List[str]=4096					,   __a   :       str="gelu"					,   __a   :       List[str]=0.1					,   __a   :       str=0.1					,   __a   :       List[str]=514					,   __a   :       int=1					,   __a   :       Tuple=0.02					,   __a   :       str=0.02					,   __a   :       Any=1e-0_5					,   __a   :       List[Any]=1					,   __a   :       List[Any]=0					,   __a   :       Optional[int]=2					,   __a   :       str="absolute"					,   __a   :       Optional[Any]=True					,   __a   :       Tuple=768					,   **__a   :       Optional[Any]					,   )		->				Optional[int]:
        super().__init__(pad_token_id=__a					,   bos_token_id=__a					,   eos_token_id=__a					,   **__a	)

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





class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase   ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:List[str]	     =					"altclip_vision_model"

  def __init__(		self   :       List[str]					,   __a   :       List[str]=768					,   __a   :       str=3072					,   __a   :       List[Any]=512					,   __a   :       List[str]=12					,   __a   :       Tuple=12					,   __a   :       Dict=3					,   __a   :       List[str]=224					,   __a   :       List[str]=32					,   __a   :       List[Any]="quick_gelu"					,   __a   :       List[Any]=1e-5					,   __a   :       List[Any]=0.0					,   __a   :       str=0.02					,   __a   :       Optional[Any]=1.0					,   **__a   :       Any					,   )		->				Any:
        super().__init__(**__a	)

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


  @classmethod
  def    __SCREAMING_SNAKE_CASE				(		cls   :       List[str]					,   __a   :       Union[str, os.PathLike]					,   **__a   :       List[str]	)		->				"PretrainedConfig":
        cls._set_token_in_kwargs(__a	)

        _UpperCamelCase,  _UpperCamelCase      :	int						=       cls.get_config_dict(__a					,   **__a	)

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





class      __SCREAMING_SNAKE_CASE    (						_UpperCamelCase   ):







  '''simple docstring'''


  SCREAMING_SNAKE_CASE__							:Any	     =					"altclip"
  SCREAMING_SNAKE_CASE__							:List[str]	     =					True

  def __init__(		self   :       Optional[Any]					,   __a   :       Dict=None					,   __a   :       List[str]=None					,   __a   :       List[str]=768					,   __a   :       Optional[int]=2.65_92					,   **__a   :       Union[str, Any]	)		->				List[Any]:
        # 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      :	Tuple						=       kwargs.pop("text_config_dict"					,   __a	)
        _UpperCamelCase      :	Tuple						=       kwargs.pop("vision_config_dict"					,   __a	)

        super().__init__(**__a	)

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

              # This is the complete result when using `text_config_dict`.
              _UpperCamelCase      :	Any						=       AltCLIPTextConfig(**__a	).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      :	Optional[int]						=       (
                                    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      :	Union[str, Any]						=       (
                                    F'''`text_config_dict` is provided which will be used to initialize `AltCLIPTextConfig`. The '''
                                    F'''value `text_config["{key}"]` will be overriden.'''
                                )
                          logger.warning(__a	)

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

              # This is the complete result when using `vision_config_dict`.
              _UpperCamelCase      :	List[Any]						=       AltCLIPVisionConfig(**__a	).to_dict()
              # convert keys to string instead of integer
              if "id2label" in _vision_config_dict:
                    _UpperCamelCase      :	Dict						=       {
                        str(__a	): 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      :	Optional[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(__a	)

            # 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      :	Optional[Any]						=       {}
              logger.info("`text_config` is `None`. Initializing the `AltCLIPTextConfig` with default values."	)

        if vision_config is None:
              _UpperCamelCase      :	Dict						=       {}
              logger.info("`vision_config` is `None`. initializing the `AltCLIPVisionConfig` with default values."	)

        _UpperCamelCase      :	Union[str, Any]						=       AltCLIPTextConfig(**__a	)
        _UpperCamelCase      :	Dict						=       AltCLIPVisionConfig(**__a	)

        _UpperCamelCase      :	Any						=       projection_dim
        _UpperCamelCase      :	Union[str, Any]						=       logit_scale_init_value
        _UpperCamelCase      :	Optional[Any]						=       1.0

  @classmethod
  def    __SCREAMING_SNAKE_CASE				(		cls   :       Optional[Any]					,   __a   :       AltCLIPTextConfig					,   __a   :       AltCLIPVisionConfig					,   **__a   :       List[str]	)		->				Optional[Any]:
        return cls(text_config=text_config.to_dict()					,   vision_config=vision_config.to_dict()					,   **__a	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       List[Any]	)		->				Any:
        _UpperCamelCase      :	Union[str, Any]						=       copy.deepcopy(self.__dict__	)
        _UpperCamelCase      :	List[str]						=       self.text_config.to_dict()
        _UpperCamelCase      :	int						=       self.vision_config.to_dict()
        _UpperCamelCase      :	Optional[int]						=       self.__class__.model_type
        return output







"""simple docstring"""


import math



class      __SCREAMING_SNAKE_CASE    :







  '''simple docstring'''


  def    __SCREAMING_SNAKE_CASE				(		self   :       Any					,   __a   :       list[list[float]]					,   __a   :       list[int]	)		->				int:
        _UpperCamelCase      :	List[Any]						=       0.0
        _UpperCamelCase      :	Union[str, Any]						=       0.0
        for i in range(len(__a	)	):
              da += math.pow((sample[i] - weights[0][i])					,   2	)
              da += math.pow((sample[i] - weights[1][i])					,   2	)
              return 0 if da > da else 1
        return 0


  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]					,   __a   :       list[list[int | float]]					,   __a   :       list[int]					,   __a   :       int					,   __a   :       float	)		->				list[list[int | float]]:
        for i in range(len(__a	)	):
              weights[j][i] += alpha * (sample[i] - weights[j][i])
        return weights





def 					lowercase__    (					)					->		None:




      """simple docstring"""

      _UpperCamelCase      :	Optional[int]						=       [[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

      # weight initialization ( n, C )
      _UpperCamelCase      :	List[str]						=       [[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

      # training
      _UpperCamelCase      :	List[Any]						=       SelfOrganizingMap()
      _UpperCamelCase      :	int						=       3
      _UpperCamelCase      :	List[Any]						=       0.5

      for _ in range(lowercase_			):
            for j in range(len(lowercase_			)			):
                  # training sample
                  _UpperCamelCase      :	int						=       training_samples[j]

                  # Compute the winning vector
                  _UpperCamelCase      :	Tuple						=       self_organizing_map.get_winner(lowercase_		,lowercase_			)

                  # Update the winning vector
                  _UpperCamelCase      :	int						=       self_organizing_map.update(lowercase_		,lowercase_		,lowercase_		,lowercase_			)

    # classify test sample
      _UpperCamelCase      :	Optional[int]						=       [0, 0, 0, 1]
      _UpperCamelCase      :	Dict						=       self_organizing_map.get_winner(lowercase_		,lowercase_			)

      # results
      print(F'''Clusters that the test sample belongs to : {winner}'''			)
      print(F'''Weights that have been trained : {weights}'''			)


# running the main() function
if __name__ == "__main__":
  main()
