




import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    TextToVideoSDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import is_xformers_available, load_numpy, skip_mps, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

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


enable_full_determinism()







@skip_mps
class __snake_case			(			a ,  unittest.TestCase		):
   UpperCAmelCase__  :    int          =  TextToVideoSDPipeline
   UpperCAmelCase__  :    List[Any]          =  TEXT_TO_IMAGE_PARAMS
   UpperCAmelCase__  :    int          =  TEXT_TO_IMAGE_BATCH_PARAMS
   # No `output_type`.
   UpperCAmelCase__  :    Tuple          =  frozenset(
       [
           '''num_inference_steps''',
           '''generator''',
           '''latents''',
           '''return_dict''',
           '''callback''',
           '''callback_steps''',
       ]		)







   def lowerCamelCase						(      self		:						str):

        """simple docstring"""


        torch.manual_seed(0)
        UpperCAmelCase_          =							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					,						)
        UpperCAmelCase_          =							DDIMScheduler(
            beta_start=0.0_0_0_8_5					,						beta_end=0.0_1_2					,						beta_schedule='''scaled_linear'''					,						clip_sample=_snake_case					,						set_alpha_to_one=_snake_case					,						)
        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					,						sample_size=128					,						)
        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					,						hidden_act='''gelu'''					,						projection_dim=512					,						)
        UpperCAmelCase_          =							CLIPTextModel(_snake_case)
        UpperCAmelCase_          =							CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

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







   def lowerCamelCase						(      self		:						Union[str, Any]					,						_snake_case		:						Any					,						_snake_case		:						List[str]=0):

        """simple docstring"""


        if str(_snake_case).startswith('''mps'''):
             UpperCAmelCase_          =							torch.manual_seed(_snake_case)
        else:
             UpperCAmelCase_          =							torch.Generator(device=_snake_case).manual_seed(_snake_case)
        UpperCAmelCase_          =							{
            '''prompt''': '''A painting of a squirrel eating a burger''',
            '''generator''': generator,
            '''num_inference_steps''': 2,
            '''guidance_scale''': 6.0,
            '''output_type''': '''pt''',
        }
        return inputs







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        UpperCAmelCase_          =							'''cpu'''  # ensure determinism for the device-dependent torch.Generator
        UpperCAmelCase_          =							self.get_dummy_components()
        UpperCAmelCase_          =							TextToVideoSDPipeline(**_snake_case)
        UpperCAmelCase_          =							sd_pipe.to(_snake_case)
        sd_pipe.set_progress_bar_config(disable=_snake_case)

        UpperCAmelCase_          =							self.get_dummy_inputs(_snake_case)
        UpperCAmelCase_          =							'''np'''
        UpperCAmelCase_          =							sd_pipe(**_snake_case).frames
        UpperCAmelCase_          =							frames[0][-3:, -3:, -1]

        assert frames[0].shape == (64, 64, 3)
        UpperCAmelCase_          =							np.array([1_5_8.0, 1_6_0.0, 1_5_3.0, 1_2_5.0, 1_0_0.0, 1_2_1.0, 1_1_1.0, 9_3.0, 1_1_3.0])

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







   def lowerCamelCase						(      self		:						Any):

        """simple docstring"""


        self._test_attention_slicing_forward_pass(test_mean_pixel_difference=_snake_case					,						expected_max_diff=3e-3)







   @unittest.skipIf(
       torch_device != '''cuda''' or not is_xformers_available()					,						reason='''XFormers attention is only available with CUDA and `xformers` installed'''					,						)
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=_snake_case					,						expected_max_diff=1e-2)







   @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''')
   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        pass







   @unittest.skip(reason='''Batching needs to be properly figured out first for this pipeline.''')
   def lowerCamelCase						(      self		:						List[Any]):

        """simple docstring"""


        pass







   @unittest.skip(reason='''`num_images_per_prompt` argument is not supported for this pipeline.''')
   def lowerCamelCase						(      self		:						Dict):

        """simple docstring"""


        pass







   def lowerCamelCase						(      self		:						Tuple):

        """simple docstring"""


        return super().test_progress_bar()







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







   def lowerCamelCase						(      self		:						Optional[Any]):

        """simple docstring"""


        UpperCAmelCase_          =							load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy''')

        UpperCAmelCase_          =							TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        UpperCAmelCase_          =							DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
        UpperCAmelCase_          =							pipe.to('''cuda''')

        UpperCAmelCase_          =							'''Spiderman is surfing'''
        UpperCAmelCase_          =							torch.Generator(device='''cpu''').manual_seed(0)

        UpperCAmelCase_          =							pipe(_snake_case					,						generator=_snake_case					,						num_inference_steps=25					,						output_type='''pt''').frames
        UpperCAmelCase_          =							video_frames.cpu().numpy()

        assert np.abs(expected_video - video).mean() < 5e-2







   def lowerCamelCase						(      self		:						List[str]):

        """simple docstring"""


        UpperCAmelCase_          =							load_numpy(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy''')

        UpperCAmelCase_          =							TextToVideoSDPipeline.from_pretrained('''damo-vilab/text-to-video-ms-1.7b''')
        UpperCAmelCase_          =							pipe.to('''cuda''')

        UpperCAmelCase_          =							'''Spiderman is surfing'''
        UpperCAmelCase_          =							torch.Generator(device='''cpu''').manual_seed(0)

        UpperCAmelCase_          =							pipe(_snake_case					,						generator=_snake_case					,						num_inference_steps=2					,						output_type='''pt''').frames
        UpperCAmelCase_          =							video_frames.cpu().numpy()

        assert np.abs(expected_video - video).mean() < 5e-2












snake_case_ : Dict    		=       {
    "Pillow": "Pillow",
    "accelerate": "accelerate>=0.11.0",
    "compel": "compel==0.1.8",
    "black": "black~=23.1",
    "datasets": "datasets",
    "filelock": "filelock",
    "flax": "flax>=0.4.1",
    "hf-doc-builder": "hf-doc-builder>=0.3.0",
    "huggingface-hub": "huggingface-hub>=0.13.2",
    "requests-mock": "requests-mock==1.10.0",
    "importlib_metadata": "importlib_metadata",
    "invisible-watermark": "invisible-watermark",
    "isort": "isort>=5.5.4",
    "jax": "jax>=0.2.8,!=0.3.2",
    "jaxlib": "jaxlib>=0.1.65",
    "Jinja2": "Jinja2",
    "k-diffusion": "k-diffusion>=0.0.12",
    "torchsde": "torchsde",
    "note_seq": "note_seq",
    "librosa": "librosa",
    "numpy": "numpy",
    "omegaconf": "omegaconf",
    "parameterized": "parameterized",
    "protobuf": "protobuf>=3.20.3,<4",
    "pytest": "pytest",
    "pytest-timeout": "pytest-timeout",
    "pytest-xdist": "pytest-xdist",
    "ruff": "ruff>=0.0.241",
    "safetensors": "safetensors",
    "sentencepiece": "sentencepiece>=0.1.91,!=0.1.92",
    "scipy": "scipy",
    "onnx": "onnx",
    "regex": "regex!=2019.12.17",
    "requests": "requests",
    "tensorboard": "tensorboard",
    "torch": "torch>=1.4",
    "torchvision": "torchvision",
    "transformers": "transformers>=4.25.1",
    "urllib3": "urllib3<=2.0.0",
}






