



"""simple docstring"""





import torch
from torch import nn
class  lowercase__  (       nn.Module ):







       '''simple docstring'''


       def __init__(				self     :    str						,							_UpperCAmelCase     :    List[str]						,							_UpperCAmelCase     :    Any						,							_UpperCAmelCase     :    int						,							_UpperCAmelCase     :    Union[str, Any]						,							_UpperCAmelCase     :    Optional[int]=1						,							_UpperCAmelCase     :    List[str]=False       )					->		Tuple:


             '''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     :    Union[str, Any]						,							_UpperCAmelCase     :    int						,							_UpperCAmelCase     :    int						,							_UpperCAmelCase     :    Tuple						,							_UpperCAmelCase     :    Optional[int]       )					->		str:


             '''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     :    Dict						,							_UpperCAmelCase     :    Tuple						,							_UpperCAmelCase     :    List[Any]=None						,							_UpperCAmelCase     :    int=False       )					->		Optional[int]:


             '''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 != -100
                         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     :    List[Any]						,							_UpperCAmelCase     :    List[str]       )					->		List[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









import warnings

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
class    __UpperCamelCase			(       _lowercase				):

 """simple docstring"""



 _lowercase   :    Optional[Any]      						=					['''image_processor''', '''tokenizer''']
 _lowercase   :    Optional[int]      						=					'''ChineseCLIPImageProcessor'''
 _lowercase   :    List[Any]      						=					('''BertTokenizer''', '''BertTokenizerFast''')

 def __init__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			Union[str, 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.'''		,				SCREAMING_SNAKE_CASE		,				)
   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__(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
  a__												= self.image_processor

 def __call__(	self		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:

  if text is None and images is None:
   raise ValueError('''You have to specify either text or images. Both cannot be none.'''      )

  if text is not None:
   a__												= self.tokenizer(SCREAMING_SNAKE_CASE		,				return_tensors=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

  if images is not None:
   a__												= self.image_processor(SCREAMING_SNAKE_CASE		,				return_tensors=SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

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

 def 						_UpperCAmelCase       (	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:
  return self.tokenizer.batch_decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 def 						_UpperCAmelCase       (	self		,				*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      ) ->			int:
  return self.tokenizer.decode(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )

 @property
 def 						_UpperCAmelCase       (	self      ) ->			List[str]:
  a__												= self.tokenizer.model_input_names
  a__												= self.image_processor.model_input_names
  return list(dict.fromkeys(tokenizer_input_names + image_processor_input_names      )      )



 @property
 def 						_UpperCAmelCase       (	self      ) ->			Optional[int]:
  warnings.warn(
      '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''		,				SCREAMING_SNAKE_CASE		,				)
  return self.image_processor_class





