

import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import 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
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class    A__(__lowerCAmelCase,       __lowerCAmelCase,       unittest.TestCase    ):
						"""simple docstring"""






						_A  :							List[str]   			=	StableDiffusionParadigmsPipeline
						_A  :							int   			=	TEXT_TO_IMAGE_PARAMS
						_A  :							Dict   			=	TEXT_TO_IMAGE_BATCH_PARAMS
						_A  :							Tuple   			=	TEXT_TO_IMAGE_IMAGE_PARAMS
						_A  :							List[Any]   			=	TEXT_TO_IMAGE_IMAGE_PARAMS





						def 	UpperCamelCase__			(  self  )      ->				Optional[int]:
									torch.manual_seed(0  )
									a_		:		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       ,     attention_head_dim=(2, 4)       ,     use_linear_projection=__SCREAMING_SNAKE_CASE       ,     )
									a_		:		int				    =				DDIMParallelScheduler(
									    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  )
									a_		:		Optional[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       ,     sample_size=128       ,     )
									torch.manual_seed(0  )
									a_		:		int				    =				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       ,     hidden_act="""gelu"""       ,     projection_dim=512       ,     )
									a_		:		Optional[int]				    =				CLIPTextModel(__SCREAMING_SNAKE_CASE  )
									a_		:		Dict				    =				CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""  )

									a_		:		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       ,     _lowercase       ,     _lowercase=0  )      ->				Optional[int]:




									if str(__SCREAMING_SNAKE_CASE  ).startswith("""mps"""  ):
												a_		:		Optional[Any]				    =				torch.manual_seed(__SCREAMING_SNAKE_CASE  )
									else:
												a_		:		int				    =				torch.Generator(device=__SCREAMING_SNAKE_CASE  ).manual_seed(__SCREAMING_SNAKE_CASE  )
									a_		:		Optional[int]				    =				{
									    """prompt""": """a photograph of an astronaut riding a horse""",
									    """generator""": generator,
									    """num_inference_steps""": 10,
									    """guidance_scale""": 6.0,
									    """output_type""": """numpy""",
									    """parallel""": 3,
									    """debug""": True,
									}
									return inputs





						def 	UpperCamelCase__			(  self  )      ->				Optional[int]:
									a_		:		int				    =				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
									a_		:		Optional[Any]				    =				self.get_dummy_components()
									a_		:		str				    =				StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE  )
									a_		:		Any				    =				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
									sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

									a_		:		str				    =				self.get_dummy_inputs(__SCREAMING_SNAKE_CASE  )
									a_		:		List[str]				    =				sd_pipe(**__SCREAMING_SNAKE_CASE  ).images
									a_		:		Tuple				    =				image[0, -3:, -3:, -1]
									assert image.shape == (1, 64, 64, 3)

									a_		:		Any				    =				np.array([0.4_7_7_3, 0.5_4_1_7, 0.4_7_2_3, 0.4_9_2_5, 0.5_6_3_1, 0.4_7_5_2, 0.5_2_4_0, 0.4_9_3_5, 0.5_0_2_3]  )

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





						def 	UpperCamelCase__			(  self  )      ->				int:
									a_		:		str				    =				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
									a_		:		Tuple				    =				self.get_dummy_components()
									torch.manual_seed(0  )
									a_		:		Tuple				    =				DDPMParallelScheduler()
									torch.manual_seed(0  )
									a_		:		Optional[int]				    =				StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE  )
									a_		:		int				    =				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
									sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

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

									a_		:		Tuple				    =				np.array([0.3_5_7_3, 0.4_4_2_0, 0.4_9_6_0, 0.4_7_9_9, 0.3_7_9_6, 0.3_8_7_9, 0.4_8_1_9, 0.4_3_6_5, 0.4_4_6_8]  )

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





						def 	UpperCamelCase__			(  self  )      ->				List[str]:
									super().test_inference_batch_consistent(batch_sizes=[1, 2]  )





						def 	UpperCamelCase__			(  self  )      ->				List[str]:
									super().test_inference_batch_single_identical(batch_size=2       ,     expected_max_diff=3e-3  )





						def 	UpperCamelCase__			(  self  )      ->				List[str]:
									a_		:		int				    =				"""cpu"""  # ensure determinism for the device-dependent torch.Generator
									a_		:		Tuple				    =				self.get_dummy_components()
									a_		:		List[str]				    =				StableDiffusionParadigmsPipeline(**__SCREAMING_SNAKE_CASE  )
									a_		:		Tuple				    =				sd_pipe.to(__SCREAMING_SNAKE_CASE  )
									sd_pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )

									a_		:		Dict				    =				self.get_dummy_inputs(__SCREAMING_SNAKE_CASE  )
									a_		:		Any				    =				"""french fries"""
									a_		:		Dict				    =				sd_pipe(**__SCREAMING_SNAKE_CASE       ,     negative_prompt=__SCREAMING_SNAKE_CASE  )
									a_		:		Union[str, Any]				    =				output.images
									a_		:		Tuple				    =				image[0, -3:, -3:, -1]

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

									a_		:		Optional[int]				    =				np.array([0.4_7_7_1, 0.5_4_2_0, 0.4_6_8_3, 0.4_9_1_8, 0.5_6_3_6, 0.4_7_2_5, 0.5_2_3_0, 0.4_9_2_3, 0.5_0_1_5]  )

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







@slow
@require_torch_gpu
class    A__(unittest.TestCase    ):
						"""simple docstring"""






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





						def 	UpperCamelCase__			(  self       ,     _lowercase=0  )      ->				str:
									a_		:		List[Any]				    =				torch.Generator(device=__SCREAMING_SNAKE_CASE  ).manual_seed(__SCREAMING_SNAKE_CASE  )
									a_		:		Any				    =				{
									    """prompt""": """a photograph of an astronaut riding a horse""",
									    """generator""": generator,
									    """num_inference_steps""": 10,
									    """guidance_scale""": 7.5,
									    """output_type""": """numpy""",
									    """parallel""": 3,
									    """debug""": True,
									}
									return inputs





						def 	UpperCamelCase__			(  self  )      ->				Union[str, Any]:
									a_		:		Optional[int]				    =				"""stabilityai/stable-diffusion-2-base"""
									a_		:		Tuple				    =				DDIMParallelScheduler.from_pretrained(__SCREAMING_SNAKE_CASE       ,     subfolder="""scheduler"""  )
									a_		:		Optional[int]				    =				StableDiffusionParadigmsPipeline.from_pretrained(__SCREAMING_SNAKE_CASE       ,     scheduler=__SCREAMING_SNAKE_CASE       ,     safety_checker=__SCREAMING_SNAKE_CASE  )
									pipe.to(__SCREAMING_SNAKE_CASE  )
									pipe.set_progress_bar_config(disable=__SCREAMING_SNAKE_CASE  )
									pipe.enable_attention_slicing()

									a_		:		Dict				    =				self.get_inputs()
									a_		:		Optional[int]				    =				pipe(**__SCREAMING_SNAKE_CASE  ).images
									a_		:		Tuple				    =				image[0, -3:, -3:, -1].flatten()

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

									a_		:		int				    =				np.array([0.9_6_2_2, 0.9_6_0_2, 0.9_7_4_8, 0.9_5_9_1, 0.9_6_3_0, 0.9_6_9_1, 0.9_6_6_1, 0.9_6_3_1, 0.9_7_4_1]  )

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





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


__snake_case							:     Optional[Any] 						=	logging.get_logger(__name__)

__snake_case							:     str 						=	{
    """ZinengTang/tvlt-base""": """https://huggingface.co/ZinengTang/tvlt-base/blob/main/config.json""",
}



class    A__(a_    ):
						"""simple docstring"""






						_A  :							Any   			=	'''tvlt'''





						def __init__(  self       ,     _lowercase=224       ,     _lowercase=2_048       ,     _lowercase=128       ,     _lowercase=[16, 16]       ,     _lowercase=[16, 16]       ,     _lowercase=3       ,     _lowercase=1       ,     _lowercase=8       ,     _lowercase=768       ,     _lowercase=12       ,     _lowercase=12       ,     _lowercase=3_072       ,     _lowercase="gelu"       ,     _lowercase=0.0       ,     _lowercase=0.0       ,     _lowercase=0.0_2       ,     _lowercase=1e-6       ,     _lowercase=True       ,     _lowercase=False       ,     _lowercase=16       ,     _lowercase=512       ,     _lowercase=8       ,     _lowercase=2_048       ,     _lowercase=0.7_5       ,     _lowercase=0.1_5       ,     _lowercase="frame-level"       ,     _lowercase=True       ,     _lowercase=True       ,     _lowercase="classification"       ,     **_lowercase       ,     )      ->				Tuple:
									super().__init__(**_lowercase  )

									if audio_mask_type not in ("frame-level", "patch_level"):
												raise ValueError(
												    """audio_mask_type must be one of two acceptable strategies - {'frame_level', 'patch-level') """
												    F'''got {audio_mask_type}'''  )

									a_		:		int				    =				image_size
									a_		:		str				    =				spectrogram_length
									a_		:		int				    =				frequency_length
									a_		:		str				    =				image_patch_size
									a_		:		int				    =				audio_patch_size
									a_		:		Any				    =				num_image_channels
									a_		:		str				    =				num_audio_channels
									a_		:		int				    =				num_frames

									a_		:		Optional[int]				    =				hidden_size
									a_		:		Tuple				    =				num_hidden_layers
									a_		:		Any				    =				num_attention_heads
									a_		:		Union[str, Any]				    =				intermediate_size
									a_		:		Tuple				    =				hidden_act
									a_		:		Union[str, Any]				    =				hidden_dropout_prob
									a_		:		int				    =				attention_probs_dropout_prob
									a_		:		Any				    =				initializer_range
									a_		:		int				    =				layer_norm_eps
									a_		:		Union[str, Any]				    =				qkv_bias
									a_		:		str				    =				use_mean_pooling

									a_		:		List[Any]				    =				decoder_num_attention_heads
									a_		:		Optional[Any]				    =				decoder_hidden_size
									a_		:		Dict				    =				decoder_num_hidden_layers
									a_		:		Optional[Any]				    =				decoder_intermediate_size
									a_		:		Dict				    =				pixel_mask_ratio
									a_		:		int				    =				audio_mask_ratio
									a_		:		Optional[Any]				    =				audio_mask_type

									a_		:		Union[str, Any]				    =				task_matching
									a_		:		Dict				    =				task_mae
									a_		:		Union[str, Any]				    =				loss_type


