




'''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       A   (		unittest.TestCase   ):







							def        __lowerCAmelCase      (  self	:  List[str] )      ->							Optional[Any]:


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a     					=    1
														_a     					=    3
														_a     					=    (32, 32)

														_a     					=    floats_tensor((batch_size, num_channels) + sizes							,      rng=random.Random(0 ) ).to(lowerCAmelCase_ )
														return image







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


														"""simple docstring"""


														torch.manual_seed(0 )
														_a     					=    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=lowerCAmelCase_							,      only_cross_attention=(True, True, False)							,      num_class_embeds=1_00							,      )
														return model







							@property
							def        __lowerCAmelCase      (  self	:  Dict )      ->							int:


														"""simple docstring"""


														torch.manual_seed(0 )
														_a     					=    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        __lowerCAmelCase      (  self	:  Dict )      ->							Tuple:


														"""simple docstring"""


														torch.manual_seed(0 )
														_a     					=    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=10_00							,      hidden_act='''gelu'''							,      projection_dim=5_12							,      )
														return CLIPTextModel(lowerCAmelCase_ )







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


														"""simple docstring"""


														_a     					=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
														_a     					=    self.dummy_cond_unet_upscale
														_a     					=    DDPMScheduler()
														_a     					=    DDIMScheduler(prediction_type='''v_prediction''' )
														_a     					=    self.dummy_vae
														_a     					=    self.dummy_text_encoder
														_a     					=    CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

														_a     					=    self.dummy_image.cpu().permute(0							,      2							,      3							,      1 )[0]
														_a     					=    Image.fromarray(np.uinta(lowerCAmelCase_ ) ).convert('''RGB''' ).resize((64, 64) )

														# make sure here that pndm scheduler skips prk
														_a     					=    StableDiffusionUpscalePipeline(
														    unet=lowerCAmelCase_							,      low_res_scheduler=lowerCAmelCase_							,      scheduler=lowerCAmelCase_							,      vae=lowerCAmelCase_							,      text_encoder=lowerCAmelCase_							,      tokenizer=lowerCAmelCase_							,      max_noise_level=3_50							,      )
														_a     					=    sd_pipe.to(lowerCAmelCase_ )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

														_a     					=    '''A painting of a squirrel eating a burger'''
														_a     					=    torch.Generator(device=lowerCAmelCase_ ).manual_seed(0 )
														_a     					=    sd_pipe(
														    [prompt]							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      guidance_scale=6.0							,      noise_level=20							,      num_inference_steps=2							,      output_type='''np'''							,      )

														_a     					=    output.images

														_a     					=    torch.Generator(device=lowerCAmelCase_ ).manual_seed(0 )
														_a     					=    sd_pipe(
														    [prompt]							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      guidance_scale=6.0							,      noise_level=20							,      num_inference_steps=2							,      output_type='''np'''							,      return_dict=lowerCAmelCase_							,      )[0]

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

														_a     					=    low_res_image.size[0] * 4
														assert image.shape == (1, expected_height_width, expected_height_width, 3)
														_a     					=    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        __lowerCAmelCase      (  self	:  Dict )      ->							List[Any]:


														"""simple docstring"""


														_a     					=    '''cpu'''  # ensure determinism for the device-dependent torch.Generator
														_a     					=    self.dummy_cond_unet_upscale
														_a     					=    DDPMScheduler()
														_a     					=    DDIMScheduler(prediction_type='''v_prediction''' )
														_a     					=    self.dummy_vae
														_a     					=    self.dummy_text_encoder
														_a     					=    CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

														_a     					=    self.dummy_image.cpu().permute(0							,      2							,      3							,      1 )[0]
														_a     					=    Image.fromarray(np.uinta(lowerCAmelCase_ ) ).convert('''RGB''' ).resize((64, 64) )

														# make sure here that pndm scheduler skips prk
														_a     					=    StableDiffusionUpscalePipeline(
														    unet=lowerCAmelCase_							,      low_res_scheduler=lowerCAmelCase_							,      scheduler=lowerCAmelCase_							,      vae=lowerCAmelCase_							,      text_encoder=lowerCAmelCase_							,      tokenizer=lowerCAmelCase_							,      max_noise_level=3_50							,      )
														_a     					=    sd_pipe.to(lowerCAmelCase_ )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

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

														_a     					=    torch.Generator(device=lowerCAmelCase_ ).manual_seed(0 )
														_a     					=    sd_pipe(
														    [prompt]							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      num_images_per_prompt=2							,      guidance_scale=6.0							,      noise_level=20							,      num_inference_steps=2							,      output_type='''np'''							,      )
														_a     					=    output.images
														assert image.shape[0] == 2







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


														"""simple docstring"""


														_a     					=    self.dummy_cond_unet_upscale
														_a     					=    DDPMScheduler()
														_a     					=    DDIMScheduler(prediction_type='''v_prediction''' )
														_a     					=    self.dummy_vae
														_a     					=    self.dummy_text_encoder
														_a     					=    CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''' )

														_a     					=    self.dummy_image.cpu().permute(0							,      2							,      3							,      1 )[0]
														_a     					=    Image.fromarray(np.uinta(lowerCAmelCase_ ) ).convert('''RGB''' ).resize((64, 64) )

														# put models in fp16, except vae as it overflows in fp16
														_a     					=    unet.half()
														_a     					=    text_encoder.half()

														# make sure here that pndm scheduler skips prk
														_a     					=    StableDiffusionUpscalePipeline(
														    unet=lowerCAmelCase_							,      low_res_scheduler=lowerCAmelCase_							,      scheduler=lowerCAmelCase_							,      vae=lowerCAmelCase_							,      text_encoder=lowerCAmelCase_							,      tokenizer=lowerCAmelCase_							,      max_noise_level=3_50							,      )
														_a     					=    sd_pipe.to(lowerCAmelCase_ )
														sd_pipe.set_progress_bar_config(disable=lowerCAmelCase_ )

														_a     					=    '''A painting of a squirrel eating a burger'''
														_a     					=    torch.manual_seed(0 )
														_a     					=    sd_pipe(
														    [prompt]							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      num_inference_steps=2							,      output_type='''np'''							,      ).images

														_a     					=    low_res_image.size[0] * 4
														assert image.shape == (1, expected_height_width, expected_height_width, 3)



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







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


														"""simple docstring"""


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







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


														"""simple docstring"""


														_a     					=    load_image(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
														    '''/sd2-upscale/low_res_cat.png''' )
														_a     					=    load_numpy(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
														    '''/upsampled_cat.npy''' )

														_a     					=    '''stabilityai/stable-diffusion-x4-upscaler'''
														_a     					=    StableDiffusionUpscalePipeline.from_pretrained(lowerCAmelCase_ )
														pipe.to(lowerCAmelCase_ )
														pipe.set_progress_bar_config(disable=lowerCAmelCase_ )
														pipe.enable_attention_slicing()

														_a     					=    '''a cat sitting on a park bench'''

														_a     					=    torch.manual_seed(0 )
														_a     					=    pipe(
														    prompt=lowerCAmelCase_							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      output_type='''np'''							,      )
														_a     					=    output.images[0]

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







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


														"""simple docstring"""


														_a     					=    load_image(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'''
														    '''/sd2-upscale/low_res_cat.png''' )
														_a     					=    load_numpy(
														    '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale'''
														    '''/upsampled_cat_fp16.npy''' )

														_a     					=    '''stabilityai/stable-diffusion-x4-upscaler'''
														_a     					=    StableDiffusionUpscalePipeline.from_pretrained(
														    lowerCAmelCase_							,      torch_dtype=torch.floataa							,      )
														pipe.to(lowerCAmelCase_ )
														pipe.set_progress_bar_config(disable=lowerCAmelCase_ )
														pipe.enable_attention_slicing()

														_a     					=    '''a cat sitting on a park bench'''

														_a     					=    torch.manual_seed(0 )
														_a     					=    pipe(
														    prompt=lowerCAmelCase_							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      output_type='''np'''							,      )
														_a     					=    output.images[0]

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







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


														"""simple docstring"""


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

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

														_a     					=    '''stabilityai/stable-diffusion-x4-upscaler'''
														_a     					=    StableDiffusionUpscalePipeline.from_pretrained(
														    lowerCAmelCase_							,      torch_dtype=torch.floataa							,      )
														pipe.to(lowerCAmelCase_ )
														pipe.set_progress_bar_config(disable=lowerCAmelCase_ )
														pipe.enable_attention_slicing(1 )
														pipe.enable_sequential_cpu_offload()

														_a     					=    '''a cat sitting on a park bench'''

														_a     					=    torch.manual_seed(0 )
														_a     					=    pipe(
														    prompt=lowerCAmelCase_							,      image=lowerCAmelCase_							,      generator=lowerCAmelCase_							,      num_inference_steps=5							,      output_type='''np'''							,      )

														_a     					=    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 unittest

from transformers import TrOCRConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers.models.trocr.modeling_trocr import TrOCRDecoder, TrOCRForCausalLM

@require_torch
class       A   :







							def __init__(  self	:  Union[str, Any]							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Union[str, Any]=99							,      lowerCAmelCase_	:  Optional[int]=13							,      lowerCAmelCase_	:  Tuple=16							,      lowerCAmelCase_	:  Optional[int]=7							,      lowerCAmelCase_	:  Optional[Any]=True							,      lowerCAmelCase_	:  Union[str, Any]=True							,      lowerCAmelCase_	:  Any=True							,      lowerCAmelCase_	:  Union[str, Any]=False							,      lowerCAmelCase_	:  int=True							,      lowerCAmelCase_	:  Tuple=2							,      lowerCAmelCase_	:  Any=32							,      lowerCAmelCase_	:  str=4							,      lowerCAmelCase_	:  Optional[Any]=4							,      lowerCAmelCase_	:  Optional[Any]=30							,      lowerCAmelCase_	:  int=0							,      lowerCAmelCase_	:  Any=1							,      lowerCAmelCase_	:  str=2							,      lowerCAmelCase_	:  Union[str, Any]=None							,      )      ->							Any:


														"""simple docstring"""


														_a     					=    parent
														_a     					=    batch_size
														_a     					=    decoder_seq_length
														# For common tests
														_a     					=    self.decoder_seq_length
														_a     					=    is_training
														_a     					=    use_attention_mask
														_a     					=    use_labels

														_a     					=    vocab_size
														_a     					=    d_model
														_a     					=    d_model
														_a     					=    decoder_layers
														_a     					=    decoder_layers
														_a     					=    decoder_ffn_dim
														_a     					=    decoder_attention_heads
														_a     					=    decoder_attention_heads
														_a     					=    eos_token_id
														_a     					=    bos_token_id
														_a     					=    pad_token_id
														_a     					=    decoder_start_token_id
														_a     					=    use_cache
														_a     					=    max_position_embeddings

														_a     					=    None
														_a     					=    decoder_seq_length
														_a     					=    2
														_a     					=    1







							def        __lowerCAmelCase      (  self	:  str )      ->							str:


														"""simple docstring"""


														_a     					=    ids_tensor([self.batch_size, self.decoder_seq_length]							,      self.vocab_size )

														_a     					=    None
														if self.use_attention_mask:
																					_a     					=    ids_tensor([self.batch_size, self.decoder_seq_length]							,      vocab_size=2 )

														_a     					=    None
														if self.use_labels:
																					_a     					=    ids_tensor([self.batch_size, self.decoder_seq_length]							,      self.vocab_size )

														_a     					=    TrOCRConfig(
														    vocab_size=self.vocab_size							,      d_model=self.d_model							,      decoder_layers=self.decoder_layers							,      decoder_ffn_dim=self.decoder_ffn_dim							,      decoder_attention_heads=self.decoder_attention_heads							,      eos_token_id=self.eos_token_id							,      bos_token_id=self.bos_token_id							,      use_cache=self.use_cache							,      pad_token_id=self.pad_token_id							,      decoder_start_token_id=self.decoder_start_token_id							,      max_position_embeddings=self.max_position_embeddings							,      )

														return (config, input_ids, attention_mask, lm_labels)







							def        __lowerCAmelCase      (  self	:  List[str]							,      lowerCAmelCase_	:  Any							,      lowerCAmelCase_	:  Dict							,      lowerCAmelCase_	:  Optional[Any]							,      lowerCAmelCase_	:  int							,      )      ->							int:


														"""simple docstring"""


														_a     					=    True
														_a     					=    TrOCRDecoder(config=lowerCAmelCase_ ).to(lowerCAmelCase_ ).eval()
														_a     					=    input_ids[:2]

														input_ids[input_ids == 0] += 1
														# first forward pass
														_a     					=    model(lowerCAmelCase_							,      use_cache=lowerCAmelCase_ )
														_a     					=    model(lowerCAmelCase_ )
														_a     					=    model(lowerCAmelCase_							,      use_cache=lowerCAmelCase_ )

														self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) )
														self.parent.assertTrue(len(lowerCAmelCase_ ) == len(lowerCAmelCase_ ) + 1 )

														_a     					=    outputs['''past_key_values''']

														# create hypothetical next token and extent to next_input_ids
														_a     					=    ids_tensor((2, 1)							,      config.vocab_size - 1 ) + 1

														# append to next input_ids and
														_a     					=    torch.cat([input_ids, next_tokens]							,      dim=-1 )

														_a     					=    model(lowerCAmelCase_ )['''last_hidden_state''']
														_a     					=    model(lowerCAmelCase_							,      past_key_values=lowerCAmelCase_ )['''last_hidden_state''']

														# select random slice
														_a     					=    ids_tensor((1,)							,      output_from_past.shape[-1] ).item()
														_a     					=    output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
														_a     					=    output_from_past[:, 0, random_slice_idx].detach()

														# test that outputs are equal for slice
														assert torch.allclose(lowerCAmelCase_							,      lowerCAmelCase_							,      atol=1e-3 )







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


														"""simple docstring"""


														_a     					=    self.prepare_config_and_inputs()
														_a					,							_a					,							_a					,							_a     					=    config_and_inputs

														_a     					=    {'''input_ids''': input_ids, '''attention_mask''': attention_mask}
														return config, inputs_dict



@require_torch
class       A   (		_a					,_a					,_a					,unittest.TestCase   ):
							lowercase_          =    (TrOCRDecoder, TrOCRForCausalLM) if is_torch_available() else ()
							lowercase_          =    (TrOCRForCausalLM,) if is_torch_available() else ()
							lowercase_          =    {'text-generation': TrOCRForCausalLM} if is_torch_available() else {}
							lowercase_          =    True
							lowercase_          =    False







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


														"""simple docstring"""


														_a     					=    TrOCRStandaloneDecoderModelTester(self							,      is_training=lowerCAmelCase_ )
														_a     					=    ConfigTester(self							,      config_class=lowerCAmelCase_ )







							def        __lowerCAmelCase      (  self	:  List[str] )      ->							Any:


														"""simple docstring"""


														pass







							def        __lowerCAmelCase      (  self	:  Tuple )      ->							Dict:


														"""simple docstring"""


														pass







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


														"""simple docstring"""


														pass







							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														_a     					=    self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past(*lowerCAmelCase_ )







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


														"""simple docstring"""


														return







							@unittest.skip('''The model doesn\'t support left padding''' )  # and it's not used enough to be worth fixing :)
							def        __lowerCAmelCase      (  self	:  Union[str, Any] )      ->							Optional[int]:


														"""simple docstring"""


														pass

