





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 	UpperCAmelCase       (		lowerCAmelCase_       )	->	List[Any]:
     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:
          a  :    str				=						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
               ):
                    a  :    str				=						ClusterConfig
               else:
                    a  :    List[Any]				=						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
               ):
                    a  :    List[Any]				=						ClusterConfig
               else:
                    a  :    Dict				=						SageMakerConfig
               return config_class.from_yaml_file(yaml_file=_SCREAMING_SNAKE_CASE       )






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





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

        if "mixed_precision" not in config_dict:
             a  :    Any				=						'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"]:
             a  :    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.
             a  :    Optional[int]				=						config_dict.pop('''dynamo_backend'''    )
             a  :    List[Any]				=						{} if dynamo_backend == 'NO' else {'dynamo_backend': dynamo_backend}
        if "use_cpu" not in config_dict:
             a  :    List[str]				=						False
        return cls(**__a    )
   def _lowercase(    self							,     lowerCAmelCase_    )	->Dict:





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


   def _lowercase(    self    )	->int:





        if isinstance(self.compute_environment							,     __a    ):
             a  :    str				=						ComputeEnvironment(self.compute_environment    )
        if isinstance(self.distributed_type							,     __a    ):
             if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
                  a  :    Union[str, Any]				=						SageMakerDistributedType(self.distributed_type    )
             else:
                  a  :    int				=						DistributedType(self.distributed_type    )
        if self.dynamo_config is None:
             a  :    Any				=						{}






@dataclass
class 							_SCREAMING_SNAKE_CASE(   lowerCAmelCase_	):
   UpperCamelCase_				:						Tuple 			=   42
   UpperCamelCase_				:						Any 			=   0
   UpperCamelCase_				:						Dict 			=   1
   UpperCamelCase_				:						Union[str, Any] 			=   None
   UpperCamelCase_				:						str 			=   None
   UpperCamelCase_				:						List[str] 			=   None
   UpperCamelCase_				:						str 			=   'static'
   UpperCamelCase_				:						Optional[int] 			=   False
   UpperCamelCase_				:						Union[str, Any] 			=   'main'

   # args for deepspeed_plugin
   UpperCamelCase_				:						Tuple 			=   None
   # args for fsdp
   UpperCamelCase_				:						List[str] 			=   None
   # args for megatron_lm
   UpperCamelCase_				:						Union[str, Any] 			=   None
   # args for ipex
   UpperCamelCase_				:						List[str] 			=   None
   # args for TPU
   UpperCamelCase_				:						Optional[Any] 			=   False

   # args for TPU pods
   UpperCamelCase_				:						Optional[Any] 			=   None
   UpperCamelCase_				:						List[Any] 			=   None
   UpperCamelCase_				:						List[Any] 			=   False
   UpperCamelCase_				:						Dict 			=   False
   UpperCamelCase_				:						Any 			=   None
   UpperCamelCase_				:						Optional[Any] 			=   None
   UpperCamelCase_				:						List[str] 			=   None
   UpperCamelCase_				:						Union[str, Any] 			=   None

   # args for dynamo
   UpperCamelCase_				:						Optional[Any] 			=   None
   def _lowercase(    self    )	->Union[str, Any]:





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






@dataclass
class 							_SCREAMING_SNAKE_CASE(   lowerCAmelCase_	):
   UpperCamelCase_				:						Dict 			=   42
   UpperCamelCase_				:						List[str] 			=   42
   UpperCamelCase_				:						List[str] 			=   None
   UpperCamelCase_				:						Optional[int] 			=   None
   UpperCamelCase_				:						Dict 			=   'us-east-1'
   UpperCamelCase_				:						Any 			=   1
   UpperCamelCase_				:						Optional[Any] 			=   'all'
   UpperCamelCase_				:						Any 			=   F"""accelerate-sagemaker-{num_machines}"""
   UpperCamelCase_				:						Any 			=   SAGEMAKER_PYTORCH_VERSION
   UpperCamelCase_				:						Optional[int] 			=   SAGEMAKER_TRANSFORMERS_VERSION
   UpperCamelCase_				:						List[Any] 			=   SAGEMAKER_PYTHON_VERSION
   UpperCamelCase_				:						Optional[int] 			=   None
   UpperCamelCase_				:						Any 			=   None
   UpperCamelCase_				:						Union[str, Any] 			=   None
   UpperCamelCase_				:						str 			=   None
from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase							   =      logging.get_logger(__name__)

UpperCAmelCase							   =      {
    # pcqm4mv1 now deprecated
    """graphormer-base""": """https://huggingface.co/clefourrier/graphormer-base-pcqm4mv2/resolve/main/config.json""",
    # See all Graphormer models at https://huggingface.co/models?filter=graphormer
}






class 							_SCREAMING_SNAKE_CASE(   snake_case__	):
   UpperCamelCase_				:						int 			=   'graphormer'
   UpperCamelCase_				:						Tuple 			=   ['past_key_values']
   def __init__(    self							,     lowerCAmelCase_ = 1							,     lowerCAmelCase_ = 5_12 * 9							,     lowerCAmelCase_ = 5_12 * 3							,     lowerCAmelCase_ = 5_12							,     lowerCAmelCase_ = 5_12							,     lowerCAmelCase_ = 5_12							,     lowerCAmelCase_ = 1_28							,     lowerCAmelCase_ = 5							,     lowerCAmelCase_ = 10_24							,     lowerCAmelCase_ = "multi_hop"							,     lowerCAmelCase_ = 5_12							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = 12							,     lowerCAmelCase_ = 7_68							,     lowerCAmelCase_ = 7_68							,     lowerCAmelCase_ = 32							,     lowerCAmelCase_ = 0.1							,     lowerCAmelCase_ = 0.1							,     lowerCAmelCase_ = 0.0							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = "gelu"							,     lowerCAmelCase_ = None							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = 0							,     lowerCAmelCase_ = False							,     lowerCAmelCase_ = 0.0							,     lowerCAmelCase_ = 8							,     lowerCAmelCase_ = None							,     lowerCAmelCase_ = None							,     lowerCAmelCase_ = True							,     lowerCAmelCase_ = True							,     lowerCAmelCase_=0							,     lowerCAmelCase_=1							,     lowerCAmelCase_=2							,     **lowerCAmelCase_							,     )	->Optional[int]:
        a  :    List[Any]				=						num_classes
        a  :    Any				=						num_atoms
        a  :    Dict				=						num_in_degree
        a  :    int				=						num_out_degree
        a  :    Dict				=						num_edges
        a  :    Tuple				=						num_spatial
        a  :    Dict				=						num_edge_dis
        a  :    Optional[int]				=						edge_type
        a  :    List[Any]				=						multi_hop_max_dist
        a  :    Optional[int]				=						spatial_pos_max
        a  :    str				=						max_nodes
        a  :    Optional[Any]				=						num_hidden_layers
        a  :    Any				=						embedding_dim
        a  :    Dict				=						embedding_dim
        a  :    Dict				=						ffn_embedding_dim
        a  :    List[Any]				=						num_attention_heads
        a  :    List[str]				=						dropout
        a  :    int				=						attention_dropout
        a  :    Optional[Any]				=						layerdrop
        a  :    str				=						encoder_normalize_before
        a  :    Optional[int]				=						pre_layernorm
        a  :    Tuple				=						apply_graphormer_init
        a  :    int				=						activation_fn
        a  :    Any				=						embed_scale
        a  :    Optional[int]				=						freeze_embeddings
        a  :    Tuple				=						num_trans_layers_to_freeze
        a  :    List[str]				=						share_input_output_embed
        a  :    List[Any]				=						traceable
        a  :    Optional[Any]				=						q_noise
        a  :    Dict				=						qn_block_size

        # These parameters are here for future extensions
        # atm, the model only supports self attention
        a  :    List[Any]				=						kdim
        a  :    List[str]				=						vdim
        a  :    int				=						self_attention
        a  :    List[Any]				=						bias

        super().__init__(
            pad_token_id=lowerCAmelCase_							,     bos_token_id=lowerCAmelCase_							,     eos_token_id=lowerCAmelCase_							,     **lowerCAmelCase_							,     )