

'''simple docstring'''







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



    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      tempfile.mkdtemp()

           # fmt: off
           A_						=      ["""""", """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
           A_						=      dict(zip(UpperCamelCase__		,				range(len(UpperCamelCase__       )       )       )       )
           A_						=      ["""#version: 0.2""", """l o""", """lo w</w>""", """e r</w>""", """"""]
           A_						=      {"""unk_token""": """<unk>"""}

           A_						=      os.path.join(self.tmpdirname		,				VOCAB_FILES_NAMES["""vocab_file"""]       )
           A_						=      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(UpperCamelCase__       ) + """\n"""       )
           with open(self.merges_file		,				"""w"""		,				encoding="""utf-8"""       ) as fp:
                  fp.write("""\n""".join(UpperCamelCase__       )       )

           A_						=      {
               """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],
           }
           A_						=      os.path.join(self.tmpdirname		,				UpperCamelCase__       )
           with open(self.image_processor_file		,				"""w"""		,				encoding="""utf-8"""       ) as fp:
                  json.dump(UpperCamelCase__		,				UpperCamelCase__       )



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           return CLIPTokenizer.from_pretrained(self.tmpdirname		,				pad_token="""!"""		,				**UpperCamelCase__       )



    def 					snake_case_			(					self		,				**UpperCamelCase__       )						->      str:





           '''simple docstring'''
           return CLIPTokenizerFast.from_pretrained(self.tmpdirname		,				pad_token="""!"""		,				**UpperCamelCase__       )



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





           '''simple docstring'''
           return OwlViTImageProcessor.from_pretrained(self.tmpdirname		,				**UpperCamelCase__       )



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           shutil.rmtree(self.tmpdirname       )



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





           '''simple docstring'''
           A_						=      [np.random.randint(255		,				size=(3, 30, 400)		,				dtype=np.uinta       )]

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

           return image_inputs



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





           '''simple docstring'''
           A_						=      self.get_tokenizer()
           A_						=      self.get_rust_tokenizer()
           A_						=      self.get_image_processor()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )
           processor_slow.save_pretrained(self.tmpdirname       )
           A_						=      OwlViTProcessor.from_pretrained(self.tmpdirname		,				use_fast=UpperCamelCase__       )

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )
           processor_fast.save_pretrained(self.tmpdirname       )
           A_						=      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		,				UpperCamelCase__       )
           self.assertIsInstance(processor_fast.tokenizer		,				UpperCamelCase__       )

           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		,				UpperCamelCase__       )
           self.assertIsInstance(processor_fast.image_processor		,				UpperCamelCase__       )



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





           '''simple docstring'''
           A_						=      OwlViTProcessor(tokenizer=self.get_tokenizer()		,				image_processor=self.get_image_processor()       )
           processor.save_pretrained(self.tmpdirname       )

           A_						=      self.get_tokenizer(bos_token="""(BOS)"""		,				eos_token="""(EOS)"""       )
           A_						=      self.get_image_processor(do_normalize=UpperCamelCase__       )

           A_						=      OwlViTProcessor.from_pretrained(
               self.tmpdirname		,				bos_token="""(BOS)"""		,				eos_token="""(EOS)"""		,				do_normalize=UpperCamelCase__       )

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

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



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





           '''simple docstring'''
           A_						=      self.get_image_processor()
           A_						=      self.get_tokenizer()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )

           A_						=      self.prepare_image_inputs()

           A_						=      image_processor(UpperCamelCase__		,				return_tensors="""np"""       )
           A_						=      processor(images=UpperCamelCase__		,				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 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           A_						=      self.get_image_processor()
           A_						=      self.get_tokenizer()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )

           A_						=      """lower newer"""

           A_						=      processor(text=UpperCamelCase__		,				return_tensors="""np"""       )

           A_						=      tokenizer(UpperCamelCase__		,				return_tensors="""np"""       )

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



    def 					snake_case_			(					self       )						->      Dict:





           '''simple docstring'''
           A_						=      self.get_image_processor()
           A_						=      self.get_tokenizer()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )

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

           A_						=      processor(text=UpperCamelCase__		,				images=UpperCamelCase__       )

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

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



    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           A_						=      """google/owlvit-base-patch32"""
           A_						=      OwlViTProcessor.from_pretrained(UpperCamelCase__       )

           A_						=      ["""cat""", """nasa badge"""]
           A_						=      processor(text=UpperCamelCase__       )

           A_						=      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(UpperCamelCase__       ):
                  processor()



    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           A_						=      """google/owlvit-base-patch32"""
           A_						=      OwlViTProcessor.from_pretrained(UpperCamelCase__       )

           A_						=      [["""cat""", """nasa badge"""], ["""person"""]]
           A_						=      processor(text=UpperCamelCase__       )

           A_						=      16
           A_						=      len(UpperCamelCase__       )
           A_						=      max([len(UpperCamelCase__       ) 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(UpperCamelCase__       ):
                  processor()



    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           A_						=      """google/owlvit-base-patch32"""
           A_						=      OwlViTProcessor.from_pretrained(UpperCamelCase__       )

           A_						=      ["""cat""", """nasa badge"""]
           A_						=      processor(text=UpperCamelCase__       )

           A_						=      16
           A_						=      inputs["""input_ids"""]
           A_						=      [
               [49406, 2368, 49407, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
               [49406, 6841, 11301, 49407, 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 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      self.get_image_processor()
           A_						=      self.get_tokenizer()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )

           A_						=      self.prepare_image_inputs()
           A_						=      self.prepare_image_inputs()

           A_						=      processor(images=UpperCamelCase__		,				query_images=UpperCamelCase__       )

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

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







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





           '''simple docstring'''
           A_						=      self.get_image_processor()
           A_						=      self.get_tokenizer()

           A_						=      OwlViTProcessor(tokenizer=UpperCamelCase__		,				image_processor=UpperCamelCase__       )

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

           A_						=      processor.batch_decode(UpperCamelCase__       )
           A_						=      tokenizer.batch_decode(UpperCamelCase__       )

           self.assertListEqual(UpperCamelCase__		,				UpperCamelCase__       )



'''simple docstring'''







import unittest

import numpy as np
import torch
from torch import nn
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModelWithProjection,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import KandinskyVaaPriorPipeline, PriorTransformer, UnCLIPScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import enable_full_determinism, skip_mps

from ..test_pipelines_common import PipelineTesterMixin


enable_full_determinism()


class 					A__      (      _snake_case						,						unittest.TestCase			):
    lowercase						=		KandinskyVaaPriorPipeline
    lowercase						=		["prompt"]
    lowercase						=		["prompt", "negative_prompt"]
    lowercase						=		[
        "num_images_per_prompt",
        "generator",
        "num_inference_steps",
        "latents",
        "negative_prompt",
        "guidance_scale",
        "output_type",
        "return_dict",
    ]
    lowercase						=		False



    @property
    def 					snake_case_			(					self       )						->      Any:





           '''simple docstring'''
           return 32



    @property
    def 					snake_case_			(					self       )						->      Optional[int]:





           '''simple docstring'''
           return 32



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





           '''simple docstring'''
           return self.time_input_dim



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           return self.time_input_dim * 4



    @property
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           return 100



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





           '''simple docstring'''
           A_						=      CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""       )
           return tokenizer



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





           '''simple docstring'''
           torch.manual_seed(0       )
           A_						=      CLIPTextConfig(
               bos_token_id=0		,				eos_token_id=2		,				hidden_size=self.text_embedder_hidden_size		,				projection_dim=self.text_embedder_hidden_size		,				intermediate_size=37		,				layer_norm_eps=1e-0_5		,				num_attention_heads=4		,				num_hidden_layers=5		,				pad_token_id=1		,				vocab_size=1000		,				)
           return CLIPTextModelWithProjection(UpperCamelCase__       )



    @property
    def 					snake_case_			(					self       )						->      Tuple:





           '''simple docstring'''
           torch.manual_seed(0       )

           A_						=      {
               """num_attention_heads""": 2,
               """attention_head_dim""": 12,
               """embedding_dim""": self.text_embedder_hidden_size,
               """num_layers""": 1,
           }

           A_						=      PriorTransformer(**UpperCamelCase__       )
           # clip_std and clip_mean is initialized to be 0 so PriorTransformer.post_process_latents will always return 0 - set clip_std to be 1 so it won't return 0
           A_						=      nn.Parameter(torch.ones(model.clip_std.shape       )       )
           return model



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           torch.manual_seed(0       )
           A_						=      CLIPVisionConfig(
               hidden_size=self.text_embedder_hidden_size		,				image_size=224		,				projection_dim=self.text_embedder_hidden_size		,				intermediate_size=37		,				num_attention_heads=4		,				num_channels=3		,				num_hidden_layers=5		,				patch_size=14		,				)

           A_						=      CLIPVisionModelWithProjection(UpperCamelCase__       )
           return model



    @property
    def 					snake_case_			(					self       )						->      str:





           '''simple docstring'''
           A_						=      CLIPImageProcessor(
               crop_size=224		,				do_center_crop=UpperCamelCase__		,				do_normalize=UpperCamelCase__		,				do_resize=UpperCamelCase__		,				image_mean=[0.48145466, 0.4578275, 0.40821073]		,				image_std=[0.26862954, 0.26130258, 0.27577711]		,				resample=3		,				size=224		,				)

           return image_processor



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





           '''simple docstring'''
           A_						=      self.dummy_prior
           A_						=      self.dummy_image_encoder
           A_						=      self.dummy_text_encoder
           A_						=      self.dummy_tokenizer
           A_						=      self.dummy_image_processor

           A_						=      UnCLIPScheduler(
               variance_type="""fixed_small_log"""		,				prediction_type="""sample"""		,				num_train_timesteps=1000		,				clip_sample=UpperCamelCase__		,				clip_sample_range=10.0		,				)

           A_						=      {
               """prior""": prior,
               """image_encoder""": image_encoder,
               """text_encoder""": text_encoder,
               """tokenizer""": tokenizer,
               """scheduler""": scheduler,
               """image_processor""": image_processor,
           }

           return components



    def 					snake_case_			(					self		,				UpperCamelCase__		,				UpperCamelCase__=0       )						->      Optional[int]:





           '''simple docstring'''
           if str(UpperCamelCase__       ).startswith("""mps"""       ):
                  A_						=      torch.manual_seed(UpperCamelCase__       )
           else:
                  A_						=      torch.Generator(device=UpperCamelCase__       ).manual_seed(UpperCamelCase__       )
           A_						=      {
               """prompt""": """horse""",
               """generator""": generator,
               """guidance_scale""": 4.0,
               """num_inference_steps""": 2,
               """output_type""": """np""",
           }
           return inputs



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





           '''simple docstring'''
           A_						=      """cpu"""

           A_						=      self.get_dummy_components()

           A_						=      self.pipeline_class(**UpperCamelCase__       )
           A_						=      pipe.to(UpperCamelCase__       )

           pipe.set_progress_bar_config(disable=UpperCamelCase__       )

           A_						=      pipe(**self.get_dummy_inputs(UpperCamelCase__       )       )
           A_						=      output.image_embeds

           A_						=      pipe(
               **self.get_dummy_inputs(UpperCamelCase__       )		,				return_dict=UpperCamelCase__		,				)[0]

           A_						=      image[0, -10:]
           A_						=      image_from_tuple[0, -10:]

           assert image.shape == (1, 32)

           A_						=      np.array(
               [-0.0532, 1.7120, 0.3656, -1.0852, -0.8946, -1.1756, 0.4348, 0.2482, 0.5146, -0.1156]       )

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



    @skip_mps
    def 					snake_case_			(					self       )						->      int:





           '''simple docstring'''
           A_						=      torch_device == """cpu"""
           A_						=      True
           A_						=      False

           self._test_inference_batch_single_identical(
               test_max_difference=UpperCamelCase__		,				relax_max_difference=UpperCamelCase__		,				test_mean_pixel_difference=UpperCamelCase__		,				)







    @skip_mps
    def 					snake_case_			(					self       )						->      Union[str, Any]:





           '''simple docstring'''
           A_						=      torch_device == """cpu"""
           A_						=      False

           self._test_attention_slicing_forward_pass(
               test_max_difference=UpperCamelCase__		,				test_mean_pixel_difference=UpperCamelCase__		,				)
