



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







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:List[str]										=      {}
		SCREAMING_SNAKE_CASE_						:Optional[Any]										=      (
		    LoRAAttnProcessor2_0 if hasattr(_snake_case ,		'scaled_dot_product_attention'     ) else LoRAAttnProcessor
		)
		for name, module in text_encoder.named_modules():
				if name.endswith(_snake_case     ):
						SCREAMING_SNAKE_CASE_						:Dict										=      lora_attn_processor_class(
						    hidden_size=module.out_proj.out_features ,		cross_attention_dim=_snake_case     )
		return text_lora_attn_procs







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE     ):
		SCREAMING_SNAKE_CASE_						:Tuple										=      create_text_encoder_lora_attn_procs(_snake_case     )
		SCREAMING_SNAKE_CASE_						:Optional[int]										=      AttnProcsLayers(_snake_case     )
		return text_encoder_lora_layers







def  SCREAMING_SNAKE_CASE__ (				SCREAMING_SNAKE_CASE ,		SCREAMING_SNAKE_CASE=False     ):
		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'     ):
								SCREAMING_SNAKE_CASE_						:Dict										=      (
								    torch.randn_like(layer_module.up.weight     )
								    if randn_weight
								    else torch.zeros_like(layer_module.up.weight     )
								)
								SCREAMING_SNAKE_CASE_						:Optional[int]										=      torch.nn.Parameter(_snake_case     )





class 				__lowerCAmelCase(							unittest.TestCase      ):






			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					torch.manual_seed(0	)
					SCREAMING_SNAKE_CASE_						:str										=      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      ,       )
					SCREAMING_SNAKE_CASE_						:Dict										=      DDIMScheduler(
					    beta_start=0.0_00_85      ,       beta_end=0.0_12      ,       beta_schedule='scaled_linear'      ,       clip_sample=UpperCamelCase_      ,       set_alpha_to_one=UpperCamelCase_      ,       steps_offset=1      ,       )
					torch.manual_seed(0	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      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      ,       )
					SCREAMING_SNAKE_CASE_						:str										=      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=1_000      ,       )
					SCREAMING_SNAKE_CASE_						:List[Any]										=      CLIPTextModel(UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:Dict										=      CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'	)

					SCREAMING_SNAKE_CASE_						:List[str]										=      create_unet_lora_layers(UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:int										=      create_text_encoder_lora_layers(UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Any										=      {
					    '''unet''': unet,
					    '''scheduler''': scheduler,
					    '''vae''': vae,
					    '''text_encoder''': text_encoder,
					    '''tokenizer''': tokenizer,
					    '''safety_checker''': None,
					    '''feature_extractor''': None,
					}
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      {
					    '''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  :							List[str]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      1
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      10
					SCREAMING_SNAKE_CASE_						:List[str]										=      4
					SCREAMING_SNAKE_CASE_						:Dict										=      (32, 32)

					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      torch.manual_seed(0	)
					SCREAMING_SNAKE_CASE_						:Any										=      floats_tensor((batch_size, num_channels) + sizes	)
					SCREAMING_SNAKE_CASE_						:Tuple										=      torch.randint(1      ,       UpperCamelCase_      ,       size=(batch_size, sequence_length)      ,       generator=UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Optional[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 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      77

					SCREAMING_SNAKE_CASE_						:str										=      torch.randint(2      ,       56      ,       size=(1, max_seq_length)      ,       generator=torch.manual_seed(0	)	)

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      {}
					SCREAMING_SNAKE_CASE_						:List[str]										=      inputs
					return prepared_inputs






			def 				_lowercase	(   self  :							int      ,       SCREAMING_SNAKE_CASE  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      self.get_dummy_components()
					LoraLoaderMixin.save_lora_weights(
					    save_directory=UpperCamelCase_      ,       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(UpperCamelCase_      ,       'pytorch_lora_weights.bin'	)	)	)






			def 				_lowercase	(   self  :							Optional[int]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:str										=      StableDiffusionPipeline(**UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      sd_pipe.to(UpperCamelCase_	)
					sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Any										=      self.get_dummy_inputs()

					SCREAMING_SNAKE_CASE_						:Tuple										=      sd_pipe(**UpperCamelCase_	).images
					SCREAMING_SNAKE_CASE_						:int										=      original_images[0, -3:, -3:, -1]

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

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

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






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:int										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Dict										=      StableDiffusionPipeline(**UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:List[str]										=      sd_pipe.to(UpperCamelCase_	)
					sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Any										=      self.get_dummy_inputs()

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      sd_pipe(**UpperCamelCase_	).images
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      original_images[0, -3:, -3:, -1]

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

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

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






			def 				_lowercase	(   self  :							Union[str, Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Tuple										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Dict										=      lora_components['''unet_lora_attn_procs''']
					SCREAMING_SNAKE_CASE_						:str										=      StableDiffusionPipeline(**UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:str										=      sd_pipe.to(UpperCamelCase_	)
					sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:List[str]										=      self.get_dummy_inputs()

					SCREAMING_SNAKE_CASE_						:List[str]										=      sd_pipe(**UpperCamelCase_	).images
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      original_images[0, -3:, -3:, -1]

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

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

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






			def 				_lowercase	(   self  :							List[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:str										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      StableDiffusionPipeline(**UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.get_dummy_tokens()

					# inference without lora
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      pipe.text_encoder(**UpperCamelCase_	)[0]
					assert outputs_without_lora.shape == (1, 77, 32)

					# create lora_attn_procs with zeroed out up.weights
					SCREAMING_SNAKE_CASE_						:Tuple										=      create_text_encoder_lora_attn_procs(pipe.text_encoder	)
					set_lora_up_weights(UpperCamelCase_      ,       randn_weight=UpperCamelCase_	)

					# monkey patch
					pipe._modify_text_encoder(UpperCamelCase_	)

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

					# inference with lora
					SCREAMING_SNAKE_CASE_						:Any										=      pipe.text_encoder(**UpperCamelCase_	)[0]
					assert outputs_with_lora.shape == (1, 77, 32)

					assert torch.allclose(
					    UpperCamelCase_      ,       UpperCamelCase_	), "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
					SCREAMING_SNAKE_CASE_						:Any										=      create_text_encoder_lora_attn_procs(pipe.text_encoder	)
					set_lora_up_weights(UpperCamelCase_      ,       randn_weight=UpperCamelCase_	)

					# monkey patch
					pipe._modify_text_encoder(UpperCamelCase_	)

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

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

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






			def 				_lowercase	(   self  :							Optional[Any]	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      StableDiffusionPipeline(**UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:Dict										=      self.get_dummy_tokens()

					# inference without lora
					SCREAMING_SNAKE_CASE_						:List[str]										=      pipe.text_encoder(**UpperCamelCase_	)[0]
					assert outputs_without_lora.shape == (1, 77, 32)

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

					# monkey patch
					pipe._modify_text_encoder(UpperCamelCase_	)

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

					# inference with lora
					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      pipe.text_encoder(**UpperCamelCase_	)[0]
					assert outputs_with_lora.shape == (1, 77, 32)

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

					# remove monkey patch
					pipe._remove_text_encoder_monkey_patch()

					# inference with removed lora
					SCREAMING_SNAKE_CASE_						:List[str]										=      pipe.text_encoder(**UpperCamelCase_	)[0]
					assert outputs_without_lora_removed.shape == (1, 77, 32)

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






			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:List[Any]										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      StableDiffusionPipeline(**UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sd_pipe.to(UpperCamelCase_	)
					sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

					SCREAMING_SNAKE_CASE_						:int										=      self.get_dummy_inputs()

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

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

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

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






			def 				_lowercase	(   self  :							List[str]	):




					"""simple docstring"""







					with tempfile.TemporaryDirectory() as tmpdirname:
							self.create_lora_weight_file(UpperCamelCase_	)

							SCREAMING_SNAKE_CASE_						:List[str]										=      self.get_dummy_components()
							SCREAMING_SNAKE_CASE_						:List[str]										=      StableDiffusionPipeline(**UpperCamelCase_	)
							SCREAMING_SNAKE_CASE_						:Dict										=      sd_pipe.to(UpperCamelCase_	)
							sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

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

            # load LoRA weight file
							sd_pipe.load_lora_weights(UpperCamelCase_	)

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






			@unittest.skipIf(torch_device != 'cuda'      ,       'This test is supposed to run on GPU'	)
			def 				_lowercase	(   self  :							str	):




					"""simple docstring"""







					with tempfile.TemporaryDirectory() as tmpdirname:
							self.create_lora_weight_file(UpperCamelCase_	)

							SCREAMING_SNAKE_CASE_						:List[str]										=      self.get_dummy_components()
							SCREAMING_SNAKE_CASE_						:Optional[Any]										=      StableDiffusionPipeline(**UpperCamelCase_	)
							SCREAMING_SNAKE_CASE_						:List[str]										=      sd_pipe.to(UpperCamelCase_	)
							sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

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

            # load LoRA weight file
							sd_pipe.load_lora_weights(UpperCamelCase_	)

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






			@unittest.skipIf(torch_device != 'cuda'      ,       'This test is supposed to run on GPU'	)
			def 				_lowercase	(   self  :							int	):




					"""simple docstring"""







					SCREAMING_SNAKE_CASE_						:Union[str, Any]										=      self.get_dummy_components()
					SCREAMING_SNAKE_CASE_						:Tuple										=      StableDiffusionPipeline(**UpperCamelCase_	)
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      sd_pipe.to(UpperCamelCase_	)
					sd_pipe.set_progress_bar_config(disable=UpperCamelCase_	)

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

					# enable XFormers
					sd_pipe.enable_xformers_memory_efficient_attention()

					SCREAMING_SNAKE_CASE_						:List[Any]										=      sd_pipe(**UpperCamelCase_	).images
					SCREAMING_SNAKE_CASE_						:Optional[int]										=      original_images[0, -3:, -3:, -1]

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

					SCREAMING_SNAKE_CASE_						:Any										=      sd_pipe(**UpperCamelCase_	).images
					SCREAMING_SNAKE_CASE_						:Optional[Any]										=      lora_images[0, -3:, -3:, -1]

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




"""simple docstring"""





import unittest

from transformers import ErnieMConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     ErnieMForInformationExtraction,
     ErnieMForMultipleChoice,
     ErnieMForQuestionAnswering,
     ErnieMForSequenceClassification,
     ErnieMForTokenClassification,
     ErnieMModel,
 )
 from transformers.models.ernie_m.modeling_ernie_m import ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST



class 		snake_case_:






     def __init__(						self	:       Optional[int]       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       Optional[int]=1_3       ,					UpperCamelCase_	:       Dict=7       ,					UpperCamelCase_	:       Dict=True       ,					UpperCamelCase_	:       str=True       ,					UpperCamelCase_	:       int=True       ,					UpperCamelCase_	:       Optional[Any]=9_9       ,					UpperCamelCase_	:       Optional[int]=3_2       ,					UpperCamelCase_	:       Any=5       ,					UpperCamelCase_	:       Any=4       ,					UpperCamelCase_	:       Tuple=3_7       ,					UpperCamelCase_	:       Tuple="gelu"       ,					UpperCamelCase_	:       Union[str, Any]=0.1       ,					UpperCamelCase_	:       List[Any]=0.1       ,					UpperCamelCase_	:       int=5_1_2       ,					UpperCamelCase_	:       int=1_6       ,					UpperCamelCase_	:       List[Any]=2       ,					UpperCamelCase_	:       str=0.02       ,					UpperCamelCase_	:       int=3       ,					UpperCamelCase_	:       str=4       ,					UpperCamelCase_	:       Union[str, Any]=None       ,					):
           lowerCAmelCase       :						List[str]							= parent
           lowerCAmelCase       :						str							= batch_size
           lowerCAmelCase       :						Dict							= seq_length
           lowerCAmelCase       :						Dict							= is_training
           lowerCAmelCase       :						Tuple							= use_input_mask
           lowerCAmelCase       :						str							= use_labels
           lowerCAmelCase       :						Optional[Any]							= vocab_size
           lowerCAmelCase       :						Optional[Any]							= hidden_size
           lowerCAmelCase       :						Optional[int]							= num_hidden_layers
           lowerCAmelCase       :						List[Any]							= num_attention_heads
           lowerCAmelCase       :						Tuple							= intermediate_size
           lowerCAmelCase       :						Tuple							= hidden_act
           lowerCAmelCase       :						Optional[int]							= hidden_dropout_prob
           lowerCAmelCase       :						str							= attention_probs_dropout_prob
           lowerCAmelCase       :						str							= max_position_embeddings
           lowerCAmelCase       :						List[str]							= type_vocab_size
           lowerCAmelCase       :						str							= type_sequence_label_size
           lowerCAmelCase       :						int							= initializer_range
           lowerCAmelCase       :						Optional[Any]							= num_labels
           lowerCAmelCase       :						Any							= num_choices
           lowerCAmelCase       :						str							= scope






     def   lowerCamelCase__  (						self	:       Dict					):
           lowerCAmelCase       :						str							= ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size					)

           lowerCAmelCase       :						List[Any]							= None
           if self.use_input_mask:
                 lowerCAmelCase       :						str							= random_attention_mask([self.batch_size, self.seq_length]					)

           lowerCAmelCase       :						List[Any]							= None
           lowerCAmelCase       :						str							= None
           lowerCAmelCase       :						str							= None
           if self.use_labels:
                 lowerCAmelCase       :						Tuple							= ids_tensor([self.batch_size]       ,					self.type_sequence_label_size					)
                 lowerCAmelCase       :						Dict							= ids_tensor([self.batch_size, self.seq_length]       ,					self.num_labels					)
                 lowerCAmelCase       :						List[Any]							= ids_tensor([self.batch_size]       ,					self.num_choices					)

           lowerCAmelCase       :						Any							= self.get_config()

           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels






     def   lowerCamelCase__  (						self	:       str					):
           lowerCAmelCase       :						Tuple							= ids_tensor([self.batch_size, self.seq_length]       ,					self.vocab_size					)

           lowerCAmelCase       :						Optional[Any]							= None
           if self.use_input_mask:
                 lowerCAmelCase       :						Union[str, Any]							= random_attention_mask([self.batch_size, self.seq_length]					)
           lowerCAmelCase       :						Dict							= self.get_config()

           return config, input_ids, input_mask






     def   lowerCamelCase__  (						self	:       Union[str, Any]					):
           return ErnieMConfig(
               vocab_size=self.vocab_size       ,					hidden_size=self.hidden_size       ,					num_hidden_layers=self.num_hidden_layers       ,					num_attention_heads=self.num_attention_heads       ,					intermediate_size=self.intermediate_size       ,					hidden_act=self.hidden_act       ,					hidden_dropout_prob=self.hidden_dropout_prob       ,					attention_probs_dropout_prob=self.attention_probs_dropout_prob       ,					max_position_embeddings=self.max_position_embeddings       ,					type_vocab_size=self.type_vocab_size       ,					initializer_range=self.initializer_range       ,					)






     def   lowerCamelCase__  (						self	:       Union[str, Any]       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       Union[str, Any]       ,					UpperCamelCase_	:       Optional[Any]       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       Union[str, Any]       ,					UpperCamelCase_	:       Optional[Any]					):
           lowerCAmelCase       :						List[str]							= ErnieMModel(config=UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           lowerCAmelCase       :						List[Any]							= model(UpperCamelCase_       ,					return_dict=UpperCamelCase_					)
           self.parent.assertEqual(result.last_hidden_state.shape       ,					(self.batch_size, self.seq_length, self.hidden_size)					)






     def   lowerCamelCase__  (						self	:       Union[str, Any]       ,					UpperCamelCase_	:       Union[str, Any]       ,					UpperCamelCase_	:       Optional[Any]       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       List[Any]       ,					UpperCamelCase_	:       List[Any]       ,					UpperCamelCase_	:       Optional[Any]					):
           lowerCAmelCase       :						Optional[int]							= ErnieMForQuestionAnswering(config=UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           lowerCAmelCase       :						List[Any]							= model(
               UpperCamelCase_       ,					attention_mask=UpperCamelCase_       ,					start_positions=UpperCamelCase_       ,					end_positions=UpperCamelCase_       ,					)
           self.parent.assertEqual(result.start_logits.shape       ,					(self.batch_size, self.seq_length)					)
           self.parent.assertEqual(result.end_logits.shape       ,					(self.batch_size, self.seq_length)					)






     def   lowerCamelCase__  (						self	:       Dict       ,					UpperCamelCase_	:       Optional[int]       ,					UpperCamelCase_	:       Any       ,					UpperCamelCase_	:       List[str]       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       Optional[Any]       ,					UpperCamelCase_	:       Union[str, Any]					):
           lowerCAmelCase       :						Tuple							= ErnieMForInformationExtraction(config=UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           lowerCAmelCase       :						Tuple							= torch.ones_like(UpperCamelCase_       ,					dtype=torch.floataa					)
           lowerCAmelCase       :						str							= model(
               UpperCamelCase_       ,					attention_mask=UpperCamelCase_       ,					start_positions=UpperCamelCase_       ,					end_positions=UpperCamelCase_       ,					)
           self.parent.assertEqual(result.start_logits.shape       ,					(self.batch_size, self.seq_length)					)
           self.parent.assertEqual(result.end_logits.shape       ,					(self.batch_size, self.seq_length)					)






     def   lowerCamelCase__  (						self	:       Optional[int]       ,					UpperCamelCase_	:       Optional[int]       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       Union[str, Any]       ,					UpperCamelCase_	:       Any					):
           lowerCAmelCase       :						Union[str, Any]							= self.num_labels
           lowerCAmelCase       :						Tuple							= ErnieMForSequenceClassification(UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           lowerCAmelCase       :						Union[str, Any]							= model(UpperCamelCase_       ,					attention_mask=UpperCamelCase_       ,					labels=UpperCamelCase_					)
           self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_labels)					)






     def   lowerCamelCase__  (						self	:       Optional[int]       ,					UpperCamelCase_	:       Any       ,					UpperCamelCase_	:       List[str]       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       List[str]					):
           lowerCAmelCase       :						Union[str, Any]							= self.num_labels
           lowerCAmelCase       :						List[Any]							= ErnieMForTokenClassification(config=UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           input_ids.to(UpperCamelCase_					)
           input_mask.to(UpperCamelCase_					)
           token_labels.to(UpperCamelCase_					)

           lowerCAmelCase       :						Optional[Any]							= model(UpperCamelCase_       ,					attention_mask=UpperCamelCase_       ,					labels=UpperCamelCase_					)

           self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.seq_length, self.num_labels)					)






     def   lowerCamelCase__  (						self	:       Any       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       Optional[int]       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       Dict       ,					UpperCamelCase_	:       str					):
           lowerCAmelCase       :						Union[str, Any]							= self.num_choices
           lowerCAmelCase       :						Any							= ErnieMForMultipleChoice(config=UpperCamelCase_					)
           model.to(UpperCamelCase_					)
           model.eval()
           lowerCAmelCase       :						Any							= input_ids.unsqueeze(1					).expand(-1       ,					self.num_choices       ,					-1					).contiguous()
           lowerCAmelCase       :						int							= input_mask.unsqueeze(1					).expand(-1       ,					self.num_choices       ,					-1					).contiguous()
           lowerCAmelCase       :						Any							= model(
               UpperCamelCase_       ,					attention_mask=UpperCamelCase_       ,					labels=UpperCamelCase_       ,					)
           self.parent.assertEqual(result.logits.shape       ,					(self.batch_size, self.num_choices)					)






     def   lowerCamelCase__  (						self	:       Tuple					):
           lowerCAmelCase       :						List[Any]							= self.prepare_config_and_inputs()
           (
               (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   (
               lowerCAmelCase
           ),   
           )       :						str							= config_and_inputs
           lowerCAmelCase       :						Dict							= {'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict



@require_torch
class 		snake_case_(							a__				,       a__				,       unittest.TestCase      ):
     __UpperCamelCase	=       (
         (
             ErnieMModel,
             ErnieMForMultipleChoice,
             ErnieMForQuestionAnswering,
             ErnieMForSequenceClassification,
             ErnieMForTokenClassification,
         )
         if is_torch_available()
         else ()
     )
     __UpperCamelCase	=       ()
     __UpperCamelCase	=       (
         {
             '''feature-extraction''': ErnieMModel,
             '''question-answering''': ErnieMForQuestionAnswering,
             '''text-classification''': ErnieMForSequenceClassification,
             '''token-classification''': ErnieMForTokenClassification,
             '''zero-shot''': ErnieMForSequenceClassification,
         }
         if is_torch_available()
         else {}
     )
     __UpperCamelCase	=       False






     def   lowerCamelCase__  (						self	:       Optional[Any]       ,					UpperCamelCase_	:       str       ,					UpperCamelCase_	:       List[Any]       ,					UpperCamelCase_	:       Tuple       ,					UpperCamelCase_	:       int       ,					UpperCamelCase_	:       Any					):
           if pipeline_test_casse_name == "QAPipelineTests":
                 return True

           return False






     def   lowerCamelCase__  (						self	:       Dict					):
           lowerCAmelCase       :						str							= ErnieMModelTester(self					)
           lowerCAmelCase       :						Optional[Any]							= ConfigTester(self       ,					config_class=UpperCamelCase_       ,					hidden_size=3_7					)






     def   lowerCamelCase__  (						self	:       Tuple					):
           self.config_tester.run_common_tests()






     def   lowerCamelCase__  (						self	:       List[str]					):
           lowerCAmelCase       :						str							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Union[str, Any]					):
           lowerCAmelCase       :						Optional[Any]							= self.model_tester.prepare_config_and_inputs()
           for type in ["absolute", "relative_key", "relative_key_query"]:
                 lowerCAmelCase       :						Union[str, Any]							= type
                 self.model_tester.create_and_check_model(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Tuple					):
           lowerCAmelCase       :						Dict							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_multiple_choice(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Dict					):
           lowerCAmelCase       :						int							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_question_answering(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Optional[int]					):
           lowerCAmelCase       :						Tuple							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_sequence_classification(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       Tuple					):
           lowerCAmelCase       :						List[str]							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_information_extraction(*UpperCamelCase_					)






     def   lowerCamelCase__  (						self	:       List[Any]					):
           lowerCAmelCase       :						str							= self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_token_classification(*UpperCamelCase_					)






     @slow
     def   lowerCamelCase__  (						self	:       List[str]					):
           for model_name in ERNIE_M_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 lowerCAmelCase       :						int							= ErnieMModel.from_pretrained(UpperCamelCase_					)
                 self.assertIsNotNone(UpperCamelCase_					)



@require_torch
class 		snake_case_(							unittest.TestCase      ):






     @slow
     def   lowerCamelCase__  (						self	:       Dict					):
           lowerCAmelCase       :						Optional[int]							= ErnieMModel.from_pretrained('''susnato/ernie-m-base_pytorch'''					)
           model.eval()
           lowerCAmelCase       :						Optional[int]							= torch.tensor([[0, 1, 2, 3, 4, 5]]					)
           lowerCAmelCase       :						str							= model(UpperCamelCase_					)[0]

           # TODO Replace vocab size
           lowerCAmelCase       :						Any							= 7_6_8

           lowerCAmelCase       :						int							= torch.Size((1, 6, hidden_size)					)
           self.assertEqual(output.shape       ,					UpperCamelCase_					)

           lowerCAmelCase       :						str							= torch.tensor(
               [[[-0.0_012, 0.1_245, -0.0_214], [-0.0_742, 0.0_244, -0.0_771], [-0.0_333, 0.1_164, -0.1_554]]]					)

           self.assertTrue(torch.allclose(output[:, :3, :3]       ,					UpperCamelCase_       ,					atol=1E-3					)					)


