


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     lowercase__	(  lowerCamelCase     :  int				)					->     int:
  if config_file is not None:
    if not os.path.isfile(lowerCamelCase				):
      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:
    lowerCAmelCase__    :    Optional[int]								=					default_config_file
  with open(lowerCamelCase				,	"r"				,	encoding="utf-8"				) as f:
    if config_file.endswith(".json"				):
      if (
          json.load(lowerCamelCase				).get("compute_environment"				,	ComputeEnvironment.LOCAL_MACHINE				)
          == ComputeEnvironment.LOCAL_MACHINE
      ):
        lowerCAmelCase__    :    Optional[int]								=					ClusterConfig
      else:
        lowerCAmelCase__    :    List[Any]								=					SageMakerConfig
      return config_class.from_json_file(json_file=lowerCamelCase				)
    else:
      if (
          yaml.safe_load(lowerCamelCase				).get("compute_environment"				,	ComputeEnvironment.LOCAL_MACHINE				)
          == ComputeEnvironment.LOCAL_MACHINE
      ):
        lowerCAmelCase__    :    Union[str, Any]								=					ClusterConfig
      else:
        lowerCAmelCase__    :    Dict								=					SageMakerConfig
      return config_class.from_yaml_file(yaml_file=lowerCamelCase				)

@dataclass
class  lowercase_  :
      __magic_name__     :							ComputeEnvironment
      __magic_name__     :							Union[DistributedType, SageMakerDistributedType]
      __magic_name__     :							str
      __magic_name__     :							bool







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







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







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







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

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







      def 				_lowerCAmelCase       (      self :							str		,  _lowercase :							Dict    ):
        with open(_lowercase		,  "w"		,  encoding="utf-8"    ) as f:
          yaml.safe_dump(self.to_dict()		,  _lowercase    )







      def 				_lowerCAmelCase       (      self :							Union[str, Any]    ):
        if isinstance(self.compute_environment		,  _lowercase    ):
          lowerCAmelCase__    :    Optional[int]								=					ComputeEnvironment(self.compute_environment    )
        if isinstance(self.distributed_type		,  _lowercase    ):
          if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
            lowerCAmelCase__    :    Union[str, Any]								=					SageMakerDistributedType(self.distributed_type    )
          else:
            lowerCAmelCase__    :    Tuple								=					DistributedType(self.distributed_type    )
        if self.dynamo_config is None:
          lowerCAmelCase__    :    List[str]								=					{}

@dataclass
class  lowercase_  ( a_						):
      __magic_name__     :							int
      __magic_name__     :							int          =				0
      __magic_name__     :							int          =				1
      __magic_name__     :							Optional[str]          =				None
      __magic_name__     :							Optional[str]          =				None
      __magic_name__     :							Optional[int]          =				None
      __magic_name__     :							Optional[str]          =				"static"
      __magic_name__     :							Optional[bool]          =				False
      __magic_name__     :							str          =				"main"

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

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

      # args for dynamo
      __magic_name__     :							dict          =				None







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

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




"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase    				=      {
    "configuration_layoutlm": ["LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "LayoutLMConfig", "LayoutLMOnnxConfig"],
    "tokenization_layoutlm": ["LayoutLMTokenizer"],
}

try:
   if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __UpperCAmelCase    				=      ["LayoutLMTokenizerFast"]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __UpperCAmelCase    				=      [
       "LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST",
       "LayoutLMForMaskedLM",
       "LayoutLMForSequenceClassification",
       "LayoutLMForTokenClassification",
       "LayoutLMForQuestionAnswering",
       "LayoutLMModel",
       "LayoutLMPreTrainedModel",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   __UpperCAmelCase    				=      [
       "TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST",
       "TFLayoutLMForMaskedLM",
       "TFLayoutLMForSequenceClassification",
       "TFLayoutLMForTokenClassification",
       "TFLayoutLMForQuestionAnswering",
       "TFLayoutLMMainLayer",
       "TFLayoutLMModel",
       "TFLayoutLMPreTrainedModel",
   ]


if TYPE_CHECKING:
   from .configuration_layoutlm import LAYOUTLM_PRETRAINED_CONFIG_ARCHIVE_MAP, LayoutLMConfig, LayoutLMOnnxConfig
   from .tokenization_layoutlm import LayoutLMTokenizer

   try:
      if not is_tokenizers_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_layoutlm_fast import LayoutLMTokenizerFast

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_layoutlm import (
          LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          LayoutLMForMaskedLM,
          LayoutLMForQuestionAnswering,
          LayoutLMForSequenceClassification,
          LayoutLMForTokenClassification,
          LayoutLMModel,
          LayoutLMPreTrainedModel,
      )
   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_layoutlm import (
          TF_LAYOUTLM_PRETRAINED_MODEL_ARCHIVE_LIST,
          TFLayoutLMForMaskedLM,
          TFLayoutLMForQuestionAnswering,
          TFLayoutLMForSequenceClassification,
          TFLayoutLMForTokenClassification,
          TFLayoutLMMainLayer,
          TFLayoutLMModel,
          TFLayoutLMPreTrainedModel,
      )

else:
   import sys

   __UpperCAmelCase    				=      _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
