


from typing import List, Optional, Union

import torch
from transformers import (
    XLMRobertaTokenizer,
)

from ...models import UNetaDConditionModel, VQModel
from ...pipelines import DiffusionPipeline
from ...pipelines.pipeline_utils import ImagePipelineOutput
from ...schedulers import DDIMScheduler, DDPMScheduler
from ...utils import (
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .text_encoder import MultilingualCLIP


__a     =			logging.get_logger(__name__)  # pylint: disable=invalid-name

__a     =			"""
    Examples:
        ```py
        >>> from diffusers import KandinskyPipeline, KandinskyPriorPipeline
        >>> import torch

        >>> pipe_prior = KandinskyPriorPipeline.from_pretrained(\"kandinsky-community/Kandinsky-2-1-prior\")
        >>> pipe_prior.to(\"cuda\")

        >>> prompt = \"red cat, 4k photo\"
        >>> out = pipe_prior(prompt)
        >>> image_emb = out.image_embeds
        >>> negative_image_emb = out.negative_image_embeds

        >>> pipe = KandinskyPipeline.from_pretrained(\"kandinsky-community/kandinsky-2-1\")
        >>> pipe.to(\"cuda\")

        >>> image = pipe(
        ...     prompt,
        ...     image_embeds=image_emb,
        ...     negative_image_embeds=negative_image_emb,
        ...     height=768,
        ...     width=768,
        ...     num_inference_steps=100,
        ... ).images

        >>> image[0].save(\"cat.png\")
        ```
"""
def   _UpperCamelCase				(						lowerCAmelCase_  ,						lowerCAmelCase_  ,						lowerCAmelCase_=8   )						->List[str]:
   UpperCAmelCase							=     h // scale_factor**2
   if h % scale_factor**2 != 0:
      new_h += 1
   UpperCAmelCase							=     w // scale_factor**2
   if w % scale_factor**2 != 0:
      new_w += 1
   return new_h * scale_factor, new_w * scale_factor






class   __lowercase				(  __snake_case  ):





      def __init__(		self						:					Optional[Any]					,       __lowerCamelCase						:					MultilingualCLIP					,       __lowerCamelCase						:					XLMRobertaTokenizer					,       __lowerCamelCase						:					UNetaDConditionModel					,       __lowerCamelCase						:					Union[DDIMScheduler, DDPMScheduler]					,       __lowerCamelCase						:					VQModel					,       )    ->   List[str]:






         """simple docstring"""







         super().__init__()

         self.register_modules(
             text_encoder=__lowerCamelCase					,       tokenizer=__lowerCamelCase					,       unet=__lowerCamelCase					,       scheduler=__lowerCamelCase					,       movq=__lowerCamelCase					,       )
         UpperCAmelCase							=     2 ** (len(self.movq.config.block_out_channels       ) - 1)





      def        _lowercase				(		self						:					str					,       __lowerCamelCase						:					Optional[Any]					,       __lowerCamelCase						:					Optional[Any]					,       __lowerCamelCase						:					List[str]					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					str					,       __lowerCamelCase						:					Any       )    ->   Tuple:






         """simple docstring"""







         if latents is None:
            UpperCAmelCase							=     randn_tensor(__lowerCamelCase					,       generator=__lowerCamelCase					,       device=__lowerCamelCase					,       dtype=__lowerCamelCase       )
         else:
            if latents.shape != shape:
               raise ValueError(F"""Unexpected latents shape, got {latents.shape}, expected {shape}"""       )
            UpperCAmelCase							=     latents.to(__lowerCamelCase       )

         UpperCAmelCase							=     latents * scheduler.init_noise_sigma
         return latents





      def        _lowercase				(		self						:					List[Any]					,       __lowerCamelCase						:					List[Any]					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					Any					,       __lowerCamelCase						:					str					,       __lowerCamelCase						:					Union[str, Any]=None					,       )    ->   List[str]:






         """simple docstring"""







         UpperCAmelCase							=     len(__lowerCamelCase       ) if isinstance(__lowerCamelCase					,       __lowerCamelCase       ) else 1
         # get prompt text embeddings
         UpperCAmelCase							=     self.tokenizer(
             __lowerCamelCase					,       padding="""max_length"""					,       truncation=__lowerCamelCase					,       max_length=7_7					,       return_attention_mask=__lowerCamelCase					,       add_special_tokens=__lowerCamelCase					,       return_tensors="""pt"""					,       )

         UpperCAmelCase							=     text_inputs.input_ids
         UpperCAmelCase							=     self.tokenizer(__lowerCamelCase					,       padding="""longest"""					,       return_tensors="""pt"""       ).input_ids

         if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(__lowerCamelCase					,       __lowerCamelCase       ):
            UpperCAmelCase							=     self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]       )
            logger.warning(
                """The following part of your input was truncated because CLIP can only handle sequences up to"""
                F""" {self.tokenizer.model_max_length} tokens: {removed_text}"""       )

         UpperCAmelCase							=     text_input_ids.to(__lowerCamelCase       )
         UpperCAmelCase							=     text_inputs.attention_mask.to(__lowerCamelCase       )

         UpperCAmelCase				,  UpperCAmelCase							=     self.text_encoder(
             input_ids=__lowerCamelCase					,       attention_mask=__lowerCamelCase       )

         UpperCAmelCase							=     prompt_embeds.repeat_interleave(__lowerCamelCase					,       dim=0       )
         UpperCAmelCase							=     text_encoder_hidden_states.repeat_interleave(__lowerCamelCase					,       dim=0       )
         UpperCAmelCase							=     text_mask.repeat_interleave(__lowerCamelCase					,       dim=0       )

         if do_classifier_free_guidance:
            UpperCAmelCase							=     4_2
            if negative_prompt is None:
               UpperCAmelCase							=     [""""""] * batch_size
            elif type(__lowerCamelCase       ) is not type(__lowerCamelCase       ):
               raise TypeError(
                   F"""`negative_prompt` should be the same type to `prompt`, but got {type(__lowerCamelCase       )} !="""
                   F""" {type(__lowerCamelCase       )}."""       )
            elif isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
               UpperCAmelCase							=     [negative_prompt]
            elif batch_size != len(__lowerCamelCase       ):
               raise ValueError(
                   F"""`negative_prompt`: {negative_prompt} has batch size {len(__lowerCamelCase       )}, but `prompt`:"""
                   F""" {prompt} has batch size {batch_size}. Please make sure that passed `negative_prompt` matches"""
                   """ the batch size of `prompt`."""       )
            else:
               UpperCAmelCase							=     negative_prompt

            UpperCAmelCase							=     self.tokenizer(
                __lowerCamelCase					,       padding="""max_length"""					,       max_length=7_7					,       truncation=__lowerCamelCase					,       return_attention_mask=__lowerCamelCase					,       add_special_tokens=__lowerCamelCase					,       return_tensors="""pt"""					,       )
            UpperCAmelCase							=     uncond_input.input_ids.to(__lowerCamelCase       )
            UpperCAmelCase							=     uncond_input.attention_mask.to(__lowerCamelCase       )

            UpperCAmelCase				,  UpperCAmelCase							=     self.text_encoder(
                input_ids=__lowerCamelCase					,       attention_mask=__lowerCamelCase       )

            # duplicate unconditional embeddings for each generation per prompt, using mps friendly method

            UpperCAmelCase							=     negative_prompt_embeds.shape[1]
            UpperCAmelCase							=     negative_prompt_embeds.repeat(1					,       __lowerCamelCase       )
            UpperCAmelCase							=     negative_prompt_embeds.view(batch_size * num_images_per_prompt					,       __lowerCamelCase       )

            UpperCAmelCase							=     uncond_text_encoder_hidden_states.shape[1]
            UpperCAmelCase							=     uncond_text_encoder_hidden_states.repeat(1					,       __lowerCamelCase					,       1       )
            UpperCAmelCase							=     uncond_text_encoder_hidden_states.view(
                batch_size * num_images_per_prompt					,       __lowerCamelCase					,       -1       )
            UpperCAmelCase							=     uncond_text_mask.repeat_interleave(__lowerCamelCase					,       dim=0       )

            # done duplicates

            # For classifier free guidance, we need to do two forward passes.
            # Here we concatenate the unconditional and text embeddings into a single batch
            # to avoid doing two forward passes
            UpperCAmelCase							=     torch.cat([negative_prompt_embeds, prompt_embeds]       )
            UpperCAmelCase							=     torch.cat([uncond_text_encoder_hidden_states, text_encoder_hidden_states]       )

            UpperCAmelCase							=     torch.cat([uncond_text_mask, text_mask]       )

         return prompt_embeds, text_encoder_hidden_states, text_mask





      def        _lowercase				(		self						:					Tuple					,       __lowerCamelCase						:					Dict=0       )    ->   Any:






         """simple docstring"""







         if is_accelerate_available():
            from accelerate import cpu_offload
         else:
            raise ImportError("""Please install accelerate via `pip install accelerate`"""       )

         UpperCAmelCase							=     torch.device(F"""cuda:{gpu_id}"""       )

         UpperCAmelCase							=     [
             self.unet,
             self.text_encoder,
             self.movq,
         ]
         for cpu_offloaded_model in models:
            if cpu_offloaded_model is not None:
               cpu_offload(__lowerCamelCase					,       __lowerCamelCase       )





      def        _lowercase				(		self						:					Optional[int]					,       __lowerCamelCase						:					List[Any]=0       )    ->   List[str]:






         """simple docstring"""







         if is_accelerate_available() and is_accelerate_version(""">="""					,       """0.17.0.dev0"""       ):
            from accelerate import cpu_offload_with_hook
         else:
            raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."""       )

         UpperCAmelCase							=     torch.device(F"""cuda:{gpu_id}"""       )

         if self.device.type != "cpu":
            self.to("""cpu"""					,       silence_dtype_warnings=__lowerCamelCase       )
            torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

         UpperCAmelCase							=     None
         for cpu_offloaded_model in [self.text_encoder, self.unet, self.movq]:
            UpperCAmelCase				,  UpperCAmelCase							=     cpu_offload_with_hook(__lowerCamelCase					,       __lowerCamelCase					,       prev_module_hook=__lowerCamelCase       )

         if self.safety_checker is not None:
            UpperCAmelCase				,  UpperCAmelCase							=     cpu_offload_with_hook(self.safety_checker					,       __lowerCamelCase					,       prev_module_hook=__lowerCamelCase       )

         # We'll offload the last model manually.
         UpperCAmelCase							=     hook





      @property
      # Copied from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline._execution_device
      def        _lowercase				(		self						:					Tuple       )    ->   List[str]:






         """simple docstring"""







         if not hasattr(self.unet					,       """_hf_hook"""       ):
            return self.device
         for module in self.unet.modules():
            if (
                hasattr(__lowerCamelCase					,       """_hf_hook"""       )
                and hasattr(module._hf_hook					,       """execution_device"""       )
                and module._hf_hook.execution_device is not None
            ):
               return torch.device(module._hf_hook.execution_device       )
         return self.device





      @torch.no_grad()
      @replace_example_docstring(__lowerCamelCase       )
      def __call__(		self						:					List[str]					,       __lowerCamelCase						:					Union[str, List[str]]					,       __lowerCamelCase						:					Union[torch.FloatTensor, List[torch.FloatTensor]]					,       __lowerCamelCase						:					Union[torch.FloatTensor, List[torch.FloatTensor]]					,       __lowerCamelCase						:					Optional[Union[str, List[str]]] = None					,       __lowerCamelCase						:					int = 5_1_2					,       __lowerCamelCase						:					int = 5_1_2					,       __lowerCamelCase						:					int = 1_0_0					,       __lowerCamelCase						:					float = 4.0					,       __lowerCamelCase						:					int = 1					,       __lowerCamelCase						:					Optional[Union[torch.Generator, List[torch.Generator]]] = None					,       __lowerCamelCase						:					Optional[torch.FloatTensor] = None					,       __lowerCamelCase						:					Optional[str] = "pil"					,       __lowerCamelCase						:					bool = True					,       )    ->   Any:






         """simple docstring"""







         if isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
            UpperCAmelCase							=     1
         elif isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
            UpperCAmelCase							=     len(__lowerCamelCase       )
         else:
            raise ValueError(F"""`prompt` has to be of type `str` or `list` but is {type(__lowerCamelCase       )}"""       )

         UpperCAmelCase							=     self._execution_device

         UpperCAmelCase							=     batch_size * num_images_per_prompt
         UpperCAmelCase							=     guidance_scale > 1.0

         UpperCAmelCase				,  UpperCAmelCase				,  UpperCAmelCase							=     self._encode_prompt(
             __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase       )

         if isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
            UpperCAmelCase							=     torch.cat(__lowerCamelCase					,       dim=0       )
         if isinstance(__lowerCamelCase					,       __lowerCamelCase       ):
            UpperCAmelCase							=     torch.cat(__lowerCamelCase					,       dim=0       )

         if do_classifier_free_guidance:
            UpperCAmelCase							=     image_embeds.repeat_interleave(__lowerCamelCase					,       dim=0       )
            UpperCAmelCase							=     negative_image_embeds.repeat_interleave(__lowerCamelCase					,       dim=0       )

         UpperCAmelCase							=     torch.cat([negative_image_embeds, image_embeds]					,       dim=0       ).to(
             dtype=prompt_embeds.dtype					,       device=__lowerCamelCase       )

         self.scheduler.set_timesteps(__lowerCamelCase					,       device=__lowerCamelCase       )
         UpperCAmelCase							=     self.scheduler.timesteps

         UpperCAmelCase							=     self.unet.config.in_channels

         UpperCAmelCase				,  UpperCAmelCase							=     get_new_h_w(__lowerCamelCase					,       __lowerCamelCase					,       self.movq_scale_factor       )

         # create initial latent
         UpperCAmelCase							=     self.prepare_latents(
             (batch_size, num_channels_latents, height, width)					,       text_encoder_hidden_states.dtype					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       self.scheduler					,       )

         for i, t in enumerate(self.progress_bar(__lowerCamelCase       )       ):
            # expand the latents if we are doing classifier free guidance
            UpperCAmelCase							=     torch.cat([latents] * 2       ) if do_classifier_free_guidance else latents

            UpperCAmelCase							=     {"""text_embeds""": prompt_embeds, """image_embeds""": image_embeds}
            UpperCAmelCase							=     self.unet(
                sample=__lowerCamelCase					,       timestep=__lowerCamelCase					,       encoder_hidden_states=__lowerCamelCase					,       added_cond_kwargs=__lowerCamelCase					,       return_dict=__lowerCamelCase					,       )[0]

            if do_classifier_free_guidance:
               UpperCAmelCase				,  UpperCAmelCase							=     noise_pred.split(latents.shape[1]					,       dim=1       )
               UpperCAmelCase				,  UpperCAmelCase							=     noise_pred.chunk(2       )
               UpperCAmelCase				,  UpperCAmelCase							=     variance_pred.chunk(2       )
               UpperCAmelCase							=     noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)
               UpperCAmelCase							=     torch.cat([noise_pred, variance_pred_text]					,       dim=1       )

            if not (
                hasattr(self.scheduler.config					,       """variance_type"""       )
                and self.scheduler.config.variance_type in ["learned", "learned_range"]
            ):
               UpperCAmelCase				,  UpperCAmelCase							=     noise_pred.split(latents.shape[1]					,       dim=1       )

            # compute the previous noisy sample x_t -> x_t-1
            UpperCAmelCase							=     self.scheduler.step(
                __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       generator=__lowerCamelCase					,       ).prev_sample
         # post-processing
         UpperCAmelCase							=     self.movq.decode(__lowerCamelCase					,       force_not_quantize=__lowerCamelCase       )["""sample"""]

         if output_type not in ["pt", "np", "pil"]:
            raise ValueError(F"""Only the output types `pt`, `pil` and `np` are supported not output_type={output_type}"""       )

         if output_type in ["np", "pil"]:
            UpperCAmelCase							=     image * 0.5 + 0.5
            UpperCAmelCase							=     image.clamp(0					,       1       )
            UpperCAmelCase							=     image.cpu().permute(0					,       2					,       3					,       1       ).float().numpy()

         if output_type == "pil":
            UpperCAmelCase							=     self.numpy_to_pil(__lowerCamelCase       )

         if not return_dict:
            return (image,)

         return ImagePipelineOutput(images=__lowerCamelCase       )







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





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






         """simple docstring"""







         UpperCAmelCase							=     tempfile.mkdtemp()

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

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

         UpperCAmelCase							=     {
             """do_resize""": True,
             """size""": 2_0,
             """do_center_crop""": True,
             """crop_size""": 1_8,
             """do_normalize""": True,
             """image_mean""": [0.48_145_466, 0.4_578_275, 0.40_821_073],
             """image_std""": [0.26_862_954, 0.26_130_258, 0.27_577_711],
         }
         UpperCAmelCase							=     os.path.join(self.tmpdirname					,       __lowerCamelCase       )
         with open(self.image_processor_file					,       """w"""					,       encoding="""utf-8"""       ) as fp:
            json.dump(__lowerCamelCase					,       __lowerCamelCase       )





      def        _lowercase				(		self						:					List[Any]					,       **__lowerCamelCase						:					Union[str, Any]       )    ->   int:






         """simple docstring"""







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





      def        _lowercase				(		self						:					Optional[Any]					,       **__lowerCamelCase						:					List[str]       )    ->   str:






         """simple docstring"""







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





      def        _lowercase				(		self						:					Union[str, Any]					,       **__lowerCamelCase						:					Optional[Any]       )    ->   Optional[int]:






         """simple docstring"""







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





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






         """simple docstring"""







         shutil.rmtree(self.tmpdirname       )





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






         """simple docstring"""







         UpperCAmelCase							=     [np.random.randint(2_5_5					,       size=(3, 3_0, 4_0_0)					,       dtype=np.uinta       )]

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

         return image_inputs





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






         """simple docstring"""







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

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )
         processor_slow.save_pretrained(self.tmpdirname       )
         UpperCAmelCase							=     OwlViTProcessor.from_pretrained(self.tmpdirname					,       use_fast=__lowerCamelCase       )

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

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





      def        _lowercase				(		self						:					str       )    ->   Dict:






         """simple docstring"""







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

         UpperCAmelCase							=     self.get_tokenizer(bos_token="""(BOS)"""					,       eos_token="""(EOS)"""       )
         UpperCAmelCase							=     self.get_image_processor(do_normalize=__lowerCamelCase       )

         UpperCAmelCase							=     OwlViTProcessor.from_pretrained(
             self.tmpdirname					,       bos_token="""(BOS)"""					,       eos_token="""(EOS)"""					,       do_normalize=__lowerCamelCase       )

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

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





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






         """simple docstring"""







         UpperCAmelCase							=     self.get_image_processor()
         UpperCAmelCase							=     self.get_tokenizer()

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )

         UpperCAmelCase							=     self.prepare_image_inputs()

         UpperCAmelCase							=     image_processor(__lowerCamelCase					,       return_tensors="""np"""       )
         UpperCAmelCase							=     processor(images=__lowerCamelCase					,       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        _lowercase				(		self						:					Optional[int]       )    ->   Any:






         """simple docstring"""







         UpperCAmelCase							=     self.get_image_processor()
         UpperCAmelCase							=     self.get_tokenizer()

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )

         UpperCAmelCase							=     """lower newer"""

         UpperCAmelCase							=     processor(text=__lowerCamelCase					,       return_tensors="""np"""       )

         UpperCAmelCase							=     tokenizer(__lowerCamelCase					,       return_tensors="""np"""       )

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





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






         """simple docstring"""







         UpperCAmelCase							=     self.get_image_processor()
         UpperCAmelCase							=     self.get_tokenizer()

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )

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

         UpperCAmelCase							=     processor(text=__lowerCamelCase					,       images=__lowerCamelCase       )

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

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





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






         """simple docstring"""







         UpperCAmelCase							=     """google/owlvit-base-patch32"""
         UpperCAmelCase							=     OwlViTProcessor.from_pretrained(__lowerCamelCase       )

         UpperCAmelCase							=     ["""cat""", """nasa badge"""]
         UpperCAmelCase							=     processor(text=__lowerCamelCase       )

         UpperCAmelCase							=     1_6
         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(__lowerCamelCase       ):
            processor()





      def        _lowercase				(		self						:					Any       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     """google/owlvit-base-patch32"""
         UpperCAmelCase							=     OwlViTProcessor.from_pretrained(__lowerCamelCase       )

         UpperCAmelCase							=     [["""cat""", """nasa badge"""], ["""person"""]]
         UpperCAmelCase							=     processor(text=__lowerCamelCase       )

         UpperCAmelCase							=     1_6
         UpperCAmelCase							=     len(__lowerCamelCase       )
         UpperCAmelCase							=     max([len(__lowerCamelCase       ) 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(__lowerCamelCase       ):
            processor()





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






         """simple docstring"""







         UpperCAmelCase							=     """google/owlvit-base-patch32"""
         UpperCAmelCase							=     OwlViTProcessor.from_pretrained(__lowerCamelCase       )

         UpperCAmelCase							=     ["""cat""", """nasa badge"""]
         UpperCAmelCase							=     processor(text=__lowerCamelCase       )

         UpperCAmelCase							=     1_6
         UpperCAmelCase							=     inputs["""input_ids"""]
         UpperCAmelCase							=     [
             [4_9_4_0_6, 2_3_6_8, 4_9_4_0_7, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
             [4_9_4_0_6, 6_8_4_1, 1_1_3_0_1, 4_9_4_0_7, 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        _lowercase				(		self						:					List[Any]       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.get_image_processor()
         UpperCAmelCase							=     self.get_tokenizer()

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )

         UpperCAmelCase							=     self.prepare_image_inputs()
         UpperCAmelCase							=     self.prepare_image_inputs()

         UpperCAmelCase							=     processor(images=__lowerCamelCase					,       query_images=__lowerCamelCase       )

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

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





      def        _lowercase				(		self						:					Tuple       )    ->   Any:






         """simple docstring"""







         UpperCAmelCase							=     self.get_image_processor()
         UpperCAmelCase							=     self.get_tokenizer()

         UpperCAmelCase							=     OwlViTProcessor(tokenizer=__lowerCamelCase					,       image_processor=__lowerCamelCase       )

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

         UpperCAmelCase							=     processor.batch_decode(__lowerCamelCase       )
         UpperCAmelCase							=     tokenizer.batch_decode(__lowerCamelCase       )

         self.assertListEqual(__lowerCamelCase					,       __lowerCamelCase       )



