



# 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


_lowerCamelCase						    =  logging.get_logger(__name__)

@dataclass
class       _SCREAMING_SNAKE_CASE	:





     def __init__(       self :							List[Any]     ,  UpperCamelCase :							Dict=False     ,  UpperCamelCase :							List[str]=False     ,  UpperCamelCase :							Union[str, Any]=6.0     ,  UpperCamelCase :							List[Any]=None     ,  UpperCamelCase :							str=False     ,  UpperCamelCase :							Union[str, Any]=False     ,  UpperCamelCase :							Union[str, Any]=None     ,  UpperCamelCase :							Dict="fp4"     ,  UpperCamelCase :							List[str]=False     ,  **UpperCamelCase :							List[str]     ,  )->Optional[int]:
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	load_in_abit
      __SCREAMING_SNAKE_CASE       :    Optional[int]       =	load_in_abit
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	llm_inta_threshold
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	llm_inta_skip_modules
      __SCREAMING_SNAKE_CASE       :    int       =	llm_inta_enable_fpaa_cpu_offload
      __SCREAMING_SNAKE_CASE       :    Tuple       =	llm_inta_has_fpaa_weight
      __SCREAMING_SNAKE_CASE       :    str       =	bnb_abit_quant_type
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	bnb_abit_use_double_quant

      if bnb_abit_compute_dtype is None:
       __SCREAMING_SNAKE_CASE       :    List[Any]       =	torch.floataa
      elif isinstance(UpperCamelCase     ,  UpperCamelCase				):
       __SCREAMING_SNAKE_CASE       :    Dict       =	getattr(UpperCamelCase     ,  UpperCamelCase				)
      elif isinstance(UpperCamelCase     ,  torch.dtype				):
       __SCREAMING_SNAKE_CASE       :    Dict       =	bnb_abit_compute_dtype
      else:
       raise ValueError("bnb_4bit_compute_dtype must be a string or a torch.dtype"				)

      self.post_init()





     def 	__snake_case    (       self :							Optional[Any]				)->Any:
      if not isinstance(self.llm_inta_threshold     ,  UpperCamelCase				):
       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     ,  UpperCamelCase				):
       raise ValueError("llm_int8_skip_modules must be a list of strings"				)
      if not isinstance(self.llm_inta_enable_fpaa_cpu_offload     ,  UpperCamelCase				):
       raise ValueError("llm_int8_enable_fp32_cpu_offload must be a boolean"				)

      if not isinstance(self.llm_inta_has_fpaa_weight     ,  UpperCamelCase				):
       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     ,  UpperCamelCase				):
       raise ValueError("bnb_4bit_quant_type must be a string"				)

      if not isinstance(self.bnb_abit_use_double_quant     ,  UpperCamelCase				):
       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 	__snake_case    (       self :							List[str]				)->Optional[Any]:
      return self.load_in_abit or self.load_in_abit





     def 	__snake_case    (       self :							List[str]				)->Any:
      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 	__snake_case    (       cls :							int     ,  UpperCamelCase :							str     ,  UpperCamelCase :							Union[str, Any]     ,  **UpperCamelCase :							List[str]				)->Any:
      __SCREAMING_SNAKE_CASE       :    List[Any]       =	cls(**UpperCamelCase				)

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

      if return_unused_kwargs:
       return config, kwargs
      else:
       return config





     def 	__snake_case    (       self :							Any     ,  UpperCamelCase :							Union[str, os.PathLike]				)->List[str]:
      with open(UpperCamelCase     ,  "w"     ,  encoding="utf-8"				) as writer:
       __SCREAMING_SNAKE_CASE       :    Any       =	self.to_dict()
       __SCREAMING_SNAKE_CASE       :    int       =	json.dumps(UpperCamelCase     ,  indent=2     ,  sort_keys=UpperCamelCase				) + "\n"

       writer.write(UpperCamelCase				)





     def 	__snake_case    (       self :							List[Any]				)->Dict[str, Any]:
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	copy.deepcopy(self.__dict__				)
      __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	str(output["bnb_4bit_compute_dtype"]				).split("."				)[1]

      return output





     def __repr__(       self :							Any				)->Union[str, Any]:
      return F"""{self.__class__.__name__} {self.to_json_string()}"""





     def 	__snake_case    (       self :							Optional[int]     ,  UpperCamelCase :							bool = True				)->str:
      if use_diff is True:
       __SCREAMING_SNAKE_CASE       :    List[str]       =	self.to_diff_dict()
      else:
       __SCREAMING_SNAKE_CASE       :    int       =	self.to_dict()
      return json.dumps(UpperCamelCase     ,  indent=2     ,  sort_keys=UpperCamelCase				) + "\n"






     def 	__snake_case    (       self :							int				)->Dict[str, Any]:
      __SCREAMING_SNAKE_CASE       :    Dict       =	self.to_dict()

      # get the default config dict
      __SCREAMING_SNAKE_CASE       :    int       =	BitsAndBytesConfig().to_dict()

      __SCREAMING_SNAKE_CASE       :    int       =	{}

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

      return serializable_config_dict











from __future__ import annotations

import math
from collections import Counter
from string import ascii_lowercase






def 	_lowerCAmelCase						(				__lowerCamelCase   :     str     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE,		__SCREAMING_SNAKE_CASE       :    Tuple       =	analyze_text(__lowerCamelCase     )
 __SCREAMING_SNAKE_CASE       :    List[Any]       =	list(" " + ascii_lowercase     )
 # what is our total sum of probabilities.
 __SCREAMING_SNAKE_CASE       :    int       =	sum(single_char_strings.values()     )

 # one length string
 __SCREAMING_SNAKE_CASE       :    str       =	0
 # for each alpha we go in our dict and if it is in it we calculate entropy
 for ch in my_alphas:
  if ch in single_char_strings:
   __SCREAMING_SNAKE_CASE       :    List[Any]       =	single_char_strings[ch]
   __SCREAMING_SNAKE_CASE       :    str       =	my_str / all_sum
   my_fir_sum += prob * math.loga(__lowerCamelCase     )  # entropy formula.

    # print entropy
 print(F"""{round(-1 * my_fir_sum     ):.1f}"""     )

 # two len string
 __SCREAMING_SNAKE_CASE       :    Optional[int]       =	sum(two_char_strings.values()     )
 __SCREAMING_SNAKE_CASE       :    int       =	0
 # for each alpha (two in size) calculate entropy.
 for cha in my_alphas:
  for cha in my_alphas:
   __SCREAMING_SNAKE_CASE       :    int       =	cha + cha
   if sequence in two_char_strings:
    __SCREAMING_SNAKE_CASE       :    Optional[Any]       =	two_char_strings[sequence]
    __SCREAMING_SNAKE_CASE       :    Union[str, Any]       =	int(__lowerCamelCase     ) / all_sum
    my_sec_sum += prob * math.loga(__lowerCamelCase     )

    # print second entropy
 print(F"""{round(-1 * my_sec_sum     ):.1f}"""     )

 # print the difference between them
 print(F"""{round((-1 * my_sec_sum) - (-1 * my_fir_sum)     ):.1f}"""     )






def 	_lowerCAmelCase						(				__lowerCamelCase   :     str     ):



 """simple docstring"""





 __SCREAMING_SNAKE_CASE       :    Any       =	Counter()  # type: ignore
 __SCREAMING_SNAKE_CASE       :    int       =	Counter()  # type: ignore
 single_char_strings[text[-1]] += 1

 # first case when we have space at start.
 two_char_strings[" " + text[0]] += 1
 for i in range(0		,	len(__lowerCamelCase     ) - 1     ):
  single_char_strings[text[i]] += 1
  two_char_strings[text[i : i + 2]] += 1
 return single_char_strings, two_char_strings






def 	_lowerCAmelCase						(				):



 """simple docstring"""





 import doctest

 doctest.testmod()
 # text = (
 #     "Had repulsive dashwoods suspicion sincerity but advantage now him. Remark "
 #     "easily garret nor nay. Civil those mrs enjoy shy fat merry. You greatest "
 #     "jointure saw horrible. He private he on be imagine suppose. Fertile "
 #     "beloved evident through no service elderly is. Blind there if every no so "
 #     "at. Own neglected you preferred way sincerity delivered his attempted. To "
 #     "of message cottage windows do besides against uncivil.  Delightful "
 #     "unreserved impossible few estimating men favourable see entreaties. She "
 #     "propriety immediate was improving. He or entrance humoured likewise "
 #     "moderate. Much nor game son say feel. Fat make met can must form into "
 #     "gate. Me we offending prevailed discovery. "
 # )

 # calculate_prob(text)


if __name__ == "__main__":
      main()






