






"""simple docstring"""





class       __A		:

					def __init__(			self  :      Optional[int]     ,		__snake_case  :      int     ,		__snake_case  :      Dict     ,		__snake_case  :      Optional[int]					)			->							List[str]:
											__magic_name__:	Any							=       None
											__magic_name__:	int							=       None
											__magic_name__:	Tuple							=       graph

											self._normalize_graph(__snake_case     ,		__snake_case					)
											__magic_name__:	List[Any]							=       len(__snake_case					)
											__magic_name__:	Optional[int]							=       None

					def   lowerCamelCase__   (			self  :      Optional[int]     ,		__snake_case  :      List[Any]     ,		__snake_case  :      Tuple					)			->							Optional[int]:
											if sources is int:
																	__magic_name__:	List[Any]							=       [sources]
											if sinks is int:
																	__magic_name__:	Dict							=       [sinks]

											if len(__snake_case					) == 0 or len(__snake_case					) == 0:
																	return

											__magic_name__:	Optional[int]							=       sources[0]
											__magic_name__:	str							=       sinks[0]

											# make fake vertex if there are more
											# than one source or sink
											if len(__snake_case					) > 1 or len(__snake_case					) > 1:
																	__magic_name__:	Tuple							=       0
																	for i in sources:
																							max_input_flow += sum(self.graph[i]					)

																	__magic_name__:	Optional[int]							=       len(self.graph					) + 1
																	for room in self.graph:
																							room.insert(0     ,		0					)
																	self.graph.insert(0     ,		[0] * size					)
																	for i in sources:
																							__magic_name__:	int							=       max_input_flow
																	__magic_name__:	Tuple							=       0

																	__magic_name__:	Dict							=       len(self.graph					) + 1
																	for room in self.graph:
																							room.append(0					)
																	self.graph.append([0] * size					)
																	for i in sinks:
																							__magic_name__:	int							=       max_input_flow
																	__magic_name__:	Optional[int]							=       size - 1

					def   lowerCamelCase__   (			self  :      List[str]					)			->							Union[str, Any]:
											if self.maximum_flow_algorithm is None:
																	raise Exception("""You need to set maximum flow algorithm before."""					)
											if self.source_index is None or self.sink_index is None:
																	return 0

											self.maximum_flow_algorithm.execute()
											return self.maximum_flow_algorithm.getMaximumFlow()






					def   lowerCamelCase__   (			self  :      List[Any]     ,		__snake_case  :      int					)			->							Any:
											__magic_name__:	Union[str, Any]							=       algorithm(self					)



class       __A		:

					def __init__(			self  :      List[Any]     ,		__snake_case  :      Any					)			->							Union[str, Any]:
											__magic_name__:	Tuple							=       flow_network
											__magic_name__:	str							=       flow_network.verticesCount
											__magic_name__:	Dict							=       flow_network.sourceIndex
											__magic_name__:	List[str]							=       flow_network.sinkIndex
											# it's just a reference, so you shouldn't change
											# it in your algorithms, use deep copy before doing that
											__magic_name__:	List[str]							=       flow_network.graph
											__magic_name__:	Union[str, Any]							=       False

					def   lowerCamelCase__   (			self  :      Dict					)			->							Any:
											if not self.executed:
																	self._algorithm()
																	__magic_name__:	int							=       True






					def   lowerCamelCase__   (			self  :      Optional[Any]					)			->							Dict:
											pass



class       __A		(   SCREAMING_SNAKE_CASE_			):

					def __init__(			self  :      Any     ,		__snake_case  :      Any					)			->							Tuple:
											super().__init__(__snake_case					)
											# use this to save your result
											__magic_name__:	Optional[int]							=       -1






					def   lowerCamelCase__   (			self  :      Dict					)			->							List[Any]:
											if not self.executed:
																	raise Exception("""You should execute algorithm before using its result!"""					)

											return self.maximum_flow



class       __A		(   SCREAMING_SNAKE_CASE_			):

					def __init__(			self  :      Union[str, Any]     ,		__snake_case  :      Dict					)			->							List[Any]:
											super().__init__(__snake_case					)

											__magic_name__:	Dict							=       [[0] * self.verticies_count for i in range(self.verticies_count					)]

											__magic_name__:	str							=       [0] * self.verticies_count
											__magic_name__:	Dict							=       [0] * self.verticies_count

					def   lowerCamelCase__   (			self  :      Any					)			->							List[str]:
											__magic_name__:	Optional[Any]							=       self.verticies_count

											# push some substance to graph
											for nextvertex_index, bandwidth in enumerate(self.graph[self.source_index]					):
																	self.preflow[self.source_index][nextvertex_index] += bandwidth
																	self.preflow[nextvertex_index][self.source_index] -= bandwidth
																	self.excesses[nextvertex_index] += bandwidth

											# Relabel-to-front selection rule
											__magic_name__:	Optional[Any]							=       [
											    i
											    for i in range(self.verticies_count					)
											    if i != self.source_index and i != self.sink_index
											]

											# move through list
											__magic_name__:	Union[str, Any]							=       0
											while i < len(__snake_case					):
																	__magic_name__:	Tuple							=       vertices_list[i]
																	__magic_name__:	str							=       self.heights[vertex_index]
																	self.process_vertex(__snake_case					)
																	if self.heights[vertex_index] > previous_height:
																							# if it was relabeled, swap elements
																							# and start from 0 index
																							vertices_list.insert(0     ,		vertices_list.pop(__snake_case					)					)
																							__magic_name__:	List[Any]							=       0
																	else:
																							i += 1

											__magic_name__:	List[str]							=       sum(self.preflow[self.source_index]					)

					def   lowerCamelCase__   (			self  :      List[Any]     ,		__snake_case  :      str					)			->							Optional[int]:
											while self.excesses[vertex_index] > 0:
																	for neighbour_index in range(self.verticies_count					):
																							# if it's neighbour and current vertex is higher
																							if (
																							    self.graph[vertex_index][neighbour_index]
																							    - self.preflow[vertex_index][neighbour_index]
																							    > 0
																							    and self.heights[vertex_index] > self.heights[neighbour_index]
																							):
																													self.push(__snake_case     ,		__snake_case					)

																	self.relabel(__snake_case					)

					def   lowerCamelCase__   (			self  :      Optional[Any]     ,		__snake_case  :      int     ,		__snake_case  :      Optional[int]					)			->							str:
											__magic_name__:	Dict							=       min(
											    self.excesses[from_index]     ,		self.graph[from_index][to_index] - self.preflow[from_index][to_index]     ,		)
											self.preflow[from_index][to_index] += preflow_delta
											self.preflow[to_index][from_index] -= preflow_delta
											self.excesses[from_index] -= preflow_delta
											self.excesses[to_index] += preflow_delta






					def   lowerCamelCase__   (			self  :      List[Any]     ,		__snake_case  :      List[str]					)			->							Union[str, Any]:
											__magic_name__:	List[Any]							=       None
											for to_index in range(self.verticies_count					):
																	if (
																	    self.graph[vertex_index][to_index]
																	    - self.preflow[vertex_index][to_index]
																	    > 0
																	) and (min_height is None or self.heights[to_index] < min_height):
																							__magic_name__:	Optional[int]							=       self.heights[to_index]

											if min_height is not None:
																	__magic_name__:	List[Any]							=       min_height + 1


if __name__ == "__main__":
							__lowerCamelCase							=  [0]
							__lowerCamelCase							=  [3]
							# graph = [
							#     [0, 0, 4, 6, 0, 0],
							#     [0, 0, 5, 2, 0, 0],
							#     [0, 0, 0, 0, 4, 4],
							#     [0, 0, 0, 0, 6, 6],
							#     [0, 0, 0, 0, 0, 0],
							#     [0, 0, 0, 0, 0, 0],
							# ]
							__lowerCamelCase							=  [[0, 7, 0, 0], [0, 0, 6, 0], [0, 0, 0, 8], [9, 0, 0, 0]]

							# prepare our network
							__lowerCamelCase							=  FlowNetwork(graph, entrances, exits)
							# set algorithm
							flow_network.set_maximum_flow_algorithm(PushRelabelExecutor)
							# and calculate
							__lowerCamelCase							=  flow_network.find_maximum_flow()

							print(f'''maximum flow is {maximum_flow}''')











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


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







class 		_A						( UpperCAmelCase_ ):

  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_()

     __UpperCamelCase	:			Optional[Any]					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			int					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			List[str]					=					StableDiffusionPipeline.from_pretrained(lowerCamelCase__			)
     __UpperCamelCase	:			str					=					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						:	Optional[Any]			):
     """simple docstring"""

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

  def    a					(    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
        __UpperCamelCase	:			Dict					=					self.unet.config.attention_head_dim // 2
     self.unet.set_attention_slice(lowerCamelCase__			)

  def    a					(    self						:	str			):
     """simple docstring"""

     self.enable_attention_slicing(lowerCamelCase__			)

  @torch.no_grad()
  def    a					(    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    a					(    self						:	Union[str, 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__						:	Dict   ,				):
     """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 = 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    a					(    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__						:	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						:	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__						:	Dict   ,				):
     """simple docstring"""

     __UpperCamelCase	:			Optional[Any]					=					"""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	:			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.2
     __UpperCamelCase	:			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
     __UpperCamelCase	:			Optional[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.4
     __UpperCamelCase	:			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 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]]			)



