





"""simple docstring"""


import copy
from typing import TYPE_CHECKING, Any, Mapping, Optional, OrderedDict

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto.configuration_auto import AutoConfig


if TYPE_CHECKING:
  from ... import PreTrainedTokenizerBase, TensorType

lowerCamelCase__				  =   logging.get_logger(__name__)



class      __SCREAMING_SNAKE_CASE    (						_UpperCAmelCase   ):
  SCREAMING_SNAKE_CASE__							:Union[str, Any]	     =					"vision-encoder-decoder"
  SCREAMING_SNAKE_CASE__							:int	     =					True

  def __init__(		self   :       List[Any]					,   **__a   :       Tuple	)		->				Union[str, Any]:
        super().__init__(**lowerCamelCase_	)
        if "encoder" not in kwargs or "decoder" not in kwargs:
              raise ValueError(
                  F'''A configuraton of type {self.model_type} cannot be instantiated because '''
                  F'''not both `encoder` and `decoder` sub-configurations are passed, but only {kwargs}'''	)

        _UpperCamelCase      :	List[str]						=       kwargs.pop("encoder"	)
        _UpperCamelCase      :	Optional[int]						=       encoder_config.pop("model_type"	)
        _UpperCamelCase      :	Any						=       kwargs.pop("decoder"	)
        _UpperCamelCase      :	List[Any]						=       decoder_config.pop("model_type"	)

        _UpperCamelCase      :	Dict						=       AutoConfig.for_model(lowerCamelCase_					,   **lowerCamelCase_	)
        _UpperCamelCase      :	Optional[int]						=       AutoConfig.for_model(lowerCamelCase_					,   **lowerCamelCase_	)
        _UpperCamelCase      :	Tuple						=       True

  @classmethod
  def    __SCREAMING_SNAKE_CASE				(		cls   :       int					,   __a   :       PretrainedConfig					,   __a   :       PretrainedConfig					,   **__a   :       List[str]	)		->				PretrainedConfig:
        logger.info("Setting `config.is_decoder=True` and `config.add_cross_attention=True` for decoder_config"	)
        _UpperCamelCase      :	Union[str, Any]						=       True
        _UpperCamelCase      :	Dict						=       True

        return cls(encoder=encoder_config.to_dict()					,   decoder=decoder_config.to_dict()					,   **lowerCamelCase_	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       Any	)		->				str:
        _UpperCamelCase      :	str						=       copy.deepcopy(self.__dict__	)
        _UpperCamelCase      :	int						=       self.encoder.to_dict()
        _UpperCamelCase      :	Tuple						=       self.decoder.to_dict()
        _UpperCamelCase      :	List[Any]						=       self.__class__.model_type
        return output





class      __SCREAMING_SNAKE_CASE    (						_UpperCAmelCase   ):
  SCREAMING_SNAKE_CASE__							:Optional[Any]	     =					version.parse("1.11"   )

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]	)		->				Mapping[str, Mapping[int, str]]:
        return OrderedDict(
            [
                ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
            ]	)

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Optional[Any]	)		->				float:
        return 1e-4


  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       str	)		->				Mapping[str, Mapping[int, str]]:
        return OrderedDict({"last_hidden_state": {0: "batch", 1: "encoder_sequence"}}	)





class      __SCREAMING_SNAKE_CASE    (						_UpperCAmelCase   ):

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Dict	)		->				Mapping[str, Mapping[int, str]]:
        _UpperCamelCase      :	List[Any]						=       OrderedDict()
        _UpperCamelCase      :	Any						=       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
        _UpperCamelCase      :	Optional[Any]						=       {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
        _UpperCamelCase      :	List[Any]						=       {0: '''batch''', 1: '''encoder_sequence'''}

        return common_inputs


  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]					,   __a   :       "PreTrainedTokenizerBase"					,   __a   :       int = -1					,   __a   :       int = -1					,   __a   :       bool = False					,   __a   :       Optional["TensorType"] = None					,   )		->				Mapping[str, Any]:
        import torch

        _UpperCamelCase      :	Dict						=       OrderedDict()

        _UpperCamelCase      :	Any						=       super().generate_dummy_inputs(
            lowerCamelCase_					,   batch_size=lowerCamelCase_					,   seq_length=lowerCamelCase_					,   is_pair=lowerCamelCase_					,   framework=lowerCamelCase_	)

        _UpperCamelCase      :	List[str]						=       dummy_input['''input_ids'''].shape
        _UpperCamelCase      :	str						=       (batch, encoder_sequence, self._config.encoder_hidden_size)
        _UpperCamelCase      :	Optional[Any]						=       dummy_input.pop("input_ids"	)
        _UpperCamelCase      :	str						=       dummy_input.pop("attention_mask"	)
        _UpperCamelCase      :	List[str]						=       torch.zeros(lowerCamelCase_	)

        return common_inputs





class      __SCREAMING_SNAKE_CASE    (						_UpperCAmelCase   ):

  @property
  def    __SCREAMING_SNAKE_CASE				(		self   :       Tuple	)		->				None:
        pass

  def    __SCREAMING_SNAKE_CASE				(		self   :       List[str]					,   __a   :       PretrainedConfig	)		->				OnnxConfig:
        return VisionEncoderDecoderEncoderOnnxConfig(lowerCamelCase_	)


  def    __SCREAMING_SNAKE_CASE				(		self   :       Union[str, Any]					,   __a   :       PretrainedConfig					,   __a   :       PretrainedConfig					,   __a   :       str = "default"	)		->				OnnxConfig:
        _UpperCamelCase      :	Union[str, Any]						=       encoder_config.hidden_size
        return VisionEncoderDecoderDecoderOnnxConfig(lowerCamelCase_					,   lowerCamelCase_	)







"""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()
