





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						: Optional[Any]		=   logging.get_logger(__name__)







@dataclass
class 		UpperCAmelCase   :


  """simple docstring"""







  def __init__(				self    :					int							,	UpperCamelCase__    :					List[Any]=False							,	UpperCamelCase__    :					Tuple=False							,	UpperCamelCase__    :					Tuple=6.0							,	UpperCamelCase__    :					Tuple=None							,	UpperCamelCase__    :					Tuple=False							,	UpperCamelCase__    :					str=False							,	UpperCamelCase__    :					List[str]=None							,	UpperCamelCase__    :					Any="fp4"							,	UpperCamelCase__    :					Optional[int]=False							,	**UpperCamelCase__    :					int							,	)     ->						int:
    _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(UpperCamelCase__							,	UpperCamelCase__  ):
      _UpperCamelCase       			=getattr(UpperCamelCase__							,	UpperCamelCase__  )
    elif isinstance(UpperCamelCase__							,	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   UpperCamelCase__      (				self    :					int  )     ->						Union[str, 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   UpperCamelCase__      (				self    :					Union[str, Any]  )     ->						Any:
    return self.load_in_abit or self.load_in_abit


  def   UpperCamelCase__      (				self    :					Optional[int]  )     ->						str:
    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   UpperCamelCase__      (				cls    :					Optional[int]							,	UpperCamelCase__    :					Tuple							,	UpperCamelCase__    :					Union[str, Any]							,	**UpperCamelCase__    :					List[str]  )     ->						str:
    _UpperCamelCase       			=cls(**UpperCamelCase__  )

    _UpperCamelCase       			=[]
    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   UpperCamelCase__      (				self    :					str							,	UpperCamelCase__    :					Union[str, os.PathLike]  )     ->						List[Any]:
    with open(UpperCamelCase__							,	'''w'''							,	encoding='''utf-8'''  ) as writer:
      _UpperCamelCase       			=self.to_dict()
      _UpperCamelCase       			=json.dumps(UpperCamelCase__							,	indent=2							,	sort_keys=UpperCamelCase__  ) + '''\n'''

      writer.write(UpperCamelCase__  )


  def   UpperCamelCase__      (				self    :					str  )     ->						Dict[str, Any]:
    _UpperCamelCase       			=copy.deepcopy(self.__dict__  )
    _UpperCamelCase       			=str(output['''bnb_4bit_compute_dtype''']  ).split('''.'''  )[1]

    return output


  def __repr__(				self    :					str  )     ->						List[str]:
    return F'''{self.__class__.__name__} {self.to_json_string()}'''


  def   UpperCamelCase__      (				self    :					Tuple							,	UpperCamelCase__    :					bool = True  )     ->						str:
    if use_diff is True:
      _UpperCamelCase       			=self.to_diff_dict()
    else:
      _UpperCamelCase       			=self.to_dict()
    return json.dumps(UpperCamelCase__							,	indent=2							,	sort_keys=UpperCamelCase__  ) + "\n"




  def   UpperCamelCase__      (				self    :					int  )     ->						Dict[str, Any]:
    _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'''






def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=[[] for _ in range(__SCREAMING_SNAKE_CASE   )]
  _UpperCamelCase       			=key - 1

  if key <= 0:
    raise ValueError('''Height of grid can\'t be 0 or negative'''   )
  if key == 1 or len(__SCREAMING_SNAKE_CASE   ) <= key:
    return input_string

  for position, character in enumerate(__SCREAMING_SNAKE_CASE   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    temp_grid[num].append(__SCREAMING_SNAKE_CASE   )
  _UpperCamelCase       			=[''''''.join(__SCREAMING_SNAKE_CASE   ) for row in temp_grid]
  _UpperCamelCase       			=''''''.join(__SCREAMING_SNAKE_CASE   )

  return output_string


def 					_a   (__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			=[]
  _UpperCamelCase       			=key - 1

  if key <= 0:
    raise ValueError('''Height of grid can\'t be 0 or negative'''   )
  if key == 1:
    return input_string

  _UpperCamelCase       			=[[] for _ in range(__SCREAMING_SNAKE_CASE   )]  # generates template
  for position in range(len(__SCREAMING_SNAKE_CASE   )   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    temp_grid[num].append('''*'''   )

  _UpperCamelCase       			=0
  for row in temp_grid:  # fills in the characters
    _UpperCamelCase       			=input_string[counter : counter + len(__SCREAMING_SNAKE_CASE   )]
    grid.append(list(__SCREAMING_SNAKE_CASE   )   )
    counter += len(__SCREAMING_SNAKE_CASE   )

  _UpperCamelCase       			=''''''  # reads as zigzag
  for position in range(len(__SCREAMING_SNAKE_CASE   )   ):
    _UpperCamelCase       			=position % (lowest * 2)  # puts it in bounds
    _UpperCamelCase       			=min(__SCREAMING_SNAKE_CASE  ,      lowest * 2 - num   )  # creates zigzag pattern
    output_string += grid[num][0]
    grid[num].pop(0   )
  return output_string


def 					_a   (__SCREAMING_SNAKE_CASE   ):


  """simple docstring"""

  _UpperCamelCase       			={}
  for key_guess in range(1  ,      len(__SCREAMING_SNAKE_CASE   )   ):  # tries every key
    _UpperCamelCase       			=decrypt(__SCREAMING_SNAKE_CASE  ,      __SCREAMING_SNAKE_CASE   )
  return results


if __name__ == "__main__":
      import doctest

      doctest.testmod()






