
# 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


lowercase						:      Union[str, Any]									= logging.get_logger(__name__)




@dataclass
class 				lowerCamelCase__							:




	'''simple docstring'''




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

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

								self.post_init()







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

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

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







	def      _lowerCamelCase       (      self	:Tuple		)	->				List[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      _lowerCamelCase       (      cls	:Union[str, Any]  ,     a	:str  ,     a	:List[Any]  ,     **a	:int		)	->				Dict:
								__UpperCamelCase			:     Any         =      cls(**a		)

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

								if return_unused_kwargs:
															return config, kwargs
								else:
															return config







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

															writer.write(a		)







	def      _lowerCamelCase       (      self	:Tuple		)	->				Dict[str, Any]:
								__UpperCamelCase			:     int         =      copy.deepcopy(self.__dict__		)
								__UpperCamelCase			:     Dict         =      str(output["bnb_4bit_compute_dtype"]		).split("."		)[1]

								return output







	def __repr__(      self	:Tuple		)	->				Any:
								return f'{self.__class__.__name__} {self.to_json_string()}'







	def      _lowerCamelCase       (      self	:Any  ,     a	:bool = True		)	->				str:
								if use_diff is True:
															__UpperCamelCase			:     Optional[Any]         =      self.to_diff_dict()
								else:
															__UpperCamelCase			:     Tuple         =      self.to_dict()
								return json.dumps(a  ,     indent=2  ,     sort_keys=a		) + "\n"







	def      _lowerCamelCase       (      self	:Union[str, Any]		)	->				Dict[str, Any]:
								__UpperCamelCase			:     List[str]         =      self.to_dict()

								# get the default config dict
								__UpperCamelCase			:     Optional[Any]         =      BitsAndBytesConfig().to_dict()

								__UpperCamelCase			:     Optional[int]         =      {}

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

								return serializable_config_dict

from __future__ import annotations

import time
from collections.abc import Sequence
from random import randint

from matplotlib import pyplot as plt




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Sequence[float]		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int)      ->					tuple[int | None, int | None, float]:






							'''simple docstring'''





							if not arr:
														return None, None, 0
							if low == high:
														return low, high, arr[low]

							__UpperCamelCase			:     Tuple         =      (low + high) // 2
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Dict         =      max_subarray(_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase)
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Optional[int]         =      max_subarray(_lowerCamelCase		,						mid + 1		,						_lowerCamelCase)
							__UpperCamelCase     ,		__UpperCamelCase     ,		__UpperCamelCase			:     Dict         =      max_cross_sum(_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase		,						_lowerCamelCase)
							if left_sum >= right_sum and left_sum >= cross_sum:
														return left_low, left_high, left_sum
							elif right_sum >= left_sum and right_sum >= cross_sum:
														return right_low, right_high, right_sum
							return cross_left, cross_right, cross_sum




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    Sequence[float]		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int		,						_lowerCamelCase				:    int)      ->					tuple[int, int, float]:






							'''simple docstring'''





							__UpperCamelCase     ,		__UpperCamelCase			:     Union[str, Any]         =      float("-inf"), -1
							__UpperCamelCase     ,		__UpperCamelCase			:     Optional[int]         =      float("-inf"), -1

							__UpperCamelCase			:     int | float         =      0
							for i in range(_lowerCamelCase		,						low - 1		,						-1):
														summ += arr[i]
														if summ > left_sum:
																					__UpperCamelCase			:     Tuple         =      summ
																					__UpperCamelCase			:     Optional[int]         =      i

							__UpperCamelCase			:     Union[str, Any]         =      0
							for i in range(mid + 1		,						high + 1):
														summ += arr[i]
														if summ > right_sum:
																					__UpperCamelCase			:     Union[str, Any]         =      summ
																					__UpperCamelCase			:     Optional[int]         =      i

							return max_left, max_right, (left_sum + right_sum)




def 			_SCREAMING_SNAKE_CASE      (	_lowerCamelCase				:    int)      ->					float:






							'''simple docstring'''





							__UpperCamelCase			:     Tuple         =      [randint(1		,						_lowerCamelCase) for _ in range(_lowerCamelCase)]
							__UpperCamelCase			:     Optional[Any]         =      time.time()
							max_subarray(_lowerCamelCase		,						0		,						input_size - 1)
							__UpperCamelCase			:     Any         =      time.time()
							return end - start




def 			_SCREAMING_SNAKE_CASE      (	)      ->					None:






							'''simple docstring'''





							__UpperCamelCase			:     Dict         =      [10, 100, 1_000, 10_000, 50_000, 100_000, 200_000, 300_000, 400_000, 500_000]
							__UpperCamelCase			:     Union[str, Any]         =      [time_max_subarray(_lowerCamelCase) for input_size in input_sizes]
							print("No of Inputs\t\tTime Taken")
							for input_size, runtime in zip(_lowerCamelCase		,						_lowerCamelCase):
														print(_lowerCamelCase		,						"\t\t"		,						_lowerCamelCase)
							plt.plot(_lowerCamelCase		,						_lowerCamelCase)
							plt.xlabel("Number of Inputs")
							plt.ylabel("Time taken in seconds")
							plt.show()


if __name__ == "__main__":
		from doctest import testmod

		testmod()