
import gc
import math
import unittest

import torch

from diffusers import UNetaDModel
from diffusers.utils import floats_tensor, logging, slow, torch_all_close, torch_device
from diffusers.utils.testing_utils import enable_full_determinism

from .test_modeling_common import ModelTesterMixin, UNetTesterMixin


lowerCamelCase_    :			List[str]    =	logging.get_logger(__name__)

enable_full_determinism()

class 							_UpperCamelCase					(       _A  ,							_A  ,							unittest.TestCase	):






			'''simple docstring'''

			__UpperCamelCase   :    Dict          =			UNetaDModel
			__UpperCamelCase   :    List[Any]          =			"""sample"""

			@property
			def    lowerCAmelCase__						(							self			:							Dict   ):
							UpperCamelCase_:				Dict						=    4
							UpperCamelCase_:				int						=    3
							UpperCamelCase_:				Any						=    (32, 32)

							UpperCamelCase_:				int						=    floats_tensor((batch_size, num_channels) + sizes   ).to(snake_case_   )
							UpperCamelCase_:				Tuple						=    torch.tensor([10]   ).to(snake_case_   )

							return {"sample": noise, "timestep": time_step}

			@property
			def    lowerCAmelCase__						(							self			:							Any   ):
							return (3, 32, 32)

			@property
			def    lowerCAmelCase__						(							self			:							str   ):
							return (3, 32, 32)




			def    lowerCAmelCase__						(							self			:							List[Any]   ):
							UpperCamelCase_:				Optional[Any]						=    {
							    "block_out_channels": (32, 64),
							    "down_block_types": ("DownBlock2D", "AttnDownBlock2D"),
							    "up_block_types": ("AttnUpBlock2D", "UpBlock2D"),
							    "attention_head_dim": 3,
							    "out_channels": 3,
							    "in_channels": 3,
							    "layers_per_block": 2,
							    "sample_size": 32,
							}
							UpperCamelCase_:				Tuple						=    self.dummy_input
							return init_dict, inputs_dict




class 							_UpperCamelCase					(       _A  ,							_A  ,							unittest.TestCase	):






			'''simple docstring'''

			__UpperCamelCase   :    str          =			UNetaDModel
			__UpperCamelCase   :    str          =			"""sample"""

			@property
			def    lowerCAmelCase__						(							self			:							int   ):
							UpperCamelCase_:				Dict						=    4
							UpperCamelCase_:				List[str]						=    4
							UpperCamelCase_:				Tuple						=    (32, 32)

							UpperCamelCase_:				int						=    floats_tensor((batch_size, num_channels) + sizes   ).to(snake_case_   )
							UpperCamelCase_:				List[Any]						=    torch.tensor([10]   ).to(snake_case_   )

							return {"sample": noise, "timestep": time_step}

			@property
			def    lowerCAmelCase__						(							self			:							Any   ):
							return (4, 32, 32)

			@property
			def    lowerCAmelCase__						(							self			:							int   ):
							return (4, 32, 32)

			def    lowerCAmelCase__						(							self			:							Optional[int]   ):
							UpperCamelCase_:				Any						=    {
							    "sample_size": 32,
							    "in_channels": 4,
							    "out_channels": 4,
							    "layers_per_block": 2,
							    "block_out_channels": (32, 64),
							    "attention_head_dim": 32,
							    "down_block_types": ("DownBlock2D", "DownBlock2D"),
							    "up_block_types": ("UpBlock2D", "UpBlock2D"),
							}
							UpperCamelCase_:				Any						=    self.dummy_input
							return init_dict, inputs_dict

			def    lowerCAmelCase__						(							self			:							List[str]   ):
							UpperCamelCase_:				Optional[int]						=    UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""    ,						output_loading_info=snake_case_   )

							self.assertIsNotNone(snake_case_   )
							self.assertEqual(len(loading_info["""missing_keys"""]   )    ,						0   )

							model.to(snake_case_   )
							UpperCamelCase_:				Any						=    model(**self.dummy_input   ).sample

							assert image is not None, "Make sure output is not None"

			@unittest.skipIf(torch_device != """cuda"""    ,						"""This test is supposed to run on GPU"""   )
			def    lowerCAmelCase__						(							self			:							Dict   ):
							UpperCamelCase_:				str						=    UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""    ,						output_loading_info=snake_case_   )
							model.to(snake_case_   )
							UpperCamelCase_:				List[Any]						=    model(**self.dummy_input   ).sample

							assert image is not None, "Make sure output is not None"

			@unittest.skipIf(torch_device != """cuda"""    ,						"""This test is supposed to run on GPU"""   )
			def    lowerCAmelCase__						(							self			:							List[Any]   ):
							# by defautl model loading will use accelerate as `low_cpu_mem_usage=True`
							UpperCamelCase_:				int						=    UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""    ,						output_loading_info=snake_case_   )
							model_accelerate.to(snake_case_   )
							model_accelerate.eval()

							UpperCamelCase_:				Dict						=    torch.randn(
							    1    ,						model_accelerate.config.in_channels    ,						model_accelerate.config.sample_size    ,						model_accelerate.config.sample_size    ,						generator=torch.manual_seed(0   )    ,						)
							UpperCamelCase_:				Union[str, Any]						=    noise.to(snake_case_   )
							UpperCamelCase_:				Optional[int]						=    torch.tensor([10] * noise.shape[0]   ).to(snake_case_   )

							UpperCamelCase_:				Union[str, Any]						=    model_accelerate(snake_case_    ,						snake_case_   )["sample"]

							# two models don't need to stay in the device at the same time
							del model_accelerate
							torch.cuda.empty_cache()
							gc.collect()

							UpperCamelCase_:				str						=    UNetaDModel.from_pretrained(
							    """fusing/unet-ldm-dummy-update"""    ,						output_loading_info=snake_case_    ,						low_cpu_mem_usage=snake_case_   )
							model_normal_load.to(snake_case_   )
							model_normal_load.eval()
							UpperCamelCase_:				List[Any]						=    model_normal_load(snake_case_    ,						snake_case_   )["sample"]

							assert torch_all_close(snake_case_    ,						snake_case_    ,						rtol=1e-3   )




			def    lowerCAmelCase__						(							self			:							int   ):
							UpperCamelCase_:				Optional[Any]						=    UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""   )
							model.eval()
							model.to(snake_case_   )

							UpperCamelCase_:				List[Any]						=    torch.randn(
							    1    ,						model.config.in_channels    ,						model.config.sample_size    ,						model.config.sample_size    ,						generator=torch.manual_seed(0   )    ,						)
							UpperCamelCase_:				int						=    noise.to(snake_case_   )
							UpperCamelCase_:				List[str]						=    torch.tensor([10] * noise.shape[0]   ).to(snake_case_   )

							with torch.no_grad():
											UpperCamelCase_:				List[Any]						=    model(snake_case_    ,						snake_case_   ).sample

							UpperCamelCase_:				Tuple						=    output[0, -1, -3:, -3:].flatten().cpu()
							# fmt: off
							UpperCamelCase_:				Union[str, Any]						=    torch.tensor([-13.3258, -20.1100, -15.9873, -17.6617, -23.0596, -17.9419, -13.3675, -16.1889, -12.3800]   )
							# fmt: on

							self.assertTrue(torch_all_close(snake_case_    ,						snake_case_    ,						rtol=1e-3   )   )




class 							_UpperCamelCase					(       _A  ,							_A  ,							unittest.TestCase	):






			'''simple docstring'''

			__UpperCamelCase   :    Tuple          =			UNetaDModel
			__UpperCamelCase   :    List[str]          =			"""sample"""

			@property
			def    lowerCAmelCase__						(							self			:							List[str]    ,						snake_case_			:							Dict=(32, 32)   ):
							UpperCamelCase_:				Union[str, Any]						=    4
							UpperCamelCase_:				List[Any]						=    3

							UpperCamelCase_:				int						=    floats_tensor((batch_size, num_channels) + sizes   ).to(snake_case_   )
							UpperCamelCase_:				str						=    torch.tensor(batch_size * [10]   ).to(dtype=torch.intaa    ,						device=snake_case_   )

							return {"sample": noise, "timestep": time_step}

			@property
			def    lowerCAmelCase__						(							self			:							Tuple   ):
							return (3, 32, 32)

			@property
			def    lowerCAmelCase__						(							self			:							Union[str, Any]   ):
							return (3, 32, 32)

			def    lowerCAmelCase__						(							self			:							Any   ):
							UpperCamelCase_:				int						=    {
							    "block_out_channels": [32, 64, 64, 64],
							    "in_channels": 3,
							    "layers_per_block": 1,
							    "out_channels": 3,
							    "time_embedding_type": "fourier",
							    "norm_eps": 1e-6,
							    "mid_block_scale_factor": math.sqrt(2.0   ),
							    "norm_num_groups": None,
							    "down_block_types": [
							        "SkipDownBlock2D",
							        "AttnSkipDownBlock2D",
							        "SkipDownBlock2D",
							        "SkipDownBlock2D",
							    ],
							    "up_block_types": [
							        "SkipUpBlock2D",
							        "SkipUpBlock2D",
							        "AttnSkipUpBlock2D",
							        "SkipUpBlock2D",
							    ],
							}
							UpperCamelCase_:				int						=    self.dummy_input
							return init_dict, inputs_dict

			@slow
			def    lowerCAmelCase__						(							self			:							Optional[Any]   ):
							UpperCamelCase_:				Optional[Any]						=    UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""    ,						output_loading_info=snake_case_   )
							self.assertIsNotNone(snake_case_   )
							self.assertEqual(len(loading_info["""missing_keys"""]   )    ,						0   )

							model.to(snake_case_   )
							UpperCamelCase_:				Any						=    self.dummy_input
							UpperCamelCase_:				str						=    floats_tensor((4, 3) + (256, 256)   ).to(snake_case_   )
							UpperCamelCase_:				List[str]						=    noise
							UpperCamelCase_:				int						=    model(**snake_case_   )

							assert image is not None, "Make sure output is not None"

			@slow
			def    lowerCAmelCase__						(							self			:							Optional[int]   ):
							UpperCamelCase_:				Any						=    UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""   )
							model.to(snake_case_   )

							UpperCamelCase_:				Optional[int]						=    4
							UpperCamelCase_:				List[str]						=    3
							UpperCamelCase_:				str						=    (256, 256)

							UpperCamelCase_:				str						=    torch.ones((batch_size, num_channels) + sizes   ).to(snake_case_   )
							UpperCamelCase_:				Tuple						=    torch.tensor(batch_size * [1e-4]   ).to(snake_case_   )

							with torch.no_grad():
											UpperCamelCase_:				List[str]						=    model(snake_case_    ,						snake_case_   ).sample

							UpperCamelCase_:				List[Any]						=    output[0, -3:, -3:, -1].flatten().cpu()
							# fmt: off
							UpperCamelCase_:				Union[str, Any]						=    torch.tensor([-4842.8691, -6499.6631, -3800.1953, -7978.2686, -1_0980.7129, -2_0028.8535, 8148.2822, 2342.2905, 567.7608]   )
							# fmt: on

							self.assertTrue(torch_all_close(snake_case_    ,						snake_case_    ,						rtol=1e-2   )   )

			def    lowerCAmelCase__						(							self			:							Tuple   ):
							UpperCamelCase_:				Tuple						=    UNetaDModel.from_pretrained("""fusing/ncsnpp-ffhq-ve-dummy-update"""   )
							model.to(snake_case_   )

							UpperCamelCase_:				Optional[int]						=    4
							UpperCamelCase_:				List[Any]						=    3
							UpperCamelCase_:				List[Any]						=    (32, 32)

							UpperCamelCase_:				Tuple						=    torch.ones((batch_size, num_channels) + sizes   ).to(snake_case_   )
							UpperCamelCase_:				Union[str, Any]						=    torch.tensor(batch_size * [1e-4]   ).to(snake_case_   )

							with torch.no_grad():
											UpperCamelCase_:				int						=    model(snake_case_    ,						snake_case_   ).sample

							UpperCamelCase_:				int						=    output[0, -3:, -3:, -1].flatten().cpu()
							# fmt: off
							UpperCamelCase_:				str						=    torch.tensor([-0.0325, -0.0900, -0.0869, -0.0332, -0.0725, -0.0270, -0.0101, 0.0227, 0.0256]   )
							# fmt: on

							self.assertTrue(torch_all_close(snake_case_    ,						snake_case_    ,						rtol=1e-2   )   )




			def    lowerCAmelCase__						(							self			:							Optional[Any]   ):
							# not required for this model
							pass










import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
		import torch

		from transformers import AutoModelForImageClassification

if is_vision_available():
		from transformers import AutoImageProcessor



@require_torch
@require_vision
class 						_UpperCAmelCase      (     unittest.TestCase							):






		@slow
		def _snake_case		(						self				:     List[str]):
				SCREAMING_SNAKE_CASE_				:Tuple				     =						AutoImageProcessor.from_pretrained("microsoft/dit-base-finetuned-rvlcdip")
				SCREAMING_SNAKE_CASE_				:int				     =						AutoModelForImageClassification.from_pretrained("microsoft/dit-base-finetuned-rvlcdip")
				model.to(UpperCAmelCase)

				from datasets import load_dataset

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						load_dataset("nielsr/rvlcdip-demo")

				SCREAMING_SNAKE_CASE_				:Dict				     =						dataset["train"][0]["image"].convert("RGB")

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						image_processor(UpperCAmelCase   ,  return_tensors="pt").to(UpperCAmelCase)

				# forward pass
				with torch.no_grad():
						SCREAMING_SNAKE_CASE_				:Optional[int]				     =						model(**UpperCAmelCase)
						SCREAMING_SNAKE_CASE_				:List[str]				     =						outputs.logits

				SCREAMING_SNAKE_CASE_				:int				     =						torch.Size((1, 16))
				self.assertEqual(logits.shape   ,  UpperCAmelCase)

				SCREAMING_SNAKE_CASE_				:Optional[Any]				     =						torch.tensor(
				    [-0.4158, -0.4092, -0.4347]   ,  device=UpperCAmelCase   ,  dtype=torch.float   ,  )
				self.assertTrue(torch.allclose(logits[0, :3]   ,  UpperCAmelCase   ,  atol=1E-4))






