"""simple docstring"""




import torch

from diffusers import DPMSolverSDEScheduler
from diffusers.utils import torch_device
from diffusers.utils.testing_utils import require_torchsde

from .test_schedulers import SchedulerCommonTest
@require_torchsde
class 						__A							( SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:			str							=		(DPMSolverSDEScheduler,)
     _UpperCamelCase							:			Optional[int]							=		10






     def     __A  (		self				,     **a__  ):
       _lowerCAmelCase						:       Optional[int]  			=     {
           """num_train_timesteps""": 1100,
           """beta_start""": 0.0_0_0_1,
           """beta_end""": 0.0_2,
           """beta_schedule""": """linear""",
           """noise_sampler_seed""": 0,
       }

       config.update(**a__  )
       return config






     def     __A  (		self  ):
       for timesteps in [10, 50, 100, 1000]:
         self.check_over_configs(num_train_timesteps=a__  )






     def     __A  (		self  ):
       for beta_start, beta_end in zip([0.0_0_0_0_1, 0.0_0_0_1, 0.0_0_1]				,     [0.0_0_0_2, 0.0_0_2, 0.0_2]  ):
         self.check_over_configs(beta_start=a__				,     beta_end=a__  )






     def     __A  (		self  ):
       for schedule in ["linear", "scaled_linear"]:
         self.check_over_configs(beta_schedule=a__  )






     def     __A  (		self  ):
       for prediction_type in ["epsilon", "v_prediction"]:
         self.check_over_configs(prediction_type=a__  )






     def     __A  (		self  ):
       _lowerCAmelCase						:       Tuple  			=     self.scheduler_classes[0]
       _lowerCAmelCase						:       Any  			=     self.get_scheduler_config()
       _lowerCAmelCase						:       Tuple  			=     scheduler_class(**a__  )

       scheduler.set_timesteps(self.num_inference_steps  )

       _lowerCAmelCase						:       int  			=     self.dummy_model()
       _lowerCAmelCase						:       Union[str, Any]  			=     self.dummy_sample_deter * scheduler.init_noise_sigma
       _lowerCAmelCase						:       int  			=     sample.to(a__  )

       for i, t in enumerate(scheduler.timesteps  ):
         _lowerCAmelCase						:       int  			=     scheduler.scale_model_input(a__				,     a__  )

         _lowerCAmelCase						:       Optional[int]  			=     model(a__				,     a__  )

         _lowerCAmelCase						:       List[Any]  			=     scheduler.step(a__				,     a__				,     a__  )
         _lowerCAmelCase						:       Dict  			=     output.prev_sample

       _lowerCAmelCase						:       Dict  			=     torch.sum(torch.abs(a__  )  )
       _lowerCAmelCase						:       Optional[Any]  			=     torch.mean(torch.abs(a__  )  )

       if torch_device in ["mps"]:
         assert abs(result_sum.item() - 1_6_7.4_7_8_2_1_0_4_4_9_2_1_8_7_5  ) < 1e-2
         assert abs(result_mean.item() - 0.2_1_7_8_7_0_5_9_6_4_5_6_5_2_7_7  ) < 1e-3
       elif torch_device in ["cuda"]:
         assert abs(result_sum.item() - 1_7_1.5_9_3_5_2_1_1_1_8_1_6_4_0_6  ) < 1e-2
         assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_6_8_9_2_2_9_9_6_5_2  ) < 1e-3
       else:
         assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2  ) < 1e-2
         assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6  ) < 1e-3






     def     __A  (		self  ):
       _lowerCAmelCase						:       Dict  			=     self.scheduler_classes[0]
       _lowerCAmelCase						:       str  			=     self.get_scheduler_config(prediction_type="""v_prediction"""  )
       _lowerCAmelCase						:       Union[str, Any]  			=     scheduler_class(**a__  )

       scheduler.set_timesteps(self.num_inference_steps  )

       _lowerCAmelCase						:       Any  			=     self.dummy_model()
       _lowerCAmelCase						:       List[str]  			=     self.dummy_sample_deter * scheduler.init_noise_sigma
       _lowerCAmelCase						:       List[Any]  			=     sample.to(a__  )

       for i, t in enumerate(scheduler.timesteps  ):
         _lowerCAmelCase						:       int  			=     scheduler.scale_model_input(a__				,     a__  )

         _lowerCAmelCase						:       int  			=     model(a__				,     a__  )

         _lowerCAmelCase						:       List[str]  			=     scheduler.step(a__				,     a__				,     a__  )
         _lowerCAmelCase						:       Optional[int]  			=     output.prev_sample

       _lowerCAmelCase						:       Any  			=     torch.sum(torch.abs(a__  )  )
       _lowerCAmelCase						:       str  			=     torch.mean(torch.abs(a__  )  )

       if torch_device in ["mps"]:
         assert abs(result_sum.item() - 1_2_4.7_7_1_4_9_2_0_0_4_3_9_4_5_3  ) < 1e-2
         assert abs(result_mean.item() - 0.1_6_2_2_6_2_8_9_0_1_4_8_1_6_2_8_4  ) < 1e-3
       elif torch_device in ["cuda"]:
         assert abs(result_sum.item() - 1_2_8.1_6_6_3_3_6_0_5_9_5_7_0_3  ) < 1e-2
         assert abs(result_mean.item() - 0.1_6_6_8_8_3_2_6_0_0_1_1_6_7_2_9_7  ) < 1e-3
       else:
         assert abs(result_sum.item() - 1_1_9.8_4_8_7_5_4_8_8_2_8_1_2_5  ) < 1e-2
         assert abs(result_mean.item() - 0.1_5_6_0_5_3_0_6_6_2_5_3_6_6_2_1  ) < 1e-3






     def     __A  (		self  ):
       _lowerCAmelCase						:       Union[str, Any]  			=     self.scheduler_classes[0]
       _lowerCAmelCase						:       Optional[Any]  			=     self.get_scheduler_config()
       _lowerCAmelCase						:       str  			=     scheduler_class(**a__  )

       scheduler.set_timesteps(self.num_inference_steps				,     device=a__  )

       _lowerCAmelCase						:       Tuple  			=     self.dummy_model()
       _lowerCAmelCase						:       Optional[Any]  			=     self.dummy_sample_deter.to(a__  ) * scheduler.init_noise_sigma

       for t in scheduler.timesteps:
         _lowerCAmelCase						:       Union[str, Any]  			=     scheduler.scale_model_input(a__				,     a__  )

         _lowerCAmelCase						:       Tuple  			=     model(a__				,     a__  )

         _lowerCAmelCase						:       Optional[int]  			=     scheduler.step(a__				,     a__				,     a__  )
         _lowerCAmelCase						:       List[str]  			=     output.prev_sample

       _lowerCAmelCase						:       int  			=     torch.sum(torch.abs(a__  )  )
       _lowerCAmelCase						:       List[str]  			=     torch.mean(torch.abs(a__  )  )

       if torch_device in ["mps"]:
         assert abs(result_sum.item() - 1_6_7.4_6_9_5_7_3_9_7_4_6_0_9_3_8  ) < 1e-2
         assert abs(result_mean.item() - 0.2_1_8_0_5_9_3_4_6_0_7_9_8_2_6_3_5  ) < 1e-3
       elif torch_device in ["cuda"]:
         assert abs(result_sum.item() - 1_7_1.5_9_3_5_3_6_3_7_6_9_5_3_1_2  ) < 1e-2
         assert abs(result_mean.item() - 0.2_2_3_4_2_9_0_8_3_8_2_4_1_5_7_7_1  ) < 1e-3
       else:
         assert abs(result_sum.item() - 1_6_2.5_2_3_8_3_4_2_2_8_5_1_5_6_2  ) < 1e-2
         assert abs(result_mean.item() - 0.2_1_1_6_1_9_5_7_0_8_5_1_3_2_6  ) < 1e-3






     def     __A  (		self  ):
       _lowerCAmelCase						:       Any  			=     self.scheduler_classes[0]
       _lowerCAmelCase						:       Dict  			=     self.get_scheduler_config()
       _lowerCAmelCase						:       Optional[int]  			=     scheduler_class(**a__				,     use_karras_sigmas=a__  )

       scheduler.set_timesteps(self.num_inference_steps				,     device=a__  )

       _lowerCAmelCase						:       Any  			=     self.dummy_model()
       _lowerCAmelCase						:       int  			=     self.dummy_sample_deter.to(a__  ) * scheduler.init_noise_sigma
       _lowerCAmelCase						:       int  			=     sample.to(a__  )

       for t in scheduler.timesteps:
         _lowerCAmelCase						:       Optional[Any]  			=     scheduler.scale_model_input(a__				,     a__  )

         _lowerCAmelCase						:       Optional[int]  			=     model(a__				,     a__  )

         _lowerCAmelCase						:       Union[str, Any]  			=     scheduler.step(a__				,     a__				,     a__  )
         _lowerCAmelCase						:       Tuple  			=     output.prev_sample

       _lowerCAmelCase						:       List[Any]  			=     torch.sum(torch.abs(a__  )  )
       _lowerCAmelCase						:       Dict  			=     torch.mean(torch.abs(a__  )  )

       if torch_device in ["mps"]:
         assert abs(result_sum.item() - 1_7_6.6_6_9_7_4_1_3_5_7_4_2_1_8_8  ) < 1e-2
         assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1  ) < 1e-2
       elif torch_device in ["cuda"]:
         assert abs(result_sum.item() - 1_7_7.6_3_6_5_3_5_6_4_4_5_3_1_2_5  ) < 1e-2
         assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1  ) < 1e-2
       else:
         assert abs(result_sum.item() - 1_7_0.3_1_3_5_2_2_3_3_8_8_6_7_2  ) < 1e-2
         assert abs(result_mean.item() - 0.2_3_0_0_3_8_7_2_7_3_0_9_8_1_8_1_1  ) < 1e-2












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	:							Optional[Any]				=logging.get_logger(__name__)

lowerCamelCase	:							Optional[int]				={
    '''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 			__a  (    A__       ):
 _lowerCAmelCase       :       Optional[int]        = '''owlvit_text_model'''







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=4_94_08							,   SCREAMING_SNAKE_CASE					:   List[str]=5_12							,   SCREAMING_SNAKE_CASE					:   List[Any]=20_48							,   SCREAMING_SNAKE_CASE					:   Any=12							,   SCREAMING_SNAKE_CASE					:   Any=8							,   SCREAMING_SNAKE_CASE					:   Dict=16							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   List[str]=1e-5							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=0.0							,   SCREAMING_SNAKE_CASE					:   Any=0.0_2							,   SCREAMING_SNAKE_CASE					:   int=1.0							,   SCREAMING_SNAKE_CASE					:   Any=0							,   SCREAMING_SNAKE_CASE					:   int=4_94_06							,   SCREAMING_SNAKE_CASE					:   List[str]=4_94_07							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]							,   ):


      '''simple docstring'''






      super().__init__(pad_token_id=SCREAMING_SNAKE_CASE							,   bos_token_id=SCREAMING_SNAKE_CASE							,   eos_token_id=SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Optional[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Any       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      # get the text config dict if we are loading from OwlViTConfig
      if config_dict.get("model_type"						) == "owlvit":
           UpperCamelCase__			:   Dict       				=  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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit_vision_model'''







 def __init__(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   str=7_68							,   SCREAMING_SNAKE_CASE					:   Dict=30_72							,   SCREAMING_SNAKE_CASE					:   int=12							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=12							,   SCREAMING_SNAKE_CASE					:   Dict=3							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=7_68							,   SCREAMING_SNAKE_CASE					:   Optional[int]=32							,   SCREAMING_SNAKE_CASE					:   Dict="quick_gelu"							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=1e-5							,   SCREAMING_SNAKE_CASE					:   List[str]=0.0							,   SCREAMING_SNAKE_CASE					:   Dict=0.0_2							,   SCREAMING_SNAKE_CASE					:   Optional[int]=1.0							,   **SCREAMING_SNAKE_CASE					:   Tuple							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

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







 @classmethod
 def 					__lowercase					(		cls					:   Dict							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   List[Any]						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   List[Any]       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      # get the vision config dict if we are loading from OwlViTConfig
      if config_dict.get("model_type"						) == "owlvit":
           UpperCamelCase__			:   List[str]       				=  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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







class 			__a  (    A__       ):
 _lowerCAmelCase       :       str        = '''owlvit'''
 _lowerCAmelCase       :       Tuple        = True







 def __init__(		self					:   Any							,   SCREAMING_SNAKE_CASE					:   Optional[Any]=None							,   SCREAMING_SNAKE_CASE					:   int=None							,   SCREAMING_SNAKE_CASE					:   str=5_12							,   SCREAMING_SNAKE_CASE					:   Any=2.6_5_9_2							,   SCREAMING_SNAKE_CASE					:   Union[str, Any]=True							,   **SCREAMING_SNAKE_CASE					:   List[Any]							,   ):


      '''simple docstring'''






      super().__init__(**SCREAMING_SNAKE_CASE						)

      if text_config is None:
           UpperCamelCase__			:   str       				=  {}
           logger.info("text_config is None. Initializing the OwlViTTextConfig with default values."						)

      if vision_config is None:
           UpperCamelCase__			:   List[str]       				=  {}
           logger.info("vision_config is None. initializing the OwlViTVisionConfig with default values."						)

      UpperCamelCase__			:   Dict       				=  OwlViTTextConfig(**SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Dict       				=  OwlViTVisionConfig(**SCREAMING_SNAKE_CASE						)

      UpperCamelCase__			:   Dict       				=  projection_dim
      UpperCamelCase__			:   Union[str, Any]       				=  logit_scale_init_value
      UpperCamelCase__			:   int       				=  return_dict
      UpperCamelCase__			:   Tuple       				=  1.0







 @classmethod
 def 					__lowercase					(		cls					:   Any							,   SCREAMING_SNAKE_CASE					:   Union[str, os.PathLike]							,   **SCREAMING_SNAKE_CASE					:   Dict						):


      '''simple docstring'''






      cls._set_token_in_kwargs(SCREAMING_SNAKE_CASE						)

      UpperCamelCase__				,     UpperCamelCase__			:   Dict       				=  cls.get_config_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)

      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(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 @classmethod
 def 					__lowercase					(		cls					:   int							,   SCREAMING_SNAKE_CASE					:   Dict							,   SCREAMING_SNAKE_CASE					:   Dict							,   **SCREAMING_SNAKE_CASE					:   Optional[int]						):


      '''simple docstring'''






      UpperCamelCase__			:   List[Any]       				=  {}
      UpperCamelCase__			:   Union[str, Any]       				=  text_config
      UpperCamelCase__			:   Optional[int]       				=  vision_config

      return cls.from_dict(SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)







 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  copy.deepcopy(self.__dict__						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.text_config.to_dict()
      UpperCamelCase__			:   List[str]       				=  self.vision_config.to_dict()
      UpperCamelCase__			:   Optional[int]       				=  self.__class__.model_type
      return output







class 			__a  (    A__       ):







 @property
 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      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 					__lowercase					(		self					:   Optional[int]						):


      '''simple docstring'''






      return OrderedDict(
          [
              ("logits_per_image", {0: "batch"}),
              ("logits_per_text", {0: "batch"}),
              ("text_embeds", {0: "batch"}),
              ("image_embeds", {0: "batch"}),
          ]						)







 @property
 def 					__lowercase					(		self					:   Dict						):


      '''simple docstring'''






      return 1e-4







 def 					__lowercase					(		self					:   Optional[int]							,   SCREAMING_SNAKE_CASE					:   "ProcessorMixin"							,   SCREAMING_SNAKE_CASE					:   int = -1							,   SCREAMING_SNAKE_CASE					:   int = -1							,   SCREAMING_SNAKE_CASE					:   Optional["TensorType"] = None							,   ):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[int]       				=  super().generate_dummy_inputs(
          processor.tokenizer							,   batch_size=SCREAMING_SNAKE_CASE							,   seq_length=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   List[str]       				=  super().generate_dummy_inputs(
          processor.image_processor							,   batch_size=SCREAMING_SNAKE_CASE							,   framework=SCREAMING_SNAKE_CASE						)
      return {**text_input_dict, **image_input_dict}







 @property
 def 					__lowercase					(		self					:   Tuple						):


      '''simple docstring'''






      return 14