



'''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 	lowercase_						( lowerCAmelCase__						:	nn.Module			):

			"""simple docstring"""





			__UpperCAmelCase :   str    					=  {}
			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 :   Tuple    					=  unet.config.block_out_channels[-1]
						elif name.startswith("""up_blocks"""			):
									__UpperCAmelCase :   Tuple    					=  int(name[len("""up_blocks."""			)]			)
									__UpperCAmelCase :   List[Any]    					=  list(reversed(unet.config.block_out_channels			)			)[block_id]
						elif name.startswith("""down_blocks"""			):
									__UpperCAmelCase :   str    					=  int(name[len("""down_blocks."""			)]			)
									__UpperCAmelCase :   Union[str, Any]    					=  unet.config.block_out_channels[block_id]
						__UpperCAmelCase :   Optional[Any]    					=  (
						    LoRAAttnProcessor2_0 if hasattr(lowerCAmelCase__      ,				"""scaled_dot_product_attention"""			) else LoRAAttnProcessor
						)
						__UpperCAmelCase :   Dict    					=  lora_attn_processor_class(
						    hidden_size=lowerCAmelCase__      ,				cross_attention_dim=lowerCAmelCase__			)
			__UpperCAmelCase :   str    					=  AttnProcsLayers(lowerCAmelCase__			)
			return lora_attn_procs, unet_lora_layers


def 	lowercase_						( lowerCAmelCase__						:	nn.Module			):

			"""simple docstring"""





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


def 	lowercase_						( lowerCAmelCase__						:	nn.Module			):

			"""simple docstring"""





			__UpperCAmelCase :   int    					=  create_text_encoder_lora_attn_procs(lowerCAmelCase__			)
			__UpperCAmelCase :   Optional[int]    					=  AttnProcsLayers(lowerCAmelCase__			)
			return text_encoder_lora_layers


def 	lowercase_						( lowerCAmelCase__						:	Dict      ,				lowerCAmelCase__						:	Dict=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 :   int    					=  (
												    torch.randn_like(layer_module.up.weight			)
												    if randn_weight
												    else torch.zeros_like(layer_module.up.weight			)
												)
												__UpperCAmelCase :   Optional[int]    					=  torch.nn.Parameter(lowerCAmelCase__			)






class _A	(     unittest.TestCase     ):





		def 				__A							(  self					)							->  int:

					'''simple docstring'''
					torch.manual_seed(0					)
					__UpperCAmelCase :   Dict    					=  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 :   Optional[int]    					=  DDIMScheduler(
					    beta_start=0.0_0085			,							beta_end=0.012			,							beta_schedule="""scaled_linear"""			,							clip_sample=__UpperCAmelCase			,							set_alpha_to_one=__UpperCAmelCase			,							steps_offset=1			,							)
					torch.manual_seed(0					)
					__UpperCAmelCase :   Optional[int]    					=  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 :   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=1_000			,							)
					__UpperCAmelCase :   int    					=  CLIPTextModel(__UpperCAmelCase					)
					__UpperCAmelCase :   Optional[Any]    					=  CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""					)

					__UpperCAmelCase					,	__UpperCAmelCase :   Optional[Any]    					=  create_unet_lora_layers(__UpperCAmelCase					)
					__UpperCAmelCase :   List[Any]    					=  create_text_encoder_lora_layers(__UpperCAmelCase					)

					__UpperCAmelCase :   Union[str, Any]    					=  {
					    """unet""": unet,
					    """scheduler""": scheduler,
					    """vae""": vae,
					    """text_encoder""": text_encoder,
					    """tokenizer""": tokenizer,
					    """safety_checker""": None,
					    """feature_extractor""": None,
					}
					__UpperCAmelCase :   Dict    					=  {
					    """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 				__A							(  self					)							->  List[str]:

					'''simple docstring'''
					__UpperCAmelCase :   List[Any]    					=  1
					__UpperCAmelCase :   Any    					=  10
					__UpperCAmelCase :   Dict    					=  4
					__UpperCAmelCase :   str    					=  (32, 32)

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

					__UpperCAmelCase :   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 				__A							(  self					)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   Optional[Any]    					=  77

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

					__UpperCAmelCase :   List[Any]    					=  {}
					__UpperCAmelCase :   Optional[Any]    					=  inputs
					return prepared_inputs





		def 				__A							(  self			,							__UpperCAmelCase					)							->  List[str]:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   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 				__A							(  self					)							->  Dict:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Union[str, Any]    					=  self.get_dummy_components()
					__UpperCAmelCase :   Any    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
					__UpperCAmelCase :   List[Any]    					=  sd_pipe.to(__UpperCAmelCase					)
					sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

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

					__UpperCAmelCase :   List[Any]    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   Dict    					=  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					)

					__UpperCAmelCase :   Tuple    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   Tuple    					=  lora_images[0, -3:, -3:, -1]

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





		def 				__A							(  self					)							->  Any:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Union[str, Any]    					=  self.get_dummy_components()
					__UpperCAmelCase :   List[str]    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
					__UpperCAmelCase :   Dict    					=  sd_pipe.to(__UpperCAmelCase					)
					sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

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

					__UpperCAmelCase :   int    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   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					)

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

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





		def 				__A							(  self					)							->  List[str]:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Optional[int]    					=  self.get_dummy_components()
					__UpperCAmelCase :   List[str]    					=  lora_components["""unet_lora_attn_procs"""]
					__UpperCAmelCase :   List[Any]    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
					__UpperCAmelCase :   int    					=  sd_pipe.to(__UpperCAmelCase					)
					sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

					__UpperCAmelCase					,	__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  self.get_dummy_inputs()

					__UpperCAmelCase :   List[str]    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   Tuple    					=  original_images[0, -3:, -3:, -1]

					with tempfile.TemporaryDirectory() as tmpdirname:
								__UpperCAmelCase :   Optional[int]    					=  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					)

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

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





		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  self.get_dummy_components()
					__UpperCAmelCase :   Union[str, Any]    					=  StableDiffusionPipeline(**__UpperCAmelCase					)

					__UpperCAmelCase :   Any    					=  self.get_dummy_tokens()

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

					# create lora_attn_procs with zeroed out up.weights
					__UpperCAmelCase :   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
					__UpperCAmelCase :   List[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
					__UpperCAmelCase :   str    					=  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
					__UpperCAmelCase :   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 				__A							(  self					)							->  str:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Any    					=  self.get_dummy_components()
					__UpperCAmelCase :   Any    					=  StableDiffusionPipeline(**__UpperCAmelCase					)

					__UpperCAmelCase :   Optional[int]    					=  self.get_dummy_tokens()

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

					# create lora_attn_procs with randn up.weights
					__UpperCAmelCase :   List[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
					__UpperCAmelCase :   Dict    					=  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
					__UpperCAmelCase :   List[Any]    					=  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 				__A							(  self					)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   int    					=  self.get_dummy_components()
					__UpperCAmelCase :   int    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
					__UpperCAmelCase :   List[Any]    					=  sd_pipe.to(__UpperCAmelCase					)
					sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

					__UpperCAmelCase					,	__UpperCAmelCase					,	__UpperCAmelCase :   Optional[Any]    					=  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					)

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

					__UpperCAmelCase :   Tuple    					=  sd_pipe(**__UpperCAmelCase			,							cross_attention_kwargs={"""scale""": 0.5}					).images
					__UpperCAmelCase :   Optional[int]    					=  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 				__A							(  self					)							->  Optional[int]:

					'''simple docstring'''
					with tempfile.TemporaryDirectory() as tmpdirname:
								self.create_lora_weight_file(__UpperCAmelCase					)

								__UpperCAmelCase					,	__UpperCAmelCase :   List[Any]    					=  self.get_dummy_components()
								__UpperCAmelCase :   Dict    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
								__UpperCAmelCase :   List[str]    					=  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					):
														__UpperCAmelCase :   Tuple    					=  (
														    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 				__A							(  self					)							->  Union[str, Any]:

					'''simple docstring'''
					with tempfile.TemporaryDirectory() as tmpdirname:
								self.create_lora_weight_file(__UpperCAmelCase					)

								__UpperCAmelCase					,	__UpperCAmelCase :   Any    					=  self.get_dummy_components()
								__UpperCAmelCase :   List[Any]    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
								__UpperCAmelCase :   Optional[int]    					=  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 				__A							(  self					)							->  Any:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Tuple    					=  self.get_dummy_components()
					__UpperCAmelCase :   List[Any]    					=  StableDiffusionPipeline(**__UpperCAmelCase					)
					__UpperCAmelCase :   Dict    					=  sd_pipe.to(__UpperCAmelCase					)
					sd_pipe.set_progress_bar_config(disable=__UpperCAmelCase					)

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

					# enable XFormers
					sd_pipe.enable_xformers_memory_efficient_attention()

					__UpperCAmelCase :   List[Any]    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   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					)

					__UpperCAmelCase :   str    					=  sd_pipe(**__UpperCAmelCase					).images
					__UpperCAmelCase :   str    					=  lora_images[0, -3:, -3:, -1]

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









'''simple docstring'''
from __future__ import annotations

import inspect
import unittest
from typing import List, Tuple

from transformers import ConvNextConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFConvNextForImageClassification, TFConvNextModel


if is_vision_available():
					from PIL import Image

					from transformers import ConvNextImageProcessor



class _A	:





		def __init__(  self			,							__UpperCAmelCase			,							__UpperCAmelCase=13			,							__UpperCAmelCase=32			,							__UpperCAmelCase=3			,							__UpperCAmelCase=4			,							__UpperCAmelCase=[10, 20, 30, 40]			,							__UpperCAmelCase=[2, 2, 3, 2]			,							__UpperCAmelCase=True			,							__UpperCAmelCase=True			,							__UpperCAmelCase=37			,							__UpperCAmelCase="gelu"			,							__UpperCAmelCase=10			,							__UpperCAmelCase=0.02			,							__UpperCAmelCase=3			,							__UpperCAmelCase=None			,							)							->  Any:

					'''simple docstring'''
					__UpperCAmelCase :   Optional[int]    					=  parent
					__UpperCAmelCase :   Optional[Any]    					=  batch_size
					__UpperCAmelCase :   List[str]    					=  image_size
					__UpperCAmelCase :   Optional[Any]    					=  num_channels
					__UpperCAmelCase :   int    					=  num_stages
					__UpperCAmelCase :   List[Any]    					=  hidden_sizes
					__UpperCAmelCase :   Optional[Any]    					=  depths
					__UpperCAmelCase :   List[str]    					=  is_training
					__UpperCAmelCase :   Tuple    					=  use_labels
					__UpperCAmelCase :   Optional[Any]    					=  intermediate_size
					__UpperCAmelCase :   Dict    					=  hidden_act
					__UpperCAmelCase :   List[str]    					=  type_sequence_label_size
					__UpperCAmelCase :   Union[str, Any]    					=  initializer_range
					__UpperCAmelCase :   Optional[Any]    					=  scope





		def 				__A							(  self					)							->  Optional[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   List[Any]    					=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

					__UpperCAmelCase :   str    					=  None
					if self.use_labels:
								__UpperCAmelCase :   Optional[Any]    					=  ids_tensor([self.batch_size]			,							self.type_sequence_label_size					)

					__UpperCAmelCase :   Optional[int]    					=  self.get_config()

					return config, pixel_values, labels





		def 				__A							(  self					)							->  Optional[Any]:

					'''simple docstring'''
					return ConvNextConfig(
					    num_channels=self.num_channels			,							hidden_sizes=self.hidden_sizes			,							depths=self.depths			,							num_stages=self.num_stages			,							hidden_act=self.hidden_act			,							is_decoder=__UpperCAmelCase			,							initializer_range=self.initializer_range			,							)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   List[Any]    					=  TFConvNextModel(config=__UpperCAmelCase					)
					__UpperCAmelCase :   List[Any]    					=  model(__UpperCAmelCase			,							training=__UpperCAmelCase					)
					# expected last hidden states: B, C, H // 32, W // 32
					self.parent.assertEqual(
					    result.last_hidden_state.shape			,							(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)			,							)





		def 				__A							(  self			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)							->  Optional[int]:

					'''simple docstring'''
					__UpperCAmelCase :   Optional[int]    					=  self.type_sequence_label_size
					__UpperCAmelCase :   List[Any]    					=  TFConvNextForImageClassification(__UpperCAmelCase					)
					__UpperCAmelCase :   List[str]    					=  model(__UpperCAmelCase			,							labels=__UpperCAmelCase			,							training=__UpperCAmelCase					)
					self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.type_sequence_label_size)					)





		def 				__A							(  self					)							->  Union[str, Any]:

					'''simple docstring'''
					__UpperCAmelCase :   List[str]    					=  self.prepare_config_and_inputs()
					__UpperCAmelCase					,	__UpperCAmelCase					,	__UpperCAmelCase :   Optional[int]    					=  config_and_inputs
					__UpperCAmelCase :   Union[str, Any]    					=  {"""pixel_values""": pixel_values}
					return config, inputs_dict



@require_tf
class _A	(     __SCREAMING_SNAKE_CASE						,					__SCREAMING_SNAKE_CASE						,					unittest.TestCase     ):
		_SCREAMING_SNAKE_CASE       :     Dict       =      (TFConvNextModel, TFConvNextForImageClassification) if is_tf_available() else ()
		_SCREAMING_SNAKE_CASE       :     Optional[Any]       =      (
		    {"feature-extraction": TFConvNextModel, "image-classification": TFConvNextForImageClassification}
		    if is_tf_available()
		    else {}
		)

		_SCREAMING_SNAKE_CASE       :     Tuple       =      False
		_SCREAMING_SNAKE_CASE       :     Tuple       =      False
		_SCREAMING_SNAKE_CASE       :     Union[str, Any]       =      False
		_SCREAMING_SNAKE_CASE       :     Optional[Any]       =      False
		_SCREAMING_SNAKE_CASE       :     str       =      False





		def 				__A							(  self					)							->  List[Any]:

					'''simple docstring'''
					__UpperCAmelCase :   Dict    					=  TFConvNextModelTester(self					)
					__UpperCAmelCase :   Optional[int]    					=  ConfigTester(
					    self			,							config_class=__UpperCAmelCase			,							has_text_modality=__UpperCAmelCase			,							hidden_size=37			,							)





		@unittest.skip(reason="""ConvNext does not use inputs_embeds"""					)
		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					pass





		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""					)					) == 0			,							reason="""TF does not support backprop for grouped convolutions on CPU."""			,							)
		@slow
		def 				__A							(  self					)							->  Optional[int]:

					'''simple docstring'''
					super().test_keras_fit()





		@unittest.skip(reason="""ConvNext does not support input and output embeddings"""					)
		def 				__A							(  self					)							->  Dict:

					'''simple docstring'''
					pass





		def 				__A							(  self					)							->  str:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   List[str]    					=  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								__UpperCAmelCase :   List[Any]    					=  model_class(__UpperCAmelCase					)
								__UpperCAmelCase :   str    					=  inspect.signature(model.call					)
								# signature.parameters is an OrderedDict => so arg_names order is deterministic
								__UpperCAmelCase :   Tuple    					=  [*signature.parameters.keys()]

								__UpperCAmelCase :   Tuple    					=  ["""pixel_values"""]
								self.assertListEqual(arg_names[:1]			,							__UpperCAmelCase					)





		def 				__A							(  self					)							->  Dict:

					'''simple docstring'''
					__UpperCAmelCase :   Dict    					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*__UpperCAmelCase					)





		@unittest.skipIf(
		    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""					)					) == 0			,							reason="""TF does not support backprop for grouped convolutions on CPU."""			,							)
		def 				__A							(  self					)							->  Union[str, Any]:

					'''simple docstring'''
					super().test_dataset_conversion()





		def 				__A							(  self					)							->  Optional[int]:

					'''simple docstring'''
					def check_hidden_states_output(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					):
								__UpperCAmelCase :   Optional[int]    					=  model_class(__UpperCAmelCase					)

								__UpperCAmelCase :   str    					=  model(**self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase					)					)
								__UpperCAmelCase :   List[str]    					=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

								__UpperCAmelCase :   Optional[int]    					=  self.model_tester.num_stages
								self.assertEqual(len(__UpperCAmelCase					)			,							expected_num_stages + 1					)

								# ConvNext's feature maps are of shape (batch_size, num_channels, height, width)
								self.assertListEqual(
								    list(hidden_states[0].shape[-2:]					)			,							[self.model_tester.image_size // 4, self.model_tester.image_size // 4]			,							)

					__UpperCAmelCase					,	__UpperCAmelCase :   Union[str, Any]    					=  self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
								__UpperCAmelCase :   List[str]    					=  True
								check_hidden_states_output(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)

								# check that output_hidden_states also work using config
								del inputs_dict["output_hidden_states"]
								__UpperCAmelCase :   Optional[Any]    					=  True

								check_hidden_states_output(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)





		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					__UpperCAmelCase					,	__UpperCAmelCase :   Any    					=  self.model_tester.prepare_config_and_inputs_for_common()

					def check_equivalence(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase={}					):
								__UpperCAmelCase :   Optional[Any]    					=  model(__UpperCAmelCase			,							return_dict=__UpperCAmelCase			,							**__UpperCAmelCase					)
								__UpperCAmelCase :   Optional[int]    					=  model(__UpperCAmelCase			,							return_dict=__UpperCAmelCase			,							**__UpperCAmelCase					).to_tuple()

								def recursive_check(__UpperCAmelCase			,							__UpperCAmelCase					):
											if isinstance(__UpperCAmelCase			,							(List, Tuple)					):
														for tuple_iterable_value, dict_iterable_value in zip(__UpperCAmelCase			,							__UpperCAmelCase					):
																	recursive_check(__UpperCAmelCase			,							__UpperCAmelCase					)
											elif tuple_object is None:
														return
											else:
														self.assertTrue(
														    all(tf.equal(__UpperCAmelCase			,							__UpperCAmelCase					)					)			,							msg=(
														        """Tuple and dict output are not equal. Difference:"""
														        f' {tf.math.reduce_max(tf.abs(tuple_object - dict_object					)					)}'
														    )			,							)

											recursive_check(__UpperCAmelCase			,							__UpperCAmelCase					)

					for model_class in self.all_model_classes:
								__UpperCAmelCase :   Dict    					=  model_class(__UpperCAmelCase					)

								__UpperCAmelCase :   Optional[int]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase					)
								__UpperCAmelCase :   Tuple    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase					)
								check_equivalence(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)

								__UpperCAmelCase :   Union[str, Any]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase			,							return_labels=__UpperCAmelCase					)
								__UpperCAmelCase :   Union[str, Any]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase			,							return_labels=__UpperCAmelCase					)
								check_equivalence(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase					)

								__UpperCAmelCase :   Optional[Any]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase					)
								__UpperCAmelCase :   List[str]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase					)
								check_equivalence(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase			,							{"""output_hidden_states""": True}					)

								__UpperCAmelCase :   List[Any]    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase			,							return_labels=__UpperCAmelCase					)
								__UpperCAmelCase :   Dict    					=  self._prepare_for_class(__UpperCAmelCase			,							__UpperCAmelCase			,							return_labels=__UpperCAmelCase					)
								check_equivalence(__UpperCAmelCase			,							__UpperCAmelCase			,							__UpperCAmelCase			,							{"""output_hidden_states""": True}					)





		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					__UpperCAmelCase :   int    					=  self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*__UpperCAmelCase					)





		@slow
		def 				__A							(  self					)							->  Tuple:

					'''simple docstring'''
					__UpperCAmelCase :   Union[str, Any]    					=  TFConvNextModel.from_pretrained("""facebook/convnext-tiny-224"""					)
					self.assertIsNotNone(__UpperCAmelCase					)


def 	lowercase_						( ):

			"""simple docstring"""





			__UpperCAmelCase :   str    					=  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""			)
			return image






@require_tf
@require_vision
class _A	(     unittest.TestCase     ):





		@cached_property
		def 				__A							(  self					)							->  Any:

					'''simple docstring'''
					return ConvNextImageProcessor.from_pretrained("""facebook/convnext-tiny-224"""					) if is_vision_available() else None





		@slow
		def 				__A							(  self					)							->  int:

					'''simple docstring'''
					__UpperCAmelCase :   Tuple    					=  TFConvNextForImageClassification.from_pretrained("""facebook/convnext-tiny-224"""					)

					__UpperCAmelCase :   List[Any]    					=  self.default_image_processor
					__UpperCAmelCase :   int    					=  prepare_img()
					__UpperCAmelCase :   Optional[Any]    					=  image_processor(images=__UpperCAmelCase			,							return_tensors="""tf"""					)

					# forward pass
					__UpperCAmelCase :   List[Any]    					=  model(**__UpperCAmelCase					)

					# verify the logits
					__UpperCAmelCase :   List[str]    					=  tf.TensorShape((1, 1_000)					)
					self.assertEqual(outputs.logits.shape			,							__UpperCAmelCase					)

					__UpperCAmelCase :   str    					=  tf.constant([-0.0260, -0.4739, 0.1911]					)

					tf.debugging.assert_near(outputs.logits[0, :3]			,							__UpperCAmelCase			,							atol=1E-4					)




