

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        a__       (   __UpperCAmelCase	,			unittest.TestCase       ):







  """simple docstring"""




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

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


  def  _lowercase      (			self    : Optional[Any]		)					->Optional[Any]:
        """simple docstring"""







        torch.manual_seed(0		)
        SCREAMING_SNAKE_CASE :      List[str]       =   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				,				)
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   DDIMScheduler(
            beta_start=0.0_00_85				,				beta_end=0.0_12				,				beta_schedule="""scaled_linear"""				,				clip_sample=lowerCAmelCase_				,				set_alpha_to_one=lowerCAmelCase_				,				)
        torch.manual_seed(0		)
        SCREAMING_SNAKE_CASE :      str       =   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		)
        SCREAMING_SNAKE_CASE :      List[Any]       =   CLIPTextConfig(
            bos_token_id=0				,				eos_token_id=2				,				hidden_size=3_2				,				intermediate_size=3_7				,				layer_norm_eps=1e-05				,				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				,				)
        SCREAMING_SNAKE_CASE :      Optional[int]       =   CLIPTextModel(lowerCAmelCase_		)
        SCREAMING_SNAKE_CASE :      List[str]       =   CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""		)

        SCREAMING_SNAKE_CASE :      int       =   {
            """unet""": unet,
            """scheduler""": scheduler,
            """vae""": vae,
            """text_encoder""": text_encoder,
            """tokenizer""": tokenizer,
        }
        return components


  def  _lowercase      (			self    : Tuple				,				UpperCAmelCase__    : int				,				UpperCAmelCase__    : Optional[Any]=0		)					->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      List[Any]       =   floats_tensor((1, 3, 3, 3_2, 3_2)				,				rng=random.Random(lowerCAmelCase_		)		).to(lowerCAmelCase_		)

        if str(lowerCAmelCase_		).startswith("""mps"""		):
              SCREAMING_SNAKE_CASE :      Optional[int]       =   torch.manual_seed(lowerCAmelCase_		)
        else:
              SCREAMING_SNAKE_CASE :      str       =   torch.Generator(device=lowerCAmelCase_		).manual_seed(lowerCAmelCase_		)
        SCREAMING_SNAKE_CASE :      Optional[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  _lowercase      (			self    : int		)					->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      int       =   """cpu"""  # ensure determinism for the device-dependent torch.Generator
        SCREAMING_SNAKE_CASE :      str       =   self.get_dummy_components()
        SCREAMING_SNAKE_CASE :      Dict       =   VideoToVideoSDPipeline(**lowerCAmelCase_		)
        SCREAMING_SNAKE_CASE :      str       =   sd_pipe.to(lowerCAmelCase_		)
        sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_		)

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

        assert frames[0].shape == (3_2, 3_2, 3)
        SCREAMING_SNAKE_CASE :      List[str]       =   np.array([1_0_6, 1_1_7, 1_1_3, 1_7_4, 1_3_7, 1_1_2, 1_4_8, 1_5_1, 1_3_1]		)

        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  _lowercase      (			self    : Any		)					->Optional[Any]:
        """simple docstring"""







        self._test_xformers_attention_forwardGenerator_pass(test_mean_pixel_difference=lowerCAmelCase_				,				expected_max_diff=5e-3		)


  @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline."""		)
  def  _lowercase      (			self    : Dict		)					->Optional[Any]:
        """simple docstring"""







        pass


  @unittest.skip(reason="""Batching needs to be properly figured out first for this pipeline."""		)
  def  _lowercase      (			self    : Optional[int]		)					->Optional[int]:
        """simple docstring"""







        pass


  @unittest.skip(reason="""`num_images_per_prompt` argument is not supported for this pipeline."""		)
  def  _lowercase      (			self    : List[Any]		)					->int:
        """simple docstring"""







        pass




  def  _lowercase      (			self    : Optional[Any]		)					->List[Any]:
        """simple docstring"""







        return super().test_progress_bar()







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







  """simple docstring"""




  def  _lowercase      (			self    : List[str]		)					->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE :      Any       =   VideoToVideoSDPipeline.from_pretrained("""cerspense/zeroscope_v2_XL"""				,				torch_dtype=torch.floataa		)
        pipe.enable_model_cpu_offload()

        # 10 frames
        SCREAMING_SNAKE_CASE :      List[str]       =   torch.Generator(device="""cpu"""		).manual_seed(0		)
        SCREAMING_SNAKE_CASE :      int       =   torch.randn((1, 1_0, 3, 1_0_2_4, 5_7_6)				,				generator=lowerCAmelCase_		)
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   video.to("""cuda"""		)

        SCREAMING_SNAKE_CASE :      int       =   """Spiderman is surfing"""

        SCREAMING_SNAKE_CASE :      List[Any]       =   pipe(lowerCAmelCase_				,				video=lowerCAmelCase_				,				generator=lowerCAmelCase_				,				num_inference_steps=3				,				output_type="""pt"""		).frames

        SCREAMING_SNAKE_CASE :      Tuple       =   np.array([-1.0_45_89_84, -1.1_27_92_97, -0.9_66_30_86, -0.91_50_39_06, -0.75_09_76_56]		)
        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


UpperCAmelCase__ :     Dict				    =    logging.get_logger(__name__)

UpperCAmelCase__ :     int				    =    {
    """abeja/gpt-neox-japanese-2.7b""": """https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/config.json""",
}







class        a__       (   UpperCAmelCase       ):







  """simple docstring"""




  UpperCAmelCase__      : Any			       ="""gpt_neox_japanese"""


  def __init__(			self    : Any				,				UpperCAmelCase__    : Any=3_2_0_0_0				,				UpperCAmelCase__    : Dict=2_5_6_0				,				UpperCAmelCase__    : List[str]=3_2				,				UpperCAmelCase__    : Optional[int]=3_2				,				UpperCAmelCase__    : List[str]=4				,				UpperCAmelCase__    : List[str]="gelu"				,				UpperCAmelCase__    : Optional[int]=1.00				,				UpperCAmelCase__    : List[Any]=1_0_0_0_0				,				UpperCAmelCase__    : Tuple=2_0_4_8				,				UpperCAmelCase__    : Optional[int]=0.02				,				UpperCAmelCase__    : Dict=1e-5				,				UpperCAmelCase__    : Dict=True				,				UpperCAmelCase__    : Tuple=3_1_9_9_6				,				UpperCAmelCase__    : Tuple=3_1_9_9_9				,				UpperCAmelCase__    : Dict=0.1				,				UpperCAmelCase__    : Union[str, Any]=0.0				,				**UpperCAmelCase__    : Optional[Any]				,				)					->Optional[Any]:
        """simple docstring"""







        super().__init__(bos_token_id=UpperCAmelCase__				,				eos_token_id=UpperCAmelCase__				,				**UpperCAmelCase__		)
        SCREAMING_SNAKE_CASE :      List[str]       =   vocab_size
        SCREAMING_SNAKE_CASE :      List[str]       =   max_position_embeddings
        SCREAMING_SNAKE_CASE :      Tuple       =   hidden_size
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   num_hidden_layers
        SCREAMING_SNAKE_CASE :      Optional[int]       =   num_attention_heads
        SCREAMING_SNAKE_CASE :      Any       =   intermediate_multiple_size
        SCREAMING_SNAKE_CASE :      Tuple       =   hidden_act
        SCREAMING_SNAKE_CASE :      Any       =   rotary_pct
        SCREAMING_SNAKE_CASE :      Tuple       =   rotary_emb_base
        SCREAMING_SNAKE_CASE :      Union[str, Any]       =   initializer_range
        SCREAMING_SNAKE_CASE :      Optional[Any]       =   layer_norm_eps
        SCREAMING_SNAKE_CASE :      str       =   use_cache
        SCREAMING_SNAKE_CASE :      Any       =   attention_dropout
        SCREAMING_SNAKE_CASE :      str       =   hidden_dropout




