
"""simple docstring"""



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       :						Dict       		=		logging.get_logger(__name__)




@dataclass
class 		a_    :







    def __init__(      self      :							Union[str, Any]  ,       __UpperCamelCase      :							Optional[Any]=False  ,       __UpperCamelCase      :							Optional[Any]=False  ,       __UpperCamelCase      :							str=6.0  ,       __UpperCamelCase      :							int=None  ,       __UpperCamelCase      :							Dict=False  ,       __UpperCamelCase      :							Union[str, Any]=False  ,       __UpperCamelCase      :							str=None  ,       __UpperCamelCase      :							int="fp4"  ,       __UpperCamelCase      :							List[Any]=False  ,       **__UpperCamelCase      :							str  ,       )		->Optional[int]:






        '''simple docstring'''






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

        if bnb_abit_compute_dtype is None:
            _UpperCAmelCase        = torch.floataa
        elif isinstance(lowerCAmelCase_  ,       lowerCAmelCase_  ):
            _UpperCAmelCase        = getattr(lowerCAmelCase_  ,       lowerCAmelCase_  )
        elif isinstance(lowerCAmelCase_  ,       torch.dtype  ):
            _UpperCAmelCase        = 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      :							List[str]  )		->Optional[Any]:






        '''simple docstring'''






        if not isinstance(self.llm_inta_threshold  ,       lowerCAmelCase_  ):
            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  ,       lowerCAmelCase_  ):
            raise ValueError("""llm_int8_skip_modules must be a list of strings"""  )
        if not isinstance(self.llm_inta_enable_fpaa_cpu_offload  ,       lowerCAmelCase_  ):
            raise ValueError("""llm_int8_enable_fp32_cpu_offload must be a boolean"""  )

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

        if not isinstance(self.bnb_abit_use_double_quant  ,       lowerCAmelCase_  ):
            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      :							str  )		->List[str]:






        '''simple docstring'''






        return self.load_in_abit or self.load_in_abit







    def   _snake_case       (      self      :							List[str]  )		->Tuple:






        '''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   _snake_case       (      cls      :							List[Any]  ,       __UpperCamelCase      :							Optional[int]  ,       __UpperCamelCase      :							str  ,       **__UpperCamelCase      :							Optional[Any]  )		->Any:






        '''simple docstring'''






        _UpperCAmelCase        = cls(**lowerCAmelCase_  )

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

        if return_unused_kwargs:
            return config, kwargs
        else:
            return config







    def   _snake_case       (      self      :							Tuple  ,       __UpperCamelCase      :							Union[str, os.PathLike]  )		->Optional[int]:






        '''simple docstring'''






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

            writer.write(lowerCAmelCase_  )







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






        '''simple docstring'''






        _UpperCAmelCase        = copy.deepcopy(self.__dict__  )
        _UpperCAmelCase        = str(output["""bnb_4bit_compute_dtype"""]  ).split("""."""  )[1]

        return output







    def __repr__(      self      :							List[str]  )		->List[Any]:






        '''simple docstring'''






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







    def   _snake_case       (      self      :							str  ,       __UpperCamelCase      :							bool = True  )		->str:






        '''simple docstring'''






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







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






        '''simple docstring'''






        _UpperCAmelCase        = self.to_dict()

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

        _UpperCAmelCase        = {}

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

        return serializable_config_dict







"""simple docstring"""
import os
from typing import Dict, List, Tuple, TypeVar, Union


lowerCamelCase_							      = TypeVar('''T''')

lowerCamelCase_							      = Union[List[T], Tuple[T, ...]]
lowerCamelCase_							      = Union[T, List[T], Dict[str, T]]
lowerCamelCase_							      = Union[str, bytes, os.PathLike]



