





import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionTextToImagePipeline
from diffusers.utils.testing_utils import nightly, require_torch_gpu, torch_device


__lowerCAmelCase :Dict     =    False






class        _a(    unittest.TestCase    ):
		pass






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







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


							'''simple docstring'''

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







		def        lowercase       (    self				)      ->      List[Any]:


							'''simple docstring'''

							_snake_case       :					Union[str, Any]				       =   VersatileDiffusionTextToImagePipeline.from_pretrained("shi-labs/versatile-diffusion"				)
							# remove text_unet
							pipe.remove_unused_weights()
							pipe.to(__snake_case				)
							pipe.set_progress_bar_config(disable=__snake_case				)

							_snake_case       :					Optional[Any]				       =   "A painting of a squirrel eating a burger "
							_snake_case       :					Tuple				       =   torch.manual_seed(0				)
							_snake_case       :					Optional[int]				       =   pipe(
							    prompt=__snake_case ,						generator=__snake_case ,						guidance_scale=7.5 ,						num_inference_steps=2 ,						output_type="numpy"				).images

							with tempfile.TemporaryDirectory() as tmpdirname:
												pipe.save_pretrained(__snake_case				)
												_snake_case       :					int				       =   VersatileDiffusionTextToImagePipeline.from_pretrained(__snake_case				)
							pipe.to(__snake_case				)
							pipe.set_progress_bar_config(disable=__snake_case				)

							_snake_case       :					Optional[int]				       =   generator.manual_seed(0				)
							_snake_case       :					Dict				       =   pipe(
							    prompt=__snake_case ,						generator=__snake_case ,						guidance_scale=7.5 ,						num_inference_steps=2 ,						output_type="numpy"				).images

							assert np.abs(image - new_image				).sum() < 1E-5, "Models don't have the same forward pass"







		def        lowercase       (    self				)      ->      List[Any]:


							'''simple docstring'''

							_snake_case       :					List[str]				       =   VersatileDiffusionTextToImagePipeline.from_pretrained(
							    "shi-labs/versatile-diffusion" ,						torch_dtype=torch.floataa				)
							pipe.to(__snake_case				)
							pipe.set_progress_bar_config(disable=__snake_case				)

							_snake_case       :					Optional[int]				       =   "A painting of a squirrel eating a burger "
							_snake_case       :					List[str]				       =   torch.manual_seed(0				)
							_snake_case       :					Dict				       =   pipe(
							    prompt=__snake_case ,						generator=__snake_case ,						guidance_scale=7.5 ,						num_inference_steps=5_0 ,						output_type="numpy"				).images

							_snake_case       :					str				       =   image[0, 2_5_3:2_5_6, 2_5_3:2_5_6, -1]

							assert image.shape == (1, 5_1_2, 5_1_2, 3)
							_snake_case       :					Optional[int]				       =   np.array([0.33_67, 0.31_69, 0.26_56, 0.38_70, 0.47_90, 0.37_96, 0.40_09, 0.48_78, 0.47_78]				)

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






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


__lowerCAmelCase :Tuple     =    logging.get_logger(__name__)

__lowerCAmelCase :int     =    {
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}






class        _a(    __A    ):
		lowerCamelCase__     :Optional[Any]   				=		'timesformer'







		def __init__(    self ,						__snake_case=2_2_4 ,						__snake_case=1_6 ,						__snake_case=3 ,						__snake_case=8 ,						__snake_case=7_6_8 ,						__snake_case=1_2 ,						__snake_case=1_2 ,						__snake_case=3_0_7_2 ,						__snake_case="gelu" ,						__snake_case=0.0 ,						__snake_case=0.0 ,						__snake_case=0.02 ,						__snake_case=1E-6 ,						__snake_case=True ,						__snake_case="divided_space_time" ,						__snake_case=0 ,						**__snake_case ,						)      ->      str:


							'''simple docstring'''

							super().__init__(**__snake_case				)

							_snake_case       :					Optional[Any]				       =   image_size
							_snake_case       :					Optional[int]				       =   patch_size
							_snake_case       :					str				       =   num_channels
							_snake_case       :					Tuple				       =   num_frames

							_snake_case       :					Union[str, Any]				       =   hidden_size
							_snake_case       :					Optional[int]				       =   num_hidden_layers
							_snake_case       :					List[Any]				       =   num_attention_heads
							_snake_case       :					Optional[int]				       =   intermediate_size
							_snake_case       :					List[str]				       =   hidden_act
							_snake_case       :					Optional[int]				       =   hidden_dropout_prob
							_snake_case       :					Optional[Any]				       =   attention_probs_dropout_prob
							_snake_case       :					Dict				       =   initializer_range
							_snake_case       :					Optional[int]				       =   layer_norm_eps
							_snake_case       :					str				       =   qkv_bias

							_snake_case       :					List[str]				       =   attention_type
							_snake_case       :					Optional[int]				       =   drop_path_rate