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


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





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

@dataclass
class       lowerCamelCase_							:






   '''simple docstring'''

   A :     Optional[Any]				=	42
   A :     Any				=	42
   A :     Any				=	42
   A :     List[str]				=	42


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


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


   def 	_UpperCamelCase       (  self     ,						__UpperCamelCase ):





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


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

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


   def 	_UpperCamelCase       (  self     ,						__UpperCamelCase ):





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







   def 	_UpperCamelCase       (  self ):





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

@dataclass
class       lowerCamelCase_							(	lowerCAmelCase_  ):






   '''simple docstring'''

   A :     Any				=	42
   A :     Any				=	0
   A :     Dict				=	1
   A :     List[str]				=	None
   A :     Dict				=	None
   A :     Any				=	None
   A :     int				=	"static"
   A :     Optional[int]				=	False
   A :     Union[str, Any]				=	"main"

   # args for deepspeed_plugin
   A :     Optional[Any]				=	None
   # args for fsdp
   A :     List[str]				=	None
   # args for megatron_lm
   A :     Optional[int]				=	None
   # args for ipex
   A :     Dict				=	None
   # args for TPU
   A :     List[Any]				=	False

   # args for TPU pods
   A :     List[Any]				=	None
   A :     Optional[int]				=	None
   A :     Optional[int]				=	False
   A :     Any				=	False
   A :     Optional[Any]				=	None
   A :     Optional[int]				=	None
   A :     Optional[Any]				=	None
   A :     Optional[int]				=	None

   # args for dynamo
   A :     Optional[Any]				=	None


   def 	_UpperCamelCase       (  self ):





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

@dataclass
class       lowerCamelCase_							(	lowerCAmelCase_  ):






   '''simple docstring'''

   A :     Optional[Any]				=	42
   A :     Union[str, Any]				=	42
   A :     Optional[Any]				=	None
   A :     int				=	None
   A :     str				=	"us-east-1"
   A :     int				=	1
   A :     int				=	"all"
   A :     Optional[int]				=	F'''accelerate-sagemaker-{num_machines}'''
   A :     Union[str, Any]				=	SAGEMAKER_PYTORCH_VERSION
   A :     str				=	SAGEMAKER_TRANSFORMERS_VERSION
   A :     List[Any]				=	SAGEMAKER_PYTHON_VERSION
   A :     Any				=	None
   A :     Optional[Any]				=	None
   A :     Any				=	None
   A :     Optional[Any]				=	None









'''simple docstring'''







import heapq





def 						snake_case__ (lowerCAmelCase_					,  lowerCAmelCase_					,  lowerCAmelCase_	):
    UpperCamelCase__      =			[(0, start)]  # cost from start node,end node
    UpperCamelCase__      =			set()
    while heap:
        ((UpperCamelCase__)    ,  (UpperCamelCase__))      =			heapq.heappop(lowerCAmelCase_	)
        if u in visited:
            continue
        visited.add(lowerCAmelCase_	)
        if u == end:
            return cost
        for v, c in graph[u]:
            if v in visited:
                continue
            UpperCamelCase__      =			cost + c
            heapq.heappush(lowerCAmelCase_					,  (next_item, v)	)
    return -1


lowercase__     :  Any    					=			{
    'A': [['B', 2], ['C', 5]],
    'B': [['A', 2], ['D', 3], ['E', 1], ['F', 1]],
    'C': [['A', 5], ['F', 3]],
    'D': [['B', 3]],
    'E': [['B', 4], ['F', 3]],
    'F': [['C', 3], ['E', 3]],
}
lowercase__     :  Optional[int]    					=			{
    'B': [['C', 1]],
    'C': [['D', 1]],
    'D': [['F', 1]],
    'E': [['B', 1], ['F', 3]],
    'F': [],
}
lowercase__     :  Union[str, Any]    					=			{
    'B': [['C', 1]],
    'C': [['D', 1]],
    'D': [['F', 1]],
    'E': [['B', 1], ['G', 2]],
    'F': [],
    'G': [['F', 1]],
}

lowercase__     :  Union[str, Any]    					=			dijkstra(G, 'E', 'C')
print(short_distance)  # E -- 3 --> F -- 3 --> C == 6

lowercase__     :  Optional[int]    					=			dijkstra(Ga, 'E', 'F')
print(short_distance)  # E -- 3 --> F == 3

lowercase__     :  Any    					=			dijkstra(Ga, 'E', 'F')
print(short_distance)  # E -- 2 --> G -- 1 --> F == 3

if __name__ == "__main__":
      import doctest

      doctest.testmod()




