






import json
import os
from dataclasses import dataclass
from enum import Enum
from typing import List, Optional, Union

import yaml

from ...utils import ComputeEnvironment, DistributedType, SageMakerDistributedType
from ...utils.constants import SAGEMAKER_PYTHON_VERSION, SAGEMAKER_PYTORCH_VERSION, SAGEMAKER_TRANSFORMERS_VERSION


UpperCAmelCase_					:		int           = os.path.expanduser(
    os.getenv('''HF_HOME''', os.path.join(os.getenv('''XDG_CACHE_HOME''', '''~/.cache'''), '''huggingface'''))
)
UpperCAmelCase_					:		str           = os.path.join(hf_cache_home, '''accelerate''')
UpperCAmelCase_					:		Dict           = os.path.join(cache_dir, '''default_config.yaml''')
UpperCAmelCase_					:		Optional[int]           = os.path.join(cache_dir, '''default_config.yaml''')

# For backward compatibility: the default config is the json one if it's the only existing file.
if os.path.isfile(default_yaml_config_file) or not os.path.isfile(default_json_config_file):
	UpperCAmelCase_					:		Tuple           = default_yaml_config_file
else:
	UpperCAmelCase_					:		List[Any]           = default_json_config_file

def   SCREAMING_SNAKE_CASE_				(				__magic_name__   :    List[Any]   )						->     Optional[Any]:






	"""simple docstring"""



	if config_file is not None:
		if not os.path.isfile(a_   ):
			raise FileNotFoundError(
			    f"""The passed configuration file `{config_file}` does not exist. """
			    """Please pass an existing file to `accelerate launch`, or use the the default one """
			    """created through `accelerate config` and run `accelerate launch` """
			    """without the `--config_file` argument."""   )
	else:
		UpperCamelCase	:Dict		=      default_config_file
	with open(a_      ,     """r"""      ,     encoding="""utf-8"""   ) as f:
		if config_file.endswith(""".json"""   ):
			if (
			    json.load(a_   ).get("""compute_environment"""      ,     ComputeEnvironment.LOCAL_MACHINE   )
			    == ComputeEnvironment.LOCAL_MACHINE
			):
				UpperCamelCase	:Optional[Any]		=      ClusterConfig
			else:
				UpperCamelCase	:Dict		=      SageMakerConfig
			return config_class.from_json_file(json_file=a_   )
		else:
			if (
			    yaml.safe_load(a_   ).get("""compute_environment"""      ,     ComputeEnvironment.LOCAL_MACHINE   )
			    == ComputeEnvironment.LOCAL_MACHINE
			):
				UpperCamelCase	:Dict		=      ClusterConfig
			else:
				UpperCamelCase	:int		=      SageMakerConfig
			return config_class.from_yaml_file(yaml_file=a_   )

@dataclass
class        _SCREAMING_SNAKE_CASE			:
	snake_case__				:	ComputeEnvironment
	snake_case__				:	Union[DistributedType, SageMakerDistributedType]
	snake_case__				:	str
	snake_case__				:	bool





	def 		_A					(						self							:   Dict      ):
		UpperCamelCase	:List[Any]		=      self.__dict__
		# For serialization, it's best to convert Enums to strings (or their underlying value type).
		for key, value in result.items():
			if isinstance(_UpperCAmelCase					,    _UpperCAmelCase      ):
				UpperCamelCase	:Union[str, Any]		=      value.value
			if isinstance(_UpperCAmelCase					,    _UpperCAmelCase      ) and not bool(_UpperCAmelCase      ):
				UpperCamelCase	:Optional[int]		=      None
		UpperCamelCase	:Optional[Any]		=      {k: v for k, v in result.items() if v is not None}
		return result





	@classmethod
	def 		_A					(						cls							:   List[Any]					,    __lowerCamelCase							:   Any=None      ):
		UpperCamelCase	:str		=      default_json_config_file if json_file is None else json_file
		with open(_UpperCAmelCase					,    """r"""					,    encoding="""utf-8"""      ) as f:
			UpperCamelCase	:Optional[int]		=      json.load(_UpperCAmelCase      )
		if "compute_environment" not in config_dict:
			UpperCamelCase	:List[str]		=      ComputeEnvironment.LOCAL_MACHINE
		if "mixed_precision" not in config_dict:
			UpperCamelCase	:Dict		=      '''fp16''' if ('''fp16''' in config_dict and config_dict['''fp16''']) else None
		if "fp16" in config_dict:  # Convert the config to the new format.
			del config_dict["fp16"]
		if "dynamo_backend" in config_dict:  # Convert the config to the new format.
			UpperCamelCase	:int		=      config_dict.pop("""dynamo_backend"""      )
			UpperCamelCase	:int		=      {} if dynamo_backend == '''NO''' else {'''dynamo_backend''': dynamo_backend}
		if "use_cpu" not in config_dict:
			UpperCamelCase	:Optional[Any]		=      False
		return cls(**_UpperCAmelCase      )





	def 		_A					(						self							:   str					,    __lowerCamelCase							:   List[Any]      ):
		with open(_UpperCAmelCase					,    """w"""					,    encoding="""utf-8"""      ) as f:
			UpperCamelCase	:Any		=      json.dumps(self.to_dict()					,    indent=2					,    sort_keys=_UpperCAmelCase      ) + '''\n'''
			f.write(_UpperCAmelCase      )





	@classmethod
	def 		_A					(						cls							:   Optional[int]					,    __lowerCamelCase							:   List[str]=None      ):
		UpperCamelCase	:List[Any]		=      default_yaml_config_file if yaml_file is None else yaml_file
		with open(_UpperCAmelCase					,    """r"""					,    encoding="""utf-8"""      ) as f:
			UpperCamelCase	:str		=      yaml.safe_load(_UpperCAmelCase      )
		if "compute_environment" not in config_dict:
			UpperCamelCase	:List[Any]		=      ComputeEnvironment.LOCAL_MACHINE

		if "mixed_precision" not in config_dict:
			UpperCamelCase	:Tuple		=      '''fp16''' if ('''fp16''' in config_dict and config_dict['''fp16''']) else None
		if isinstance(config_dict["""mixed_precision"""]					,    _UpperCAmelCase      ) and not config_dict["mixed_precision"]:
			UpperCamelCase	:Any		=      '''no'''
		if "fp16" in config_dict:  # Convert the config to the new format.
			del config_dict["fp16"]
		if "dynamo_backend" in config_dict:  # Convert the config to the new format.
			UpperCamelCase	:Optional[int]		=      config_dict.pop("""dynamo_backend"""      )
			UpperCamelCase	:Dict		=      {} if dynamo_backend == '''NO''' else {'''dynamo_backend''': dynamo_backend}
		if "use_cpu" not in config_dict:
			UpperCamelCase	:List[str]		=      False
		return cls(**_UpperCAmelCase      )





	def 		_A					(						self							:   List[str]					,    __lowerCamelCase							:   Optional[Any]      ):
		with open(_UpperCAmelCase					,    """w"""					,    encoding="""utf-8"""      ) as f:
			yaml.safe_dump(self.to_dict()					,    _UpperCAmelCase      )





	def 		_A					(						self							:   Union[str, Any]      ):
		if isinstance(self.compute_environment					,    _UpperCAmelCase      ):
			UpperCamelCase	:Tuple		=      ComputeEnvironment(self.compute_environment      )
		if isinstance(self.distributed_type					,    _UpperCAmelCase      ):
			if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
				UpperCamelCase	:Optional[Any]		=      SageMakerDistributedType(self.distributed_type      )
			else:
				UpperCamelCase	:Dict		=      DistributedType(self.distributed_type      )
		if self.dynamo_config is None:
			UpperCamelCase	:Optional[Any]		=      {}

@dataclass
class        _SCREAMING_SNAKE_CASE			(     UpperCamelCase__ ):
	snake_case__				:	int
	snake_case__				:	int	      =    0
	snake_case__				:	int	      =    1
	snake_case__				:	Optional[str]	      =    None
	snake_case__				:	Optional[str]	      =    None
	snake_case__				:	Optional[int]	      =    None
	snake_case__				:	Optional[str]	      =    "static"
	snake_case__				:	Optional[bool]	      =    False
	snake_case__				:	str	      =    "main"

	# args for deepspeed_plugin
	snake_case__				:	dict	      =    None
	# args for fsdp
	snake_case__				:	dict	      =    None
	# args for megatron_lm
	snake_case__				:	dict	      =    None
	# args for ipex
	snake_case__				:	dict	      =    None
	# args for TPU
	snake_case__				:	bool	      =    False

	# args for TPU pods
	snake_case__				:	str	      =    None
	snake_case__				:	str	      =    None
	snake_case__				:	bool	      =    False
	snake_case__				:	bool	      =    False
	snake_case__				:	str	      =    None
	snake_case__				:	List[str]	      =    None
	snake_case__				:	List[str]	      =    None
	snake_case__				:	List[str]	      =    None

	# args for dynamo
	snake_case__				:	dict	      =    None





	def 		_A					(						self							:   Tuple      ):
		if self.deepspeed_config is None:
			UpperCamelCase	:Union[str, Any]		=      {}
		if self.fsdp_config is None:
			UpperCamelCase	:Any		=      {}
		if self.megatron_lm_config is None:
			UpperCamelCase	:Tuple		=      {}
		if self.ipex_config is None:
			UpperCamelCase	:List[str]		=      {}
		return super().__post_init__()

@dataclass
class        _SCREAMING_SNAKE_CASE			(     UpperCamelCase__ ):
	snake_case__				:	str
	snake_case__				:	str
	snake_case__				:	Optional[str]	      =    None
	snake_case__				:	Optional[str]	      =    None
	snake_case__				:	str	      =    "us-east-1"
	snake_case__				:	int	      =    1
	snake_case__				:	str	      =    "all"
	snake_case__				:	str	      =    f'''accelerate-sagemaker-{num_machines}'''
	snake_case__				:	str	      =    SAGEMAKER_PYTORCH_VERSION
	snake_case__				:	str	      =    SAGEMAKER_TRANSFORMERS_VERSION
	snake_case__				:	str	      =    SAGEMAKER_PYTHON_VERSION
	snake_case__				:	str	      =    None
	snake_case__				:	str	      =    None
	snake_case__				:	dict	      =    None
	snake_case__				:	dict	      =    None








'''simple docstring'''







from ctypes import c_float, sizeof
from enum import Enum
from typing import TYPE_CHECKING, Optional, Union


if TYPE_CHECKING:
  from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore




class 					__UpperCAmelCase (		UpperCamelCase__	):

      '''simple docstring'''





      SCREAMING_SNAKE_CASE				:    Dict				       =					c_float




      @property
      def 							lowerCamelCase     (					self						):
             return sizeof(self.value						)






def  lowerCAmelCase__		(					a_  :     int			,						a_  :     int			,						a_  :     int = 0			)   -> int:
       # < 0 is possible if using a dynamic axis
       if dimension <= 0:
              UpperCAmelCase__	:   List[Any]												=	fixed_dimension

       dimension -= num_token_to_add
       return dimension






def  lowerCAmelCase__		(					a_  :     int			,						a_  :     ParameterFormat			)   -> int:
       return num_parameters * dtype.size






def  lowerCAmelCase__		(					a_  :     str			)   -> Optional[Union["AutoTokenizer", "AutoFeatureExtractor", "AutoProcessor"]]:
       from .. import AutoFeatureExtractor, AutoProcessor, AutoTokenizer  # tests_ignore

       try:
              return AutoProcessor.from_pretrained(a_			)
       except (ValueError, OSError, KeyError):
              UpperCAmelCase__							,							UpperCAmelCase__	:   Dict												=	None, None
              try:
                     UpperCAmelCase__	:   Union[str, Any]												=	AutoTokenizer.from_pretrained(a_			)
              except (OSError, KeyError):
                     pass
              try:
                     UpperCAmelCase__	:   List[str]												=	AutoFeatureExtractor.from_pretrained(a_			)
              except (OSError, KeyError):
                     pass

              if tokenizer is not None and feature_extractor is not None:
                     raise ValueError(
                         f"""Couldn't auto-detect preprocessor for {model_name}. Found both a tokenizer and a feature extractor."""			)
              elif tokenizer is None and feature_extractor is None:
                     return None
              elif tokenizer is not None:
                     return tokenizer
              else:
                     return feature_extractor