

import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()







class     a__    (				UpperCAmelCase		,     UpperCAmelCase		,     unittest.TestCase    ):







  """simple docstring"""




  UpperCAmelCase__ :	List[str]         =StableDiffusionXLPipeline
  UpperCAmelCase__ :	List[Any]         =TEXT_TO_IMAGE_PARAMS
  UpperCAmelCase__ :	Optional[Any]         =TEXT_TO_IMAGE_BATCH_PARAMS
  UpperCAmelCase__ :	Union[str, Any]         =TEXT_TO_IMAGE_IMAGE_PARAMS
  UpperCAmelCase__ :	List[str]         =TEXT_TO_IMAGE_IMAGE_PARAMS


  def 	_lowercase (     self      :	Union[str, Any]						)   ->Optional[Any]:
        """simple docstring"""







        torch.manual_seed(0						)
        SCREAMING_SNAKE_CASE	: Tuple					       =				UNetaDConditionModel(
            block_out_channels=(3_2, 6_4)			,			layers_per_block=2			,			sample_size=3_2			,			in_channels=4			,			out_channels=4			,			down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")			,			up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")			,			attention_head_dim=(2, 4)			,			use_linear_projection=UpperCAmelCase__			,			addition_embed_type="""text_time"""			,			addition_time_embed_dim=8			,			transformer_layers_per_block=(1, 2)			,			projection_class_embeddings_input_dim=8_0			,			cross_attention_dim=6_4			,			)
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				EulerDiscreteScheduler(
            beta_start=0.0_00_85			,			beta_end=0.0_12			,			steps_offset=1			,			beta_schedule="""scaled_linear"""			,			timestep_spacing="""leading"""			,			)
        torch.manual_seed(0						)
        SCREAMING_SNAKE_CASE	: List[str]					       =				AutoencoderKL(
            block_out_channels=[3_2, 6_4]			,			in_channels=3			,			out_channels=3			,			down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]			,			up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]			,			latent_channels=4			,			sample_size=1_2_8			,			)
        torch.manual_seed(0						)
        SCREAMING_SNAKE_CASE	: str					       =				CLIPTextConfig(
            bos_token_id=0			,			eos_token_id=2			,			hidden_size=3_2			,			intermediate_size=3_7			,			layer_norm_eps=1e-05			,			num_attention_heads=4			,			num_hidden_layers=5			,			pad_token_id=1			,			vocab_size=1_0_0_0			,			hidden_act="""gelu"""			,			projection_dim=3_2			,			)
        SCREAMING_SNAKE_CASE	: List[Any]					       =				CLIPTextModel(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Any					       =				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			,			local_files_only=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: str					       =				CLIPTextModelWithProjection(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			,			local_files_only=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: Any					       =				{
            """unet""": unet,
            """scheduler""": scheduler,
            """vae""": vae,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
            """text_encoder_2""": text_encoder_a,
            """tokenizer_2""": tokenizer_a,
            # "safety_checker": None,
            # "feature_extractor": None,
        }
        return components


  def 	_lowercase (     self      :	Optional[Any]			,			UpperCAmelCase__      :	Optional[Any]			,			UpperCAmelCase__      :	Optional[int]=0						)   ->List[Any]:
        """simple docstring"""







        if str(UpperCAmelCase__						).startswith("""mps"""						):
              SCREAMING_SNAKE_CASE	: Any					       =				torch.manual_seed(UpperCAmelCase__						)
        else:
              SCREAMING_SNAKE_CASE	: int					       =				torch.Generator(device=UpperCAmelCase__						).manual_seed(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				{
            """prompt""": """A painting of a squirrel eating a burger""",
            """generator""": generator,
            """num_inference_steps""": 2,
            """guidance_scale""": 5.0,
            """output_type""": """numpy""",
        }
        return inputs


  def 	_lowercase (     self      :	Any						)   ->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
        SCREAMING_SNAKE_CASE	: List[str]					       =				self.get_dummy_components()
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				StableDiffusionXLPipeline(**UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Dict					       =				sd_pipe.to(UpperCAmelCase__						)
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: Dict					       =				self.get_dummy_inputs(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: List[Any]					       =				sd_pipe(**UpperCAmelCase__						).images
        SCREAMING_SNAKE_CASE	: str					       =				image[0, -3:, -3:, -1]

        assert image.shape == (1, 6_4, 6_4, 3)
        SCREAMING_SNAKE_CASE	: Dict					       =				np.array([0.58_73, 0.61_28, 0.47_97, 0.51_22, 0.56_74, 0.46_39, 0.52_27, 0.51_49, 0.47_47]						)

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


  def 	_lowercase (     self      :	Optional[int]						)   ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[int]					       =				self.get_dummy_components()
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				StableDiffusionXLPipeline(**UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				sd_pipe.to(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Any					       =				sd_pipe.to(UpperCAmelCase__						)
        sd_pipe.set_progress_bar_config(disable=UpperCAmelCase__						)

        # forward without prompt embeds
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				self.get_dummy_inputs(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Dict					       =				3 * ["""this is a negative prompt"""]
        SCREAMING_SNAKE_CASE	: Dict					       =				negative_prompt
        SCREAMING_SNAKE_CASE	: Tuple					       =				3 * [inputs["""prompt"""]]

        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				sd_pipe(**UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				output.images[0, -3:, -3:, -1]

        # forward with prompt embeds
        SCREAMING_SNAKE_CASE	: Dict					       =				self.get_dummy_inputs(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Dict					       =				3 * ["""this is a negative prompt"""]
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				3 * [inputs.pop("""prompt"""						)]

        (
            (
            SCREAMING_SNAKE_CASE
        )       ,		(
            SCREAMING_SNAKE_CASE
        )       ,		(
            SCREAMING_SNAKE_CASE
        )       ,		(
            SCREAMING_SNAKE_CASE
        )       ,		
        )	: int					       =				sd_pipe.encode_prompt(UpperCAmelCase__			,			negative_prompt=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				sd_pipe(
            **UpperCAmelCase__			,			prompt_embeds=UpperCAmelCase__			,			negative_prompt_embeds=UpperCAmelCase__			,			pooled_prompt_embeds=UpperCAmelCase__			,			negative_pooled_prompt_embeds=UpperCAmelCase__			,			)
        SCREAMING_SNAKE_CASE	: List[str]					       =				output.images[0, -3:, -3:, -1]

        # make sure that it's equal
        assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1e-4


  def 	_lowercase (     self      :	Optional[Any]						)   ->int:
        """simple docstring"""







        super().test_attention_slicing_forward_pass(expected_max_diff=3e-3						)




  def 	_lowercase (     self      :	List[str]						)   ->Optional[int]:
        """simple docstring"""







        super().test_inference_batch_single_identical(expected_max_diff=3e-3						)







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







  """simple docstring"""




  def 	_lowercase (     self      :	Dict						)   ->Optional[Any]:
        """simple docstring"""







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


  def 	_lowercase (     self      :	List[str]			,			UpperCAmelCase__      :	Optional[Any]			,			UpperCAmelCase__      :	Optional[Any]="cpu"			,			UpperCAmelCase__      :	List[Any]=torch.floataa			,			UpperCAmelCase__      :	str=0						)   ->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[Any]					       =				torch.Generator(device=UpperCAmelCase__						).manual_seed(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Dict					       =				np.random.RandomState(UpperCAmelCase__						).standard_normal((1, 4, 6_4, 6_4)						)
        SCREAMING_SNAKE_CASE	: List[str]					       =				torch.from_numpy(UpperCAmelCase__						).to(device=UpperCAmelCase__			,			dtype=UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				{
            """prompt""": """a photograph of an astronaut riding a horse""",
            """latents""": latents,
            """generator""": generator,
            """num_inference_steps""": 3,
            """guidance_scale""": 7.5,
            """output_type""": """numpy""",
        }
        return inputs




  def 	_lowercase (     self      :	List[Any]						)   ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: str					       =				DiffusionPipeline.from_pretrained("""stabilityai/stable-diffusion-2-base"""						)
        pipe.to(UpperCAmelCase__						)
        pipe.set_progress_bar_config(disable=UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: Any					       =				self.get_inputs(UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: Tuple					       =				pipe(**UpperCAmelCase__						).images
        SCREAMING_SNAKE_CASE	: Dict					       =				image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 5_1_2, 5_1_2, 3)
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				np.array([0.4_94_93, 0.4_78_96, 0.4_07_98, 0.5_42_14, 0.5_32_12, 0.4_82_02, 0.4_76_56, 0.4_63_29, 0.4_85_06]						)
        assert np.abs(image_slice - expected_slice						).max() < 7e-3







import inspect
import os
import unittest

import torch

import accelerate
from accelerate import debug_launcher
from accelerate.test_utils import (
    execute_subprocess_async,
    require_cpu,
    require_multi_gpu,
    require_single_gpu,
    test_sync,
)
from accelerate.utils import patch_environment







class     a__    (				unittest.TestCase    ):







  """simple docstring"""




  def 	_lowercase (     self      :	Optional[int]						)   ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				inspect.getfile(accelerate.test_utils						)
        SCREAMING_SNAKE_CASE	: int					       =				os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ["""scripts""", """test_sync.py"""]						)


  @require_cpu
  def 	_lowercase (     self      :	Optional[int]						)   ->str:
        """simple docstring"""







        debug_launcher(test_sync.main			,			num_processes=1						)


  @require_cpu
  def 	_lowercase (     self      :	Optional[int]						)   ->int:
        """simple docstring"""







        debug_launcher(test_sync.main						)


  @require_single_gpu
  def 	_lowercase (     self      :	str						)   ->Any:
        """simple docstring"""







        test_sync.main()




  @require_multi_gpu
  def 	_lowercase (     self      :	Union[str, Any]						)   ->List[Any]:
        """simple docstring"""







        print(f"Found {torch.cuda.device_count()} devices."						)
        SCREAMING_SNAKE_CASE	: List[str]					       =				["""torchrun""", f"--nproc_per_node={torch.cuda.device_count()}", self.test_file_path]
        with patch_environment(omp_num_threads=1						):
              execute_subprocess_async(UpperCAmelCase__			,			env=os.environ.copy()						)




