


"""simple docstring"""






# 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


UpperCamelCase__							:List[Any]       =  os.path.expanduser(
    os.getenv("""HF_HOME""", os.path.join(os.getenv("""XDG_CACHE_HOME""", """~/.cache"""), """huggingface"""))
)
UpperCamelCase__							:List[str]       =  os.path.join(hf_cache_home, """accelerate""")
UpperCamelCase__							:int       =  os.path.join(cache_dir, """default_config.yaml""")
UpperCamelCase__							:Union[str, Any]       =  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__							:List[str]       =  default_yaml_config_file
else:
					UpperCamelCase__							:Tuple       =  default_json_config_file





def 							A_					(   snake_case__		)				->     Optional[Any]:
			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				:int				    =  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				:Tuple				    =  ClusterConfig
									else:
												_UpperCamelCase				:int				    =  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				:str				    =  ClusterConfig
									else:
												_UpperCamelCase				:List[str]				    =  SageMakerConfig
									return config_class.from_yaml_file(yaml_file=snake_case__		)






@dataclass
class     A:

		"""simple docstring"""





		A         =       42
		A         =       42
		A         =       42
		A         =       42
		def      _UpperCamelCase(   self      )			->  Any:

					"""simple docstring"""



					_UpperCamelCase				:Dict				    =  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(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__      ):
											_UpperCamelCase				:Tuple				    =  value.value
								if isinstance(SCREAMING_SNAKE_CASE__				,							SCREAMING_SNAKE_CASE__      ) and not bool(SCREAMING_SNAKE_CASE__      ):
											_UpperCamelCase				:Union[str, Any]				    =  None
					_UpperCamelCase				:Dict				    =  {k: v for k, v in result.items() if v is not None}
					return result
		@classmethod
		def      _UpperCamelCase(   cls				,							SCREAMING_SNAKE_CASE__=None      )			->  Tuple:

					"""simple docstring"""



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

					"""simple docstring"""



					with open(SCREAMING_SNAKE_CASE__				,							'''w'''				,							encoding='''utf-8'''      ) as f:
								_UpperCamelCase				:str				    =  json.dumps(self.to_dict()				,							indent=2				,							sort_keys=SCREAMING_SNAKE_CASE__      ) + '''\n'''
								f.write(SCREAMING_SNAKE_CASE__      )
		@classmethod
		def      _UpperCamelCase(   cls				,							SCREAMING_SNAKE_CASE__=None      )			->  Optional[int]:

					"""simple docstring"""



					_UpperCamelCase				:Union[str, Any]				    =  default_yaml_config_file if yaml_file is None else yaml_file
					with open(SCREAMING_SNAKE_CASE__				,							'''r'''				,							encoding='''utf-8'''      ) as f:
								_UpperCamelCase				:Tuple				    =  yaml.safe_load(SCREAMING_SNAKE_CASE__      )
					if "compute_environment" not in config_dict:
								_UpperCamelCase				:int				    =  ComputeEnvironment.LOCAL_MACHINE

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

					"""simple docstring"""



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





		def      _UpperCamelCase(   self      )			->  Tuple:

					"""simple docstring"""



					if isinstance(self.compute_environment				,							SCREAMING_SNAKE_CASE__      ):
								_UpperCamelCase				:Optional[int]				    =  ComputeEnvironment(self.compute_environment      )
					if isinstance(self.distributed_type				,							SCREAMING_SNAKE_CASE__      ):
								if self.compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
											_UpperCamelCase				:int				    =  SageMakerDistributedType(self.distributed_type      )
								else:
											_UpperCamelCase				:Optional[int]				    =  DistributedType(self.distributed_type      )
					if self.dynamo_config is None:
								_UpperCamelCase				:List[str]				    =  {}






@dataclass
class     A(     lowerCamelCase__		):

		"""simple docstring"""





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

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

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

		# args for dynamo
		A         =       None
		def      _UpperCamelCase(   self      )			->  List[str]:

					"""simple docstring"""



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






@dataclass
class     A(     lowerCamelCase__		):

		"""simple docstring"""





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





"""simple docstring"""






from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL

from ...utils import BaseOutput, is_invisible_watermark_available, is_torch_available, is_transformers_available






@dataclass
class     A(     lowerCamelCase__		):

		"""simple docstring"""





		A         =       42


if is_transformers_available() and is_torch_available() and is_invisible_watermark_available():
					from .pipeline_stable_diffusion_xl import StableDiffusionXLPipeline
					from .pipeline_stable_diffusion_xl_imgaimg import StableDiffusionXLImgaImgPipeline

