

'''simple docstring'''




import inspect
from typing import List, Optional, Union

import torch
from torch import nn
from torch.nn import functional as F
from torchvision import transforms
from transformers import CLIPImageProcessor, CLIPModel, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    DPMSolverMultistepScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion import StableDiffusionPipelineOutput



class     lowercase_		(						nn.Module	):

  '''simple docstring'''





  def __init__(       self				,	a_				,	a_=1.0       )							->			Union[str, Any]:


    """simple docstring"""
    super().__init__()

    UpperCAmelCase  							=		cut_size
    UpperCAmelCase  							=		cut_power





  def   snake_case_     (       self				,	a_				,	a_       )							->			int:


    """simple docstring"""
    UpperCAmelCase							, UpperCAmelCase  							=		pixel_values.shape[2:4]
    UpperCAmelCase  							=		min(a_				,	a_       )
    UpperCAmelCase  							=		min(a_				,	a_				,	self.cut_size       )
    UpperCAmelCase  							=		[]
    for _ in range(a_       ):
      UpperCAmelCase  							=		int(torch.rand([]       ) ** self.cut_power * (max_size - min_size) + min_size       )
      UpperCAmelCase  							=		torch.randint(0				,	sideX - size + 1				,	()       )
      UpperCAmelCase  							=		torch.randint(0				,	sideY - size + 1				,	()       )
      UpperCAmelCase  							=		pixel_values[:, :, offsety : offsety + size, offsetx : offsetx + size]
      cutouts.append(F.adaptive_avg_poolad(a_				,	self.cut_size       )       )
    return torch.cat(a_       )




def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				str    ,				SCREAMING_SNAKE_CASE :				Union[str, Any]  ):
  UpperCAmelCase  							=		F.normalize(SCREAMING_SNAKE_CASE    ,				dim=-1  )
  UpperCAmelCase  							=		F.normalize(SCREAMING_SNAKE_CASE    ,				dim=-1  )
  return (x - y).norm(dim=-1  ).div(2  ).arcsin().pow(2  ).mul(2  )


def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Dict    ,				SCREAMING_SNAKE_CASE :				List[str]  ):
  for param in model.parameters():
    UpperCAmelCase  							=		value



class     lowercase_		(						a	):

  '''simple docstring'''





  def __init__(       self				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	)							->			Optional[int]:


    """simple docstring"""
    super().__init__()
    self.register_modules(
        vae=a_				,	text_encoder=a_				,	clip_model=a_				,	tokenizer=a_				,	unet=a_				,	scheduler=a_				,	feature_extractor=a_				,	)

    UpperCAmelCase  							=		transforms.Normalize(mean=feature_extractor.image_mean				,	std=feature_extractor.image_std       )
    UpperCAmelCase  							=		(
        feature_extractor.size
        if isinstance(feature_extractor.size				,	a_       )
        else feature_extractor.size['shortest_edge']
    )
    UpperCAmelCase  							=		MakeCutouts(self.cut_out_size       )

    set_requires_grad(self.text_encoder				,	a_       )
    set_requires_grad(self.clip_model				,	a_       )
  def   snake_case_     (       self				,	a_ = "auto"       )							->			Optional[Any]:


    """simple docstring"""
    if slice_size == "auto":
      # half the attention head size is usually a good trade-off between
      # speed and memory
      UpperCAmelCase  							=		self.unet.config.attention_head_dim // 2
    self.unet.set_attention_slice(a_       )
  def   snake_case_     (       self       )							->			str:


    """simple docstring"""
    self.enable_attention_slicing(a_       )
  def   snake_case_     (       self       )							->			Optional[Any]:


    """simple docstring"""
    set_requires_grad(self.vae				,	a_       )
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    set_requires_grad(self.vae				,	a_       )
  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    set_requires_grad(self.unet				,	a_       )
  def   snake_case_     (       self       )							->			Optional[Any]:


    """simple docstring"""
    set_requires_grad(self.unet				,	a_       )
  @torch.enable_grad()
  def   snake_case_     (       self				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_=True				,	)							->			Union[str, Any]:


    """simple docstring"""
    UpperCAmelCase  							=		latents.detach().requires_grad_()

    UpperCAmelCase  							=		self.scheduler.scale_model_input(a_				,	a_       )

    # predict the noise residual
    UpperCAmelCase  							=		self.unet(a_				,	a_				,	encoder_hidden_states=a_       ).sample

    if isinstance(self.scheduler				,	(PNDMScheduler, DDIMScheduler, DPMSolverMultistepScheduler)       ):
      UpperCAmelCase  							=		self.scheduler.alphas_cumprod[timestep]
      UpperCAmelCase  							=		1 - alpha_prod_t
      # compute predicted original sample from predicted noise also called
      # "predicted x_0" of formula (12) from https://arxiv.org/pdf/2010.02502.pdf
      UpperCAmelCase  							=		(latents - beta_prod_t ** 0.5 * noise_pred) / alpha_prod_t ** 0.5

      UpperCAmelCase  							=		torch.sqrt(a_       )
      UpperCAmelCase  							=		pred_original_sample * (fac) + latents * (1 - fac)
    elif isinstance(self.scheduler				,	a_       ):
      UpperCAmelCase  							=		self.scheduler.sigmas[index]
      UpperCAmelCase  							=		latents - sigma * noise_pred
    else:
      raise ValueError(F'''scheduler type {type(self.scheduler       )} not supported'''       )

    UpperCAmelCase  							=		1 / self.vae.config.scaling_factor * sample
    UpperCAmelCase  							=		self.vae.decode(a_       ).sample
    UpperCAmelCase  							=		(image / 2 + 0.5).clamp(0				,	1       )

    if use_cutouts:
      UpperCAmelCase  							=		self.make_cutouts(a_				,	a_       )
    else:
      UpperCAmelCase  							=		transforms.Resize(self.cut_out_size       )(a_       )
    UpperCAmelCase  							=		self.normalize(a_       ).to(latents.dtype       )

    UpperCAmelCase  							=		self.clip_model.get_image_features(a_       )
    UpperCAmelCase  							=		image_embeddings_clip / image_embeddings_clip.norm(p=2				,	dim=-1				,	keepdim=a_       )

    if use_cutouts:
      UpperCAmelCase  							=		spherical_dist_loss(a_				,	a_       )
      UpperCAmelCase  							=		dists.view([num_cutouts, sample.shape[0], -1]       )
      UpperCAmelCase  							=		dists.sum(2       ).mean(0       ).sum() * clip_guidance_scale
    else:
      UpperCAmelCase  							=		spherical_dist_loss(a_				,	a_       ).mean() * clip_guidance_scale

    UpperCAmelCase  							=		-torch.autograd.grad(a_				,	a_       )[0]

    if isinstance(self.scheduler				,	a_       ):
      UpperCAmelCase  							=		latents.detach() + grads * (sigma**2)
      UpperCAmelCase  							=		noise_pred_original
    else:
      UpperCAmelCase  							=		noise_pred_original - torch.sqrt(a_       ) * grads
    return noise_pred, latents





  @torch.no_grad()
  def __call__(       self				,	a_				,	a_ = 5_1_2				,	a_ = 5_1_2				,	a_ = 5_0				,	a_ = 7.5				,	a_ = 1				,	a_ = 0.0				,	a_ = 1_0_0				,	a_ = None				,	a_ = 4				,	a_ = True				,	a_ = None				,	a_ = None				,	a_ = "pil"				,	a_ = True				,	)							->			Optional[Any]:


    """simple docstring"""
    if isinstance(a_				,	a_       ):
      UpperCAmelCase  							=		1
    elif isinstance(a_				,	a_       ):
      UpperCAmelCase  							=		len(a_       )
    else:
      raise ValueError(F'''`prompt` has to be of type `str` or `list` but is {type(a_       )}'''       )

    if height % 8 != 0 or width % 8 != 0:
      raise ValueError(F'''`height` and `width` have to be divisible by 8 but are {height} and {width}.'''       )

    # get prompt text embeddings
    UpperCAmelCase  							=		self.tokenizer(
        a_				,	padding='max_length'				,	max_length=self.tokenizer.model_max_length				,	truncation=a_				,	return_tensors='pt'				,	)
    UpperCAmelCase  							=		self.text_encoder(text_input.input_ids.to(self.device       )       )[0]
    # duplicate text embeddings for each generation per prompt
    UpperCAmelCase  							=		text_embeddings.repeat_interleave(a_				,	dim=0       )

    if clip_guidance_scale > 0:
      if clip_prompt is not None:
        UpperCAmelCase  							=		self.tokenizer(
            a_				,	padding='max_length'				,	max_length=self.tokenizer.model_max_length				,	truncation=a_				,	return_tensors='pt'				,	).input_ids.to(self.device       )
      else:
        UpperCAmelCase  							=		text_input.input_ids.to(self.device       )
      UpperCAmelCase  							=		self.clip_model.get_text_features(a_       )
      UpperCAmelCase  							=		text_embeddings_clip / text_embeddings_clip.norm(p=2				,	dim=-1				,	keepdim=a_       )
      # duplicate text embeddings clip for each generation per prompt
      UpperCAmelCase  							=		text_embeddings_clip.repeat_interleave(a_				,	dim=0       )

    # here `guidance_scale` is defined analog to the guidance weight `w` of equation (2)
    # of the Imagen paper: https://arxiv.org/pdf/2205.11487.pdf . `guidance_scale = 1`
    # corresponds to doing no classifier free guidance.
    UpperCAmelCase  							=		guidance_scale > 1.0
    # get unconditional embeddings for classifier free guidance
    if do_classifier_free_guidance:
      UpperCAmelCase  							=		text_input.input_ids.shape[-1]
      UpperCAmelCase  							=		self.tokenizer(['']				,	padding='max_length'				,	max_length=a_				,	return_tensors='pt'       )
      UpperCAmelCase  							=		self.text_encoder(uncond_input.input_ids.to(self.device       )       )[0]
      # duplicate unconditional embeddings for each generation per prompt
      UpperCAmelCase  							=		uncond_embeddings.repeat_interleave(a_				,	dim=0       )

      # For classifier free guidance, we need to do two forward passes.
      # Here we concatenate the unconditional and text embeddings into a single batch
      # to avoid doing two forward passes
      UpperCAmelCase  							=		torch.cat([uncond_embeddings, text_embeddings]       )

    # get the initial random noise unless the user supplied it

    # Unlike in other pipelines, latents need to be generated in the target device
    # for 1-to-1 results reproducibility with the CompVis implementation.
    # However this currently doesn't work in `mps`.
    UpperCAmelCase  							=		(batch_size * num_images_per_prompt, self.unet.config.in_channels, height // 8, width // 8)
    UpperCAmelCase  							=		text_embeddings.dtype
    if latents is None:
      if self.device.type == "mps":
        # randn does not work reproducibly on mps
        UpperCAmelCase  							=		torch.randn(a_				,	generator=a_				,	device='cpu'				,	dtype=a_       ).to(
            self.device       )
      else:
        UpperCAmelCase  							=		torch.randn(a_				,	generator=a_				,	device=self.device				,	dtype=a_       )
    else:
      if latents.shape != latents_shape:
        raise ValueError(F'''Unexpected latents shape, got {latents.shape}, expected {latents_shape}'''       )
      UpperCAmelCase  							=		latents.to(self.device       )

    # set timesteps
    UpperCAmelCase  							=		'offset' in set(inspect.signature(self.scheduler.set_timesteps       ).parameters.keys()       )
    UpperCAmelCase  							=		{}
    if accepts_offset:
      UpperCAmelCase  							=		1

    self.scheduler.set_timesteps(a_				,	**a_       )

    # Some schedulers like PNDM have timesteps as arrays
    # It's more optimized to move all timesteps to correct device beforehand
    UpperCAmelCase  							=		self.scheduler.timesteps.to(self.device       )

    # scale the initial noise by the standard deviation required by the scheduler
    UpperCAmelCase  							=		latents * self.scheduler.init_noise_sigma

    # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature
    # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
    # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
    # and should be between [0, 1]
    UpperCAmelCase  							=		'eta' in set(inspect.signature(self.scheduler.step       ).parameters.keys()       )
    UpperCAmelCase  							=		{}
    if accepts_eta:
      UpperCAmelCase  							=		eta

    # check if the scheduler accepts generator
    UpperCAmelCase  							=		'generator' in set(inspect.signature(self.scheduler.step       ).parameters.keys()       )
    if accepts_generator:
      UpperCAmelCase  							=		generator

    for i, t in enumerate(self.progress_bar(a_       )       ):
      # expand the latents if we are doing classifier free guidance
      UpperCAmelCase  							=		torch.cat([latents] * 2       ) if do_classifier_free_guidance else latents
      UpperCAmelCase  							=		self.scheduler.scale_model_input(a_				,	a_       )

      # predict the noise residual
      UpperCAmelCase  							=		self.unet(a_				,	a_				,	encoder_hidden_states=a_       ).sample

      # perform classifier free guidance
      if do_classifier_free_guidance:
        UpperCAmelCase							, UpperCAmelCase  							=		noise_pred.chunk(2       )
        UpperCAmelCase  							=		noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

      # perform clip guidance
      if clip_guidance_scale > 0:
        UpperCAmelCase  							=		(
            text_embeddings.chunk(2       )[1] if do_classifier_free_guidance else text_embeddings
        )
        UpperCAmelCase							, UpperCAmelCase  							=		self.cond_fn(
            a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	a_				,	)

      # compute the previous noisy sample x_t -> x_t-1
      UpperCAmelCase  							=		self.scheduler.step(a_				,	a_				,	a_				,	**a_       ).prev_sample

    # scale and decode the image latents with vae
    UpperCAmelCase  							=		1 / self.vae.config.scaling_factor * latents
    UpperCAmelCase  							=		self.vae.decode(a_       ).sample

    UpperCAmelCase  							=		(image / 2 + 0.5).clamp(0				,	1       )
    UpperCAmelCase  							=		image.cpu().permute(0				,	2				,	3				,	1       ).numpy()

    if output_type == "pil":
      UpperCAmelCase  							=		self.numpy_to_pil(a_       )

    if not return_dict:
      return (image, None)

    return StableDiffusionPipelineOutput(images=a_				,	nsfw_content_detected=a_       )





'''simple docstring'''




import os
import pathlib
import tempfile
import uuid

import numpy as np

from ..utils import is_soundfile_availble, is_torch_available, is_vision_available, logging


_a			:	int								=  logging.get_logger(__name__)

if is_vision_available():
  import PIL.Image
  from PIL import Image
  from PIL.Image import Image as ImageType
else:
  _a			:	List[str]								=  object

if is_torch_available():
  import torch

if is_soundfile_availble():
  import soundfile as sf



class     lowercase_		:

  '''simple docstring'''





  def __init__(       self				,	a_       )							->			Dict:


    """simple docstring"""
    UpperCAmelCase  							=		value
  def __str__(       self       )							->			Any:


    """simple docstring"""
    return self.to_string()
  def   snake_case_     (       self       )							->			Union[str, Any]:


    """simple docstring"""
    logger.error(
        'This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable'       )
    return self._value





  def   snake_case_     (       self       )							->			str:


    """simple docstring"""
    logger.error(
        'This is a raw AgentType of unknown type. Display in notebooks and string conversion will be unreliable'       )
    return str(self._value       )




class     lowercase_		(						a	,    a	):

  '''simple docstring'''





  def   snake_case_     (       self       )							->			Dict:


    """simple docstring"""
    return self._value





  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    return self._value




class     lowercase_		(						a	,    a	):

  '''simple docstring'''





  def __init__(       self				,	a_       )							->			int:


    """simple docstring"""
    super().__init__(a_       )

    if not is_vision_available():
      raise ImportError('PIL must be installed in order to handle images.'       )

    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None

    if isinstance(a_				,	a_       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	(str, pathlib.Path)       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	torch.Tensor       ):
      UpperCAmelCase  							=		value
    else:
      raise ValueError(F'''Unsupported type for {self.__class__.__name__}: {type(a_       )}'''       )
  def   snake_case_     (       self				,	a_=None				,	a_=None       )							->			Dict:


    """simple docstring"""
    from IPython.display import Image, display

    display(Image(self.to_string()       )       )
  def   snake_case_     (       self       )							->			List[str]:


    """simple docstring"""
    if self._raw is not None:
      return self._raw

    if self._path is not None:
      UpperCAmelCase  							=		Image.open(self._path       )
      return self._raw





  def   snake_case_     (       self       )							->			List[Any]:


    """simple docstring"""
    if self._path is not None:
      return self._path

    if self._raw is not None:
      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.png'       )
      self._raw.save(self._path       )

      return self._path

    if self._tensor is not None:
      UpperCAmelCase  							=		self._tensor.cpu().detach().numpy()

      # There is likely simpler than load into image into save
      UpperCAmelCase  							=		Image.fromarray((array * 2_5_5).astype(np.uinta       )       )

      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.png'       )

      img.save(self._path       )

      return self._path




class     lowercase_		(						a	):

  '''simple docstring'''





  def __init__(       self				,	a_				,	a_=1_6_0_0_0       )							->			str:


    """simple docstring"""
    super().__init__(a_       )

    if not is_soundfile_availble():
      raise ImportError('soundfile must be installed in order to handle audio.'       )

    UpperCAmelCase  							=		None
    UpperCAmelCase  							=		None

    UpperCAmelCase  							=		samplerate

    if isinstance(a_				,	(str, pathlib.Path)       ):
      UpperCAmelCase  							=		value
    elif isinstance(a_				,	torch.Tensor       ):
      UpperCAmelCase  							=		value
    else:
      raise ValueError(F'''Unsupported audio type: {type(a_       )}'''       )
  def   snake_case_     (       self				,	a_=None				,	a_=None       )							->			Dict:


    """simple docstring"""
    from IPython.display import Audio, display

    display(Audio(self.to_string()				,	rate=self.samplerate       )       )
  def   snake_case_     (       self       )							->			List[str]:


    """simple docstring"""
    if self._tensor is not None:
      return self._tensor

    if self._path is not None:
      UpperCAmelCase							, UpperCAmelCase  							=		sf.read(self._path       )
      UpperCAmelCase  							=		torch.tensor(a_       )
      return self._tensor





  def   snake_case_     (       self       )							->			int:


    """simple docstring"""
    if self._path is not None:
      return self._path

    if self._tensor is not None:
      UpperCAmelCase  							=		tempfile.mkdtemp()
      UpperCAmelCase  							=		os.path.join(a_				,	str(uuid.uuida()       ) + '.wav'       )
      sf.write(self._path				,	self._tensor				,	samplerate=self.samplerate       )
      return self._path




_a			:	Optional[int]								=  {'text': AgentText, 'image': AgentImage, 'audio': AgentAudio}
_a			:	Tuple								=  {str: AgentText}

if is_vision_available():
  _a			:	List[str]								=  AgentImage


def  lowerCamelCase__						(  *SCREAMING_SNAKE_CASE :				List[str]    ,				**SCREAMING_SNAKE_CASE :				Tuple  ):
  UpperCAmelCase  							=		[(arg.to_raw() if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ) else arg) for arg in args]
  UpperCAmelCase  							=		{k: (v.to_raw() if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ) else v) for k, v in kwargs.items()}
  return args, kwargs



def  lowerCamelCase__						(  SCREAMING_SNAKE_CASE :				Tuple    ,				SCREAMING_SNAKE_CASE :				Optional[Any]=None  ):
  if isinstance(SCREAMING_SNAKE_CASE    ,				SCREAMING_SNAKE_CASE  ):
    UpperCAmelCase  							=		{}
    for i, (k, v) in enumerate(outputs.items()  ):
      if output_types is not None:
        # If the class has defined outputs, we can map directly according to the class definition
        if output_types[i] in AGENT_TYPE_MAPPING:
          UpperCAmelCase  							=		AGENT_TYPE_MAPPING[output_types[i]](SCREAMING_SNAKE_CASE  )
        else:
          UpperCAmelCase  							=		AgentType(SCREAMING_SNAKE_CASE  )

      else:
        # If the class does not have defined output, then we map according to the type
        for _k, _v in INSTANCE_TYPE_MAPPING.items():
          if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
            UpperCAmelCase  							=		_v(SCREAMING_SNAKE_CASE  )
        if k not in decoded_outputs:
          UpperCAmelCase  							=		AgentType[v]

  elif isinstance(SCREAMING_SNAKE_CASE    ,				(list, tuple)  ):
    UpperCAmelCase  							=		type(SCREAMING_SNAKE_CASE  )()
    for i, v in enumerate(SCREAMING_SNAKE_CASE  ):
      if output_types is not None:
        # If the class has defined outputs, we can map directly according to the class definition
        if output_types[i] in AGENT_TYPE_MAPPING:
          decoded_outputs.append(AGENT_TYPE_MAPPING[output_types[i]](SCREAMING_SNAKE_CASE  )  )
        else:
          decoded_outputs.append(AgentType(SCREAMING_SNAKE_CASE  )  )
      else:
        # If the class does not have defined output, then we map according to the type
        UpperCAmelCase  							=		False
        for _k, _v in INSTANCE_TYPE_MAPPING.items():
          if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
            decoded_outputs.append(_v(SCREAMING_SNAKE_CASE  )  )
            UpperCAmelCase  							=		True

        if not found:
          decoded_outputs.append(AgentType(SCREAMING_SNAKE_CASE  )  )

  else:
    if output_types[0] in AGENT_TYPE_MAPPING:
      # If the class has defined outputs, we can map directly according to the class definition
      UpperCAmelCase  							=		AGENT_TYPE_MAPPING[output_types[0]](SCREAMING_SNAKE_CASE  )

    else:
      # If the class does not have defined output, then we map according to the type
      for _k, _v in INSTANCE_TYPE_MAPPING.items():
        if isinstance(SCREAMING_SNAKE_CASE    ,				_k  ):
          return _v(SCREAMING_SNAKE_CASE  )
      return AgentType(SCREAMING_SNAKE_CASE  )

  return decoded_outputs


