





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 							__SCREAMING_SNAKE_CASE       (  _a							):






							def __init__(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase = True	,			):
														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 					_lowerCamelCase		(    self	):
														return {k: getattr(self	,			__lowerCAmelCase	) for k in self.config.keys() if not k.startswith("""_"""	)}






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase = "auto"	):
														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 					_lowerCamelCase		(    self	):
														self.enable_attention_slicing(__lowerCAmelCase	)






							@torch.no_grad()
							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 50	,			__lowerCAmelCase = 7.5	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			__lowerCAmelCase = 0.0	,			__lowerCAmelCase = None	,			__lowerCAmelCase = None	,			__lowerCAmelCase = "pil"	,			__lowerCAmelCase = True	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			**__lowerCAmelCase	,			):
														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 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 50	,			__lowerCAmelCase = 7.5	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			__lowerCAmelCase = 0.0	,			__lowerCAmelCase = None	,			__lowerCAmelCase = None	,			__lowerCAmelCase = "pil"	,			__lowerCAmelCase = True	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			**__lowerCAmelCase	,			):
														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 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 50	,			__lowerCAmelCase = 7.5	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			__lowerCAmelCase = 0.0	,			__lowerCAmelCase = None	,			__lowerCAmelCase = None	,			__lowerCAmelCase = "pil"	,			__lowerCAmelCase = True	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			**__lowerCAmelCase	,			):
														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 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 50	,			__lowerCAmelCase = 7.5	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			__lowerCAmelCase = 0.0	,			__lowerCAmelCase = None	,			__lowerCAmelCase = None	,			__lowerCAmelCase = "pil"	,			__lowerCAmelCase = True	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			**__lowerCAmelCase	,			):
														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 					_lowerCamelCase		(    self	,			__lowerCAmelCase	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 512	,			__lowerCAmelCase = 50	,			__lowerCAmelCase = 7.5	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			__lowerCAmelCase = 0.0	,			__lowerCAmelCase = None	,			__lowerCAmelCase = None	,			__lowerCAmelCase = "pil"	,			__lowerCAmelCase = True	,			__lowerCAmelCase = None	,			__lowerCAmelCase = 1	,			**__lowerCAmelCase	,			):
														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]]	)











# We ignore warnings about stepping the scheduler since we step it ourselves during gradient accumulation

import warnings

from .state import AcceleratorState, GradientState


warnings.filterwarnings("ignore", category=UserWarning, module="torch.optim.lr_scheduler")

class 							__SCREAMING_SNAKE_CASE       :






							def __init__(    self	,			__lowerCAmelCase	,			__lowerCAmelCase	,			__lowerCAmelCase = True	,			__lowerCAmelCase = False	):
														UpperCamelCase__					  =      scheduler
														UpperCamelCase__					  =      optimizers if isinstance(__lowerCAmelCase	,			(list, tuple)	) else [optimizers]
														UpperCamelCase__					  =      split_batches
														UpperCamelCase__					  =      step_with_optimizer
														UpperCamelCase__					  =      GradientState()






							def 					_lowerCamelCase		(    self	,			*__lowerCAmelCase	,			**__lowerCAmelCase	):
														if not self.step_with_optimizer:
																					# No link between scheduler and optimizer -> just step
																					self.scheduler.step(*__lowerCAmelCase	,			**__lowerCAmelCase	)
																					return

														# Otherwise, first make sure the optimizer was stepped.
														if not self.gradient_state.sync_gradients:
																					if self.gradient_state.adjust_scheduler:
																												self.scheduler._step_count += 1
																					return

														for opt in self.optimizers:
																					if opt.step_was_skipped:
																												return
														if self.split_batches:
																					# Split batches -> the training dataloader batch size is not changed so one step per training step
																					self.scheduler.step(*__lowerCAmelCase	,			**__lowerCAmelCase	)
														else:
																					# Otherwise the training dataloader batch size was multiplied by `num_processes`, so we need to do
																					# num_processes steps per training step
																					UpperCamelCase__					  =      AcceleratorState().num_processes
																					for _ in range(__lowerCAmelCase	):
																												# Special case when using OneCycle and `drop_last` was not used
																												if hasattr(self.scheduler	,			"""total_steps"""	):
																																			if self.scheduler._step_count <= self.scheduler.total_steps:
																																										self.scheduler.step(*__lowerCAmelCase	,			**__lowerCAmelCase	)
																												else:
																																			self.scheduler.step(*__lowerCAmelCase	,			**__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														return self.scheduler.get_last_lr()






							def 					_lowerCamelCase		(    self	):
														return self.scheduler.state_dict()






							def 					_lowerCamelCase		(    self	,			__lowerCAmelCase	):
														self.scheduler.load_state_dict(__lowerCAmelCase	)






							def 					_lowerCamelCase		(    self	):
														return self.scheduler.get_lr()







							def 					_lowerCamelCase		(    self	,			*__lowerCAmelCase	,			**__lowerCAmelCase	):
														return self.scheduler.print_lr(*__lowerCAmelCase	,			**__lowerCAmelCase	)




