





import json
import os
import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers import CLIPTokenizer, CLIPTokenizerFast
from transformers.models.clip.tokenization_clip import VOCAB_FILES_NAMES
from transformers.testing_utils import require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import OwlViTImageProcessor, OwlViTProcessor





@require_vision
class  lowerCAmelCase							(       unittest.TestCase ):







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  tempfile.mkdtemp()

     # fmt: off
     lowercase__			       =  ["", "l", "o", "w", "e", "r", "s", "t", "i", "d", "n", "lo", "l</w>", "w</w>", "r</w>", "t</w>", "low</w>", "er</w>", "lowest</w>", "newer</w>", "wider", "<unk>", "<|startoftext|>", "<|endoftext|>"]
     # fmt: on
     lowercase__			       =  dict(zip(_lowercase   ,							range(len(_lowercase ) ) ) )
     lowercase__			       =  ["#version: 0.2", "l o", "lo w</w>", "e r</w>", ""]
     lowercase__			       =  {"unk_token": "<unk>"}

     lowercase__			       =  os.path.join(self.tmpdirname   ,							VOCAB_FILES_NAMES["vocab_file"] )
     lowercase__			       =  os.path.join(self.tmpdirname   ,							VOCAB_FILES_NAMES["merges_file"] )
     with open(self.vocab_file   ,							"w"   ,							encoding="utf-8" ) as fp:
        fp.write(json.dumps(_lowercase ) + "\n" )
     with open(self.merges_file   ,							"w"   ,							encoding="utf-8" ) as fp:
        fp.write("\n".join(_lowercase ) )

     lowercase__			       =  {
         "do_resize": True,
         "size": 20,
         "do_center_crop": True,
         "crop_size": 18,
         "do_normalize": True,
         "image_mean": [0.48145466, 0.4578275, 0.40821073],
         "image_std": [0.26862954, 0.26130258, 0.27577711],
     }
     lowercase__			       =  os.path.join(self.tmpdirname   ,							_lowercase )
     with open(self.image_processor_file   ,							"w"   ,							encoding="utf-8" ) as fp:
        json.dump(_lowercase   ,							_lowercase )







  def   UpperCAmelCase					(      self  :Optional[Any]   ,							**_lowercase  :Dict ):






     '''simple docstring'''





     return CLIPTokenizer.from_pretrained(self.tmpdirname   ,							pad_token="!"   ,							**_lowercase )







  def   UpperCAmelCase					(      self  :Optional[Any]   ,							**_lowercase  :int ):






     '''simple docstring'''





     return CLIPTokenizerFast.from_pretrained(self.tmpdirname   ,							pad_token="!"   ,							**_lowercase )







  def   UpperCAmelCase					(      self  :List[str]   ,							**_lowercase  :List[Any] ):






     '''simple docstring'''





     return OwlViTImageProcessor.from_pretrained(self.tmpdirname   ,							**_lowercase )







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     shutil.rmtree(self.tmpdirname )







  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     lowercase__			       =  [np.random.randint(2_55   ,							size=(3, 30, 4_00)   ,							dtype=np.uinta )]

     lowercase__			       =  [Image.fromarray(np.moveaxis(_lowercase   ,							0   ,							-1 ) ) for x in image_inputs]

     return image_inputs







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     lowercase__			       =  self.get_tokenizer()
     lowercase__			       =  self.get_rust_tokenizer()
     lowercase__			       =  self.get_image_processor()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )
     processor_slow.save_pretrained(self.tmpdirname )
     lowercase__			       =  OwlViTProcessor.from_pretrained(self.tmpdirname   ,							use_fast=_lowercase )

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )
     processor_fast.save_pretrained(self.tmpdirname )
     lowercase__			       =  OwlViTProcessor.from_pretrained(self.tmpdirname )

     self.assertEqual(processor_slow.tokenizer.get_vocab()   ,							tokenizer_slow.get_vocab() )
     self.assertEqual(processor_fast.tokenizer.get_vocab()   ,							tokenizer_fast.get_vocab() )
     self.assertEqual(tokenizer_slow.get_vocab()   ,							tokenizer_fast.get_vocab() )
     self.assertIsInstance(processor_slow.tokenizer   ,							_lowercase )
     self.assertIsInstance(processor_fast.tokenizer   ,							_lowercase )

     self.assertEqual(processor_slow.image_processor.to_json_string()   ,							image_processor.to_json_string() )
     self.assertEqual(processor_fast.image_processor.to_json_string()   ,							image_processor.to_json_string() )
     self.assertIsInstance(processor_slow.image_processor   ,							_lowercase )
     self.assertIsInstance(processor_fast.image_processor   ,							_lowercase )







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  OwlViTProcessor(tokenizer=self.get_tokenizer()   ,							image_processor=self.get_image_processor() )
     processor.save_pretrained(self.tmpdirname )

     lowercase__			       =  self.get_tokenizer(bos_token="(BOS)"   ,							eos_token="(EOS)" )
     lowercase__			       =  self.get_image_processor(do_normalize=_lowercase )

     lowercase__			       =  OwlViTProcessor.from_pretrained(
         self.tmpdirname   ,							bos_token="(BOS)"   ,							eos_token="(EOS)"   ,							do_normalize=_lowercase )

     self.assertEqual(processor.tokenizer.get_vocab()   ,							tokenizer_add_kwargs.get_vocab() )
     self.assertIsInstance(processor.tokenizer   ,							_lowercase )

     self.assertEqual(processor.image_processor.to_json_string()   ,							image_processor_add_kwargs.to_json_string() )
     self.assertIsInstance(processor.image_processor   ,							_lowercase )







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  self.get_image_processor()
     lowercase__			       =  self.get_tokenizer()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )

     lowercase__			       =  self.prepare_image_inputs()

     lowercase__			       =  image_processor(_lowercase   ,							return_tensors="np" )
     lowercase__			       =  processor(images=_lowercase   ,							return_tensors="np" )

     for key in input_image_proc.keys():
        self.assertAlmostEqual(input_image_proc[key].sum()   ,							input_processor[key].sum()   ,							delta=1e-2 )







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  self.get_image_processor()
     lowercase__			       =  self.get_tokenizer()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )

     lowercase__			       =  "lower newer"

     lowercase__			       =  processor(text=_lowercase   ,							return_tensors="np" )

     lowercase__			       =  tokenizer(_lowercase   ,							return_tensors="np" )

     for key in encoded_tok.keys():
        self.assertListEqual(encoded_tok[key][0].tolist()   ,							encoded_processor[key][0].tolist() )







  def   UpperCAmelCase					(      self  :Union[str, Any] ):






     '''simple docstring'''





     lowercase__			       =  self.get_image_processor()
     lowercase__			       =  self.get_tokenizer()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )

     lowercase__			       =  "lower newer"
     lowercase__			       =  self.prepare_image_inputs()

     lowercase__			       =  processor(text=_lowercase   ,							images=_lowercase )

     self.assertListEqual(list(inputs.keys() )   ,							["input_ids", "attention_mask", "pixel_values"] )

     # test if it raises when no input is passed
     with pytest.raises(_lowercase ):
        processor()







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__			       =  "google/owlvit-base-patch32"
     lowercase__			       =  OwlViTProcessor.from_pretrained(_lowercase )

     lowercase__			       =  ["cat", "nasa badge"]
     lowercase__			       =  processor(text=_lowercase )

     lowercase__			       =  16
     self.assertListEqual(list(inputs.keys() )   ,							["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape   ,							(2, seq_length) )

     # test if it raises when no input is passed
     with pytest.raises(_lowercase ):
        processor()







  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     lowercase__			       =  "google/owlvit-base-patch32"
     lowercase__			       =  OwlViTProcessor.from_pretrained(_lowercase )

     lowercase__			       =  [["cat", "nasa badge"], ["person"]]
     lowercase__			       =  processor(text=_lowercase )

     lowercase__			       =  16
     lowercase__			       =  len(_lowercase )
     lowercase__			       =  max([len(_lowercase ) for texts in input_texts] )

     self.assertListEqual(list(inputs.keys() )   ,							["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape   ,							(batch_size * num_max_text_queries, seq_length) )

     # test if it raises when no input is passed
     with pytest.raises(_lowercase ):
        processor()







  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     lowercase__			       =  "google/owlvit-base-patch32"
     lowercase__			       =  OwlViTProcessor.from_pretrained(_lowercase )

     lowercase__			       =  ["cat", "nasa badge"]
     lowercase__			       =  processor(text=_lowercase )

     lowercase__			       =  16
     lowercase__			       =  inputs["input_ids"]
     lowercase__			       =  [
         [4_94_06, 23_68, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
         [4_94_06, 68_41, 1_13_01, 4_94_07, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
     ]

     self.assertListEqual(list(inputs.keys() )   ,							["input_ids", "attention_mask"] )
     self.assertEqual(inputs["input_ids"].shape   ,							(2, seq_length) )
     self.assertListEqual(list(input_ids[0] )   ,							predicted_ids[0] )
     self.assertListEqual(list(input_ids[1] )   ,							predicted_ids[1] )







  def   UpperCAmelCase					(      self  :str ):






     '''simple docstring'''





     lowercase__			       =  self.get_image_processor()
     lowercase__			       =  self.get_tokenizer()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )

     lowercase__			       =  self.prepare_image_inputs()
     lowercase__			       =  self.prepare_image_inputs()

     lowercase__			       =  processor(images=_lowercase   ,							query_images=_lowercase )

     self.assertListEqual(list(inputs.keys() )   ,							["query_pixel_values", "pixel_values"] )

     # test if it raises when no input is passed
     with pytest.raises(_lowercase ):
        processor()







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     lowercase__			       =  self.get_image_processor()
     lowercase__			       =  self.get_tokenizer()

     lowercase__			       =  OwlViTProcessor(tokenizer=_lowercase   ,							image_processor=_lowercase )

     lowercase__			       =  [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

     lowercase__			       =  processor.batch_decode(_lowercase )
     lowercase__			       =  tokenizer.batch_decode(_lowercase )

     self.assertListEqual(_lowercase   ,							_lowercase )













import gc
import random
import unittest

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

import diffusers
from diffusers import (
    AutoencoderKL,
    EulerDiscreteScheduler,
    StableDiffusionLatentUpscalePipeline,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.schedulers import KarrasDiffusionSchedulers
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 ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()


def _A       (   __magic_name__	):
   lowercase__			       =  [tensor.shape for tensor in tensor_list]
   return all(shape == shapes[0] for shape in shapes[1:]	)





class  lowerCAmelCase							(       lowercase_							,							lowercase_							,							lowercase_							,							unittest.TestCase ):
  __lowerCamelCase		=      StableDiffusionLatentUpscalePipeline
  __lowerCamelCase		=      TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {
      'height',
      'width',
      'cross_attention_kwargs',
      'negative_prompt_embeds',
      'prompt_embeds',
  }
  __lowerCamelCase		=      PipelineTesterMixin.required_optional_params - {'num_images_per_prompt'}
  __lowerCamelCase		=      TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
  __lowerCamelCase		=      frozenset(
      [] )  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
  __lowerCamelCase		=      frozenset([] )

  __lowerCamelCase		=      True







  @property
  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     lowercase__			       =  1
     lowercase__			       =  4
     lowercase__			       =  (16, 16)

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







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     torch.manual_seed(0 )
     lowercase__			       =  UNetaDConditionModel(
         act_fn="gelu"   ,							attention_head_dim=8   ,							norm_num_groups=_lowercase   ,							block_out_channels=[32, 32, 64, 64]   ,							time_cond_proj_dim=1_60   ,							conv_in_kernel=1   ,							conv_out_kernel=1   ,							cross_attention_dim=32   ,							down_block_types=(
             "KDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
             "KCrossAttnDownBlock2D",
         )   ,							in_channels=8   ,							mid_block_type=_lowercase   ,							only_cross_attention=_lowercase   ,							out_channels=5   ,							resnet_time_scale_shift="scale_shift"   ,							time_embedding_type="fourier"   ,							timestep_post_act="gelu"   ,							up_block_types=("KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KCrossAttnUpBlock2D", "KUpBlock2D")   ,							)
     lowercase__			       =  AutoencoderKL(
         block_out_channels=[32, 32, 64, 64]   ,							in_channels=3   ,							out_channels=3   ,							down_block_types=[
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
             "DownEncoderBlock2D",
         ]   ,							up_block_types=["UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D", "UpDecoderBlock2D"]   ,							latent_channels=4   ,							)
     lowercase__			       =  EulerDiscreteScheduler(prediction_type="sample" )
     lowercase__			       =  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="quick_gelu"   ,							projection_dim=5_12   ,							)
     lowercase__			       =  CLIPTextModel(_lowercase )
     lowercase__			       =  CLIPTokenizer.from_pretrained("hf-internal-testing/tiny-random-clip" )

     lowercase__			       =  {
         "unet": model.eval(),
         "vae": vae.eval(),
         "scheduler": scheduler,
         "text_encoder": text_encoder,
         "tokenizer": tokenizer,
     }

     return components







  def   UpperCAmelCase					(      self  :Dict   ,							_lowercase  :Union[str, Any]   ,							_lowercase  :int=0 ):






     '''simple docstring'''





     if str(_lowercase ).startswith("mps" ):
        lowercase__			       =  torch.manual_seed(_lowercase )
     else:
        lowercase__			       =  torch.Generator(device=_lowercase ).manual_seed(_lowercase )
     lowercase__			       =  {
         "prompt": "A painting of a squirrel eating a burger",
         "image": self.dummy_image.cpu(),
         "generator": generator,
         "num_inference_steps": 2,
         "output_type": "numpy",
     }
     return inputs







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





     lowercase__			       =  "cpu"

     lowercase__			       =  self.get_dummy_components()
     lowercase__			       =  self.pipeline_class(**_lowercase )
     pipe.to(_lowercase )
     pipe.set_progress_bar_config(disable=_lowercase )

     lowercase__			       =  self.get_dummy_inputs(_lowercase )
     lowercase__			       =  pipe(**_lowercase ).images
     lowercase__			       =  image[0, -3:, -3:, -1]

     self.assertEqual(image.shape   ,							(1, 2_56, 2_56, 3) )
     lowercase__			       =  np.array(
         [0.47222412, 0.41921633, 0.44717434, 0.46874192, 0.42588258, 0.46150726, 0.4677534, 0.45583832, 0.48579055] )
     lowercase__			       =  np.abs(image_slice.flatten() - expected_slice ).max()
     self.assertLessEqual(_lowercase   ,							1e-3 )







  def   UpperCAmelCase					(      self  :Any ):






     '''simple docstring'''





     super().test_attention_slicing_forward_pass(expected_max_diff=7e-3 )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :int ):






     '''simple docstring'''





     super().test_dict_tuple_outputs_equivalent(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :List[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :Optional[int] ):






     '''simple docstring'''





     super().test_save_load_local(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :Tuple ):






     '''simple docstring'''





     super().test_save_load_optional_components(expected_max_difference=3e-3 )







  def   UpperCAmelCase					(      self  :Dict ):






     '''simple docstring'''





     lowercase__			       =  [
         "DDIMScheduler",
         "DDPMScheduler",
         "PNDMScheduler",
         "HeunDiscreteScheduler",
         "EulerAncestralDiscreteScheduler",
         "KDPM2DiscreteScheduler",
         "KDPM2AncestralDiscreteScheduler",
         "DPMSolverSDEScheduler",
     ]
     lowercase__			       =  self.get_dummy_components()
     lowercase__			       =  self.pipeline_class(**_lowercase )

     # make sure that PNDM does not need warm-up
     pipe.scheduler.register_to_config(skip_prk_steps=_lowercase )

     pipe.to(_lowercase )
     pipe.set_progress_bar_config(disable=_lowercase )
     lowercase__			       =  self.get_dummy_inputs(_lowercase )
     lowercase__			       =  2

     lowercase__			       =  []
     for scheduler_enum in KarrasDiffusionSchedulers:
        if scheduler_enum.name in skip_schedulers:
           # no sigma schedulers are not supported
           # no schedulers
           continue

        lowercase__			       =  getattr(_lowercase   ,							scheduler_enum.name )
        lowercase__			       =  scheduler_cls.from_config(pipe.scheduler.config )
        lowercase__			       =  pipe(**_lowercase )[0]
        outputs.append(_lowercase )

     assert check_same_shape(_lowercase )





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







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





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







  def   UpperCAmelCase					(      self  :str ):






     '''simple docstring'''





     lowercase__			       =  torch.manual_seed(33 )

     lowercase__			       =  StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4"   ,							torch_dtype=torch.floataa )
     pipe.to("cuda" )

     lowercase__			       =  StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler"   ,							torch_dtype=torch.floataa )
     upscaler.to("cuda" )

     lowercase__			       =  "a photo of an astronaut high resolution, unreal engine, ultra realistic"

     lowercase__			       =  pipe(_lowercase   ,							generator=_lowercase   ,							output_type="latent" ).images

     lowercase__			       =  upscaler(
         prompt=_lowercase   ,							image=_lowercase   ,							num_inference_steps=20   ,							guidance_scale=0   ,							generator=_lowercase   ,							output_type="np"   ,							).images[0]

     lowercase__			       =  load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/astronaut_1024.npy" )
     assert np.abs((expected_image - image).mean() ) < 5e-2







  def   UpperCAmelCase					(      self  :Optional[Any] ):






     '''simple docstring'''





     lowercase__			       =  torch.manual_seed(33 )

     lowercase__			       =  StableDiffusionLatentUpscalePipeline.from_pretrained(
         "stabilityai/sd-x2-latent-upscaler"   ,							torch_dtype=torch.floataa )
     upscaler.to("cuda" )

     lowercase__			       =  "the temple of fire by Ross Tran and Gerardo Dottori, oil on canvas"

     lowercase__			       =  load_image(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_512.png" )

     lowercase__			       =  upscaler(
         prompt=_lowercase   ,							image=_lowercase   ,							num_inference_steps=20   ,							guidance_scale=0   ,							generator=_lowercase   ,							output_type="np"   ,							).images[0]

     lowercase__			       =  load_numpy(
         "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/latent-upscaler/fire_temple_1024.npy" )
     assert np.abs((expected_image - image).max() ) < 5e-2






