
"""simple docstring"""




# Copyright 2023 The HuggingFace Inc. team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
import importlib.metadata
import json
import os
from dataclasses import dataclass
from typing import Any, Dict, Union

from packaging import version

from ..utils import is_torch_available, logging


if is_torch_available():
    import torch


a		:       Optional[Any]     						=       logging.get_logger(__name__)







@dataclass
class     lowercase:



     def __init__(							self     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=6.0     ,				__SCREAMING_SNAKE_CASE=None     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=False     ,				__SCREAMING_SNAKE_CASE=None     ,				__SCREAMING_SNAKE_CASE="fp4"     ,				__SCREAMING_SNAKE_CASE=False     ,				**__SCREAMING_SNAKE_CASE     ,				)		->     Optional[Any]:







         """simple docstring"""



         a__		=			load_in_abit
         a__		=			load_in_abit
         a__		=			llm_inta_threshold
         a__		=			llm_inta_skip_modules
         a__		=			llm_inta_enable_fpaa_cpu_offload
         a__		=			llm_inta_has_fpaa_weight
         a__		=			bnb_abit_quant_type
         a__		=			bnb_abit_use_double_quant

         if bnb_abit_compute_dtype is None:
             a__		=			torch.floataa
         elif isinstance(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   ):
             a__		=			getattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
         elif isinstance(__SCREAMING_SNAKE_CASE     ,				torch.dtype   ):
             a__		=			bnb_abit_compute_dtype
         else:
             raise ValueError('bnb_4bit_compute_dtype must be a string or a torch.dtype'   )

         self.post_init()



     def 			lowercase__  (							self   )		->     List[Any]:







         """simple docstring"""



         if not isinstance(self.llm_inta_threshold     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('llm_int8_threshold must be a float'   )

         if self.llm_inta_skip_modules is not None and not isinstance(self.llm_inta_skip_modules     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('llm_int8_skip_modules must be a list of strings'   )
         if not isinstance(self.llm_inta_enable_fpaa_cpu_offload     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('llm_int8_enable_fp32_cpu_offload must be a boolean'   )

         if not isinstance(self.llm_inta_has_fpaa_weight     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('llm_int8_has_fp16_weight must be a boolean'   )

         if self.bnb_abit_compute_dtype is not None and not isinstance(self.bnb_abit_compute_dtype     ,				torch.dtype   ):
             raise ValueError('bnb_4bit_compute_dtype must be torch.dtype'   )

         if not isinstance(self.bnb_abit_quant_type     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('bnb_4bit_quant_type must be a string'   )

         if not isinstance(self.bnb_abit_use_double_quant     ,				__SCREAMING_SNAKE_CASE   ):
             raise ValueError('bnb_4bit_use_double_quant must be a boolean'   )

         if self.load_in_abit and not version.parse(importlib.metadata.version('bitsandbytes'   )   ) >= version.parse(
             '0.39.0'   ):
             raise ValueError(
                 '4 bit quantization requires bitsandbytes>=0.39.0 - please upgrade your bitsandbytes version'   )



     def 			lowercase__  (							self   )		->     Dict:







         """simple docstring"""



         return self.load_in_abit or self.load_in_abit



     def 			lowercase__  (							self   )		->     List[str]:







         """simple docstring"""



         if self.load_in_abit:
             return "llm_int8"
         elif self.load_in_abit and self.bnb_abit_quant_type == "fp4":
             return "fp4"
         elif self.load_in_abit and self.bnb_abit_quant_type == "nf4":
             return "nf4"
         else:
             return None



     @classmethod
     def 			lowercase__  (							cls     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				**__SCREAMING_SNAKE_CASE   )		->     Union[str, Any]:







         """simple docstring"""



         a__		=			cls(**__SCREAMING_SNAKE_CASE   )

         a__		=			[]
         for key, value in kwargs.items():
             if hasattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   ):
                 setattr(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )
                 to_remove.append(__SCREAMING_SNAKE_CASE   )
         for key in to_remove:
             kwargs.pop(__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )

         if return_unused_kwargs:
             return config, kwargs
         else:
             return config



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE   )		->     str:







         """simple docstring"""



         with open(__SCREAMING_SNAKE_CASE     ,				'w'     ,				encoding='utf-8'   ) as writer:
             a__		=			self.to_dict()
             a__		=			json.dumps(__SCREAMING_SNAKE_CASE     ,				indent=2     ,				sort_keys=__SCREAMING_SNAKE_CASE   ) + '\n'

             writer.write(__SCREAMING_SNAKE_CASE   )



     def 			lowercase__  (							self   )		->     Dict[str, Any]:







         """simple docstring"""



         a__		=			copy.deepcopy(self.__dict__   )
         a__		=			str(output['bnb_4bit_compute_dtype']   ).split('.'   )[1]

         return output



     def __repr__(							self   )		->     Dict:







         """simple docstring"""



         return f'{self.__class__.__name__} {self.to_json_string()}'



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE = True   )		->     str:







         """simple docstring"""



         if use_diff is True:
             a__		=			self.to_diff_dict()
         else:
             a__		=			self.to_dict()
         return json.dumps(__SCREAMING_SNAKE_CASE     ,				indent=2     ,				sort_keys=__SCREAMING_SNAKE_CASE   ) + "\n"




     def 			lowercase__  (							self   )		->     Dict[str, Any]:







         """simple docstring"""



         a__		=			self.to_dict()

         # get the default config dict
         a__		=			BitsAndBytesConfig().to_dict()

         a__		=			{}

         # only serialize values that differ from the default config
         for key, value in config_dict.items():
             if value != default_config_dict[key]:
                 a__		=			value

         return serializable_config_dict




"""simple docstring"""




import math







class     lowercase:



     def 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     int:







         """simple docstring"""



         a__		=			0.0
         a__		=			0.0
         for i in range(len(__SCREAMING_SNAKE_CASE   )   ):
             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 			lowercase__  (							self     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE     ,				__SCREAMING_SNAKE_CASE   )		->     list[list[int | float]]:







         """simple docstring"""



         for i in range(len(__SCREAMING_SNAKE_CASE   )   ):
             weights[j][i] += alpha * (sample[i] - weights[j][i])
         return weights




def     __magic_name__		(					)     ->						None:
    # Training Examples ( m, n )
    a__		=			[[1, 1, 0, 0], [0, 0, 0, 1], [1, 0, 0, 0], [0, 0, 1, 1]]

    # weight initialization ( n, C )
    a__		=			[[0.2, 0.6, 0.5, 0.9], [0.8, 0.4, 0.7, 0.3]]

    # training
    a__		=			SelfOrganizingMap()
    a__		=			3
    a__		=			0.5

    for _ in range(UpperCamelCase     ):
        for j in range(len(UpperCamelCase     )     ):
            # training sample
            a__		=			training_samples[j]

            # Compute the winning vector
            a__		=			self_organizing_map.get_winner(UpperCamelCase							,		UpperCamelCase     )

            # Update the winning vector
            a__		=			self_organizing_map.update(UpperCamelCase							,		UpperCamelCase							,		UpperCamelCase							,		UpperCamelCase     )

    # classify test sample
    a__		=			[0, 0, 0, 1]
    a__		=			self_organizing_map.get_winner(UpperCamelCase							,		UpperCamelCase     )

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


