

from collections.abc import Sequence
from queue import Queue

class 						__UpperCamelCase    :




	def __init__(		self	:						List[Any]		,	lowerCAmelCase	:						List[Any]		,	lowerCAmelCase	:						Union[str, Any]		,	lowerCAmelCase	:						Union[str, Any]		,	lowerCAmelCase	:						Optional[int]=None		,	lowerCAmelCase	:						str=None							):


		'''simple docstring'''







		UpperCAmelCase_       				=			start
		UpperCAmelCase_       				=			end
		UpperCAmelCase_       				=			val
		UpperCAmelCase_       				=			(start + end) // 2
		UpperCAmelCase_       				=			left
		UpperCAmelCase_       				=			right




	def __repr__(		self	:						Tuple							):


		'''simple docstring'''







		return F"SegmentTreeNode(start={self.start}, end={self.end}, val={self.val})"


class 						__UpperCamelCase    :




	def __init__(		self	:						Union[str, Any]		,	lowerCAmelCase	:						Sequence		,	lowerCAmelCase	:						List[Any]							):


		'''simple docstring'''







		UpperCAmelCase_       				=			collection
		UpperCAmelCase_       				=			function
		if self.collection:
			UpperCAmelCase_       				=			self._build_tree(0		,	len(lowerCAmelCase							) - 1							)




	def       __A					(		self	:						Dict		,	lowerCAmelCase	:						int		,	lowerCAmelCase	:						List[Any]							):


		'''simple docstring'''







		self._update_tree(self.root		,	lowerCAmelCase		,	lowerCAmelCase							)




	def       __A					(		self	:						List[Any]		,	lowerCAmelCase	:						Optional[Any]		,	lowerCAmelCase	:						Any							):


		'''simple docstring'''







		return self._query_range(self.root		,	lowerCAmelCase		,	lowerCAmelCase							)




	def       __A					(		self	:						Optional[Any]		,	lowerCAmelCase	:						Any		,	lowerCAmelCase	:						List[str]							):


		'''simple docstring'''







		if start == end:
			return SegmentTreeNode(lowerCAmelCase		,	lowerCAmelCase		,	self.collection[start]							)
		UpperCAmelCase_       				=			(start + end) // 2
		UpperCAmelCase_       				=			self._build_tree(lowerCAmelCase		,	lowerCAmelCase							)
		UpperCAmelCase_       				=			self._build_tree(mid + 1		,	lowerCAmelCase							)
		return SegmentTreeNode(lowerCAmelCase		,	lowerCAmelCase		,	self.fn(left.val		,	right.val							)		,	lowerCAmelCase		,	lowerCAmelCase							)




	def       __A					(		self	:						Tuple		,	lowerCAmelCase	:						Optional[Any]		,	lowerCAmelCase	:						List[Any]		,	lowerCAmelCase	:						Dict							):


		'''simple docstring'''







		if node.start == i and node.end == i:
			UpperCAmelCase_       				=			val
			return
		if i <= node.mid:
			self._update_tree(node.left		,	lowerCAmelCase		,	lowerCAmelCase							)
		else:
			self._update_tree(node.right		,	lowerCAmelCase		,	lowerCAmelCase							)
		UpperCAmelCase_       				=			self.fn(node.left.val		,	node.right.val							)




	def       __A					(		self	:						int		,	lowerCAmelCase	:						Dict		,	lowerCAmelCase	:						Any		,	lowerCAmelCase	:						List[Any]							):


		'''simple docstring'''







		if node.start == i and node.end == j:
			return node.val

		if i <= node.mid:
			if j <= node.mid:
				# range in left child tree
				return self._query_range(node.left		,	lowerCAmelCase		,	lowerCAmelCase							)
			else:
				# range in left child tree and right child tree
				return self.fn(
				    self._query_range(node.left		,	lowerCAmelCase		,	node.mid							)		,	self._query_range(node.right		,	node.mid + 1		,	lowerCAmelCase							)		,	)
		else:
			# range in right child tree
			return self._query_range(node.right		,	lowerCAmelCase		,	lowerCAmelCase							)




	def       __A					(		self	:						str							):


		'''simple docstring'''







		if self.root is not None:
			UpperCAmelCase_       				=			Queue()
			queue.put(self.root							)
			while not queue.empty():
				UpperCAmelCase_       				=			queue.get()
				yield node

				if node.left is not None:
					queue.put(node.left							)

				if node.right is not None:
					queue.put(node.right							)


if __name__ == "__main__":
				import operator

				for fn in [operator.add, max, min]:
								print("""*""" * 50)
								_a:    Tuple 							=			SegmentTree([2, 1, 5, 3, 4], fn)
								for node in arr.traverse():
												print(node)
								print()

								arr.update(1, 5)
								for node in arr.traverse():
												print(node)
								print()

								print(arr.query_range(3, 4))  # 7
								print(arr.query_range(2, 2))  # 5
								print(arr.query_range(1, 3))  # 13
								print()


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:    Optional[int] 							=			"""CompVis/stable-diffusion-v1-1"""
_a:    Dict 							=			"""CompVis/stable-diffusion-v1-2"""
_a:    Optional[Any] 							=			"""CompVis/stable-diffusion-v1-3"""
_a:    str 							=			"""CompVis/stable-diffusion-v1-4"""

class 						__UpperCamelCase    (					lowercase						):




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


		'''simple docstring'''







		super()._init_()

		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			StableDiffusionPipeline.from_pretrained(lowerCAmelCase							)
		UpperCAmelCase_       				=			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       __A					(		self	:						Tuple							):


		'''simple docstring'''







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




	def       __A					(		self	:						Dict		,	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
			UpperCAmelCase_       				=			self.unet.config.attention_head_dim // 2
		self.unet.set_attention_slice(lowerCAmelCase							)




	def       __A					(		self	:						int							):


		'''simple docstring'''







		self.enable_attention_slicing(lowerCAmelCase							)




	@torch.no_grad()
	def       __A					(		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       __A					(		self	:						Dict		,	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	:						List[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       __A					(		self	:						List[str]		,	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       __A					(		self	:						Dict		,	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       __A					(		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	:						Union[str, Any]		,	):


		'''simple docstring'''







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