


import gc
import random
import unittest

import numpy as np
import torch
from PIL import Image

from diffusers import (
    DDIMScheduler,
    KandinskyVaaInpaintPipeline,
    KandinskyVaaPriorPipeline,
    UNetaDConditionModel,
    VQModel,
)
from diffusers.utils import floats_tensor, load_image, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()






class 	__lowercase					(	__snake_case			,       unittest.TestCase	):
      UpperCamelCase										=							KandinskyVaaInpaintPipeline
      UpperCamelCase										=							['''image_embeds''', '''negative_image_embeds''', '''image''', '''mask_image''']
      UpperCamelCase										=							[
          '''image_embeds''',
          '''negative_image_embeds''',
          '''image''',
          '''mask_image''',
      ]
      UpperCamelCase										=							[
          '''generator''',
          '''height''',
          '''width''',
          '''latents''',
          '''guidance_scale''',
          '''num_inference_steps''',
          '''return_dict''',
          '''guidance_scale''',
          '''num_images_per_prompt''',
          '''output_type''',
          '''return_dict''',
      ]
      UpperCamelCase										=							False





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






         """simple docstring"""







         return 3_2





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






         """simple docstring"""







         return 3_2





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






         """simple docstring"""







         return self.time_input_dim





      @property
      def 							_lowercase					(		self    :       List[Any]							)     -> Tuple:






         """simple docstring"""







         return self.time_input_dim * 4





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






         """simple docstring"""







         return 1_0_0





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






         """simple docstring"""







         torch.manual_seed(0							)

         UpperCAmelCase      					=  {
             """in_channels""": 9,
             # Out channels is double in channels because predicts mean and variance
             """out_channels""": 8,
             """addition_embed_type""": """image""",
             """down_block_types""": ("""ResnetDownsampleBlock2D""", """SimpleCrossAttnDownBlock2D"""),
             """up_block_types""": ("""SimpleCrossAttnUpBlock2D""", """ResnetUpsampleBlock2D"""),
             """mid_block_type""": """UNetMidBlock2DSimpleCrossAttn""",
             """block_out_channels""": (self.block_out_channels_a, self.block_out_channels_a * 2),
             """layers_per_block""": 1,
             """encoder_hid_dim""": self.text_embedder_hidden_size,
             """encoder_hid_dim_type""": """image_proj""",
             """cross_attention_dim""": self.cross_attention_dim,
             """attention_head_dim""": 4,
             """resnet_time_scale_shift""": """scale_shift""",
             """class_embed_type""": None,
         }

         UpperCAmelCase      					=  UNetaDConditionModel(**__lowerCamelCase							)
         return model





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






         """simple docstring"""







         return {
             "block_out_channels": [3_2, 6_4],
             "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
             "in_channels": 3,
             "latent_channels": 4,
             "layers_per_block": 1,
             "norm_num_groups": 8,
             "norm_type": "spatial",
             "num_vq_embeddings": 1_2,
             "out_channels": 3,
             "up_block_types": [
                 "AttnUpDecoderBlock2D",
                 "UpDecoderBlock2D",
             ],
             "vq_embed_dim": 4,
         }





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






         """simple docstring"""







         torch.manual_seed(0							)
         UpperCAmelCase      					=  VQModel(**self.dummy_movq_kwargs							)
         return model





      def 							_lowercase					(		self    :       int							)     -> Any:






         """simple docstring"""







         UpperCAmelCase      					=  self.dummy_unet
         UpperCAmelCase      					=  self.dummy_movq

         UpperCAmelCase      					=  DDIMScheduler(
             num_train_timesteps=1_0_0_0       ,							beta_schedule="""linear"""       ,							beta_start=0.00_085       ,							beta_end=0.012       ,							clip_sample=__lowerCamelCase       ,							set_alpha_to_one=__lowerCamelCase       ,							steps_offset=1       ,							prediction_type="""epsilon"""       ,							thresholding=__lowerCamelCase       ,							)

         UpperCAmelCase      					=  {
             """unet""": unet,
             """scheduler""": scheduler,
             """movq""": movq,
         }

         return components





      def 							_lowercase					(		self    :       int       ,							__lowerCamelCase    :       Union[str, Any]       ,							__lowerCamelCase    :       int=0							)     -> Tuple:






         """simple docstring"""







         UpperCAmelCase      					=  floats_tensor((1, self.text_embedder_hidden_size)       ,							rng=random.Random(__lowerCamelCase							)							).to(__lowerCamelCase							)
         UpperCAmelCase      					=  floats_tensor((1, self.text_embedder_hidden_size)       ,							rng=random.Random(seed + 1							)							).to(
             __lowerCamelCase							)
         # create init_image
         UpperCAmelCase      					=  floats_tensor((1, 3, 6_4, 6_4)       ,							rng=random.Random(__lowerCamelCase							)							).to(__lowerCamelCase							)
         UpperCAmelCase      					=  image.cpu().permute(0       ,							2       ,							3       ,							1							)[0]
         UpperCAmelCase      					=  Image.fromarray(np.uinta(__lowerCamelCase							)							).convert("""RGB"""							).resize((2_5_6, 2_5_6)							)
         # create mask
         UpperCAmelCase      					=  np.ones((6_4, 6_4)       ,							dtype=np.floataa							)
         UpperCAmelCase      					=  0

         if str(__lowerCamelCase							).startswith("""mps"""							):
            UpperCAmelCase      					=  torch.manual_seed(__lowerCamelCase							)
         else:
            UpperCAmelCase      					=  torch.Generator(device=__lowerCamelCase							).manual_seed(__lowerCamelCase							)
         UpperCAmelCase      					=  {
             """image""": init_image,
             """mask_image""": mask,
             """image_embeds""": image_embeds,
             """negative_image_embeds""": negative_image_embeds,
             """generator""": generator,
             """height""": 6_4,
             """width""": 6_4,
             """num_inference_steps""": 2,
             """guidance_scale""": 4.0,
             """output_type""": """np""",
         }
         return inputs





      def 							_lowercase					(		self    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         UpperCAmelCase      					=  """cpu"""

         UpperCAmelCase      					=  self.get_dummy_components()

         UpperCAmelCase      					=  self.pipeline_class(**__lowerCamelCase							)
         UpperCAmelCase      					=  pipe.to(__lowerCamelCase							)

         pipe.set_progress_bar_config(disable=__lowerCamelCase							)

         UpperCAmelCase      					=  pipe(**self.get_dummy_inputs(__lowerCamelCase							)							)
         UpperCAmelCase      					=  output.images

         UpperCAmelCase      					=  pipe(
             **self.get_dummy_inputs(__lowerCamelCase							)       ,							return_dict=__lowerCamelCase       ,							)[0]

         UpperCAmelCase      					=  image[0, -3:, -3:, -1]
         UpperCAmelCase      					=  image_from_tuple[0, -3:, -3:, -1]

         print(F"""image.shape {image.shape}"""							)

         assert image.shape == (1, 6_4, 6_4, 3)

         UpperCAmelCase      					=  np.array(
             [0.50_775_903, 0.49_527_195, 0.48_824_543, 0.50_192_237, 0.48_644_906, 0.49_373_814, 0.4_780_598, 0.47_234_827, 0.48_327_848]							)

         assert (
             np.abs(image_slice.flatten() - expected_slice							).max() < 1e-2
         ), F""" expected_slice {expected_slice}, but got {image_slice.flatten()}"""
         assert (
             np.abs(image_from_tuple_slice.flatten() - expected_slice							).max() < 1e-2
         ), F""" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"""





      def 							_lowercase					(		self    :       Dict							)     -> List[Any]:






         """simple docstring"""







         super().test_inference_batch_single_identical(expected_max_diff=3e-3							)






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





      def 							_lowercase					(		self    :       List[str]							)     -> str:






         """simple docstring"""







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





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






         """simple docstring"""







         UpperCAmelCase      					=  load_numpy(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"""
             """/kandinskyv22/kandinskyv22_inpaint_cat_with_hat_fp16.npy"""							)

         UpperCAmelCase      					=  load_image(
             """https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main""" """/kandinsky/cat.png"""							)
         UpperCAmelCase      					=  np.ones((7_6_8, 7_6_8)       ,							dtype=np.floataa							)
         UpperCAmelCase      					=  0

         UpperCAmelCase      					=  """a hat"""

         UpperCAmelCase      					=  KandinskyVaaPriorPipeline.from_pretrained(
             """kandinsky-community/kandinsky-2-2-prior"""       ,							torch_dtype=torch.floataa							)
         pipe_prior.to(__lowerCamelCase							)

         UpperCAmelCase      					=  KandinskyVaaInpaintPipeline.from_pretrained(
             """kandinsky-community/kandinsky-2-2-decoder-inpaint"""       ,							torch_dtype=torch.floataa							)
         UpperCAmelCase      					=  pipeline.to(__lowerCamelCase							)
         pipeline.set_progress_bar_config(disable=__lowerCamelCase							)

         UpperCAmelCase      					=  torch.Generator(device="""cpu"""							).manual_seed(0							)
         UpperCAmelCase					,	UpperCAmelCase      					=  pipe_prior(
             __lowerCamelCase       ,							generator=__lowerCamelCase       ,							num_inference_steps=5       ,							negative_prompt=""""""       ,							).to_tuple()

         UpperCAmelCase      					=  pipeline(
             image=__lowerCamelCase       ,							mask_image=__lowerCamelCase       ,							image_embeds=__lowerCamelCase       ,							negative_image_embeds=__lowerCamelCase       ,							generator=__lowerCamelCase       ,							num_inference_steps=1_0_0       ,							height=7_6_8       ,							width=7_6_8       ,							output_type="""np"""       ,							)

         UpperCAmelCase      					=  output.images[0]

         assert image.shape == (7_6_8, 7_6_8, 3)

         assert_mean_pixel_difference(__lowerCamelCase       ,							__lowerCamelCase							)







import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a	 =      logging.get_logger(__name__)

__a	 =      {
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_text_model'''
      UpperCamelCase										=							['''past_key_values''']
      UpperCamelCase										=							{
          '''hidden_size''': '''hidden_size''',
          '''num_attention_heads''': '''num_heads''',
          '''num_hidden_layers''': '''num_layers''',
      }





      def __init__(		self    :       Optional[Any]       ,							__lowerCamelCase    :       Optional[int]=5_0_2_4_4       ,							__lowerCamelCase    :       int=7_6_8       ,							__lowerCamelCase    :       Union[str, Any]=6_4       ,							__lowerCamelCase    :       Dict=2_0_4_8       ,							__lowerCamelCase    :       int=1_2       ,							__lowerCamelCase    :       Any=1_2       ,							__lowerCamelCase    :       Union[str, Any]=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							__lowerCamelCase    :       Union[str, Any]=0.1       ,							__lowerCamelCase    :       str=1e-6       ,							__lowerCamelCase    :       int=1.0       ,							__lowerCamelCase    :       Optional[int]="gelu_new"       ,							__lowerCamelCase    :       int=0       ,							__lowerCamelCase    :       int=False       ,							__lowerCamelCase    :       Optional[Any]=0       ,							__lowerCamelCase    :       Dict=1       ,							__lowerCamelCase    :       List[Any]=False       ,							__lowerCamelCase    :       Optional[Any]=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> int:






         """simple docstring"""







         UpperCAmelCase      					=  vocab_size
         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  d_kv
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  num_layers
         UpperCAmelCase      					=  num_heads
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  layer_norm_epsilon
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  use_cache

         UpperCAmelCase      					=  eos_token_id
         UpperCAmelCase      					=  decoder_start_token_id

         # for backwards compatibility
         UpperCAmelCase      					=  dense_act_fn

         super().__init__(
             pad_token_id=__lowerCamelCase       ,							eos_token_id=__lowerCamelCase       ,							decoder_start_token_id=__lowerCamelCase       ,							tie_word_embeddings=__lowerCamelCase       ,							is_decoder=__lowerCamelCase       ,							**__lowerCamelCase       ,							)





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       Any							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the text config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""text_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_vision_model'''





      def __init__(		self    :       str       ,							__lowerCamelCase    :       Any=7_6_8       ,							__lowerCamelCase    :       Dict=7_6_8       ,							__lowerCamelCase    :       List[Any]=2_0_4_8       ,							__lowerCamelCase    :       List[Any]=6_4       ,							__lowerCamelCase    :       Union[str, Any]=1_2       ,							__lowerCamelCase    :       Optional[int]=1_2       ,							__lowerCamelCase    :       Tuple="gelu_new"       ,							__lowerCamelCase    :       Tuple=1e-6       ,							__lowerCamelCase    :       Any=0.0       ,							__lowerCamelCase    :       List[Any]=0.0       ,							__lowerCamelCase    :       int=1e-1_0       ,							__lowerCamelCase    :       Union[str, Any]=1.0       ,							__lowerCamelCase    :       Any=4_0_9_6       ,							__lowerCamelCase    :       Dict=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							**__lowerCamelCase    :       Optional[int]       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(**__lowerCamelCase							)

         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  patch_embed_hidden_size
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  num_hidden_layers
         UpperCAmelCase      					=  num_attention_heads
         UpperCAmelCase      					=  initializer_range
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  attention_dropout
         UpperCAmelCase      					=  layer_norm_eps
         UpperCAmelCase      					=  dense_act_fn
         UpperCAmelCase      					=  seq_len
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  d_kv





      @classmethod
      def 							_lowercase					(		cls    :       Any       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       int							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the vision config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""vision_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct'''
      UpperCamelCase										=							True





      def __init__(		self    :       str       ,							__lowerCamelCase    :       str=None       ,							__lowerCamelCase    :       Dict=None       ,							__lowerCamelCase    :       Optional[Any]=1.0       ,							__lowerCamelCase    :       str=0.02       ,							__lowerCamelCase    :       Any=False       ,							__lowerCamelCase    :       Tuple=False       ,							__lowerCamelCase    :       str=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(tie_word_embeddings=__lowerCamelCase       ,							is_encoder_decoder=__lowerCamelCase       ,							**__lowerCamelCase							)

         if text_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values."""							)

         if vision_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values."""							)

         UpperCAmelCase      					=  PixaStructTextConfig(**__lowerCamelCase							)
         UpperCAmelCase      					=  PixaStructVisionConfig(**__lowerCamelCase							)

         UpperCAmelCase      					=  self.text_config.decoder_start_token_id
         UpperCAmelCase      					=  self.text_config.pad_token_id
         UpperCAmelCase      					=  self.text_config.eos_token_id

         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  initializer_range

         UpperCAmelCase      					=  self.initializer_range
         UpperCAmelCase      					=  self.initializer_range

         UpperCAmelCase      					=  is_vqa





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       PixaStructTextConfig       ,							__lowerCamelCase    :       PixaStructVisionConfig       ,							**__lowerCamelCase    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         return cls(text_config=text_config.to_dict()       ,							vision_config=vision_config.to_dict()       ,							**__lowerCamelCase							)





      def 							_lowercase					(		self    :       List[str]							)     -> Optional[int]:






         """simple docstring"""







         UpperCAmelCase      					=  copy.deepcopy(self.__dict__							)
         UpperCAmelCase      					=  self.text_config.to_dict()
         UpperCAmelCase      					=  self.vision_config.to_dict()
         UpperCAmelCase      					=  self.__class__.model_type
         return output



