





"""simple docstring"""





import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPanoramaPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu, skip_mps

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()




@skip_mps
class 							snake_case							(			__lowercase      ,  __lowercase      ,  unittest.TestCase ):

      '''simple docstring'''



      _A         =       StableDiffusionPanoramaPipeline
      _A         =       TEXT_TO_IMAGE_PARAMS
      _A         =       TEXT_TO_IMAGE_BATCH_PARAMS
      _A         =       TEXT_TO_IMAGE_IMAGE_PARAMS
      _A         =       TEXT_TO_IMAGE_IMAGE_PARAMS

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


        '''simple docstring'''




        torch.manual_seed(0 )
        __UpperCAmelCase    :						int       =						UNetaDConditionModel(
            block_out_channels=(32, 64)						,				layers_per_block=1						,				sample_size=32						,				in_channels=4						,				out_channels=4						,				down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')						,				up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')						,				cross_attention_dim=32						,				)
        __UpperCAmelCase    :						Tuple       =						DDIMScheduler()
        torch.manual_seed(0 )
        __UpperCAmelCase    :						Any       =						AutoencoderKL(
            block_out_channels=[32, 64]						,				in_channels=3						,				out_channels=3						,				down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']						,				up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']						,				latent_channels=4						,				)
        torch.manual_seed(0 )
        __UpperCAmelCase    :						str       =						CLIPTextConfig(
            bos_token_id=0						,				eos_token_id=2						,				hidden_size=32						,				intermediate_size=37						,				layer_norm_eps=1e-05						,				num_attention_heads=4						,				num_hidden_layers=5						,				pad_token_id=1						,				vocab_size=1_000						,				)
        __UpperCAmelCase    :						Union[str, Any]       =						CLIPTextModel(_A )
        __UpperCAmelCase    :						Dict       =						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

        __UpperCAmelCase    :						Optional[Any]       =						{
            '''unet''': unet,
            '''scheduler''': scheduler,
            '''vae''': vae,
            '''text_encoder''': text_encoder,
            '''tokenizer''': tokenizer,
            '''safety_checker''': None,
            '''feature_extractor''': None,
        }
        return components

      def       A_					(							self       :      Optional[Any]						,				__lowercase       :      str						,				__lowercase       :      int=0 ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[Any]       =						torch.manual_seed(_A )
        __UpperCAmelCase    :						Dict       =						{
            '''prompt''': '''a photo of the dolomites''',
            '''generator''': generator,
            # Setting height and width to None to prevent OOMs on CPU.
            '''height''': None,
            '''width''': None,
            '''num_inference_steps''': 1,
            '''guidance_scale''': 6.0,
            '''output_type''': '''numpy''',
        }
        return inputs

      def       A_					(							self       :      Dict ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase    :						Optional[Any]       =						self.get_dummy_components()
        __UpperCAmelCase    :						Union[str, Any]       =						StableDiffusionPanoramaPipeline(**_A )
        __UpperCAmelCase    :						List[Any]       =						sd_pipe.to(_A )
        sd_pipe.set_progress_bar_config(disable=_A )

        __UpperCAmelCase    :						Union[str, Any]       =						self.get_dummy_inputs(_A )
        __UpperCAmelCase    :						List[str]       =						sd_pipe(**_A ).images
        __UpperCAmelCase    :						int       =						image[0, -3:, -3:, -1]
        assert image.shape == (1, 64, 64, 3)

        __UpperCAmelCase    :						Union[str, Any]       =						np.array([0.6_1_8_6, 0.5_3_7_4, 0.4_9_1_5, 0.4_1_3_5, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_7, 0.4_7_5_7] )

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

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        super().test_inference_batch_consistent(batch_sizes=[1, 2] )

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        super().test_inference_batch_single_identical(batch_size=2						,				expected_max_diff=3.25e-3 )

      def       A_					(							self       :      Optional[Any] ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[Any]       =						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase    :						Tuple       =						self.get_dummy_components()
        __UpperCAmelCase    :						Union[str, Any]       =						StableDiffusionPanoramaPipeline(**_A )
        __UpperCAmelCase    :						Any       =						sd_pipe.to(_A )
        sd_pipe.set_progress_bar_config(disable=_A )

        __UpperCAmelCase    :						List[str]       =						self.get_dummy_inputs(_A )
        __UpperCAmelCase    :						Dict       =						'''french fries'''
        __UpperCAmelCase    :						Dict       =						sd_pipe(**_A						,				negative_prompt=_A )
        __UpperCAmelCase    :						Union[str, Any]       =						output.images
        __UpperCAmelCase    :						Tuple       =						image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        __UpperCAmelCase    :						int       =						np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7] )

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

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase    :						int       =						self.get_dummy_components()
        __UpperCAmelCase    :						int       =						StableDiffusionPanoramaPipeline(**_A )
        __UpperCAmelCase    :						int       =						sd_pipe.to(_A )
        sd_pipe.set_progress_bar_config(disable=_A )

        __UpperCAmelCase    :						List[Any]       =						self.get_dummy_inputs(_A )
        __UpperCAmelCase    :						Tuple       =						sd_pipe(**_A						,				view_batch_size=2 )
        __UpperCAmelCase    :						int       =						output.images
        __UpperCAmelCase    :						Tuple       =						image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        __UpperCAmelCase    :						Dict       =						np.array([0.6_1_8_7, 0.5_3_7_5, 0.4_9_1_5, 0.4_1_3_6, 0.4_1_1_4, 0.4_5_6_3, 0.5_1_2_8, 0.4_9_7_6, 0.4_7_5_7] )

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

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase    :						Dict       =						self.get_dummy_components()
        __UpperCAmelCase    :						Union[str, Any]       =						EulerAncestralDiscreteScheduler(
            beta_start=0.0_0_0_8_5						,				beta_end=0.0_1_2						,				beta_schedule='''scaled_linear''' )
        __UpperCAmelCase    :						Any       =						StableDiffusionPanoramaPipeline(**_A )
        __UpperCAmelCase    :						Union[str, Any]       =						sd_pipe.to(_A )
        sd_pipe.set_progress_bar_config(disable=_A )

        __UpperCAmelCase    :						int       =						self.get_dummy_inputs(_A )
        __UpperCAmelCase    :						List[Any]       =						sd_pipe(**_A ).images
        __UpperCAmelCase    :						Optional[Any]       =						image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        __UpperCAmelCase    :						List[Any]       =						np.array([0.4_0_2_4, 0.6_5_1_0, 0.4_9_0_1, 0.5_3_7_8, 0.5_8_1_3, 0.5_6_2_2, 0.4_7_9_5, 0.4_4_6_7, 0.4_9_5_2] )

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







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


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        __UpperCAmelCase    :						Dict       =						self.get_dummy_components()
        __UpperCAmelCase    :						Tuple       =						PNDMScheduler(
            beta_start=0.0_0_0_8_5						,				beta_end=0.0_1_2						,				beta_schedule='''scaled_linear'''						,				skip_prk_steps=_A )
        __UpperCAmelCase    :						List[str]       =						StableDiffusionPanoramaPipeline(**_A )
        __UpperCAmelCase    :						Optional[int]       =						sd_pipe.to(_A )
        sd_pipe.set_progress_bar_config(disable=_A )

        __UpperCAmelCase    :						Optional[int]       =						self.get_dummy_inputs(_A )
        __UpperCAmelCase    :						List[str]       =						sd_pipe(**_A ).images
        __UpperCAmelCase    :						Union[str, Any]       =						image[0, -3:, -3:, -1]

        assert image.shape == (1, 64, 64, 3)

        __UpperCAmelCase    :						str       =						np.array([0.6_3_9_1, 0.6_2_9_1, 0.4_8_6_1, 0.5_1_3_4, 0.5_5_5_2, 0.4_5_7_8, 0.5_0_3_2, 0.5_0_2_3, 0.4_5_3_9] )

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




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

      '''simple docstring'''



      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




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

      def       A_					(							self       :      Tuple						,				__lowercase       :      Tuple=0 ):


        '''simple docstring'''




        __UpperCAmelCase    :						Any       =						torch.manual_seed(_A )
        __UpperCAmelCase    :						List[str]       =						{
            '''prompt''': '''a photo of the dolomites''',
            '''generator''': generator,
            '''num_inference_steps''': 3,
            '''guidance_scale''': 7.5,
            '''output_type''': '''numpy''',
        }
        return inputs

      def       A_					(							self       :      str ):


        '''simple docstring'''




        __UpperCAmelCase    :						List[str]       =						'''stabilityai/stable-diffusion-2-base'''
        __UpperCAmelCase    :						Dict       =						DDIMScheduler.from_pretrained(_A						,				subfolder='''scheduler''' )
        __UpperCAmelCase    :						Optional[int]       =						StableDiffusionPanoramaPipeline.from_pretrained(_A						,				scheduler=_A						,				safety_checker=_A )
        pipe.to(_A )
        pipe.set_progress_bar_config(disable=_A )
        pipe.enable_attention_slicing()

        __UpperCAmelCase    :						List[Any]       =						self.get_inputs()
        __UpperCAmelCase    :						Tuple       =						pipe(**_A ).images
        __UpperCAmelCase    :						Optional[int]       =						image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 2_048, 3)

        __UpperCAmelCase    :						str       =						np.array(
            [
                0.3_6_9_6_8_3_9_2,
                0.2_7_0_2_5_3_7_2,
                0.3_2_4_4_6_7_6_6,
                0.2_8_3_7_9_3_8_7,
                0.3_6_3_6_3_2_7_4,
                0.3_0_7_3_3_3_4_7,
                0.2_7_1_0_0_0_2_7,
                0.2_7_0_5_4_1_2_5,
                0.2_5_5_3_6_0_9_6,
            ] )

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

      def       A_					(							self       :      Optional[int] ):


        '''simple docstring'''




        __UpperCAmelCase    :						Tuple       =						StableDiffusionPanoramaPipeline.from_pretrained(
            '''stabilityai/stable-diffusion-2-base'''						,				safety_checker=_A )
        __UpperCAmelCase    :						List[str]       =						LMSDiscreteScheduler.from_config(pipe.scheduler.config )
        pipe.to(_A )
        pipe.set_progress_bar_config(disable=_A )
        pipe.enable_attention_slicing()

        __UpperCAmelCase    :						Any       =						self.get_inputs()
        __UpperCAmelCase    :						List[Any]       =						pipe(**_A ).images
        __UpperCAmelCase    :						str       =						image[0, -3:, -3:, -1].flatten()

        assert image.shape == (1, 512, 2_048, 3)

        __UpperCAmelCase    :						List[Any]       =						np.array(
            [
                [
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                    0.0,
                ]
            ] )

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

      def       A_					(							self       :      int ):


        '''simple docstring'''




        __UpperCAmelCase    :						Dict       =						0

        def callback_fn(__lowercase       :      int						,				__lowercase       :      int						,				__lowercase       :      torch.FloatTensor ) -> None:
          __UpperCAmelCase    :						Union[str, Any]       =						True
          nonlocal number_of_steps
          number_of_steps += 1
          if step == 1:
            __UpperCAmelCase    :						Optional[int]       =						latents.detach().cpu().numpy()
            assert latents.shape == (1, 4, 64, 256)
            __UpperCAmelCase    :						str       =						latents[0, -3:, -3:, -1]

            __UpperCAmelCase    :						List[Any]       =						np.array(
                [
                    0.1_8_6_8_1_8_6_9,
                    0.3_3_9_0_7_8_1_6,
                    0.5_3_6_1_2_7_6,
                    0.1_4_4_3_2_8_6_5,
                    -0.0_2_8_5_6_6_1_1,
                    -0.7_3_9_4_1_1_2_3,
                    0.2_3_3_9_7_9_8_7,
                    0.4_7_3_2_2_6_8_2,
                    -0.3_7_8_2_3_1_6_4,
                ] )
            assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2
          elif step == 2:
            __UpperCAmelCase    :						Any       =						latents.detach().cpu().numpy()
            assert latents.shape == (1, 4, 64, 256)
            __UpperCAmelCase    :						Dict       =						latents[0, -3:, -3:, -1]

            __UpperCAmelCase    :						Dict       =						np.array(
                [
                    0.1_8_5_3_9_6_4_5,
                    0.3_3_9_8_7_2_4_8,
                    0.5_3_7_8_5_5_9,
                    0.1_4_4_3_7_1_4_2,
                    -0.0_2_4_5_5_2_6_1,
                    -0.7_3_3_8_3_1_7,
                    0.2_3_9_9_0_7_5_5,
                    0.4_7_3_5_6_2_7_2,
                    -0.3_7_8_6_5_0_5,
                ] )

            assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2

        __UpperCAmelCase    :						Optional[Any]       =						False

        __UpperCAmelCase    :						Any       =						'''stabilityai/stable-diffusion-2-base'''
        __UpperCAmelCase    :						Tuple       =						DDIMScheduler.from_pretrained(_A						,				subfolder='''scheduler''' )
        __UpperCAmelCase    :						Union[str, Any]       =						StableDiffusionPanoramaPipeline.from_pretrained(_A						,				scheduler=_A						,				safety_checker=_A )
        __UpperCAmelCase    :						Any       =						pipe.to(_A )
        pipe.set_progress_bar_config(disable=_A )
        pipe.enable_attention_slicing()

        __UpperCAmelCase    :						Any       =						self.get_inputs()
        pipe(**_A						,				callback=_A						,				callback_steps=1 )
        assert callback_fn.has_been_called
        assert number_of_steps == 3







      def       A_					(							self       :      int ):


        '''simple docstring'''




        torch.cuda.empty_cache()
        torch.cuda.reset_max_memory_allocated()
        torch.cuda.reset_peak_memory_stats()

        __UpperCAmelCase    :						int       =						'''stabilityai/stable-diffusion-2-base'''
        __UpperCAmelCase    :						int       =						DDIMScheduler.from_pretrained(_A						,				subfolder='''scheduler''' )
        __UpperCAmelCase    :						Optional[Any]       =						StableDiffusionPanoramaPipeline.from_pretrained(_A						,				scheduler=_A						,				safety_checker=_A )
        __UpperCAmelCase    :						Tuple       =						pipe.to(_A )
        pipe.set_progress_bar_config(disable=_A )
        pipe.enable_attention_slicing(1 )
        pipe.enable_sequential_cpu_offload()

        __UpperCAmelCase    :						Any       =						self.get_inputs()
        __UpperCAmelCase    :						int       =						pipe(**_A )

        __UpperCAmelCase    :						Optional[Any]       =						torch.cuda.max_memory_allocated()
        # make sure that less than 5.2 GB is allocated
        assert mem_bytes < 5.5 * 10**9






"""simple docstring"""





import gc
import unittest

from diffusers import FlaxDPMSolverMultistepScheduler, FlaxStableDiffusionPipeline
from diffusers.utils import is_flax_available, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
  import jax
  import jax.numpy as jnp
  from flax.jax_utils import replicate
  from flax.training.common_utils import shard




@slow
@require_flax
class 							snake_case							(			unittest.TestCase ):

      '''simple docstring'''



      def       A_					(							self       :      str ):


        '''simple docstring'''




        super().tearDown()
        gc.collect()

      def       A_					(							self       :      List[Any] ):


        '''simple docstring'''




        __UpperCAmelCase       ,     __UpperCAmelCase    :						List[Any]       =						FlaxStableDiffusionPipeline.from_pretrained(
            '''stabilityai/stable-diffusion-2'''						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)

        __UpperCAmelCase    :						Dict       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						Any       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						List[Any]       =						replicate(__lowercase )
        __UpperCAmelCase    :						Optional[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						str       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						Dict       =						jax.random.split(__lowercase						,				jax.device_count() )

        __UpperCAmelCase    :						Tuple       =						sd_pipe(__lowercase						,				__lowercase						,				__lowercase						,				num_inference_steps=25						,				jit=__lowercase )[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        __UpperCAmelCase    :						Any       =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
        __UpperCAmelCase    :						Union[str, Any]       =						images[0, 253:256, 253:256, -1]

        __UpperCAmelCase    :						str       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						List[Any]       =						jnp.array([0.4_2_3_8, 0.4_4_1_4, 0.4_3_9_5, 0.4_4_5_3, 0.4_6_2_9, 0.4_5_9_0, 0.4_5_3_1, 0.4_5_5_0_8, 0.4_5_1_2] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2







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


        '''simple docstring'''




        __UpperCAmelCase    :						Optional[int]       =						'''stabilityai/stable-diffusion-2'''
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Any       =						FlaxDPMSolverMultistepScheduler.from_pretrained(__lowercase						,				subfolder='''scheduler''' )
        __UpperCAmelCase       ,     __UpperCAmelCase    :						Optional[int]       =						FlaxStableDiffusionPipeline.from_pretrained(
            __lowercase						,				scheduler=__lowercase						,				revision='''bf16'''						,				dtype=jnp.bfloataa						,				)
        __UpperCAmelCase    :						Dict       =						scheduler_params

        __UpperCAmelCase    :						int       =						'''A painting of a squirrel eating a burger'''
        __UpperCAmelCase    :						List[Any]       =						jax.device_count()
        __UpperCAmelCase    :						Optional[int]       =						num_samples * [prompt]
        __UpperCAmelCase    :						Tuple       =						sd_pipe.prepare_inputs(__lowercase )

        __UpperCAmelCase    :						Optional[int]       =						replicate(__lowercase )
        __UpperCAmelCase    :						List[Any]       =						shard(__lowercase )

        __UpperCAmelCase    :						Union[str, Any]       =						jax.random.PRNGKey(0 )
        __UpperCAmelCase    :						List[Any]       =						jax.random.split(__lowercase						,				jax.device_count() )

        __UpperCAmelCase    :						List[Any]       =						sd_pipe(__lowercase						,				__lowercase						,				__lowercase						,				num_inference_steps=25						,				jit=__lowercase )[0]
        assert images.shape == (jax.device_count(), 1, 768, 768, 3)

        __UpperCAmelCase    :						Tuple       =						images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:] )
        __UpperCAmelCase    :						Any       =						images[0, 253:256, 253:256, -1]

        __UpperCAmelCase    :						List[str]       =						jnp.asarray(jax.device_get(image_slice.flatten() ) )
        __UpperCAmelCase    :						Any       =						jnp.array([0.4_3_3_6, 0.4_2_9_6_9, 0.4_4_5_3, 0.4_1_9_9, 0.4_2_9_7, 0.4_5_3_1, 0.4_4_3_4, 0.4_4_3_4, 0.4_2_9_7] )
        print(f'''output_slice: {output_slice}''' )
        assert jnp.abs(output_slice - expected_slice ).max() < 1e-2