

'''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 			lowercase_	:




  """simple docstring"""




  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]			):
   return self.get_dummy_input()






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     Union[str, Any]			):
   if self.block_type == "down":
    return (4, 3_2, 1_6, 1_6)
   elif self.block_type == "mid":
    return (4, 3_2, 3_2, 3_2)
   elif self.block_type == "up":
    return (4, 3_2, 6_4, 6_4)

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






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]					,lowercase__   :     int=True					,lowercase__   :     Dict=False					,lowercase__   :     Union[str, Any]=False					,lowercase__   :     Tuple=False					,):
   __lowercase							  = 4
   __lowercase							  = 3_2
   __lowercase							  = (3_2, 3_2)

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

   if include_temb:
    __lowercase							  = 1_2_8
    __lowercase							  = randn_tensor((batch_size, temb_channels)					,generator=lowercase__					,device=lowercase__			)

   if include_res_hidden_states_tuple:
    __lowercase							  = torch.manual_seed(1			)
    __lowercase							  = (randn_tensor(lowercase__					,generator=lowercase__					,device=lowercase__			),)

   if include_encoder_hidden_states:
    __lowercase							  = floats_tensor((batch_size, 3_2, 3_2)			).to(lowercase__			)

   if include_skip_sample:
    __lowercase							  = randn_tensor(((batch_size, 3) + sizes)					,generator=lowercase__					,device=lowercase__			)

   return dummy_input






  def 					SCREAMING_SNAKE_CASE      (      self   :     Any			):
   __lowercase							  = {
       '''in_channels''': 3_2,
       '''out_channels''': 3_2,
       '''temb_channels''': 1_2_8,
   }
   if self.block_type == "up":
    __lowercase							  = 3_2

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

   __lowercase							  = self.dummy_input
   return init_dict, inputs_dict






  def 					SCREAMING_SNAKE_CASE      (      self   :     Tuple					,lowercase__   :     Tuple			):
   __lowercase  ,	__lowercase							  = self.prepare_init_args_and_inputs_for_common()
   __lowercase							  = self.block_class(**lowercase__			)
   unet_block.to(lowercase__			)
   unet_block.eval()

   with torch.no_grad():
    __lowercase							  = unet_block(**lowercase__			)

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

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

   __lowercase							  = output[0, -1, -3:, -3:]
   __lowercase							  = 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 					SCREAMING_SNAKE_CASE      (      self   :     Dict			):
   __lowercase  ,	__lowercase							  = self.prepare_init_args_and_inputs_for_common()
   __lowercase							  = self.block_class(**lowercase__			)
   model.to(lowercase__			)
   model.train()
   __lowercase							  = model(**lowercase__			)

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

   __lowercase							  = torch.device(lowercase__			)
   __lowercase							  = randn_tensor(output.shape					,device=lowercase__			)
   __lowercase							  = torch.nn.functional.mse_loss(lowercase__					,lowercase__			)
   loss.backward()




'''simple docstring'''






import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding






class 			lowercase_	(lowerCamelCase__      ):




  """simple docstring"""




  SCREAMING_SNAKE_CASE    :  Optional[Any]							=			['image_processor', 'tokenizer']
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			'CLIPImageProcessor'
  SCREAMING_SNAKE_CASE    :  Union[str, Any]							=			('CLIPTokenizer', 'CLIPTokenizerFast')






  def __init__(      self   :     List[Any]					,lowercase__   :     Dict=None					,lowercase__   :     Union[str, Any]=None					,**lowercase__   :     Tuple			):
   __lowercase							  = None
   if "feature_extractor" in kwargs:
    warnings.warn(
        '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
        ''' instead.'''					,lowercase__					,)
    __lowercase							  = kwargs.pop('''feature_extractor'''			)

   __lowercase							  = image_processor if image_processor is not None else feature_extractor
   if image_processor is None:
    raise ValueError('''You need to specify an `image_processor`.'''			)
   if tokenizer is None:
    raise ValueError('''You need to specify a `tokenizer`.'''			)

   super().__init__(lowercase__					,lowercase__			)






  def __call__(      self   :     List[Any]					,lowercase__   :     str=None					,lowercase__   :     List[Any]=None					,lowercase__   :     Optional[Any]=None					,**lowercase__   :     int			):

   if text is None and images is None:
    raise ValueError('''You have to specify either text or images. Both cannot be none.'''			)

   if text is not None:
    __lowercase							  = self.tokenizer(lowercase__					,return_tensors=lowercase__					,**lowercase__			)

   if images is not None:
    __lowercase							  = self.image_processor(lowercase__					,return_tensors=lowercase__					,**lowercase__			)

   if text is not None and images is not None:
    __lowercase							  = image_features.pixel_values
    return encoding
   elif text is not None:
    return encoding
   else:
    return BatchEncoding(data=dict(**lowercase__			)					,tensor_type=lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]					,*lowercase__   :     List[str]					,**lowercase__   :     int			):
   return self.tokenizer.batch_decode(*lowercase__					,**lowercase__			)






  def 					SCREAMING_SNAKE_CASE      (      self   :     List[Any]					,*lowercase__   :     Optional[int]					,**lowercase__   :     Union[str, Any]			):
   return self.tokenizer.decode(*lowercase__					,**lowercase__			)






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[Any]			):
   __lowercase							  = self.tokenizer.model_input_names
   __lowercase							  = self.image_processor.model_input_names
   return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names			)			)






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     List[str]			):
   warnings.warn(
       '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''					,lowercase__					,)
   return self.image_processor_class






  @property
  def 					SCREAMING_SNAKE_CASE      (      self   :     Optional[int]			):
   warnings.warn(
       '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''					,lowercase__					,)
   return self.image_processor

