



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

			'''simple docstring'''




			UpperCamelCase__      :			Dict      =  StableDiffusionLDMaDPipeline
			UpperCamelCase__      :			List[str]      =  TEXT_TO_IMAGE_PARAMS
			UpperCamelCase__      :			Dict      =  TEXT_TO_IMAGE_BATCH_PARAMS
			UpperCamelCase__      :			Any      =  TEXT_TO_IMAGE_IMAGE_PARAMS






			def 	_A     (   self		):







										'''simple docstring'''

										torch.manual_seed(0		)
										__SCREAMING_SNAKE_CASE									= 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				,							)
										__SCREAMING_SNAKE_CASE									= DDIMScheduler(
										    beta_start=0.0_0_0_8_5				,							beta_end=0.0_1_2				,							beta_schedule='scaled_linear'				,							clip_sample=_SCREAMING_SNAKE_CASE				,							set_alpha_to_one=_SCREAMING_SNAKE_CASE				,							)
										torch.manual_seed(0		)
										__SCREAMING_SNAKE_CASE									= 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		)
										__SCREAMING_SNAKE_CASE									= 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				,							)
										__SCREAMING_SNAKE_CASE									= CLIPTextModel(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'		)

										__SCREAMING_SNAKE_CASE									= {
										    '''unet''': unet,
										    '''scheduler''': scheduler,
										    '''vae''': vae,
										    '''text_encoder''': text_encoder,
										    '''tokenizer''': tokenizer,
										    '''safety_checker''': None,
										    '''feature_extractor''': None,
										}
										return components






			def 	_A     (   self				,							_A				,							_A=0		):







										'''simple docstring'''

										if str(_SCREAMING_SNAKE_CASE		).startswith('mps'		):
																	__SCREAMING_SNAKE_CASE									= torch.manual_seed(_SCREAMING_SNAKE_CASE		)
										else:
																	__SCREAMING_SNAKE_CASE									= torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= {
										    '''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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= '''cpu'''  # ensure determinism for the device-dependent torch.Generator

										__SCREAMING_SNAKE_CASE									= self.get_dummy_components()
										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe.to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_dummy_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth

										__SCREAMING_SNAKE_CASE									= rgb[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE									= depth[0, -3:, -1]

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

										__SCREAMING_SNAKE_CASE									= np.array(
										    [0.3_7_3_3_8_1_7_6, 0.7_0_2_4_7, 0.7_4_2_0_3_1_9_3, 0.5_1_6_4_3_6_0_4, 0.5_8_2_5_6_7_9_3, 0.6_0_9_3_2_1_3_6, 0.4_1_8_1_0_9_5, 0.4_8_3_5_5_8_7_7, 0.4_6_5_3_5_2_6_2]		)
										__SCREAMING_SNAKE_CASE									= np.array([103.46_727, 85.812_004, 87.849_236]		)

										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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= self.get_dummy_components()
										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe.to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_dummy_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= 3 * [inputs['''prompt''']]

										# forward
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth
										__SCREAMING_SNAKE_CASE									= rgb_slice_a[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE									= depth_slice_a[0, -3:, -1]

										__SCREAMING_SNAKE_CASE									= self.get_dummy_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= 3 * [inputs.pop('prompt'		)]

										__SCREAMING_SNAKE_CASE									= ldmad_pipe.tokenizer(
										    _SCREAMING_SNAKE_CASE				,							padding='max_length'				,							max_length=ldmad_pipe.tokenizer.model_max_length				,							truncation=_SCREAMING_SNAKE_CASE				,							return_tensors='pt'				,							)
										__SCREAMING_SNAKE_CASE									= text_inputs['''input_ids'''].to(_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= ldmad_pipe.text_encoder(_SCREAMING_SNAKE_CASE		)[0]

										__SCREAMING_SNAKE_CASE									= prompt_embeds

										# forward
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth
										__SCREAMING_SNAKE_CASE									= rgb_slice_a[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE									= 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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= '''cpu'''  # ensure determinism for the device-dependent torch.Generator
										__SCREAMING_SNAKE_CASE									= self.get_dummy_components()
										__SCREAMING_SNAKE_CASE									= PNDMScheduler(skip_prk_steps=_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe.to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_dummy_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= '''french fries'''
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE				,							negative_prompt=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth
										__SCREAMING_SNAKE_CASE									= rgb[0, -3:, -3:, -1]
										__SCREAMING_SNAKE_CASE									= depth[0, -3:, -1]

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

										__SCREAMING_SNAKE_CASE									= np.array(
										    [0.3_7_0_4_4, 0.7_1_8_1_1_5_0_3, 0.7_2_2_3_2_5_1, 0.4_8_6_0_3_6_7_5, 0.5_6_3_8_3_9_1, 0.6_3_6_4_9_4_8, 0.4_2_8_3_3_7_0_4, 0.4_9_0_1_3_1_5, 0.4_7_9_2_6_2_1_7]		)
										__SCREAMING_SNAKE_CASE									= np.array([107.84_738, 84.62_802, 89.962_135]		)
										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        UpperCAmelCase_  (       unittest.TestCase	):

			'''simple docstring'''




			def 	_A     (   self		):







										'''simple docstring'''

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






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







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= np.random.RandomState(_SCREAMING_SNAKE_CASE		).standard_normal((1, 4, 64, 64)		)
										__SCREAMING_SNAKE_CASE									= torch.from_numpy(_SCREAMING_SNAKE_CASE		).to(device=_SCREAMING_SNAKE_CASE				,							dtype=_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= {
										    '''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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline.from_pretrained('Intel/ldm3d'		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe.to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth
										__SCREAMING_SNAKE_CASE									= rgb[0, -3:, -3:, -1].flatten()
										__SCREAMING_SNAKE_CASE									= rgb[0, -3:, -1].flatten()

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

										__SCREAMING_SNAKE_CASE									= np.array(
										    [0.5_3_8_0_5_4_6_5, 0.5_6_7_0_7_3_0_5, 0.5_4_8_6_5_1_5, 0.5_7_0_1_2_2_3_6, 0.5_8_1_4_5_1_1, 0.5_6_2_5_3_4_8_7, 0.5_4_8_4_3_0_1_4, 0.5_5_0_9_2_2_6_3, 0.6_4_5_9_7_0_6]		)
										__SCREAMING_SNAKE_CASE									= np.array(
										    [0.9_2_6_3_7_8_1, 0.6_6_7_8_6_7_2, 0.5_4_8_6_5_1_5, 0.9_2_2_0_2_1_4_5, 0.6_7_8_3_1_1_3_5, 0.5_6_2_5_3_4_8_7, 0.9_2_4_1_6_9_4, 0.7_5_5_1_4_7_8, 0.6_4_5_9_7_0_6]		)
										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        UpperCAmelCase_  (       unittest.TestCase	):

			'''simple docstring'''




			def 	_A     (   self		):







										'''simple docstring'''

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






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







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= torch.Generator(device=_SCREAMING_SNAKE_CASE		).manual_seed(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= np.random.RandomState(_SCREAMING_SNAKE_CASE		).standard_normal((1, 4, 64, 64)		)
										__SCREAMING_SNAKE_CASE									= torch.from_numpy(_SCREAMING_SNAKE_CASE		).to(device=_SCREAMING_SNAKE_CASE				,							dtype=_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= {
										    '''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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline.from_pretrained('Intel/ldm3d'		).to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth

										__SCREAMING_SNAKE_CASE									= 0.4_9_5_5_8_6
										__SCREAMING_SNAKE_CASE									= 0.3_3_7_9_5_5_1_5
										__SCREAMING_SNAKE_CASE									= 112.48_518
										__SCREAMING_SNAKE_CASE									= 98.489_746
										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 	_A     (   self		):







										'''simple docstring'''

										__SCREAMING_SNAKE_CASE									= StableDiffusionLDMaDPipeline.from_pretrained('Intel/ldm3d-4c'		).to(_SCREAMING_SNAKE_CASE		)
										ldmad_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE		)

										__SCREAMING_SNAKE_CASE									= self.get_inputs(_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= ldmad_pipe(**_SCREAMING_SNAKE_CASE		)
										__SCREAMING_SNAKE_CASE									= output.rgb, output.depth

										__SCREAMING_SNAKE_CASE									= 0.4_1_9_4_1_2_7
										__SCREAMING_SNAKE_CASE									= 0.3_5_3_7_5_5_8_6
										__SCREAMING_SNAKE_CASE									= 0.5_6_3_8_5_0_2
										__SCREAMING_SNAKE_CASE									= 0.3_4_6_8_6_1_0_3

										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











import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__		:					Dict							=logging.get_logger(__name__)

lowerCAmelCase__		:					Optional[int]							={
    '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''',
    # See all SEW models at https://huggingface.co/models?filter=sew
}

class        UpperCAmelCase_  (       UpperCamelCase_	):

						'''simple docstring'''




						UpperCamelCase__      :			Optional[Any]      =  '''sew'''






						def __init__(   self				,							_A=32				,							_A=768				,							_A=12				,							_A=12				,							_A=3_072				,							_A=2				,							_A="gelu"				,							_A=0.1				,							_A=0.1				,							_A=0.1				,							_A=0.0				,							_A=0.1				,							_A=0.1				,							_A=0.0_2				,							_A=1e-5				,							_A="group"				,							_A="gelu"				,							_A=(64, 128, 128, 128, 128, 256, 256, 256, 256, 512, 512, 512, 512)				,							_A=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)				,							_A=(10, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)				,							_A=False				,							_A=128				,							_A=16				,							_A=True				,							_A=0.0_5				,							_A=10				,							_A=2				,							_A=0.0				,							_A=10				,							_A=0				,							_A="mean"				,							_A=False				,							_A=False				,							_A=256				,							_A=0				,							_A=1				,							_A=2				,							**_A				,							):







													'''simple docstring'''

													super().__init__(**_A				,							pad_token_id=_A				,							bos_token_id=_A				,							eos_token_id=_A		)
													__SCREAMING_SNAKE_CASE									= hidden_size
													__SCREAMING_SNAKE_CASE									= feat_extract_norm
													__SCREAMING_SNAKE_CASE									= feat_extract_activation
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= list(_A		)
													__SCREAMING_SNAKE_CASE									= conv_bias
													__SCREAMING_SNAKE_CASE									= num_conv_pos_embeddings
													__SCREAMING_SNAKE_CASE									= num_conv_pos_embedding_groups
													__SCREAMING_SNAKE_CASE									= len(self.conv_dim		)
													__SCREAMING_SNAKE_CASE									= num_hidden_layers
													__SCREAMING_SNAKE_CASE									= intermediate_size
													__SCREAMING_SNAKE_CASE									= squeeze_factor
													__SCREAMING_SNAKE_CASE									= hidden_act
													__SCREAMING_SNAKE_CASE									= num_attention_heads
													__SCREAMING_SNAKE_CASE									= hidden_dropout
													__SCREAMING_SNAKE_CASE									= attention_dropout
													__SCREAMING_SNAKE_CASE									= activation_dropout
													__SCREAMING_SNAKE_CASE									= feat_proj_dropout
													__SCREAMING_SNAKE_CASE									= final_dropout
													__SCREAMING_SNAKE_CASE									= layerdrop
													__SCREAMING_SNAKE_CASE									= layer_norm_eps
													__SCREAMING_SNAKE_CASE									= initializer_range
													__SCREAMING_SNAKE_CASE									= vocab_size

													if (
													    (len(self.conv_stride		) != self.num_feat_extract_layers)
													    or (len(self.conv_kernel		) != self.num_feat_extract_layers)
													    or (len(self.conv_dim		) != self.num_feat_extract_layers)
													):
																				raise ValueError(
																				    'Configuration for convolutional layers is incorrect.'
																				    'It is required that `len(config.conv_dim)` == `len(config.conv_stride)` == `len(config.conv_kernel)`,'
																				    f"""but is `len(config.conv_dim) = {len(self.conv_dim		)}`, `len(config.conv_stride)"""
																				    f"""= {len(self.conv_stride		)}`, `len(config.conv_kernel) = {len(self.conv_kernel		)}`."""		)

													# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
													__SCREAMING_SNAKE_CASE									= apply_spec_augment
													__SCREAMING_SNAKE_CASE									= mask_time_prob
													__SCREAMING_SNAKE_CASE									= mask_time_length
													__SCREAMING_SNAKE_CASE									= mask_time_min_masks
													__SCREAMING_SNAKE_CASE									= mask_feature_prob
													__SCREAMING_SNAKE_CASE									= mask_feature_length
													__SCREAMING_SNAKE_CASE									= mask_feature_min_masks

													# ctc loss
													__SCREAMING_SNAKE_CASE									= ctc_loss_reduction
													__SCREAMING_SNAKE_CASE									= ctc_zero_infinity

													# sequence classification
													__SCREAMING_SNAKE_CASE									= use_weighted_layer_sum
													__SCREAMING_SNAKE_CASE									= classifier_proj_size






						@property
						def 	_A     (   self		):







													'''simple docstring'''

													return functools.reduce(operator.mul				,							self.conv_stride				,							1		)






