
'''simple docstring'''





from typing import Optional

import torch
from PIL import Image
from tqdm.auto import tqdm
from transformers import CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, DiffusionPipeline, UNetaDConditionModel
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import (
    deprecate,
)






class        UpperCAmelCase_       (						A							):

 '''simple docstring'''







 def __init__(				self		:     Dict ,							snake_case__		:     AutoencoderKL ,							snake_case__		:     CLIPTextModel ,							snake_case__		:     CLIPTokenizer ,							snake_case__		:     UNetaDConditionModel ,							snake_case__		:     DDIMScheduler ,							snake_case__		:     float = 0.93 ,							snake_case__		:     bool = True ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       mixing_coeff
   UpperCAmelCase__			:				Optional[Any]												=       leapfrog_steps

   super().__init__()
   self.register_modules(
       vae=snake_case__ ,							text_encoder=snake_case__ ,							tokenizer=snake_case__ ,							unet=snake_case__ ,							scheduler=snake_case__ ,							)

   UpperCAmelCase__			:				int												=       2 ** (len(self.vae.config.block_out_channels   ) - 1)
   UpperCAmelCase__			:				int												=       VaeImageProcessor(vae_scale_factor=self.vae_scale_factor   )







 def        UpperCamelCase     (				self		:     Dict ,							snake_case__		:     str ,							snake_case__		:     Optional[str] = None ,							snake_case__		:     bool = False   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Union[str, Any]												=       self.tokenizer(
       snake_case__ ,							padding="max_length" ,							max_length=self.tokenizer.model_max_length ,							truncation=snake_case__ ,							return_tensors="pt" ,							)

   UpperCAmelCase__			:				Optional[int]												=       self.text_encoder(text_inputs.input_ids.to(self.device   )   ).last_hidden_state

   UpperCAmelCase__			:				Tuple												=       prompt_embeds.to(dtype=self.text_encoder.dtype ,							device=self.device   )

   if do_classifier_free_guidance:
     UpperCAmelCase__			:				Dict												=       "" if negative_prompt is None else negative_prompt

     UpperCAmelCase__			:				List[Any]												=       self.tokenizer(
         snake_case__ ,							padding="max_length" ,							max_length=self.tokenizer.model_max_length ,							truncation=snake_case__ ,							return_tensors="pt" ,							)

     UpperCAmelCase__			:				List[str]												=       self.text_encoder(uncond_input.input_ids.to(self.device   )   ).last_hidden_state

     UpperCAmelCase__			:				List[Any]												=       torch.cat([negative_prompt_embeds, prompt_embeds]   )

   return prompt_embeds







 def        UpperCamelCase     (				self		:     Optional[int] ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[Any]												=       self.mixing_coeff * x + (1 - self.mixing_coeff) * y
   UpperCAmelCase__			:				Optional[int]												=       self.mixing_coeff * y + (1 - self.mixing_coeff) * x

   return [x, y]







 def        UpperCamelCase     (				self		:     Optional[Any] ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Any												=       (y - (1 - self.mixing_coeff) * x) / self.mixing_coeff
   UpperCAmelCase__			:				Union[str, Any]												=       (x - (1 - self.mixing_coeff) * y) / self.mixing_coeff

   return [x, y]







 def        UpperCamelCase     (				self		:     Optional[Any] ,							snake_case__		:     torch.Tensor   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       int(snake_case__   )

   UpperCAmelCase__			:				Tuple												=       self.scheduler.alphas_cumprod[t] if t >= 0 else self.scheduler.final_alpha_cumprod

   return alpha_prod, 1 - alpha_prod







 def        UpperCamelCase     (				self		:     List[Any] ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[Any]												=       timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

   UpperCAmelCase__					, UpperCAmelCase__			:				List[Any]												=       self._get_alpha_and_beta(snake_case__   )
   UpperCAmelCase__					, UpperCAmelCase__			:				Optional[Any]												=       self._get_alpha_and_beta(snake_case__   )

   UpperCAmelCase__			:				List[Any]												=       (alpha_prod_t_prev / alpha_prod_t) ** 0.5
   UpperCAmelCase__			:				List[str]												=       -a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5

   UpperCAmelCase__			:				Optional[Any]												=       (base - b_t * model_output) / a_t

   return model_input, next_model_input.to(base.dtype   )







 def        UpperCamelCase     (				self		:     List[str] ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[Any]												=       timestep - self.scheduler.config.num_train_timesteps / self.scheduler.num_inference_steps

   UpperCAmelCase__					, UpperCAmelCase__			:				Optional[Any]												=       self._get_alpha_and_beta(snake_case__   )
   UpperCAmelCase__					, UpperCAmelCase__			:				int												=       self._get_alpha_and_beta(snake_case__   )

   UpperCAmelCase__			:				Union[str, Any]												=       (alpha_prod_t_prev / alpha_prod_t) ** 0.5
   UpperCAmelCase__			:				Tuple												=       -a_t * (beta_prod_t**0.5) + beta_prod_t_prev**0.5
   UpperCAmelCase__			:				Optional[int]												=       a_t * base + b_t * model_output

   return model_input, next_model_input.to(base.dtype   )







 @torch.no_grad()
 def        UpperCamelCase     (				self		:     int ,							snake_case__		:     torch.Tensor   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       1 / self.vae.config.scaling_factor * latents
   UpperCAmelCase__			:				List[str]												=       self.vae.decode(snake_case__   ).sample
   UpperCAmelCase__			:				Tuple												=       (image / 2 + 0.5).clamp(0 ,							1   )
   return image







 @torch.no_grad()
 def        UpperCamelCase     (				self		:     int ,							snake_case__		:     Image.Image ,							snake_case__		:     torch.Tensor ,							snake_case__		:     torch.Tensor ,							snake_case__		:     float ,							snake_case__		:     Optional[torch.Generator] = None ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				Union[str, Any]												=       guidance_scale > 1.0

   UpperCAmelCase__			:				int												=       image.to(device=self.device ,							dtype=text_embeds.dtype   )
   UpperCAmelCase__			:				str												=       self.vae.encode(snake_case__   ).latent_dist.sample(snake_case__   )

   UpperCAmelCase__			:				List[str]												=       self.vae.config.scaling_factor * latent

   UpperCAmelCase__			:				Any												=       [latent.clone(), latent.clone()]

   for i, t in tqdm(enumerate(snake_case__   ) ,							total=len(snake_case__   )   ):
     UpperCAmelCase__			:				Tuple												=       self.noise_mixing_layer(x=coupled_latents[0] ,							y=coupled_latents[1]   )

     # j - model_input index, k - base index
     for j in range(2   ):
       UpperCAmelCase__			:				Tuple												=       j ^ 1

       if self.leapfrog_steps:
         if i % 2 == 0:
           UpperCAmelCase__					, UpperCAmelCase__			:				Tuple												=       j, k

       UpperCAmelCase__			:				Tuple												=       coupled_latents[j]
       UpperCAmelCase__			:				List[Any]												=       coupled_latents[k]

       UpperCAmelCase__			:				str												=       torch.cat([model_input] * 2   ) if do_classifier_free_guidance else model_input

       UpperCAmelCase__			:				Dict												=       self.unet(snake_case__ ,							snake_case__ ,							encoder_hidden_states=snake_case__   ).sample

       if do_classifier_free_guidance:
         UpperCAmelCase__					, UpperCAmelCase__			:				Any												=       noise_pred.chunk(2   )
         UpperCAmelCase__			:				int												=       noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

       UpperCAmelCase__					, UpperCAmelCase__			:				Union[str, Any]												=       self.noise_step(
           base=snake_case__ ,							model_input=snake_case__ ,							model_output=snake_case__ ,							timestep=snake_case__ ,							)

       UpperCAmelCase__			:				Tuple												=       model_input

   return coupled_latents







 @torch.no_grad()
 def __call__(				self		:     int ,							snake_case__		:     str ,							snake_case__		:     str ,							snake_case__		:     Image.Image ,							snake_case__		:     float = 3.0 ,							snake_case__		:     int = 50 ,							snake_case__		:     float = 0.8 ,							snake_case__		:     Optional[str] = None ,							snake_case__		:     Optional[torch.Generator] = None ,							snake_case__		:     Optional[str] = "pil" ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       guidance_scale > 1.0

   UpperCAmelCase__			:				Union[str, Any]												=       self.image_processor.preprocess(snake_case__   )

   UpperCAmelCase__			:				List[Any]												=       self._encode_prompt(snake_case__ ,							snake_case__ ,							snake_case__   )
   UpperCAmelCase__			:				Any												=       self._encode_prompt(snake_case__ ,							snake_case__ ,							snake_case__   )

   self.scheduler.set_timesteps(snake_case__ ,							self.device   )

   UpperCAmelCase__			:				Optional[int]												=       num_inference_steps - int(num_inference_steps * strength   )
   UpperCAmelCase__			:				List[str]												=       self.scheduler.timesteps[t_limit:]
   UpperCAmelCase__			:				Any												=       fwd_timesteps.flip(0   )

   UpperCAmelCase__			:				List[Any]												=       self.prepare_latents(snake_case__ ,							snake_case__ ,							snake_case__ ,							snake_case__ ,							snake_case__   )

   for i, t in tqdm(enumerate(snake_case__   ) ,							total=len(snake_case__   )   ):
     # j - model_input index, k - base index
     for k in range(2   ):
       UpperCAmelCase__			:				Union[str, Any]												=       k ^ 1

       if self.leapfrog_steps:
         if i % 2 == 1:
           UpperCAmelCase__					, UpperCAmelCase__			:				List[Any]												=       j, k

       UpperCAmelCase__			:				Optional[int]												=       coupled_latents[j]
       UpperCAmelCase__			:				int												=       coupled_latents[k]

       UpperCAmelCase__			:				List[Any]												=       torch.cat([model_input] * 2   ) if do_classifier_free_guidance else model_input

       UpperCAmelCase__			:				Optional[Any]												=       self.unet(snake_case__ ,							snake_case__ ,							encoder_hidden_states=snake_case__   ).sample

       if do_classifier_free_guidance:
         UpperCAmelCase__					, UpperCAmelCase__			:				Union[str, Any]												=       noise_pred.chunk(2   )
         UpperCAmelCase__			:				Optional[Any]												=       noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

       UpperCAmelCase__					, UpperCAmelCase__			:				Any												=       self.denoise_step(
           base=snake_case__ ,							model_input=snake_case__ ,							model_output=snake_case__ ,							timestep=snake_case__ ,							)

       UpperCAmelCase__			:				Dict												=       model_input

     UpperCAmelCase__			:				Any												=       self.denoise_mixing_layer(x=coupled_latents[0] ,							y=coupled_latents[1]   )

   # either one is fine
   UpperCAmelCase__			:				Union[str, Any]												=       coupled_latents[0]

   if output_type not in ["latent", "pt", "np", "pil"]:
     UpperCAmelCase__			:				Optional[int]												=       (
         F"""the output_type {output_type} is outdated. Please make sure to set it to one of these instead: """
         "`pil`, `np`, `pt`, `latent`"
     )
     deprecate("Unsupported output_type" ,							"1.0.0" ,							snake_case__ ,							standard_warn=snake_case__   )
     UpperCAmelCase__			:				Union[str, Any]												=       "np"

   if output_type == "latent":
     UpperCAmelCase__			:				Tuple												=       final_latent
   else:
     UpperCAmelCase__			:				str												=       self.decode_latents(snake_case__   )
     UpperCAmelCase__			:				int												=       self.image_processor.postprocess(snake_case__ ,							output_type=snake_case__   )

   return image





'''simple docstring'''





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import VideoMAEImageProcessor






class        UpperCAmelCase_       (						unittest.TestCase							):

 '''simple docstring'''







 def __init__(				self		:     str ,							snake_case__		:     Optional[int] ,							snake_case__		:     Union[str, Any]=7 ,							snake_case__		:     Any=3 ,							snake_case__		:     Tuple=10 ,							snake_case__		:     Dict=18 ,							snake_case__		:     Dict=30 ,							snake_case__		:     Tuple=4_00 ,							snake_case__		:     str=True ,							snake_case__		:     Optional[Any]=None ,							snake_case__		:     Union[str, Any]=True ,							snake_case__		:     List[Any]=[0.5, 0.5, 0.5] ,							snake_case__		:     Dict=[0.5, 0.5, 0.5] ,							snake_case__		:     Tuple=None ,							):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       size if size is not None else {"shortest_edge": 18}
   UpperCAmelCase__			:				Optional[int]												=       crop_size if crop_size is not None else {"height": 18, "width": 18}

   UpperCAmelCase__			:				List[Any]												=       parent
   UpperCAmelCase__			:				Optional[Any]												=       batch_size
   UpperCAmelCase__			:				int												=       num_channels
   UpperCAmelCase__			:				str												=       num_frames
   UpperCAmelCase__			:				List[Any]												=       image_size
   UpperCAmelCase__			:				Optional[int]												=       min_resolution
   UpperCAmelCase__			:				int												=       max_resolution
   UpperCAmelCase__			:				Optional[int]												=       do_resize
   UpperCAmelCase__			:				List[str]												=       size
   UpperCAmelCase__			:				Optional[int]												=       do_normalize
   UpperCAmelCase__			:				int												=       image_mean
   UpperCAmelCase__			:				Any												=       image_std
   UpperCAmelCase__			:				Optional[int]												=       crop_size







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   return {
       "image_mean": self.image_mean,
       "image_std": self.image_std,
       "do_normalize": self.do_normalize,
       "do_resize": self.do_resize,
       "size": self.size,
       "crop_size": self.crop_size,
   }






@require_torch
@require_vision
class        UpperCAmelCase_       (						A				,							unittest.TestCase							):

 '''simple docstring'''
 lowercase_				:						Any				=       VideoMAEImageProcessor if is_vision_available() else None







 def        UpperCamelCase     (				self		:     List[str]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Optional[int]												=       VideoMAEImageProcessingTester(self   )







 @property
 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   return self.image_processor_tester.prepare_image_processor_dict()







 def        UpperCamelCase     (				self		:     Any   ):



   '''simple docstring'''




   UpperCAmelCase__			:				str												=       self.image_processing_class(**self.image_processor_dict   )
   self.assertTrue(hasattr(snake_case__ ,							"image_mean"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"image_std"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_normalize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_resize"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"do_center_crop"   )   )
   self.assertTrue(hasattr(snake_case__ ,							"size"   )   )







 def        UpperCamelCase     (				self		:     Tuple   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.image_processing_class.from_dict(self.image_processor_dict   )
   self.assertEqual(image_processor.size ,							{"shortest_edge": 18}   )
   self.assertEqual(image_processor.crop_size ,							{"height": 18, "width": 18}   )

   UpperCAmelCase__			:				Union[str, Any]												=       self.image_processing_class.from_dict(self.image_processor_dict ,							size=42 ,							crop_size=84   )
   self.assertEqual(image_processor.size ,							{"shortest_edge": 42}   )
   self.assertEqual(image_processor.crop_size ,							{"height": 84, "width": 84}   )







 def        UpperCamelCase     (				self		:     Optional[int]   ):



   '''simple docstring'''




   pass







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				List[str]												=       self.image_processing_class(**self.image_processor_dict   )
   # create random PIL videos
   UpperCAmelCase__			:				Optional[int]												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							Image.Image   )

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Tuple												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)







 def        UpperCamelCase     (				self		:     Union[str, Any]   ):



   '''simple docstring'''




   UpperCAmelCase__			:				Dict												=       self.image_processing_class(**self.image_processor_dict   )
   # create random numpy tensors
   UpperCAmelCase__			:				List[Any]												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__ ,							numpify=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							np.ndarray   )

   # Test not batched input
   UpperCAmelCase__			:				Dict												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Optional[int]												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)







 def        UpperCamelCase     (				self		:     Dict   ):



   '''simple docstring'''




   UpperCAmelCase__			:				int												=       self.image_processing_class(**self.image_processor_dict   )
   # create random PyTorch tensors
   UpperCAmelCase__			:				Dict												=       prepare_video_inputs(self.image_processor_tester ,							equal_resolution=snake_case__ ,							torchify=snake_case__   )
   for video in video_inputs:
     self.assertIsInstance(snake_case__ ,							snake_case__   )
     self.assertIsInstance(video[0] ,							torch.Tensor   )

   # Test not batched input
   UpperCAmelCase__			:				int												=       image_processing(video_inputs[0] ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           1,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)

   # Test batched
   UpperCAmelCase__			:				Optional[Any]												=       image_processing(snake_case__ ,							return_tensors="pt"   ).pixel_values
   self.assertEqual(
       encoded_videos.shape ,							(
           self.image_processor_tester.batch_size,
           self.image_processor_tester.num_frames,
           self.image_processor_tester.num_channels,
           self.image_processor_tester.crop_size["height"],
           self.image_processor_tester.crop_size["width"],
       ) ,							)



