
'''simple docstring'''


def 				__magic_name__(					UpperCAmelCase__   :					str  ):
						UpperCamelCase_    :  List[str]				    =    str(UpperCAmelCase__  ).strip()
						if not oct_string:
												raise ValueError("""Empty string was passed to the function"""  )
						UpperCamelCase_    :  int				    =    oct_string[0] == "-"
						if is_negative:
												UpperCamelCase_    :  Union[str, Any]				    =    oct_string[1:]
						if not oct_string.isdigit() or not all(0 <= int(UpperCAmelCase__  ) <= 7 for char in oct_string  ):
												raise ValueError("""Non-octal value was passed to the function"""  )
						UpperCamelCase_    :  List[Any]				    =    0
						for char in oct_string:
												UpperCamelCase_    :  Optional[Any]				    =    8 * decimal_number + int(UpperCAmelCase__  )
						if is_negative:
												UpperCamelCase_    :  Any				    =    -decimal_number
						return decimal_number


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

			testmod()






'''simple docstring'''


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


__SCREAMING_SNAKE_CASE  =			os.path.expanduser(
    os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
)
__SCREAMING_SNAKE_CASE  =			os.path.join(hf_cache_home, "accelerate")
__SCREAMING_SNAKE_CASE  =			os.path.join(cache_dir, "default_config.yaml")
__SCREAMING_SNAKE_CASE  =			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):
	__SCREAMING_SNAKE_CASE  =			default_yaml_config_file
else:
	__SCREAMING_SNAKE_CASE  =			default_json_config_file
def 				__magic_name__(					UpperCAmelCase__   :					int  ):
						if config_file is not None:
												if not os.path.isfile(UpperCAmelCase__  ):
																		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_    :  List[Any]				    =    default_config_file
						with open(UpperCAmelCase__       ,							"""r"""       ,							encoding="""utf-8"""  ) as f:
												if config_file.endswith(""".json"""  ):
																		if (
																		    json.load(UpperCAmelCase__  ).get("""compute_environment"""       ,							ComputeEnvironment.LOCAL_MACHINE  )
																		    == ComputeEnvironment.LOCAL_MACHINE
																		):
																								UpperCamelCase_    :  int				    =    ClusterConfig
																		else:
																								UpperCamelCase_    :  Optional[Any]				    =    SageMakerConfig
																		return config_class.from_json_file(json_file=UpperCAmelCase__  )
												else:
																		if (
																		    yaml.safe_load(UpperCAmelCase__  ).get("""compute_environment"""       ,							ComputeEnvironment.LOCAL_MACHINE  )
																		    == ComputeEnvironment.LOCAL_MACHINE
																		):
																								UpperCamelCase_    :  Union[str, Any]				    =    ClusterConfig
																		else:
																								UpperCamelCase_    :  Optional[int]				    =    SageMakerConfig
																		return config_class.from_yaml_file(yaml_file=UpperCAmelCase__  )




@dataclass
class 						__snake_case :
		a_        =				42
		a_        =				42
		a_        =				42
		a_        =				42




		def   _snake_case  (     self				:List[Any])				-> Optional[Any]:






								'''simple docstring'''






								UpperCamelCase_    :  List[str]				    =    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(_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE):
																				UpperCamelCase_    :  str				    =    value.value
														if isinstance(_SCREAMING_SNAKE_CASE ,						_SCREAMING_SNAKE_CASE) and not bool(_SCREAMING_SNAKE_CASE):
																				UpperCamelCase_    :  Dict				    =    None
								UpperCamelCase_    :  Dict				    =    {k: v for k, v in result.items() if v is not None}
								return result




		@classmethod
		def   _snake_case  (     cls				:Dict ,						_SCREAMING_SNAKE_CASE				:Optional[int]=None)				-> int:






								'''simple docstring'''






								UpperCamelCase_    :  str				    =    default_json_config_file if json_file is None else json_file
								with open(_SCREAMING_SNAKE_CASE ,						"""r""" ,						encoding="""utf-8""") as f:
														UpperCamelCase_    :  List[str]				    =    json.load(_SCREAMING_SNAKE_CASE)
								if "compute_environment" not in config_dict:
														UpperCamelCase_    :  Union[str, Any]				    =    ComputeEnvironment.LOCAL_MACHINE
								if "mixed_precision" not in config_dict:
														UpperCamelCase_    :  List[Any]				    =    """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_    :  Optional[int]				    =    config_dict.pop("""dynamo_backend""")
														UpperCamelCase_    :  List[str]				    =    {} if dynamo_backend == """NO""" else {"""dynamo_backend""": dynamo_backend}
								if "use_cpu" not in config_dict:
														UpperCamelCase_    :  Dict				    =    False
								return cls(**_SCREAMING_SNAKE_CASE)




		def   _snake_case  (     self				:int ,						_SCREAMING_SNAKE_CASE				:List[Any])				-> List[str]:






								'''simple docstring'''






								with open(_SCREAMING_SNAKE_CASE ,						"""w""" ,						encoding="""utf-8""") as f:
														UpperCamelCase_    :  Union[str, Any]				    =    json.dumps(self.to_dict() ,						indent=2 ,						sort_keys=_SCREAMING_SNAKE_CASE) + """\n"""
														f.write(_SCREAMING_SNAKE_CASE)




		@classmethod
		def   _snake_case  (     cls				:Optional[Any] ,						_SCREAMING_SNAKE_CASE				:Any=None)				-> List[str]:






								'''simple docstring'''






								UpperCamelCase_    :  List[str]				    =    default_yaml_config_file if yaml_file is None else yaml_file
								with open(_SCREAMING_SNAKE_CASE ,						"""r""" ,						encoding="""utf-8""") as f:
														UpperCamelCase_    :  Any				    =    yaml.safe_load(_SCREAMING_SNAKE_CASE)
								if "compute_environment" not in config_dict:
														UpperCamelCase_    :  Union[str, Any]				    =    ComputeEnvironment.LOCAL_MACHINE

								if "mixed_precision" not in config_dict:
														UpperCamelCase_    :  int				    =    """fp16""" if ("""fp16""" in config_dict and config_dict["""fp16"""]) else None
								if isinstance(config_dict["""mixed_precision"""] ,						_SCREAMING_SNAKE_CASE) and not config_dict["mixed_precision"]:
														UpperCamelCase_    :  Union[str, 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_    :  Any				    =    config_dict.pop("""dynamo_backend""")
														UpperCamelCase_    :  Optional[Any]				    =    {} if dynamo_backend == """NO""" else {"""dynamo_backend""": dynamo_backend}
								if "use_cpu" not in config_dict:
														UpperCamelCase_    :  List[str]				    =    False
								return cls(**_SCREAMING_SNAKE_CASE)




		def   _snake_case  (     self				:List[Any] ,						_SCREAMING_SNAKE_CASE				:str)				-> int:






								'''simple docstring'''






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





		def   _snake_case  (     self				:Any)				-> int:






								'''simple docstring'''






								if isinstance(self.compute_environment ,						_SCREAMING_SNAKE_CASE):
														UpperCamelCase_    :  Dict				    =    ComputeEnvironment(self.compute_environment)
								if isinstance(self.distributed_type ,						_SCREAMING_SNAKE_CASE):
														if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
																				UpperCamelCase_    :  Optional[Any]				    =    SageMakerDistributedType(self.distributed_type)
														else:
																				UpperCamelCase_    :  Optional[int]				    =    DistributedType(self.distributed_type)
								if self.dynamo_config is None:
														UpperCamelCase_    :  str				    =    {}







@dataclass
class 						__snake_case (						_a  ):
		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   _snake_case  (     self				:List[str])				-> Tuple:






								'''simple docstring'''






								if self.deepspeed_config is None:
														UpperCamelCase_    :  Tuple				    =    {}
								if self.fsdp_config is None:
														UpperCamelCase_    :  Union[str, Any]				    =    {}
								if self.megatron_lm_config is None:
														UpperCamelCase_    :  int				    =    {}
								if self.ipex_config is None:
														UpperCamelCase_    :  Optional[int]				    =    {}
								return super().__post_init__()







@dataclass
class 						__snake_case (						_a  ):
		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




