



'''simple docstring'''




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


UpperCamelCase__       :	Tuple    = logging.get_logger(__name__)

enable_full_determinism()






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


			'''simple docstring'''

			_A      :		List[Any]			    =		UNetaDModel
			_A      :		List[str]			    =		'''sample'''

			@property
			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Any           =		4
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		3
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		(3_2, 3_2)

					__SCREAMING_SNAKE_CASE       :		int           =		floats_tensor((batch_size, num_channels) + sizes						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		int           =		torch.tensor([1_0]						).to(lowerCAmelCase__						)

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

			@property
			def UpperCamelCase__					(							self     : List[str]						):





					"""simple docstring"""






					return (3, 3_2, 3_2)

			@property
			def UpperCamelCase__					(							self     : List[str]						):





					"""simple docstring"""






					return (3, 3_2, 3_2)



			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		{
					    """block_out_channels""": (3_2, 6_4),
					    """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""": 3_2,
					}
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		self.dummy_input
					return init_dict, inputs_dict






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


			'''simple docstring'''

			_A      :		str			    =		UNetaDModel
			_A      :		Union[str, Any]			    =		'''sample'''

			@property
			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		4
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		4
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		(3_2, 3_2)

					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		floats_tensor((batch_size, num_channels) + sizes						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		torch.tensor([1_0]						).to(lowerCAmelCase__						)

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

			@property
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					return (4, 3_2, 3_2)

			@property
			def UpperCamelCase__					(							self     : Tuple						):





					"""simple docstring"""






					return (4, 3_2, 3_2)

			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		str           =		{
					    """sample_size""": 3_2,
					    """in_channels""": 4,
					    """out_channels""": 4,
					    """layers_per_block""": 2,
					    """block_out_channels""": (3_2, 6_4),
					    """attention_head_dim""": 3_2,
					    """down_block_types""": ("""DownBlock2D""", """DownBlock2D"""),
					    """up_block_types""": ("""UpBlock2D""", """UpBlock2D"""),
					}
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		self.dummy_input
					return init_dict, inputs_dict

			def UpperCamelCase__					(							self     : int						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		List[Any]           =		UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""							,     output_loading_info=lowerCAmelCase__						)

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

					model.to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Dict           =		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 UpperCamelCase__					(							self     : Union[str, Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		str           =		UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""							,     output_loading_info=lowerCAmelCase__						)
					model.to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		int           =		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 UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		int           =		UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""							,     output_loading_info=lowerCAmelCase__						)
					model_accelerate.to(lowerCAmelCase__						)
					model_accelerate.eval()

					__SCREAMING_SNAKE_CASE       :		Tuple           =		torch.randn(
					    1							,     model_accelerate.config.in_channels							,     model_accelerate.config.sample_size							,     model_accelerate.config.sample_size							,     generator=torch.manual_seed(0						)							,     )
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		noise.to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Any           =		torch.tensor([1_0] * noise.shape[0]						).to(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		Tuple           =		model_accelerate(lowerCAmelCase__							,     lowerCAmelCase__						)["""sample"""]

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

					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		str           =		UNetaDModel.from_pretrained(
					    """fusing/unet-ldm-dummy-update"""							,     output_loading_info=lowerCAmelCase__							,     low_cpu_mem_usage=lowerCAmelCase__						)
					model_normal_load.to(lowerCAmelCase__						)
					model_normal_load.eval()
					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		model_normal_load(lowerCAmelCase__							,     lowerCAmelCase__						)["""sample"""]

					assert torch_all_close(lowerCAmelCase__							,     lowerCAmelCase__							,     rtol=1E-3						)



			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		UNetaDModel.from_pretrained("""fusing/unet-ldm-dummy-update"""						)
					model.eval()
					model.to(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.randn(
					    1							,     model.config.in_channels							,     model.config.sample_size							,     model.config.sample_size							,     generator=torch.manual_seed(0						)							,     )
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		noise.to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.tensor([1_0] * noise.shape[0]						).to(lowerCAmelCase__						)

					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		model(lowerCAmelCase__							,     lowerCAmelCase__						).sample

					__SCREAMING_SNAKE_CASE       :		List[Any]           =		output[0, -1, -3:, -3:].flatten().cpu()
					# fmt: off
					__SCREAMING_SNAKE_CASE       :		List[str]           =		torch.tensor([-13.32_58, -20.11_00, -15.98_73, -17.66_17, -23.05_96, -17.94_19, -13.36_75, -16.18_89, -12.38_00]						)
					# fmt: on

					self.assertTrue(torch_all_close(lowerCAmelCase__							,     lowerCAmelCase__							,     rtol=1E-3						)						)






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


			'''simple docstring'''

			_A      :		int			    =		UNetaDModel
			_A      :		Tuple			    =		'''sample'''

			@property
			def UpperCamelCase__					(							self     : Union[str, Any]							,     lowerCAmelCase__     : Any=(3_2, 3_2)						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		4
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		3

					__SCREAMING_SNAKE_CASE       :		Any           =		floats_tensor((batch_size, num_channels) + sizes						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		torch.tensor(batch_size * [1_0]						).to(dtype=torch.intaa							,     device=lowerCAmelCase__						)

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

			@property
			def UpperCamelCase__					(							self     : Tuple						):





					"""simple docstring"""






					return (3, 3_2, 3_2)

			@property
			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					return (3, 3_2, 3_2)

			def UpperCamelCase__					(							self     : Any						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		{
					    """block_out_channels""": [3_2, 6_4, 6_4, 6_4],
					    """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""",
					    ],
					}
					__SCREAMING_SNAKE_CASE       :		Tuple           =		self.dummy_input
					return init_dict, inputs_dict

			@slow
			def UpperCamelCase__					(							self     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       ,						__SCREAMING_SNAKE_CASE       :		Tuple           =		UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""							,     output_loading_info=lowerCAmelCase__						)
					self.assertIsNotNone(lowerCAmelCase__						)
					self.assertEqual(len(loading_info["""missing_keys"""]						)							,     0						)

					model.to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		self.dummy_input
					__SCREAMING_SNAKE_CASE       :		List[str]           =		floats_tensor((4, 3) + (2_5_6, 2_5_6)						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		noise
					__SCREAMING_SNAKE_CASE       :		List[str]           =		model(**lowerCAmelCase__						)

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

			@slow
			def UpperCamelCase__					(							self     : str						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		UNetaDModel.from_pretrained("""google/ncsnpp-celebahq-256"""						)
					model.to(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		Any           =		4
					__SCREAMING_SNAKE_CASE       :		str           =		3
					__SCREAMING_SNAKE_CASE       :		List[str]           =		(2_5_6, 2_5_6)

					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		torch.ones((batch_size, num_channels) + sizes						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		int           =		torch.tensor(batch_size * [1E-4]						).to(lowerCAmelCase__						)

					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		Optional[int]           =		model(lowerCAmelCase__							,     lowerCAmelCase__						).sample

					__SCREAMING_SNAKE_CASE       :		int           =		output[0, -3:, -3:, -1].flatten().cpu()
					# fmt: off
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		torch.tensor([-48_42.86_91, -64_99.66_31, -38_00.19_53, -79_78.26_86, -1_09_80.71_29, -2_00_28.85_35, 81_48.28_22, 23_42.29_05, 5_67.76_08]						)
					# fmt: on

					self.assertTrue(torch_all_close(lowerCAmelCase__							,     lowerCAmelCase__							,     rtol=1E-2						)						)

			def UpperCamelCase__					(							self     : List[Any]						):





					"""simple docstring"""






					__SCREAMING_SNAKE_CASE       :		Optional[Any]           =		UNetaDModel.from_pretrained("""fusing/ncsnpp-ffhq-ve-dummy-update"""						)
					model.to(lowerCAmelCase__						)

					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		4
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		3
					__SCREAMING_SNAKE_CASE       :		List[Any]           =		(3_2, 3_2)

					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		torch.ones((batch_size, num_channels) + sizes						).to(lowerCAmelCase__						)
					__SCREAMING_SNAKE_CASE       :		Union[str, Any]           =		torch.tensor(batch_size * [1E-4]						).to(lowerCAmelCase__						)

					with torch.no_grad():
							__SCREAMING_SNAKE_CASE       :		Any           =		model(lowerCAmelCase__							,     lowerCAmelCase__						).sample

					__SCREAMING_SNAKE_CASE       :		Optional[int]           =		output[0, -3:, -3:, -1].flatten().cpu()
					# fmt: off
					__SCREAMING_SNAKE_CASE       :		Tuple           =		torch.tensor([-0.03_25, -0.09_00, -0.08_69, -0.03_32, -0.07_25, -0.02_70, -0.01_01, 0.02_27, 0.02_56]						)
					# fmt: on

					self.assertTrue(torch_all_close(lowerCAmelCase__							,     lowerCAmelCase__							,     rtol=1E-2						)						)



			def UpperCamelCase__					(							self     : Optional[int]						):





					"""simple docstring"""






					pass




'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCamelCase__       :	Any    = {
    '''configuration_instructblip''': [
        '''INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''InstructBlipConfig''',
        '''InstructBlipQFormerConfig''',
        '''InstructBlipVisionConfig''',
    ],
    '''processing_instructblip''': ['''InstructBlipProcessor'''],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						UpperCamelCase__       :	Optional[Any]    = [
						    '''INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
						    '''InstructBlipQFormerModel''',
						    '''InstructBlipPreTrainedModel''',
						    '''InstructBlipForConditionalGeneration''',
						    '''InstructBlipVisionModel''',
						]

if TYPE_CHECKING:
						from .configuration_instructblip import (
						    INSTRUCTBLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
						    InstructBlipConfig,
						    InstructBlipQFormerConfig,
						    InstructBlipVisionConfig,
						)
						from .processing_instructblip import InstructBlipProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_instructblip import (
												    INSTRUCTBLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
												    InstructBlipForConditionalGeneration,
												    InstructBlipPreTrainedModel,
												    InstructBlipQFormerModel,
												    InstructBlipVisionModel,
												)

else:
						import sys

						UpperCamelCase__       :	str    = _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)