



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


snake_case__					:     int    		=   """CompVis/stable-diffusion-v1-1"""
snake_case__					:     Dict    		=   """CompVis/stable-diffusion-v1-2"""
snake_case__					:     Dict    		=   """CompVis/stable-diffusion-v1-3"""
snake_case__					:     Optional[Any]    		=   """CompVis/stable-diffusion-v1-4"""

class   _A   ( _lowercase	):


						'''simple docstring'''





						def __init__(	self	:							Tuple							,      lowerCamelCase	:							AutoencoderKL							,      lowerCamelCase	:							CLIPTextModel							,      lowerCamelCase	:							CLIPTokenizer							,      lowerCamelCase	:							UNetaDConditionModel							,      lowerCamelCase	:							Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]							,      lowerCamelCase	:							StableDiffusionSafetyChecker							,      lowerCamelCase	:							CLIPImageProcessor							,      lowerCamelCase	:							bool = True							,      ):






							'''simple docstring'''



							super()._init_()

							__lowercase		       =			StableDiffusionPipeline.from_pretrained(lowerCamelCase	)
							__lowercase		       =			StableDiffusionPipeline.from_pretrained(lowerCamelCase	)
							__lowercase		       =			StableDiffusionPipeline.from_pretrained(lowerCamelCase	)
							__lowercase		       =			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     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



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



						def     _snake_case	(	self	:							Optional[Any]							,      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
								__lowercase		       =			self.unet.config.attention_head_dim // 2
							self.unet.set_attention_slice(lowerCamelCase	)



						def     _snake_case	(	self	:							Tuple	):






							'''simple docstring'''



							self.enable_attention_slicing(lowerCamelCase	)



						@torch.no_grad()
						def     _snake_case	(	self	:							Union[str, Any]							,      lowerCamelCase	:							Union[str, List[str]]							,      lowerCamelCase	:							int = 512							,      lowerCamelCase	:							int = 512							,      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	:							Tuple							,      ):






							'''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     _snake_case	(	self	:							int							,      lowerCamelCase	:							Union[str, List[str]]							,      lowerCamelCase	:							int = 512							,      lowerCamelCase	:							int = 512							,      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     _snake_case	(	self	:							List[Any]							,      lowerCamelCase	:							Union[str, List[str]]							,      lowerCamelCase	:							int = 512							,      lowerCamelCase	:							int = 512							,      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     _snake_case	(	self	:							Any							,      lowerCamelCase	:							Union[str, List[str]]							,      lowerCamelCase	:							int = 512							,      lowerCamelCase	:							int = 512							,      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[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     _snake_case	(	self	:							Optional[Any]							,      lowerCamelCase	:							Union[str, List[str]]							,      lowerCamelCase	:							int = 512							,      lowerCamelCase	:							int = 512							,      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[Any]							,      ):






							'''simple docstring'''



							__lowercase		       =			"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
							__lowercase		       =			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
							__lowercase		       =			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
							__lowercase		       =			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
							__lowercase		       =			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]]	)








from __future__ import annotations

from collections import namedtuple
from dataclasses import dataclass

@dataclass
class   _A   :


						'''simple docstring'''





						_snake_case			:     int
						_snake_case			:     TreeNode | None      							=      None
						_snake_case			:     TreeNode | None      							=      None

snake_case__					:     Dict    		=   namedtuple("""CoinsDistribResult""", """moves excess""")



def    snake_case_				(    _SCREAMING_SNAKE_CASE   ):

	if root is None:
		return 0

	# Validation
	def count_nodes(_SCREAMING_SNAKE_CASE   ) -> int:
		if node is None:
			return 0

		return count_nodes(node.left   ) + count_nodes(node.right   ) + 1

	def count_coins(_SCREAMING_SNAKE_CASE   ) -> int:
		if node is None:
			return 0

		return count_coins(node.left   ) + count_coins(node.right   ) + node.data

	if count_nodes(_SCREAMING_SNAKE_CASE   ) != count_coins(_SCREAMING_SNAKE_CASE   ):
		raise ValueError("The nodes number should be same as the number of coins"   )

	# Main calculation
	def get_distrib(_SCREAMING_SNAKE_CASE   ) -> CoinsDistribResult:

		if node is None:
			return CoinsDistribResult(0  ,	1   )

		__lowercase       ,    __lowercase		       =			get_distrib(node.left   )
		__lowercase       ,    __lowercase		       =			get_distrib(node.right   )

		__lowercase		       =			1 - left_distrib_excess
		__lowercase		       =			1 - right_distrib_excess

		__lowercase		       =			(
		    left_distrib_moves
		    + right_distrib_moves
		    + abs(_SCREAMING_SNAKE_CASE   )
		    + abs(_SCREAMING_SNAKE_CASE   )
		)
		__lowercase		       =			node.data - coins_to_left - coins_to_right

		return CoinsDistribResult(_SCREAMING_SNAKE_CASE  ,	_SCREAMING_SNAKE_CASE   )

	return get_distrib(_SCREAMING_SNAKE_CASE   )[0]


if __name__ == "__main__":
	import doctest

	doctest.testmod()



