






'''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


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 UpperCAmelCase_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      '''owlvit_text_model'''






      def __init__(			self      :		Tuple  ,   UpperCAmelCase      :		int=4_9408  ,   UpperCAmelCase      :		int=512  ,   UpperCAmelCase      :		List[Any]=2048  ,   UpperCAmelCase      :		Any=12  ,   UpperCAmelCase      :		Dict=8  ,   UpperCAmelCase      :		Any=16  ,   UpperCAmelCase      :		List[str]="quick_gelu"  ,   UpperCAmelCase      :		Optional[int]=1e-5  ,   UpperCAmelCase      :		Optional[Any]=0.0  ,   UpperCAmelCase      :		Tuple=0.0_2  ,   UpperCAmelCase      :		Any=1.0  ,   UpperCAmelCase      :		List[str]=0  ,   UpperCAmelCase      :		int=4_9406  ,   UpperCAmelCase      :		Optional[Any]=4_9407  ,   **UpperCAmelCase      :		str  ,   )	->	Optional[int]:


       '''simple docstring'''

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

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






      @classmethod
      def 					A__   (			cls      :		Optional[int]  ,   UpperCAmelCase      :		Union[str, os.PathLike]  ,   **UpperCAmelCase      :		Optional[int]		)	->	"PretrainedConfig":


       '''simple docstring'''

       cls._set_token_in_kwargs(UpperCAmelCase		)

       lowercase	,		lowercase       : Tuple						=cls.get_config_dict(UpperCAmelCase  ,   **UpperCAmelCase		)

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





class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      '''owlvit_vision_model'''






      def __init__(			self      :		Optional[Any]  ,   UpperCAmelCase      :		Tuple=768  ,   UpperCAmelCase      :		int=3072  ,   UpperCAmelCase      :		Optional[Any]=12  ,   UpperCAmelCase      :		Optional[Any]=12  ,   UpperCAmelCase      :		int=3  ,   UpperCAmelCase      :		Tuple=768  ,   UpperCAmelCase      :		str=32  ,   UpperCAmelCase      :		List[Any]="quick_gelu"  ,   UpperCAmelCase      :		str=1e-5  ,   UpperCAmelCase      :		Any=0.0  ,   UpperCAmelCase      :		Any=0.0_2  ,   UpperCAmelCase      :		Dict=1.0  ,   **UpperCAmelCase      :		List[Any]  ,   )	->	Optional[int]:


       '''simple docstring'''

       super().__init__(**UpperCAmelCase		)

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






      @classmethod
      def 					A__   (			cls      :		str  ,   UpperCAmelCase      :		Union[str, os.PathLike]  ,   **UpperCAmelCase      :		Optional[Any]		)	->	"PretrainedConfig":


       '''simple docstring'''

       cls._set_token_in_kwargs(UpperCAmelCase		)

       lowercase	,		lowercase       : int						=cls.get_config_dict(UpperCAmelCase  ,   **UpperCAmelCase		)

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





class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      '''owlvit'''
      UpperCamelCase_             =      True






      def __init__(			self      :		List[Any]  ,   UpperCAmelCase      :		Optional[int]=None  ,   UpperCAmelCase      :		Optional[Any]=None  ,   UpperCAmelCase      :		str=512  ,   UpperCAmelCase      :		Optional[int]=2.6_5_9_2  ,   UpperCAmelCase      :		List[str]=True  ,   **UpperCAmelCase      :		str  ,   )	->	int:


       '''simple docstring'''

       super().__init__(**UpperCAmelCase		)

       if text_config is None:
        lowercase       : Union[str, Any]						={}
        logger.info('''text_config is None. Initializing the OwlViTTextConfig with default values.'''		)

       if vision_config is None:
        lowercase       : int						={}
        logger.info('''vision_config is None. initializing the OwlViTVisionConfig with default values.'''		)

       lowercase       : List[Any]						=OwlViTTextConfig(**UpperCAmelCase		)
       lowercase       : List[str]						=OwlViTVisionConfig(**UpperCAmelCase		)

       lowercase       : Tuple						=projection_dim
       lowercase       : str						=logit_scale_init_value
       lowercase       : Optional[Any]						=return_dict
       lowercase       : int						=1.0






      @classmethod
      def 					A__   (			cls      :		Dict  ,   UpperCAmelCase      :		Union[str, os.PathLike]  ,   **UpperCAmelCase      :		Dict		)	->	"PretrainedConfig":


       '''simple docstring'''

       cls._set_token_in_kwargs(UpperCAmelCase		)

       lowercase	,		lowercase       : Optional[int]						=cls.get_config_dict(UpperCAmelCase  ,   **UpperCAmelCase		)

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






      @classmethod
      def 					A__   (			cls      :		Union[str, Any]  ,   UpperCAmelCase      :		Dict  ,   UpperCAmelCase      :		Dict  ,   **UpperCAmelCase      :		Any		)	->	Optional[int]:


       '''simple docstring'''

       lowercase       : Optional[int]						={}
       lowercase       : Any						=text_config
       lowercase       : Any						=vision_config

       return cls.from_dict(UpperCAmelCase  ,   **UpperCAmelCase		)






      def 					A__   (			self      :		str		)	->	Optional[Any]:


       '''simple docstring'''

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





class UpperCAmelCase_       (     __A					):




      """simple docstring"""




      @property
      def 					A__   (			self      :		int		)	->	Mapping[str, Mapping[int, 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 					A__   (			self      :		str		)	->	Mapping[str, Mapping[int, str]]:


       '''simple docstring'''

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






      @property
      def 					A__   (			self      :		int		)	->	float:


       '''simple docstring'''

       return 1e-4






      def 					A__   (			self      :		Dict  ,   UpperCAmelCase      :		"ProcessorMixin"  ,   UpperCAmelCase      :		int = -1  ,   UpperCAmelCase      :		int = -1  ,   UpperCAmelCase      :		Optional["TensorType"] = None  ,   )	->	Mapping[str, Any]:


       '''simple docstring'''

       lowercase       : Union[str, Any]						=super().generate_dummy_inputs(
           processor.tokenizer  ,   batch_size=UpperCAmelCase  ,   seq_length=UpperCAmelCase  ,   framework=UpperCAmelCase		)
       lowercase       : Optional[int]						=super().generate_dummy_inputs(
           processor.image_processor  ,   batch_size=UpperCAmelCase  ,   framework=UpperCAmelCase		)
       return {**text_input_dict, **image_input_dict}






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


       '''simple docstring'''

       return 14





"""simple docstring"""





import itertools
import json
import linecache
import os
import pickle
import re
import socket
import string
from collections import Counter
from logging import getLogger
from pathlib import Path
from typing import Callable, Dict, Iterable, List

import git
import torch
from torch.utils.data import Dataset

from transformers import BartTokenizer, RagTokenizer, TaTokenizer
def 				A_  (__a					, __a					, __a					, __a					, __a=True					, __a="pt"					):

					'''simple docstring'''





					A_								=    {"add_prefix_space": True} if isinstance(__a					, __a					) and not line.startswith(" "					) else {}
					A_								=    padding_side
					return tokenizer(
					    [line]					, max_length=__a					, padding="max_length" if pad_to_max_length else None					, truncation=__a					, return_tensors=__a					, add_special_tokens=__a					, **__a					, )
def 				A_  (__a					, __a					, __a=None					, ):

					'''simple docstring'''





					A_								=    input_ids.ne(__a					).any(dim=0					)
					if attention_mask is None:
										return input_ids[:, keep_column_mask]
					else:
										return (input_ids[:, keep_column_mask], attention_mask[:, keep_column_mask])



class __lowerCAmelCase					(       _lowercase   ):




			"""simple docstring"""







			def __init__(					self    :       Optional[Any]							, _snake_case    :       List[Any]							, _snake_case    :       List[Any]							, _snake_case    :       int							, _snake_case    :       Tuple							, _snake_case    :       Dict="train"							, _snake_case    :       List[Any]=None							, _snake_case    :       Optional[int]=None							, _snake_case    :       Optional[int]=None							, _snake_case    :       Any=""							, )   ->			List[str]:







								"""simple docstring"""
								super().__init__()
								A_								=    Path(_snake_case						).joinpath(type_path + ".source"						)
								A_								=    Path(_snake_case						).joinpath(type_path + ".target"						)
								A_								=    self.get_char_lens(self.src_file						)
								A_								=    max_source_length
								A_								=    max_target_length
								assert min(self.src_lens						) > 0, F'found empty line in {self.src_file}'
								A_								=    tokenizer
								A_								=    prefix
								if n_obs is not None:
													A_								=    self.src_lens[:n_obs]
								A_								=    src_lang
								A_								=    tgt_lang




			def __len__(					self    :       Optional[int]						)   ->			List[str]:







								"""simple docstring"""
								return len(self.src_lens						)




			def __getitem__(					self    :       str							, _snake_case    :       Optional[int]						)   ->			Dict[str, torch.Tensor]:







								"""simple docstring"""
								A_								=    index + 1  # linecache starts at 1
								A_								=    self.prefix + linecache.getline(str(self.src_file						)							, _snake_case						).rstrip("\n"						)
								A_								=    linecache.getline(str(self.tgt_file						)							, _snake_case						).rstrip("\n"						)
								assert source_line, F'empty source line for index {index}'
								assert tgt_line, F'empty tgt line for index {index}'

								# Need to add eos token manually for T5
								if isinstance(self.tokenizer							, _snake_case						):
													source_line += self.tokenizer.eos_token
													tgt_line += self.tokenizer.eos_token

								# Pad source and target to the right
								A_								=    (
								    self.tokenizer.question_encoder if isinstance(self.tokenizer							, _snake_case						) else self.tokenizer
								)
								A_								=    self.tokenizer.generator if isinstance(self.tokenizer							, _snake_case						) else self.tokenizer

								A_								=    encode_line(_snake_case							, _snake_case							, self.max_source_length							, "right"						)
								A_								=    encode_line(_snake_case							, _snake_case							, self.max_target_length							, "right"						)

								A_								=    source_inputs["input_ids"].squeeze()
								A_								=    target_inputs["input_ids"].squeeze()
								A_								=    source_inputs["attention_mask"].squeeze()
								return {
								    "input_ids": source_ids,
								    "attention_mask": src_mask,
								    "decoder_input_ids": target_ids,
								}




			@staticmethod
			def   lowerCamelCase__  (					_snake_case    :       List[Any]						)   ->			str:







								"""simple docstring"""
								return [len(_snake_case						) for x in Path(_snake_case						).open().readlines()]






			def   lowerCamelCase__  (					self    :       Any							, _snake_case    :       List[str]						)   ->			Dict[str, torch.Tensor]:







								"""simple docstring"""
								A_								=    torch.stack([x["input_ids"] for x in batch]						)
								A_								=    torch.stack([x["attention_mask"] for x in batch]						)
								A_								=    torch.stack([x["decoder_input_ids"] for x in batch]						)
								A_								=    (
								    self.tokenizer.generator.pad_token_id
								    if isinstance(self.tokenizer							, _snake_case						)
								    else self.tokenizer.pad_token_id
								)
								A_								=    (
								    self.tokenizer.question_encoder.pad_token_id
								    if isinstance(self.tokenizer							, _snake_case						)
								    else self.tokenizer.pad_token_id
								)
								A_								=    trim_batch(_snake_case							, _snake_case						)
								A_			,     A_								=    trim_batch(_snake_case							, _snake_case							, attention_mask=_snake_case						)
								A_								=    {
								    "input_ids": source_ids,
								    "attention_mask": source_mask,
								    "decoder_input_ids": y,
								}
								return batch





UpperCamelCase_       :    Any						      =			getLogger(__name__)
def 				A_  (__a					):

					'''simple docstring'''





					return list(itertools.chain.from_iterable(__a					)					)
def 				A_  (__a					):

					'''simple docstring'''





					A_								=    get_git_info()
					save_json(__a					, os.path.join(__a					, "git_log.json"					)					)
def 				A_  (__a					, __a					, __a=4					, **__a					):

					'''simple docstring'''





					with open(__a					, "w"					) as f:
										json.dump(__a					, __a					, indent=__a					, **__a					)
def 				A_  (__a					):

					'''simple docstring'''





					with open(__a					) as f:
										return json.load(__a					)
def 				A_  ():

					'''simple docstring'''





					A_								=    git.Repo(search_parent_directories=__a					)
					A_								=    {
					    "repo_id": str(__a					),
					    "repo_sha": str(repo.head.object.hexsha					),
					    "repo_branch": str(repo.active_branch					),
					    "hostname": str(socket.gethostname()					),
					}
					return repo_infos
def 				A_  (__a					, __a					):

					'''simple docstring'''





					return list(map(__a					, __a					)					)
def 				A_  (__a					, __a					):

					'''simple docstring'''





					with open(__a					, "wb"					) as f:
										return pickle.dump(__a					, __a					)
def 				A_  (__a					):

					'''simple docstring'''





					def remove_articles(__a					):
										return re.sub(R"\b(a|an|the)\b"					, " "					, __a					)

					def white_space_fix(__a					):
										return " ".join(text.split()					)

					def remove_punc(__a					):
										A_								=    set(string.punctuation					)
										return "".join(ch for ch in text if ch not in exclude					)

					def lower(__a					):
										return text.lower()

					return white_space_fix(remove_articles(remove_punc(lower(__a					)					)					)					)
def 				A_  (__a					, __a					):

					'''simple docstring'''





					A_								=    normalize_answer(__a					).split()
					A_								=    normalize_answer(__a					).split()
					A_								=    Counter(__a					) & Counter(__a					)
					A_								=    sum(common.values()					)
					if num_same == 0:
										return 0
					A_								=    1.0 * num_same / len(__a					)
					A_								=    1.0 * num_same / len(__a					)
					A_								=    (2 * precision * recall) / (precision + recall)
					return fa
def 				A_  (__a					, __a					):

					'''simple docstring'''





					return normalize_answer(__a					) == normalize_answer(__a					)
def 				A_  (__a					, __a					):

					'''simple docstring'''





					assert len(__a					) == len(__a					)
					A_								=    0
					for hypo, pred in zip(__a					, __a					):
										em += exact_match_score(__a					, __a					)
					if len(__a					) > 0:
										em /= len(__a					)
					return {"em": em}
def 				A_  (__a					):

					'''simple docstring'''





					return model_prefix.startswith("rag"					)




def 				A_  (__a					, __a					, __a					):

					'''simple docstring'''





					A_								=    {p: p for p in extra_params}
					# T5 models don't have `dropout` param, they have `dropout_rate` instead
					A_								=    "dropout_rate"
					for p in extra_params:
										if getattr(__a					, __a					, __a					):
															if not hasattr(__a					, __a					) and not hasattr(__a					, equivalent_param[p]					):
																				logger.info("config doesn't have a `{}` attribute".format(__a					)					)
																				delattr(__a					, __a					)
																				continue
															A_								=    p if hasattr(__a					, __a					) else equivalent_param[p]
															setattr(__a					, __a					, getattr(__a					, __a					)					)
															delattr(__a					, __a					)
					return hparams, config




