






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


_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 _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:
            UpperCAmelCase							=       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
                  ):
                        UpperCAmelCase							=       ClusterConfig
                  else:
                        UpperCAmelCase							=       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
                  ):
                        UpperCAmelCase							=       ClusterConfig
                  else:
                        UpperCAmelCase							=       SageMakerConfig
                  return config_class.from_yaml_file(yaml_file=_snake_case							)







@dataclass
class 					lowerCamelCase__					:
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			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(snake_case__	,snake_case__					):
                      UpperCAmelCase							=       value.value
                if isinstance(snake_case__	,snake_case__					) and not bool(snake_case__					):
                      UpperCAmelCase							=       None
          UpperCAmelCase							=       {k: v for k, v in result.items() if v is not None}
          return result






    @classmethod
    def 				_UpperCamelCase							( cls	,A=None					):
          UpperCAmelCase							=       default_json_config_file if json_file is None else json_file
          with open(snake_case__	,"""r"""	,encoding="""utf-8"""					) as f:
                UpperCAmelCase							=       json.load(snake_case__					)
          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(**snake_case__					)






    def 				_UpperCamelCase							( self	,A					):





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






    @classmethod
    def 				_UpperCamelCase							( cls	,A=None					):
          UpperCAmelCase							=       default_yaml_config_file if yaml_file is None else yaml_file
          with open(snake_case__	,"""r"""	,encoding="""utf-8"""					) as f:
                UpperCAmelCase							=       yaml.safe_load(snake_case__					)
          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"""]	,snake_case__					) 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(**snake_case__					)






    def 				_UpperCamelCase							( self	,A					):





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






    def 				_UpperCamelCase							( self					):





          if isinstance(self.compute_environment	,snake_case__					):
                UpperCAmelCase							=       ComputeEnvironment(self.compute_environment					)
          if isinstance(self.distributed_type	,snake_case__					):
                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__					(       a_ ):
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			0
    SCREAMING_SNAKE_CASE		=			1
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			"static"
    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			"main"

    # args for deepspeed_plugin
    SCREAMING_SNAKE_CASE		=			None
    # args for fsdp
    SCREAMING_SNAKE_CASE		=			None
    # args for megatron_lm
    SCREAMING_SNAKE_CASE		=			None
    # args for ipex
    SCREAMING_SNAKE_CASE		=			None
    # args for TPU
    SCREAMING_SNAKE_CASE		=			False

    # args for TPU pods
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			False
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None

    # args for dynamo
    SCREAMING_SNAKE_CASE		=			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__					(       a_ ):
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			42
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			"us-east-1"
    SCREAMING_SNAKE_CASE		=			1
    SCREAMING_SNAKE_CASE		=			"all"
    SCREAMING_SNAKE_CASE		=			f"accelerate-sagemaker-{num_machines}"
    SCREAMING_SNAKE_CASE		=			SAGEMAKER_PYTORCH_VERSION
    SCREAMING_SNAKE_CASE		=			SAGEMAKER_TRANSFORMERS_VERSION
    SCREAMING_SNAKE_CASE		=			SAGEMAKER_PYTHON_VERSION
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None
    SCREAMING_SNAKE_CASE		=			None











"""simple docstring"""







import unittest
from timeit import timeit






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	first_num if first_num >= second_num else second_num
       UpperCAmelCase					=	max_num
       while (common_mult % first_num > 0) or (common_mult % second_num > 0):
              common_mult += max_num
       return common_mult






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return b if a == 0 else greatest_common_divisor(b % a		,					lowerCAmelCase						)






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return first_num // greatest_common_divisor(lowerCAmelCase		,					lowerCAmelCase						) * second_num






def 					_lowerCAmelCase				(					):







       '''simple docstring'''
       UpperCAmelCase					=	(
           """from __main__ import least_common_multiple_slow, least_common_multiple_fast"""
       )
       print(
           """least_common_multiple_slow():"""		,					timeit("""least_common_multiple_slow(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)
       print(
           """least_common_multiple_fast():"""		,					timeit("""least_common_multiple_fast(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)





class      UpperCamelCase_       (				unittest.TestCase     ):
  _A				:    List[str] =		(
      (10, 20),
      (13, 15),
      (4, 31),
      (10, 42),
      (43, 34),
      (5, 12),
      (12, 25),
      (10, 25),
      (6, 9),
  )
  _A				:    int =		(20, 195, 124, 210, 1462, 60, 300, 50, 18)



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         for i, (first_num, second_num) in enumerate(self.test_inputs ):
                UpperCAmelCase					=	least_common_multiple_slow(snake_case__       ,						snake_case__ )
                UpperCAmelCase					=	least_common_multiple_fast(snake_case__       ,						snake_case__ )
                with self.subTest(i=snake_case__ ):
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )


if __name__ == "__main__":
 benchmark()
 unittest.main()
