'''simple docstring'''




import torch
from torch import nn

class 	lowerCamelCase_	(nn.Module    ):
      '''simple docstring'''

      def __init__(       self						:				Optional[Any]						,					A						:				Dict						,					A						:				Tuple						,					A						:				Optional[Any]						,					A						:				Tuple						,					A						:				Union[str, Any]=1						,					A						:				str=False       ):
             super().__init__()

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

             _UpperCAmelCase     :  Union[str, Any]							      =    cutoffs + [n_token]
             _UpperCAmelCase     :  str							      =    [0] + self.cutoffs
             _UpperCAmelCase     :  Dict							      =    div_val

             _UpperCAmelCase     :  Tuple							      =    self.cutoffs[0]
             _UpperCAmelCase     :  Tuple							      =    len(self.cutoffs       ) - 1
             _UpperCAmelCase     :  Tuple							      =    self.shortlist_size + self.n_clusters

             if self.n_clusters > 0:
                    _UpperCAmelCase     :  Any							      =    nn.Parameter(torch.zeros(self.n_clusters						,					self.d_embed       )       )
                    _UpperCAmelCase     :  Optional[int]							      =    nn.Parameter(torch.zeros(self.n_clusters       )       )

             _UpperCAmelCase     :  str							      =    nn.ModuleList()
             _UpperCAmelCase     :  Dict							      =    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(A						,					A       )       )       )
                           else:
                                  self.out_projs.append(A       )

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

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

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

             _UpperCAmelCase     :  Dict							      =    keep_order




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

             return logit




      def      _A	(       self						:				Optional[int]						,					A						:				Tuple						,					A						:				Union[str, Any]=None						,					A						:				Any=False       ):

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

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

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

                           weights.append(A       )
                           biases.append(A       )

                    _UpperCAmelCase      ,  _UpperCAmelCase      ,  _UpperCAmelCase     :  List[str]							      =    weights[0], biases[0], self.out_projs[0]

                    _UpperCAmelCase     :  Dict							      =    self._compute_logit(A						,					A						,					A						,					A       )
                    _UpperCAmelCase     :  List[str]							      =    nn.functional.log_softmax(A						,					dim=1       )

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

                    _UpperCAmelCase     :  Dict							      =    0
                    _UpperCAmelCase     :  Any							      =    [0] + self.cutoffs
                    for i in range(len(A       ) - 1       ):
                           _UpperCAmelCase      ,  _UpperCAmelCase     :  str							      =    cutoff_values[i], cutoff_values[i + 1]

                           if labels is not None:
                                  _UpperCAmelCase     :  List[Any]							      =    (labels >= l_idx) & (labels < r_idx)
                                  _UpperCAmelCase     :  Any							      =    mask_i.nonzero().squeeze()

                                  if indices_i.numel() == 0:
                                         continue

                                  _UpperCAmelCase     :  Union[str, Any]							      =    labels.index_select(0						,					A       ) - l_idx
                                  _UpperCAmelCase     :  Any							      =    head_logprob.index_select(0						,					A       )
                                  _UpperCAmelCase     :  Dict							      =    hidden.index_select(0						,					A       )
                           else:
                                  _UpperCAmelCase     :  List[Any]							      =    hidden

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

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

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

             return out





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

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

                           weights.append(A       )
                           biases.append(A       )

                    _UpperCAmelCase      ,  _UpperCAmelCase      ,  _UpperCAmelCase     :  Optional[int]							      =    weights[0], biases[0], self.out_projs[0]
                    _UpperCAmelCase     :  Optional[Any]							      =    self._compute_logit(A						,					A						,					A						,					A       )

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

                    _UpperCAmelCase     :  Optional[Any]							      =    [0] + self.cutoffs
                    for i in range(len(A       ) - 1       ):
                           _UpperCAmelCase      ,  _UpperCAmelCase     :  List[str]							      =    cutoff_values[i], cutoff_values[i + 1]

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

                                  _UpperCAmelCase     :  int							      =    self._compute_logit(A						,					A						,					A						,					A       )
                                  _UpperCAmelCase     :  List[str]							      =    nn.functional.log_softmax(A						,					dim=1       )

                                  _UpperCAmelCase     :  Optional[Any]							      =    head_logprob[:, -i] + tail_logprob_i
                                  _UpperCAmelCase     :  Any							      =    logprob_i

                    return out


'''simple docstring'''




import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_distilbert import DistilBertTokenizer


__SCREAMING_SNAKE_CASE       :     str     =  logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE       :     str     =  {"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

__SCREAMING_SNAKE_CASE       :     Dict     =  {
    """vocab_file""": {
        """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/vocab.txt""",
        """distilbert-base-uncased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/vocab.txt"""
        ),
        """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/vocab.txt""",
        """distilbert-base-cased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/vocab.txt"""
        ),
        """distilbert-base-german-cased""": """https://huggingface.co/distilbert-base-german-cased/resolve/main/vocab.txt""",
        """distilbert-base-multilingual-cased""": (
            """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/vocab.txt"""
        ),
    },
    """tokenizer_file""": {
        """distilbert-base-uncased""": """https://huggingface.co/distilbert-base-uncased/resolve/main/tokenizer.json""",
        """distilbert-base-uncased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-cased""": """https://huggingface.co/distilbert-base-cased/resolve/main/tokenizer.json""",
        """distilbert-base-cased-distilled-squad""": (
            """https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-german-cased""": (
            """https://huggingface.co/distilbert-base-german-cased/resolve/main/tokenizer.json"""
        ),
        """distilbert-base-multilingual-cased""": (
            """https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/tokenizer.json"""
        ),
    },
}

__SCREAMING_SNAKE_CASE       :     Optional[Any]     =  {
    """distilbert-base-uncased""": 512,
    """distilbert-base-uncased-distilled-squad""": 512,
    """distilbert-base-cased""": 512,
    """distilbert-base-cased-distilled-squad""": 512,
    """distilbert-base-german-cased""": 512,
    """distilbert-base-multilingual-cased""": 512,
}


__SCREAMING_SNAKE_CASE       :     List[Any]     =  {
    """distilbert-base-uncased""": {"""do_lower_case""": True},
    """distilbert-base-uncased-distilled-squad""": {"""do_lower_case""": True},
    """distilbert-base-cased""": {"""do_lower_case""": False},
    """distilbert-base-cased-distilled-squad""": {"""do_lower_case""": False},
    """distilbert-base-german-cased""": {"""do_lower_case""": False},
    """distilbert-base-multilingual-cased""": {"""do_lower_case""": False},
}

class 	lowerCamelCase_	(snake_case__    ):
      '''simple docstring'''

      __UpperCamelCase:  Union[str, Any]						=						VOCAB_FILES_NAMES
      __UpperCamelCase:  str						=						PRETRAINED_VOCAB_FILES_MAP
      __UpperCamelCase:  Optional[Any]						=						PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
      __UpperCamelCase:  Any						=						PRETRAINED_INIT_CONFIGURATION
      __UpperCamelCase:  str						=						["input_ids", "attention_mask"]
      __UpperCamelCase:  List[str]						=						DistilBertTokenizer




      def __init__(       self						:				str						,					A						:				int=None						,					A						:				Tuple=None						,					A						:				Tuple=True						,					A						:				Dict="[UNK]"						,					A						:				List[Any]="[SEP]"						,					A						:				Optional[Any]="[PAD]"						,					A						:				Dict="[CLS]"						,					A						:				Tuple="[MASK]"						,					A						:				str=True						,					A						:				Dict=None						,					**A						:				List[Any]						,					):
             super().__init__(
                 A						,					tokenizer_file=A						,					do_lower_case=A						,					unk_token=A						,					sep_token=A						,					pad_token=A						,					cls_token=A						,					mask_token=A						,					tokenize_chinese_chars=A						,					strip_accents=A						,					**A						,					)

             _UpperCAmelCase     :  str							      =    json.loads(self.backend_tokenizer.normalizer.__getstate__()       )
             if (
                 normalizer_state.get("lowercase"						,					A       ) != do_lower_case
                 or normalizer_state.get("strip_accents"						,					A       ) != strip_accents
                 or normalizer_state.get("handle_chinese_chars"						,					A       ) != tokenize_chinese_chars
             ):
                    _UpperCAmelCase     :  Dict							      =    getattr(A						,					normalizer_state.pop("type"       )       )
                    _UpperCAmelCase     :  int							      =    do_lower_case
                    _UpperCAmelCase     :  Optional[int]							      =    strip_accents
                    _UpperCAmelCase     :  str							      =    tokenize_chinese_chars
                    _UpperCAmelCase     :  List[Any]							      =    normalizer_class(**A       )

             _UpperCAmelCase     :  Dict							      =    do_lower_case




      def      _A	(       self						:				List[Any]						,					A						:				Tuple						,					A						:				Any=None       ):
             _UpperCAmelCase     :  Optional[int]							      =    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

             if token_ids_a:
                    output += token_ids_a + [self.sep_token_id]

             return output




      def      _A	(       self						:				int						,					A						:				List[int]						,					A						:				Optional[List[int]] = None       ):
             _UpperCAmelCase     :  Any							      =    [self.sep_token_id]
             _UpperCAmelCase     :  str							      =    [self.cls_token_id]
             if token_ids_a is None:
                    return len(cls + token_ids_a + sep       ) * [0]
             return len(cls + token_ids_a + sep       ) * [0] + len(token_ids_a + sep       ) * [1]





      def      _A	(       self						:				Dict						,					A						:				str						,					A						:				Optional[str] = None       ):
             _UpperCAmelCase     :  Any							      =    self._tokenizer.model.save(A						,					name=A       )
             return tuple(A       )

