



"""simple docstring"""




import gc
import unittest

from parameterized import parameterized

from diffusers import FlaxUNetaDConditionModel
from diffusers.utils import is_flax_available
from diffusers.utils.testing_utils import load_hf_numpy, require_flax, slow


if is_flax_available():
 import jax
 import jax.numpy as jnp






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



      def 						snake_case_ (					self					:     Union[str, Any]					,     A					:     Dict					,     A					:     List[Any]						):
           return f'gaussian_noise_s={seed}_shape={"_".join([str(A						) for s in shape]						)}.npy'



      def 						snake_case_ (					self					:     Union[str, Any]						):
           # clean up the VRAM after each test
           super().tearDown()
           gc.collect()



      def 						snake_case_ (					self					:     List[Any]					,     A					:     Dict=0					,     A					:     Union[str, Any]=(4, 4, 6_4, 6_4)					,     A					:     Any=False						):
           _UpperCAmelCase      :     Optional[int]        =   jnp.bfloataa if fpaa else jnp.floataa
           _UpperCAmelCase      :     Union[str, Any]        =   jnp.array(load_hf_numpy(self.get_file_format(A					,     A						)						)					,     dtype=A						)
           return image



      def 						snake_case_ (					self					:     Any					,     A					:     Tuple=False					,     A					:     Optional[Any]="CompVis/stable-diffusion-v1-4"						):
           _UpperCAmelCase      :     Optional[Any]        =   jnp.bfloataa if fpaa else jnp.floataa
           _UpperCAmelCase      :     List[Any]        =   "bf16" if fpaa else None

           _UpperCAmelCase    ,	_UpperCAmelCase      :     Tuple        =   FlaxUNetaDConditionModel.from_pretrained(
               A					,     subfolder="unet"					,     dtype=A					,     revision=A						)
           return model, params



      def 						snake_case_ (					self					:     Any					,     A					:     Tuple=0					,     A					:     Optional[Any]=(4, 7_7, 7_6_8)					,     A					:     Dict=False						):
           _UpperCAmelCase      :     Dict        =   jnp.bfloataa if fpaa else jnp.floataa
           _UpperCAmelCase      :     Dict        =   jnp.array(load_hf_numpy(self.get_file_format(A					,     A						)						)					,     dtype=A						)
           return hidden_states



      @parameterized.expand(
          [
              # fmt: off
              [8_3, 4, [-0.2_323, -0.1_304, 0.0_813, -0.3_093, -0.0_919, -0.1_571, -0.1_125, -0.5_806]],
              [1_7, 0.55, [-0.0_831, -0.2_443, 0.0_901, -0.0_919, 0.3_396, 0.0_103, -0.3_743, 0.0_701]],
              [8, 0.89, [-0.4_863, 0.0_859, 0.0_875, -0.1_658, 0.9_199, -0.0_114, 0.4_839, 0.4_639]],
              [3, 1_0_0_0, [-0.5_649, 0.2_402, -0.5_518, 0.1_248, 1.1_328, -0.2_443, -0.0_325, -1.0_078]],
              # fmt: on
          ]						)
      def 						snake_case_ (					self					:     int					,     A					:     Union[str, Any]					,     A					:     Tuple					,     A					:     List[Any]						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     Union[str, Any]        =   self.get_unet_model(model_id="CompVis/stable-diffusion-v1-4"					,     fpaa=A						)
           _UpperCAmelCase      :     Optional[Any]        =   self.get_latents(A					,     fpaa=A						)
           _UpperCAmelCase      :     Tuple        =   self.get_encoder_hidden_states(A					,     fpaa=A						)

           _UpperCAmelCase      :     Optional[Any]        =   model.apply(
               {"params": params}					,     A					,     jnp.array(A					,     dtype=jnp.intaa						)					,     encoder_hidden_states=A					,     ).sample

           assert sample.shape == latents.shape

           _UpperCAmelCase      :     Optional[int]        =   jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())						)					,     dtype=jnp.floataa						)
           _UpperCAmelCase      :     List[str]        =   jnp.array(A					,     dtype=jnp.floataa						)

           # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, in the same hardware
           assert jnp.allclose(A					,     A					,     atol=1e-2						)



      @parameterized.expand(
          [
              # fmt: off
              [8_3, 4, [0.1_514, 0.0_807, 0.1_624, 0.1_016, -0.1_896, 0.0_263, 0.0_677, 0.2_310]],
              [1_7, 0.55, [0.1_164, -0.0_216, 0.0_170, 0.1_589, -0.3_120, 0.1_005, -0.0_581, -0.1_458]],
              [8, 0.89, [-0.1_758, -0.0_169, 0.1_004, -0.1_411, 0.1_312, 0.1_103, -0.1_996, 0.2_139]],
              [3, 1_0_0_0, [0.1_214, 0.0_352, -0.0_731, -0.1_562, -0.0_994, -0.0_906, -0.2_340, -0.0_539]],
              # fmt: on
          ]						)
      def 						snake_case_ (					self					:     Optional[Any]					,     A					:     str					,     A					:     str					,     A					:     Optional[Any]						):
           _UpperCAmelCase    ,	_UpperCAmelCase      :     Optional[int]        =   self.get_unet_model(model_id="stabilityai/stable-diffusion-2"					,     fpaa=A						)
           _UpperCAmelCase      :     Tuple        =   self.get_latents(A					,     shape=(4, 4, 9_6, 9_6)					,     fpaa=A						)
           _UpperCAmelCase      :     Tuple        =   self.get_encoder_hidden_states(A					,     shape=(4, 7_7, 1_0_2_4)					,     fpaa=A						)

           _UpperCAmelCase      :     Any        =   model.apply(
               {"params": params}					,     A					,     jnp.array(A					,     dtype=jnp.intaa						)					,     encoder_hidden_states=A					,     ).sample

           assert sample.shape == latents.shape

           _UpperCAmelCase      :     str        =   jnp.asarray(jax.device_get((sample[-1, -2:, -2:, :2].flatten())						)					,     dtype=jnp.floataa						)
           _UpperCAmelCase      :     Optional[Any]        =   jnp.array(A					,     dtype=jnp.floataa						)

           # Found torch (float16) and flax (bfloat16) outputs to be within this tolerance, on the same hardware
           assert jnp.allclose(A					,     A					,     atol=1e-2						)





"""simple docstring"""




import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
 from ...processing_utils import ProcessorMixin
 from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


_lowerCAmelCase    : Dict							=							logging.get_logger(__name__)

_lowerCAmelCase    : List[str]							=							{
    "google/owlvit-base-patch32": "https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json",
    "google/owlvit-base-patch16": "https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json",
    "google/owlvit-large-patch14": "https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json",
}






class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       int								=			'owlvit_text_model'



      def __init__(					self					:     int					,     A					:     int=4_9_4_0_8					,     A					:     Optional[Any]=5_1_2					,     A					:     Optional[Any]=2_0_4_8					,     A					:     str=1_2					,     A					:     int=8					,     A					:     Tuple=1_6					,     A					:     List[Any]="quick_gelu"					,     A					:     Tuple=1e-5					,     A					:     Union[str, Any]=0.0					,     A					:     List[Any]=0.02					,     A					:     str=1.0					,     A					:     str=0					,     A					:     List[str]=4_9_4_0_6					,     A					:     str=4_9_4_0_7					,     **A					:     Optional[Any]					,     ):
           super().__init__(pad_token_id=A					,     bos_token_id=A					,     eos_token_id=A					,     **A						)

           _UpperCAmelCase      :     Union[str, Any]        =   vocab_size
           _UpperCAmelCase      :     str        =   hidden_size
           _UpperCAmelCase      :     List[Any]        =   intermediate_size
           _UpperCAmelCase      :     Any        =   num_hidden_layers
           _UpperCAmelCase      :     str        =   num_attention_heads
           _UpperCAmelCase      :     List[str]        =   max_position_embeddings
           _UpperCAmelCase      :     List[Any]        =   hidden_act
           _UpperCAmelCase      :     Tuple        =   layer_norm_eps
           _UpperCAmelCase      :     List[str]        =   attention_dropout
           _UpperCAmelCase      :     Optional[Any]        =   initializer_range
           _UpperCAmelCase      :     List[Any]        =   initializer_factor



      @classmethod
      def 						snake_case_ (					cls					:     Any					,     A					:     Union[str, os.PathLike]					,     **A					:     Dict						):
           cls._set_token_in_kwargs(A						)

           _UpperCAmelCase    ,	_UpperCAmelCase      :     List[str]        =   cls.get_config_dict(A					,     **A						)

           # get the text config dict if we are loading from OwlViTConfig
           if config_dict.get("model_type"						) == "owlvit":
                _UpperCAmelCase      :     int        =   config_dict["text_config"]

           if "model_type" in config_dict and hasattr(cls					,     "model_type"						) and config_dict["model_type"] != cls.model_type:
                logger.warning(
                    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'						)

           return cls.from_dict(A					,     **A						)







class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       Tuple								=			'owlvit_vision_model'



      def __init__(					self					:     Union[str, Any]					,     A					:     Optional[int]=7_6_8					,     A					:     int=3_0_7_2					,     A					:     List[str]=1_2					,     A					:     List[str]=1_2					,     A					:     Optional[int]=3					,     A					:     Optional[int]=7_6_8					,     A					:     str=3_2					,     A					:     Tuple="quick_gelu"					,     A					:     Dict=1e-5					,     A					:     Optional[int]=0.0					,     A					:     List[Any]=0.02					,     A					:     str=1.0					,     **A					:     Tuple					,     ):
           super().__init__(**A						)

           _UpperCAmelCase      :     List[str]        =   hidden_size
           _UpperCAmelCase      :     Tuple        =   intermediate_size
           _UpperCAmelCase      :     Optional[Any]        =   num_hidden_layers
           _UpperCAmelCase      :     Dict        =   num_attention_heads
           _UpperCAmelCase      :     Optional[Any]        =   num_channels
           _UpperCAmelCase      :     Union[str, Any]        =   image_size
           _UpperCAmelCase      :     Dict        =   patch_size
           _UpperCAmelCase      :     List[str]        =   hidden_act
           _UpperCAmelCase      :     Union[str, Any]        =   layer_norm_eps
           _UpperCAmelCase      :     Any        =   attention_dropout
           _UpperCAmelCase      :     Tuple        =   initializer_range
           _UpperCAmelCase      :     Tuple        =   initializer_factor



      @classmethod
      def 						snake_case_ (					cls					:     Optional[int]					,     A					:     Union[str, os.PathLike]					,     **A					:     int						):
           cls._set_token_in_kwargs(A						)

           _UpperCAmelCase    ,	_UpperCAmelCase      :     Dict        =   cls.get_config_dict(A					,     **A						)

           # get the vision config dict if we are loading from OwlViTConfig
           if config_dict.get("model_type"						) == "owlvit":
                _UpperCAmelCase      :     Tuple        =   config_dict["vision_config"]

           if "model_type" in config_dict and hasattr(cls					,     "model_type"						) and config_dict["model_type"] != cls.model_type:
                logger.warning(
                    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'						)

           return cls.from_dict(A					,     **A						)







class      UpperCAmelCase_  (    _UpperCamelCase						):
      __SCREAMING_SNAKE_CASE						:       List[str]								=			'owlvit'
      __SCREAMING_SNAKE_CASE						:       Optional[Any]								=			True



      def __init__(					self					:     Optional[Any]					,     A					:     Dict=None					,     A					:     Tuple=None					,     A					:     Optional[Any]=5_1_2					,     A					:     Optional[Any]=2.6_592					,     A					:     int=True					,     **A					:     Tuple					,     ):
           super().__init__(**A						)

           if text_config is None:
                _UpperCAmelCase      :     List[Any]        =   {}
                logger.info("text_config is None. Initializing the OwlViTTextConfig with default values."						)

           if vision_config is None:
                _UpperCAmelCase      :     Tuple        =   {}
                logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."						)

           _UpperCAmelCase      :     str        =   OwlViTTextConfig(**A						)
           _UpperCAmelCase      :     int        =   OwlViTVisionConfig(**A						)

           _UpperCAmelCase      :     Optional[Any]        =   projection_dim
           _UpperCAmelCase      :     str        =   logit_scale_init_value
           _UpperCAmelCase      :     Optional[Any]        =   return_dict
           _UpperCAmelCase      :     str        =   1.0



      @classmethod
      def 						snake_case_ (					cls					:     Dict					,     A					:     Union[str, os.PathLike]					,     **A					:     Any						):
           cls._set_token_in_kwargs(A						)

           _UpperCAmelCase    ,	_UpperCAmelCase      :     str        =   cls.get_config_dict(A					,     **A						)

           if "model_type" in config_dict and hasattr(cls					,     "model_type"						) and config_dict["model_type"] != cls.model_type:
                logger.warning(
                    f'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
                    f'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'						)

           return cls.from_dict(A					,     **A						)



      @classmethod
      def 						snake_case_ (					cls					:     Optional[int]					,     A					:     Dict					,     A					:     Dict					,     **A					:     Optional[Any]						):
           _UpperCAmelCase      :     Optional[Any]        =   {}
           _UpperCAmelCase      :     int        =   text_config
           _UpperCAmelCase      :     Dict        =   vision_config

           return cls.from_dict(A					,     **A						)



      def 						snake_case_ (					self					:     Optional[int]						):
           _UpperCAmelCase      :     str        =   copy.deepcopy(self.__dict__						)
           _UpperCAmelCase      :     Optional[int]        =   self.text_config.to_dict()
           _UpperCAmelCase      :     Optional[int]        =   self.vision_config.to_dict()
           _UpperCAmelCase      :     List[Any]        =   self.__class__.model_type
           return output







class      UpperCAmelCase_  (    _UpperCamelCase						):



      @property
      def 						snake_case_ (					self					:     List[str]						):
           return OrderedDict(
               [
                   ("input_ids", {0: "batch", 1: "sequence"}),
                   ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
                   ("attention_mask", {0: "batch", 1: "sequence"}),
               ]						)



      @property
      def 						snake_case_ (					self					:     Optional[int]						):
           return OrderedDict(
               [
                   ("logits_per_image", {0: "batch"}),
                   ("logits_per_text", {0: "batch"}),
                   ("text_embeds", {0: "batch"}),
                   ("image_embeds", {0: "batch"}),
               ]						)



      @property
      def 						snake_case_ (					self					:     str						):
           return 1e-4



      def 						snake_case_ (					self					:     str					,     A					:     "ProcessorMixin"					,     A					:     int = -1					,     A					:     int = -1					,     A					:     Optional["TensorType"] = None					,     ):
           _UpperCAmelCase      :     Optional[Any]        =   super().generate_dummy_inputs(
               processor.tokenizer					,     batch_size=A					,     seq_length=A					,     framework=A						)
           _UpperCAmelCase      :     Union[str, Any]        =   super().generate_dummy_inputs(
               processor.image_processor					,     batch_size=A					,     framework=A						)
           return {**text_input_dict, **image_input_dict}



      @property
      def 						snake_case_ (					self					:     List[Any]						):
           return 1_4
