





"""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 			_a						:






				@property
				def     snake_case     (			self		:   Optional[Any]							)     ->				Dict:




										'''simple docstring'''



										return self.get_dummy_input()






				@property
				def     snake_case     (			self		:   int							)     ->				List[Any]:




										'''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     snake_case     (			self		:   List[Any],			lowerCAmelCase__		:   str=True,			lowerCAmelCase__		:   int=False,			lowerCAmelCase__		:   Any=False,			lowerCAmelCase__		:   List[str]=False,			)     ->				List[Any]:




										'''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     snake_case     (			self		:   Optional[Any]							)     ->				Optional[int]:




										'''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     snake_case     (			self		:   Union[str, Any],			lowerCAmelCase__		:   int							)     ->				Optional[Any]:




										'''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     snake_case     (			self		:   Optional[int]							)     ->				Optional[int]:




										'''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"""




from typing import List, Optional, Union

from ...image_utils import ImageInput
from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType




class 			_a						(				_lowerCAmelCase    ):
				UpperCamelCase								=   ['''image_processor''', '''tokenizer''']
				UpperCamelCase								=   '''BlipImageProcessor'''
				UpperCamelCase								=   '''AutoTokenizer'''






				def __init__(			self		:   List[str],			lowerCAmelCase__		:   Optional[int],			lowerCAmelCase__		:   Optional[int]							)     ->				int:




										'''simple docstring'''



										_UpperCamelCase      : Any         =   False
										super().__init__(lowerCAmelCase__,			lowerCAmelCase__							)
										_UpperCamelCase      : Tuple         =   self.image_processor






				def __call__(			self		:   str,			lowerCAmelCase__		:   ImageInput = None,			lowerCAmelCase__		:   Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Union[bool, str, PaddingStrategy] = False,			lowerCAmelCase__		:   Union[bool, str, TruncationStrategy] = None,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   int = 0,			lowerCAmelCase__		:   Optional[int] = None,			lowerCAmelCase__		:   Optional[bool] = None,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = False,			lowerCAmelCase__		:   bool = True,			lowerCAmelCase__		:   Optional[Union[str, TensorType]] = None,			**lowerCAmelCase__		:   Optional[Any],			)     ->				BatchEncoding:




										'''simple docstring'''



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

										# Get only text
										if images is None:
																_UpperCamelCase      : int         =   self.tokenizer
																_UpperCamelCase      : List[str]         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
																return text_encoding

										# add pixel_values
										_UpperCamelCase      : List[str]         =   self.image_processor(lowerCAmelCase__,			return_tensors=lowerCAmelCase__							)

										if text is not None:
																_UpperCamelCase      : Any         =   self.tokenizer(
																    text=lowerCAmelCase__,			add_special_tokens=lowerCAmelCase__,			padding=lowerCAmelCase__,			truncation=lowerCAmelCase__,			max_length=lowerCAmelCase__,			stride=lowerCAmelCase__,			pad_to_multiple_of=lowerCAmelCase__,			return_attention_mask=lowerCAmelCase__,			return_overflowing_tokens=lowerCAmelCase__,			return_special_tokens_mask=lowerCAmelCase__,			return_offsets_mapping=lowerCAmelCase__,			return_token_type_ids=lowerCAmelCase__,			return_length=lowerCAmelCase__,			verbose=lowerCAmelCase__,			return_tensors=lowerCAmelCase__,			**lowerCAmelCase__,			)
										else:
																_UpperCamelCase      : List[Any]         =   None

										if text_encoding is not None:
																encoding_image_processor.update(lowerCAmelCase__							)

										return encoding_image_processor






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   List[str],			**lowerCAmelCase__		:   str							)     ->				Optional[int]:




										'''simple docstring'''



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






				def     snake_case     (			self		:   List[Any],			*lowerCAmelCase__		:   Dict,			**lowerCAmelCase__		:   Any							)     ->				List[str]:




										'''simple docstring'''



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






				@property
				# Copied from transformers.models.blip.processing_blip.BlipProcessor.model_input_names
				def     snake_case     (			self		:   Any							)     ->				str:




										'''simple docstring'''



										_UpperCamelCase      : List[str]         =   self.tokenizer.model_input_names
										_UpperCamelCase      : Union[str, Any]         =   self.image_processor.model_input_names
										return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names							)							)



