


import torch
from torch import nn







class 		_snake_case  (      nn.Module							):




 def __init__(		self					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase=1					,  _lowerCamelCase=False    ):
       super().__init__()

       a						:int 							=       n_token
       a						:List[str] 							=       d_embed
       a						:Union[str, Any] 							=       d_proj

       a						:Union[str, Any] 							=       cutoffs + [n_token]
       a						:Optional[int] 							=       [0] + self.cutoffs
       a						:str 							=       div_val

       a						:Dict 							=       self.cutoffs[0]
       a						:Any 							=       len(self.cutoffs    ) - 1
       a						:int 							=       self.shortlist_size + self.n_clusters

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

       a						:Optional[Any] 							=       nn.ModuleList()
       a						:Optional[int] 							=       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(_lowerCamelCase					,  _lowerCamelCase    )    )    )
                   else:
                         self.out_projs.append(_lowerCamelCase    )

             self.out_layers.append(nn.Linear(_lowerCamelCase					,  _lowerCamelCase    )    )
       else:
             for i in range(len(self.cutoffs    )    ):
                   a							,				a						:Tuple 							=       self.cutoff_ends[i], self.cutoff_ends[i + 1]
                   a						:Optional[Any] 							=       d_embed // (div_val**i)

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

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

       a						:Dict 							=       keep_order




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    ):
       if proj is None:
             a						:Any 							=       nn.functional.linear(_lowerCamelCase					,  _lowerCamelCase					,  bias=_lowerCamelCase    )
       else:
             # if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
             a						:Union[str, Any] 							=       nn.functional.linear(_lowerCamelCase					,  proj.t().contiguous()    )
             a						:List[Any] 							=       nn.functional.linear(_lowerCamelCase					,  _lowerCamelCase					,  bias=_lowerCamelCase    )
             # else:
             #     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
             #     if bias is not None:
             #         logit = logit + bias

       return logit




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase					,  _lowerCamelCase=None					,  _lowerCamelCase=False    ):

       if labels is not None:
             # Shift so that tokens < n predict n
             a						:Any 							=       hidden[..., :-1, :].contiguous()
             a						:str 							=       labels[..., 1:].contiguous()
             a						:List[str] 							=       hidden.view(-1					,  hidden.size(-1    )    )
             a						:Optional[Any] 							=       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:
             a						:Tuple 							=       hidden.view(-1					,  hidden.size(-1    )    )

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

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

                   weights.append(_lowerCamelCase    )
                   biases.append(_lowerCamelCase    )

             a							,				a							,				a						:Any 							=       weights[0], biases[0], self.out_projs[0]

             a						:List[Any] 							=       self._compute_logit(_lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    )
             a						:str 							=       nn.functional.log_softmax(_lowerCamelCase					,  dim=1    )

             if labels is None:
                   a						:List[Any] 							=       hidden.new_empty((head_logit.size(0    ), self.n_token)    )
             else:
                   a						:Union[str, Any] 							=       torch.zeros_like(_lowerCamelCase					,  dtype=hidden.dtype					,  device=hidden.device    )

             a						:str 							=       0
             a						:str 							=       [0] + self.cutoffs
             for i in range(len(_lowerCamelCase    ) - 1    ):
                   a							,				a						:int 							=       cutoff_values[i], cutoff_values[i + 1]

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

                         if indices_i.numel() == 0:
                               continue

                         a						:int 							=       labels.index_select(0					,  _lowerCamelCase    ) - l_idx
                         a						:Optional[Any] 							=       head_logprob.index_select(0					,  _lowerCamelCase    )
                         a						:Optional[Any] 							=       hidden.index_select(0					,  _lowerCamelCase    )
                   else:
                         a						:Optional[int] 							=       hidden

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

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

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

       return out




 def        SCREAMING_SNAKE_CASE__				(		self					,  _lowerCamelCase    ):
       if self.n_clusters == 0:
             a						:List[Any] 							=       self._compute_logit(_lowerCamelCase					,  self.out_layers[0].weight					,  self.out_layers[0].bias					,  self.out_projs[0]    )
             return nn.functional.log_softmax(_lowerCamelCase					,  dim=-1    )
       else:
             # construct weights and biases
             a							,				a						:Optional[int] 							=       [], []
             for i in range(len(self.cutoffs    )    ):
                   if self.div_val == 1:
                         a							,				a						:Union[str, Any] 							=       self.cutoff_ends[i], self.cutoff_ends[i + 1]
                         a						:str 							=       self.out_layers[0].weight[l_idx:r_idx]
                         a						:Any 							=       self.out_layers[0].bias[l_idx:r_idx]
                   else:
                         a						:Tuple 							=       self.out_layers[i].weight
                         a						:str 							=       self.out_layers[i].bias

                   if i == 0:
                         a						:List[str] 							=       torch.cat([weight_i, self.cluster_weight]					,  dim=0    )
                         a						:Dict 							=       torch.cat([bias_i, self.cluster_bias]					,  dim=0    )

                   weights.append(_lowerCamelCase    )
                   biases.append(_lowerCamelCase    )

             a							,				a							,				a						:Union[str, Any] 							=       weights[0], biases[0], self.out_projs[0]
             a						:Any 							=       self._compute_logit(_lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    )

             a						:int 							=       hidden.new_empty((head_logit.size(0    ), self.n_token)    )
             a						:Union[str, Any] 							=       nn.functional.log_softmax(_lowerCamelCase					,  dim=1    )

             a						:List[Any] 							=       [0] + self.cutoffs
             for i in range(len(_lowerCamelCase    ) - 1    ):
                   a							,				a						:Dict 							=       cutoff_values[i], cutoff_values[i + 1]

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

                         a						:Optional[Any] 							=       self._compute_logit(_lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase					,  _lowerCamelCase    )
                         a						:Union[str, Any] 							=       nn.functional.log_softmax(_lowerCamelCase					,  dim=1    )

                         a						:Optional[int] 							=       head_logprob[:, -i] + tail_logprob_i
                         a						:List[Any] 							=       logprob_i

             return out









from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding







class 		_snake_case  (      _snake_case							):
 SCREAMING_SNAKE_CASE__      =       'ClapFeatureExtractor'
 SCREAMING_SNAKE_CASE__      =       ('RobertaTokenizer', 'RobertaTokenizerFast')




 def __init__(		self					,  _lowerCamelCase					,  _lowerCamelCase    ):
       super().__init__(_lowerCamelCase					,  _lowerCamelCase    )




 def __call__(		self					,  _lowerCamelCase=None					,  _lowerCamelCase=None					,  _lowerCamelCase=None					,  **_lowerCamelCase    ):
       a						:Dict 							=       kwargs.pop('''sampling_rate'''					,  _lowerCamelCase    )

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

       if text is not None:
             a						:Optional[int] 							=       self.tokenizer(_lowerCamelCase					,  return_tensors=_lowerCamelCase					,  **_lowerCamelCase    )

       if audios is not None:
             a						:Tuple 							=       self.feature_extractor(
                 _lowerCamelCase					,  sampling_rate=_lowerCamelCase					,  return_tensors=_lowerCamelCase					,  **_lowerCamelCase    )

       if text is not None and audios is not None:
             a						:Union[str, Any] 							=       audio_features.input_features
             return encoding
       elif text is not None:
             return encoding
       else:
             return BatchEncoding(data=dict(**_lowerCamelCase    )					,  tensor_type=_lowerCamelCase    )




 def        SCREAMING_SNAKE_CASE__				(		self					,  *_lowerCamelCase					,  **_lowerCamelCase    ):
       return self.tokenizer.batch_decode(*_lowerCamelCase					,  **_lowerCamelCase    )




 def        SCREAMING_SNAKE_CASE__				(		self					,  *_lowerCamelCase					,  **_lowerCamelCase    ):
       return self.tokenizer.decode(*_lowerCamelCase					,  **_lowerCamelCase    )




 @property
 def        SCREAMING_SNAKE_CASE__				(		self    ):
       a						:List[Any] 							=       self.tokenizer.model_input_names
       a						:str 							=       self.feature_extractor.model_input_names
       return list(dict.fromkeys(tokenizer_input_names + feature_extractor_input_names    )    )





