



import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMParallelScheduler,
    DDPMParallelScheduler,
    StableDiffusionParadigmsPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    require_torch_gpu,
)

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class 							lowerCAmelCase_		(		_snake_case							,		_snake_case							,		unittest.TestCase     ):
		SCREAMING_SNAKE_CASE_       :			str					       =	StableDiffusionParadigmsPipeline
		SCREAMING_SNAKE_CASE_       :			Union[str, Any]					       =	TEXT_TO_IMAGE_PARAMS
		SCREAMING_SNAKE_CASE_       :			Any					       =	TEXT_TO_IMAGE_BATCH_PARAMS
		SCREAMING_SNAKE_CASE_       :			Any					       =	TEXT_TO_IMAGE_IMAGE_PARAMS
		SCREAMING_SNAKE_CASE_       :			Optional[Any]					       =	TEXT_TO_IMAGE_IMAGE_PARAMS







		def 					a_  (    self  :   Tuple					)       ->		List[str]:


						'''simple docstring'''







						torch.manual_seed(0					)
						_UpperCAmelCase						:  Any     					=	UNetaDConditionModel(
						    block_out_channels=(32, 64)       ,		layers_per_block=2       ,		sample_size=32       ,		in_channels=4       ,		out_channels=4       ,		down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')       ,		up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')       ,		cross_attention_dim=32       ,		attention_head_dim=(2, 4)       ,		use_linear_projection=lowerCAmelCase__       ,		)
						_UpperCAmelCase						:  Tuple     					=	DDIMParallelScheduler(
						    beta_start=0.00_085       ,		beta_end=0.012       ,		beta_schedule='''scaled_linear'''       ,		clip_sample=lowerCAmelCase__       ,		set_alpha_to_one=lowerCAmelCase__       ,		)
						torch.manual_seed(0					)
						_UpperCAmelCase						:  Any     					=	AutoencoderKL(
						    block_out_channels=[32, 64]       ,		in_channels=3       ,		out_channels=3       ,		down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']       ,		up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']       ,		latent_channels=4       ,		sample_size=128       ,		)
						torch.manual_seed(0					)
						_UpperCAmelCase						:  Tuple     					=	CLIPTextConfig(
						    bos_token_id=0       ,		eos_token_id=2       ,		hidden_size=32       ,		intermediate_size=37       ,		layer_norm_eps=1E-05       ,		num_attention_heads=4       ,		num_hidden_layers=5       ,		pad_token_id=1       ,		vocab_size=1000       ,		hidden_act='''gelu'''       ,		projection_dim=512       ,		)
						_UpperCAmelCase						:  str     					=	CLIPTextModel(lowerCAmelCase__					)
						_UpperCAmelCase						:  Optional[Any]     					=	CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''					)

						_UpperCAmelCase						:  Dict     					=	{
						    '''unet''': unet,
						    '''scheduler''': scheduler,
						    '''vae''': vae,
						    '''text_encoder''': text_encoder,
						    '''tokenizer''': tokenizer,
						    '''safety_checker''': None,
						    '''feature_extractor''': None,
						}
						return components







		def 					a_  (    self  :   Optional[int]       ,		UpperCAmelCase_  :   Tuple       ,		UpperCAmelCase_  :   Dict=0					)       ->		Optional[int]:


						'''simple docstring'''







						if str(lowerCAmelCase__					).startswith('''mps'''					):
										_UpperCAmelCase						:  Optional[Any]     					=	torch.manual_seed(lowerCAmelCase__					)
						else:
										_UpperCAmelCase						:  Dict     					=	torch.Generator(device=lowerCAmelCase__					).manual_seed(lowerCAmelCase__					)
						_UpperCAmelCase						:  int     					=	{
						    '''prompt''': '''a photograph of an astronaut riding a horse''',
						    '''generator''': generator,
						    '''num_inference_steps''': 10,
						    '''guidance_scale''': 6.0,
						    '''output_type''': '''numpy''',
						    '''parallel''': 3,
						    '''debug''': True,
						}
						return inputs







		def 					a_  (    self  :   int					)       ->		int:


						'''simple docstring'''







						_UpperCAmelCase						:  str     					=	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase						:  List[Any]     					=	self.get_dummy_components()
						_UpperCAmelCase						:  Optional[Any]     					=	StableDiffusionParadigmsPipeline(**lowerCAmelCase__					)
						_UpperCAmelCase						:  str     					=	sd_pipe.to(lowerCAmelCase__					)
						sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__					)

						_UpperCAmelCase						:  Tuple     					=	self.get_dummy_inputs(lowerCAmelCase__					)
						_UpperCAmelCase						:  List[Any]     					=	sd_pipe(**lowerCAmelCase__					).images
						_UpperCAmelCase						:  str     					=	image[0, -3:, -3:, -1]
						assert image.shape == (1, 64, 64, 3)

						_UpperCAmelCase						:  Tuple     					=	np.array([0.4_773, 0.5_417, 0.4_723, 0.4_925, 0.5_631, 0.4_752, 0.5_240, 0.4_935, 0.5_023]					)

						assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2







		def 					a_  (    self  :   Any					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  Tuple     					=	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase						:  Optional[int]     					=	self.get_dummy_components()
						torch.manual_seed(0					)
						_UpperCAmelCase						:  str     					=	DDPMParallelScheduler()
						torch.manual_seed(0					)
						_UpperCAmelCase						:  Optional[Any]     					=	StableDiffusionParadigmsPipeline(**lowerCAmelCase__					)
						_UpperCAmelCase						:  Optional[int]     					=	sd_pipe.to(lowerCAmelCase__					)
						sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__					)

						_UpperCAmelCase						:  List[str]     					=	self.get_dummy_inputs(lowerCAmelCase__					)
						_UpperCAmelCase						:  Any     					=	sd_pipe(**lowerCAmelCase__					).images
						_UpperCAmelCase						:  int     					=	image[0, -3:, -3:, -1]
						assert image.shape == (1, 64, 64, 3)

						_UpperCAmelCase						:  List[Any]     					=	np.array([0.3_573, 0.4_420, 0.4_960, 0.4_799, 0.3_796, 0.3_879, 0.4_819, 0.4_365, 0.4_468]					)

						assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2







		def 					a_  (    self  :   List[Any]					)       ->		Optional[int]:


						'''simple docstring'''







						super().test_inference_batch_consistent(batch_sizes=[1, 2]					)







		def 					a_  (    self  :   Any					)       ->		Tuple:


						'''simple docstring'''







						super().test_inference_batch_single_identical(batch_size=2       ,		expected_max_diff=3E-3					)







		def 					a_  (    self  :   str					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  int     					=	'''cpu'''  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase						:  int     					=	self.get_dummy_components()
						_UpperCAmelCase						:  List[Any]     					=	StableDiffusionParadigmsPipeline(**lowerCAmelCase__					)
						_UpperCAmelCase						:  Tuple     					=	sd_pipe.to(lowerCAmelCase__					)
						sd_pipe.set_progress_bar_config(disable=lowerCAmelCase__					)

						_UpperCAmelCase						:  Optional[Any]     					=	self.get_dummy_inputs(lowerCAmelCase__					)
						_UpperCAmelCase						:  Tuple     					=	'''french fries'''
						_UpperCAmelCase						:  Dict     					=	sd_pipe(**lowerCAmelCase__       ,		negative_prompt=lowerCAmelCase__					)
						_UpperCAmelCase						:  str     					=	output.images
						_UpperCAmelCase						:  str     					=	image[0, -3:, -3:, -1]

						assert image.shape == (1, 64, 64, 3)

						_UpperCAmelCase						:  str     					=	np.array([0.4_771, 0.5_420, 0.4_683, 0.4_918, 0.5_636, 0.4_725, 0.5_230, 0.4_923, 0.5_015]					)

						assert np.abs(image_slice.flatten() - expected_slice					).max() < 1E-2





@slow
@require_torch_gpu
class 							lowerCAmelCase_		(		unittest.TestCase     ):







		def 					a_  (    self  :   Optional[Any]					)       ->		Dict:


						'''simple docstring'''







						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()







		def 					a_  (    self  :   Any       ,		UpperCAmelCase_  :   Union[str, Any]=0					)       ->		Any:


						'''simple docstring'''







						_UpperCAmelCase						:  List[Any]     					=	torch.Generator(device=lowerCAmelCase__					).manual_seed(lowerCAmelCase__					)
						_UpperCAmelCase						:  List[str]     					=	{
						    '''prompt''': '''a photograph of an astronaut riding a horse''',
						    '''generator''': generator,
						    '''num_inference_steps''': 10,
						    '''guidance_scale''': 7.5,
						    '''output_type''': '''numpy''',
						    '''parallel''': 3,
						    '''debug''': True,
						}
						return inputs







		def 					a_  (    self  :   Any					)       ->		Optional[Any]:


						'''simple docstring'''







						_UpperCAmelCase						:  Any     					=	'''stabilityai/stable-diffusion-2-base'''
						_UpperCAmelCase						:  Optional[int]     					=	DDIMParallelScheduler.from_pretrained(lowerCAmelCase__       ,		subfolder='''scheduler'''					)
						_UpperCAmelCase						:  Any     					=	StableDiffusionParadigmsPipeline.from_pretrained(lowerCAmelCase__       ,		scheduler=lowerCAmelCase__       ,		safety_checker=lowerCAmelCase__					)
						pipe.to(lowerCAmelCase__					)
						pipe.set_progress_bar_config(disable=lowerCAmelCase__					)
						pipe.enable_attention_slicing()

						_UpperCAmelCase						:  Optional[Any]     					=	self.get_inputs()
						_UpperCAmelCase						:  Optional[Any]     					=	pipe(**lowerCAmelCase__					).images
						_UpperCAmelCase						:  Any     					=	image[0, -3:, -3:, -1].flatten()

						assert image.shape == (1, 512, 512, 3)

						_UpperCAmelCase						:  Tuple     					=	np.array([0.9_622, 0.9_602, 0.9_748, 0.9_591, 0.9_630, 0.9_691, 0.9_661, 0.9_631, 0.9_741]					)

						assert np.abs(expected_slice - image_slice					).max() < 1E-2



import warnings
from typing import List, Optional, Union

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






class     __magic_name__     (							_snake_case     ):
 UpperCAmelCase				     =    ["""image_processor""", """tokenizer"""]
 UpperCAmelCase				     =    """LayoutLMv2ImageProcessor"""
 UpperCAmelCase				     =    ("""LayoutLMv2Tokenizer""", """LayoutLMv2TokenizerFast""")


 def __init__(    self		:			Any      ,    lowerCAmelCase__		:			Any=None      ,    lowerCAmelCase__		:			Optional[Any]=None      ,    **lowerCAmelCase__		:			int     )     ->   Union[str, Any]:
   UpperCAmelCase          =	None
   if "feature_extractor" in kwargs:
     warnings.warn(
         "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
         " instead."      ,    lowerCAmelCase__      ,    )
     UpperCAmelCase          =	kwargs.pop("feature_extractor"     )

   UpperCAmelCase          =	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__(lowerCAmelCase__      ,    lowerCAmelCase__     )


 def __call__(    self		:			int      ,    lowerCAmelCase__		:			Tuple      ,    lowerCAmelCase__		:			Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None      ,    lowerCAmelCase__		:			Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None      ,    lowerCAmelCase__		:			Union[List[List[int]], List[List[List[int]]]] = None      ,    lowerCAmelCase__		:			Optional[Union[List[int], List[List[int]]]] = None      ,    lowerCAmelCase__		:			bool = True      ,    lowerCAmelCase__		:			Union[bool, str, PaddingStrategy] = False      ,    lowerCAmelCase__		:			Union[bool, str, TruncationStrategy] = False      ,    lowerCAmelCase__		:			Optional[int] = None      ,    lowerCAmelCase__		:			int = 0      ,    lowerCAmelCase__		:			Optional[int] = None      ,    lowerCAmelCase__		:			Optional[bool] = None      ,    lowerCAmelCase__		:			Optional[bool] = None      ,    lowerCAmelCase__		:			bool = False      ,    lowerCAmelCase__		:			bool = False      ,    lowerCAmelCase__		:			bool = False      ,    lowerCAmelCase__		:			bool = False      ,    lowerCAmelCase__		:			bool = True      ,    lowerCAmelCase__		:			Optional[Union[str, TensorType]] = None      ,    **lowerCAmelCase__		:			Any      ,    )     ->   BatchEncoding:
   # verify input
   if self.image_processor.apply_ocr and (boxes is not None):
     raise ValueError(
         "You cannot provide bounding boxes if you initialized the image processor with apply_ocr set to True."     )

   if self.image_processor.apply_ocr and (word_labels is not None):
     raise ValueError(
         "You cannot provide word labels if you initialized the image processor with apply_ocr set to True."     )

   if return_overflowing_tokens is True and return_offsets_mapping is False:
     raise ValueError("You cannot return overflowing tokens without returning the offsets mapping."     )

   # first, apply the image processor
   UpperCAmelCase          =	self.image_processor(images=lowerCAmelCase__      ,    return_tensors=lowerCAmelCase__     )

   # second, apply the tokenizer
   if text is not None and self.image_processor.apply_ocr and text_pair is None:
     if isinstance(lowerCAmelCase__      ,    lowerCAmelCase__     ):
       UpperCAmelCase          =	[text]  # add batch dimension (as the image processor always adds a batch dimension)
     UpperCAmelCase          =	features["words"]

   UpperCAmelCase          =	self.tokenizer(
       text=text if text is not None else features["words"]      ,    text_pair=text_pair if text_pair is not None else None      ,    boxes=boxes if boxes is not None else features["boxes"]      ,    word_labels=lowerCAmelCase__      ,    add_special_tokens=lowerCAmelCase__      ,    padding=lowerCAmelCase__      ,    truncation=lowerCAmelCase__      ,    max_length=lowerCAmelCase__      ,    stride=lowerCAmelCase__      ,    pad_to_multiple_of=lowerCAmelCase__      ,    return_token_type_ids=lowerCAmelCase__      ,    return_attention_mask=lowerCAmelCase__      ,    return_overflowing_tokens=lowerCAmelCase__      ,    return_special_tokens_mask=lowerCAmelCase__      ,    return_offsets_mapping=lowerCAmelCase__      ,    return_length=lowerCAmelCase__      ,    verbose=lowerCAmelCase__      ,    return_tensors=lowerCAmelCase__      ,    **lowerCAmelCase__      ,    )

   # add pixel values
   UpperCAmelCase          =	features.pop("pixel_values"     )
   if return_overflowing_tokens is True:
     UpperCAmelCase          =	self.get_overflowing_images(lowerCAmelCase__      ,    encoded_inputs["overflow_to_sample_mapping"]     )
   UpperCAmelCase          =	images

   return encoded_inputs


 def _UpperCamelCase						(    self		:			str      ,    lowerCAmelCase__		:			Union[str, Any]      ,    lowerCAmelCase__		:			Union[str, Any]     )     ->   int:
   # in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
   UpperCAmelCase          =	[]
   for sample_idx in overflow_to_sample_mapping:
     images_with_overflow.append(images[sample_idx]     )

   if len(lowerCAmelCase__     ) != len(lowerCAmelCase__     ):
     raise ValueError(
         "Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got"
         f" {len(lowerCAmelCase__     )} and {len(lowerCAmelCase__     )}"     )

   return images_with_overflow


 def _UpperCamelCase						(    self		:			Optional[Any]      ,    *lowerCAmelCase__		:			int      ,    **lowerCAmelCase__		:			Union[str, Any]     )     ->   List[str]:
   return self.tokenizer.batch_decode(*lowerCAmelCase__      ,    **lowerCAmelCase__     )


 def _UpperCamelCase						(    self		:			Union[str, Any]      ,    *lowerCAmelCase__		:			Tuple      ,    **lowerCAmelCase__		:			Union[str, Any]     )     ->   List[Any]:
   return self.tokenizer.decode(*lowerCAmelCase__      ,    **lowerCAmelCase__     )


 @property
 def _UpperCamelCase						(    self		:			int     )     ->   List[str]:
   return ["input_ids", "bbox", "token_type_ids", "attention_mask", "image"]


 @property
 def _UpperCamelCase						(    self		:			Dict     )     ->   List[str]:
   warnings.warn(
       "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."      ,    lowerCAmelCase__      ,    )
   return self.image_processor_class


 @property
 def _UpperCamelCase						(    self		:			List[Any]     )     ->   int:
   warnings.warn(
       "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."      ,    lowerCAmelCase__      ,    )
   return self.image_processor



