

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


A_							:     Union[str, Any]     =	os.path.expanduser(
    os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
)
A_							:     Union[str, Any]     =	os.path.join(hf_cache_home, "accelerate")
A_							:     Union[str, Any]     =	os.path.join(cache_dir, "default_config.yaml")
A_							:     List[Any]     =	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):
						A_							:     int     =	default_yaml_config_file
else:
						A_							:     Any     =	default_json_config_file

def 		A    (       snake_case__      ):




			'''simple docstring'''


			if config_file is not None:
						if not os.path.isfile(snake_case__      ):
									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:
						SCREAMING_SNAKE_CASE__									= default_config_file
			with open(snake_case__						,			"""r"""						,			encoding="""utf-8"""      ) as f:
						if config_file.endswith(""".json"""      ):
									if (
									    json.load(snake_case__      ).get("""compute_environment"""						,			ComputeEnvironment.LOCAL_MACHINE      )
									    == ComputeEnvironment.LOCAL_MACHINE
									):
												SCREAMING_SNAKE_CASE__									= ClusterConfig
									else:
												SCREAMING_SNAKE_CASE__									= SageMakerConfig
									return config_class.from_json_file(json_file=snake_case__      )
						else:
									if (
									    yaml.safe_load(snake_case__      ).get("""compute_environment"""						,			ComputeEnvironment.LOCAL_MACHINE      )
									    == ComputeEnvironment.LOCAL_MACHINE
									):
												SCREAMING_SNAKE_CASE__									= ClusterConfig
									else:
												SCREAMING_SNAKE_CASE__									= SageMakerConfig
									return config_class.from_yaml_file(yaml_file=snake_case__      )





@dataclass
class 				lowerCamelCase  :
			lowerCamelCase__				:						ComputeEnvironment
			lowerCamelCase__				:						Union[DistributedType, SageMakerDistributedType]
			lowerCamelCase__				:						str
			lowerCamelCase__				:						bool

			def    SCREAMING_SNAKE_CASE			(   self			:  List[Any]    )				->      Any:
						SCREAMING_SNAKE_CASE__									= 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    ):
												SCREAMING_SNAKE_CASE__									= value.value
									if isinstance(__UpperCAmelCase      ,			__UpperCAmelCase    ) and not bool(__UpperCAmelCase    ):
												SCREAMING_SNAKE_CASE__									= None
						SCREAMING_SNAKE_CASE__									= {k: v for k, v in result.items() if v is not None}
						return result

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

			def    SCREAMING_SNAKE_CASE			(   self			:  Optional[int]      ,			__UpperCAmelCase			:  Union[str, Any]    )				->      Optional[int]:
						with open(__UpperCAmelCase      ,			"""w"""      ,			encoding="""utf-8"""    ) as f:
									SCREAMING_SNAKE_CASE__									= json.dumps(self.to_dict()      ,			indent=2      ,			sort_keys=__UpperCAmelCase    ) + """\n"""
									f.write(__UpperCAmelCase    )

			@classmethod
			def    SCREAMING_SNAKE_CASE			(   cls			:  str      ,			__UpperCAmelCase			:  Union[str, Any]=None    )				->      Dict:
						SCREAMING_SNAKE_CASE__									= default_yaml_config_file if yaml_file is None else yaml_file
						with open(__UpperCAmelCase      ,			"""r"""      ,			encoding="""utf-8"""    ) as f:
									SCREAMING_SNAKE_CASE__									= yaml.safe_load(__UpperCAmelCase    )
						if "compute_environment" not in config_dict:
									SCREAMING_SNAKE_CASE__									= ComputeEnvironment.LOCAL_MACHINE

						if "mixed_precision" not in config_dict:
									SCREAMING_SNAKE_CASE__									= """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"]:
									SCREAMING_SNAKE_CASE__									= """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.
									SCREAMING_SNAKE_CASE__									= config_dict.pop("""dynamo_backend"""    )
									SCREAMING_SNAKE_CASE__									= {} if dynamo_backend == """NO""" else {"""dynamo_backend""": dynamo_backend}
						if "use_cpu" not in config_dict:
									SCREAMING_SNAKE_CASE__									= False
						return cls(**__UpperCAmelCase    )

			def    SCREAMING_SNAKE_CASE			(   self			:  Any      ,			__UpperCAmelCase			:  Tuple    )				->      Dict:
						with open(__UpperCAmelCase      ,			"""w"""      ,			encoding="""utf-8"""    ) as f:
									yaml.safe_dump(self.to_dict()      ,			__UpperCAmelCase    )





			def    SCREAMING_SNAKE_CASE			(   self			:  Optional[Any]    )				->      str:
						if isinstance(self.compute_environment      ,			__UpperCAmelCase    ):
									SCREAMING_SNAKE_CASE__									= ComputeEnvironment(self.compute_environment    )
						if isinstance(self.distributed_type      ,			__UpperCAmelCase    ):
									if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
												SCREAMING_SNAKE_CASE__									= SageMakerDistributedType(self.distributed_type    )
									else:
												SCREAMING_SNAKE_CASE__									= DistributedType(self.distributed_type    )
						if self.dynamo_config is None:
									SCREAMING_SNAKE_CASE__									= {}





@dataclass
class 				lowerCamelCase  (A__     ):
			lowerCamelCase__				:						int
			lowerCamelCase__				:						int   						=							0
			lowerCamelCase__				:						int   						=							1
			lowerCamelCase__				:						Optional[str]   						=							None
			lowerCamelCase__				:						Optional[str]   						=							None
			lowerCamelCase__				:						Optional[int]   						=							None
			lowerCamelCase__				:						Optional[str]   						=							"static"
			lowerCamelCase__				:						Optional[bool]   						=							False
			lowerCamelCase__				:						str   						=							"main"

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

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

			# args for dynamo
			lowerCamelCase__				:						dict   						=							None

			def    SCREAMING_SNAKE_CASE			(   self			:  Tuple    )				->      Dict:
						if self.deepspeed_config is None:
									SCREAMING_SNAKE_CASE__									= {}
						if self.fsdp_config is None:
									SCREAMING_SNAKE_CASE__									= {}
						if self.megatron_lm_config is None:
									SCREAMING_SNAKE_CASE__									= {}
						if self.ipex_config is None:
									SCREAMING_SNAKE_CASE__									= {}
						return super().__post_init__()





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





"""simple docstring"""




class 				lowerCamelCase  :

			def __init__(   self			:  Dict    )				->      None:
						SCREAMING_SNAKE_CASE__									= [0, 1]





			def    SCREAMING_SNAKE_CASE			(   self			:  Optional[int]      ,			__UpperCAmelCase			:  int    )				->      list:
						if (difference := index - (len(self.sequence    ) - 2)) >= 1:
									for _ in range(__UpperCAmelCase    ):
												self.sequence.append(self.sequence[-1] + self.sequence[-2]    )
						return self.sequence[:index]

def 		A    (       ):




			'''simple docstring'''


			print(
			    """Fibonacci Series Using Dynamic Programming\n"""						,			"""Enter the index of the Fibonacci number you want to calculate """						,			"""in the prompt below. (To exit enter exit or Ctrl-C)\n"""						,			sep=""""""						,			)
			SCREAMING_SNAKE_CASE__									= Fibonacci()

			while True:
						SCREAMING_SNAKE_CASE__									= input(""">> """      )
						if prompt in {"exit", "quit"}:
									break

						try:
									SCREAMING_SNAKE_CASE__									= int(snake_case__      )
						except ValueError:
									print("""Enter a number or 'exit'"""      )
									continue

						print(fibonacci.get(snake_case__      )      )


if __name__ == "__main__":
						main()


