import torch
from torch import nn





class      A							(				nn.Module     ):


   def __init__(self     :						Any       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						str       ,  __UpperCAmelCase     :						Dict=1       ,  __UpperCAmelCase     :						int=False		)						->				List[Any]:
         """simple docstring"""



         super().__init__()

         UpperCAmelCase__								=  n_token
         UpperCAmelCase__								=  d_embed
         UpperCAmelCase__								=  d_proj

         UpperCAmelCase__								=  cutoffs + [n_token]
         UpperCAmelCase__								=  [0] + self.cutoffs
         UpperCAmelCase__								=  div_val

         UpperCAmelCase__								=  self.cutoffs[0]
         UpperCAmelCase__								=  len(self.cutoffs		) - 1
         UpperCAmelCase__								=  self.shortlist_size + self.n_clusters

         if self.n_clusters > 0:
               UpperCAmelCase__								=  nn.Parameter(torch.zeros(self.n_clusters       ,  self.d_embed		)		)
               UpperCAmelCase__								=  nn.Parameter(torch.zeros(self.n_clusters		)		)

         UpperCAmelCase__								=  nn.ModuleList()
         UpperCAmelCase__								=  nn.ParameterList()

         if div_val == 1:
               for i in range(len(self.cutoffs		)		):
                     if d_proj != d_embed:
                           self.out_projs.append(nn.Parameter(torch.FloatTensor(__UpperCAmelCase       ,  __UpperCAmelCase		)		)		)
                     else:
                           self.out_projs.append(__UpperCAmelCase		)

               self.out_layers.append(nn.Linear(__UpperCAmelCase       ,  __UpperCAmelCase		)		)
         else:
               for i in range(len(self.cutoffs		)		):
                     UpperCAmelCase__		,      UpperCAmelCase__								=  self.cutoff_ends[i], self.cutoff_ends[i + 1]
                     UpperCAmelCase__								=  d_embed // (div_val**i)

                     self.out_projs.append(nn.Parameter(torch.FloatTensor(__UpperCAmelCase       ,  __UpperCAmelCase		)		)		)

                     self.out_layers.append(nn.Linear(__UpperCAmelCase       ,  r_idx - l_idx		)		)

         UpperCAmelCase__								=  keep_order


   def 			lowercase_      (self     :						int       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						Any       ,  __UpperCAmelCase     :						List[Any]       ,  __UpperCAmelCase     :						Any		)						->				int:
         """simple docstring"""



         if proj is None:
               UpperCAmelCase__								=  nn.functional.linear(__UpperCAmelCase       ,  __UpperCAmelCase       ,  bias=__UpperCAmelCase		)
         else:
               # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
               UpperCAmelCase__								=  nn.functional.linear(__UpperCAmelCase       ,  proj.t().contiguous()		)
               UpperCAmelCase__								=  nn.functional.linear(__UpperCAmelCase       ,  __UpperCAmelCase       ,  bias=__UpperCAmelCase		)
               # else:
               #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
               #     if bias is not None:
               #         logit = logit + bias

         return logit


   def 			lowercase_      (self     :						List[str]       ,  __UpperCAmelCase     :						List[str]       ,  __UpperCAmelCase     :						Union[str, Any]=None       ,  __UpperCAmelCase     :						str=False		)						->				Tuple:
         """simple docstring"""



         if labels is not None:
               # Shift so that tokens < n predict n
               UpperCAmelCase__								=  hidden[..., :-1, :].contiguous()
               UpperCAmelCase__								=  labels[..., 1:].contiguous()
               UpperCAmelCase__								=  hidden.view(-1       ,  hidden.size(-1		)		)
               UpperCAmelCase__								=  labels.view(-1		)
               if hidden.size(0		) != labels.size(0		):
                     raise RuntimeError("Input and labels should have the same size in the batch dimension."		)
         else:
               UpperCAmelCase__								=  hidden.view(-1       ,  hidden.size(-1		)		)

         if self.n_clusters == 0:
               UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  self.out_layers[0].weight       ,  self.out_layers[0].bias       ,  self.out_projs[0]		)
               if labels is not None:
                     UpperCAmelCase__								=  labels != -1_0_0
                     UpperCAmelCase__								=  torch.zeros_like(__UpperCAmelCase       ,  dtype=hidden.dtype       ,  device=hidden.device		)
                     UpperCAmelCase__								=  (
                         -nn.functional.log_softmax(__UpperCAmelCase       ,  dim=-1		)[mask].gather(1       ,  labels[mask].unsqueeze(1		)		).squeeze(1		)
                     )
               else:
                     UpperCAmelCase__								=  nn.functional.log_softmax(__UpperCAmelCase       ,  dim=-1		)
         else:
               # construct weights and biases
               UpperCAmelCase__		,      UpperCAmelCase__								=  [], []
               for i in range(len(self.cutoffs		)		):
                     if self.div_val == 1:
                           UpperCAmelCase__		,      UpperCAmelCase__								=  self.cutoff_ends[i], self.cutoff_ends[i + 1]
                           UpperCAmelCase__								=  self.out_layers[0].weight[l_idx:r_idx]
                           UpperCAmelCase__								=  self.out_layers[0].bias[l_idx:r_idx]
                     else:
                           UpperCAmelCase__								=  self.out_layers[i].weight
                           UpperCAmelCase__								=  self.out_layers[i].bias

                     if i == 0:
                           UpperCAmelCase__								=  torch.cat([weight_i, self.cluster_weight]       ,  dim=0		)
                           UpperCAmelCase__								=  torch.cat([bias_i, self.cluster_bias]       ,  dim=0		)

                     weights.append(__UpperCAmelCase		)
                     biases.append(__UpperCAmelCase		)

               UpperCAmelCase__		,      UpperCAmelCase__		,      UpperCAmelCase__								=  weights[0], biases[0], self.out_projs[0]

               UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)
               UpperCAmelCase__								=  nn.functional.log_softmax(__UpperCAmelCase       ,  dim=1		)

               if labels is None:
                     UpperCAmelCase__								=  hidden.new_empty((head_logit.size(0		), self.n_token)		)
               else:
                     UpperCAmelCase__								=  torch.zeros_like(__UpperCAmelCase       ,  dtype=hidden.dtype       ,  device=hidden.device		)

               UpperCAmelCase__								=  0
               UpperCAmelCase__								=  [0] + self.cutoffs
               for i in range(len(__UpperCAmelCase		) - 1		):
                     UpperCAmelCase__		,      UpperCAmelCase__								=  cutoff_values[i], cutoff_values[i + 1]

                     if labels is not None:
                           UpperCAmelCase__								=  (labels >= l_idx) & (labels < r_idx)
                           UpperCAmelCase__								=  mask_i.nonzero().squeeze()

                           if indices_i.numel() == 0:
                                 continue

                           UpperCAmelCase__								=  labels.index_select(0       ,  __UpperCAmelCase		) - l_idx
                           UpperCAmelCase__								=  head_logprob.index_select(0       ,  __UpperCAmelCase		)
                           UpperCAmelCase__								=  hidden.index_select(0       ,  __UpperCAmelCase		)
                     else:
                           UpperCAmelCase__								=  hidden

                     if i == 0:
                           if labels is not None:
                                 UpperCAmelCase__								=  head_logprob_i.gather(1       ,  target_i[:, None]		).squeeze(1		)
                           else:
                                 UpperCAmelCase__								=  head_logprob[:, : self.cutoffs[0]]
                     else:
                           UpperCAmelCase__		,      UpperCAmelCase__		,      UpperCAmelCase__								=  weights[i], biases[i], self.out_projs[i]

                           UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)
                           UpperCAmelCase__								=  nn.functional.log_softmax(__UpperCAmelCase       ,  dim=1		)
                           UpperCAmelCase__								=  self.cutoffs[0] + i - 1  # No probability for the head cluster
                           if labels is not None:
                                 UpperCAmelCase__								=  head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
                                     1       ,  target_i[:, None]		).squeeze(1		)
                           else:
                                 UpperCAmelCase__								=  head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
                                 UpperCAmelCase__								=  logprob_i

                     if labels is not None:
                           if (hasattr(self       ,  "keep_order"		) and self.keep_order) or keep_order:
                                 out.index_copy_(0       ,  __UpperCAmelCase       ,  -logprob_i		)
                           else:
                                 out[offset : offset + logprob_i.size(0		)].copy_(-logprob_i		)
                           offset += logprob_i.size(0		)

         return out





   def 			lowercase_      (self     :						Optional[int]       ,  __UpperCAmelCase     :						Any		)						->				str:
         """simple docstring"""



         if self.n_clusters == 0:
               UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  self.out_layers[0].weight       ,  self.out_layers[0].bias       ,  self.out_projs[0]		)
               return nn.functional.log_softmax(__UpperCAmelCase       ,  dim=-1		)
         else:
               # construct weights and biases
               UpperCAmelCase__		,      UpperCAmelCase__								=  [], []
               for i in range(len(self.cutoffs		)		):
                     if self.div_val == 1:
                           UpperCAmelCase__		,      UpperCAmelCase__								=  self.cutoff_ends[i], self.cutoff_ends[i + 1]
                           UpperCAmelCase__								=  self.out_layers[0].weight[l_idx:r_idx]
                           UpperCAmelCase__								=  self.out_layers[0].bias[l_idx:r_idx]
                     else:
                           UpperCAmelCase__								=  self.out_layers[i].weight
                           UpperCAmelCase__								=  self.out_layers[i].bias

                     if i == 0:
                           UpperCAmelCase__								=  torch.cat([weight_i, self.cluster_weight]       ,  dim=0		)
                           UpperCAmelCase__								=  torch.cat([bias_i, self.cluster_bias]       ,  dim=0		)

                     weights.append(__UpperCAmelCase		)
                     biases.append(__UpperCAmelCase		)

               UpperCAmelCase__		,      UpperCAmelCase__		,      UpperCAmelCase__								=  weights[0], biases[0], self.out_projs[0]
               UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)

               UpperCAmelCase__								=  hidden.new_empty((head_logit.size(0		), self.n_token)		)
               UpperCAmelCase__								=  nn.functional.log_softmax(__UpperCAmelCase       ,  dim=1		)

               UpperCAmelCase__								=  [0] + self.cutoffs
               for i in range(len(__UpperCAmelCase		) - 1		):
                     UpperCAmelCase__		,      UpperCAmelCase__								=  cutoff_values[i], cutoff_values[i + 1]

                     if i == 0:
                           UpperCAmelCase__								=  head_logprob[:, : self.cutoffs[0]]
                     else:
                           UpperCAmelCase__		,      UpperCAmelCase__		,      UpperCAmelCase__								=  weights[i], biases[i], self.out_projs[i]

                           UpperCAmelCase__								=  self._compute_logit(__UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase       ,  __UpperCAmelCase		)
                           UpperCAmelCase__								=  nn.functional.log_softmax(__UpperCAmelCase       ,  dim=1		)

                           UpperCAmelCase__								=  head_logprob[:, -i] + tail_logprob_i
                           UpperCAmelCase__								=  logprob_i

               return out










'''simple docstring'''







import warnings
from typing import List

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import is_flax_available, is_tf_available, is_torch_available
class 				__SCREAMING_SNAKE_CASE  (lowerCamelCase_      ):






				"""simple docstring"""







				__a            =['image_processor', 'tokenizer']
				__a            ='OwlViTImageProcessor'
				__a            =('CLIPTokenizer', 'CLIPTokenizerFast')




				def __init__(  self     :					List[Any]					,				__a     :					str=None					,				__a     :					List[str]=None					,				**__a     :					List[Any]					):
								_a								= None
								if "feature_extractor" in kwargs:
												warnings.warn(
												    "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
												    " instead."					,				__a					,				)
												_a								= kwargs.pop("feature_extractor"					)

								_a								= image_processor if image_processor is not None else feature_extractor
								if image_processor is None:
												raise ValueError("You need to specify an `image_processor`."					)
								if tokenizer is None:
												raise ValueError("You need to specify a `tokenizer`."					)

								super().__init__(__a					,				__a					)




				def __call__(  self     :					Union[str, Any]					,				__a     :					Any=None					,				__a     :					List[str]=None					,				__a     :					int=None					,				__a     :					Optional[int]="max_length"					,				__a     :					List[str]="np"					,				**__a     :					Any					):

								if text is None and query_images is None and images is None:
												raise ValueError(
												    "You have to specify at least one text or query image or image. All three cannot be none."					)

								if text is not None:
												if isinstance(__a					,				__a					) or (isinstance(__a					,				__a					) and not isinstance(text[0]					,				__a					)):
																_a								= [self.tokenizer(__a					,				padding=__a					,				return_tensors=__a					,				**__a					)]

												elif isinstance(__a					,				__a					) and isinstance(text[0]					,				__a					):
																_a								= []

																# Maximum number of queries across batch
																_a								= max([len(__a					) for t in text]					)

																# Pad all batch samples to max number of text queries
																for t in text:
																				if len(__a					) != max_num_queries:
																								_a								= t + [" "] * (max_num_queries - len(__a					))

																				_a								= self.tokenizer(__a					,				padding=__a					,				return_tensors=__a					,				**__a					)
																				encodings.append(__a					)
												else:
																raise TypeError("Input text should be a string, a list of strings or a nested list of strings"					)

												if return_tensors == "np":
																_a								= np.concatenate([encoding["input_ids"] for encoding in encodings]					,				axis=0					)
																_a								= np.concatenate([encoding["attention_mask"] for encoding in encodings]					,				axis=0					)

												elif return_tensors == "jax" and is_flax_available():
																import jax.numpy as jnp

																_a								= jnp.concatenate([encoding["input_ids"] for encoding in encodings]					,				axis=0					)
																_a								= jnp.concatenate([encoding["attention_mask"] for encoding in encodings]					,				axis=0					)

												elif return_tensors == "pt" and is_torch_available():
																import torch

																_a								= torch.cat([encoding["input_ids"] for encoding in encodings]					,				dim=0					)
																_a								= torch.cat([encoding["attention_mask"] for encoding in encodings]					,				dim=0					)

												elif return_tensors == "tf" and is_tf_available():
																import tensorflow as tf

																_a								= tf.stack([encoding["input_ids"] for encoding in encodings]					,				axis=0					)
																_a								= tf.stack([encoding["attention_mask"] for encoding in encodings]					,				axis=0					)

												else:
																raise ValueError("Target return tensor type could not be returned"					)

												_a								= BatchEncoding()
												_a								= input_ids
												_a								= attention_mask

								if query_images is not None:
												_a								= BatchEncoding()
												_a								= self.image_processor(
												    __a					,				return_tensors=__a					,				**__a					).pixel_values
												_a								= query_pixel_values

								if images is not None:
												_a								= self.image_processor(__a					,				return_tensors=__a					,				**__a					)

								if text is not None and images is not None:
												_a								= image_features.pixel_values
												return encoding
								elif query_images is not None and images is not None:
												_a								= image_features.pixel_values
												return encoding
								elif text is not None or query_images is not None:
												return encoding
								else:
												return BatchEncoding(data=dict(**__a					)					,				tensor_type=__a					)




				def   UpperCamelCase__   (  self     :					List[str]					,				*__a     :					Union[str, Any]					,				**__a     :					int					):
								return self.image_processor.post_process(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					Optional[int]					,				*__a     :					Optional[Any]					,				**__a     :					List[str]					):
								return self.image_processor.post_process_object_detection(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					Optional[Any]					,				*__a     :					Dict					,				**__a     :					Union[str, Any]					):
								return self.image_processor.post_process_image_guided_detection(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					str					,				*__a     :					Tuple					,				**__a     :					Tuple					):
								return self.tokenizer.batch_decode(*__a					,				**__a					)




				def   UpperCamelCase__   (  self     :					List[str]					,				*__a     :					List[Any]					,				**__a     :					Optional[int]					):
								return self.tokenizer.decode(*__a					,				**__a					)




				@property
				def   UpperCamelCase__   (  self     :					List[str]					):
								warnings.warn(
								    "`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead."					,				__a					,				)
								return self.image_processor_class




				@property
				def   UpperCamelCase__   (  self     :					str					):
								warnings.warn(
								    "`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead."					,				__a					,				)
								return self.image_processor
