
'''simple docstring'''
import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    PNDMScheduler,
    StableDiffusionLDMaDPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import nightly, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS


enable_full_determinism()


class      _lowerCAmelCase       (       unittest.TestCase ):







							'''simple docstring'''

							lowerCAmelCase_   		=		StableDiffusionLDMaDPipeline
							lowerCAmelCase_   		=		TEXT_TO_IMAGE_PARAMS
							lowerCAmelCase_   		=		TEXT_TO_IMAGE_BATCH_PARAMS
							lowerCAmelCase_   		=		TEXT_TO_IMAGE_IMAGE_PARAMS

							def 			lowercase				(self    )	->  Any:
														torch.manual_seed(0    )
														_snake_case  =    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    ,    )
														_snake_case  =    DDIMScheduler(
														    beta_start=0.0_0085    ,    beta_end=0.012    ,    beta_schedule="""scaled_linear"""    ,    clip_sample=lowercase_    ,    set_alpha_to_one=lowercase_    ,    )
														torch.manual_seed(0    )
														_snake_case  =    AutoencoderKL(
														    block_out_channels=[32, 64]    ,    in_channels=6    ,    out_channels=6    ,    down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]    ,    up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]    ,    latent_channels=4    ,    )
														torch.manual_seed(0    )
														_snake_case  =    CLIPTextConfig(
														    bos_token_id=0    ,    eos_token_id=2    ,    hidden_size=32    ,    intermediate_size=37    ,    layer_norm_eps=1e-0_5    ,    num_attention_heads=4    ,    num_hidden_layers=5    ,    pad_token_id=1    ,    vocab_size=1000    ,    )
														_snake_case  =    CLIPTextModel(lowercase_    )
														_snake_case  =    CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

														_snake_case  =    {
														    """unet""": unet,
														    """scheduler""": scheduler,
														    """vae""": vae,
														    """text_encoder""": text_encoder,
														    """tokenizer""": tokenizer,
														    """safety_checker""": None,
														    """feature_extractor""": None,
														}
														return components

							def 			lowercase				(self    ,    UpperCAmelCase    ,    UpperCAmelCase=0    )	->  List[str]:
														if str(lowercase_    ).startswith("""mps"""    ):
																					_snake_case  =    torch.manual_seed(lowercase_    )
														else:
																					_snake_case  =    torch.Generator(device=lowercase_    ).manual_seed(lowercase_    )
														_snake_case  =    {
														    """prompt""": """A painting of a squirrel eating a burger""",
														    """generator""": generator,
														    """num_inference_steps""": 2,
														    """guidance_scale""": 6.0,
														    """output_type""": """numpy""",
														}
														return inputs

							def 			lowercase				(self    )	->  Any:
														_snake_case  =    """cpu"""  # ensure determinism for the device-dependent torch.Generator

														_snake_case  =    self.get_dummy_components()
														_snake_case  =    StableDiffusionLDMaDPipeline(**lowercase_    )
														_snake_case  =    ldmad_pipe.to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_dummy_inputs(lowercase_    )
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth

														_snake_case  =    rgb[0, -3:, -3:, -1]
														_snake_case  =    depth[0, -3:, -1]

														assert rgb.shape == (1, 64, 64, 3)
														assert depth.shape == (1, 64, 64)

														_snake_case  =    np.array(
														    [0.3733_8176, 0.7_0247, 0.7420_3193, 0.5164_3604, 0.5825_6793, 0.6093_2136, 0.418_1095, 0.4835_5877, 0.4653_5262]    )
														_snake_case  =    np.array([103.46727, 85.81_2004, 87.84_9236]    )

														assert np.abs(image_slice_rgb.flatten() - expected_slice_rgb    ).max() < 1e-2
														assert np.abs(image_slice_depth.flatten() - expected_slice_depth    ).max() < 1e-2

							def 			lowercase				(self    )	->  Optional[int]:
														_snake_case  =    self.get_dummy_components()
														_snake_case  =    StableDiffusionLDMaDPipeline(**lowercase_    )
														_snake_case  =    ldmad_pipe.to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_dummy_inputs(lowercase_    )
														_snake_case  =    3 * [inputs["""prompt"""]]

														# forward
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth
														_snake_case  =    rgb_slice_a[0, -3:, -3:, -1]
														_snake_case  =    depth_slice_a[0, -3:, -1]

														_snake_case  =    self.get_dummy_inputs(lowercase_    )
														_snake_case  =    3 * [inputs.pop("""prompt"""    )]

														_snake_case  =    ldmad_pipe.tokenizer(
														    lowercase_    ,    padding="""max_length"""    ,    max_length=ldmad_pipe.tokenizer.model_max_length    ,    truncation=lowercase_    ,    return_tensors="""pt"""    ,    )
														_snake_case  =    text_inputs["""input_ids"""].to(lowercase_    )

														_snake_case  =    ldmad_pipe.text_encoder(lowercase_    )[0]

														_snake_case  =    prompt_embeds

														# forward
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth
														_snake_case  =    rgb_slice_a[0, -3:, -3:, -1]
														_snake_case  =    depth_slice_a[0, -3:, -1]

														assert np.abs(rgb_slice_a.flatten() - rgb_slice_a.flatten()    ).max() < 1e-4
														assert np.abs(depth_slice_a.flatten() - depth_slice_a.flatten()    ).max() < 1e-4





							def 			lowercase				(self    )	->  Dict:
														_snake_case  =    """cpu"""  # ensure determinism for the device-dependent torch.Generator
														_snake_case  =    self.get_dummy_components()
														_snake_case  =    PNDMScheduler(skip_prk_steps=lowercase_    )
														_snake_case  =    StableDiffusionLDMaDPipeline(**lowercase_    )
														_snake_case  =    ldmad_pipe.to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_dummy_inputs(lowercase_    )
														_snake_case  =    """french fries"""
														_snake_case  =    ldmad_pipe(**lowercase_    ,    negative_prompt=lowercase_    )

														_snake_case,				_snake_case  =    output.rgb, output.depth
														_snake_case  =    rgb[0, -3:, -3:, -1]
														_snake_case  =    depth[0, -3:, -1]

														assert rgb.shape == (1, 64, 64, 3)
														assert depth.shape == (1, 64, 64)

														_snake_case  =    np.array(
														    [0.3_7044, 0.7181_1503, 0.722_3251, 0.4860_3675, 0.563_8391, 0.636_4948, 0.4283_3704, 0.490_1315, 0.4792_6217]    )
														_snake_case  =    np.array([107.84738, 84.6_2802, 89.96_2135]    )
														assert np.abs(rgb_slice.flatten() - expected_slice_rgb    ).max() < 1e-2
														assert np.abs(depth_slice.flatten() - expected_slice_depth    ).max() < 1e-2


@slow
@require_torch_gpu
class      _lowerCAmelCase       (       unittest.TestCase ):







							'''simple docstring'''

							def 			lowercase				(self    )	->  Tuple:
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()

							def 			lowercase				(self    ,    UpperCAmelCase    ,    UpperCAmelCase="cpu"    ,    UpperCAmelCase=torch.floataa    ,    UpperCAmelCase=0    )	->  Optional[Any]:
														_snake_case  =    torch.Generator(device=lowercase_    ).manual_seed(lowercase_    )
														_snake_case  =    np.random.RandomState(lowercase_    ).standard_normal((1, 4, 64, 64)    )
														_snake_case  =    torch.from_numpy(lowercase_    ).to(device=lowercase_    ,    dtype=lowercase_    )
														_snake_case  =    {
														    """prompt""": """a photograph of an astronaut riding a horse""",
														    """latents""": latents,
														    """generator""": generator,
														    """num_inference_steps""": 3,
														    """guidance_scale""": 7.5,
														    """output_type""": """numpy""",
														}
														return inputs





							def 			lowercase				(self    )	->  List[Any]:
														_snake_case  =    StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""    )
														_snake_case  =    ldmad_pipe.to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_inputs(lowercase_    )
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth
														_snake_case  =    rgb[0, -3:, -3:, -1].flatten()
														_snake_case  =    rgb[0, -3:, -1].flatten()

														assert rgb.shape == (1, 512, 512, 3)
														assert depth.shape == (1, 512, 512)

														_snake_case  =    np.array(
														    [0.5380_5465, 0.5670_7305, 0.548_6515, 0.5701_2236, 0.581_4511, 0.5625_3487, 0.5484_3014, 0.5509_2263, 0.645_9706]    )
														_snake_case  =    np.array(
														    [0.926_3781, 0.667_8672, 0.548_6515, 0.9220_2145, 0.6783_1135, 0.5625_3487, 0.924_1694, 0.755_1478, 0.645_9706]    )
														assert np.abs(rgb_slice - expected_slice_rgb    ).max() < 3e-3
														assert np.abs(depth_slice - expected_slice_depth    ).max() < 3e-3


@nightly
@require_torch_gpu
class      _lowerCAmelCase       (       unittest.TestCase ):







							'''simple docstring'''

							def 			lowercase				(self    )	->  Any:
														super().tearDown()
														gc.collect()
														torch.cuda.empty_cache()

							def 			lowercase				(self    ,    UpperCAmelCase    ,    UpperCAmelCase="cpu"    ,    UpperCAmelCase=torch.floataa    ,    UpperCAmelCase=0    )	->  List[str]:
														_snake_case  =    torch.Generator(device=lowercase_    ).manual_seed(lowercase_    )
														_snake_case  =    np.random.RandomState(lowercase_    ).standard_normal((1, 4, 64, 64)    )
														_snake_case  =    torch.from_numpy(lowercase_    ).to(device=lowercase_    ,    dtype=lowercase_    )
														_snake_case  =    {
														    """prompt""": """a photograph of an astronaut riding a horse""",
														    """latents""": latents,
														    """generator""": generator,
														    """num_inference_steps""": 50,
														    """guidance_scale""": 7.5,
														    """output_type""": """numpy""",
														}
														return inputs

							def 			lowercase				(self    )	->  List[Any]:
														_snake_case  =    StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d"""    ).to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_inputs(lowercase_    )
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth

														_snake_case  =    0.49_5586
														_snake_case  =    0.3379_5515
														_snake_case  =    112.48518
														_snake_case  =    98.48_9746
														assert np.abs(expected_rgb_mean - rgb.mean()    ) < 1e-3
														assert np.abs(expected_rgb_std - rgb.std()    ) < 1e-3
														assert np.abs(expected_depth_mean - depth.mean()    ) < 1e-3
														assert np.abs(expected_depth_std - depth.std()    ) < 1e-3





							def 			lowercase				(self    )	->  Optional[int]:
														_snake_case  =    StableDiffusionLDMaDPipeline.from_pretrained("""Intel/ldm3d-4c"""    ).to(lowercase_    )
														ldmad_pipe.set_progress_bar_config(disable=lowercase_    )

														_snake_case  =    self.get_inputs(lowercase_    )
														_snake_case  =    ldmad_pipe(**lowercase_    )
														_snake_case,				_snake_case  =    output.rgb, output.depth

														_snake_case  =    0.419_4127
														_snake_case  =    0.3537_5586
														_snake_case  =    0.563_8502
														_snake_case  =    0.3468_6103

														assert rgb.shape == (1, 512, 512, 3)
														assert depth.shape == (1, 512, 512, 1)
														assert np.abs(expected_rgb_mean - rgb.mean()    ) < 1e-3
														assert np.abs(expected_rgb_std - rgb.std()    ) < 1e-3
														assert np.abs(expected_depth_mean - depth.mean()    ) < 1e-3
														assert np.abs(expected_depth_std - depth.std()    ) < 1e-3



import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
	import torch

	from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin




class       lowercase_					:





				def __init__(				self       ,  lowercase_       ,  lowercase_=None       ,  lowercase_=None       ,  lowercase_=None       ,  lowercase_="resnet50"       ,  lowercase_=3       ,  lowercase_=32       ,  lowercase_=3       ,  lowercase_=True       ,  lowercase_=True       ,  )  -> Union[str, Any]:
											a__      =parent
											a__      =out_indices if out_indices is not None else [4]
											a__      =stage_names
											a__      =out_features
											a__      =backbone
											a__      =batch_size
											a__      =image_size
											a__      =num_channels
											a__      =use_pretrained_backbone
											a__      =is_training





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
											a__      =self.get_config()

											return config, pixel_values





				def 						__UpperCamelCase    (				self)  -> Tuple:
											return TimmBackboneConfig(
											    image_size=self.image_size       ,  num_channels=self.num_channels       ,  out_features=self.out_features       ,  out_indices=self.out_indices       ,  stage_names=self.stage_names       ,  use_pretrained_backbone=self.use_pretrained_backbone       ,  backbone=self.backbone       ,  )





				def 						__UpperCamelCase    (				self       ,  lowercase_       ,  lowercase_)  -> str:
											a__      =TimmBackbone(config=lowercase_)
											model.to(lowercase_)
											model.eval()
											with torch.no_grad():
																		a__      =model(lowercase_)
											self.parent.assertEqual(
											    result.feature_map[-1].shape       ,  (self.batch_size, model.channels[-1], 14, 14)       ,  )





				def 						__UpperCamelCase    (				self)  -> str:
											a__      =self.prepare_config_and_inputs()
											a__    ,     a__      =config_and_inputs
											a__      ={'pixel_values': pixel_values}
											return config, inputs_dict







@require_torch
@require_timm
class       lowercase_					(lowercase__      ,					lowercase__      ,					lowercase__      ,					unittest.TestCase   ):
				snake_case	    =(TimmBackbone,) if is_torch_available() else ()
				snake_case	    ={'feature-extraction': TimmBackbone} if is_torch_available() else {}
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False
				snake_case	    =False





				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											a__      =TimmBackboneModelTester(self)
											a__      =ConfigTester(self       ,  config_class=lowercase_       ,  has_text_modality=lowercase_)





				def 						__UpperCamelCase    (				self)  -> Dict:
											self.config_tester.create_and_test_config_to_json_string()
											self.config_tester.create_and_test_config_to_json_file()
											self.config_tester.create_and_test_config_from_and_save_pretrained()
											self.config_tester.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()





				def 						__UpperCamelCase    (				self)  -> str:
											a__      ='resnet18'
											a__      ='microsoft/resnet-18'

											a__      =AutoBackbone.from_pretrained(lowercase_       ,  use_timm_backbone=lowercase_)
											a__      =AutoBackbone.from_pretrained(lowercase_)

											self.assertEqual(len(timm_model.out_features)       ,  len(transformers_model.out_features))
											self.assertEqual(len(timm_model.stage_names)       ,  len(transformers_model.stage_names))
											self.assertEqual(timm_model.channels       ,  transformers_model.channels)
											# Out indices are set to the last layer by default. For timm models, we don't know
											# the number of layers in advance, so we set it to (-1,), whereas for transformers
											# models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
											self.assertEqual(timm_model.out_indices       ,  (-1,))
											self.assertEqual(transformers_model.out_indices       ,  [len(timm_model.stage_names) - 1])

											a__      =AutoBackbone.from_pretrained(lowercase_       ,  use_timm_backbone=lowercase_       ,  out_indices=[1, 2, 3])
											a__      =AutoBackbone.from_pretrained(lowercase_       ,  out_indices=[1, 2, 3])

											self.assertEqual(timm_model.out_indices       ,  transformers_model.out_indices)
											self.assertEqual(len(timm_model.out_features)       ,  len(transformers_model.out_features))
											self.assertEqual(timm_model.channels       ,  transformers_model.channels)





				@unittest.skip('TimmBackbone doesn\'t support feed forward chunking')
				def 						__UpperCamelCase    (				self)  -> int:
											pass





				@unittest.skip('TimmBackbone doesn\'t have num_hidden_layers attribute')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('TimmBackbone initialization is managed on the timm side')
				def 						__UpperCamelCase    (				self)  -> Any:
											pass





				@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> Any:
											pass





				@unittest.skip('TimmBackbone models doesn\'t have inputs_embeds')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('TimmBackbone model cannot be created without specifying a backbone checkpoint')
				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											pass





				@unittest.skip('model weights aren\'t tied in TimmBackbone.')
				def 						__UpperCamelCase    (				self)  -> Dict:
											pass





				@unittest.skip('model weights aren\'t tied in TimmBackbone.')
				def 						__UpperCamelCase    (				self)  -> List[Any]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> List[str]:
											pass





				@unittest.skip('Only checkpoints on timm can be loaded into TimmBackbone')
				def 						__UpperCamelCase    (				self)  -> Union[str, Any]:
											pass





				@unittest.skip('TimmBackbone doesn\'t have hidden size info in its configuration.')
				def 						__UpperCamelCase    (				self)  -> int:
											pass





				@unittest.skip('TimmBackbone doesn\'t support output_attentions.')
				def 						__UpperCamelCase    (				self)  -> str:
											pass





				@unittest.skip('Safetensors is not supported by timm.')
				def 						__UpperCamelCase    (				self)  -> Optional[int]:
											pass





				@unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
				def 						__UpperCamelCase    (				self)  -> Optional[Any]:
											pass





				def 						__UpperCamelCase    (				self)  -> Any:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		a__      =model_class(lowercase_)
																		a__      =inspect.signature(model.forward)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		a__      =[*signature.parameters.keys()]

																		a__      =['pixel_values']
																		self.assertListEqual(arg_names[:1]       ,  lowercase_)





				def 						__UpperCamelCase    (				self)  -> Any:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()
											a__      =True
											a__      =self.has_attentions

											# no need to test all models as different heads yield the same functionality
											a__      =self.all_model_classes[0]
											a__      =model_class(lowercase_)
											model.to(lowercase_)

											a__      =self._prepare_for_class(lowercase_       ,  lowercase_)
											a__      =model(**lowercase_)
											a__      =outputs[0][-1]

											# Encoder-/Decoder-only models
											a__      =outputs.hidden_states[0]
											hidden_states.retain_grad()

											if self.has_attentions:
																		a__      =outputs.attentions[0]
																		attentions.retain_grad()

											output.flatten()[0].backward(retain_graph=lowercase_)

											self.assertIsNotNone(hidden_states.grad)

											if self.has_attentions:
																		self.assertIsNotNone(attentions.grad)





				def 						__UpperCamelCase    (				self)  -> List[str]:
											a__    ,     a__      =self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

																		self.assertEqual(len(result.feature_maps)       ,  len(config.out_indices))
																		self.assertEqual(len(model.channels)       ,  len(config.out_indices))

																		# Check output of last stage is taken if out_features=None, out_indices=None
																		a__      =copy.deepcopy(lowercase_)
																		a__      =None
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

																		self.assertEqual(len(result.feature_maps)       ,  1)
																		self.assertEqual(len(model.channels)       ,  1)

																		# Check backbone can be initialized with fresh weights
																		a__      =copy.deepcopy(lowercase_)
																		a__      =False
																		a__      =model_class(lowercase_)
																		model.to(lowercase_)
																		model.eval()
																		a__      =model(**lowercase_)

