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 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return self.get_dummy_input()


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

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




       '''simple docstring'''

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

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

       if include_temb:
            UpperCAmelCase_							:       Any       =				1_2_8
            UpperCAmelCase_							:       Union[str, Any]       =				randn_tensor((batch_size, temb_channels)				,		generator=_UpperCamelCase				,		device=_UpperCamelCase						)

       if include_res_hidden_states_tuple:
            UpperCAmelCase_							:       Dict       =				torch.manual_seed(1						)
            UpperCAmelCase_							:       List[str]       =				(randn_tensor(_UpperCamelCase				,		generator=_UpperCamelCase				,		device=_UpperCamelCase						),)

       if include_encoder_hidden_states:
            UpperCAmelCase_							:       Dict       =				floats_tensor((batch_size, 3_2, 3_2)						).to(_UpperCamelCase						)

       if include_skip_sample:
            UpperCAmelCase_							:       Union[str, Any]       =				randn_tensor(((batch_size, 3) + sizes)				,		generator=_UpperCamelCase				,		device=_UpperCamelCase						)

       return dummy_input


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[Any]       =				{
           """in_channels""": 3_2,
           """out_channels""": 3_2,
           """temb_channels""": 1_2_8,
       }
       if self.block_type == "up":
            UpperCAmelCase_							:       Any       =				3_2

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

       UpperCAmelCase_							:       Tuple       =				self.dummy_input
       return init_dict, inputs_dict


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				self.prepare_init_args_and_inputs_for_common()
       UpperCAmelCase_							:       List[str]       =				self.block_class(**_UpperCamelCase						)
       unet_block.to(_UpperCamelCase						)
       unet_block.eval()

       with torch.no_grad():
            UpperCAmelCase_							:       Optional[int]       =				unet_block(**_UpperCamelCase						)

       if isinstance(_UpperCamelCase				,		_UpperCamelCase						):
            UpperCAmelCase_							:       str       =				output[0]

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

       UpperCAmelCase_							:       Tuple       =				output[0, -1, -3:, -3:]
       UpperCAmelCase_							:       Optional[Any]       =				torch.tensor(_UpperCamelCase						).to(_UpperCamelCase						)
       assert torch_all_close(output_slice.flatten()				,		_UpperCamelCase				,		atol=5E-3						)






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




       '''simple docstring'''

       UpperCAmelCase_							:       List[str]       =				self.prepare_init_args_and_inputs_for_common()
       UpperCAmelCase_							:       List[Any]       =				self.block_class(**_UpperCamelCase						)
       model.to(_UpperCamelCase						)
       model.train()
       UpperCAmelCase_							:       Union[str, Any]       =				model(**_UpperCamelCase						)

       if isinstance(_UpperCamelCase				,		_UpperCamelCase						):
            UpperCAmelCase_							:       Tuple       =				output[0]

       UpperCAmelCase_							:       Optional[int]       =				torch.device(_UpperCamelCase						)
       UpperCAmelCase_							:       Any       =				randn_tensor(output.shape				,		device=_UpperCamelCase						)
       UpperCAmelCase_							:       Union[str, Any]       =				torch.nn.functional.mse_loss(_UpperCamelCase				,		_UpperCamelCase						)
       loss.backward()







'''simple docstring'''


import warnings

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






class 					__SCREAMING_SNAKE_CASE						(	lowerCamelCase_	):







  '''simple docstring'''






  lowerCamelCase_		:Tuple						   =		['''image_processor''', '''tokenizer''']
  lowerCamelCase_		:Optional[Any]						   =		'''ViTImageProcessor'''
  lowerCamelCase_		:int						   =		('''CLIPTokenizer''', '''CLIPTokenizerFast''')


  def __init__(			self				,		snake_case_=None				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Dict       =				None
       if "feature_extractor" in kwargs:
            warnings.warn(
                'The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'
                ' instead.'				,		snake_case_				,		)
            UpperCAmelCase_							:       int       =				kwargs.pop('feature_extractor'						)

       UpperCAmelCase_							:       str       =				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__(snake_case_				,		snake_case_						)


  def __call__(			self				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		snake_case_=None				,		**snake_case_						):




       '''simple docstring'''

       if text is None and visual_prompt is None and images is None:
            raise ValueError('You have to specify either text, visual prompt or images.'						)

       if text is not None and visual_prompt is not None:
            raise ValueError('You have to specify exactly one type of prompt. Either text or visual prompt.'						)

       if text is not None:
            UpperCAmelCase_							:       Optional[int]       =				self.tokenizer(snake_case_				,		return_tensors=snake_case_				,		**snake_case_						)

       if visual_prompt is not None:
            UpperCAmelCase_							:       Optional[Any]       =				self.image_processor(snake_case_				,		return_tensors=snake_case_				,		**snake_case_						)

       if images is not None:
            UpperCAmelCase_							:       int       =				self.image_processor(snake_case_				,		return_tensors=snake_case_				,		**snake_case_						)

       if visual_prompt is not None and images is not None:
            UpperCAmelCase_							:       Tuple       =				{
                'pixel_values': image_features.pixel_values,
                'conditional_pixel_values': prompt_features.pixel_values,
            }
            return encoding
       elif text is not None and images is not None:
            UpperCAmelCase_							:       Optional[int]       =				image_features.pixel_values
            return encoding
       elif text is not None:
            return encoding
       elif visual_prompt is not None:
            UpperCAmelCase_							:       Dict       =				{
                'conditional_pixel_values': prompt_features.pixel_values,
            }
            return encoding
       else:
            return BatchEncoding(data=dict(**snake_case_						)				,		tensor_type=snake_case_						)


  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       return self.tokenizer.batch_decode(*snake_case_				,		**snake_case_						)


  def _UpperCamelCase					(			self				,		*snake_case_				,		**snake_case_						):




       '''simple docstring'''

       return self.tokenizer.decode(*snake_case_				,		**snake_case_						)


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       warnings.warn(
           '`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'				,		snake_case_				,		)
       return self.image_processor_class






  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       warnings.warn(
           '`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'				,		snake_case_				,		)
       return self.image_processor






