
import random
import unittest

import numpy as np
import torch
from PIL import Image
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorEmbaEmbPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import floats_tensor, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()







class     __A(				a						,     unittest.TestCase    ):
				snake_case_		     =					KandinskyVaaPriorEmbaEmbPipeline
				snake_case_		     =					['''prompt''', '''image''']
				snake_case_		     =					['''prompt''', '''image''']
				snake_case_		     =					[
				    '''num_images_per_prompt''',
				    '''strength''',
				    '''generator''',
				    '''num_inference_steps''',
				    '''latents''',
				    '''negative_prompt''',
				    '''guidance_scale''',
				    '''output_type''',
				    '''return_dict''',
				]
				snake_case_		     =					False




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Tuple:


								'''simple docstring'''




								return 32




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Any:


								'''simple docstring'''




								return 32




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Any:


								'''simple docstring'''




								return self.time_input_dim




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   str:


								'''simple docstring'''




								return self.time_input_dim * 4




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   str:


								'''simple docstring'''




								return 100




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   str:


								'''simple docstring'''




								__a    							=					CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''       )
								return tokenizer




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Any:


								'''simple docstring'''




								torch.manual_seed(0       )
								__a    							=					CLIPTextConfig(
								    bos_token_id=0	,  eos_token_id=2	,  hidden_size=self.text_embedder_hidden_size	,  projection_dim=self.text_embedder_hidden_size	,  intermediate_size=37	,  layer_norm_eps=1E-05	,  num_attention_heads=4	,  num_hidden_layers=5	,  pad_token_id=1	,  vocab_size=1_000	,  )
								return CLIPTextModelWithProjection(_snake_case       )




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   List[str]:


								'''simple docstring'''




								torch.manual_seed(0       )

								__a    							=					{
								    '''num_attention_heads''': 2,
								    '''attention_head_dim''': 12,
								    '''embedding_dim''': self.text_embedder_hidden_size,
								    '''num_layers''': 1,
								}

								__a    							=					PriorTransformer(**_snake_case       )
								# clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
								__a    							=					nn.Parameter(torch.ones(model.clip_std.shape       )       )
								return model




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Union[str, Any]:


								'''simple docstring'''




								torch.manual_seed(0       )
								__a    							=					CLIPVisionConfig(
								    hidden_size=self.text_embedder_hidden_size	,  image_size=224	,  projection_dim=self.text_embedder_hidden_size	,  intermediate_size=37	,  num_attention_heads=4	,  num_channels=3	,  num_hidden_layers=5	,  patch_size=14	,  )

								__a    							=					CLIPVisionModelWithProjection(_snake_case       )
								return model




				@property
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Union[str, Any]:


								'''simple docstring'''




								__a    							=					CLIPImageProcessor(
								    crop_size=224	,  do_center_crop=_snake_case	,  do_normalize=_snake_case	,  do_resize=_snake_case	,  image_mean=[0.4814_5466, 0.457_8275, 0.4082_1073]	,  image_std=[0.2686_2954, 0.2613_0258, 0.2757_7711]	,  resample=3	,  size=224	,  )

								return image_processor




				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Optional[int]:


								'''simple docstring'''




								__a    							=					self.dummy_prior
								__a    							=					self.dummy_image_encoder
								__a    							=					self.dummy_text_encoder
								__a    							=					self.dummy_tokenizer
								__a    							=					self.dummy_image_processor

								__a    							=					UnCLIPScheduler(
								    variance_type='''fixed_small_log'''	,  prediction_type='''sample'''	,  num_train_timesteps=1_000	,  clip_sample=_snake_case	,  clip_sample_range=10.0	,  )

								__a    							=					{
								    '''prior''': prior,
								    '''image_encoder''': image_encoder,
								    '''text_encoder''': text_encoder,
								    '''tokenizer''': tokenizer,
								    '''scheduler''': scheduler,
								    '''image_processor''': image_processor,
								}

								return components




				def        SCREAMING_SNAKE_CASE_	(  self	,  _snake_case	,  _snake_case=0       )					->   str:


								'''simple docstring'''




								if str(_snake_case       ).startswith('''mps'''       ):
												__a    							=					torch.manual_seed(_snake_case       )
								else:
												__a    							=					torch.Generator(device=_snake_case       ).manual_seed(_snake_case       )

								__a    							=					floats_tensor((1, 3, 64, 64)	,  rng=random.Random(_snake_case       )       ).to(_snake_case       )
								__a    							=					image.cpu().permute(0	,  2	,  3	,  1       )[0]
								__a    							=					Image.fromarray(np.uinta(_snake_case       )       ).convert('''RGB'''       ).resize((256, 256)       )

								__a    							=					{
								    '''prompt''': '''horse''',
								    '''image''': init_image,
								    '''strength''': 0.5,
								    '''generator''': generator,
								    '''guidance_scale''': 4.0,
								    '''num_inference_steps''': 2,
								    '''output_type''': '''np''',
								}
								return inputs




				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Tuple:


								'''simple docstring'''




								__a    							=					'''cpu'''

								__a    							=					self.get_dummy_components()

								__a    							=					self.pipeline_class(**_snake_case       )
								__a    							=					pipe.to(_snake_case       )

								pipe.set_progress_bar_config(disable=_snake_case       )

								__a    							=					pipe(**self.get_dummy_inputs(_snake_case       )       )
								__a    							=					output.image_embeds

								__a    							=					pipe(
								    **self.get_dummy_inputs(_snake_case       )	,  return_dict=_snake_case	,  )[0]

								__a    							=					image[0, -10:]
								__a    							=					image_from_tuple[0, -10:]

								assert image.shape == (1, 32)

								__a    							=					np.array(
								    [
								        0.107_1284,
								        1.333_0271,
								        0.6126_0223,
								        -0.669_1065,
								        -0.384_6852,
								        -1.030_3661,
								        0.2271_6111,
								        0.0334_8901,
								        0.3004_0675,
								        -0.2480_5029,
								    ]       )

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




				@skip_mps
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   int:


								'''simple docstring'''




								__a    							=					torch_device == '''cpu'''
								__a    							=					True
								__a    							=					False

								self._test_inference_batch_single_identical(
								    test_max_difference=_snake_case	,  relax_max_difference=_snake_case	,  test_mean_pixel_difference=_snake_case	,  )




				@skip_mps
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   List[str]:


								'''simple docstring'''




								__a    							=					torch_device == '''cpu'''
								__a    							=					False

								self._test_attention_slicing_forward_pass(
								    test_max_difference=_snake_case	,  test_mean_pixel_difference=_snake_case	,  )

from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


A  :			int     				=				logging.get_logger(__name__)

A  :			Dict     				=				{
    'layoutlmv2-base-uncased': 'https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json',
    'layoutlmv2-large-uncased': 'https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json',
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
					import detectrona







class     __A(				a    ):
				snake_case_		     =					'''layoutlmv2'''




				def __init__(  self	,  _snake_case=30_522	,  _snake_case=768	,  _snake_case=12	,  _snake_case=12	,  _snake_case=3_072	,  _snake_case="gelu"	,  _snake_case=0.1	,  _snake_case=0.1	,  _snake_case=512	,  _snake_case=2	,  _snake_case=0.02	,  _snake_case=1E-12	,  _snake_case=0	,  _snake_case=1_024	,  _snake_case=128	,  _snake_case=32	,  _snake_case=True	,  _snake_case=256	,  _snake_case=64	,  _snake_case=True	,  _snake_case=[7, 7, 256]	,  _snake_case=128	,  _snake_case=128	,  _snake_case=True	,  _snake_case=True	,  _snake_case=False	,  _snake_case=None	,  **_snake_case	,  )					->   Optional[int]:


								'''simple docstring'''




								super().__init__(
								    vocab_size=_snake_case	,  hidden_size=_snake_case	,  num_hidden_layers=_snake_case	,  num_attention_heads=_snake_case	,  intermediate_size=_snake_case	,  hidden_act=_snake_case	,  hidden_dropout_prob=_snake_case	,  attention_probs_dropout_prob=_snake_case	,  max_position_embeddings=_snake_case	,  type_vocab_size=_snake_case	,  initializer_range=_snake_case	,  layer_norm_eps=_snake_case	,  pad_token_id=_snake_case	,  **_snake_case	,  )
								__a    							=					max_ad_position_embeddings
								__a    							=					max_rel_pos
								__a    							=					rel_pos_bins
								__a    							=					fast_qkv
								__a    							=					max_rel_ad_pos
								__a    							=					rel_ad_pos_bins
								__a    							=					convert_sync_batchnorm
								__a    							=					image_feature_pool_shape
								__a    							=					coordinate_size
								__a    							=					shape_size
								__a    							=					has_relative_attention_bias
								__a    							=					has_spatial_attention_bias
								__a    							=					has_visual_segment_embedding
								__a    							=					(
								    detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
								)




				@classmethod
				def        SCREAMING_SNAKE_CASE_	(  self       )					->   Dict:


								'''simple docstring'''




								return {
								    "MODEL.MASK_ON": True,
								    "MODEL.PIXEL_STD": [57.375, 57.120, 58.395],
								    "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
								    "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
								    "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [128], [256], [512]],
								    "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
								    "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2_000,
								    "MODEL.RPN.PRE_NMS_TOPK_TEST": 1_000,
								    "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1_000,
								    "MODEL.POST_NMS_TOPK_TEST": 1_000,
								    "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
								    "MODEL.ROI_HEADS.NUM_CLASSES": 5,
								    "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
								    "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
								    "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
								    "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
								    "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
								    "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
								    "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
								    "MODEL.RESNETS.DEPTH": 101,
								    "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
								    "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
								    "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
								    "MODEL.RESNETS.NUM_GROUPS": 32,
								    "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
								    "MODEL.RESNETS.STRIDE_IN_1X1": False,
								}




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


								'''simple docstring'''




								__a    							=					detectrona.config.get_cfg()
								for k, v in self.detectrona_config_args.items():
												__a    							=					k.split('''.'''       )
												__a    							=					detectrona_config
												for attribute in attributes[:-1]:
																__a    							=					getattr(_snake_case	,  _snake_case       )
												setattr(_snake_case	,  attributes[-1]	,  _snake_case       )

								return detectrona_config