





# 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									=				os.path.expanduser(
    os.getenv("HF_HOME", os.path.join(os.getenv("XDG_CACHE_HOME", "~/.cache"), "huggingface"))
)
_a									=				os.path.join(hf_cache_home, "accelerate")
_a									=				os.path.join(cache_dir, "default_config.yaml")
_a									=				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									=				default_yaml_config_file
else:
			_a									=				default_json_config_file





def lowerCAmelCase__(__snake_case							)    ->				List[Any]:







	'''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:
		lowerCamelCase__        =		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
			):
				lowerCamelCase__        =		ClusterConfig
			else:
				lowerCamelCase__        =		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
			):
				lowerCamelCase__        =		ClusterConfig
			else:
				lowerCamelCase__        =		SageMakerConfig
			return config_class.from_yaml_file(yaml_file=__snake_case							)






@dataclass
class  __A   :

			'''simple docstring'''







			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						42





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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





			@classmethod
			def 		__lowerCamelCase    (   cls	,  __lowerCAmelCase=None							):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase							):



				'''simple docstring'''







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





			@classmethod
			def 		__lowerCamelCase    (   cls	,  __lowerCAmelCase=None							):



				'''simple docstring'''







				lowerCamelCase__        =		default_yaml_config_file if yaml_file is None else yaml_file
				with open(__lowerCAmelCase	,  '''r'''	,  encoding='''utf-8'''							) as f:
					lowerCamelCase__        =		yaml.safe_load(__lowerCAmelCase							)
				if "compute_environment" not in config_dict:
					lowerCamelCase__        =		ComputeEnvironment.LOCAL_MACHINE

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





			def 		__lowerCamelCase    (   self	,  __lowerCAmelCase							):



				'''simple docstring'''







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





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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






@dataclass
class  __A   (   lowerCAmelCase			):

			'''simple docstring'''







			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						0
			lowerCAmelCase_												=						1
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						"static"
			lowerCAmelCase_												=						False
			lowerCAmelCase_												=						"main"

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

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

			# args for dynamo
			lowerCAmelCase_												=						None





			def 		__lowerCamelCase    (   self							):



				'''simple docstring'''







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






@dataclass
class  __A   (   lowerCAmelCase			):

			'''simple docstring'''







			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						42
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						"us-east-1"
			lowerCAmelCase_												=						1
			lowerCAmelCase_												=						"all"
			lowerCAmelCase_												=						F"""accelerate-sagemaker-{num_machines}"""
			lowerCAmelCase_												=						SAGEMAKER_PYTORCH_VERSION
			lowerCAmelCase_												=						SAGEMAKER_TRANSFORMERS_VERSION
			lowerCAmelCase_												=						SAGEMAKER_PYTHON_VERSION
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None
			lowerCAmelCase_												=						None










from __future__ import annotations

import math
import random





def lowerCAmelCase__(__snake_case	,__snake_case	,__snake_case							)    ->				list[int]:







	'''simple docstring'''







	lowerCamelCase__        =		d * e - 1
	lowerCamelCase__        =		0
	lowerCamelCase__        =		0
	while p == 0:
		lowerCamelCase__        =		random.randint(2	,n - 1							)
		lowerCamelCase__        =		k
		while True:
			if t % 2 == 0:
				lowerCamelCase__        =		t // 2
				lowerCamelCase__        =		(g**t) % n
				lowerCamelCase__        =		math.gcd(x - 1	,__snake_case							)
				if x > 1 and y > 1:
					lowerCamelCase__        =		y
					lowerCamelCase__        =		n // y
					break  # find the correct factors
			else:
				break  # t is not divisible by 2, break and choose another g
	return sorted([p, q]							)


if __name__ == "__main__":
			import doctest

			doctest.testmod()



