





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   (       _a				,						unittest.TestCase    ):
							snake_case       : Optional[int]         =    LDMTextToImagePipeline
							snake_case       : str         =    TEXT_TO_IMAGE_PARAMS - {
							    """negative_prompt""",
							    """negative_prompt_embeds""",
							    """cross_attention_kwargs""",
							    """prompt_embeds""",
							}
							snake_case       : Optional[int]         =    PipelineTesterMixin.required_optional_params - {
							    """num_images_per_prompt""",
							    """callback""",
							    """callback_steps""",
							}
							snake_case       : Optional[int]         =    TEXT_TO_IMAGE_BATCH_PARAMS
							snake_case       : Any         =    False






							def 			_lowerCamelCase						(  self					):
														torch.manual_seed(0					)
														UpperCamelCase__			       =     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					,	)
														UpperCamelCase__			       =     DDIMScheduler(
														    beta_start=0.0_0085					,	beta_end=0.012					,	beta_schedule="""scaled_linear"""					,	clip_sample=__lowerCAmelCase					,	set_alpha_to_one=__lowerCAmelCase					,	)
														torch.manual_seed(0					)
														UpperCamelCase__			       =     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__			       =     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					,	)
														UpperCamelCase__			       =     CLIPTextModel(__lowerCAmelCase					)
														UpperCamelCase__			       =     CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

														UpperCamelCase__			       =     {
														    """unet""": unet,
														    """scheduler""": scheduler,
														    """vqvae""": vae,
														    """bert""": text_encoder,
														    """tokenizer""": tokenizer,
														}
														return components






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=0					):
														if str(__lowerCAmelCase					).startswith("""mps"""					):
																					UpperCamelCase__			       =     torch.manual_seed(__lowerCAmelCase					)
														else:
																					UpperCamelCase__			       =     torch.Generator(device=__lowerCAmelCase					).manual_seed(__lowerCAmelCase					)
														UpperCamelCase__			       =     {
														    """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					):
														UpperCamelCase__			       =     """cpu"""  # ensure determinism for the device-dependent torch.Generator

														UpperCamelCase__			       =     self.get_dummy_components()
														UpperCamelCase__			       =     LDMTextToImagePipeline(**__lowerCAmelCase					)
														pipe.to(__lowerCAmelCase					)
														pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

														UpperCamelCase__			       =     self.get_dummy_inputs(__lowerCAmelCase					)
														UpperCamelCase__			       =     pipe(**__lowerCAmelCase					).images
														UpperCamelCase__			       =     image[0, -3:, -3:, -1]

														assert image.shape == (1, 16, 16, 3)
														UpperCamelCase__			       =     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					):
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=torch.floataa					,	__lowerCAmelCase=0					):
														UpperCamelCase__			       =     torch.manual_seed(__lowerCAmelCase					)
														UpperCamelCase__			       =     np.random.RandomState(__lowerCAmelCase					).standard_normal((1, 4, 32, 32)					)
														UpperCamelCase__			       =     torch.from_numpy(__lowerCAmelCase					).to(device=__lowerCAmelCase					,	dtype=__lowerCAmelCase					)
														UpperCamelCase__			       =     {
														    """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					):
														UpperCamelCase__			       =     LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""					).to(__lowerCAmelCase					)
														pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

														UpperCamelCase__			       =     self.get_inputs(__lowerCAmelCase					)
														UpperCamelCase__			       =     pipe(**__lowerCAmelCase					).images
														UpperCamelCase__			       =     image[0, -3:, -3:, -1].flatten()

														assert image.shape == (1, 256, 256, 3)
														UpperCamelCase__			       =     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]					)
														UpperCamelCase__			       =     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					):
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()






							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					,	__lowerCAmelCase=torch.floataa					,	__lowerCAmelCase=0					):
														UpperCamelCase__			       =     torch.manual_seed(__lowerCAmelCase					)
														UpperCamelCase__			       =     np.random.RandomState(__lowerCAmelCase					).standard_normal((1, 4, 32, 32)					)
														UpperCamelCase__			       =     torch.from_numpy(__lowerCAmelCase					).to(device=__lowerCAmelCase					,	dtype=__lowerCAmelCase					)
														UpperCamelCase__			       =     {
														    """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					):
														UpperCamelCase__			       =     LDMTextToImagePipeline.from_pretrained("""CompVis/ldm-text2im-large-256"""					).to(__lowerCAmelCase					)
														pipe.set_progress_bar_config(disable=__lowerCAmelCase					)

														UpperCamelCase__			       =     self.get_inputs(__lowerCAmelCase					)
														UpperCamelCase__			       =     pipe(**__lowerCAmelCase					).images[0]

														UpperCamelCase__			       =     load_numpy(
														    """https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/ldm_text2img/ldm_large_256_ddim.npy"""					)
														UpperCamelCase__			       =     np.abs(expected_image - image					).max()
														assert max_diff < 1E-3











import warnings

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


UpperCamelCase__     =	logging.get_logger(__name__)

UpperCamelCase__     =	{
    "xlnet-base-cased": "https://huggingface.co/xlnet-base-cased/resolve/main/config.json",
    "xlnet-large-cased": "https://huggingface.co/xlnet-large-cased/resolve/main/config.json",
}

class     __SCREAMING_SNAKE_CASE   (       _a    ):
							snake_case       : Any         =    """xlnet"""
							snake_case       : Optional[Any]         =    ["""mems"""]
							snake_case       : Any         =    {
							    """n_token""": """vocab_size""",  # Backward compatibility
							    """hidden_size""": """d_model""",
							    """num_attention_heads""": """n_head""",
							    """num_hidden_layers""": """n_layer""",
							}






							def __init__(  self					,	__lowerCAmelCase=32000					,	__lowerCAmelCase=1024					,	__lowerCAmelCase=24					,	__lowerCAmelCase=16					,	__lowerCAmelCase=4096					,	__lowerCAmelCase="gelu"					,	__lowerCAmelCase=True					,	__lowerCAmelCase="bi"					,	__lowerCAmelCase=0.02					,	__lowerCAmelCase=1E-12					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=512					,	__lowerCAmelCase=None					,	__lowerCAmelCase=True					,	__lowerCAmelCase=False					,	__lowerCAmelCase=False					,	__lowerCAmelCase=-1					,	__lowerCAmelCase=False					,	__lowerCAmelCase="last"					,	__lowerCAmelCase=True					,	__lowerCAmelCase="tanh"					,	__lowerCAmelCase=0.1					,	__lowerCAmelCase=5					,	__lowerCAmelCase=5					,	__lowerCAmelCase=5					,	__lowerCAmelCase=1					,	__lowerCAmelCase=2					,	**__lowerCAmelCase					,	):
														UpperCamelCase__			       =     vocab_size
														UpperCamelCase__			       =     d_model
														UpperCamelCase__			       =     n_layer
														UpperCamelCase__			       =     n_head
														if d_model % n_head != 0:
																					raise ValueError(f"""'d_model % n_head' ({d_model % n_head}) should be equal to 0"""					)
														if "d_head" in kwargs:
																					if kwargs["d_head"] != d_model // n_head:
																												raise ValueError(
																												    f"""`d_head` ({kwargs["d_head"]}) should be equal to `d_model // n_head` ({d_model // n_head})"""					)
														UpperCamelCase__			       =     d_model // n_head
														UpperCamelCase__			       =     ff_activation
														UpperCamelCase__			       =     d_inner
														UpperCamelCase__			       =     untie_r
														UpperCamelCase__			       =     attn_type

														UpperCamelCase__			       =     initializer_range
														UpperCamelCase__			       =     layer_norm_eps

														UpperCamelCase__			       =     dropout
														UpperCamelCase__			       =     mem_len
														UpperCamelCase__			       =     reuse_len
														UpperCamelCase__			       =     bi_data
														UpperCamelCase__			       =     clamp_len
														UpperCamelCase__			       =     same_length

														UpperCamelCase__			       =     summary_type
														UpperCamelCase__			       =     summary_use_proj
														UpperCamelCase__			       =     summary_activation
														UpperCamelCase__			       =     summary_last_dropout
														UpperCamelCase__			       =     start_n_top
														UpperCamelCase__			       =     end_n_top

														UpperCamelCase__			       =     bos_token_id
														UpperCamelCase__			       =     pad_token_id
														UpperCamelCase__			       =     eos_token_id

														if "use_cache" in kwargs:
																					warnings.warn(
																					    """The `use_cache` argument is deprecated and will be removed in a future version, use `use_mems_eval`"""
																					    """ instead."""					,	__lowerCAmelCase					,	)
																					UpperCamelCase__			       =     kwargs["""use_cache"""]

														UpperCamelCase__			       =     use_mems_eval
														UpperCamelCase__			       =     use_mems_train
														super().__init__(pad_token_id=__lowerCAmelCase					,	bos_token_id=__lowerCAmelCase					,	eos_token_id=__lowerCAmelCase					,	**__lowerCAmelCase					)






							@property
							def 			_lowerCamelCase						(  self					):
														logger.info(f"""The model {self.model_type} is one of the few models that has no sequence length limit."""					)
														return -1







							@max_position_embeddings.setter
							def 			_lowerCamelCase						(  self					,	__lowerCAmelCase					):
														# Message copied from Transformer-XL documentation
														raise NotImplementedError(
														    f"""The model {self.model_type} is one of the few models that has no sequence length limit."""					)




