






'''simple docstring'''



import gc
import random
import unittest

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

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableDiffusionUpscalePipeline, UNetaDConditionModel
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


enable_full_determinism()
class UpperCAmelCase_					(    unittest.TestCase				):




      """simple docstring"""




      def 				A__      ( self			:		Optional[int]		)  ->  Dict:


       '''simple docstring'''

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






      @property
      def 				A__      ( self			:		Dict		)  ->  Any:


       '''simple docstring'''

       lowercase					:   str				  =1
       lowercase					:   Any				  =3
       lowercase					:   Dict				  =(32, 32)

       lowercase					:   str				  =floats_tensor((batch_size, num_channels) + sizes     ,      rng=random.Random(0		)		).to(UpperCAmelCase		)
       return image






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


       '''simple docstring'''

       torch.manual_seed(0		)
       lowercase					:   Any				  =UNetaDConditionModel(
           block_out_channels=(32, 32, 64)     ,      layers_per_block=2     ,      sample_size=32     ,      in_channels=7     ,      out_channels=4     ,      down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''', '''CrossAttnDownBlock2D''')     ,      up_block_types=('''CrossAttnUpBlock2D''', '''CrossAttnUpBlock2D''', '''UpBlock2D''')     ,      cross_attention_dim=32     ,      attention_head_dim=8     ,      use_linear_projection=UpperCAmelCase     ,      only_cross_attention=(True, True, False)     ,      num_class_embeds=100     ,      )
       return model






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


       '''simple docstring'''

       torch.manual_seed(0		)
       lowercase					:   Dict				  =AutoencoderKL(
           block_out_channels=[32, 32, 64]     ,      in_channels=3     ,      out_channels=3     ,      down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''', '''DownEncoderBlock2D''']     ,      up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''', '''UpDecoderBlock2D''']     ,      latent_channels=4     ,      )
       return model






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


       '''simple docstring'''

       torch.manual_seed(0		)
       lowercase					:   List[Any]				  =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     ,      )
       return CLIPTextModel(UpperCAmelCase		)






      def 				A__      ( self			:		Tuple		)  ->  Optional[int]:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       lowercase					:   List[str]				  =self.dummy_cond_unet_upscale
       lowercase					:   Optional[int]				  =DDPMScheduler()
       lowercase					:   Tuple				  =DDIMScheduler(prediction_type='''v_prediction'''		)
       lowercase					:   Optional[int]				  =self.dummy_vae
       lowercase					:   Any				  =self.dummy_text_encoder
       lowercase					:   Tuple				  =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''		)

       lowercase					:   Any				  =self.dummy_image.cpu().permute(0     ,      2     ,      3     ,      1		)[0]
       lowercase					:   int				  =Image.fromarray(np.uinta(UpperCAmelCase		)		).convert('''RGB'''		).resize((64, 64)		)

       # make sure here that pndm scheduler skips prk
       lowercase					:   List[str]				  =StableDiffusionUpscalePipeline(
           unet=UpperCAmelCase     ,      low_res_scheduler=UpperCAmelCase     ,      scheduler=UpperCAmelCase     ,      vae=UpperCAmelCase     ,      text_encoder=UpperCAmelCase     ,      tokenizer=UpperCAmelCase     ,      max_noise_level=350     ,      )
       lowercase					:   List[Any]				  =sd_pipe.to(UpperCAmelCase		)
       sd_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase					:   Optional[Any]				  ='''A painting of a squirrel eating a burger'''
       lowercase					:   str				  =torch.Generator(device=UpperCAmelCase		).manual_seed(0		)
       lowercase					:   Dict				  =sd_pipe(
           [prompt]     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      guidance_scale=6.0     ,      noise_level=20     ,      num_inference_steps=2     ,      output_type='''np'''     ,      )

       lowercase					:   int				  =output.images

       lowercase					:   Union[str, Any]				  =torch.Generator(device=UpperCAmelCase		).manual_seed(0		)
       lowercase					:   Union[str, Any]				  =sd_pipe(
           [prompt]     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      guidance_scale=6.0     ,      noise_level=20     ,      num_inference_steps=2     ,      output_type='''np'''     ,      return_dict=UpperCAmelCase     ,      )[0]

       lowercase					:   int				  =image[0, -3:, -3:, -1]
       lowercase					:   Optional[int]				  =image_from_tuple[0, -3:, -3:, -1]

       lowercase					:   Union[str, Any]				  =low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)
       lowercase					:   Optional[int]				  =np.array([0.3_1_1_3, 0.3_9_1_0, 0.4_2_7_2, 0.4_8_5_9, 0.5_0_6_1, 0.4_6_5_2, 0.5_3_6_2, 0.5_7_1_5, 0.5_6_6_1]		)

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






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


       '''simple docstring'''

       lowercase					:   List[Any]				  ='''cpu'''  # ensure determinism for the device-dependent torch.Generator
       lowercase					:   List[str]				  =self.dummy_cond_unet_upscale
       lowercase					:   Optional[Any]				  =DDPMScheduler()
       lowercase					:   int				  =DDIMScheduler(prediction_type='''v_prediction'''		)
       lowercase					:   int				  =self.dummy_vae
       lowercase					:   Optional[int]				  =self.dummy_text_encoder
       lowercase					:   List[Any]				  =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''		)

       lowercase					:   str				  =self.dummy_image.cpu().permute(0     ,      2     ,      3     ,      1		)[0]
       lowercase					:   Dict				  =Image.fromarray(np.uinta(UpperCAmelCase		)		).convert('''RGB'''		).resize((64, 64)		)

       # make sure here that pndm scheduler skips prk
       lowercase					:   Union[str, Any]				  =StableDiffusionUpscalePipeline(
           unet=UpperCAmelCase     ,      low_res_scheduler=UpperCAmelCase     ,      scheduler=UpperCAmelCase     ,      vae=UpperCAmelCase     ,      text_encoder=UpperCAmelCase     ,      tokenizer=UpperCAmelCase     ,      max_noise_level=350     ,      )
       lowercase					:   str				  =sd_pipe.to(UpperCAmelCase		)
       sd_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase					:   Optional[Any]				  ='''A painting of a squirrel eating a burger'''
       lowercase					:   List[str]				  =sd_pipe(
           2 * [prompt]     ,      image=2 * [low_res_image]     ,      guidance_scale=6.0     ,      noise_level=20     ,      num_inference_steps=2     ,      output_type='''np'''     ,      )
       lowercase					:   Optional[Any]				  =output.images
       assert image.shape[0] == 2

       lowercase					:   List[str]				  =torch.Generator(device=UpperCAmelCase		).manual_seed(0		)
       lowercase					:   Tuple				  =sd_pipe(
           [prompt]     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      num_images_per_prompt=2     ,      guidance_scale=6.0     ,      noise_level=20     ,      num_inference_steps=2     ,      output_type='''np'''     ,      )
       lowercase					:   List[Any]				  =output.images
       assert image.shape[0] == 2






      @unittest.skipIf(torch_device != '''cuda'''     ,      '''This test requires a GPU'''		)
      def 				A__      ( self			:		Any		)  ->  Dict:


       '''simple docstring'''

       lowercase					:   List[Any]				  =self.dummy_cond_unet_upscale
       lowercase					:   List[str]				  =DDPMScheduler()
       lowercase					:   Union[str, Any]				  =DDIMScheduler(prediction_type='''v_prediction'''		)
       lowercase					:   List[str]				  =self.dummy_vae
       lowercase					:   Optional[Any]				  =self.dummy_text_encoder
       lowercase					:   Any				  =CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''		)

       lowercase					:   List[Any]				  =self.dummy_image.cpu().permute(0     ,      2     ,      3     ,      1		)[0]
       lowercase					:   Optional[int]				  =Image.fromarray(np.uinta(UpperCAmelCase		)		).convert('''RGB'''		).resize((64, 64)		)

       # put models in fp16, except vae as it overflows in fp16
       lowercase					:   Union[str, Any]				  =unet.half()
       lowercase					:   Union[str, Any]				  =text_encoder.half()

       # make sure here that pndm scheduler skips prk
       lowercase					:   Dict				  =StableDiffusionUpscalePipeline(
           unet=UpperCAmelCase     ,      low_res_scheduler=UpperCAmelCase     ,      scheduler=UpperCAmelCase     ,      vae=UpperCAmelCase     ,      text_encoder=UpperCAmelCase     ,      tokenizer=UpperCAmelCase     ,      max_noise_level=350     ,      )
       lowercase					:   int				  =sd_pipe.to(UpperCAmelCase		)
       sd_pipe.set_progress_bar_config(disable=UpperCAmelCase		)

       lowercase					:   List[Any]				  ='''A painting of a squirrel eating a burger'''
       lowercase					:   List[str]				  =torch.manual_seed(0		)
       lowercase					:   Union[str, Any]				  =sd_pipe(
           [prompt]     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      num_inference_steps=2     ,      output_type='''np'''     ,      ).images

       lowercase					:   List[str]				  =low_res_image.size[0] * 4
       assert image.shape == (1, expected_height_width, expected_height_width, 3)





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




      """simple docstring"""




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


       '''simple docstring'''

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






      def 				A__      ( self			:		Any		)  ->  int:


       '''simple docstring'''

       lowercase					:   Optional[Any]				  =load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/sd2-upscale/low_res_cat.png'''		)
       lowercase					:   List[str]				  =load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
           '''/upsampled_cat.npy'''		)

       lowercase					:   List[str]				  ='''stabilityai/stable-diffusion-x4-upscaler'''
       lowercase					:   Tuple				  =StableDiffusionUpscalePipeline.from_pretrained(UpperCAmelCase		)
       pipe.to(UpperCAmelCase		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase		)
       pipe.enable_attention_slicing()

       lowercase					:   List[str]				  ='''a cat sitting on a park bench'''

       lowercase					:   Any				  =torch.manual_seed(0		)
       lowercase					:   Dict				  =pipe(
           prompt=UpperCAmelCase     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      output_type='''np'''     ,      )
       lowercase					:   List[Any]				  =output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image		).max() < 1e-3






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


       '''simple docstring'''

       lowercase					:   Union[str, Any]				  =load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/sd2-upscale/low_res_cat.png'''		)
       lowercase					:   Any				  =load_numpy(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
           '''/upsampled_cat_fp16.npy'''		)

       lowercase					:   Optional[Any]				  ='''stabilityai/stable-diffusion-x4-upscaler'''
       lowercase					:   Union[str, Any]				  =StableDiffusionUpscalePipeline.from_pretrained(
           UpperCAmelCase     ,      torch_dtype=torch.floataa     ,      )
       pipe.to(UpperCAmelCase		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase		)
       pipe.enable_attention_slicing()

       lowercase					:   Union[str, Any]				  ='''a cat sitting on a park bench'''

       lowercase					:   Any				  =torch.manual_seed(0		)
       lowercase					:   Optional[Any]				  =pipe(
           prompt=UpperCAmelCase     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      output_type='''np'''     ,      )
       lowercase					:   Any				  =output.images[0]

       assert image.shape == (512, 512, 3)
       assert np.abs(expected_image - image		).max() < 5e-1






      def 				A__      ( self			:		Tuple		)  ->  Tuple:


       '''simple docstring'''

       torch.cuda.empty_cache()
       torch.cuda.reset_max_memory_allocated()
       torch.cuda.reset_peak_memory_stats()

       lowercase					:   List[Any]				  =load_image(
           '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
           '''/sd2-upscale/low_res_cat.png'''		)

       lowercase					:   Optional[int]				  ='''stabilityai/stable-diffusion-x4-upscaler'''
       lowercase					:   Union[str, Any]				  =StableDiffusionUpscalePipeline.from_pretrained(
           UpperCAmelCase     ,      torch_dtype=torch.floataa     ,      )
       pipe.to(UpperCAmelCase		)
       pipe.set_progress_bar_config(disable=UpperCAmelCase		)
       pipe.enable_attention_slicing(1		)
       pipe.enable_sequential_cpu_offload()

       lowercase					:   Any				  ='''a cat sitting on a park bench'''

       lowercase					:   Union[str, Any]				  =torch.manual_seed(0		)
       lowercase					:   Tuple				  =pipe(
           prompt=UpperCAmelCase     ,      image=UpperCAmelCase     ,      generator=UpperCAmelCase     ,      num_inference_steps=5     ,      output_type='''np'''     ,      )

       lowercase					:   Any				  =torch.cuda.max_memory_allocated()
       # make sure that less than 2.9 GB is allocated
       assert mem_bytes < 2.9 * 10**9











'''simple docstring'''



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 UpperCAmelCase_					(    __A				):




      """simple docstring"""




      UpperCamelCase_								=			['''image_processor''', '''tokenizer''']
      UpperCamelCase_								=			'''LayoutLMv2ImageProcessor'''
      UpperCamelCase_								=			('''LayoutXLMTokenizer''', '''LayoutXLMTokenizerFast''')






      def __init__( self			:		List[str]     ,      UpperCAmelCase			:		Tuple=None     ,      UpperCAmelCase			:		str=None     ,      **UpperCAmelCase			:		Dict		)  ->  Optional[int]:


       '''simple docstring'''

       if "feature_extractor" in kwargs:
        warnings.warn(
            '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
            ''' instead.'''     ,      UpperCAmelCase     ,      )
        lowercase					:   Any				  =kwargs.pop('''feature_extractor'''		)

       lowercase					:   Dict				  =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__(UpperCAmelCase     ,      UpperCAmelCase		)






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


       '''simple docstring'''

       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
       lowercase					:   Tuple				  =self.image_processor(images=UpperCAmelCase     ,      return_tensors=UpperCAmelCase		)

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

       lowercase					:   Optional[Any]				  =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=UpperCAmelCase     ,      add_special_tokens=UpperCAmelCase     ,      padding=UpperCAmelCase     ,      truncation=UpperCAmelCase     ,      max_length=UpperCAmelCase     ,      stride=UpperCAmelCase     ,      pad_to_multiple_of=UpperCAmelCase     ,      return_token_type_ids=UpperCAmelCase     ,      return_attention_mask=UpperCAmelCase     ,      return_overflowing_tokens=UpperCAmelCase     ,      return_special_tokens_mask=UpperCAmelCase     ,      return_offsets_mapping=UpperCAmelCase     ,      return_length=UpperCAmelCase     ,      verbose=UpperCAmelCase     ,      return_tensors=UpperCAmelCase     ,      **UpperCAmelCase     ,      )

       # add pixel values
       lowercase					:   List[str]				  =features.pop('''pixel_values'''		)
       if return_overflowing_tokens is True:
        lowercase					:   str				  =self.get_overflowing_images(UpperCAmelCase     ,      encoded_inputs['''overflow_to_sample_mapping''']		)
       lowercase					:   Dict				  =images

       return encoded_inputs






      def 				A__      ( self			:		Any     ,      UpperCAmelCase			:		Optional[Any]     ,      UpperCAmelCase			:		str		)  ->  str:


       '''simple docstring'''

       lowercase					:   str				  =[]
       for sample_idx in overflow_to_sample_mapping:
        images_with_overflow.append(images[sample_idx]		)

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

       return images_with_overflow






      def 				A__      ( self			:		Union[str, Any]     ,      *UpperCAmelCase			:		Tuple     ,      **UpperCAmelCase			:		Dict		)  ->  int:


       '''simple docstring'''

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






      def 				A__      ( self			:		Dict     ,      *UpperCAmelCase			:		Any     ,      **UpperCAmelCase			:		Dict		)  ->  str:


       '''simple docstring'''

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






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


       '''simple docstring'''

       return ["input_ids", "bbox", "attention_mask", "image"]






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


       '''simple docstring'''

       warnings.warn(
           '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''     ,      UpperCAmelCase     ,      )
       return self.image_processor_class






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


       '''simple docstring'''

       warnings.warn(
           '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''     ,      UpperCAmelCase     ,      )
       return self.image_processor



