


"""simple docstring"""
import math
from dataclasses import dataclass
from typing import List, Optional, Tuple, Union

import numpy as np
import torch

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, logging, randn_tensor
from .scheduling_utils import KarrasDiffusionSchedulers, SchedulerMixin


SCREAMING_SNAKE_CASE					:       Any							   =							logging.get_logger(__name__)  # pylint: disable=invalid-name



@dataclass
# Copied from diffusers.schedulers.scheduling_ddpm.DDPMSchedulerOutput with DDPM->EulerDiscrete
class      _UpperCAmelCase					(				__snake_case							):






		'''simple docstring'''

		lowerCamelCase__           =42
		lowerCamelCase__           =None






def 						lowercase				(	_snake_case		: Union[str, Any]	,  _snake_case		: List[str]=0.999	,  _snake_case		: Union[str, Any]="cosine"	,  )	->Union[str, Any]:
	"""simple docstring"""





	if alpha_transform_type == "cosine":

		def alpha_bar_fn(_snake_case		: Tuple	):
			return math.cos((t + 0.008) / 1.008 * math.pi / 2	) ** 2

	elif alpha_transform_type == "exp":

		def alpha_bar_fn(_snake_case		: Union[str, Any]	):
			return math.exp(t * -12.0	)

	else:
		raise ValueError(f"""Unsupported alpha_tranform_type: {alpha_transform_type}"""	)

	__snake_case      :		List[str]         =							[]
	for i in range(_snake_case	):
		__snake_case      :		List[str]         =							i / num_diffusion_timesteps
		__snake_case      :		Optional[int]         =							(i + 1) / num_diffusion_timesteps
		betas.append(min(1 - alpha_bar_fn(_snake_case	) / alpha_bar_fn(_snake_case	)	,  _snake_case	)	)
	return torch.tensor(_snake_case	,  dtype=torch.floataa	)



class      _UpperCAmelCase					(				__snake_case,							__snake_case							):






		'''simple docstring'''

		lowerCamelCase__           =[e.name for e in KarrasDiffusionSchedulers]
		lowerCamelCase__           =1



		@register_to_config
		def __init__(self							,			a_ = 10_00							,			a_ = 0.0001							,			a_ = 0.02							,			a_ = "linear"							,			a_ = None							,			a_ = "epsilon"							,			a_ = "linear"							,			a_ = False							,			a_ = "linspace"							,			a_ = 0							,			):





			'''simple docstring'''

			if trained_betas is not None:
				__snake_case      :		Tuple         =							torch.tensor(a_							,			dtype=torch.floataa   )
			elif beta_schedule == "linear":
				__snake_case      :		List[Any]         =							torch.linspace(a_							,			a_							,			a_							,			dtype=torch.floataa   )
			elif beta_schedule == "scaled_linear":
				# this schedule is very specific to the latent diffusion model.
				__snake_case      :		Tuple         =							(
				    torch.linspace(beta_start**0.5							,			beta_end**0.5							,			a_							,			dtype=torch.floataa   ) ** 2
				)
			elif beta_schedule == "squaredcos_cap_v2":
				# Glide cosine schedule
				__snake_case      :		Union[str, Any]         =							betas_for_alpha_bar(a_   )
			else:
				raise NotImplementedError(f"""{beta_schedule} does is not implemented for {self.__class__}"""   )

			__snake_case      :		Tuple         =							1.0 - self.betas
			__snake_case      :		Optional[int]         =							torch.cumprod(self.alphas							,			dim=0   )

			__snake_case      :		str         =							np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5   )
			__snake_case      :		Dict         =							np.concatenate([sigmas[::-1], [0.0]]   ).astype(np.floataa   )
			__snake_case      :		List[str]         =							torch.from_numpy(a_   )

			# setable values
			__snake_case      :		List[Any]         =							None
			__snake_case      :		str         =							np.linspace(0							,			num_train_timesteps - 1							,			a_							,			dtype=a_   )[::-1].copy()
			__snake_case      :		int         =							torch.from_numpy(a_   )
			__snake_case      :		int         =							False
			__snake_case      :		Union[str, Any]         =							use_karras_sigmas



		@property
		def 							SCREAMING_SNAKE_CASE    (self   ):





			'''simple docstring'''

			if self.config.timestep_spacing in ["linspace", "trailing"]:
				return self.sigmas.max()

			return (self.sigmas.max() ** 2 + 1) ** 0.5



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_   ):





			'''simple docstring'''

			if isinstance(a_							,			torch.Tensor   ):
				__snake_case      :		Tuple         =							timestep.to(self.timesteps.device   )
			__snake_case      :		int         =							(self.timesteps == timestep).nonzero().item()
			__snake_case      :		List[Any]         =							self.sigmas[step_index]

			__snake_case      :		Any         =							sample / ((sigma**2 + 1) ** 0.5)

			__snake_case      :		Union[str, Any]         =							True
			return sample



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_ = None   ):





			'''simple docstring'''

			__snake_case      :		Union[str, Any]         =							num_inference_steps

			# "linspace", "leading", "trailing" corresponds to annotation of Table 2. of https://arxiv.org/abs/2305.08891
			if self.config.timestep_spacing == "linspace":
				__snake_case      :		Tuple         =							np.linspace(0							,			self.config.num_train_timesteps - 1							,			a_							,			dtype=a_   )[
				    ::-1
				].copy()
			elif self.config.timestep_spacing == "leading":
				__snake_case      :		List[str]         =							self.config.num_train_timesteps // self.num_inference_steps
				# creates integer timesteps by multiplying by ratio
				# casting to int to avoid issues when num_inference_step is power of 3
				__snake_case      :		Dict         =							(np.arange(0							,			a_   ) * step_ratio).round()[::-1].copy().astype(a_   )
				timesteps += self.config.steps_offset
			elif self.config.timestep_spacing == "trailing":
				__snake_case      :		Optional[int]         =							self.config.num_train_timesteps / self.num_inference_steps
				# creates integer timesteps by multiplying by ratio
				# casting to int to avoid issues when num_inference_step is power of 3
				__snake_case      :		Union[str, Any]         =							(np.arange(self.config.num_train_timesteps							,			0							,			-step_ratio   )).round().copy().astype(a_   )
				timesteps -= 1
			else:
				raise ValueError(
				    f"""{self.config.timestep_spacing} is not supported. Please make sure to choose one of 'linspace', 'leading' or 'trailing'."""   )

			__snake_case      :		Dict         =							np.array(((1 - self.alphas_cumprod) / self.alphas_cumprod) ** 0.5   )
			__snake_case      :		Tuple         =							np.log(a_   )

			if self.config.interpolation_type == "linear":
				__snake_case      :		Union[str, Any]         =							np.interp(a_							,			np.arange(0							,			len(a_   )   )							,			a_   )
			elif self.config.interpolation_type == "log_linear":
				__snake_case      :		List[str]         =							torch.linspace(np.log(sigmas[-1]   )							,			np.log(sigmas[0]   )							,			num_inference_steps + 1   ).exp()
			else:
				raise ValueError(
				    f"""{self.config.interpolation_type} is not implemented. Please specify interpolation_type to either"""
				    ''' \'linear\' or \'log_linear\''''   )

			if self.use_karras_sigmas:
				__snake_case      :		str         =							self._convert_to_karras(in_sigmas=a_							,			num_inference_steps=self.num_inference_steps   )
				__snake_case      :		Dict         =							np.array([self._sigma_to_t(a_							,			a_   ) for sigma in sigmas]   )

			__snake_case      :		Optional[Any]         =							np.concatenate([sigmas, [0.0]]   ).astype(np.floataa   )
			__snake_case      :		List[Any]         =							torch.from_numpy(a_   ).to(device=a_   )
			if str(a_   ).startswith('''mps'''   ):
				# mps does not support float64
				__snake_case      :		Dict         =							torch.from_numpy(a_   ).to(a_							,			dtype=torch.floataa   )
			else:
				__snake_case      :		List[str]         =							torch.from_numpy(a_   ).to(device=a_   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		int         =							np.log(a_   )

			# get distribution
			__snake_case      :		Tuple         =							log_sigma - log_sigmas[:, np.newaxis]

			# get sigmas range
			__snake_case      :		int         =							np.cumsum((dists >= 0)							,			axis=0   ).argmax(axis=0   ).clip(max=log_sigmas.shape[0] - 2   )
			__snake_case      :		Union[str, Any]         =							low_idx + 1

			__snake_case      :		Union[str, Any]         =							log_sigmas[low_idx]
			__snake_case      :		int         =							log_sigmas[high_idx]

			# interpolate sigmas
			__snake_case      :		int         =							(low - log_sigma) / (low - high)
			__snake_case      :		Optional[int]         =							np.clip(a_							,			0							,			1   )

			# transform interpolation to time range
			__snake_case      :		int         =							(1 - w) * low_idx + w * high_idx
			__snake_case      :		Any         =							t.reshape(sigma.shape   )
			return t



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_   ):





			'''simple docstring'''

			__snake_case      :		float         =							in_sigmas[-1].item()
			__snake_case      :		float         =							in_sigmas[0].item()

			__snake_case      :		Dict         =							7.0  # 7.0 is the value used in the paper
			__snake_case      :		int         =							np.linspace(0							,			1							,			a_   )
			__snake_case      :		int         =							sigma_min ** (1 / rho)
			__snake_case      :		Dict         =							sigma_max ** (1 / rho)
			__snake_case      :		Any         =							(max_inv_rho + ramp * (min_inv_rho - max_inv_rho)) ** rho
			return sigmas



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			a_ = 0.0							,			a_ = 0.0							,			a_ = float('''inf'''   )							,			a_ = 1.0							,			a_ = None							,			a_ = True							,			):





			'''simple docstring'''

			if (
			    isinstance(a_							,			a_   )
			    or isinstance(a_							,			torch.IntTensor   )
			    or isinstance(a_							,			torch.LongTensor   )
			):
				raise ValueError(
				    (
				        '''Passing integer indices (e.g. from `enumerate(timesteps)`) as timesteps to'''
				        ''' `EulerDiscreteScheduler.step()` is not supported. Make sure to pass'''
				        ''' one of the `scheduler.timesteps` as a timestep.'''
				    )							,			)

			if not self.is_scale_input_called:
				logger.warning(
				    '''The `scale_model_input` function should be called before `step` to ensure correct denoising. '''
				    '''See `StableDiffusionPipeline` for a usage example.'''   )

			if isinstance(a_							,			torch.Tensor   ):
				__snake_case      :		Tuple         =							timestep.to(self.timesteps.device   )

			__snake_case      :		List[str]         =							(self.timesteps == timestep).nonzero().item()
			__snake_case      :		Optional[Any]         =							self.sigmas[step_index]

			__snake_case      :		str         =							min(s_churn / (len(self.sigmas   ) - 1)							,			2**0.5 - 1   ) if s_tmin <= sigma <= s_tmax else 0.0

			__snake_case      :		Optional[int]         =							randn_tensor(
			    model_output.shape							,			dtype=model_output.dtype							,			device=model_output.device							,			generator=a_   )

			__snake_case      :		Union[str, Any]         =							noise * s_noise
			__snake_case      :		str         =							sigma * (gamma + 1)

			if gamma > 0:
				__snake_case      :		Union[str, Any]         =							sample + eps * (sigma_hat**2 - sigma**2) ** 0.5

			# 1. compute predicted original sample (x_0) from sigma-scaled predicted noise
			# NOTE: "original_sample" should not be an expected prediction_type but is left in for
			# backwards compatibility
			if self.config.prediction_type == "original_sample" or self.config.prediction_type == "sample":
				__snake_case      :		List[Any]         =							model_output
			elif self.config.prediction_type == "epsilon":
				__snake_case      :		str         =							sample - sigma_hat * model_output
			elif self.config.prediction_type == "v_prediction":
				# * c_out + input * c_skip
				__snake_case      :		Dict         =							model_output * (-sigma / (sigma**2 + 1) ** 0.5) + (sample / (sigma**2 + 1))
			else:
				raise ValueError(
				    f"""prediction_type given as {self.config.prediction_type} must be one of `epsilon`, or `v_prediction`"""   )

			# 2. Convert to an ODE derivative
			__snake_case      :		Union[str, Any]         =							(sample - pred_original_sample) / sigma_hat

			__snake_case      :		Optional[Any]         =							self.sigmas[step_index + 1] - sigma_hat

			__snake_case      :		Union[str, Any]         =							sample + derivative * dt

			if not return_dict:
				return (prev_sample,)

			return EulerDiscreteSchedulerOutput(prev_sample=a_							,			pred_original_sample=a_   )



		def 							SCREAMING_SNAKE_CASE    (self							,			a_							,			a_							,			a_							,			):





			'''simple docstring'''

			__snake_case      :		Any         =							self.sigmas.to(device=original_samples.device							,			dtype=original_samples.dtype   )
			if original_samples.device.type == "mps" and torch.is_floating_point(a_   ):
				# mps does not support float64
				__snake_case      :		Union[str, Any]         =							self.timesteps.to(original_samples.device							,			dtype=torch.floataa   )
				__snake_case      :		Any         =							timesteps.to(original_samples.device							,			dtype=torch.floataa   )
			else:
				__snake_case      :		List[Any]         =							self.timesteps.to(original_samples.device   )
				__snake_case      :		List[Any]         =							timesteps.to(original_samples.device   )

			__snake_case      :		List[Any]         =							[(schedule_timesteps == t).nonzero().item() for t in timesteps]

			__snake_case      :		Dict         =							sigmas[step_indices].flatten()
			while len(sigma.shape   ) < len(original_samples.shape   ):
				__snake_case      :		Union[str, Any]         =							sigma.unsqueeze(-1   )

			__snake_case      :		Optional[Any]         =							original_samples + noise * sigma
			return noisy_samples



		def __len__(self   ):





			'''simple docstring'''

			return self.config.num_train_timesteps






'''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 			_UpperCamelCase			(  _a						:						nn.Module      ):




    """simple docstring"""



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







def 			_UpperCamelCase			(  _a						:						nn.Module      ):




    """simple docstring"""



    __UpperCamelCase						:						Optional[int]		     =	{}
    __UpperCamelCase						:						List[str]		     =	(
        LoRAAttnProcessor2_0 if hasattr(_a					,  'scaled_dot_product_attention'      ) else LoRAAttnProcessor
    )
    for name, module in text_encoder.named_modules():
        if name.endswith(_a      ):
            __UpperCamelCase						:						List[Any]		     =	lora_attn_processor_class(
                hidden_size=module.out_proj.out_features					,  cross_attention_dim=_a      )
    return text_lora_attn_procs







def 			_UpperCamelCase			(  _a						:						nn.Module      ):




    """simple docstring"""



    __UpperCamelCase						:						Optional[Any]		     =	create_text_encoder_lora_attn_procs(_a      )
    __UpperCamelCase						:						Any		     =	AttnProcsLayers(_a      )
    return text_encoder_lora_layers







def 			_UpperCamelCase			(  _a						:						int					,  _a						:						List[Any]=False      ):




    """simple docstring"""



    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						:						List[str]		     =	(
                    torch.randn_like(layer_module.up.weight      )
                    if randn_weight
                    else torch.zeros_like(layer_module.up.weight      )
                )
                __UpperCamelCase						:						Any		     =	torch.nn.Parameter(_a      )






class       __lowercase		(    unittest.TestCase    ):







     """simple docstring"""






     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=4  ,      out_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')  ,      cross_attention_dim=3_2  ,      )
         __UpperCamelCase						:						int		     =	DDIMScheduler(
             beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule='scaled_linear'  ,      clip_sample=_lowerCamelCase  ,      set_alpha_to_one=_lowerCamelCase  ,      steps_offset=1  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	AutoencoderKL(
             block_out_channels=[3_2, 6_4]  ,      in_channels=3  ,      out_channels=3  ,      down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']  ,      up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']  ,      latent_channels=4  ,      )
         __UpperCamelCase						:						str		     =	CLIPTextConfig(
             bos_token_id=0  ,      eos_token_id=2  ,      hidden_size=3_2  ,      intermediate_size=3_7  ,      layer_norm_eps=1E-0_5  ,      num_attention_heads=4  ,      num_hidden_layers=5  ,      pad_token_id=1  ,      vocab_size=1_0_0_0  ,      )
         __UpperCamelCase						:						List[str]		     =	CLIPTextModel(_lowerCamelCase     )
         __UpperCamelCase						:						int		     =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

         __UpperCamelCase     ,       __UpperCamelCase						:						Union[str, Any]		     =	create_unet_lora_layers(_lowerCamelCase     )
         __UpperCamelCase						:						Any		     =	create_text_encoder_lora_layers(_lowerCamelCase     )

         __UpperCamelCase						:						Dict		     =	{
             'unet': unet,
             'scheduler': scheduler,
             'vae': vae,
             'text_encoder': text_encoder,
             'tokenizer': tokenizer,
             'safety_checker': None,
             'feature_extractor': None,
         }
         __UpperCamelCase						:						List[str]		     =	{
             '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 lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Any		     =	1
         __UpperCamelCase						:						Union[str, Any]		     =	1_0
         __UpperCamelCase						:						Dict		     =	4
         __UpperCamelCase						:						Optional[Any]		     =	(3_2, 3_2)

         __UpperCamelCase						:						Any		     =	torch.manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	floats_tensor((batch_size, num_channels) + sizes     )
         __UpperCamelCase						:						List[str]		     =	torch.randint(1  ,      _lowerCamelCase  ,      size=(batch_size, sequence_length)  ,      generator=_lowerCamelCase     )

         __UpperCamelCase						:						Any		     =	{
             '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 lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						int		     =	7_7

         __UpperCamelCase						:						Any		     =	torch.randint(2  ,      5_6  ,      size=(1, max_seq_length)  ,      generator=torch.manual_seed(0     )     )

         __UpperCamelCase						:						List[str]		     =	{}
         __UpperCamelCase						:						Optional[int]		     =	inputs
         return prepared_inputs







     def lowerCAmelCase  (		self  ,      _lowerCamelCase     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						int		     =	self.get_dummy_components()
         LoraLoaderMixin.save_lora_weights(
             save_directory=_lowerCamelCase  ,      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(_lowerCamelCase  ,      'pytorch_lora_weights.bin'     )     )     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						List[str]		     =	self.get_dummy_components()
         __UpperCamelCase						:						List[str]		     =	StableDiffusionPipeline(**_lowerCamelCase     )
         __UpperCamelCase						:						Dict		     =	sd_pipe.to(_lowerCamelCase     )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

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

         __UpperCamelCase						:						Any		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						int		     =	original_images[0, -3:, -3:, -1]

         with tempfile.TemporaryDirectory() as tmpdirname:
             LoraLoaderMixin.save_lora_weights(
                 save_directory=_lowerCamelCase  ,      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(_lowerCamelCase  ,      'pytorch_lora_weights.bin'     )     )     )
             sd_pipe.load_lora_weights(_lowerCamelCase     )

         __UpperCamelCase						:						Optional[int]		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						Optional[int]		     =	lora_images[0, -3:, -3:, -1]

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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						Tuple		     =	self.get_dummy_components()
         __UpperCamelCase						:						str		     =	StableDiffusionPipeline(**_lowerCamelCase     )
         __UpperCamelCase						:						Union[str, Any]		     =	sd_pipe.to(_lowerCamelCase     )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase						:						Tuple		     =	self.get_dummy_inputs()

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

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

         __UpperCamelCase						:						Dict		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						Dict		     =	lora_images[0, -3:, -3:, -1]

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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						List[str]		     =	self.get_dummy_components()
         __UpperCamelCase						:						List[Any]		     =	lora_components['unet_lora_attn_procs']
         __UpperCamelCase						:						Union[str, Any]		     =	StableDiffusionPipeline(**_lowerCamelCase     )
         __UpperCamelCase						:						int		     =	sd_pipe.to(_lowerCamelCase     )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase						:						str		     =	self.get_dummy_inputs()

         __UpperCamelCase						:						Dict		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						Dict		     =	original_images[0, -3:, -3:, -1]

         with tempfile.TemporaryDirectory() as tmpdirname:
             __UpperCamelCase						:						Union[str, Any]		     =	sd_pipe.unet
             unet.set_attn_processor(_lowerCamelCase     )
             unet.save_attn_procs(_lowerCamelCase     )
             self.assertTrue(os.path.isfile(os.path.join(_lowerCamelCase  ,      'pytorch_lora_weights.bin'     )     )     )
             sd_pipe.load_lora_weights(_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						Optional[Any]		     =	lora_images[0, -3:, -3:, -1]

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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						Dict		     =	self.get_dummy_components()
         __UpperCamelCase						:						Any		     =	StableDiffusionPipeline(**_lowerCamelCase     )

         __UpperCamelCase						:						Any		     =	self.get_dummy_tokens()

         # inference without lora
         __UpperCamelCase						:						List[str]		     =	pipe.text_encoder(**_lowerCamelCase     )[0]
         assert outputs_without_lora.shape == (1, 7_7, 3_2)

         # create lora_attn_procs with zeroed out up.weights
         __UpperCamelCase						:						Any		     =	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
         set_lora_up_weights(_lowerCamelCase  ,      randn_weight=_lowerCamelCase     )

         # monkey patch
         pipe._modify_text_encoder(_lowerCamelCase     )

         # 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(**_lowerCamelCase     )[0]
         assert outputs_with_lora.shape == (1, 7_7, 3_2)

         assert torch.allclose(
             _lowerCamelCase  ,      _lowerCamelCase     ), "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						:						Optional[int]		     =	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
         set_lora_up_weights(_lowerCamelCase  ,      randn_weight=_lowerCamelCase     )

         # monkey patch
         pipe._modify_text_encoder(_lowerCamelCase     )

         # 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(**_lowerCamelCase     )[0]
         assert outputs_with_lora.shape == (1, 7_7, 3_2)

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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						Optional[Any]		     =	self.get_dummy_components()
         __UpperCamelCase						:						Any		     =	StableDiffusionPipeline(**_lowerCamelCase     )

         __UpperCamelCase						:						List[Any]		     =	self.get_dummy_tokens()

         # inference without lora
         __UpperCamelCase						:						str		     =	pipe.text_encoder(**_lowerCamelCase     )[0]
         assert outputs_without_lora.shape == (1, 7_7, 3_2)

         # create lora_attn_procs with randn up.weights
         __UpperCamelCase						:						Any		     =	create_text_encoder_lora_attn_procs(pipe.text_encoder     )
         set_lora_up_weights(_lowerCamelCase  ,      randn_weight=_lowerCamelCase     )

         # monkey patch
         pipe._modify_text_encoder(_lowerCamelCase     )

         # 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						:						Any		     =	pipe.text_encoder(**_lowerCamelCase     )[0]
         assert outputs_with_lora.shape == (1, 7_7, 3_2)

         assert not torch.allclose(
             _lowerCamelCase  ,      _lowerCamelCase     ), "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(**_lowerCamelCase     )[0]
         assert outputs_without_lora_removed.shape == (1, 7_7, 3_2)

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







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						Optional[int]		     =	self.get_dummy_components()
         __UpperCamelCase						:						Any		     =	StableDiffusionPipeline(**_lowerCamelCase     )
         __UpperCamelCase						:						List[str]		     =	sd_pipe.to(_lowerCamelCase     )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase						:						int		     =	self.get_dummy_inputs()

         with tempfile.TemporaryDirectory() as tmpdirname:
             LoraLoaderMixin.save_lora_weights(
                 save_directory=_lowerCamelCase  ,      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(_lowerCamelCase  ,      'pytorch_lora_weights.bin'     )     )     )
             sd_pipe.load_lora_weights(_lowerCamelCase     )

         __UpperCamelCase						:						Optional[int]		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						List[Any]		     =	lora_images[0, -3:, -3:, -1]

         __UpperCamelCase						:						List[str]		     =	sd_pipe(**_lowerCamelCase  ,      cross_attention_kwargs={'scale': 0.5}     ).images
         __UpperCamelCase						:						Union[str, Any]		     =	lora_images_with_scale[0, -3:, -3:, -1]

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







     def lowerCAmelCase  (		self     ):
         with tempfile.TemporaryDirectory() as tmpdirname:
             self.create_lora_weight_file(_lowerCamelCase     )

             __UpperCamelCase     ,       __UpperCamelCase						:						Dict		     =	self.get_dummy_components()
             __UpperCamelCase						:						Union[str, Any]		     =	StableDiffusionPipeline(**_lowerCamelCase     )
             __UpperCamelCase						:						Tuple		     =	sd_pipe.to(_lowerCamelCase     )
             sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

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

            # load LoRA weight file
             sd_pipe.load_lora_weights(_lowerCamelCase     )

             # check if lora attention processors are used
             for _, module in sd_pipe.unet.named_modules():
                 if isinstance(_lowerCamelCase  ,      _lowerCamelCase     ):
                     __UpperCamelCase						:						Union[str, Any]		     =	(
                         LoRAAttnProcessor2_0 if hasattr(_lowerCamelCase  ,      'scaled_dot_product_attention'     ) else LoRAAttnProcessor
                     )
                     self.assertIsInstance(module.processor  ,      _lowerCamelCase     )







     @unittest.skipIf(torch_device != 'cuda'  ,      'This test is supposed to run on GPU'     )
     def lowerCAmelCase  (		self     ):
         with tempfile.TemporaryDirectory() as tmpdirname:
             self.create_lora_weight_file(_lowerCamelCase     )

             __UpperCamelCase     ,       __UpperCamelCase						:						Optional[Any]		     =	self.get_dummy_components()
             __UpperCamelCase						:						Dict		     =	StableDiffusionPipeline(**_lowerCamelCase     )
             __UpperCamelCase						:						str		     =	sd_pipe.to(_lowerCamelCase     )
             sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

             # 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(_lowerCamelCase  ,      _lowerCamelCase     ):
                     self.assertIsInstance(module.processor  ,      _lowerCamelCase     )

            # load LoRA weight file
             sd_pipe.load_lora_weights(_lowerCamelCase     )

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







     @unittest.skipIf(torch_device != 'cuda'  ,      'This test is supposed to run on GPU'     )
     def lowerCAmelCase  (		self     ):
         __UpperCamelCase     ,       __UpperCamelCase						:						Union[str, Any]		     =	self.get_dummy_components()
         __UpperCamelCase						:						Dict		     =	StableDiffusionPipeline(**_lowerCamelCase     )
         __UpperCamelCase						:						str		     =	sd_pipe.to(_lowerCamelCase     )
         sd_pipe.set_progress_bar_config(disable=_lowerCamelCase     )

         __UpperCamelCase     ,       __UpperCamelCase     ,       __UpperCamelCase						:						Optional[int]		     =	self.get_dummy_inputs()

         # enable XFormers
         sd_pipe.enable_xformers_memory_efficient_attention()

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

         with tempfile.TemporaryDirectory() as tmpdirname:
             LoraLoaderMixin.save_lora_weights(
                 save_directory=_lowerCamelCase  ,      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(_lowerCamelCase  ,      'pytorch_lora_weights.bin'     )     )     )
             sd_pipe.load_lora_weights(_lowerCamelCase     )

         __UpperCamelCase						:						Union[str, Any]		     =	sd_pipe(**_lowerCamelCase     ).images
         __UpperCamelCase						:						List[Any]		     =	lora_images[0, -3:, -3:, -1]

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



