




'''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       :						List[Any]								=		'''CompVis/stable-diffusion-v1-1'''
a       :						Optional[Any]								=		'''CompVis/stable-diffusion-v1-2'''
a       :						Any								=		'''CompVis/stable-diffusion-v1-3'''
a       :						Optional[int]								=		'''CompVis/stable-diffusion-v1-4'''



class 		SCREAMING_SNAKE_CASE__  (			_UpperCamelCase	):





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



						"""simple docstring"""




						super()._init_()

						__snake_case			=		StableDiffusionPipeline.from_pretrained(a_							)
						__snake_case			=		StableDiffusionPipeline.from_pretrained(a_							)
						__snake_case			=		StableDiffusionPipeline.from_pretrained(a_							)
						__snake_case			=		StableDiffusionPipeline(
						    vae=a_      ,		text_encoder=a_      ,		tokenizer=a_      ,		unet=a_      ,		scheduler=a_      ,		safety_checker=a_      ,		feature_extractor=a_      ,		requires_safety_checker=a_      ,		)

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





			@property
			def 							A (      self						:    int							):



						"""simple docstring"""




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





			def 							A (      self						:    int      ,		a_						:    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
									__snake_case			=		self.unet.config.attention_head_dim // 2
						self.unet.set_attention_slice(a_							)





			def 							A (      self						:    str							):



						"""simple docstring"""




						self.enable_attention_slicing(a_							)





			@torch.no_grad()
			def 							A (      self						:    List[str]      ,		a_						:    Union[str, List[str]]      ,		a_						:    int = 512      ,		a_						:    int = 512      ,		a_						:    int = 50      ,		a_						:    float = 7.5      ,		a_						:    Optional[Union[str, List[str]]] = None      ,		a_						:    Optional[int] = 1      ,		a_						:    float = 0.0      ,		a_						:    Optional[torch.Generator] = None      ,		a_						:    Optional[torch.FloatTensor] = None      ,		a_						:    Optional[str] = "pil"      ,		a_						:    bool = True      ,		a_						:    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		a_						:    int = 1      ,		**a_						:    Optional[int]      ,		):



						"""simple docstring"""




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





			@torch.no_grad()
			def 							A (      self						:    int      ,		a_						:    Union[str, List[str]]      ,		a_						:    int = 512      ,		a_						:    int = 512      ,		a_						:    int = 50      ,		a_						:    float = 7.5      ,		a_						:    Optional[Union[str, List[str]]] = None      ,		a_						:    Optional[int] = 1      ,		a_						:    float = 0.0      ,		a_						:    Optional[torch.Generator] = None      ,		a_						:    Optional[torch.FloatTensor] = None      ,		a_						:    Optional[str] = "pil"      ,		a_						:    bool = True      ,		a_						:    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		a_						:    int = 1      ,		**a_						:    Union[str, Any]      ,		):



						"""simple docstring"""




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





			@torch.no_grad()
			def 							A (      self						:    Union[str, Any]      ,		a_						:    Union[str, List[str]]      ,		a_						:    int = 512      ,		a_						:    int = 512      ,		a_						:    int = 50      ,		a_						:    float = 7.5      ,		a_						:    Optional[Union[str, List[str]]] = None      ,		a_						:    Optional[int] = 1      ,		a_						:    float = 0.0      ,		a_						:    Optional[torch.Generator] = None      ,		a_						:    Optional[torch.FloatTensor] = None      ,		a_						:    Optional[str] = "pil"      ,		a_						:    bool = True      ,		a_						:    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		a_						:    int = 1      ,		**a_						:    Dict      ,		):



						"""simple docstring"""




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





			@torch.no_grad()
			def 							A (      self						:    Any      ,		a_						:    Union[str, List[str]]      ,		a_						:    int = 512      ,		a_						:    int = 512      ,		a_						:    int = 50      ,		a_						:    float = 7.5      ,		a_						:    Optional[Union[str, List[str]]] = None      ,		a_						:    Optional[int] = 1      ,		a_						:    float = 0.0      ,		a_						:    Optional[torch.Generator] = None      ,		a_						:    Optional[torch.FloatTensor] = None      ,		a_						:    Optional[str] = "pil"      ,		a_						:    bool = True      ,		a_						:    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		a_						:    int = 1      ,		**a_						:    Optional[Any]      ,		):



						"""simple docstring"""




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






			@torch.no_grad()
			def 							A (      self						:    int      ,		a_						:    Union[str, List[str]]      ,		a_						:    int = 512      ,		a_						:    int = 512      ,		a_						:    int = 50      ,		a_						:    float = 7.5      ,		a_						:    Optional[Union[str, List[str]]] = None      ,		a_						:    Optional[int] = 1      ,		a_						:    float = 0.0      ,		a_						:    Optional[torch.Generator] = None      ,		a_						:    Optional[torch.FloatTensor] = None      ,		a_						:    Optional[str] = "pil"      ,		a_						:    bool = True      ,		a_						:    Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,		a_						:    int = 1      ,		**a_						:    Union[str, Any]      ,		):



						"""simple docstring"""




						__snake_case			=		"cuda" if torch.cuda.is_available() else "cpu"
						self.to(a_							)

						# 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
						__snake_case			=		self.textaimg_sda_a(
						    prompt=a_      ,		height=a_      ,		width=a_      ,		num_inference_steps=a_      ,		guidance_scale=a_      ,		negative_prompt=a_      ,		num_images_per_prompt=a_      ,		eta=a_      ,		generator=a_      ,		latents=a_      ,		output_type=a_      ,		return_dict=a_      ,		callback=a_      ,		callback_steps=a_      ,		**a_      ,		)

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

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

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

						# 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"""




from __future__ import annotations

from statistics import mean







def 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__      ,	lowerCamelCase__  ):





		"""simple docstring"""




		lowerCAmelCase__   = [0] * no_of_processes
		lowerCAmelCase__   = [0] * no_of_processes

		# Initialize remaining_time to waiting_time.

		for i in range(lowerCamelCase__  ):
				lowerCAmelCase__   = burst_time[i]
		lowerCAmelCase__   = []

		lowerCAmelCase__   = 0
		lowerCAmelCase__   = 0

		# When processes are not completed,
		# A process whose arrival time has passed \
		# and has remaining execution time is put into the ready_process.
		# The shortest process in the ready_process, target_process is executed.

		while completed != no_of_processes:
				lowerCAmelCase__   = []
				lowerCAmelCase__   = -1

				for i in range(lowerCamelCase__  ):
						if (arrival_time[i] <= total_time) and (remaining_time[i] > 0):
								ready_process.append(lowerCamelCase__  )

				if len(lowerCamelCase__  ) > 0:
						lowerCAmelCase__   = ready_process[0]
						for i in ready_process:
								if remaining_time[i] < remaining_time[target_process]:
										lowerCAmelCase__   = i
						total_time += burst_time[target_process]
						completed += 1
						lowerCAmelCase__   = 0
						lowerCAmelCase__   = (
						    total_time - arrival_time[target_process] - burst_time[target_process]
						)
				else:
						total_time += 1

		return waiting_time







def 				_UpperCAmelCase		(							lowerCamelCase__      ,	lowerCamelCase__      ,	lowerCamelCase__  ):





		"""simple docstring"""




		lowerCAmelCase__   = [0] * no_of_processes
		for i in range(lowerCamelCase__  ):
				lowerCAmelCase__   = burst_time[i] + waiting_time[i]
		return turn_around_time


if __name__ == "__main__":
			print("[TEST CASE 01]")

			__lowerCAmelCase     :				str    				=    4
			__lowerCAmelCase     :				Dict    				=    [2, 5, 3, 7]
			__lowerCAmelCase     :				List[str]    				=    [0, 0, 0, 0]
			__lowerCAmelCase     :				Union[str, Any]    				=    calculate_waitingtime(arrival_time, burst_time, no_of_processes)
			__lowerCAmelCase     :				Union[str, Any]    				=    calculate_turnaroundtime(
			    burst_time, no_of_processes, waiting_time
			)

			# Printing the Result
			print("PID\tBurst Time\tArrival Time\tWaiting Time\tTurnaround Time")
			for i, process_id in enumerate(list(range(1, 5))):
						print(
						    F"{process_id}\t{burst_time[i]}\t\t\t{arrival_time[i]}\t\t\t\t"
						    F"{waiting_time[i]}\t\t\t\t{turn_around_time[i]}"
						)
			print(F"\nAverage waiting time = {mean(waiting_time):.5f}")
			print(F"Average turnaround time = {mean(turn_around_time):.5f}")




