





"""simple docstring"""





from typing import Any, Callable, Dict, List, Optional, Union

import PIL.Image
import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionImgaImgPipeline,
    StableDiffusionInpaintPipelineLegacy,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.configuration_utils import FrozenDict
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker
from diffusers.utils import deprecate, logging


_lowerCamelCase       =	logging.get_logger(__name__)  # pylint: disable=invalid-name





class 			snake_case		(   __UpperCAmelCase   ):
      lowerCamelCase__						= ['''safety_checker''', '''feature_extractor''']

      def __init__(			self   :List[Any]						,							_lowerCamelCase   :AutoencoderKL						,							_lowerCamelCase   :CLIPTextModel						,							_lowerCamelCase   :CLIPTokenizer						,							_lowerCamelCase   :UNetaDConditionModel						,							_lowerCamelCase   :Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]						,							_lowerCamelCase   :StableDiffusionSafetyChecker						,							_lowerCamelCase   :CLIPImageProcessor						,							_lowerCamelCase   :bool = True						,							):
       super().__init__()
       if hasattr(scheduler.config						,							'''steps_offset'''						) and scheduler.config.steps_offset != 1:
        __SCREAMING_SNAKE_CASE :			Union[str, Any]												=       (
            f'''The configuration file of this scheduler: {scheduler} is outdated. `steps_offset`'''
            f''' should be set to 1 instead of {scheduler.config.steps_offset}. Please make sure '''
            '''to update the config accordingly as leaving `steps_offset` might led to incorrect results'''
            ''' in future versions. If you have downloaded this checkpoint from the Hugging Face Hub,'''
            ''' it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json`'''
            ''' file'''
        )
        deprecate('''steps_offset!=1'''						,							'''1.0.0'''						,							_lowerCamelCase						,							standard_warn=_lowerCamelCase						)
        __SCREAMING_SNAKE_CASE :			Optional[Any]												=       dict(scheduler.config						)
        __SCREAMING_SNAKE_CASE :			str												=       1
        __SCREAMING_SNAKE_CASE :			int												=       FrozenDict(_lowerCamelCase						)

       self.register_modules(
           vae=_lowerCamelCase						,							text_encoder=_lowerCamelCase						,							tokenizer=_lowerCamelCase						,							unet=_lowerCamelCase						,							scheduler=_lowerCamelCase						,							safety_checker=_lowerCamelCase						,							feature_extractor=_lowerCamelCase						,							)
       self.register_to_config(requires_safety_checker=_lowerCamelCase						)

      @property
      def 						SCREAMING_SNAKE_CASE_			(			self   :List[str]						):
       return {k: getattr(self						,							_lowerCamelCase						) for k in self.config.keys() if not k.startswith('''_'''						)}

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[str]						,							_lowerCamelCase   :Optional[Union[str, int]] = "auto"						):
       if slice_size == "auto":
        # half the attention head size is usually a good trade-off between
        # speed and memory
        __SCREAMING_SNAKE_CASE :			Any												=       self.unet.config.attention_head_dim // 2
       self.unet.set_attention_slice(_lowerCamelCase						)

      def 						SCREAMING_SNAKE_CASE_			(			self   :List[str]						):
       self.enable_attention_slicing(_lowerCamelCase						)

      @torch.no_grad()
      def 						SCREAMING_SNAKE_CASE_			(			self   :List[Any]						,							_lowerCamelCase   :Union[str, List[str]]						,							_lowerCamelCase   :Union[torch.FloatTensor, PIL.Image.Image]						,							_lowerCamelCase   :Union[torch.FloatTensor, PIL.Image.Image]						,							_lowerCamelCase   :float = 0.8						,							_lowerCamelCase   :Optional[int] = 5_0						,							_lowerCamelCase   :Optional[float] = 7.5						,							_lowerCamelCase   :Optional[Union[str, List[str]]] = None						,							_lowerCamelCase   :Optional[int] = 1						,							_lowerCamelCase   :Optional[float] = 0.0						,							_lowerCamelCase   :Optional[torch.Generator] = None						,							_lowerCamelCase   :Optional[str] = "pil"						,							_lowerCamelCase   :bool = True						,							_lowerCamelCase   :Optional[Callable[[int, int, torch.FloatTensor], None]] = None						,							_lowerCamelCase   :int = 1						,							):
       # For more information on how this function works, please see: https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion#diffusers.StableDiffusionImg2ImgPipeline
       return StableDiffusionInpaintPipelineLegacy(**self.components						)(
           prompt=_lowerCamelCase						,							image=_lowerCamelCase						,							mask_image=_lowerCamelCase						,							strength=_lowerCamelCase						,							num_inference_steps=_lowerCamelCase						,							guidance_scale=_lowerCamelCase						,							negative_prompt=_lowerCamelCase						,							num_images_per_prompt=_lowerCamelCase						,							eta=_lowerCamelCase						,							generator=_lowerCamelCase						,							output_type=_lowerCamelCase						,							return_dict=_lowerCamelCase						,							callback=_lowerCamelCase						,							)

      @torch.no_grad()
      def 						SCREAMING_SNAKE_CASE_			(			self   :Optional[int]						,							_lowerCamelCase   :Union[str, List[str]]						,							_lowerCamelCase   :Union[torch.FloatTensor, PIL.Image.Image]						,							_lowerCamelCase   :float = 0.8						,							_lowerCamelCase   :Optional[int] = 5_0						,							_lowerCamelCase   :Optional[float] = 7.5						,							_lowerCamelCase   :Optional[Union[str, List[str]]] = None						,							_lowerCamelCase   :Optional[int] = 1						,							_lowerCamelCase   :Optional[float] = 0.0						,							_lowerCamelCase   :Optional[torch.Generator] = None						,							_lowerCamelCase   :Optional[str] = "pil"						,							_lowerCamelCase   :bool = True						,							_lowerCamelCase   :Optional[Callable[[int, int, torch.FloatTensor], None]] = None						,							_lowerCamelCase   :int = 1						,							**_lowerCamelCase   :List[str]						,							):
       # For more information on how this function works, please see: https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion#diffusers.StableDiffusionImg2ImgPipeline
       return StableDiffusionImgaImgPipeline(**self.components						)(
           prompt=_lowerCamelCase						,							image=_lowerCamelCase						,							strength=_lowerCamelCase						,							num_inference_steps=_lowerCamelCase						,							guidance_scale=_lowerCamelCase						,							negative_prompt=_lowerCamelCase						,							num_images_per_prompt=_lowerCamelCase						,							eta=_lowerCamelCase						,							generator=_lowerCamelCase						,							output_type=_lowerCamelCase						,							return_dict=_lowerCamelCase						,							callback=_lowerCamelCase						,							callback_steps=_lowerCamelCase						,							)



      @torch.no_grad()
      def 						SCREAMING_SNAKE_CASE_			(			self   :Dict						,							_lowerCamelCase   :Union[str, List[str]]						,							_lowerCamelCase   :int = 5_1_2						,							_lowerCamelCase   :int = 5_1_2						,							_lowerCamelCase   :int = 5_0						,							_lowerCamelCase   :float = 7.5						,							_lowerCamelCase   :Optional[Union[str, List[str]]] = None						,							_lowerCamelCase   :Optional[int] = 1						,							_lowerCamelCase   :float = 0.0						,							_lowerCamelCase   :Optional[torch.Generator] = None						,							_lowerCamelCase   :Optional[torch.FloatTensor] = None						,							_lowerCamelCase   :Optional[str] = "pil"						,							_lowerCamelCase   :bool = True						,							_lowerCamelCase   :Optional[Callable[[int, int, torch.FloatTensor], None]] = None						,							_lowerCamelCase   :int = 1						,							):
       # For more information on how this function https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion#diffusers.StableDiffusionPipeline
       return StableDiffusionPipeline(**self.components						)(
           prompt=_lowerCamelCase						,							height=_lowerCamelCase						,							width=_lowerCamelCase						,							num_inference_steps=_lowerCamelCase						,							guidance_scale=_lowerCamelCase						,							negative_prompt=_lowerCamelCase						,							num_images_per_prompt=_lowerCamelCase						,							eta=_lowerCamelCase						,							generator=_lowerCamelCase						,							latents=_lowerCamelCase						,							output_type=_lowerCamelCase						,							return_dict=_lowerCamelCase						,							callback=_lowerCamelCase						,							callback_steps=_lowerCamelCase						,							)









"""simple docstring"""



import unittest

import torch

from diffusers import DDIMScheduler, TextToVideoZeroPipeline
from diffusers.utils import load_pt, require_torch_gpu, slow

from ..test_pipelines_common import assert_mean_pixel_difference




@slow
@require_torch_gpu
class        lowerCAmelCase__       (  unittest.TestCase					):
   def 	__a				(						self	:     Optional[Any]				):


        '''simple docstring'''
        UpperCAmelCase__		:  int				=				"runwayml/stable-diffusion-v1-5"
        UpperCAmelCase__		:  Optional[Any]				=				TextToVideoZeroPipeline.from_pretrained(snake_case__				,       torch_dtype=torch.floataa				).to("cuda"				)
        UpperCAmelCase__		:  Any				=				DDIMScheduler.from_config(pipe.scheduler.config				)
        UpperCAmelCase__		:  Optional[Any]				=				torch.Generator(device="cuda"				).manual_seed(0				)

        UpperCAmelCase__		:  Any				=				"A bear is playing a guitar on Times Square"
        UpperCAmelCase__		:  Dict				=				pipe(prompt=snake_case__				,       generator=snake_case__				).images

        UpperCAmelCase__		:  List[Any]				=				load_pt(
            "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/text-to-video/A bear is playing a guitar on Times Square.pt"				)

        assert_mean_pixel_difference(snake_case__				,       snake_case__				)


