






'''simple docstring'''

import numpy as np
from numpy import ndarray
from scipy.optimize import Bounds, LinearConstraint, minimize







def 					lowercase__				(   __UpperCamelCase					)->				float:
      return np.dot(__UpperCamelCase	,		__UpperCamelCase					)






class        a_		:

  def __init__(   self    ,	*,
      _SCREAMING_SNAKE_CASE = np.inf    ,	_SCREAMING_SNAKE_CASE = "linear"    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	)						->   None:



        """simple docstring"""






        UpperCamelCase       						=      regularization
        UpperCamelCase       						=      gamma
        if kernel == "linear":
              UpperCamelCase       						=      self.__linear
        elif kernel == "rbf":
              if self.gamma == 0:
                    raise ValueError("""rbf kernel requires gamma"""   )
              if not isinstance(self.gamma    ,	(float, int)   ):
                    raise ValueError("""gamma must be float or int"""   )
              if not self.gamma > 0:
                    raise ValueError("""gamma must be > 0"""   )
              UpperCamelCase       						=      self.__rbf
              # in the future, there could be a default value like in sklearn
              # sklear: def_gamma = 1/(n_features * X.var()) (wiki)
              # previously it was 1/(n_features)
        else:
              UpperCamelCase       						=      F"Unknown kernel: {kernel}"
              raise ValueError(_SCREAMING_SNAKE_CASE   )

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   float:



        """simple docstring"""






        return np.dot(_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   float:



        """simple docstring"""






        return np.exp(-(self.gamma * norm_squared(vectora - vectora   ))   )

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE   )						->   None:



        """simple docstring"""






        UpperCamelCase       						=      observations
        UpperCamelCase       						=      classes

        # using Wolfe's Dual to calculate w.
        # Primal problem: minimize 1/2*norm_squared(w)
        #   constraint: yn(w . xn + b) >= 1
        #
        # With l a vector
        # Dual problem: maximize sum_n(ln) -
        #       1/2 * sum_n(sum_m(ln*lm*yn*ym*xn . xm))
        #   constraint: self.C >= ln >= 0
        #           and sum_n(ln*yn) = 0
        # Then we get w using w = sum_n(ln*yn*xn)
        # At the end we can get b ~= mean(yn - w . xn)
        #
        # Since we use kernels, we only need l_star to calculate b
        # and to classify observations

        ((UpperCamelCase)						,)       						=      np.shape(_SCREAMING_SNAKE_CASE   )

        def to_minimize(_SCREAMING_SNAKE_CASE   ) -> float:
              UpperCamelCase       						=      0
              ((UpperCamelCase)						,)       						=      np.shape(_SCREAMING_SNAKE_CASE   )
              for i in range(_SCREAMING_SNAKE_CASE   ):
                    for j in range(_SCREAMING_SNAKE_CASE   ):
                          s += (
                              candidate[i]
                              * candidate[j]
                              * classes[i]
                              * classes[j]
                              * self.kernel(observations[i]    ,	observations[j]   )
                          )
              return 1 / 2 * s - sum(_SCREAMING_SNAKE_CASE   )

        UpperCamelCase       						=      LinearConstraint(_SCREAMING_SNAKE_CASE    ,	0    ,	0   )
        UpperCamelCase       						=      Bounds(0    ,	self.regularization   )

        UpperCamelCase       						=      minimize(
            _SCREAMING_SNAKE_CASE    ,	np.ones(_SCREAMING_SNAKE_CASE   )    ,	bounds=_SCREAMING_SNAKE_CASE    ,	constraints=[ly_contraint]   ).x
        UpperCamelCase       						=      l_star

        # calculating mean offset of separation plane to points
        UpperCamelCase       						=      0
        for i in range(_SCREAMING_SNAKE_CASE   ):
              for j in range(_SCREAMING_SNAKE_CASE   ):
                    s += classes[i] - classes[i] * self.optimum[i] * self.kernel(
                        observations[i]    ,	observations[j]   )
        UpperCamelCase       						=      s / n


  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE   )						->   int:



        """simple docstring"""






        UpperCamelCase       						=      sum(
            self.optimum[n]
            * self.classes[n]
            * self.kernel(self.observations[n]    ,	_SCREAMING_SNAKE_CASE   )
            for n in range(len(self.classes   )   )   )
        return 1 if s + self.offset >= 0 else -1


if __name__ == "__main__":
     import doctest

     doctest.testmod()








'''simple docstring'''

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

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-1'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-2'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-3'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-4'






class        a_		(    lowerCamelCase       ):

  def __init__(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = True    ,	)						->   Any:



        """simple docstring"""






        super()._init_()

        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline(
            vae=_SCREAMING_SNAKE_CASE    ,	text_encoder=_SCREAMING_SNAKE_CASE    ,	tokenizer=_SCREAMING_SNAKE_CASE    ,	unet=_SCREAMING_SNAKE_CASE    ,	scheduler=_SCREAMING_SNAKE_CASE    ,	safety_checker=_SCREAMING_SNAKE_CASE    ,	feature_extractor=_SCREAMING_SNAKE_CASE    ,	requires_safety_checker=_SCREAMING_SNAKE_CASE    ,	)

        self.register_modules(pipelinea=self.pipea    ,	pipelinea=self.pipea    ,	pipelinea=self.pipea    ,	pipelinea=self.pipea   )

  @property
  def 						A__		(   self   )						->   Dict[str, Any]:



        """simple docstring"""






        return {k: getattr(self    ,	_SCREAMING_SNAKE_CASE   ) for k in self.config.keys() if not k.startswith("""_"""   )}

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE = "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(_SCREAMING_SNAKE_CASE   )

  def 						A__		(   self   )						->   Tuple:



        """simple docstring"""






        self.enable_attention_slicing(_SCREAMING_SNAKE_CASE   )

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[Any]:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[Any]:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   Dict:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   str:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)


  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[str]:



        """simple docstring"""






        UpperCamelCase       						=      """cuda""" if torch.cuda.is_available() else """cpu"""
        self.to(_SCREAMING_SNAKE_CASE   )

        # Checks if the height and width are divisible by 8 or not
        if height % 8 != 0 or width % 8 != 0:
              raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}."   )

        # Get first result from Stable Diffusion Checkpoint v1.1
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.2
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.3
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.4
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
        return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]   )
