
'''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'''


from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
   from ..models.auto import AutoTokenizer


class 						lowercase__	:






   '''simple docstring'''







   def       UpperCAmelCase				(			self ,   lowerCamelCase__   ):

      '''simple docstring'''







      raise NotImplementedError()







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      raise NotImplementedError()




class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = False ,   **lowerCamelCase__   ):

      '''simple docstring'''







      UpperCamelCase       =  tokenizer
      UpperCamelCase       =  skip_prompt
      UpperCamelCase       =  decode_kwargs

      # variables used in the streaming process
      UpperCamelCase       =  []
      UpperCamelCase       =  0
      UpperCamelCase       =  True







   def       UpperCAmelCase				(			self ,   lowerCamelCase__   ):

      '''simple docstring'''







      if len(value.shape   ) > 1 and value.shape[0] > 1:
         raise ValueError('''TextStreamer only supports batch size 1'''   )
      elif len(value.shape   ) > 1:
         UpperCamelCase       =  value[0]

      if self.skip_prompt and self.next_tokens_are_prompt:
         UpperCamelCase       =  False
         return

      # Add the new token to the cache and decodes the entire thing.
      self.token_cache.extend(value.tolist()   )
      UpperCamelCase       =  self.tokenizer.decode(self.token_cache ,   **self.decode_kwargs   )

      # After the symbol for a new line, we flush the cache.
      if text.endswith('''\n'''   ):
         UpperCamelCase       =  text[self.print_len :]
         UpperCamelCase       =  []
         UpperCamelCase       =  0
      # If the last token is a CJK character, we print the characters.
      elif len(lowerCamelCase__   ) > 0 and self._is_chinese_char(ord(text[-1]   )   ):
         UpperCamelCase       =  text[self.print_len :]
         self.print_len += len(lowerCamelCase__   )
      # Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
      # which may change with the subsequent token -- there are probably smarter ways to do this!)
      else:
         UpperCamelCase       =  text[self.print_len : text.rfind(''' '''   ) + 1]
         self.print_len += len(lowerCamelCase__   )

      self.on_finalized_text(lowerCamelCase__   )







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      if len(self.token_cache   ) > 0:
         UpperCamelCase       =  self.tokenizer.decode(self.token_cache ,   **self.decode_kwargs   )
         UpperCamelCase       =  text[self.print_len :]
         UpperCamelCase       =  []
         UpperCamelCase       =  0
      else:
         UpperCamelCase       =  ''''''

      UpperCamelCase       =  True
      self.on_finalized_text(lowerCamelCase__ ,   stream_end=lowerCamelCase__   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = False   ):

      '''simple docstring'''







      print(lowerCamelCase__ ,   flush=lowerCamelCase__ ,   end='''''' if not stream_end else None   )







   def       UpperCAmelCase				(			self ,   lowerCamelCase__   ):

      '''simple docstring'''







      if (
          (cp >= 0x4_E_0_0 and cp <= 0x9_F_F_F)
          or (cp >= 0x3_4_0_0 and cp <= 0x4_D_B_F)  #
          or (cp >= 0x2_0_0_0_0 and cp <= 0x2_A_6_D_F)  #
          or (cp >= 0x2_A_7_0_0 and cp <= 0x2_B_7_3_F)  #
          or (cp >= 0x2_B_7_4_0 and cp <= 0x2_B_8_1_F)  #
          or (cp >= 0x2_B_8_2_0 and cp <= 0x2_C_E_A_F)  #
          or (cp >= 0xF_9_0_0 and cp <= 0xF_A_F_F)
          or (cp >= 0x2_F_8_0_0 and cp <= 0x2_F_A_1_F)  #
      ):  #
         return True

      return False




class 						lowercase__	(  snake_case_   ):






   '''simple docstring'''







   def __init__(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = False ,   lowerCamelCase__ = None ,   **lowerCamelCase__   ):

      '''simple docstring'''







      super().__init__(lowerCamelCase__ ,   lowerCamelCase__ ,   **lowerCamelCase__   )
      UpperCamelCase       =  Queue()
      UpperCamelCase       =  None
      UpperCamelCase       =  timeout







   def       UpperCAmelCase				(			self ,   lowerCamelCase__ ,   lowerCamelCase__ = False   ):

      '''simple docstring'''







      self.text_queue.put(lowerCamelCase__ ,   timeout=self.timeout   )
      if stream_end:
         self.text_queue.put(self.stop_signal ,   timeout=self.timeout   )







   def __iter__(			self   ):

      '''simple docstring'''







      return self







   def       UpperCAmelCase				(			self   ):

      '''simple docstring'''







      UpperCamelCase       =  self.text_queue.get(timeout=self.timeout   )
      if value == self.stop_signal:
         raise StopIteration()
      else:
         return value





