
'''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


A__   :		int 		='CompVis/stable-diffusion-v1-1'
A__   :		Tuple 		='CompVis/stable-diffusion-v1-2'
A__   :		Optional[Any] 		='CompVis/stable-diffusion-v1-3'
A__   :		Optional[Any] 		='CompVis/stable-diffusion-v1-4'







class  __A				( _SCREAMING_SNAKE_CASE			):
 def __init__(   self			:    List[str]				,   lowerCamelCase			:    AutoencoderKL				,   lowerCamelCase			:    CLIPTextModel				,   lowerCamelCase			:    CLIPTokenizer				,   lowerCamelCase			:    UNetaDConditionModel				,   lowerCamelCase			:    Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]				,   lowerCamelCase			:    StableDiffusionSafetyChecker				,   lowerCamelCase			:    CLIPImageProcessor				,   lowerCamelCase			:    bool = True				,   ):



   """simple docstring"""





   super()._init_()

   __A		:     List[Any]												=		StableDiffusionPipeline.from_pretrained(lowerCamelCase       )
   __A		:     Union[str, Any]												=		StableDiffusionPipeline.from_pretrained(lowerCamelCase       )
   __A		:     Any												=		StableDiffusionPipeline.from_pretrained(lowerCamelCase       )
   __A		:     List[Any]												=		StableDiffusionPipeline(
       vae=lowerCamelCase				,   text_encoder=lowerCamelCase				,   tokenizer=lowerCamelCase				,   unet=lowerCamelCase				,   scheduler=lowerCamelCase				,   safety_checker=lowerCamelCase				,   feature_extractor=lowerCamelCase				,   requires_safety_checker=lowerCamelCase				,   )

   self.register_modules(pipelinea=self.pipea				,   pipelinea=self.pipea				,   pipelinea=self.pipea				,   pipelinea=self.pipea       )
 @property
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   return {k: getattr(self				,   lowerCamelCase       ) for k in self.config.keys() if not k.startswith("""_"""       )}
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Optional[Union[str, int]] = "auto"       ):



   """simple docstring"""





   if slice_size == "auto":
     # half the attention head size is usually a good trade-off between
     # speed and memory
     __A		:     List[Any]												=		self.unet.config.attention_head_dim // 2
   self.unet.set_attention_slice(lowerCamelCase       )
 def 		lowercase_(   self			:    Tuple       ):



   """simple docstring"""





   self.enable_attention_slicing(lowerCamelCase       )
 @torch.no_grad()
 def 		lowercase_(   self			:    Optional[Any]				,   lowerCamelCase			:    Union[str, List[str]]				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 50				,   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				,   **lowerCamelCase			:    Union[str, Any]				,   ):



   """simple docstring"""





   return self.pipea(
       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				,   **lowerCamelCase				,   )
 @torch.no_grad()
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Union[str, List[str]]				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 50				,   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				,   **lowerCamelCase			:    str				,   ):



   """simple docstring"""





   return self.pipea(
       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				,   **lowerCamelCase				,   )
 @torch.no_grad()
 def 		lowercase_(   self			:    Any				,   lowerCamelCase			:    Union[str, List[str]]				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 50				,   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				,   **lowerCamelCase			:    Optional[int]				,   ):



   """simple docstring"""





   return self.pipea(
       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				,   **lowerCamelCase				,   )
 @torch.no_grad()
 def 		lowercase_(   self			:    List[str]				,   lowerCamelCase			:    Union[str, List[str]]				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 50				,   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				,   **lowerCamelCase			:    Any				,   ):



   """simple docstring"""





   return self.pipea(
       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				,   **lowerCamelCase				,   )






 @torch.no_grad()
 def 		lowercase_(   self			:    Dict				,   lowerCamelCase			:    Union[str, List[str]]				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 5_12				,   lowerCamelCase			:    int = 50				,   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				,   **lowerCamelCase			:    str				,   ):



   """simple docstring"""





   __A		:     Optional[int]												=		"""cuda""" if torch.cuda.is_available() else """cpu"""
   self.to(lowerCamelCase       )

   # 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
   __A		:     List[Any]												=		self.textaimg_sda_a(
       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				,   **lowerCamelCase				,   )

   # Get first result from Stable Diffusion Checkpoint v1.2
   __A		:     Optional[int]												=		self.textaimg_sda_a(
       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				,   **lowerCamelCase				,   )

   # Get first result from Stable Diffusion Checkpoint v1.3
   __A		:     Dict												=		self.textaimg_sda_a(
       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				,   **lowerCamelCase				,   )

   # Get first result from Stable Diffusion Checkpoint v1.4
   __A		:     Dict												=		self.textaimg_sda_a(
       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				,   **lowerCamelCase				,   )

   # 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]]       )





'''simple docstring'''
def        A_     (						__SCREAMING_SNAKE_CASE						:  int )				->       None:





  """simple docstring"""

  __A		:     Tuple												=		generate_pascal_triangle(__SCREAMING_SNAKE_CASE )
  for row_idx in range(__SCREAMING_SNAKE_CASE ):
    # Print left spaces
    for _ in range(num_rows - row_idx - 1 ):
      print(end=""" """ )
    # Print row values
    for col_idx in range(row_idx + 1 ):
      if col_idx != row_idx:
        print(triangle[row_idx][col_idx]			,     end=""" """ )
      else:
        print(triangle[row_idx][col_idx]			,     end="""""" )
    print()







def        A_     (						__SCREAMING_SNAKE_CASE						:  int )				->       list[list[int]]:





  """simple docstring"""

  if not isinstance(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE ):
    raise TypeError("""The input value of 'num_rows' should be 'int'""" )

  if num_rows == 0:
    return []
  elif num_rows < 0:
    raise ValueError(
        """The input value of 'num_rows' should be greater than or equal to 0""" )

  __A		:     list[list[int]]												=		[]
  for current_row_idx in range(__SCREAMING_SNAKE_CASE ):
    __A		:     Tuple												=		populate_current_row(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE )
    triangle.append(__SCREAMING_SNAKE_CASE )
  return triangle







def        A_     (						__SCREAMING_SNAKE_CASE						:  list[list[int]]			,     __SCREAMING_SNAKE_CASE						:  int )				->       list[int]:





  """simple docstring"""

  __A		:     Union[str, Any]												=		[-1] * (current_row_idx + 1)
  # first and last elements of current row are equal to 1
  __A							,					__A		:     Tuple												=		1, 1
  for current_col_idx in range(1			,     __SCREAMING_SNAKE_CASE ):
    calculate_current_element(
        __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE )
  return current_row







def        A_     (						__SCREAMING_SNAKE_CASE						:  list[list[int]]			,     __SCREAMING_SNAKE_CASE						:  list[int]			,     __SCREAMING_SNAKE_CASE						:  int			,     __SCREAMING_SNAKE_CASE						:  int			,     )				->       None:





  """simple docstring"""

  __A		:     str												=		triangle[current_row_idx - 1][current_col_idx - 1]
  __A		:     Union[str, Any]												=		triangle[current_row_idx - 1][current_col_idx]
  __A		:     Any												=		above_to_left_elt + above_to_right_elt







def        A_     (						__SCREAMING_SNAKE_CASE						:  int )				->       list[list[int]]:





  """simple docstring"""

  if not isinstance(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE ):
    raise TypeError("""The input value of 'num_rows' should be 'int'""" )

  if num_rows == 0:
    return []
  elif num_rows < 0:
    raise ValueError(
        """The input value of 'num_rows' should be greater than or equal to 0""" )

  __A		:     list[list[int]]												=		[[1]]

  for row_index in range(1			,     __SCREAMING_SNAKE_CASE ):
    __A		:     Optional[Any]												=		[0] + result[-1] + [0]
    __A		:     Optional[Any]												=		row_index + 1
    # Calculate the number of distinct elements in a row
    __A		:     int												=		sum(divmod(__SCREAMING_SNAKE_CASE			,     2 ) )
    __A		:     str												=		[
        temp_row[i - 1] + temp_row[i] for i in range(1			,     distinct_elements + 1 )
    ]
    __A		:     Tuple												=		row_first_half[: (row_index + 1) // 2]
    row_second_half.reverse()
    __A		:     Any												=		row_first_half + row_second_half
    result.append(__SCREAMING_SNAKE_CASE )

  return result







def        A_     (						)				->       None:





  """simple docstring"""

  from collections.abc import Callable
  from timeit import timeit

  def benchmark_a_function(__SCREAMING_SNAKE_CASE						:  Callable			,     __SCREAMING_SNAKE_CASE						:  int ) -> None:
    __A		:     List[Any]												=		F"{func.__name__}({value})"
    __A		:     Dict												=		timeit(F"__main__.{call}"			,     setup="""import __main__""" )
    # print(f"{call:38} = {func(value)} -- {timing:.4f} seconds")
    print(F"{call:38} -- {timing:.4f} seconds" )

  for value in range(15 ):  # (1, 7, 14):
    for func in (generate_pascal_triangle, generate_pascal_triangle_optimized):
      benchmark_a_function(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE )
    print()


if __name__ == "__main__":
     import doctest

     doctest.testmod()
     benchmark()



