

import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch







@require_torch
class 			lowercase    :




    @property
    def    a							( self	):
           return self.get_dummy_input()




    @property
    def    a							( self	):
           if self.block_type == "down":
                  return (4, 32, 16, 16)
           elif self.block_type == "mid":
                  return (4, 32, 32, 32)
           elif self.block_type == "up":
                  return (4, 32, 64, 64)

           raise ValueError(F'''\'{self.block_type}\' is not a supported block_type. Set it to \'up\', \'mid\', or \'down\'.'''	)




    def    a							( self				,       snake_case=True				,       snake_case=False				,       snake_case=False				,       snake_case=False				,       ):
           snake_case_          =   4
           snake_case_          =   32
           snake_case_          =   (32, 32)

           snake_case_          =   torch.manual_seed(0	)
           snake_case_          =   torch.device(snake_case	)
           snake_case_          =   (batch_size, num_channels) + sizes
           snake_case_          =   randn_tensor(snake_case				,       generator=snake_case				,       device=snake_case	)
           snake_case_          =   {'hidden_states': hidden_states}

           if include_temb:
                  snake_case_          =   128
                  snake_case_          =   randn_tensor((batch_size, temb_channels)				,       generator=snake_case				,       device=snake_case	)

           if include_res_hidden_states_tuple:
                  snake_case_          =   torch.manual_seed(1	)
                  snake_case_          =   (randn_tensor(snake_case				,       generator=snake_case				,       device=snake_case	),)

           if include_encoder_hidden_states:
                  snake_case_          =   floats_tensor((batch_size, 32, 32)	).to(snake_case	)

           if include_skip_sample:
                  snake_case_          =   randn_tensor(((batch_size, 3) + sizes)				,       generator=snake_case				,       device=snake_case	)

           return dummy_input




    def    a							( self	):
           snake_case_          =   {
               'in_channels': 32,
               'out_channels': 32,
               'temb_channels': 128,
           }
           if self.block_type == "up":
                  snake_case_          =   32

           if self.block_type == "mid":
                  init_dict.pop('out_channels'	)

           snake_case_          =   self.dummy_input
           return init_dict, inputs_dict




    def    a							( self				,       snake_case	):
           snake_case_    ,   snake_case_          =   self.prepare_init_args_and_inputs_for_common()
           snake_case_          =   self.block_class(**snake_case	)
           unet_block.to(snake_case	)
           unet_block.eval()

           with torch.no_grad():
                  snake_case_          =   unet_block(**snake_case	)

           if isinstance(snake_case				,       snake_case	):
                  snake_case_          =   output[0]

           self.assertEqual(output.shape				,       self.output_shape	)

           snake_case_          =   output[0, -1, -3:, -3:]
           snake_case_          =   torch.tensor(snake_case	).to(snake_case	)
           assert torch_all_close(output_slice.flatten()				,       snake_case				,       atol=5e-3	)




    @unittest.skipIf(torch_device == 'mps'				,       'Training is not supported in mps'	)
    def    a							( self	):
           snake_case_    ,   snake_case_          =   self.prepare_init_args_and_inputs_for_common()
           snake_case_          =   self.block_class(**snake_case	)
           model.to(snake_case	)
           model.train()
           snake_case_          =   model(**snake_case	)

           if isinstance(snake_case				,       snake_case	):
                  snake_case_          =   output[0]

           snake_case_          =   torch.device(snake_case	)
           snake_case_          =   randn_tensor(output.shape				,       device=snake_case	)
           snake_case_          =   torch.nn.functional.mse_loss(snake_case				,       snake_case	)
           loss.backward()







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_tf_available,
    is_torch_available,
    is_vision_available,
)


_UpperCAmelCase		:       Dict						    =	{
    """configuration_blip""": [
        """BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """BlipConfig""",
        """BlipTextConfig""",
        """BlipVisionConfig""",
    ],
    """processing_blip""": ["""BlipProcessor"""],
}

try:
					if not is_vision_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_UpperCAmelCase		:       Optional[int]						    =	["""BlipImageProcessor"""]


try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_UpperCAmelCase		:       Any						    =	[
					    """BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """BlipModel""",
					    """BlipPreTrainedModel""",
					    """BlipForConditionalGeneration""",
					    """BlipForQuestionAnswering""",
					    """BlipVisionModel""",
					    """BlipTextModel""",
					    """BlipForImageTextRetrieval""",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					_UpperCAmelCase		:       Any						    =	[
					    """TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST""",
					    """TFBlipModel""",
					    """TFBlipPreTrainedModel""",
					    """TFBlipForConditionalGeneration""",
					    """TFBlipForQuestionAnswering""",
					    """TFBlipVisionModel""",
					    """TFBlipTextModel""",
					    """TFBlipForImageTextRetrieval""",
					]

if TYPE_CHECKING:
					from .configuration_blip import BLIP_PRETRAINED_CONFIG_ARCHIVE_MAP, BlipConfig, BlipTextConfig, BlipVisionConfig
					from .processing_blip import BlipProcessor

					try:
										if not is_vision_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .image_processing_blip import BlipImageProcessor

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_blip import (
										    BLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
										    BlipForConditionalGeneration,
										    BlipForImageTextRetrieval,
										    BlipForQuestionAnswering,
										    BlipModel,
										    BlipPreTrainedModel,
										    BlipTextModel,
										    BlipVisionModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_blip import (
										    TF_BLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFBlipForConditionalGeneration,
										    TFBlipForImageTextRetrieval,
										    TFBlipForQuestionAnswering,
										    TFBlipModel,
										    TFBlipPreTrainedModel,
										    TFBlipTextModel,
										    TFBlipVisionModel,
										)

else:
					import sys

					_UpperCAmelCase		:       Optional[int]						    =	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




