






'''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 argparse
import os

from accelerate.utils import ComputeEnvironment

from .cluster import get_cluster_input
from .config_args import cache_dir, default_config_file, default_yaml_config_file, load_config_from_file  # noqa: F401
from .config_utils import _ask_field, _ask_options, _convert_compute_environment  # noqa: F401
from .sagemaker import get_sagemaker_input


UpperCAmelCase__						:List[str]          =			'Launches a series of prompts to create and save a `default_config.yaml` configuration file for your training system. Should always be ran first on your machine'
def     __lowercase  ()   ->     List[str]:


     """simple docstring"""
     __lowerCamelCase						:       Tuple				   =				_ask_options(
         """In which compute environment are you running?""",						["""This machine""", """AWS (Amazon SageMaker)"""],						_convert_compute_environment,						)
     if compute_environment == ComputeEnvironment.AMAZON_SAGEMAKER:
          __lowerCamelCase						:       int				   =				get_sagemaker_input()
     else:
          __lowerCamelCase						:       Optional[int]				   =				get_cluster_input()
     return config
def     __lowercase  (_lowercase=None     )   ->     str:


     """simple docstring"""
     if subparsers is not None:
          __lowerCamelCase						:       Dict				   =				subparsers.add_parser("""config""",						description=_A     )
     else:
          __lowerCamelCase						:       Optional[int]				   =				argparse.ArgumentParser("""Accelerate config command""",						description=_A     )

     parser.add_argument(
         """--config_file""",						default=_A,						help=(
             """The path to use to store the config file. Will default to a file named default_config.yaml in the cache """
             """location, which is the content of the environment `HF_HOME` suffixed with 'accelerate', or if you don't have """
             """such an environment variable, your cache directory ('~/.cache' or the content of `XDG_CACHE_HOME`) suffixed """
             """with 'huggingface'."""
         ),						)

     if subparsers is not None:
          parser.set_defaults(func=_A     )
     return parser
def     __lowercase  (_lowercase     )   ->     Optional[int]:


     """simple docstring"""
     __lowerCamelCase						:       List[Any]				   =				get_user_input()
     if args.config_file is not None:
          __lowerCamelCase						:       List[Any]				   =				args.config_file
     else:
          if not os.path.isdir(_A     ):
               os.makedirs(_A     )
          __lowerCamelCase						:       Tuple				   =				default_yaml_config_file

     if config_file.endswith(""".json"""     ):
          config.to_json_file(_A     )
     else:
          config.to_yaml_file(_A     )
     print(f"accelerate configuration saved at {config_file}"     )
def     __lowercase  ()   ->     Dict:


     """simple docstring"""
     __lowerCamelCase						:       Dict				   =				config_command_parser()
     __lowerCamelCase						:       List[Any]				   =				parser.parse_args()
     config_command(_A     )


if __name__ == "__main__":
   main()











'''simple docstring'''

import gc
import unittest

import numpy as np
import torch

from diffusers import StableDiffusionKDiffusionPipeline
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu


enable_full_determinism()







@slow
@require_torch_gpu
class 	SCREAMING_SNAKE_CASE	(      unittest.TestCase  ):




       def 					a_					(					self      : Union[str, Any]					):


            """simple docstring"""







            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()




       def 					a_					(					self      : Optional[int]					):


            """simple docstring"""







            __lowerCamelCase						:       Dict				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""					)
            __lowerCamelCase						:       Tuple				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       Any				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       List[Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Dict				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       Tuple				   =				output.images

            __lowerCamelCase						:       int				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       List[str]				   =				np.array([0.0447, 0.0492, 0.0468, 0.0408, 0.0383, 0.0408, 0.0354, 0.0380, 0.0339]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2




       def 					a_					(					self      : Dict					):


            """simple docstring"""







            __lowerCamelCase						:       Any				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       int				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_euler"""					)

            __lowerCamelCase						:       List[Any]				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Union[str, Any]				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       int				   =				sd_pipe([prompt]	,    generator=A__	,    guidance_scale=9.0	,    num_inference_steps=20	,    output_type="""np"""					)

            __lowerCamelCase						:       List[str]				   =				output.images

            __lowerCamelCase						:       Optional[int]				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       Optional[int]				   =				np.array([0.1237, 0.1320, 0.1438, 0.1359, 0.1390, 0.1132, 0.1277, 0.1175, 0.1112]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 5e-1




       def 					a_					(					self      : str					):


            """simple docstring"""







            __lowerCamelCase						:       List[str]				   =				StableDiffusionKDiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""					)
            __lowerCamelCase						:       List[str]				   =				sd_pipe.to(A__					)
            sd_pipe.set_progress_bar_config(disable=A__					)

            sd_pipe.set_scheduler("""sample_dpmpp_2m"""					)

            __lowerCamelCase						:       int				   =				"""A painting of a squirrel eating a burger"""
            __lowerCamelCase						:       Tuple				   =				torch.manual_seed(0					)
            __lowerCamelCase						:       Union[str, Any]				   =				sd_pipe(
                [prompt]	,    generator=A__	,    guidance_scale=7.5	,    num_inference_steps=15	,    output_type="""np"""	,    use_karras_sigmas=A__	,    )

            __lowerCamelCase						:       int				   =				output.images

            __lowerCamelCase						:       Any				   =				image[0, -3:, -3:, -1]

            assert image.shape == (1, 512, 512, 3)
            __lowerCamelCase						:       int				   =				np.array(
                [0.1138_1689, 0.1211_2921, 0.138_9457, 0.1254_9606, 0.124_4964, 0.1083_1517, 0.1156_2866, 0.1086_7816, 0.1049_9048]					)

            assert np.abs(image_slice.flatten() - expected_slice					).max() < 1e-2



