



'''simple docstring'''




import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device



def  __lowerCamelCase    (   _lowercase						) ->     List[str]:
							UpperCAmelCase	:						Union[str, Any]					=						{}
							for name in unet.attn_processors.keys():
														UpperCAmelCase	:						List[str]					=						None if name.endswith("""attn1.processor"""						) else unet.config.cross_attention_dim
														if name.startswith("""mid_block"""						):
																					UpperCAmelCase	:						Union[str, Any]					=						unet.config.block_out_channels[-1]
														elif name.startswith("""up_blocks"""						):
																					UpperCAmelCase	:						str					=						int(name[len("""up_blocks."""						)]						)
																					UpperCAmelCase	:						Tuple					=						list(reversed(unet.config.block_out_channels						)						)[block_id]
														elif name.startswith("""down_blocks"""						):
																					UpperCAmelCase	:						List[Any]					=						int(name[len("""down_blocks."""						)]						)
																					UpperCAmelCase	:						Optional[int]					=						unet.config.block_out_channels[block_id]
														UpperCAmelCase	:						str					=						(
														    LoRAAttnProcessor2_0 if hasattr(_lowercase      ,							"""scaled_dot_product_attention"""						) else LoRAAttnProcessor
														)
														UpperCAmelCase	:						str					=						lora_attn_processor_class(
														    hidden_size=_lowercase      ,							cross_attention_dim=_lowercase						)
							UpperCAmelCase	:						Any					=						AttnProcsLayers(_lowercase						)
							return lora_attn_procs, unet_lora_layers



def  __lowerCamelCase    (   _lowercase						) ->     str:
							UpperCAmelCase	:						List[Any]					=						{}
							UpperCAmelCase	:						Optional[Any]					=						(
							    LoRAAttnProcessor2_0 if hasattr(_lowercase      ,							"""scaled_dot_product_attention"""						) else LoRAAttnProcessor
							)
							for name, module in text_encoder.named_modules():
														if name.endswith(_lowercase						):
																					UpperCAmelCase	:						List[Any]					=						lora_attn_processor_class(
																					    hidden_size=module.out_proj.out_features      ,							cross_attention_dim=_lowercase						)
							return text_lora_attn_procs



def  __lowerCamelCase    (   _lowercase						) ->     int:
							UpperCAmelCase	:						int					=						create_text_encoder_lora_attn_procs(_lowercase						)
							UpperCAmelCase	:						List[str]					=						AttnProcsLayers(_lowercase						)
							return text_encoder_lora_layers



def  __lowerCamelCase    (   _lowercase      ,							_lowercase=False						) ->     Tuple:
							for _, attn_proc in text_lora_attn_procs.items():
														# set up.weights
														for layer_name, layer_module in attn_proc.named_modules():
																					if layer_name.endswith("""_lora"""						):
																												UpperCAmelCase	:						Dict					=						(
																												    torch.randn_like(layer_module.up.weight						)
																												    if randn_weight
																												    else torch.zeros_like(layer_module.up.weight						)
																												)
																												UpperCAmelCase	:						List[str]					=						torch.nn.Parameter(_lowercase						)


class      UpperCamelCase_					(				unittest.TestCase     ):





	def 					_lowercase(    self       )     ->     Optional[int]:
								torch.manual_seed(0       )
								UpperCAmelCase	:						Tuple					=						UNetaDConditionModel(
								    block_out_channels=(32, 64)						,						layers_per_block=2						,						sample_size=32						,						in_channels=4						,						out_channels=4						,						down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")						,						up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")						,						cross_attention_dim=32						,						)
								UpperCAmelCase	:						str					=						DDIMScheduler(
								    beta_start=0.0_0_0_8_5						,						beta_end=0.0_1_2						,						beta_schedule="""scaled_linear"""						,						clip_sample=A						,						set_alpha_to_one=A						,						steps_offset=1						,						)
								torch.manual_seed(0       )
								UpperCAmelCase	:						Optional[Any]					=						AutoencoderKL(
								    block_out_channels=[32, 64]						,						in_channels=3						,						out_channels=3						,						down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]						,						up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]						,						latent_channels=4						,						)
								UpperCAmelCase	:						Union[str, Any]					=						CLIPTextConfig(
								    bos_token_id=0						,						eos_token_id=2						,						hidden_size=32						,						intermediate_size=37						,						layer_norm_eps=1e-05						,						num_attention_heads=4						,						num_hidden_layers=5						,						pad_token_id=1						,						vocab_size=1000						,						)
								UpperCAmelCase	:						Dict					=						CLIPTextModel(A       )
								UpperCAmelCase	:						Union[str, Any]					=						CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""       )

								UpperCAmelCase	,	UpperCAmelCase	:						Optional[int]					=						create_unet_lora_layers(A       )
								UpperCAmelCase	:						Optional[Any]					=						create_text_encoder_lora_layers(A       )

								UpperCAmelCase	:						List[Any]					=						{
								    """unet""": unet,
								    """scheduler""": scheduler,
								    """vae""": vae,
								    """text_encoder""": text_encoder,
								    """tokenizer""": tokenizer,
								    """safety_checker""": None,
								    """feature_extractor""": None,
								}
								UpperCAmelCase	:						Any					=						{
								    """unet_lora_layers""": unet_lora_layers,
								    """text_encoder_lora_layers""": text_encoder_lora_layers,
								    """unet_lora_attn_procs""": unet_lora_attn_procs,
								}
								return pipeline_components, lora_components





	def 					_lowercase(    self       )     ->     str:
								UpperCAmelCase	:						Dict					=						1
								UpperCAmelCase	:						Tuple					=						10
								UpperCAmelCase	:						str					=						4
								UpperCAmelCase	:						Any					=						(32, 32)

								UpperCAmelCase	:						str					=						torch.manual_seed(0       )
								UpperCAmelCase	:						str					=						floats_tensor((batch_size, num_channels) + sizes       )
								UpperCAmelCase	:						Any					=						torch.randint(1						,						A						,						size=(batch_size, sequence_length)						,						generator=A       )

								UpperCAmelCase	:						int					=						{
								    """prompt""": """A painting of a squirrel eating a burger""",
								    """generator""": generator,
								    """num_inference_steps""": 2,
								    """guidance_scale""": 6.0,
								    """output_type""": """numpy""",
								}

								return noise, input_ids, pipeline_inputs

								# copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb





	def 					_lowercase(    self       )     ->     Union[str, Any]:
								UpperCAmelCase	:						str					=						77

								UpperCAmelCase	:						Union[str, Any]					=						torch.randint(2						,						56						,						size=(1, max_seq_length)						,						generator=torch.manual_seed(0       )       )

								UpperCAmelCase	:						Any					=						{}
								UpperCAmelCase	:						Optional[int]					=						inputs
								return prepared_inputs





	def 					_lowercase(    self						,						A       )     ->     Union[str, Any]:
								UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						self.get_dummy_components()
								LoraLoaderMixin.save_lora_weights(
								    save_directory=A						,						unet_lora_layers=lora_components["""unet_lora_layers"""]						,						text_encoder_lora_layers=lora_components["""text_encoder_lora_layers"""]						,						)
								self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.bin"""       )       )       )





	def 					_lowercase(    self       )     ->     List[str]:
								UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						self.get_dummy_components()
								UpperCAmelCase	:						Optional[Any]					=						StableDiffusionPipeline(**A       )
								UpperCAmelCase	:						Dict					=						sd_pipe.to(A       )
								sd_pipe.set_progress_bar_config(disable=A       )

								UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase	:						Any					=						self.get_dummy_inputs()

								UpperCAmelCase	:						str					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						str					=						original_images[0, -3:, -3:, -1]

								with tempfile.TemporaryDirectory() as tmpdirname:
															LoraLoaderMixin.save_lora_weights(
															    save_directory=A						,						unet_lora_layers=lora_components["""unet_lora_layers"""]						,						text_encoder_lora_layers=lora_components["""text_encoder_lora_layers"""]						,						)
															self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.bin"""       )       )       )
															sd_pipe.load_lora_weights(A       )

								UpperCAmelCase	:						Tuple					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						Optional[Any]					=						lora_images[0, -3:, -3:, -1]

								# Outputs shouldn't match.
								self.assertFalse(torch.allclose(torch.from_numpy(A       )						,						torch.from_numpy(A       )       )       )





	def 					_lowercase(    self       )     ->     List[str]:
								UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						self.get_dummy_components()
								UpperCAmelCase	:						str					=						StableDiffusionPipeline(**A       )
								UpperCAmelCase	:						List[str]					=						sd_pipe.to(A       )
								sd_pipe.set_progress_bar_config(disable=A       )

								UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase	:						Union[str, Any]					=						self.get_dummy_inputs()

								UpperCAmelCase	:						List[str]					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						Any					=						original_images[0, -3:, -3:, -1]

								with tempfile.TemporaryDirectory() as tmpdirname:
															LoraLoaderMixin.save_lora_weights(
															    save_directory=A						,						unet_lora_layers=lora_components["""unet_lora_layers"""]						,						text_encoder_lora_layers=lora_components["""text_encoder_lora_layers"""]						,						safe_serialization=A						,						)
															self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.safetensors"""       )       )       )
															sd_pipe.load_lora_weights(A       )

								UpperCAmelCase	:						List[Any]					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						str					=						lora_images[0, -3:, -3:, -1]

								# Outputs shouldn't match.
								self.assertFalse(torch.allclose(torch.from_numpy(A       )						,						torch.from_numpy(A       )       )       )





	def 					_lowercase(    self       )     ->     Optional[int]:
								UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						self.get_dummy_components()
								UpperCAmelCase	:						int					=						lora_components["""unet_lora_attn_procs"""]
								UpperCAmelCase	:						Optional[int]					=						StableDiffusionPipeline(**A       )
								UpperCAmelCase	:						List[Any]					=						sd_pipe.to(A       )
								sd_pipe.set_progress_bar_config(disable=A       )

								UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase	:						Dict					=						self.get_dummy_inputs()

								UpperCAmelCase	:						Optional[int]					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						Optional[Any]					=						original_images[0, -3:, -3:, -1]

								with tempfile.TemporaryDirectory() as tmpdirname:
															UpperCAmelCase	:						Tuple					=						sd_pipe.unet
															unet.set_attn_processor(A       )
															unet.save_attn_procs(A       )
															self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.bin"""       )       )       )
															sd_pipe.load_lora_weights(A       )

								UpperCAmelCase	:						int					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						Optional[Any]					=						lora_images[0, -3:, -3:, -1]

								# Outputs shouldn't match.
								self.assertFalse(torch.allclose(torch.from_numpy(A       )						,						torch.from_numpy(A       )       )       )





	def 					_lowercase(    self       )     ->     Dict:
								UpperCAmelCase	,	UpperCAmelCase	:						Optional[int]					=						self.get_dummy_components()
								UpperCAmelCase	:						List[Any]					=						StableDiffusionPipeline(**A       )

								UpperCAmelCase	:						Optional[Any]					=						self.get_dummy_tokens()

								# inference without lora
								UpperCAmelCase	:						Dict					=						pipe.text_encoder(**A       )[0]
								assert outputs_without_lora.shape == (1, 77, 32)

								# create lora_attn_procs with zeroed out up.weights
								UpperCAmelCase	:						Optional[int]					=						create_text_encoder_lora_attn_procs(pipe.text_encoder       )
								set_lora_up_weights(A						,						randn_weight=A       )

								# monkey patch
								pipe._modify_text_encoder(A       )

								# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
								del text_attn_procs
								gc.collect()

								# inference with lora
								UpperCAmelCase	:						Optional[int]					=						pipe.text_encoder(**A       )[0]
								assert outputs_with_lora.shape == (1, 77, 32)

								assert torch.allclose(
								    A						,						A       ), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

								# create lora_attn_procs with randn up.weights
								UpperCAmelCase	:						str					=						create_text_encoder_lora_attn_procs(pipe.text_encoder       )
								set_lora_up_weights(A						,						randn_weight=A       )

								# monkey patch
								pipe._modify_text_encoder(A       )

								# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
								del text_attn_procs
								gc.collect()

								# inference with lora
								UpperCAmelCase	:						Tuple					=						pipe.text_encoder(**A       )[0]
								assert outputs_with_lora.shape == (1, 77, 32)

								assert not torch.allclose(
								    A						,						A       ), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"





	def 					_lowercase(    self       )     ->     Dict:
								UpperCAmelCase	,	UpperCAmelCase	:						Dict					=						self.get_dummy_components()
								UpperCAmelCase	:						str					=						StableDiffusionPipeline(**A       )

								UpperCAmelCase	:						Optional[Any]					=						self.get_dummy_tokens()

								# inference without lora
								UpperCAmelCase	:						Union[str, Any]					=						pipe.text_encoder(**A       )[0]
								assert outputs_without_lora.shape == (1, 77, 32)

								# create lora_attn_procs with randn up.weights
								UpperCAmelCase	:						Dict					=						create_text_encoder_lora_attn_procs(pipe.text_encoder       )
								set_lora_up_weights(A						,						randn_weight=A       )

								# monkey patch
								pipe._modify_text_encoder(A       )

								# verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
								del text_attn_procs
								gc.collect()

								# inference with lora
								UpperCAmelCase	:						str					=						pipe.text_encoder(**A       )[0]
								assert outputs_with_lora.shape == (1, 77, 32)

								assert not torch.allclose(
								    A						,						A       ), "lora outputs should be different to without lora outputs"

								# remove monkey patch
								pipe._remove_text_encoder_monkey_patch()

								# inference with removed lora
								UpperCAmelCase	:						Dict					=						pipe.text_encoder(**A       )[0]
								assert outputs_without_lora_removed.shape == (1, 77, 32)

								assert torch.allclose(
								    A						,						A       ), "remove lora monkey patch should restore the original outputs"





	def 					_lowercase(    self       )     ->     Optional[int]:
								UpperCAmelCase	,	UpperCAmelCase	:						Any					=						self.get_dummy_components()
								UpperCAmelCase	:						str					=						StableDiffusionPipeline(**A       )
								UpperCAmelCase	:						int					=						sd_pipe.to(A       )
								sd_pipe.set_progress_bar_config(disable=A       )

								UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase	:						Any					=						self.get_dummy_inputs()

								with tempfile.TemporaryDirectory() as tmpdirname:
															LoraLoaderMixin.save_lora_weights(
															    save_directory=A						,						unet_lora_layers=lora_components["""unet_lora_layers"""]						,						text_encoder_lora_layers=lora_components["""text_encoder_lora_layers"""]						,						)
															self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.bin"""       )       )       )
															sd_pipe.load_lora_weights(A       )

								UpperCAmelCase	:						Any					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						List[Any]					=						lora_images[0, -3:, -3:, -1]

								UpperCAmelCase	:						List[str]					=						sd_pipe(**A						,						cross_attention_kwargs={"""scale""": 0.5}       ).images
								UpperCAmelCase	:						List[str]					=						lora_images_with_scale[0, -3:, -3:, -1]

								# Outputs shouldn't match.
								self.assertFalse(
								    torch.allclose(torch.from_numpy(A       )						,						torch.from_numpy(A       )       )       )





	def 					_lowercase(    self       )     ->     Tuple:
								with tempfile.TemporaryDirectory() as tmpdirname:
															self.create_lora_weight_file(A       )

															UpperCAmelCase	,	UpperCAmelCase	:						str					=						self.get_dummy_components()
															UpperCAmelCase	:						Any					=						StableDiffusionPipeline(**A       )
															UpperCAmelCase	:						Optional[int]					=						sd_pipe.to(A       )
															sd_pipe.set_progress_bar_config(disable=A       )

															# check if vanilla attention processors are used
															for _, module in sd_pipe.unet.named_modules():
																						if isinstance(A						,						A       ):
																													self.assertIsInstance(module.processor						,						(AttnProcessor, AttnProcessor2_0)       )

            # load LoRA weight file
															sd_pipe.load_lora_weights(A       )

															# check if lora attention processors are used
															for _, module in sd_pipe.unet.named_modules():
																						if isinstance(A						,						A       ):
																													UpperCAmelCase	:						Optional[Any]					=						(
																													    LoRAAttnProcessor2_0 if hasattr(A						,						"""scaled_dot_product_attention"""       ) else LoRAAttnProcessor
																													)
																													self.assertIsInstance(module.processor						,						A       )





	@unittest.skipIf(torch_device != """cuda"""						,						"""This test is supposed to run on GPU"""       )
	def 					_lowercase(    self       )     ->     int:
								with tempfile.TemporaryDirectory() as tmpdirname:
															self.create_lora_weight_file(A       )

															UpperCAmelCase	,	UpperCAmelCase	:						List[Any]					=						self.get_dummy_components()
															UpperCAmelCase	:						List[Any]					=						StableDiffusionPipeline(**A       )
															UpperCAmelCase	:						Tuple					=						sd_pipe.to(A       )
															sd_pipe.set_progress_bar_config(disable=A       )

															# enable XFormers
															sd_pipe.enable_xformers_memory_efficient_attention()

															# check if xFormers attention processors are used
															for _, module in sd_pipe.unet.named_modules():
																						if isinstance(A						,						A       ):
																													self.assertIsInstance(module.processor						,						A       )

            # load LoRA weight file
															sd_pipe.load_lora_weights(A       )

															# check if lora attention processors are used
															for _, module in sd_pipe.unet.named_modules():
																						if isinstance(A						,						A       ):
																													self.assertIsInstance(module.processor						,						A       )






	@unittest.skipIf(torch_device != """cuda"""						,						"""This test is supposed to run on GPU"""       )
	def 					_lowercase(    self       )     ->     List[Any]:
								UpperCAmelCase	,	UpperCAmelCase	:						str					=						self.get_dummy_components()
								UpperCAmelCase	:						Union[str, Any]					=						StableDiffusionPipeline(**A       )
								UpperCAmelCase	:						List[Any]					=						sd_pipe.to(A       )
								sd_pipe.set_progress_bar_config(disable=A       )

								UpperCAmelCase	,	UpperCAmelCase	,	UpperCAmelCase	:						Union[str, Any]					=						self.get_dummy_inputs()

								# enable XFormers
								sd_pipe.enable_xformers_memory_efficient_attention()

								UpperCAmelCase	:						Tuple					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						Any					=						original_images[0, -3:, -3:, -1]

								with tempfile.TemporaryDirectory() as tmpdirname:
															LoraLoaderMixin.save_lora_weights(
															    save_directory=A						,						unet_lora_layers=lora_components["""unet_lora_layers"""]						,						text_encoder_lora_layers=lora_components["""text_encoder_lora_layers"""]						,						)
															self.assertTrue(os.path.isfile(os.path.join(A						,						"""pytorch_lora_weights.bin"""       )       )       )
															sd_pipe.load_lora_weights(A       )

								UpperCAmelCase	:						Dict					=						sd_pipe(**A       ).images
								UpperCAmelCase	:						int					=						lora_images[0, -3:, -3:, -1]

								# Outputs shouldn't match.
								self.assertFalse(torch.allclose(torch.from_numpy(A       )						,						torch.from_numpy(A       )       )       )






'''simple docstring'''




class      UpperCamelCase_					:





	def __init__(    self       )     ->     None:
								UpperCAmelCase	:						int					=						[0, 1]






	def 					_lowercase(    self						,						A       )     ->     list:
								if (difference := index - (len(self.sequence       ) - 2)) >= 1:
															for _ in range(A       ):
																						self.sequence.append(self.sequence[-1] + self.sequence[-2]       )
								return self.sequence[:index]




def  __lowerCamelCase    (   ) ->     None:
							print(
							    """Fibonacci Series Using Dynamic Programming\n"""      ,							"""Enter the index of the Fibonacci number you want to calculate """      ,							"""in the prompt below. (To exit enter exit or Ctrl-C)\n"""      ,							sep=""""""      ,							)
							UpperCAmelCase	:						Tuple					=						Fibonacci()

							while True:
														UpperCAmelCase	:						str					=						input(""">> """						)
														if prompt in {"exit", "quit"}:
																					break

														try:
																					UpperCAmelCase	:						int					=						int(_lowercase						)
														except ValueError:
																					print("""Enter a number or 'exit'"""						)
																					continue

														print(fibonacci.get(_lowercase						)						)


if __name__ == "__main__":
							main()

