




"""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   a__			(       unittest.TestCase  ):
						snake_case__             = StableDiffusionLDMaDPipeline
						snake_case__             = TEXT_TO_IMAGE_PARAMS
						snake_case__             = TEXT_TO_IMAGE_BATCH_PARAMS
						snake_case__             = TEXT_TO_IMAGE_IMAGE_PARAMS





						def       __UpperCamelCase     (							self      :	str)						->       Optional[int]:




											"""simple docstring"""







											torch.manual_seed(0)
											_lowerCAmelCase:List[Any]									=  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:str									=  DDIMScheduler(
											    beta_start=0.00085     ,beta_end=0.012     ,beta_schedule='''scaled_linear'''     ,clip_sample=a__     ,set_alpha_to_one=a__     ,)
											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=1000     ,)
											_lowerCAmelCase:int									=  CLIPTextModel(a__)
											_lowerCAmelCase:Any									=  CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

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





						def       __UpperCamelCase     (							self      :	int     ,a__      :	Any     ,a__      :	Union[str, Any]=0)						->       List[str]:




											"""simple docstring"""







											if str(a__).startswith('''mps'''):
																_lowerCAmelCase:Dict									=  torch.manual_seed(a__)
											else:
																_lowerCAmelCase:List[Any]									=  torch.Generator(device=a__).manual_seed(a__)
											_lowerCAmelCase:Union[str, Any]									=  {
											    '''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       __UpperCamelCase     (							self      :	List[str])						->       Union[str, Any]:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator

											_lowerCAmelCase:List[Any]									=  self.get_dummy_components()
											_lowerCAmelCase:Optional[int]									=  StableDiffusionLDMaDPipeline(**a__)
											_lowerCAmelCase:str									=  ldmad_pipe.to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:str									=  self.get_dummy_inputs(a__)
											_lowerCAmelCase:str									=  ldmad_pipe(**a__)
											_lowerCAmelCase:List[str]									=  output.rgb, output.depth

											_lowerCAmelCase:int									=  rgb[0, -3:, -3:, -1]
											_lowerCAmelCase:List[str]									=  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:List[str]									=  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       __UpperCamelCase     (							self      :	Any)						->       Any:




											"""simple docstring"""







											_lowerCAmelCase:int									=  self.get_dummy_components()
											_lowerCAmelCase:List[Any]									=  StableDiffusionLDMaDPipeline(**a__)
											_lowerCAmelCase:Optional[Any]									=  ldmad_pipe.to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:Optional[int]									=  self.get_dummy_inputs(a__)
											_lowerCAmelCase:Any									=  3 * [inputs['''prompt''']]

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

											_lowerCAmelCase:List[str]									=  self.get_dummy_inputs(a__)
											_lowerCAmelCase:Dict									=  3 * [inputs.pop('''prompt''')]

											_lowerCAmelCase:List[Any]									=  ldmad_pipe.tokenizer(
											    a__     ,padding='''max_length'''     ,max_length=ldmad_pipe.tokenizer.model_max_length     ,truncation=a__     ,return_tensors='''pt'''     ,)
											_lowerCAmelCase:str									=  text_inputs['''input_ids'''].to(a__)

											_lowerCAmelCase:Tuple									=  ldmad_pipe.text_encoder(a__)[0]

											_lowerCAmelCase:List[Any]									=  prompt_embeds

											# forward
											_lowerCAmelCase:Optional[int]									=  ldmad_pipe(**a__)
											_lowerCAmelCase:Tuple									=  output.rgb, output.depth
											_lowerCAmelCase:Optional[Any]									=  rgb_slice_a[0, -3:, -3:, -1]
											_lowerCAmelCase:List[str]									=  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       __UpperCamelCase     (							self      :	Dict)						->       int:




											"""simple docstring"""







											_lowerCAmelCase:str									=  '''cpu'''  # ensure determinism for the device-dependent torch.Generator
											_lowerCAmelCase:Dict									=  self.get_dummy_components()
											_lowerCAmelCase:Tuple									=  PNDMScheduler(skip_prk_steps=a__)
											_lowerCAmelCase:Dict									=  StableDiffusionLDMaDPipeline(**a__)
											_lowerCAmelCase:Optional[int]									=  ldmad_pipe.to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:Any									=  self.get_dummy_inputs(a__)
											_lowerCAmelCase:Any									=  '''french fries'''
											_lowerCAmelCase:List[Any]									=  ldmad_pipe(**a__     ,negative_prompt=a__)

											_lowerCAmelCase:int									=  output.rgb, output.depth
											_lowerCAmelCase:Union[str, Any]									=  rgb[0, -3:, -3:, -1]
											_lowerCAmelCase:str									=  depth[0, -3:, -1]

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

											_lowerCAmelCase:List[Any]									=  np.array(
											    [0.37044, 0.71811503, 0.7223251, 0.48603675, 0.5638391, 0.6364948, 0.42833704, 0.4901315, 0.47926217])
											_lowerCAmelCase:List[Any]									=  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   a__			(       unittest.TestCase  ):





						def       __UpperCamelCase     (							self      :	List[str])						->       List[str]:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	Dict     ,a__      :	Optional[Any]     ,a__      :	Union[str, Any]="cpu"     ,a__      :	int=torch.floataa     ,a__      :	int=0)						->       List[Any]:




											"""simple docstring"""







											_lowerCAmelCase:Dict									=  torch.Generator(device=a__).manual_seed(a__)
											_lowerCAmelCase:Union[str, Any]									=  np.random.RandomState(a__).standard_normal((1, 4, 64, 64))
											_lowerCAmelCase:str									=  torch.from_numpy(a__).to(device=a__     ,dtype=a__)
											_lowerCAmelCase: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       __UpperCamelCase     (							self      :	List[str])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:str									=  StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''')
											_lowerCAmelCase:List[str]									=  ldmad_pipe.to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:Union[str, Any]									=  self.get_inputs(a__)
											_lowerCAmelCase:Dict									=  ldmad_pipe(**a__)
											_lowerCAmelCase:List[Any]									=  output.rgb, output.depth
											_lowerCAmelCase:str									=  rgb[0, -3:, -3:, -1].flatten()
											_lowerCAmelCase:Dict									=  rgb[0, -3:, -1].flatten()

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

											_lowerCAmelCase:int									=  np.array(
											    [0.53805465, 0.56707305, 0.5486515, 0.57012236, 0.5814511, 0.56253487, 0.54843014, 0.55092263, 0.6459706])
											_lowerCAmelCase:Optional[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   a__			(       unittest.TestCase  ):





						def       __UpperCamelCase     (							self      :	List[Any])						->       Dict:




											"""simple docstring"""







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





						def       __UpperCamelCase     (							self      :	int     ,a__      :	Optional[int]     ,a__      :	Union[str, Any]="cpu"     ,a__      :	List[str]=torch.floataa     ,a__      :	Optional[int]=0)						->       int:




											"""simple docstring"""







											_lowerCAmelCase:List[Any]									=  torch.Generator(device=a__).manual_seed(a__)
											_lowerCAmelCase:Optional[Any]									=  np.random.RandomState(a__).standard_normal((1, 4, 64, 64))
											_lowerCAmelCase:Dict									=  torch.from_numpy(a__).to(device=a__     ,dtype=a__)
											_lowerCAmelCase:Optional[int]									=  {
											    '''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       __UpperCamelCase     (							self      :	List[str])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:List[str]									=  StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d''').to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:int									=  self.get_inputs(a__)
											_lowerCAmelCase:int									=  ldmad_pipe(**a__)
											_lowerCAmelCase:List[str]									=  output.rgb, output.depth

											_lowerCAmelCase:Any									=  0.495586
											_lowerCAmelCase:Union[str, Any]									=  0.33795515
											_lowerCAmelCase:Tuple									=  112.48518
											_lowerCAmelCase: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       __UpperCamelCase     (							self      :	List[str])						->       Dict:




											"""simple docstring"""







											_lowerCAmelCase:Tuple									=  StableDiffusionLDMaDPipeline.from_pretrained('''Intel/ldm3d-4c''').to(a__)
											ldmad_pipe.set_progress_bar_config(disable=a__)

											_lowerCAmelCase:Optional[int]									=  self.get_inputs(a__)
											_lowerCAmelCase:str									=  ldmad_pipe(**a__)
											_lowerCAmelCase:str									=  output.rgb, output.depth

											_lowerCAmelCase:List[str]									=  0.4194127
											_lowerCAmelCase:Dict									=  0.35375586
											_lowerCAmelCase:Any									=  0.5638502
											_lowerCAmelCase:Tuple									=  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"""






import warnings

from ...utils import logging
from .image_processing_poolformer import PoolFormerImageProcessor


UpperCamelCase__						=			logging.get_logger(__name__)




class   a__			(       UpperCamelCase_  ):





						def __init__(							self      :	Dict     ,*a__      :	List[str]     ,**a__      :	str)						->       None:




											"""simple docstring"""







											warnings.warn(
											    '''The class PoolFormerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
											    ''' Please use PoolFormerImageProcessor instead.'''     ,a__     ,)
											super().__init__(*a__     ,**a__)






