


'''simple docstring'''
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


UpperCAmelCase_			:  Optional[int]										=   {
    'configuration_x_clip': [
        'XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'XCLIPConfig',
        'XCLIPTextConfig',
        'XCLIPVisionConfig',
    ],
    'processing_x_clip': ['XCLIPProcessor'],
}

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 UpperCAmelCase_			:  List[str]										=   [
     'XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST',
     'XCLIPModel',
     'XCLIPPreTrainedModel',
     'XCLIPTextModel',
     'XCLIPVisionModel',
 ]

if TYPE_CHECKING:
 from .configuration_x_clip import (
     XCLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
     XCLIPConfig,
     XCLIPTextConfig,
     XCLIPVisionConfig,
 )
 from .processing_x_clip import XCLIPProcessor

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_x_clip import (
      XCLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
      XCLIPModel,
      XCLIPPreTrainedModel,
      XCLIPTextModel,
      XCLIPVisionModel,
  )

else:
 import sys

 UpperCAmelCase_			:  Tuple										=   _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)










'''simple docstring'''
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     _lowerCamelCase       :




 '''simple docstring'''


 @property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  return self.get_dummy_input()


 @property
 def 			snake_case__ (   self    ):

  """simple docstring"""







  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 			snake_case__ (   self   ,   __lowercase=True   ,   __lowercase=False   ,   __lowercase=False   ,   __lowercase=False   ,   ):

  """simple docstring"""







  __A	:		List[str]	  =      4
  __A	:		List[Any]	  =      32
  __A	:		Optional[Any]	  =      (32, 32)

  __A	:		List[str]	  =      torch.manual_seed(0    )
  __A	:		Union[str, Any]	  =      torch.device(__lowercase    )
  __A	:		Optional[int]	  =      (batch_size, num_channels) + sizes
  __A	:		str	  =      randn_tensor(__lowercase   ,   generator=__lowercase   ,   device=__lowercase    )
  __A	:		int	  =      {'hidden_states': hidden_states}

  if include_temb:
   __A	:		Optional[Any]	  =      128
   __A	:		Any	  =      randn_tensor((batch_size, temb_channels)   ,   generator=__lowercase   ,   device=__lowercase    )

  if include_res_hidden_states_tuple:
   __A	:		Any	  =      torch.manual_seed(1    )
   __A	:		List[Any]	  =      (randn_tensor(__lowercase   ,   generator=__lowercase   ,   device=__lowercase    ),)

  if include_encoder_hidden_states:
   __A	:		str	  =      floats_tensor((batch_size, 32, 32)    ).to(__lowercase    )

  if include_skip_sample:
   __A	:		List[Any]	  =      randn_tensor(((batch_size, 3) + sizes)   ,   generator=__lowercase   ,   device=__lowercase    )

  return dummy_input


 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A	:		Tuple	  =      {
      'in_channels': 32,
      'out_channels': 32,
      'temb_channels': 128,
  }
  if self.block_type == "up":
   __A	:		int	  =      32

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

  __A	:		Optional[int]	  =      self.dummy_input
  return init_dict, inputs_dict


 def 			snake_case__ (   self   ,   __lowercase    ):

  """simple docstring"""







  __A  ,__A	:		List[Any]	  =      self.prepare_init_args_and_inputs_for_common()
  __A	:		int	  =      self.block_class(**__lowercase    )
  unet_block.to(__lowercase    )
  unet_block.eval()

  with torch.no_grad():
   __A	:		List[Any]	  =      unet_block(**__lowercase    )

  if isinstance(__lowercase   ,   __lowercase    ):
   __A	:		Tuple	  =      output[0]

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

  __A	:		Any	  =      output[0, -1, -3:, -3:]
  __A	:		Dict	  =      torch.tensor(__lowercase    ).to(__lowercase    )
  assert torch_all_close(output_slice.flatten()   ,   __lowercase   ,   atol=5E-3    )







 @unittest.skipIf(torch_device == 'mps'   ,   'Training is not supported in mps'    )
 def 			snake_case__ (   self    ):

  """simple docstring"""







  __A  ,__A	:		str	  =      self.prepare_init_args_and_inputs_for_common()
  __A	:		Optional[Any]	  =      self.block_class(**__lowercase    )
  model.to(__lowercase    )
  model.train()
  __A	:		Any	  =      model(**__lowercase    )

  if isinstance(__lowercase   ,   __lowercase    ):
   __A	:		Any	  =      output[0]

  __A	:		int	  =      torch.device(__lowercase    )
  __A	:		Dict	  =      randn_tensor(output.shape   ,   device=__lowercase    )
  __A	:		Dict	  =      torch.nn.functional.mse_loss(__lowercase   ,   __lowercase    )
  loss.backward()






