
'''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 						lowercase__	(  snake_case_,					unittest.TestCase   ):






   '''simple docstring'''







   _snake_case		   =							TextToVideoSDPipeline
   _snake_case		   =							TEXT_TO_IMAGE_PARAMS
   _snake_case		   =							TEXT_TO_IMAGE_BATCH_PARAMS
   # No `output_type`.
   _snake_case		   =							frozenset(
       [
           '''num_inference_steps''',
           '''generator''',
           '''latents''',
           '''return_dict''',
           '''callback''',
           '''callback_steps''',
       ]   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      torch.manual_seed(0   )
      UpperCamelCase       =  UNetaDConditionModel(
          block_out_channels=(3_2, 6_4, 6_4, 6_4) ,   layers_per_block=2 ,   sample_size=3_2 ,   in_channels=4 ,   out_channels=4 ,   down_block_types=('''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''CrossAttnDownBlock3D''', '''DownBlock3D''') ,   up_block_types=('''UpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''', '''CrossAttnUpBlock3D''') ,   cross_attention_dim=3_2 ,   attention_head_dim=4 ,   )
      UpperCamelCase       =  DDIMScheduler(
          beta_start=0.00085 ,   beta_end=0.012 ,   beta_schedule='''scaled_linear''' ,   clip_sample=lowerCamelCase__ ,   set_alpha_to_one=lowerCamelCase__ ,   )
      torch.manual_seed(0   )
      UpperCamelCase       =  AutoencoderKL(
          block_out_channels=[3_2, 6_4] ,   in_channels=3 ,   out_channels=3 ,   down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D'''] ,   up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D'''] ,   latent_channels=4 ,   sample_size=1_2_8 ,   )
      torch.manual_seed(0   )
      UpperCamelCase       =  CLIPTextConfig(
          bos_token_id=0 ,   eos_token_id=2 ,   hidden_size=3_2 ,   intermediate_size=3_7 ,   layer_norm_eps=1e-0_5 ,   num_attention_heads=4 ,   num_hidden_layers=5 ,   pad_token_id=1 ,   vocab_size=1_0_0_0 ,   hidden_act='''gelu''' ,   projection_dim=5_1_2 ,   )
      UpperCamelCase       =  CLIPTextModel(lowerCamelCase__   )
      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       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__=0   ):

      '''simple docstring'''







      if str(lowerCamelCase__   ).startswith('''mps'''   ):
         UpperCamelCase       =  torch.manual_seed(lowerCamelCase__   )
      else:
         UpperCamelCase       =  torch.Generator(device=lowerCamelCase__   ).manual_seed(lowerCamelCase__   )
      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       UpperCAmelCase				(			self   ):

      '''simple docstring'''







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

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

      assert frames[0].shape == (6_4, 6_4, 3)
      UpperCamelCase       =  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       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      self._test_attention_slicing_forward_pass(test_mean_pixel_difference=lowerCamelCase__ ,   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       UpperCAmelCase				(			self   ):

      '''simple docstring'''







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







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

      '''simple docstring'''







      pass







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

      '''simple docstring'''







      pass







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

      '''simple docstring'''







      pass







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      return super().test_progress_bar()




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






   '''simple docstring'''







   def       UpperCAmelCase				(			self   ):

      '''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(lowerCamelCase__ ,   generator=lowerCamelCase__ ,   num_inference_steps=2_5 ,   output_type='''pt'''   ).frames
      UpperCamelCase       =  video_frames.cpu().numpy()

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







   def       UpperCAmelCase				(			self   ):

      '''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(lowerCamelCase__ ,   generator=lowerCamelCase__ ,   num_inference_steps=2 ,   output_type='''pt'''   ).frames
      UpperCamelCase       =  video_frames.cpu().numpy()

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







'''simple docstring'''


import os
from typing import List, Optional, Union

from ...image_processing_utils import BatchFeature
from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType
from ..auto import AutoTokenizer


class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   _snake_case		   =							['''image_processor''', '''tokenizer''']
   _snake_case		   =							'''BlipImageProcessor'''
   _snake_case		   =							'''AutoTokenizer'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__ ,   lowerCamelCase__   ):

      '''simple docstring'''







      super().__init__(lowerCamelCase__ ,   lowerCamelCase__   )

      # add QFormer tokenizer
      UpperCamelCase       =  qformer_tokenizer







   def __call__(			self ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = True ,   lowerCamelCase__ = False ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = 0 ,   lowerCamelCase__ = None ,   lowerCamelCase__ = None ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = False ,   lowerCamelCase__ = True ,   lowerCamelCase__ = None ,   **lowerCamelCase__ ,   ):

      '''simple docstring'''







      if images is None and text is None:
         raise ValueError('''You have to specify at least images or text.'''   )

      UpperCamelCase       =  BatchFeature()

      if text is not None:
         UpperCamelCase       =  self.tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         encoding.update(lowerCamelCase__   )
         UpperCamelCase       =  self.qformer_tokenizer(
             text=lowerCamelCase__ ,   add_special_tokens=lowerCamelCase__ ,   padding=lowerCamelCase__ ,   truncation=lowerCamelCase__ ,   max_length=lowerCamelCase__ ,   stride=lowerCamelCase__ ,   pad_to_multiple_of=lowerCamelCase__ ,   return_attention_mask=lowerCamelCase__ ,   return_overflowing_tokens=lowerCamelCase__ ,   return_special_tokens_mask=lowerCamelCase__ ,   return_offsets_mapping=lowerCamelCase__ ,   return_token_type_ids=lowerCamelCase__ ,   return_length=lowerCamelCase__ ,   verbose=lowerCamelCase__ ,   return_tensors=lowerCamelCase__ ,   **lowerCamelCase__ ,   )
         UpperCamelCase       =  qformer_text_encoding.pop('''input_ids'''   )
         UpperCamelCase       =  qformer_text_encoding.pop('''attention_mask'''   )

      if images is not None:
         UpperCamelCase       =  self.image_processor(lowerCamelCase__ ,   return_tensors=lowerCamelCase__   )
         encoding.update(lowerCamelCase__   )

      return encoding







   def       UpperCAmelCase				(			self ,   *lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      return self.tokenizer.batch_decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   *lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      return self.tokenizer.decode(*lowerCamelCase__ ,   **lowerCamelCase__   )







   @property
   # Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.tokenizer.model_input_names
      UpperCamelCase       =  self.image_processor.model_input_names
      return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names   )   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      if os.path.isfile(lowerCamelCase__   ):
         raise ValueError(f'Provided path ({save_directory}) should be a directory, not a file'   )
      os.makedirs(lowerCamelCase__ ,   exist_ok=lowerCamelCase__   )
      UpperCamelCase       =  os.path.join(lowerCamelCase__ ,   '''qformer_tokenizer'''   )
      self.qformer_tokenizer.save_pretrained(lowerCamelCase__   )
      return super().save_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )







   @classmethod
   def       UpperCAmelCase				(			cls ,   lowerCamelCase__ ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  AutoTokenizer.from_pretrained(lowerCamelCase__ ,   subfolder='''qformer_tokenizer'''   )
      UpperCamelCase       =  cls._get_arguments_from_pretrained(lowerCamelCase__ ,   **lowerCamelCase__   )
      args.append(lowerCamelCase__   )
      return cls(*lowerCamelCase__   )





