


"""simple docstring"""







import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    PNDMScheduler,
    StableDiffusionLDMaDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import nightly, 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


enable_full_determinism()


class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
       """simple docstring"""


       a  :    Any       =StableDiffusionLDMaDPipeline
       a  :    Union[str, Any]       =TEXT_TO_IMAGE_PARAMS
       a  :    Union[str, Any]       =TEXT_TO_IMAGE_BATCH_PARAMS
       a  :    Any       =TEXT_TO_IMAGE_IMAGE_PARAMS

       def 	lowercase__							(						self						):






         """simple docstring"""





         torch.manual_seed(0						)
         lowerCAmelCase					:					str								=							UNetaDConditionModel(
             block_out_channels=(32, 64)		,					layers_per_block=2		,					sample_size=32		,					in_channels=4		,					out_channels=4		,					down_block_types=("DownBlock2D", "CrossAttnDownBlock2D")		,					up_block_types=("CrossAttnUpBlock2D", "UpBlock2D")		,					cross_attention_dim=32		,					)
         lowerCAmelCase					:					Dict								=							DDIMScheduler(
             beta_start=0.00085		,					beta_end=0.012		,					beta_schedule="scaled_linear"		,					clip_sample=snake_case__		,					set_alpha_to_one=snake_case__		,					)
         torch.manual_seed(0						)
         lowerCAmelCase					:					Dict								=							AutoencoderKL(
             block_out_channels=[32, 64]		,					in_channels=6		,					out_channels=6		,					down_block_types=["DownEncoderBlock2D", "DownEncoderBlock2D"]		,					up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D"]		,					latent_channels=4		,					)
         torch.manual_seed(0						)
         lowerCAmelCase					:					Dict								=							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		,					)
         lowerCAmelCase					:					int								=							CLIPTextModel(snake_case__						)
         lowerCAmelCase					:					Optional[int]								=							CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"						)

         lowerCAmelCase					:					Any								=							{
             "unet": unet,
             "scheduler": scheduler,
             "vae": vae,
             "text_encoder": text_encoder,
             "tokenizer": tokenizer,
             "safety_checker": None,
             "feature_extractor": None,
         }
         return components

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__=0						):






         """simple docstring"""





         if str(snake_case__						).startswith("mps"						):
           lowerCAmelCase					:					int								=							torch.manual_seed(snake_case__						)
         else:
           lowerCAmelCase					:					Dict								=							torch.Generator(device=snake_case__						).manual_seed(snake_case__						)
         lowerCAmelCase					:					Dict								=							{
             "prompt": "A painting of a squirrel eating a burger",
             "generator": generator,
             "num_inference_steps": 2,
             "guidance_scale": 6.0,
             "output_type": "numpy",
         }
         return inputs

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Dict								=							"cpu"  # ensure determinism for the device-dependent torch.Generator

         lowerCAmelCase					:					Union[str, Any]								=							self.get_dummy_components()
         lowerCAmelCase					:					Tuple								=							StableDiffusionLDMaDPipeline(**snake_case__						)
         lowerCAmelCase					:					List[str]								=							ldmad_pipe.to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Optional[int]								=							self.get_dummy_inputs(snake_case__						)
         lowerCAmelCase					:					List[Any]								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					Union[str, Any]								=							output.rgb, output.depth

         lowerCAmelCase					:					List[str]								=							rgb[0, -3:, -3:, -1]
         lowerCAmelCase					:					Optional[int]								=							depth[0, -3:, -1]

         assert rgb.shape == (1, 64, 64, 3)
         assert depth.shape == (1, 64, 64)

         lowerCAmelCase					:					Any								=							np.array(
             [0.37338176, 0.70247, 0.74203193, 0.51643604, 0.58256793, 0.60932136, 0.4181095, 0.48355877, 0.46535262]						)
         lowerCAmelCase					:					Any								=							np.array([103.46727, 85.812004, 87.849236]						)

         assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb						).max() < 1e-2
         assert np.abs(image_slice_depth.flatten() - expected_slice_depth						).max() < 1e-2

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.get_dummy_components()
         lowerCAmelCase					:					int								=							StableDiffusionLDMaDPipeline(**snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							ldmad_pipe.to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Optional[Any]								=							self.get_dummy_inputs(snake_case__						)
         lowerCAmelCase					:					int								=							3 * [inputs["prompt"]]

         # forward
         lowerCAmelCase					:					str								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					Union[str, Any]								=							output.rgb, output.depth
         lowerCAmelCase					:					Any								=							rgb_slice_a[0, -3:, -3:, -1]
         lowerCAmelCase					:					str								=							depth_slice_a[0, -3:, -1]

         lowerCAmelCase					:					Optional[Any]								=							self.get_dummy_inputs(snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							3 * [inputs.pop("prompt"						)]

         lowerCAmelCase					:					List[Any]								=							ldmad_pipe.tokenizer(
             snake_case__		,					padding="max_length"		,					max_length=ldmad_pipe.tokenizer.model_max_length		,					truncation=snake_case__		,					return_tensors="pt"		,					)
         lowerCAmelCase					:					Tuple								=							text_inputs["input_ids"].to(snake_case__						)

         lowerCAmelCase					:					List[Any]								=							ldmad_pipe.text_encoder(snake_case__						)[0]

         lowerCAmelCase					:					Optional[int]								=							prompt_embeds

         # forward
         lowerCAmelCase					:					List[Any]								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					Optional[int]								=							output.rgb, output.depth
         lowerCAmelCase					:					List[str]								=							rgb_slice_a[0, -3:, -3:, -1]
         lowerCAmelCase					:					Optional[int]								=							depth_slice_a[0, -3:, -1]

         assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()						).max() < 1e-4
         assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()						).max() < 1e-4







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Union[str, Any]								=							"cpu"  # ensure determinism for the device-dependent torch.Generator
         lowerCAmelCase					:					Tuple								=							self.get_dummy_components()
         lowerCAmelCase					:					str								=							PNDMScheduler(skip_prk_steps=snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							StableDiffusionLDMaDPipeline(**snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							ldmad_pipe.to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Any								=							self.get_dummy_inputs(snake_case__						)
         lowerCAmelCase					:					Optional[Any]								=							"french fries"
         lowerCAmelCase					:					Optional[int]								=							ldmad_pipe(**snake_case__		,					negative_prompt=snake_case__						)

         lowerCAmelCase						,  lowerCAmelCase					:					Any								=							output.rgb, output.depth
         lowerCAmelCase					:					Optional[int]								=							rgb[0, -3:, -3:, -1]
         lowerCAmelCase					:					Optional[Any]								=							depth[0, -3:, -1]

         assert rgb.shape == (1, 64, 64, 3)
         assert depth.shape == (1, 64, 64)

         lowerCAmelCase					:					Dict								=							np.array(
             [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217]						)
         lowerCAmelCase					:					List[str]								=							np.array([107.84738, 84.62802, 89.962135]						)
         assert np.abs(rgb_slice.flatten() - expected_slice_rgb						).max() < 1e-2
         assert np.abs(depth_slice.flatten() - expected_slice_depth						).max() < 1e-2


@slow
@require_torch_gpu
class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
       """simple docstring"""


       def 	lowercase__							(						self						):






         """simple docstring"""





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

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__="cpu"		,					snake_case__=torch.floataa		,					snake_case__=0						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							torch.Generator(device=snake_case__						).manual_seed(snake_case__						)
         lowerCAmelCase					:					List[Any]								=							np.random.RandomState(snake_case__						).standard_normal((1, 4, 64, 64)						)
         lowerCAmelCase					:					str								=							torch.from_numpy(snake_case__						).to(device=snake_case__		,					dtype=snake_case__						)
         lowerCAmelCase					:					List[str]								=							{
             "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						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d"						)
         lowerCAmelCase					:					List[Any]								=							ldmad_pipe.to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Union[str, Any]								=							self.get_inputs(snake_case__						)
         lowerCAmelCase					:					int								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					Tuple								=							output.rgb, output.depth
         lowerCAmelCase					:					Dict								=							rgb[0, -3:, -3:, -1].flatten()
         lowerCAmelCase					:					str								=							rgb[0, -3:, -1].flatten()

         assert rgb.shape == (1, 512, 512, 3)
         assert depth.shape == (1, 512, 512)

         lowerCAmelCase					:					Any								=							np.array(
             [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706]						)
         lowerCAmelCase					:					int								=							np.array(
             [0.9263781, 0.6678672, 0.5486515, 0.92202145, 0.67831135, 0.56253487, 0.9241694, 0.7551478, 0.6459706]						)
         assert np.abs(rgb_slice - expected_slice_rgb						).max() < 3e-3
         assert np.abs(depth_slice - expected_slice_depth						).max() < 3e-3


@nightly
@require_torch_gpu
class 				SCREAMING_SNAKE_CASE__    (		unittest.TestCase    ):
       """simple docstring"""


       def 	lowercase__							(						self						):






         """simple docstring"""





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

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__="cpu"		,					snake_case__=torch.floataa		,					snake_case__=0						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							torch.Generator(device=snake_case__						).manual_seed(snake_case__						)
         lowerCAmelCase					:					Tuple								=							np.random.RandomState(snake_case__						).standard_normal((1, 4, 64, 64)						)
         lowerCAmelCase					:					List[Any]								=							torch.from_numpy(snake_case__						).to(device=snake_case__		,					dtype=snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							{
             "prompt": "a photograph of an astronaut riding a horse",
             "latents": latents,
             "generator": generator,
             "num_inference_steps": 50,
             "guidance_scale": 7.5,
             "output_type": "numpy",
         }
         return inputs

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d"						).to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Any								=							self.get_inputs(snake_case__						)
         lowerCAmelCase					:					Tuple								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					Optional[int]								=							output.rgb, output.depth

         lowerCAmelCase					:					Union[str, Any]								=							0.495586
         lowerCAmelCase					:					Tuple								=							0.33795515
         lowerCAmelCase					:					int								=							112.48518
         lowerCAmelCase					:					Union[str, Any]								=							98.489746
         assert np.abs(expected_rgb_mean - rgb.mean()						) < 1e-3
         assert np.abs(expected_rgb_std - rgb.std()						) < 1e-3
         assert np.abs(expected_depth_mean - depth.mean()						) < 1e-3
         assert np.abs(expected_depth_std - depth.std()						) < 1e-3







       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Tuple								=							StableDiffusionLDMaDPipeline.from_pretrained("Intel/ldm3d-4c"						).to(snake_case__						)
         ldmad_pipe.set_progress_bar_config(disable=snake_case__						)

         lowerCAmelCase					:					Any								=							self.get_inputs(snake_case__						)
         lowerCAmelCase					:					List[str]								=							ldmad_pipe(**snake_case__						)
         lowerCAmelCase						,  lowerCAmelCase					:					int								=							output.rgb, output.depth

         lowerCAmelCase					:					int								=							0.4194127
         lowerCAmelCase					:					Dict								=							0.35375586
         lowerCAmelCase					:					List[Any]								=							0.5638502
         lowerCAmelCase					:					Union[str, Any]								=							0.34686103

         assert rgb.shape == (1, 512, 512, 3)
         assert depth.shape == (1, 512, 512, 1)
         assert np.abs(expected_rgb_mean - rgb.mean()						) < 1e-3
         assert np.abs(expected_rgb_std - rgb.std()						) < 1e-3
         assert np.abs(expected_depth_mean - depth.mean()						) < 1e-3
         assert np.abs(expected_depth_std - depth.std()						) < 1e-3









"""simple docstring"""







from typing import Any


class 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       def __init__(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							data
         lowerCAmelCase					:					Tuple								=							None


class 				SCREAMING_SNAKE_CASE__    :
       """simple docstring"""


       def __init__(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[int]								=							None

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							self.head
         while temp is not None:
           print(temp.data		,					end=" "						)
           lowerCAmelCase					:					List[str]								=							temp.next
         print()

       def 	lowercase__							(						self		,					snake_case__						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							Node(snake_case__						)
         lowerCAmelCase					:					Union[str, Any]								=							self.head
         lowerCAmelCase					:					Optional[Any]								=							new_node







       def 	lowercase__							(						self		,					snake_case__		,					snake_case__						):






         """simple docstring"""





         if node_data_a == node_data_a:
           return
         else:
           lowerCAmelCase					:					str								=							self.head
           while node_a is not None and node_a.data != node_data_a:
             lowerCAmelCase					:					Union[str, Any]								=							node_a.next

           lowerCAmelCase					:					Any								=							self.head
           while node_a is not None and node_a.data != node_data_a:
             lowerCAmelCase					:					List[Any]								=							node_a.next

           if node_a is None or node_a is None:
             return

           lowerCAmelCase						,  lowerCAmelCase					:					str								=							node_a.data, node_a.data


if __name__ == "__main__":
     lowerCAmelCase__				    =       LinkedList()
     for i in range(5, 0, -1):
          ll.push(i)

     ll.print_list()

     ll.swap_nodes(1, 4)
     print('''After swapping''')
     ll.print_list()





