



"""simple docstring"""

import gc
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, LDMTextToImagePipeline, UNetaDConditionModel
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    load_numpy,
    nightly,
    require_torch_gpu,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()




class SCREAMING_SNAKE_CASE__					(		__snake_case     ,					unittest.TestCase      ):
						_lowerCAmelCase							=					LDMTextToImagePipeline
						_lowerCAmelCase							=					TEXT_TO_IMAGE_PARAMS - {
						    "negative_prompt",
						    "negative_prompt_embeds",
						    "cross_attention_kwargs",
						    "prompt_embeds",
						}
						_lowerCAmelCase							=					PipelineTesterMixin.required_optional_params - {
						    "num_images_per_prompt",
						    "callback",
						    "callback_steps",
						}
						_lowerCAmelCase							=					TEXT_TO_IMAGE_BATCH_PARAMS
						_lowerCAmelCase							=					False



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													torch.manual_seed(0							)
													__a		:		Tuple									=       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						,     )
													__a		:		int									=       DDIMScheduler(
													    beta_start=0.0_0085						,     beta_end=0.012						,     beta_schedule="""scaled_linear"""						,     clip_sample=_lowercase						,     set_alpha_to_one=_lowercase						,     )
													torch.manual_seed(0							)
													__a		:		List[str]									=       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							)
													__a		:		Union[str, 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=1000						,     )
													__a		:		Optional[Any]									=       CLIPTextModel(_lowercase							)
													__a		:		Tuple									=       CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""							)

													__a		:		List[str]									=       {
													    """unet""": unet,
													    """scheduler""": scheduler,
													    """vqvae""": vae,
													    """bert""": text_encoder,
													    """tokenizer""": tokenizer,
													}
													return components



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase=0							):





													'''simple docstring'''

													if str(_lowercase							).startswith("""mps"""							):
																				__a		:		Union[str, Any]									=       torch.manual_seed(_lowercase							)
													else:
																				__a		:		Any									=       torch.Generator(device=_lowercase							).manual_seed(_lowercase							)
													__a		:		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     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Optional[int]									=       """cpu"""  # ensure determinism for the device-dependent torch.Generator

													__a		:		List[str]									=       self.get_dummy_components()
													__a		:		Union[str, Any]									=       LDMTextToImagePipeline(**_lowercase							)
													pipe.to(_lowercase							)
													pipe.set_progress_bar_config(disable=_lowercase							)

													__a		:		Dict									=       self.get_dummy_inputs(_lowercase							)
													__a		:		List[Any]									=       pipe(**_lowercase							).images
													__a		:		Tuple									=       image[0, -3:, -3:, -1]

													assert image.shape == (1, 16, 16, 3)
													__a		:		Union[str, Any]									=       np.array([0.6101, 0.6156, 0.5622, 0.4895, 0.6661, 0.3804, 0.5748, 0.6136, 0.5014]							)

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







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



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase=torch.floataa						,     _lowercase=0							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       torch.manual_seed(_lowercase							)
													__a		:		int									=       np.random.RandomState(_lowercase							).standard_normal((1, 4, 32, 32)							)
													__a		:		Optional[int]									=       torch.from_numpy(_lowercase							).to(device=_lowercase						,     dtype=_lowercase							)
													__a		:		Any									=       {
													    """prompt""": """A painting of a squirrel eating a burger""",
													    """latents""": latents,
													    """generator""": generator,
													    """num_inference_steps""": 3,
													    """guidance_scale""": 6.0,
													    """output_type""": """numpy""",
													}
													return inputs





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		Union[str, Any]									=       LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""							).to(_lowercase							)
													pipe.set_progress_bar_config(disable=_lowercase							)

													__a		:		Optional[Any]									=       self.get_inputs(_lowercase							)
													__a		:		Any									=       pipe(**_lowercase							).images
													__a		:		Optional[int]									=       image[0, -3:, -3:, -1].flatten()

													assert image.shape == (1, 256, 256, 3)
													__a		:		Union[str, Any]									=       np.array([0.5_1825, 0.5_2850, 0.5_2543, 0.5_4258, 0.5_2304, 0.5_2569, 0.5_4363, 0.5_5276, 0.5_6878]							)
													__a		:		int									=       np.abs(expected_slice - image_slice							).max()
													assert max_diff < 1e-3







@nightly
@require_torch_gpu
class SCREAMING_SNAKE_CASE__					(		unittest.TestCase      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

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



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase=torch.floataa						,     _lowercase=0							):





													'''simple docstring'''

													__a		:		List[str]									=       torch.manual_seed(_lowercase							)
													__a		:		str									=       np.random.RandomState(_lowercase							).standard_normal((1, 4, 32, 32)							)
													__a		:		List[Any]									=       torch.from_numpy(_lowercase							).to(device=_lowercase						,     dtype=_lowercase							)
													__a		:		int									=       {
													    """prompt""": """A painting of a squirrel eating a burger""",
													    """latents""": latents,
													    """generator""": generator,
													    """num_inference_steps""": 50,
													    """guidance_scale""": 6.0,
													    """output_type""": """numpy""",
													}
													return inputs





						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[Any]									=       LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""							).to(_lowercase							)
													pipe.set_progress_bar_config(disable=_lowercase							)

													__a		:		Tuple									=       self.get_inputs(_lowercase							)
													__a		:		str									=       pipe(**_lowercase							).images[0]

													__a		:		Any									=       load_numpy(
													    """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy"""							)
													__a		:		List[str]									=       np.abs(expected_image - image							).max()
													assert max_diff < 1e-3






"""simple docstring"""

def       __magic_name__    (   _lowerCamelCase				: int  ,       _lowerCamelCase				: float  ,       _lowerCamelCase				: float					):
							return round(float(moles / volume					) * nfactor					)





def       __magic_name__    (   _lowerCamelCase				: float  ,       _lowerCamelCase				: float  ,       _lowerCamelCase				: float					):
							return round(float((moles * 0.08_21 * temperature) / (volume)					)					)





def       __magic_name__    (   _lowerCamelCase				: float  ,       _lowerCamelCase				: float  ,       _lowerCamelCase				: float					):
							return round(float((moles * 0.08_21 * temperature) / (pressure)					)					)





def       __magic_name__    (   _lowerCamelCase				: float  ,       _lowerCamelCase				: float  ,       _lowerCamelCase				: float					):
							return round(float((pressure * volume) / (0.08_21 * moles)					)					)


if __name__ == "__main__":
	import doctest

	doctest.testmod()

