

import gc
import os
import tempfile
import unittest

import torch
import torch.nn as nn
import torch.nn.functional as F
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import AutoencoderKL, DDIMScheduler, StableDiffusionPipeline, UNetaDConditionModel
from diffusers.loaders import AttnProcsLayers, LoraLoaderMixin
from diffusers.models.attention_processor import (
    Attention,
    AttnProcessor,
    AttnProcessor2_0,
    LoRAAttnProcessor,
    LoRAAttnProcessor2_0,
    LoRAXFormersAttnProcessor,
    XFormersAttnProcessor,
)
from diffusers.utils import TEXT_ENCODER_ATTN_MODULE, floats_tensor, torch_device
def 		UpperCamelCase_(    snake_case__:						nn.Module     ) ->				str:
 UpperCAmelCase__           =       {}
 for name in unet.attn_processors.keys():
  UpperCAmelCase__           =       None if name.endswith('attn1.processor'     ) else unet.config.cross_attention_dim
  if name.startswith('mid_block'     ):
   UpperCAmelCase__           =       unet.config.block_out_channels[-1]
  elif name.startswith('up_blocks'     ):
   UpperCAmelCase__           =       int(name[len('up_blocks.'     )]     )
   UpperCAmelCase__           =       list(reversed(unet.config.block_out_channels     )     )[block_id]
  elif name.startswith('down_blocks'     ):
   UpperCAmelCase__           =       int(name[len('down_blocks.'     )]     )
   UpperCAmelCase__           =       unet.config.block_out_channels[block_id]
  UpperCAmelCase__           =       (
      LoRAAttnProcessor2_0 if hasattr(snake_case__						,					'scaled_dot_product_attention'     ) else LoRAAttnProcessor
  )
  UpperCAmelCase__           =       lora_attn_processor_class(
      hidden_size=snake_case__						,					cross_attention_dim=snake_case__     )
 UpperCAmelCase__           =       AttnProcsLayers(snake_case__     )
 return lora_attn_procs, unet_lora_layers
def 		UpperCamelCase_(    snake_case__:						nn.Module     ) ->				Any:
 UpperCAmelCase__           =       {}
 UpperCAmelCase__           =       (
     LoRAAttnProcessor2_0 if hasattr(snake_case__						,					'scaled_dot_product_attention'     ) else LoRAAttnProcessor
 )
 for name, module in text_encoder.named_modules():
  if name.endswith(snake_case__     ):
   UpperCAmelCase__           =       lora_attn_processor_class(
       hidden_size=module.out_proj.out_features						,					cross_attention_dim=snake_case__     )
 return text_lora_attn_procs
def 		UpperCamelCase_(    snake_case__:						nn.Module     ) ->				Optional[int]:
 UpperCAmelCase__           =       create_text_encoder_lora_attn_procs(snake_case__     )
 UpperCAmelCase__           =       AttnProcsLayers(snake_case__     )
 return text_encoder_lora_layers
def 		UpperCamelCase_(    snake_case__:						str						,					snake_case__:						Tuple=False     ) ->				Any:
 for _, attn_proc in text_lora_attn_procs.items():
  # set up.weights
  for layer_name, layer_module in attn_proc.named_modules():
   if layer_name.endswith('_lora'     ):
    UpperCAmelCase__           =       (
        torch.randn_like(layer_module.up.weight     )
        if randn_weight
        else torch.zeros_like(layer_module.up.weight     )
    )
    UpperCAmelCase__           =       torch.nn.Parameter(snake_case__     )







class 					lowercase		( unittest.TestCase      ):



 '''simple docstring'''


 def 						UpperCamelCase__							(self				)				->      Any:






  """simple docstring"""





  torch.manual_seed(0				)
  UpperCAmelCase__           =       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 , )
  UpperCAmelCase__           =       DDIMScheduler(
      beta_start=0.0_00_85 , beta_end=0.0_12 , beta_schedule='scaled_linear' , clip_sample=__a , set_alpha_to_one=__a , steps_offset=1 , )
  torch.manual_seed(0				)
  UpperCAmelCase__           =       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 , )
  UpperCAmelCase__           =       CLIPTextConfig(
      bos_token_id=0 , eos_token_id=2 , hidden_size=32 , intermediate_size=37 , layer_norm_eps=1E-0_5 , num_attention_heads=4 , num_hidden_layers=5 , pad_token_id=1 , vocab_size=1000 , )
  UpperCAmelCase__           =       CLIPTextModel(__a				)
  UpperCAmelCase__           =       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'				)

  UpperCAmelCase__       ,					UpperCAmelCase__           =       create_unet_lora_layers(__a				)
  UpperCAmelCase__           =       create_text_encoder_lora_layers(__a				)

  UpperCAmelCase__           =       {
      'unet': unet,
      'scheduler': scheduler,
      'vae': vae,
      'text_encoder': text_encoder,
      'tokenizer': tokenizer,
      'safety_checker': None,
      'feature_extractor': None,
  }
  UpperCAmelCase__           =       {
      'unet_lora_layers': unet_lora_layers,
      'text_encoder_lora_layers': text_encoder_lora_layers,
      'unet_lora_attn_procs': unet_lora_attn_procs,
  }
  return pipeline_components, lora_components




 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  UpperCAmelCase__           =       1
  UpperCAmelCase__           =       10
  UpperCAmelCase__           =       4
  UpperCAmelCase__           =       (32, 32)

  UpperCAmelCase__           =       torch.manual_seed(0				)
  UpperCAmelCase__           =       floats_tensor((batch_size, num_channels) + sizes				)
  UpperCAmelCase__           =       torch.randint(1 , __a , size=(batch_size, sequence_length) , generator=__a				)

  UpperCAmelCase__           =       {
      'prompt': 'A painting of a squirrel eating a burger',
      'generator': generator,
      'num_inference_steps': 2,
      'guidance_scale': 6.0,
      'output_type': 'numpy',
  }

  return noise, input_ids, pipeline_inputs

  # copied from: https://colab.research.google.com/gist/sayakpaul/df2ef6e1ae6d8c10a49d859883b10860/scratchpad.ipynb




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






  """simple docstring"""





  UpperCAmelCase__           =       77

  UpperCAmelCase__           =       torch.randint(2 , 56 , size=(1, max_seq_length) , generator=torch.manual_seed(0				)				)

  UpperCAmelCase__           =       {}
  UpperCAmelCase__           =       inputs
  return prepared_inputs




 def 						UpperCamelCase__							(self , __a				)				->      Optional[Any]:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  LoraLoaderMixin.save_lora_weights(
      save_directory=__a , unet_lora_layers=lora_components['unet_lora_layers'] , text_encoder_lora_layers=lora_components['text_encoder_lora_layers'] , )
  self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.bin'				)				)				)




 def 						UpperCamelCase__							(self				)				->      Any:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
  UpperCAmelCase__           =       sd_pipe.to(__a				)
  sd_pipe.set_progress_bar_config(disable=__a				)

  UpperCAmelCase__       ,					UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_inputs()

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       original_images[0, -3:, -3:, -1]

  with tempfile.TemporaryDirectory() as tmpdirname:
   LoraLoaderMixin.save_lora_weights(
       save_directory=__a , unet_lora_layers=lora_components['unet_lora_layers'] , text_encoder_lora_layers=lora_components['text_encoder_lora_layers'] , )
   self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.bin'				)				)				)
   sd_pipe.load_lora_weights(__a				)

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       lora_images[0, -3:, -3:, -1]

  # Outputs shouldn't match.
  self.assertFalse(torch.allclose(torch.from_numpy(__a				) , torch.from_numpy(__a				)				)				)




 def 						UpperCamelCase__							(self				)				->      List[Any]:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
  UpperCAmelCase__           =       sd_pipe.to(__a				)
  sd_pipe.set_progress_bar_config(disable=__a				)

  UpperCAmelCase__       ,					UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_inputs()

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       original_images[0, -3:, -3:, -1]

  with tempfile.TemporaryDirectory() as tmpdirname:
   LoraLoaderMixin.save_lora_weights(
       save_directory=__a , unet_lora_layers=lora_components['unet_lora_layers'] , text_encoder_lora_layers=lora_components['text_encoder_lora_layers'] , safe_serialization=__a , )
   self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.safetensors'				)				)				)
   sd_pipe.load_lora_weights(__a				)

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       lora_images[0, -3:, -3:, -1]

  # Outputs shouldn't match.
  self.assertFalse(torch.allclose(torch.from_numpy(__a				) , torch.from_numpy(__a				)				)				)




 def 						UpperCamelCase__							(self				)				->      str:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       lora_components['unet_lora_attn_procs']
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
  UpperCAmelCase__           =       sd_pipe.to(__a				)
  sd_pipe.set_progress_bar_config(disable=__a				)

  UpperCAmelCase__       ,					UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_inputs()

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       original_images[0, -3:, -3:, -1]

  with tempfile.TemporaryDirectory() as tmpdirname:
   UpperCAmelCase__           =       sd_pipe.unet
   unet.set_attn_processor(__a				)
   unet.save_attn_procs(__a				)
   self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.bin'				)				)				)
   sd_pipe.load_lora_weights(__a				)

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       lora_images[0, -3:, -3:, -1]

  # Outputs shouldn't match.
  self.assertFalse(torch.allclose(torch.from_numpy(__a				) , torch.from_numpy(__a				)				)				)




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






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)

  UpperCAmelCase__           =       self.get_dummy_tokens()

  # inference without lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_without_lora.shape == (1, 77, 32)

  # create lora_attn_procs with zeroed out up.weights
  UpperCAmelCase__           =       create_text_encoder_lora_attn_procs(pipe.text_encoder				)
  set_lora_up_weights(__a , randn_weight=__a				)

  # monkey patch
  pipe._modify_text_encoder(__a				)

  # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
  del text_attn_procs
  gc.collect()

  # inference with lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_with_lora.shape == (1, 77, 32)

  assert torch.allclose(
      __a , __a				), "lora_up_weight are all zero, so the lora outputs should be the same to without lora outputs"

  # create lora_attn_procs with randn up.weights
  UpperCAmelCase__           =       create_text_encoder_lora_attn_procs(pipe.text_encoder				)
  set_lora_up_weights(__a , randn_weight=__a				)

  # monkey patch
  pipe._modify_text_encoder(__a				)

  # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
  del text_attn_procs
  gc.collect()

  # inference with lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_with_lora.shape == (1, 77, 32)

  assert not torch.allclose(
      __a , __a				), "lora_up_weight are not zero, so the lora outputs should be different to without lora outputs"




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






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)

  UpperCAmelCase__           =       self.get_dummy_tokens()

  # inference without lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_without_lora.shape == (1, 77, 32)

  # create lora_attn_procs with randn up.weights
  UpperCAmelCase__           =       create_text_encoder_lora_attn_procs(pipe.text_encoder				)
  set_lora_up_weights(__a , randn_weight=__a				)

  # monkey patch
  pipe._modify_text_encoder(__a				)

  # verify that it's okay to release the text_attn_procs which holds the LoRAAttnProcessor.
  del text_attn_procs
  gc.collect()

  # inference with lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_with_lora.shape == (1, 77, 32)

  assert not torch.allclose(
      __a , __a				), "lora outputs should be different to without lora outputs"

  # remove monkey patch
  pipe._remove_text_encoder_monkey_patch()

  # inference with removed lora
  UpperCAmelCase__           =       pipe.text_encoder(**__a				)[0]
  assert outputs_without_lora_removed.shape == (1, 77, 32)

  assert torch.allclose(
      __a , __a				), "remove lora monkey patch should restore the original outputs"




 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
  UpperCAmelCase__           =       sd_pipe.to(__a				)
  sd_pipe.set_progress_bar_config(disable=__a				)

  UpperCAmelCase__       ,					UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_inputs()

  with tempfile.TemporaryDirectory() as tmpdirname:
   LoraLoaderMixin.save_lora_weights(
       save_directory=__a , unet_lora_layers=lora_components['unet_lora_layers'] , text_encoder_lora_layers=lora_components['text_encoder_lora_layers'] , )
   self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.bin'				)				)				)
   sd_pipe.load_lora_weights(__a				)

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       lora_images[0, -3:, -3:, -1]

  UpperCAmelCase__           =       sd_pipe(**__a , cross_attention_kwargs={'scale': 0.5}				).images
  UpperCAmelCase__           =       lora_images_with_scale[0, -3:, -3:, -1]

  # Outputs shouldn't match.
  self.assertFalse(
      torch.allclose(torch.from_numpy(__a				) , torch.from_numpy(__a				)				)				)




 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  with tempfile.TemporaryDirectory() as tmpdirname:
   self.create_lora_weight_file(__a				)

   UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
   UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
   UpperCAmelCase__           =       sd_pipe.to(__a				)
   sd_pipe.set_progress_bar_config(disable=__a				)

   # check if vanilla attention processors are used
   for _, module in sd_pipe.unet.named_modules():
    if isinstance(__a , __a				):
     self.assertIsInstance(module.processor , (AttnProcessor, AttnProcessor2_0)				)

            # load LoRA weight file
   sd_pipe.load_lora_weights(__a				)

   # check if lora attention processors are used
   for _, module in sd_pipe.unet.named_modules():
    if isinstance(__a , __a				):
     UpperCAmelCase__           =       (
         LoRAAttnProcessor2_0 if hasattr(__a , 'scaled_dot_product_attention'				) else LoRAAttnProcessor
     )
     self.assertIsInstance(module.processor , __a				)




 @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU'				)
 def 						UpperCamelCase__							(self				)				->      str:






  """simple docstring"""





  with tempfile.TemporaryDirectory() as tmpdirname:
   self.create_lora_weight_file(__a				)

   UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
   UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
   UpperCAmelCase__           =       sd_pipe.to(__a				)
   sd_pipe.set_progress_bar_config(disable=__a				)

   # enable XFormers
   sd_pipe.enable_xformers_memory_efficient_attention()

   # check if xFormers attention processors are used
   for _, module in sd_pipe.unet.named_modules():
    if isinstance(__a , __a				):
     self.assertIsInstance(module.processor , __a				)

            # load LoRA weight file
   sd_pipe.load_lora_weights(__a				)

   # check if lora attention processors are used
   for _, module in sd_pipe.unet.named_modules():
    if isinstance(__a , __a				):
     self.assertIsInstance(module.processor , __a				)




 @unittest.skipIf(torch_device != 'cuda' , 'This test is supposed to run on GPU'				)
 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_components()
  UpperCAmelCase__           =       StableDiffusionPipeline(**__a				)
  UpperCAmelCase__           =       sd_pipe.to(__a				)
  sd_pipe.set_progress_bar_config(disable=__a				)

  UpperCAmelCase__       ,					UpperCAmelCase__       ,					UpperCAmelCase__           =       self.get_dummy_inputs()

  # enable XFormers
  sd_pipe.enable_xformers_memory_efficient_attention()

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       original_images[0, -3:, -3:, -1]

  with tempfile.TemporaryDirectory() as tmpdirname:
   LoraLoaderMixin.save_lora_weights(
       save_directory=__a , unet_lora_layers=lora_components['unet_lora_layers'] , text_encoder_lora_layers=lora_components['text_encoder_lora_layers'] , )
   self.assertTrue(os.path.isfile(os.path.join(__a , 'pytorch_lora_weights.bin'				)				)				)
   sd_pipe.load_lora_weights(__a				)

  UpperCAmelCase__           =       sd_pipe(**__a				).images
  UpperCAmelCase__           =       lora_images[0, -3:, -3:, -1]

  # Outputs shouldn't match.
  self.assertFalse(torch.allclose(torch.from_numpy(__a				) , torch.from_numpy(__a				)				)				)






import random
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
 import torch

if is_vision_available():
 import PIL

 from transformers import FlavaImageProcessor
 from transformers.image_utils import PILImageResampling
 from transformers.models.flava.image_processing_flava import (
     FLAVA_CODEBOOK_MEAN,
     FLAVA_CODEBOOK_STD,
     FLAVA_IMAGE_MEAN,
     FLAVA_IMAGE_STD,
 )
else:
 _UpperCamelCase     =    _UpperCamelCase     =    _UpperCamelCase     =    _UpperCamelCase     =    None



class 					lowercase		( unittest.TestCase      ):



 '''simple docstring'''


 def __init__(self , __a , __a=7 , __a=3 , __a=30 , __a=400 , __a=True , __a=None , __a=True , __a=None , __a=None , __a=True , __a=1 / 255 , __a=True , __a=FLAVA_IMAGE_MEAN , __a=FLAVA_IMAGE_STD , __a=14 , __a=75 , __a=None , __a=16 , __a=0.3 , __a=None , __a=True , __a=None , __a=None , __a=True , __a=None , __a=True , __a=True , __a=FLAVA_CODEBOOK_MEAN , __a=FLAVA_CODEBOOK_STD , )				->      Optional[int]:






  """simple docstring"""





  UpperCAmelCase__           =       size if size is not None else {'height': 224, 'width': 224}
  UpperCAmelCase__           =       crop_size if crop_size is not None else {'height': 224, 'width': 224}
  UpperCAmelCase__           =       codebook_size if codebook_size is not None else {'height': 112, 'width': 112}
  UpperCAmelCase__           =       codebook_crop_size if codebook_crop_size is not None else {'height': 112, 'width': 112}

  UpperCAmelCase__           =       parent
  UpperCAmelCase__           =       batch_size
  UpperCAmelCase__           =       num_channels
  UpperCAmelCase__           =       do_resize
  UpperCAmelCase__           =       do_rescale
  UpperCAmelCase__           =       rescale_factor
  UpperCAmelCase__           =       min_resolution
  UpperCAmelCase__           =       max_resolution
  UpperCAmelCase__           =       size
  UpperCAmelCase__           =       resample if resample is not None else PILImageResampling.BICUBIC
  UpperCAmelCase__           =       do_normalize
  UpperCAmelCase__           =       image_mean
  UpperCAmelCase__           =       image_std
  UpperCAmelCase__           =       do_center_crop
  UpperCAmelCase__           =       crop_size

  UpperCAmelCase__           =       input_size_patches
  UpperCAmelCase__           =       total_mask_patches
  UpperCAmelCase__           =       mask_group_max_patches
  UpperCAmelCase__           =       mask_group_min_patches
  UpperCAmelCase__           =       mask_group_min_aspect_ratio
  UpperCAmelCase__           =       mask_group_max_aspect_ratio

  UpperCAmelCase__           =       codebook_do_resize
  UpperCAmelCase__           =       codebook_size
  UpperCAmelCase__           =       codebook_resample if codebook_resample is not None else PILImageResampling.LANCZOS
  UpperCAmelCase__           =       codebook_do_center_crop
  UpperCAmelCase__           =       codebook_crop_size
  UpperCAmelCase__           =       codebook_do_map_pixels
  UpperCAmelCase__           =       codebook_do_normalize
  UpperCAmelCase__           =       codebook_image_mean
  UpperCAmelCase__           =       codebook_image_std




 def 						UpperCamelCase__							(self				)				->      Any:






  """simple docstring"""





  return {
      "image_mean": self.image_mean,
      "image_std": self.image_std,
      "do_normalize": self.do_normalize,
      "do_resize": self.do_resize,
      "size": self.size,
      "resample": self.resample,
      "do_rescale": self.do_rescale,
      "rescale_factor": self.rescale_factor,
      "do_center_crop": self.do_center_crop,
      "crop_size": self.crop_size,
      "input_size_patches": self.input_size_patches,
      "total_mask_patches": self.total_mask_patches,
      "mask_group_max_patches": self.mask_group_max_patches,
      "mask_group_min_patches": self.mask_group_min_patches,
      "mask_group_min_aspect_ratio": self.mask_group_min_aspect_ratio,
      "mask_group_max_aspect_ratio": self.mask_group_min_aspect_ratio,
      "codebook_do_resize": self.codebook_do_resize,
      "codebook_size": self.codebook_size,
      "codebook_resample": self.codebook_resample,
      "codebook_do_center_crop": self.codebook_do_center_crop,
      "codebook_crop_size": self.codebook_crop_size,
      "codebook_do_map_pixels": self.codebook_do_map_pixels,
      "codebook_do_normalize": self.codebook_do_normalize,
      "codebook_image_mean": self.codebook_image_mean,
      "codebook_image_std": self.codebook_image_std,
  }




 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  return (self.size["height"], self.size["width"])




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






  """simple docstring"""





  return (
      (self.input_size_patches, self.input_size_patches)
      if not isinstance(self.input_size_patches , __a				)
      else self.input_size_patches
  )




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






  """simple docstring"""





  return (self.codebook_size["height"], self.codebook_size["width"])







@require_torch
@require_vision
class 					lowercase		( _UpperCamelCase				,       unittest.TestCase      ):



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE      							=       FlavaImageProcessor if is_vision_available() else None
 __SCREAMING_SNAKE_CASE      							=       None




 def 						UpperCamelCase__							(self				)				->      Tuple:






  """simple docstring"""





  UpperCAmelCase__           =       FlavaImageProcessingTester(self				)




 @property
 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  return self.image_processor_tester.prepare_image_processor_dict()




 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  self.assertTrue(hasattr(__a , 'image_mean'				)				)
  self.assertTrue(hasattr(__a , 'image_std'				)				)
  self.assertTrue(hasattr(__a , 'do_normalize'				)				)
  self.assertTrue(hasattr(__a , 'do_resize'				)				)
  self.assertTrue(hasattr(__a , 'resample'				)				)
  self.assertTrue(hasattr(__a , 'crop_size'				)				)
  self.assertTrue(hasattr(__a , 'do_center_crop'				)				)
  self.assertTrue(hasattr(__a , 'do_rescale'				)				)
  self.assertTrue(hasattr(__a , 'rescale_factor'				)				)
  self.assertTrue(hasattr(__a , 'masking_generator'				)				)
  self.assertTrue(hasattr(__a , 'codebook_do_resize'				)				)
  self.assertTrue(hasattr(__a , 'codebook_size'				)				)
  self.assertTrue(hasattr(__a , 'codebook_resample'				)				)
  self.assertTrue(hasattr(__a , 'codebook_do_center_crop'				)				)
  self.assertTrue(hasattr(__a , 'codebook_crop_size'				)				)
  self.assertTrue(hasattr(__a , 'codebook_do_map_pixels'				)				)
  self.assertTrue(hasattr(__a , 'codebook_do_normalize'				)				)
  self.assertTrue(hasattr(__a , 'codebook_image_mean'				)				)
  self.assertTrue(hasattr(__a , 'codebook_image_std'				)				)




 def 						UpperCamelCase__							(self				)				->      Tuple:






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class.from_dict(self.image_processor_dict				)
  self.assertEqual(image_processor.size , {'height': 224, 'width': 224}				)
  self.assertEqual(image_processor.crop_size , {'height': 224, 'width': 224}				)
  self.assertEqual(image_processor.codebook_size , {'height': 112, 'width': 112}				)
  self.assertEqual(image_processor.codebook_crop_size , {'height': 112, 'width': 112}				)

  UpperCAmelCase__           =       self.image_processing_class.from_dict(
      self.image_processor_dict , size=42 , crop_size=84 , codebook_size=33 , codebook_crop_size=66				)
  self.assertEqual(image_processor.size , {'height': 42, 'width': 42}				)
  self.assertEqual(image_processor.crop_size , {'height': 84, 'width': 84}				)
  self.assertEqual(image_processor.codebook_size , {'height': 33, 'width': 33}				)
  self.assertEqual(image_processor.codebook_crop_size , {'height': 66, 'width': 66}				)




 def 						UpperCamelCase__							(self				)				->      Any:






  """simple docstring"""





  pass




 def 						UpperCamelCase__							(self				)				->      List[Any]:






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  # create random PIL images
  UpperCAmelCase__           =       prepare_image_inputs(self.image_processor_tester , equal_resolution=__a				)
  for image in image_inputs:
   self.assertIsInstance(__a , PIL.Image.Image				)

  # Test not batched input
  UpperCAmelCase__           =       image_processing(image_inputs[0] , return_tensors='pt'				)

  # Test no bool masked pos
  self.assertFalse('bool_masked_pos' in encoded_images				)

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()

  self.assertEqual(
      encoded_images.pixel_values.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , )

  # Test batched
  UpperCAmelCase__           =       image_processing(__a , return_tensors='pt'				)
  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()

  # Test no bool masked pos
  self.assertFalse('bool_masked_pos' in encoded_images				)

  self.assertEqual(
      encoded_images.pixel_values.shape , (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          expected_height,
          expected_width,
      ) , )




 def 						UpperCamelCase__							(self , __a , __a				)				->      Any:






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  # create random tensors
  UpperCAmelCase__           =       prepare_image_inputs(self.image_processor_tester , equal_resolution=__a , **__a				)
  for image in image_inputs:
   self.assertIsInstance(__a , __a				)

  # Test not batched input
  UpperCAmelCase__           =       image_processing(image_inputs[0] , return_tensors='pt'				)

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()
  self.assertEqual(
      encoded_images.pixel_values.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , )

  UpperCAmelCase__           =       image_processing(__a , return_image_mask=__a , return_tensors='pt'				)

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()
  self.assertEqual(
      encoded_images.pixel_values.shape , (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          expected_height,
          expected_width,
      ) , )

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_mask_size()
  self.assertEqual(
      encoded_images.bool_masked_pos.shape , (
          self.image_processor_tester.batch_size,
          expected_height,
          expected_width,
      ) , )

  # Test batched
  UpperCAmelCase__           =       image_processing(__a , return_tensors='pt'				).pixel_values

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()
  self.assertEqual(
      encoded_images.shape , (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          expected_height,
          expected_width,
      ) , )

  # Test masking
  UpperCAmelCase__           =       image_processing(__a , return_image_mask=__a , return_tensors='pt'				)

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_image_size()
  self.assertEqual(
      encoded_images.pixel_values.shape , (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          expected_height,
          expected_width,
      ) , )

  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_mask_size()
  self.assertEqual(
      encoded_images.bool_masked_pos.shape , (
          self.image_processor_tester.batch_size,
          expected_height,
          expected_width,
      ) , )




 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  self._test_call_framework(np.ndarray , prepare_kwargs={'numpify': True}				)




 def 						UpperCamelCase__							(self				)				->      int:






  """simple docstring"""





  self._test_call_framework(torch.Tensor , prepare_kwargs={'torchify': True}				)




 def 						UpperCamelCase__							(self				)				->      Dict:






  """simple docstring"""





  random.seed(1234				)
  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  UpperCAmelCase__           =       prepare_image_inputs(self.image_processor_tester , equal_resolution=__a , torchify=__a				)

  # Test not batched input
  UpperCAmelCase__           =       image_processing(image_inputs[0] , return_image_mask=__a , return_tensors='pt'				)
  self.assertEqual(encoded_images.bool_masked_pos.sum().item() , 75				)




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






  """simple docstring"""





  UpperCAmelCase__           =       self.image_processing_class(**self.image_processor_dict				)
  # create random PIL images
  UpperCAmelCase__           =       prepare_image_inputs(self.image_processor_tester , equal_resolution=__a				)
  for image in image_inputs:
   self.assertIsInstance(__a , PIL.Image.Image				)

  # Test not batched input
  UpperCAmelCase__           =       image_processing(image_inputs[0] , return_codebook_pixels=__a , return_tensors='pt'				)
  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_codebook_image_size()
  self.assertEqual(
      encoded_images.codebook_pixel_values.shape , (1, self.image_processor_tester.num_channels, expected_height, expected_width) , )

  # Test batched
  UpperCAmelCase__           =       image_processing(__a , return_codebook_pixels=__a , return_tensors='pt'				)
  UpperCAmelCase__       ,					UpperCAmelCase__           =       self.image_processor_tester.get_expected_codebook_image_size()
  self.assertEqual(
      encoded_images.codebook_pixel_values.shape , (
          self.image_processor_tester.batch_size,
          self.image_processor_tester.num_channels,
          expected_height,
          expected_width,
      ) , )



