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


SCREAMING_SNAKE_CASE									=						logging.get_logger(__name__)

SCREAMING_SNAKE_CASE									=						{
    '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  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "owlvit_text_model"







				def __init__( self    :       List[Any]			,					lowerCamelCase_    :       Optional[int]=4_9_4_0_8			,					lowerCamelCase_    :       Tuple=5_1_2			,					lowerCamelCase_    :       List[str]=2_0_4_8			,					lowerCamelCase_    :       Optional[Any]=1_2			,					lowerCamelCase_    :       Dict=8			,					lowerCamelCase_    :       Optional[Any]=1_6			,					lowerCamelCase_    :       Optional[Any]="quick_gelu"			,					lowerCamelCase_    :       str=1E-5			,					lowerCamelCase_    :       int=0.0			,					lowerCamelCase_    :       Any=0.02			,					lowerCamelCase_    :       List[Any]=1.0			,					lowerCamelCase_    :       Union[str, Any]=0			,					lowerCamelCase_    :       Any=4_9_4_0_6			,					lowerCamelCase_    :       Dict=4_9_4_0_7			,					**lowerCamelCase_    :       str			,					):

					"""simple docstring"""






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

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







				@classmethod
				def 							__UpperCAmelCase					( cls    :       str			,					lowerCamelCase_    :       Optional[Any]			,					**lowerCamelCase_    :       Optional[Any]						):

					"""simple docstring"""






					cls._set_token_in_kwargs(lowerCamelCase_						)

					_lowercase				,      _lowercase :    Dict							=							cls.get_config_dict(lowerCamelCase_			,					**lowerCamelCase_						)

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



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "owlvit_vision_model"







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

					"""simple docstring"""






					super().__init__(**lowerCamelCase_						)

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







				@classmethod
				def 							__UpperCAmelCase					( cls    :       Union[str, Any]			,					lowerCamelCase_    :       Optional[int]			,					**lowerCamelCase_    :       Any						):

					"""simple docstring"""






					cls._set_token_in_kwargs(lowerCamelCase_						)

					_lowercase				,      _lowercase :    Optional[Any]							=							cls.get_config_dict(lowerCamelCase_			,					**lowerCamelCase_						)

					# get the vision config dict if we are loading from OwlViTConfig
					if config_dict.get('model_type'						) == "owlvit":
						_lowercase :    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(lowerCamelCase_			,					**lowerCamelCase_						)



class  _lowerCamelCase			(__lowerCamelCase     ):
				_snake_case     =  "owlvit"
				_snake_case     =  True







				def __init__( self    :       Union[str, Any]			,					lowerCamelCase_    :       Optional[int]=None			,					lowerCamelCase_    :       int=None			,					lowerCamelCase_    :       Tuple=5_1_2			,					lowerCamelCase_    :       Optional[Any]=2.6592			,					lowerCamelCase_    :       Optional[Any]=True			,					**lowerCamelCase_    :       int			,					):

					"""simple docstring"""






					super().__init__(**lowerCamelCase_						)

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

					if vision_config is None:
						_lowercase :    Union[str, Any]							=							{}
						logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'						)

					_lowercase :    List[Any]							=							OwlViTTextConfig(**lowerCamelCase_						)
					_lowercase :    Union[str, Any]							=							OwlViTVisionConfig(**lowerCamelCase_						)

					_lowercase :    List[str]							=							projection_dim
					_lowercase :    Optional[int]							=							logit_scale_init_value
					_lowercase :    Optional[Any]							=							return_dict
					_lowercase :    str							=							1.0







				@classmethod
				def 							__UpperCAmelCase					( cls    :       Union[str, Any]			,					lowerCamelCase_    :       Union[str, Any]			,					**lowerCamelCase_    :       Union[str, Any]						):

					"""simple docstring"""






					cls._set_token_in_kwargs(lowerCamelCase_						)

					_lowercase				,      _lowercase :    List[Any]							=							cls.get_config_dict(lowerCamelCase_			,					**lowerCamelCase_						)

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







				@classmethod
				def 							__UpperCAmelCase					( cls    :       str			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       str			,					**lowerCamelCase_    :       Optional[int]						):

					"""simple docstring"""






					_lowercase :    Tuple							=							{}
					_lowercase :    Dict							=							text_config
					_lowercase :    str							=							vision_config

					return cls.from_dict(lowerCamelCase_			,					**lowerCamelCase_						)







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					_lowercase :    Tuple							=							copy.deepcopy(self.__dict__						)
					_lowercase :    Optional[Any]							=							self.text_config.to_dict()
					_lowercase :    Optional[Any]							=							self.vision_config.to_dict()
					_lowercase :    List[Any]							=							self.__class__.model_type
					return output



class  _lowerCamelCase			(__lowerCamelCase     ):







				@property
				def 							__UpperCAmelCase					( self    :       List[str]						):

					"""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 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






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







				@property
				def 							__UpperCAmelCase					( self    :       Tuple						):

					"""simple docstring"""






					return 1E-4







				def 							__UpperCAmelCase					( self    :       Dict			,					lowerCamelCase_    :       Union[str, Any]			,					lowerCamelCase_    :       Union[str, Any] = -1			,					lowerCamelCase_    :       str = -1			,					lowerCamelCase_    :       Optional[int] = None			,					):

					"""simple docstring"""






					_lowercase :    str							=							super().generate_dummy_inputs(
					    processor.tokenizer			,					batch_size=lowerCamelCase_			,					seq_length=lowerCamelCase_			,					framework=lowerCamelCase_						)
					_lowercase :    int							=							super().generate_dummy_inputs(
					    processor.image_processor			,					batch_size=lowerCamelCase_			,					framework=lowerCamelCase_						)
					return {**text_input_dict, **image_input_dict}







				@property
				def 							__UpperCAmelCase					( self    :       List[str]						):

					"""simple docstring"""






					return 1_4




"""simple docstring"""







from __future__ import annotations

import math
import random
from collections.abc import Collection
from typing import overload



class  _lowerCamelCase			:







				def __init__( self    :       str			,					lowerCamelCase_    :       Collection[float] | None = None						):

					"""simple docstring"""






					if components is None:
						_lowercase :    Any							=							[]
					_lowercase :    int							=							list(lowerCamelCase_						)







				def __len__( self    :       int						):

					"""simple docstring"""






					return len(self.__components						)







				def __str__( self    :       List[Any]						):

					"""simple docstring"""






					return "(" + ",".join(map(lowerCamelCase_			,					self.__components						)						) + ")"







				def __add__( self    :       Dict			,					lowerCamelCase_    :       Vector						):

					"""simple docstring"""






					_lowercase :    Union[str, Any]							=							len(self						)
					if size == len(lowerCamelCase_						):
						_lowercase :    int							=							[self.__components[i] + other.component(lowerCamelCase_						) for i in range(lowerCamelCase_						)]
						return Vector(lowerCamelCase_						)
					else:
						raise Exception('must have the same size'						)







				def __sub__( self    :       int			,					lowerCamelCase_    :       Vector						):

					"""simple docstring"""






					_lowercase :    Optional[int]							=							len(self						)
					if size == len(lowerCamelCase_						):
						_lowercase :    Optional[int]							=							[self.__components[i] - other.component(lowerCamelCase_						) for i in range(lowerCamelCase_						)]
						return Vector(lowerCamelCase_						)
					else:  # error case
						raise Exception('must have the same size'						)







				@overload
				def __mul__( self    :       Tuple			,					lowerCamelCase_    :       float						):

					"""simple docstring"""






					...







				@overload
				def __mul__( self    :       Tuple			,					lowerCamelCase_    :       Vector						):

					"""simple docstring"""






					...







				def __mul__( self    :       int			,					lowerCamelCase_    :       float | Vector						):

					"""simple docstring"""






					if isinstance(lowerCamelCase_			,					(float, int)						):
						_lowercase :    List[Any]							=							[c * other for c in self.__components]
						return Vector(lowerCamelCase_						)
					elif isinstance(lowerCamelCase_			,					lowerCamelCase_						) and len(self						) == len(lowerCamelCase_						):
						_lowercase :    Any							=							len(self						)
						_lowercase :    List[Any]							=							[self.__components[i] * other.component(lowerCamelCase_						) for i in range(lowerCamelCase_						)]
						return sum(lowerCamelCase_						)
					else:  # error case
						raise Exception('invalid operand!'						)







				def 							__UpperCAmelCase					( self    :       Dict						):

					"""simple docstring"""






					return Vector(self.__components						)







				def 							__UpperCAmelCase					( self    :       List[str]			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if isinstance(lowerCamelCase_			,					lowerCamelCase_						) and -len(self.__components						) <= i < len(self.__components						):
						return self.__components[i]
					else:
						raise Exception('index out of range'						)







				def 							__UpperCAmelCase					( self    :       str			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       float						):

					"""simple docstring"""






					assert -len(self.__components						) <= pos < len(self.__components						)
					_lowercase :    Tuple							=							value







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					if len(self.__components						) == 0:
						raise Exception('Vector is empty'						)
					_lowercase :    Optional[int]							=							[c**2 for c in self.__components]
					return math.sqrt(sum(lowerCamelCase_						)						)







				def 							__UpperCAmelCase					( self    :       int			,					lowerCamelCase_    :       Vector			,					lowerCamelCase_    :       bool = False						):

					"""simple docstring"""






					_lowercase :    int							=							self * other
					_lowercase :    Any							=							self.euclidean_length() * other.euclidean_length()
					if deg:
						return math.degrees(math.acos(num / den						)						)
					else:
						return math.acos(num / den						)



def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	assert isinstance(__UpperCAmelCase     ,__UpperCAmelCase			)
	return Vector([0] * dimension			)


def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	assert isinstance(__UpperCAmelCase     ,__UpperCAmelCase			) and (isinstance(__UpperCAmelCase     ,__UpperCAmelCase			))
	_lowercase :    str							=							[0] * dimension
	_lowercase :    List[Any]							=							1
	return Vector(__UpperCAmelCase			)


def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	assert (
	    isinstance(__UpperCAmelCase     ,__UpperCAmelCase			)
	    and isinstance(__UpperCAmelCase     ,__UpperCAmelCase			)
	    and (isinstance(__UpperCAmelCase     ,(int, float)			))
	)
	return x * scalar + y


def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	random.seed(__UpperCAmelCase			)
	_lowercase :    Tuple							=							[random.randint(__UpperCAmelCase     ,__UpperCAmelCase			) for _ in range(__UpperCAmelCase			)]
	return Vector(__UpperCAmelCase			)



class  _lowerCamelCase			:







				def __init__( self    :       Dict			,					lowerCamelCase_    :       list[list[float]]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					_lowercase :    Tuple							=							matrix
					_lowercase :    str							=							w
					_lowercase :    Tuple							=							h







				def __str__( self    :       int						):

					"""simple docstring"""






					_lowercase :    Tuple							=							''
					for i in range(self.__height						):
						ans += "|"
						for j in range(self.__width						):
							if j < self.__width - 1:
								ans += str(self.__matrix[i][j]						) + ","
							else:
								ans += str(self.__matrix[i][j]						) + "|\n"
					return ans







				def __add__( self    :       Dict			,					lowerCamelCase_    :       Matrix						):

					"""simple docstring"""






					if self.__width == other.width() and self.__height == other.height():
						_lowercase :    Dict							=							[]
						for i in range(self.__height						):
							_lowercase :    Tuple							=							[
							    self.__matrix[i][j] + other.component(lowerCamelCase_			,					lowerCamelCase_						)
							    for j in range(self.__width						)
							]
							matrix.append(lowerCamelCase_						)
						return Matrix(lowerCamelCase_			,					self.__width			,					self.__height						)
					else:
						raise Exception('matrix must have the same dimension!'						)







				def __sub__( self    :       str			,					lowerCamelCase_    :       Matrix						):

					"""simple docstring"""






					if self.__width == other.width() and self.__height == other.height():
						_lowercase :    List[Any]							=							[]
						for i in range(self.__height						):
							_lowercase :    int							=							[
							    self.__matrix[i][j] - other.component(lowerCamelCase_			,					lowerCamelCase_						)
							    for j in range(self.__width						)
							]
							matrix.append(lowerCamelCase_						)
						return Matrix(lowerCamelCase_			,					self.__width			,					self.__height						)
					else:
						raise Exception('matrices must have the same dimension!'						)







				@overload
				def __mul__( self    :       Union[str, Any]			,					lowerCamelCase_    :       float						):

					"""simple docstring"""






					...







				@overload
				def __mul__( self    :       List[str]			,					lowerCamelCase_    :       Vector						):

					"""simple docstring"""






					...







				def __mul__( self    :       int			,					lowerCamelCase_    :       float | Vector						):

					"""simple docstring"""






					if isinstance(lowerCamelCase_			,					lowerCamelCase_						):  # matrix-vector
						if len(lowerCamelCase_						) == self.__width:
							_lowercase :    Optional[int]							=							zero_vector(self.__height						)
							for i in range(self.__height						):
								_lowercase :    Optional[Any]							=							[
								    self.__matrix[i][j] * other.component(lowerCamelCase_						)
								    for j in range(self.__width						)
								]
								ans.change_component(lowerCamelCase_			,					sum(lowerCamelCase_						)						)
							return ans
						else:
							raise Exception(
							    'vector must have the same size as the '
							    'number of columns of the matrix!'						)
					elif isinstance(lowerCamelCase_			,					(int, float)						):  # matrix-scalar
						_lowercase :    Optional[int]							=							[
						    [self.__matrix[i][j] * other for j in range(self.__width						)]
						    for i in range(self.__height						)
						]
						return Matrix(lowerCamelCase_			,					self.__width			,					self.__height						)
					return None







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

					"""simple docstring"""






					return self.__height







				def 							__UpperCAmelCase					( self    :       List[Any]						):

					"""simple docstring"""






					return self.__width







				def 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if 0 <= x < self.__height and 0 <= y < self.__width:
						return self.__matrix[x][y]
					else:
						raise Exception('change_component: indices out of bounds'						)







				def 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       float						):

					"""simple docstring"""






					if 0 <= x < self.__height and 0 <= y < self.__width:
						_lowercase :    int							=							value
					else:
						raise Exception('change_component: indices out of bounds'						)







				def 							__UpperCAmelCase					( self    :       int			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if self.__height != self.__width:
						raise Exception('Matrix is not square'						)
					_lowercase :    Union[str, Any]							=							self.__matrix[:x] + self.__matrix[x + 1 :]
					for i in range(len(lowerCamelCase_						)						):
						_lowercase :    int							=							minor[i][:y] + minor[i][y + 1 :]
					return Matrix(lowerCamelCase_			,					self.__width - 1			,					self.__height - 1						).determinant()







				def 							__UpperCAmelCase					( self    :       List[str]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       int						):

					"""simple docstring"""






					if self.__height != self.__width:
						raise Exception('Matrix is not square'						)
					if 0 <= x < self.__height and 0 <= y < self.__width:
						return (-1) ** (x + y) * self.minor(lowerCamelCase_			,					lowerCamelCase_						)
					else:
						raise Exception('Indices out of bounds'						)







				def 							__UpperCAmelCase					( self    :       Dict						):

					"""simple docstring"""






					if self.__height != self.__width:
						raise Exception('Matrix is not square'						)
					if self.__height < 1:
						raise Exception('Matrix has no element'						)
					elif self.__height == 1:
						return self.__matrix[0][0]
					elif self.__height == 2:
						return (
						    self.__matrix[0][0] * self.__matrix[1][1]
						    - self.__matrix[0][1] * self.__matrix[1][0]
						)
					else:
						_lowercase :    Union[str, Any]							=							[
						    self.__matrix[0][y] * self.cofactor(0			,					lowerCamelCase_						) for y in range(self.__width						)
						]
						return sum(lowerCamelCase_						)



def      __lowerCAmelCase(		__UpperCAmelCase			):






	"""simple docstring"""





	_lowercase :    list[list[float]]							=							[[0] * n for _ in range(__UpperCAmelCase			)]
	return Matrix(__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			)







def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	random.seed(__UpperCAmelCase			)
	_lowercase :    list[list[float]]							=							[
	    [random.randint(__UpperCAmelCase     ,__UpperCAmelCase			) for _ in range(__UpperCAmelCase			)] for _ in range(__UpperCAmelCase			)
	]
	return Matrix(__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			)



