'''simple docstring'''






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    a (       __lowerCAmelCase						,  unittest.TestCase				):
  __lowerCAmelCase				      =					TextToVideoSDPipeline
  __lowerCAmelCase				      =					TEXT_TO_IMAGE_PARAMS
  __lowerCAmelCase				      =					TEXT_TO_IMAGE_BATCH_PARAMS
  # No `output_type`.
  __lowerCAmelCase				      =					frozenset(
      [
          """num_inference_steps""",
          """generator""",
          """latents""",
          """return_dict""",
          """callback""",
          """callback_steps""",
      ]				)







  def    lowercase_  (							self					):

    '''simple docstring'''







    torch.manual_seed(0					)
    __UpperCAmelCase:					Tuple		= 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:					Union[str, Any]		= 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:					List[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					)
    __UpperCAmelCase:					Optional[Any]		= CLIPTextConfig(
        bos_token_id=0   ,    eos_token_id=2   ,    hidden_size=32   ,    intermediate_size=37   ,    layer_norm_eps=1e-0_5   ,    num_attention_heads=4   ,    num_hidden_layers=5   ,    pad_token_id=1   ,    vocab_size=1000   ,    hidden_act="""gelu"""   ,    projection_dim=512   ,    )
    __UpperCAmelCase:					str		= CLIPTextModel(snake_case_					)
    __UpperCAmelCase:					int		= CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

    __UpperCAmelCase:					Optional[int]		= {
        """unet""": unet,
        """scheduler""": scheduler,
        """vae""": vae,
        """text_encoder""": text_encoder,
        """tokenizer""": tokenizer,
    }
    return components







  def    lowercase_  (							self   ,    snake_case_   ,    snake_case_=0					):

    '''simple docstring'''







    if str(snake_case_					).startswith("""mps"""					):
      __UpperCAmelCase:					Any		= torch.manual_seed(snake_case_					)
    else:
      __UpperCAmelCase:					Optional[int]		= torch.Generator(device=snake_case_					).manual_seed(snake_case_					)
    __UpperCAmelCase:					List[str]		= {
        """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    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					List[Any]		= """cpu"""  # ensure determinism for the device-dependent torch.Generator
    __UpperCAmelCase:					Tuple		= self.get_dummy_components()
    __UpperCAmelCase:					str		= TextToVideoSDPipeline(**snake_case_					)
    __UpperCAmelCase:					Optional[Any]		= sd_pipe.to(snake_case_					)
    sd_pipe.set_progress_bar_config(disable=snake_case_					)

    __UpperCAmelCase:					Union[str, Any]		= self.get_dummy_inputs(snake_case_					)
    __UpperCAmelCase:					List[str]		= """np"""
    __UpperCAmelCase:					str		= sd_pipe(**snake_case_					).frames
    __UpperCAmelCase:					List[Any]		= frames[0][-3:, -3:, -1]

    assert frames[0].shape == (64, 64, 3)
    __UpperCAmelCase:					Optional[int]		= np.array([158.0, 160.0, 153.0, 125.0, 100.0, 121.0, 111.0, 93.0, 113.0]					)

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







  def    lowercase_  (							self					):

    '''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    lowercase_  (							self					):

    '''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    lowercase_  (							self					):

    '''simple docstring'''







    pass







  @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline."""					)
  def    lowercase_  (							self					):

    '''simple docstring'''







    pass







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

    '''simple docstring'''







    pass







  def    lowercase_  (							self					):

    '''simple docstring'''







    return super().test_progress_bar()




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







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Optional[int]		= load_numpy(
        """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video.npy"""					)

    __UpperCAmelCase:					Any		= TextToVideoSDPipeline.from_pretrained("""damo-vilab/text-to-video-ms-1.7b"""					)
    __UpperCAmelCase:					Union[str, Any]		= DPMSolverMultistepScheduler.from_config(pipe.scheduler.config					)
    __UpperCAmelCase:					List[str]		= pipe.to("""cuda"""					)

    __UpperCAmelCase:					Optional[int]		= """Spiderman is surfing"""
    __UpperCAmelCase:					List[Any]		= torch.Generator(device="""cpu"""					).manual_seed(0					)

    __UpperCAmelCase:					List[str]		= pipe(snake_case_   ,    generator=snake_case_   ,    num_inference_steps=25   ,    output_type="""pt"""					).frames
    __UpperCAmelCase:					List[str]		= video_frames.cpu().numpy()

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







  def    lowercase_  (							self					):

    '''simple docstring'''







    __UpperCAmelCase:					Union[str, Any]		= load_numpy(
        """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text_to_video/video_2step.npy"""					)

    __UpperCAmelCase:					Optional[Any]		= TextToVideoSDPipeline.from_pretrained("""damo-vilab/text-to-video-ms-1.7b"""					)
    __UpperCAmelCase:					Union[str, Any]		= pipe.to("""cuda"""					)

    __UpperCAmelCase:					List[Any]		= """Spiderman is surfing"""
    __UpperCAmelCase:					Union[str, Any]		= torch.Generator(device="""cpu"""					).manual_seed(0					)

    __UpperCAmelCase:					Dict		= pipe(snake_case_   ,    generator=snake_case_   ,    num_inference_steps=2   ,    output_type="""pt"""					).frames
    __UpperCAmelCase:					str		= video_frames.cpu().numpy()

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






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


SCREAMING_SNAKE_CASE_	   =			logging.get_logger(__name__)

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




class    a (       __lowerCAmelCase				):



  """simple docstring"""
  __lowerCAmelCase				      =					"""timesformer"""







  def __init__(							self   ,    snake_case_=224   ,    snake_case_=16   ,    snake_case_=3   ,    snake_case_=8   ,    snake_case_=768   ,    snake_case_=12   ,    snake_case_=12   ,    snake_case_=3072   ,    snake_case_="gelu"   ,    snake_case_=0.0   ,    snake_case_=0.0   ,    snake_case_=0.0_2   ,    snake_case_=1e-6   ,    snake_case_=True   ,    snake_case_="divided_space_time"   ,    snake_case_=0   ,    **snake_case_   ,    ):

    '''simple docstring'''







    super().__init__(**snake_case_					)

    __UpperCAmelCase:					Tuple		= image_size
    __UpperCAmelCase:					List[Any]		= patch_size
    __UpperCAmelCase:					Optional[Any]		= num_channels
    __UpperCAmelCase:					int		= num_frames

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

    __UpperCAmelCase:					str		= attention_type
    __UpperCAmelCase:					Tuple		= drop_path_rate