



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								=   os.path.expanduser(
    os.getenv("""HF_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """huggingface"""))
)
UpperCAmelCase								=   os.path.join(hf_cache_home, """accelerate""")
UpperCAmelCase								=   os.path.join(cache_dir, """default_config.yaml""")
UpperCAmelCase								=   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								=   default_yaml_config_file
else:
	UpperCAmelCase								=   default_json_config_file

def      A_ (		__a       :			Tuple						):

				"""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:
								a__			 =						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
												):
																a__			 =						ClusterConfig
												else:
																a__			 =						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
												):
																a__			 =						ClusterConfig
												else:
																a__			 =						SageMakerConfig
												return config_class.from_yaml_file(yaml_file=_A						)

@dataclass
class 	__snake_case :







							'''simple docstring'''






							UpperCamelCase__			: ComputeEnvironment
							UpperCamelCase__			: Union[DistributedType, SageMakerDistributedType]
							UpperCamelCase__			: str
							UpperCamelCase__			: bool


							def _a (							self						):
											a__			 =						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(__lowerCAmelCase					,					__lowerCAmelCase						):
																			a__			 =						value.value
															if isinstance(__lowerCAmelCase					,					__lowerCAmelCase						) and not bool(__lowerCAmelCase						):
																			a__			 =						None
											a__			 =						{k: v for k, v in result.items() if v is not None}
											return result


							@classmethod
							def _a (							cls					,					a_=None						):
											a__			 =						default_json_config_file if json_file is None else json_file
											with open(__lowerCAmelCase					,					"""r"""					,					encoding="""utf-8"""						) as f:
															a__			 =						json.load(__lowerCAmelCase						)
											if "compute_environment" not in config_dict:
															a__			 =						ComputeEnvironment.LOCAL_MACHINE
											if "mixed_precision" not in config_dict:
															a__			 =						"""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.
															a__			 =						config_dict.pop("""dynamo_backend"""						)
															a__			 =						{} if dynamo_backend == """NO""" else {"""dynamo_backend""": dynamo_backend}
											if "use_cpu" not in config_dict:
															a__			 =						False
											return cls(**__lowerCAmelCase						)


							def _a (							self					,					a_						):


											with open(__lowerCAmelCase					,					"""w"""					,					encoding="""utf-8"""						) as f:
															a__			 =						json.dumps(self.to_dict()					,					indent=2					,					sort_keys=__lowerCAmelCase						) + """\n"""
															f.write(__lowerCAmelCase						)


							@classmethod
							def _a (							cls					,					a_=None						):
											a__			 =						default_yaml_config_file if yaml_file is None else yaml_file
											with open(__lowerCAmelCase					,					"""r"""					,					encoding="""utf-8"""						) as f:
															a__			 =						yaml.safe_load(__lowerCAmelCase						)
											if "compute_environment" not in config_dict:
															a__			 =						ComputeEnvironment.LOCAL_MACHINE

											if "mixed_precision" not in config_dict:
															a__			 =						"""fp16""" if ("""fp16""" in config_dict and config_dict["""fp16"""]) else None
											if isinstance(config_dict["""mixed_precision"""]					,					__lowerCAmelCase						) and not config_dict["mixed_precision"]:
															a__			 =						"""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.
															a__			 =						config_dict.pop("""dynamo_backend"""						)
															a__			 =						{} if dynamo_backend == """NO""" else {"""dynamo_backend""": dynamo_backend}
											if "use_cpu" not in config_dict:
															a__			 =						False
											return cls(**__lowerCAmelCase						)


							def _a (							self					,					a_						):


											with open(__lowerCAmelCase					,					"""w"""					,					encoding="""utf-8"""						) as f:
															yaml.safe_dump(self.to_dict()					,					__lowerCAmelCase						)







							def _a (							self						):


											if isinstance(self.compute_environment					,					__lowerCAmelCase						):
															a__			 =						ComputeEnvironment(self.compute_environment						)
											if isinstance(self.distributed_type					,					__lowerCAmelCase						):
															if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
																			a__			 =						SageMakerDistributedType(self.distributed_type						)
															else:
																			a__			 =						DistributedType(self.distributed_type						)
											if self.dynamo_config is None:
															a__			 =						{}




@dataclass
class 	__snake_case (				lowerCAmelCase__				):







							'''simple docstring'''






							UpperCamelCase__			: int
							UpperCamelCase__			: int             =   0
							UpperCamelCase__			: int             =   1
							UpperCamelCase__			: Optional[str]             =   None
							UpperCamelCase__			: Optional[str]             =   None
							UpperCamelCase__			: Optional[int]             =   None
							UpperCamelCase__			: Optional[str]             =   "static"
							UpperCamelCase__			: Optional[bool]             =   False
							UpperCamelCase__			: str             =   "main"

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

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

							# args for dynamo
							UpperCamelCase__			: dict             =   None


							def _a (							self						):


											if self.deepspeed_config is None:
															a__			 =						{}
											if self.fsdp_config is None:
															a__			 =						{}
											if self.megatron_lm_config is None:
															a__			 =						{}
											if self.ipex_config is None:
															a__			 =						{}
											return super().__post_init__()




@dataclass
class 	__snake_case (				lowerCAmelCase__				):







							'''simple docstring'''






							UpperCamelCase__			: str
							UpperCamelCase__			: str
							UpperCamelCase__			: Optional[str]             =   None
							UpperCamelCase__			: Optional[str]             =   None
							UpperCamelCase__			: str             =   "us-east-1"
							UpperCamelCase__			: int             =   1
							UpperCamelCase__			: str             =   "all"
							UpperCamelCase__			: str             =   F"""accelerate-sagemaker-{num_machines}"""
							UpperCamelCase__			: str             =   SAGEMAKER_PYTORCH_VERSION
							UpperCamelCase__			: str             =   SAGEMAKER_TRANSFORMERS_VERSION
							UpperCamelCase__			: str             =   SAGEMAKER_PYTHON_VERSION
							UpperCamelCase__			: str             =   None
							UpperCamelCase__			: str             =   None
							UpperCamelCase__			: dict             =   None
							UpperCamelCase__			: dict             =   None






def      A_ (		__a       :			int = 1_000						):

				"""simple docstring"""
				return sum(i for i in range(__a						) if i % 3 == 0 or i % 5 == 0						)


if __name__ == "__main__":
	print(f"""{solution() = }""")

