



# 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        __a     (    lowerCAmelCase_   :					List[Any]				)      -> Union[str, Any]:






						'''simple docstring'''



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






@dataclass
class 		lowercase						:






	"""simple docstring"""


	a__      :					ComputeEnvironment
	a__      :					Union[DistributedType, SageMakerDistributedType]
	a__      :					str
	a__      :					bool




	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[Any]     )			-> Optional[Any]:
							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(__UpperCAmelCase						,  __UpperCAmelCase     ):
																			UpperCAmelCase_=   value.value
													if isinstance(__UpperCAmelCase						,  __UpperCAmelCase     ) and not bool(__UpperCAmelCase     ):
																			UpperCAmelCase_=   None
							UpperCAmelCase_=   {k: v for k, v in result.items() if v is not None}
							return result




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




	def   _SCREAMING_SNAKE_CASE		(	self	:						Tuple						,  __UpperCAmelCase	:						int     )			-> Any:
							with open(__UpperCAmelCase						,  """w"""						,  encoding="""utf-8"""     ) as f:
													UpperCAmelCase_=   json.dumps(self.to_dict()						,  indent=2						,  sort_keys=__UpperCAmelCase     ) + """\n"""
													f.write(__UpperCAmelCase     )




	@classmethod
	def   _SCREAMING_SNAKE_CASE		(	cls	:						Optional[Any]						,  __UpperCAmelCase	:						int=None     )			-> Optional[Any]:
							UpperCAmelCase_=   default_yaml_config_file if yaml_file is None else yaml_file
							with open(__UpperCAmelCase						,  """r"""						,  encoding="""utf-8"""     ) as f:
													UpperCAmelCase_=   yaml.safe_load(__UpperCAmelCase     )
							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"""]						,  __UpperCAmelCase     ) 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(**__UpperCAmelCase     )




	def   _SCREAMING_SNAKE_CASE		(	self	:						Dict						,  __UpperCAmelCase	:						int     )			-> Dict:
							with open(__UpperCAmelCase						,  """w"""						,  encoding="""utf-8"""     ) as f:
													yaml.safe_dump(self.to_dict()						,  __UpperCAmelCase     )




	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[Any]     )			-> Optional[int]:
							if isinstance(self.compute_environment						,  __UpperCAmelCase     ):
													UpperCAmelCase_=   ComputeEnvironment(self.compute_environment     )
							if isinstance(self.distributed_type						,  __UpperCAmelCase     ):
													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 		lowercase						( snake_case__):






	"""simple docstring"""


	a__      :					int
	a__      :					int		   =							0
	a__      :					int		   =							1
	a__      :					Optional[str]		   =							None
	a__      :					Optional[str]		   =							None
	a__      :					Optional[int]		   =							None
	a__      :					Optional[str]		   =							"static"
	a__      :					Optional[bool]		   =							False
	a__      :					str		   =							"main"

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

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

	# args for dynamo
	a__      :					dict		   =							None




	def   _SCREAMING_SNAKE_CASE		(	self	:						Optional[int]     )			-> str:
							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 		lowercase						( snake_case__):






	"""simple docstring"""


	a__      :					str
	a__      :					str
	a__      :					Optional[str]		   =							None
	a__      :					Optional[str]		   =							None
	a__      :					str		   =							"us-east-1"
	a__      :					int		   =							1
	a__      :					str		   =							"all"
	a__      :					str		   =							F'accelerate-sagemaker-{num_machines}'
	a__      :					str		   =							SAGEMAKER_PYTORCH_VERSION
	a__      :					str		   =							SAGEMAKER_TRANSFORMERS_VERSION
	a__      :					str		   =							SAGEMAKER_PYTHON_VERSION
	a__      :					str		   =							None
	a__      :					str		   =							None
	a__      :					dict		   =							None
	a__      :					dict		   =							None









from __future__ import annotations



def        __a     (    lowerCAmelCase_   :					int				)      -> int | None:






						'''simple docstring'''



						for r in range(1			,lowerCAmelCase_				):
												UpperCAmelCase_=   []
												for x in range(n - 1				):
																		UpperCAmelCase_=   pow(lowerCAmelCase_			,lowerCAmelCase_			,lowerCAmelCase_				)
																		if val in li:
																								break
																		li.append(lowerCAmelCase_				)
												else:
																		return r
						return None


if __name__ == "__main__":
	__A				    =						int(input('''Enter a prime number q: '''))
	__A				    =						find_primitive(q)
	if a is None:
		print(f'Cannot find the primitive for the value: {a!r}')
	else:
		__A				    =						int(input('''Enter private key of A: '''))
		__A				    =						pow(a, a_private, q)
		__A				    =						int(input('''Enter private key of B: '''))
		__A				    =						pow(a, b_private, q)

		__A				    =						pow(b_public, a_private, q)
		__A				    =						pow(a_public, b_private, q)

		print('''The key value generated by A is: ''', a_secret)
		print('''The key value generated by B is: ''', b_secret)




