'''simple docstring'''





import random
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    UNetaDConditionModel,
    VideoToVideoSDPipeline,
)
from diffusers.utils import floats_tensor, is_xformers_available, skip_mps
from diffusers.utils.testing_utils import enable_full_determinism, slow, torch_device

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()






@skip_mps
class 					UpperCAmelCase			(							snake_case_ ,     unittest.TestCase			):




    '''simple docstring'''







    snake_case__				: Tuple						    =    VideoToVideoSDPipeline
    snake_case__				: Dict						    =    TEXT_GUIDED_IMAGE_VARIATION_PARAMS.union({"video"}			) - {"image", "width", "height"}
    snake_case__				: List[str]						    =    TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS.union({"video"}			) - {"image"}
    snake_case__				: List[Any]						    =    PipelineTesterMixin.required_optional_params - {"latents"}
    snake_case__				: int						    =    False

    # No `output_type`.
    snake_case__				: str						    =    frozenset(
        [
            "num_inference_steps",
            "generator",
            "latents",
            "return_dict",
            "callback",
            "callback_steps",
        ]			)






    def    _UpperCamelCase     (							self			)				->			Tuple:
         torch.manual_seed(0			)
         SCREAMING_SNAKE_CASE       :      Any     						=							UNetaDConditionModel(
             block_out_channels=(32, 64, 64, 64)     ,				layers_per_block=2     ,				sample_size=32     ,				in_channels=4     ,				out_channels=4     ,				down_block_types=('CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'CrossAttnDownBlock3D', 'DownBlock3D')     ,				up_block_types=('UpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D', 'CrossAttnUpBlock3D')     ,				cross_attention_dim=32     ,				attention_head_dim=4     ,				)
         SCREAMING_SNAKE_CASE       :      Optional[int]     						=							DDIMScheduler(
             beta_start=0.0_0_0_8_5     ,				beta_end=0.0_1_2     ,				beta_schedule='scaled_linear'     ,				clip_sample=lowercase__     ,				set_alpha_to_one=lowercase__     ,				)
         torch.manual_seed(0			)
         SCREAMING_SNAKE_CASE       :      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			)
         SCREAMING_SNAKE_CASE       :      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     ,				)
         SCREAMING_SNAKE_CASE       :      Optional[Any]     						=							CLIPTextModel(lowercase__			)
         SCREAMING_SNAKE_CASE       :      List[str]     						=							CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'			)

         SCREAMING_SNAKE_CASE       :      Tuple     						=							{
             'unet': unet,
             'scheduler': scheduler,
             'vae': vae,
             'text_encoder': text_encoder,
             'tokenizer': tokenizer,
         }
         return components






    def    _UpperCamelCase     (							self     ,				lowercase__     ,				lowercase__=0			)				->			Optional[Any]:
         # 3 frames
         SCREAMING_SNAKE_CASE       :      Union[str, Any]     						=							floats_tensor((1, 3, 3, 32, 32)     ,				rng=random.Random(lowercase__			)			).to(lowercase__			)

         if str(lowercase__			).startswith('mps'			):
              SCREAMING_SNAKE_CASE       :      List[Any]     						=							torch.manual_seed(lowercase__			)
         else:
              SCREAMING_SNAKE_CASE       :      Tuple     						=							torch.Generator(device=lowercase__			).manual_seed(lowercase__			)
         SCREAMING_SNAKE_CASE       :      Any     						=							{
             'prompt': 'A painting of a squirrel eating a burger',
             'video': video,
             'generator': generator,
             'num_inference_steps': 2,
             'guidance_scale': 6.0,
             'output_type': 'pt',
         }
         return inputs






    def    _UpperCamelCase     (							self			)				->			Optional[Any]:
         SCREAMING_SNAKE_CASE       :      Optional[Any]     						=							'cpu'  # ensure determinism for the device-dependent torch.Generator
         SCREAMING_SNAKE_CASE       :      List[str]     						=							self.get_dummy_components()
         SCREAMING_SNAKE_CASE       :      Dict     						=							VideoToVideoSDPipeline(**lowercase__			)
         SCREAMING_SNAKE_CASE       :      List[str]     						=							sd_pipe.to(lowercase__			)
         sd_pipe.set_progress_bar_config(disable=lowercase__			)

         SCREAMING_SNAKE_CASE       :      Optional[Any]     						=							self.get_dummy_inputs(lowercase__			)
         SCREAMING_SNAKE_CASE       :      Union[str, Any]     						=							'np'
         SCREAMING_SNAKE_CASE       :      Dict     						=							sd_pipe(**lowercase__			).frames
         SCREAMING_SNAKE_CASE       :      Tuple     						=							frames[0][-3:, -3:, -1]

         assert frames[0].shape == (32, 32, 3)
         SCREAMING_SNAKE_CASE       :      Union[str, Any]     						=							np.array([106, 117, 113, 174, 137, 112, 148, 151, 131]			)

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






    @unittest.skipIf(
        torch_device != 'cuda' or not is_xformers_available()     ,				reason='XFormers attention is only available with CUDA and `xformers` installed'     ,				)
    def    _UpperCamelCase     (							self			)				->			Any:
         self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowercase__     ,				expected_max_diff=5E-3			)






    @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'			)
    def    _UpperCamelCase     (							self			)				->			Any:
         pass






    @unittest.skip(reason='Batching needs to be properly figured out first for this pipeline.'			)
    def    _UpperCamelCase     (							self			)				->			Optional[Any]:
         pass






    @unittest.skip(reason='`num_images_per_prompt` argument is not supported for this pipeline.'			)
    def    _UpperCamelCase     (							self			)				->			Union[str, Any]:
         pass






    def    _UpperCamelCase     (							self			)				->			Union[str, Any]:
         return super().test_progress_bar()






@slow
@skip_mps
class 					UpperCAmelCase			(							unittest.TestCase			):




    '''simple docstring'''







    def    _UpperCamelCase     (							self			)				->			Dict:
         SCREAMING_SNAKE_CASE       :      Optional[int]     						=							VideoToVideoSDPipeline.from_pretrained('cerspense/zeroscope_v2_XL'     ,				torch_dtype=torch.floataa			)
         pipe.enable_model_cpu_offload()

         # 10 frames
         SCREAMING_SNAKE_CASE       :      int     						=							torch.Generator(device='cpu'			).manual_seed(0			)
         SCREAMING_SNAKE_CASE       :      str     						=							torch.randn((1, 10, 3, 1_024, 576)     ,				generator=lowercase__			)
         SCREAMING_SNAKE_CASE       :      Optional[int]     						=							video.to('cuda'			)

         SCREAMING_SNAKE_CASE       :      List[str]     						=							'Spiderman is surfing'

         SCREAMING_SNAKE_CASE       :      List[str]     						=							pipe(lowercase__     ,				video=lowercase__     ,				generator=lowercase__     ,				num_inference_steps=3     ,				output_type='pt'			).frames

         SCREAMING_SNAKE_CASE       :      Any     						=							np.array([-1.0_4_5_8_9_8_4, -1.1_2_7_9_2_9_7, -0.9_6_6_3_0_8_6, -0.9_1_5_0_3_9_0_6, -0.7_5_0_9_7_6_5_6]			)
         assert np.abs(video_frames.cpu().numpy()[0, 0, 0, 0, -5:] - expected_array			).sum() < 1E-2











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


lowercase    				=   logging.get_logger(__name__)

lowercase    				=   {
    "transfo-xl-wt103": "https://huggingface.co/transfo-xl-wt103/resolve/main/config.json",
}




class 					UpperCamelCase_ (     snake_case_							):



	'''simple docstring'''




	lowerCAmelCase						=					'''transfo-xl'''
	lowerCAmelCase						=					['''mems''']
	lowerCAmelCase						=					{
	    '''n_token''': '''vocab_size''',
	    '''hidden_size''': '''d_model''',
	    '''num_attention_heads''': '''n_head''',
	    '''num_hidden_layers''': '''n_layer''',
	}




	def __init__(			self			,     a=26_77_35			,     a=[2_00_00, 4_00_00, 20_00_00]			,     a=10_24			,     a=10_24			,     a=16			,     a=64			,     a=40_96			,     a=4			,     a=False			,     a=18			,     a=16_00			,     a=10_00			,     a=True			,     a=True			,     a=0			,     a=-1			,     a=True			,     a=0.1			,     a=0.0			,     a=True			,     a="normal"			,     a=0.01			,     a=0.01			,     a=0.02			,     a=1E-5			,     a=0			,     **a			,     )			->       Union[str, Any]:
								snake_case_           =				vocab_size
								snake_case_           =				[]
								self.cutoffs.extend(a   )
								if proj_share_all_but_first:
															snake_case_           =				[False] + [True] * len(self.cutoffs   )
								else:
															snake_case_           =				[False] + [False] * len(self.cutoffs   )
								snake_case_           =				d_model
								snake_case_           =				d_embed
								snake_case_           =				d_head
								snake_case_           =				d_inner
								snake_case_           =				div_val
								snake_case_           =				pre_lnorm
								snake_case_           =				n_layer
								snake_case_           =				n_head
								snake_case_           =				mem_len
								snake_case_           =				same_length
								snake_case_           =				attn_type
								snake_case_           =				clamp_len
								snake_case_           =				sample_softmax
								snake_case_           =				adaptive
								snake_case_           =				dropout
								snake_case_           =				dropatt
								snake_case_           =				untie_r
								snake_case_           =				init
								snake_case_           =				init_range
								snake_case_           =				proj_init_std
								snake_case_           =				init_std
								snake_case_           =				layer_norm_epsilon
								super().__init__(eos_token_id=a			,     **a   )




	@property
	def 				_UpperCamelCase     (			self   )			->       int:
								# Message copied from Transformer-XL documentation
								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 				_UpperCamelCase     (			self			,     a   )			->       List[str]:
								# 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.'''   )





