


"""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


UpperCamelCase__				:Dict			   =	logging.get_logger(__name__)






@dataclass
class      A:

		"""simple docstring"""





		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__					,				)      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:Dict					     =	load_in_abit
					_UpperCamelCase					:Optional[Any]					     =	load_in_abit
					_UpperCamelCase					:Optional[Any]					     =	llm_inta_threshold
					_UpperCamelCase					:Optional[int]					     =	llm_inta_skip_modules
					_UpperCamelCase					:Dict					     =	llm_inta_enable_fpaa_cpu_offload
					_UpperCamelCase					:str					     =	llm_inta_has_fpaa_weight
					_UpperCamelCase					:List[Any]					     =	bnb_abit_quant_type
					_UpperCamelCase					:List[str]					     =	bnb_abit_use_double_quant

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

					"""simple docstring"""



					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   )      ->	Tuple:

					"""simple docstring"""



					return self.load_in_abit or self.load_in_abit
		def   _UpperCamelCase( self   )      ->	List[Any]:

					"""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   _UpperCamelCase( cls					,				SCREAMING_SNAKE_CASE__					,				SCREAMING_SNAKE_CASE__					,				**SCREAMING_SNAKE_CASE__   )      ->	List[str]:

					"""simple docstring"""



					_UpperCamelCase					:List[Any]					     =	cls(**UpperCAmelCase__   )

					_UpperCamelCase					:Optional[int]					     =	[]
					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					,				SCREAMING_SNAKE_CASE__   )      ->	List[str]:

					"""simple docstring"""



					with open(UpperCAmelCase__					,				'''w'''					,				encoding='''utf-8'''   ) as writer:
								_UpperCamelCase					:Dict					     =	self.to_dict()
								_UpperCamelCase					:Optional[int]					     =	json.dumps(UpperCAmelCase__					,				indent=2					,				sort_keys=UpperCAmelCase__   ) + '''\n'''

								writer.write(UpperCAmelCase__   )
		def   _UpperCamelCase( self   )      ->	Dict[str, Any]:

					"""simple docstring"""



					_UpperCamelCase					:Tuple					     =	copy.deepcopy(self.__dict__   )
					_UpperCamelCase					:List[Any]					     =	str(output['''bnb_4bit_compute_dtype''']   ).split('''.'''   )[1]

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

					"""simple docstring"""



					return f"{self.__class__.__name__} {self.to_json_string()}"
		def   _UpperCamelCase( self					,				SCREAMING_SNAKE_CASE__ = True   )      ->	str:

					"""simple docstring"""



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





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

					"""simple docstring"""



					_UpperCamelCase					:Union[str, Any]					     =	self.to_dict()

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

					_UpperCamelCase					:Tuple					     =	{}

					# 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







# Copyright 2021 The HuggingFace 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 argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


_lowerCamelCase      :						Optional[Any]							=  """Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine"""







def SCREAMING_SNAKE_CASE							(			)  ->			Dict:



       """simple docstring"""

       A__ 					=       _ask_options(
           '''In which compute environment are you running?'''					,							['''This machine''', '''AWS (Amazon SageMaker)''']					,							_convert_compute_environment					,							)
       if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
              A__ 					=       get_sagemaker_input()
       else:
              A__ 					=       get_cluster_input()
       return config







def SCREAMING_SNAKE_CASE							(			lowercase_=None		)  ->			List[Any]:



       """simple docstring"""

       if subparsers is not None:
              A__ 					=       subparsers.add_parser('''config'''					,							description=lowercase_		)
       else:
              A__ 					=       argparse.ArgumentParser('''Accelerate config command'''					,							description=lowercase_		)

       parser.add_argument(
           '''--config_file'''					,							default=lowercase_					,							help=(
               '''The path to use to store the config file. Will default to a file named default_config.yaml in the cache '''
               '''location, which is the content of the environment `HF_HOME` suffixed with \'accelerate\', or if you don\'t have '''
               '''such an environment variable, your cache directory (\'~/.cache\' or the content of `XDG_CACHE_HOME`) suffixed '''
               '''with \'huggingface\'.'''
           )					,							)

       if subparsers is not None:
              parser.set_defaults(func=lowercase_		)
       return parser







def SCREAMING_SNAKE_CASE							(			lowercase_		)  ->			Any:



       """simple docstring"""

       A__ 					=       get_user_input()
       if args.config_file is not None:
              A__ 					=       args.config_file
       else:
              if not os.path.isdir(lowercase_		):
                     os.makedirs(lowercase_		)
              A__ 					=       default_yaml_config_file

       if config_file.endswith('''.json'''		):
              config.to_json_file(lowercase_		)
       else:
              config.to_yaml_file(lowercase_		)
       print(f"""accelerate configuration saved at {config_file}"""		)







def SCREAMING_SNAKE_CASE							(			)  ->			Union[str, Any]:



       """simple docstring"""

       A__ 					=       config_command_parser()
       A__ 					=       parser.parse_args()
       config_command(lowercase_		)


if __name__ == "__main__":
  main()


