






"""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      UpperCamelCase_			(__A							,		__A							,		unittest.TestCase	):
	__magic_name__       =							StableDiffusionPanoramaPipeline
	__magic_name__       =							TEXT_TO_IMAGE_PARAMS
	__magic_name__       =							TEXT_TO_IMAGE_BATCH_PARAMS
	__magic_name__       =							TEXT_TO_IMAGE_IMAGE_PARAMS
	__magic_name__       =							TEXT_TO_IMAGE_IMAGE_PARAMS





	def 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     Tuple:
				torch.manual_seed(0		)
				UpperCAmelCase_	:					Any     	=   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_	:					List[Any]     	=   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_	:					Any     	=   CLIPTextModel(lowerCAmelCase_		)
				UpperCAmelCase_	:					int     	=   CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip"		)

				UpperCAmelCase_	:					Union[str, Any]     	=   {
				    "unet": unet,
				    "scheduler": scheduler,
				    "vae": vae,
				    "text_encoder": text_encoder,
				    "tokenizer": tokenizer,
				    "safety_checker": None,
				    "feature_extractor": None,
				}
				return components





	def 	_SCREAMING_SNAKE_CASE ( self   :					Any      ,      lowerCAmelCase_   :					Optional[Any]      ,      lowerCAmelCase_   :					Optional[int]=0		)		->     List[Any]:
				UpperCAmelCase_	:					Dict     	=   torch.manual_seed(lowerCAmelCase_		)
				UpperCAmelCase_	:					Tuple     	=   {
				    "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 	_SCREAMING_SNAKE_CASE ( self   :					int		)		->     int:
				UpperCAmelCase_	:					Optional[int]     	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_	:					str     	=   self.get_dummy_components()
				UpperCAmelCase_	:					int     	=   StableDiffusionPanoramaPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_	:					Optional[Any]     	=   sd_pipe.to(lowerCAmelCase_		)
				sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

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

				UpperCAmelCase_	:					List[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 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     Dict:
				super().test_inference_batch_consistent(batch_sizes=[1, 2]		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     Tuple:
				super().test_inference_batch_single_identical(batch_size=2      ,      expected_max_diff=3.25e-3		)





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     int:
				UpperCAmelCase_	:					str     	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_	:					List[Any]     	=   self.get_dummy_components()
				UpperCAmelCase_	:					Optional[int]     	=   StableDiffusionPanoramaPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_	:					str     	=   sd_pipe.to(lowerCAmelCase_		)
				sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

				UpperCAmelCase_	:					Any     	=   self.get_dummy_inputs(lowerCAmelCase_		)
				UpperCAmelCase_	:					Any     	=   "french fries"
				UpperCAmelCase_	:					Optional[Any]     	=   sd_pipe(**lowerCAmelCase_      ,      negative_prompt=lowerCAmelCase_		)
				UpperCAmelCase_	:					List[Any]     	=   output.images
				UpperCAmelCase_	:					str     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     Optional[int]:
				UpperCAmelCase_	:					List[str]     	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_	:					List[str]     	=   self.get_dummy_components()
				UpperCAmelCase_	:					Optional[int]     	=   StableDiffusionPanoramaPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_	:					List[str]     	=   sd_pipe.to(lowerCAmelCase_		)
				sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

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

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

				UpperCAmelCase_	:					str     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     List[Any]:
				UpperCAmelCase_	:					str     	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_	:					Tuple     	=   self.get_dummy_components()
				UpperCAmelCase_	:					Dict     	=   EulerAncestralDiscreteScheduler(
				    beta_start=0.0_0_0_8_5      ,      beta_end=0.0_1_2      ,      beta_schedule="scaled_linear"		)
				UpperCAmelCase_	:					str     	=   StableDiffusionPanoramaPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_	:					Tuple     	=   sd_pipe.to(lowerCAmelCase_		)
				sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

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

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

				UpperCAmelCase_	:					str     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					Dict		)		->     Any:
				UpperCAmelCase_	:					List[Any]     	=   "cpu"  # ensure determinism for the device-dependent torch.Generator
				UpperCAmelCase_	:					Optional[int]     	=   self.get_dummy_components()
				UpperCAmelCase_	:					List[Any]     	=   PNDMScheduler(
				    beta_start=0.0_0_0_8_5      ,      beta_end=0.0_1_2      ,      beta_schedule="scaled_linear"      ,      skip_prk_steps=lowerCAmelCase_		)
				UpperCAmelCase_	:					int     	=   StableDiffusionPanoramaPipeline(**lowerCAmelCase_		)
				UpperCAmelCase_	:					Any     	=   sd_pipe.to(lowerCAmelCase_		)
				sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

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

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

				UpperCAmelCase_	:					Dict     	=   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      UpperCamelCase_			(unittest.TestCase	):





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[Any]		)		->     int:
				super().tearDown()
				gc.collect()
				torch.cuda.empty_cache()





	def 	_SCREAMING_SNAKE_CASE ( self   :					str      ,      lowerCAmelCase_   :					str=0		)		->     Dict:
				UpperCAmelCase_	:					Dict     	=   torch.manual_seed(lowerCAmelCase_		)
				UpperCAmelCase_	:					Dict     	=   {
				    "prompt": "a photo of the dolomites",
				    "generator": generator,
				    "num_inference_steps": 3,
				    "guidance_scale": 7.5,
				    "output_type": "numpy",
				}
				return inputs





	def 	_SCREAMING_SNAKE_CASE ( self   :					Tuple		)		->     Optional[Any]:
				UpperCAmelCase_	:					Tuple     	=   "stabilityai/stable-diffusion-2-base"
				UpperCAmelCase_	:					str     	=   DDIMScheduler.from_pretrained(lowerCAmelCase_      ,      subfolder="scheduler"		)
				UpperCAmelCase_	:					Tuple     	=   StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase_      ,      scheduler=lowerCAmelCase_      ,      safety_checker=lowerCAmelCase_		)
				pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)
				pipe.enable_attention_slicing()

				UpperCAmelCase_	:					List[str]     	=   self.get_inputs()
				UpperCAmelCase_	:					Optional[Any]     	=   pipe(**lowerCAmelCase_		).images
				UpperCAmelCase_	:					Union[str, Any]     	=   image[0, -3:, -3:, -1].flatten()

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

				UpperCAmelCase_	:					Dict     	=   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 	_SCREAMING_SNAKE_CASE ( self   :					Tuple		)		->     Union[str, Any]:
				UpperCAmelCase_	:					List[Any]     	=   StableDiffusionPanoramaPipeline.from_pretrained(
				    "stabilityai/stable-diffusion-2-base"      ,      safety_checker=lowerCAmelCase_		)
				UpperCAmelCase_	:					Optional[Any]     	=   LMSDiscreteScheduler.from_config(pipe.scheduler.config		)
				pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)
				pipe.enable_attention_slicing()

				UpperCAmelCase_	:					List[str]     	=   self.get_inputs()
				UpperCAmelCase_	:					Union[str, Any]     	=   pipe(**lowerCAmelCase_		).images
				UpperCAmelCase_	:					Optional[Any]     	=   image[0, -3:, -3:, -1].flatten()

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

				UpperCAmelCase_	:					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 	_SCREAMING_SNAKE_CASE ( self   :					List[str]		)		->     int:
				UpperCAmelCase_	:					Any     	=   0

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

										UpperCAmelCase_	:					str     	=   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_	:					List[str]     	=   latents.detach().cpu().numpy()
										assert latents.shape == (1, 4, 64, 256)
										UpperCAmelCase_	:					List[Any]     	=   latents[0, -3:, -3:, -1]

										UpperCAmelCase_	:					Union[str, Any]     	=   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_	:					Tuple     	=   False

				UpperCAmelCase_	:					List[str]     	=   "stabilityai/stable-diffusion-2-base"
				UpperCAmelCase_	:					Optional[int]     	=   DDIMScheduler.from_pretrained(lowerCAmelCase_      ,      subfolder="scheduler"		)
				UpperCAmelCase_	:					Any     	=   StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase_      ,      scheduler=lowerCAmelCase_      ,      safety_checker=lowerCAmelCase_		)
				UpperCAmelCase_	:					Optional[int]     	=   pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)
				pipe.enable_attention_slicing()

				UpperCAmelCase_	:					List[Any]     	=   self.get_inputs()
				pipe(**lowerCAmelCase_      ,      callback=lowerCAmelCase_      ,      callback_steps=1		)
				assert callback_fn.has_been_called
				assert number_of_steps == 3





	def 	_SCREAMING_SNAKE_CASE ( self   :					Optional[int]		)		->     Tuple:
				torch.cuda.empty_cache()
				torch.cuda.reset_max_memory_allocated()
				torch.cuda.reset_peak_memory_stats()

				UpperCAmelCase_	:					Optional[Any]     	=   "stabilityai/stable-diffusion-2-base"
				UpperCAmelCase_	:					Dict     	=   DDIMScheduler.from_pretrained(lowerCAmelCase_      ,      subfolder="scheduler"		)
				UpperCAmelCase_	:					List[Any]     	=   StableDiffusionPanoramaPipeline.from_pretrained(lowerCAmelCase_      ,      scheduler=lowerCAmelCase_      ,      safety_checker=lowerCAmelCase_		)
				UpperCAmelCase_	:					List[Any]     	=   pipe.to(lowerCAmelCase_		)
				pipe.set_progress_bar_config(disable=lowerCAmelCase_		)
				pipe.enable_attention_slicing(1		)
				pipe.enable_sequential_cpu_offload()

				UpperCAmelCase_	:					Optional[Any]     	=   self.get_inputs()
				UpperCAmelCase_	:					str     	=   pipe(**lowerCAmelCase_		)

				UpperCAmelCase_	:					str     	=   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

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

from diffusers import TransformeraDModel, VQDiffusionPipeline, VQDiffusionScheduler, VQModel
from diffusers.pipelines.vq_diffusion.pipeline_vq_diffusion import LearnedClassifierFreeSamplingEmbeddings
from diffusers.utils import load_numpy, slow, torch_device
from diffusers.utils.testing_utils import require_torch_gpu


UpperCamelCase      					=False




class      A     (   unittest.TestCase    ):






 """simple docstring"""





 def 	_UpperCAmelCase							(				self			):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()
 @property
 def 	_UpperCAmelCase							(				self			):
       return 12
 @property
 def 	_UpperCAmelCase							(				self			):
       return 12
 @property
 def 	_UpperCAmelCase							(				self			):
       return 32
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)
       UpperCamelCase_					: Union[str, Any]            =				VQModel(
           block_out_channels=[32, 64]      ,  in_channels=3      ,  out_channels=3      ,  down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]      ,  up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]      ,  latent_channels=3      ,  num_vq_embeddings=self.num_embed      ,  vq_embed_dim=3      ,  )
       return model
 @property
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: str            =				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""			)
       return tokenizer
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)
       UpperCamelCase_					: Any            =				CLIPTextConfig(
           bos_token_id=0      ,  eos_token_id=2      ,  hidden_size=self.text_embedder_hidden_size      ,  intermediate_size=37      ,  layer_norm_eps=1E-05      ,  num_attention_heads=4      ,  num_hidden_layers=5      ,  pad_token_id=1      ,  vocab_size=10_00      ,  )
       return CLIPTextModel(__lowerCAmelCase			)
 @property
 def 	_UpperCAmelCase							(				self			):
       torch.manual_seed(0			)

       UpperCamelCase_					: Optional[Any]            =				12
       UpperCamelCase_					: Optional[int]            =				12

       UpperCamelCase_					: Optional[Any]            =				{
           """attention_bias""": True,
           """cross_attention_dim""": 32,
           """attention_head_dim""": height * width,
           """num_attention_heads""": 1,
           """num_vector_embeds""": self.num_embed,
           """num_embeds_ada_norm""": self.num_embeds_ada_norm,
           """norm_num_groups""": 32,
           """sample_size""": width,
           """activation_fn""": """geglu-approximate""",
       }

       UpperCamelCase_					: Optional[Any]            =				TransformeraDModel(**__lowerCAmelCase			)
       return model
 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: Union[str, Any]            =				"""cpu"""

       UpperCamelCase_					: List[str]            =				self.dummy_vqvae
       UpperCamelCase_					: str            =				self.dummy_text_encoder
       UpperCamelCase_					: Optional[Any]            =				self.dummy_tokenizer
       UpperCamelCase_					: Optional[int]            =				self.dummy_transformer
       UpperCamelCase_					: Optional[Any]            =				VQDiffusionScheduler(self.num_embed			)
       UpperCamelCase_					: Union[str, Any]            =				LearnedClassifierFreeSamplingEmbeddings(learnable=__lowerCAmelCase			)

       UpperCamelCase_					: Tuple            =				VQDiffusionPipeline(
           vqvae=__lowerCAmelCase      ,  text_encoder=__lowerCAmelCase      ,  tokenizer=__lowerCAmelCase      ,  transformer=__lowerCAmelCase      ,  scheduler=__lowerCAmelCase      ,  learned_classifier_free_sampling_embeddings=__lowerCAmelCase      ,  )
       UpperCamelCase_					: Union[str, Any]            =				pipe.to(__lowerCAmelCase			)
       pipe.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Optional[int]            =				"""teddy bear playing in the pool"""

       UpperCamelCase_					: Union[str, Any]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: List[Any]            =				pipe([prompt]      ,  generator=__lowerCAmelCase      ,  num_inference_steps=2      ,  output_type="""np"""			)
       UpperCamelCase_					: Dict            =				output.images

       UpperCamelCase_					: List[str]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: Dict            =				pipe(
           [prompt]      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  return_dict=__lowerCAmelCase      ,  num_inference_steps=2			)[0]

       UpperCamelCase_					: Union[str, Any]            =				image[0, -3:, -3:, -1]
       UpperCamelCase_					: Any            =				image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase_					: List[str]            =				np.array([0.65_51, 0.61_68, 0.50_08, 0.56_76, 0.56_59, 0.42_95, 0.60_73, 0.55_99, 0.49_92]			)

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





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: int            =				"""cpu"""

       UpperCamelCase_					: List[Any]            =				self.dummy_vqvae
       UpperCamelCase_					: Union[str, Any]            =				self.dummy_text_encoder
       UpperCamelCase_					: int            =				self.dummy_tokenizer
       UpperCamelCase_					: Tuple            =				self.dummy_transformer
       UpperCamelCase_					: Tuple            =				VQDiffusionScheduler(self.num_embed			)
       UpperCamelCase_					: List[Any]            =				LearnedClassifierFreeSamplingEmbeddings(
           learnable=__lowerCAmelCase      ,  hidden_size=self.text_embedder_hidden_size      ,  length=tokenizer.model_max_length			)

       UpperCamelCase_					: Optional[Any]            =				VQDiffusionPipeline(
           vqvae=__lowerCAmelCase      ,  text_encoder=__lowerCAmelCase      ,  tokenizer=__lowerCAmelCase      ,  transformer=__lowerCAmelCase      ,  scheduler=__lowerCAmelCase      ,  learned_classifier_free_sampling_embeddings=__lowerCAmelCase      ,  )
       UpperCamelCase_					: List[Any]            =				pipe.to(__lowerCAmelCase			)
       pipe.set_progress_bar_config(disable=__lowerCAmelCase			)

       UpperCamelCase_					: Tuple            =				"""teddy bear playing in the pool"""

       UpperCamelCase_					: List[str]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: Tuple            =				pipe([prompt]      ,  generator=__lowerCAmelCase      ,  num_inference_steps=2      ,  output_type="""np"""			)
       UpperCamelCase_					: List[str]            =				output.images

       UpperCamelCase_					: Optional[int]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: List[str]            =				pipe(
           [prompt]      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  return_dict=__lowerCAmelCase      ,  num_inference_steps=2			)[0]

       UpperCamelCase_					: Dict            =				image[0, -3:, -3:, -1]
       UpperCamelCase_					: Optional[int]            =				image_from_tuple[0, -3:, -3:, -1]

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

       UpperCamelCase_					: List[Any]            =				np.array([0.66_93, 0.60_75, 0.49_59, 0.57_01, 0.55_83, 0.43_33, 0.61_71, 0.56_84, 0.49_88]			)

       assert np.abs(image_slice.flatten() - expected_slice			).max() < 2.0
       assert np.abs(image_from_tuple_slice.flatten() - expected_slice			).max() < 1E-2






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






 """simple docstring"""





 def 	_UpperCAmelCase							(				self			):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()





 def 	_UpperCAmelCase							(				self			):
       UpperCamelCase_					: List[Any]            =				load_numpy(
           """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
           """/vq_diffusion/teddy_bear_pool_classifier_free_sampling.npy"""			)

       UpperCamelCase_					: Optional[int]            =				VQDiffusionPipeline.from_pretrained("""microsoft/vq-diffusion-ithq"""			)
       UpperCamelCase_					: str            =				pipeline.to(__lowerCAmelCase			)
       pipeline.set_progress_bar_config(disable=__lowerCAmelCase			)

       # requires GPU generator for gumbel softmax
       # don't use GPU generator in tests though
       UpperCamelCase_					: List[Any]            =				torch.Generator(device=__lowerCAmelCase			).manual_seed(0			)
       UpperCamelCase_					: str            =				pipeline(
           """teddy bear playing in the pool"""      ,  num_images_per_prompt=1      ,  generator=__lowerCAmelCase      ,  output_type="""np"""      ,  )

       UpperCamelCase_					: Union[str, Any]            =				output.images[0]

       assert image.shape == (2_56, 2_56, 3)
       assert np.abs(expected_image - image			).max() < 2.0






