




'''simple docstring'''






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


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

def       __lowerCAmelCase (   snake_case__						):


			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				:      str									=       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				:      Any									=       ClusterConfig
									else:
												__UpperCamelCase				:      Tuple									=       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				:      List[str]									=       ClusterConfig
									else:
												__UpperCamelCase				:      List[Any]									=       SageMakerConfig
									return config_class.from_yaml_file(yaml_file=_A						)



@dataclass
class  A		:




				'''simple docstring'''





				A				     =    42
				A				     =    42
				A				     =    42
				A				     =    42
				def 	a_	(self      )     ->	Any:
							__UpperCamelCase				:      Optional[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(__lowerCAmelCase			,  __lowerCAmelCase      ):
													__UpperCamelCase				:      List[Any]									=       value.value
										if isinstance(__lowerCAmelCase			,  __lowerCAmelCase      ) and not bool(__lowerCAmelCase      ):
													__UpperCamelCase				:      str									=       None
							__UpperCamelCase				:      int									=       {k: v for k, v in result.items() if v is not None}
							return result
				@classmethod
				def 	a_	(cls			,  _UpperCAmelCase=None      )     ->	str:
							__UpperCamelCase				:      List[Any]									=       default_json_config_file if json_file is None else json_file
							with open(__lowerCAmelCase			,  "r"			,  encoding="utf-8"      ) as f:
										__UpperCamelCase				:      Dict									=       json.load(__lowerCAmelCase      )
							if "compute_environment" not in config_dict:
										__UpperCamelCase				:      Optional[Any]									=       ComputeEnvironment.LOCAL_MACHINE
							if "mixed_precision" not in config_dict:
										__UpperCamelCase				:      str									=       "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				:      Union[str, Any]									=       config_dict.pop("dynamo_backend"      )
										__UpperCamelCase				:      str									=       {} if dynamo_backend == "NO" else {"dynamo_backend": dynamo_backend}
							if "use_cpu" not in config_dict:
										__UpperCamelCase				:      Any									=       False
							return cls(**__lowerCAmelCase      )
				def 	a_	(self			,  _UpperCAmelCase      )     ->	Union[str, Any]:


							with open(__lowerCAmelCase			,  "w"			,  encoding="utf-8"      ) as f:
										__UpperCamelCase				:      List[str]									=       json.dumps(self.to_dict()			,  indent=2			,  sort_keys=__lowerCAmelCase      ) + "\n"
										f.write(__lowerCAmelCase      )
				@classmethod
				def 	a_	(cls			,  _UpperCAmelCase=None      )     ->	Dict:
							__UpperCamelCase				:      Optional[Any]									=       default_yaml_config_file if yaml_file is None else yaml_file
							with open(__lowerCAmelCase			,  "r"			,  encoding="utf-8"      ) as f:
										__UpperCamelCase				:      List[str]									=       yaml.safe_load(__lowerCAmelCase      )
							if "compute_environment" not in config_dict:
										__UpperCamelCase				:      Tuple									=       ComputeEnvironment.LOCAL_MACHINE

							if "mixed_precision" not in config_dict:
										__UpperCamelCase				:      List[str]									=       "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"]:
										__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[Any]									=       config_dict.pop("dynamo_backend"      )
										__UpperCamelCase				:      Any									=       {} if dynamo_backend == "NO" else {"dynamo_backend": dynamo_backend}
							if "use_cpu" not in config_dict:
										__UpperCamelCase				:      Dict									=       False
							return cls(**__lowerCAmelCase      )
				def 	a_	(self			,  _UpperCAmelCase      )     ->	Tuple:


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





				def 	a_	(self      )     ->	Optional[Any]:


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







@dataclass
class  A		(			lowerCAmelCase__		):




				'''simple docstring'''





				A				     =    42
				A				     =    0
				A				     =    1
				A				     =    None
				A				     =    None
				A				     =    None
				A				     =    "static"
				A				     =    False
				A				     =    "main"

				# args for deepspeed_plugin
				A				     =    None
				# args for fsdp
				A				     =    None
				# args for megatron_lm
				A				     =    None
				# args for ipex
				A				     =    None
				# args for TPU
				A				     =    False

				# args for TPU pods
				A				     =    None
				A				     =    None
				A				     =    False
				A				     =    False
				A				     =    None
				A				     =    None
				A				     =    None
				A				     =    None

				# args for dynamo
				A				     =    None
				def 	a_	(self      )     ->	Optional[int]:


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







@dataclass
class  A		(			lowerCAmelCase__		):




				'''simple docstring'''





				A				     =    42
				A				     =    42
				A				     =    None
				A				     =    None
				A				     =    "us-east-1"
				A				     =    1
				A				     =    "all"
				A				     =    f"accelerate-sagemaker-{num_machines}"
				A				     =    SAGEMAKER_PYTORCH_VERSION
				A				     =    SAGEMAKER_TRANSFORMERS_VERSION
				A				     =    SAGEMAKER_PYTHON_VERSION
				A				     =    None
				A				     =    None
				A				     =    None
				A				     =    None





'''simple docstring'''






from __future__ import annotations

import random
from collections.abc import Iterable
from typing import Any, Generic, TypeVar

_lowerCAmelCase				     = TypeVar('''T''', bound=bool)



class  A		(			Generic[T]		):




				'''simple docstring'''





				def __init__(self			,  _UpperCAmelCase      )     ->	None:
							__UpperCamelCase				:      T									=       value
							__UpperCamelCase				:      RandomizedHeapNode[T] | None									=       None
							__UpperCamelCase				:      RandomizedHeapNode[T] | None									=       None
				@property
				def 	a_	(self      )     ->	T:
							return self._value





				@staticmethod
				def 	a_	(_UpperCAmelCase			,  _UpperCAmelCase      )     ->	RandomizedHeapNode[T] | None:
							if not roota:
										return roota

							if not roota:
										return roota

							if roota.value > roota.value:
										__UpperCamelCase		, __UpperCamelCase				:      int									=       roota, roota

							if random.choice([True, False]      ):
										__UpperCamelCase		, __UpperCamelCase				:      Dict									=       roota.right, roota.left

							__UpperCamelCase				:      Any									=       RandomizedHeapNode.merge(roota.left			,  _UpperCAmelCase      )

							return roota







class  A		(			Generic[T]		):




				'''simple docstring'''





				def __init__(self			,  _UpperCAmelCase = ()      )     ->	None:
							__UpperCamelCase				:      RandomizedHeapNode[T] | None									=       None

							if data:
										for item in data:
													self.insert(_UpperCAmelCase      )
				def 	a_	(self			,  _UpperCAmelCase      )     ->	None:
							__UpperCamelCase				:      str									=       RandomizedHeapNode.merge(self._root			,  RandomizedHeapNode(_UpperCAmelCase      )      )
				def 	a_	(self      )     ->	T | None:
							__UpperCamelCase				:      Dict									=       self.top()

							if self._root is None:
										return None

							__UpperCamelCase				:      List[str]									=       RandomizedHeapNode.merge(self._root.left			,  self._root.right      )

							return result
				def 	a_	(self      )     ->	T:
							if not self._root:
										raise IndexError("Can't get top element for the empty heap."      )
							return self._root.value
				def 	a_	(self      )     ->	None:
							__UpperCamelCase				:      int									=       None
				def 	a_	(self      )     ->	list[Any]:
							__UpperCamelCase				:      int									=       []
							while self:
										result.append(self.pop()      )

							return result





				def __bool__(self      )     ->	bool:
							return self._root is not None


if __name__ == "__main__":
				import doctest

				doctest.testmod()



