


import json
import os
from functools import lru_cache
from typing import List, Optional, Tuple

import regex as re

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import logging


__UpperCamelCase    :						Optional[int]     =							logging.get_logger(__name__)


__UpperCamelCase    :						Any     =							{'vocab_file': 'vocab.json', 'merges_file': 'merges.txt'}

# See all BART models at https://huggingface.co/models?filter=bart
__UpperCamelCase    :						Tuple     =							{
    'vocab_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/vocab.json',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/vocab.json',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/vocab.json',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/vocab.json',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/vocab.json',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/vocab.json',
    },
    'merges_file': {
        'facebook/bart-base': 'https://huggingface.co/facebook/bart-base/resolve/main/merges.txt',
        'facebook/bart-large': 'https://huggingface.co/facebook/bart-large/resolve/main/merges.txt',
        'facebook/bart-large-mnli': 'https://huggingface.co/facebook/bart-large-mnli/resolve/main/merges.txt',
        'facebook/bart-large-cnn': 'https://huggingface.co/facebook/bart-large-cnn/resolve/main/merges.txt',
        'facebook/bart-large-xsum': 'https://huggingface.co/facebook/bart-large-xsum/resolve/main/merges.txt',
        'yjernite/bart_eli5': 'https://huggingface.co/yjernite/bart_eli5/resolve/main/merges.txt',
    },
}

__UpperCamelCase    :						int     =							{
    'facebook/bart-base': 1024,
    'facebook/bart-large': 1024,
    'facebook/bart-large-mnli': 1024,
    'facebook/bart-large-cnn': 1024,
    'facebook/bart-large-xsum': 1024,
    'yjernite/bart_eli5': 1024,
}





@lru_cache()
def   _UpperCAmelCase       (     ):
      """simple docstring"""




      __lowerCamelCase     :      Optional[int]       						=						(
          list(range(ord("""!"""      )						,	ord("""~"""      ) + 1      )      ) + list(range(ord("""¡"""      )						,	ord("""¬"""      ) + 1      )      ) + list(range(ord("""®"""      )						,	ord("""ÿ"""      ) + 1      )      )
      )
      __lowerCamelCase     :      Optional[int]       						=						bs[:]
      __lowerCamelCase     :      Dict       						=						0
      for b in range(2**8      ):
            if b not in bs:
                  bs.append(UpperCAmelCase      )
                  cs.append(2**8 + n      )
                  n += 1
      __lowerCamelCase     :      str       						=						[chr(UpperCAmelCase      ) for n in cs]
      return dict(zip(UpperCAmelCase						,	UpperCAmelCase      )      )





def   _UpperCAmelCase       (     UpperCAmelCase      :			int      ):
      """simple docstring"""




      __lowerCamelCase     :      Any       						=						set()
      __lowerCamelCase     :      Dict       						=						word[0]
      for char in word[1:]:
            pairs.add((prev_char, char)      )
            __lowerCamelCase     :      Any       						=						char
      return pairs




class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							str    = VOCAB_FILES_NAMES
   a_				:							str    = PRETRAINED_VOCAB_FILES_MAP
   a_				:							List[str]    = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   a_				:							List[Any]    = ["input_ids", "attention_mask"]

   def __init__(  self  : List[Any]       ,  _lowerCamelCase  : Union[str, Any]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : List[Any]="replace"       ,  _lowerCamelCase  : Optional[Any]="<s>"       ,  _lowerCamelCase  : Union[str, Any]="</s>"       ,  _lowerCamelCase  : List[Any]="</s>"       ,  _lowerCamelCase  : List[Any]="<s>"       ,  _lowerCamelCase  : Union[str, Any]="<unk>"       ,  _lowerCamelCase  : str="<pad>"       ,  _lowerCamelCase  : List[str]="<mask>"       ,  _lowerCamelCase  : Union[str, Any]=False       ,  **_lowerCamelCase  : Union[str, Any]       ,  ):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else bos_token
         __lowerCamelCase     :      Optional[int]       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else eos_token
         __lowerCamelCase     :      Optional[Any]       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else sep_token
         __lowerCamelCase     :      Tuple       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else cls_token
         __lowerCamelCase     :      int       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else unk_token
         __lowerCamelCase     :      List[str]       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else pad_token

         # Mask token behave like a normal word, i.e. include the space before it
         __lowerCamelCase     :      Optional[Any]       						=						AddedToken(_lowerCamelCase       ,  lstrip=_lowerCamelCase       ,  rstrip=_lowerCamelCase							) if isinstance(_lowerCamelCase       ,  _lowerCamelCase							) else mask_token

         super().__init__(
             errors=_lowerCamelCase       ,  bos_token=_lowerCamelCase       ,  eos_token=_lowerCamelCase       ,  unk_token=_lowerCamelCase       ,  sep_token=_lowerCamelCase       ,  cls_token=_lowerCamelCase       ,  pad_token=_lowerCamelCase       ,  mask_token=_lowerCamelCase       ,  add_prefix_space=_lowerCamelCase       ,  **_lowerCamelCase       ,  )

         with open(_lowerCamelCase       ,  encoding="""utf-8"""							) as vocab_handle:
               __lowerCamelCase     :      Union[str, Any]       						=						json.load(_lowerCamelCase							)
         __lowerCamelCase     :      Optional[int]       						=						{v: k for k, v in self.encoder.items()}
         __lowerCamelCase     :      Optional[Any]       						=						errors  # how to handle errors in decoding
         __lowerCamelCase     :      int       						=						bytes_to_unicode()
         __lowerCamelCase     :      List[str]       						=						{v: k for k, v in self.byte_encoder.items()}
         with open(_lowerCamelCase       ,  encoding="""utf-8"""							) as merges_handle:
               __lowerCamelCase     :      int       						=						merges_handle.read().split("""\n"""							)[1:-1]
         __lowerCamelCase     :      Tuple       						=						[tuple(merge.split()							) for merge in bpe_merges]
         __lowerCamelCase     :      int       						=						dict(zip(_lowerCamelCase       ,  range(len(_lowerCamelCase							)							)							)							)
         __lowerCamelCase     :      Optional[int]       						=						{}
         __lowerCamelCase     :      str       						=						add_prefix_space

         # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
         __lowerCamelCase     :      Optional[Any]       						=						re.compile(R"""'s|'t|'re|'ve|'m|'ll|'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+"""							)

   @property
   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         return len(self.encoder							)

   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         return dict(self.encoder       ,  **self.added_tokens_encoder							)

   def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Dict							):






         '''simple docstring'''



         if token in self.cache:
               return self.cache[token]
         __lowerCamelCase     :      List[str]       						=						tuple(_lowerCamelCase							)
         __lowerCamelCase     :      Tuple       						=						get_pairs(_lowerCamelCase							)

         if not pairs:
               return token

         while True:
               __lowerCamelCase     :      Union[str, Any]       						=						min(_lowerCamelCase       ,  key=lambda _lowerCamelCase							: self.bpe_ranks.get(_lowerCamelCase       ,  float("""inf"""							)							)							)
               if bigram not in self.bpe_ranks:
                     break
               __lowerCamelCase						, __lowerCamelCase     :      Union[str, Any]       						=						bigram
               __lowerCamelCase     :      Union[str, Any]       						=						[]
               __lowerCamelCase     :      Dict       						=						0
               while i < len(_lowerCamelCase							):
                     try:
                           __lowerCamelCase     :      str       						=						word.index(_lowerCamelCase       ,  _lowerCamelCase							)
                     except ValueError:
                           new_word.extend(word[i:]							)
                           break
                     else:
                           new_word.extend(word[i:j]							)
                           __lowerCamelCase     :      List[Any]       						=						j

                     if word[i] == first and i < len(_lowerCamelCase							) - 1 and word[i + 1] == second:
                           new_word.append(first + second							)
                           i += 2
                     else:
                           new_word.append(word[i]							)
                           i += 1
               __lowerCamelCase     :      Any       						=						tuple(_lowerCamelCase							)
               __lowerCamelCase     :      List[str]       						=						new_word
               if len(_lowerCamelCase							) == 1:
                     break
               else:
                     __lowerCamelCase     :      Tuple       						=						get_pairs(_lowerCamelCase							)
         __lowerCamelCase     :      Union[str, Any]       						=						""" """.join(_lowerCamelCase							)
         __lowerCamelCase     :      Dict       						=						word
         return word

   def   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Dict       						=						[]
         for token in re.findall(self.pat       ,  _lowerCamelCase							):
               __lowerCamelCase     :      Any       						=						"""""".join(
                   self.byte_encoder[b] for b in token.encode("""utf-8"""							)							)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
               bpe_tokens.extend(bpe_token for bpe_token in self.bpe(_lowerCamelCase							).split(""" """							)							)
         return bpe_tokens

   def   _snake_case    (  self  : Optional[Any]       ,  _lowerCamelCase  : Optional[int]							):






         '''simple docstring'''



         return self.encoder.get(_lowerCamelCase       ,  self.encoder.get(self.unk_token							)							)

   def   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : Dict							):






         '''simple docstring'''



         return self.decoder.get(_lowerCamelCase							)

   def   _snake_case    (  self  : Any       ,  _lowerCamelCase  : Union[str, Any]							):






         '''simple docstring'''



         __lowerCamelCase     :      Union[str, Any]       						=						"""""".join(_lowerCamelCase							)
         __lowerCamelCase     :      List[Any]       						=						bytearray([self.byte_decoder[c] for c in text]							).decode("""utf-8"""       ,  errors=self.errors							)
         return text

   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : str       ,  _lowerCamelCase  : Optional[str] = None							):






         '''simple docstring'''



         if not os.path.isdir(_lowerCamelCase							):
               logger.error(F"""Vocabulary path ({save_directory}) should be a directory"""							)
               return
         __lowerCamelCase     :      Optional[int]       						=						os.path.join(
             _lowerCamelCase       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)
         __lowerCamelCase     :      Optional[int]       						=						os.path.join(
             _lowerCamelCase       ,  (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""merges_file"""]							)

         with open(_lowerCamelCase       ,  """w"""       ,  encoding="""utf-8"""							) as f:
               f.write(json.dumps(self.encoder       ,  indent=2       ,  sort_keys=_lowerCamelCase       ,  ensure_ascii=_lowerCamelCase							) + """\n"""							)

         __lowerCamelCase     :      int       						=						0
         with open(_lowerCamelCase       ,  """w"""       ,  encoding="""utf-8"""							) as writer:
               writer.write("""#version: 0.2\n"""							)
               for bpe_tokens, token_index in sorted(self.bpe_ranks.items()       ,  key=lambda _lowerCamelCase							: kv[1]							):
                     if index != token_index:
                           logger.warning(
                               F"""Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."""
                               """ Please check that the tokenizer is not corrupted!"""							)
                           __lowerCamelCase     :      int       						=						token_index
                     writer.write(""" """.join(_lowerCamelCase							) + """\n"""							)
                     index += 1

         return vocab_file, merge_file

   def   _snake_case    (  self  : Dict       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None							):






         '''simple docstring'''



         if token_ids_a is None:
               return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
         __lowerCamelCase     :      Optional[Any]       						=						[self.cls_token_id]
         __lowerCamelCase     :      str       						=						[self.sep_token_id]
         return cls + token_ids_a + sep + sep + token_ids_a + sep

   def   _snake_case    (  self  : Optional[Any]       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None       ,  _lowerCamelCase  : bool = False							):






         '''simple docstring'''



         if already_has_special_tokens:
               return super().get_special_tokens_mask(
                   token_ids_a=_lowerCamelCase       ,  token_ids_a=_lowerCamelCase       ,  already_has_special_tokens=_lowerCamelCase							)

         if token_ids_a is None:
               return [1] + ([0] * len(_lowerCamelCase							)) + [1]
         return [1] + ([0] * len(_lowerCamelCase							)) + [1, 1] + ([0] * len(_lowerCamelCase							)) + [1]

   def   _snake_case    (  self  : List[Any]       ,  _lowerCamelCase  : List[int]       ,  _lowerCamelCase  : Optional[List[int]] = None							):






         '''simple docstring'''



         __lowerCamelCase     :      str       						=						[self.sep_token_id]
         __lowerCamelCase     :      List[Any]       						=						[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 + sep + token_ids_a + sep							) * [0]


   def   _snake_case    (  self  : str       ,  _lowerCamelCase  : Optional[int]       ,  _lowerCamelCase  : str=False       ,  **_lowerCamelCase  : str							):






         '''simple docstring'''



         __lowerCamelCase     :      Any       						=						kwargs.pop("""add_prefix_space"""       ,  self.add_prefix_space							)
         if (is_split_into_words or add_prefix_space) and (len(_lowerCamelCase							) > 0 and not text[0].isspace()):
               __lowerCamelCase     :      int       						=						""" """ + text
         return (text, kwargs)







import copy
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


__UpperCamelCase    :						Optional[int]     =							logging.get_logger(__name__)

__UpperCamelCase    :						Any     =							{
    'microsoft/conditional-detr-resnet-50': (
        'https://huggingface.co/microsoft/conditional-detr-resnet-50/resolve/main/config.json'
    ),
}



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Optional[int]    = "conditional_detr"
   a_				:							Union[str, Any]    = ["past_key_values"]
   a_				:							str    = {
       "hidden_size": "d_model",
       "num_attention_heads": "encoder_attention_heads",
   }

   def __init__(  self  : Dict       ,  _lowerCamelCase  : Optional[int]=True       ,  _lowerCamelCase  : int=None       ,  _lowerCamelCase  : Optional[int]=3       ,  _lowerCamelCase  : Tuple=3_0_0       ,  _lowerCamelCase  : Any=6       ,  _lowerCamelCase  : Union[str, Any]=2_0_4_8       ,  _lowerCamelCase  : List[str]=8       ,  _lowerCamelCase  : Any=6       ,  _lowerCamelCase  : Any=2_0_4_8       ,  _lowerCamelCase  : List[Any]=8       ,  _lowerCamelCase  : Optional[int]=0.0       ,  _lowerCamelCase  : Any=0.0       ,  _lowerCamelCase  : Tuple=True       ,  _lowerCamelCase  : Union[str, Any]="relu"       ,  _lowerCamelCase  : str=2_5_6       ,  _lowerCamelCase  : int=0.1       ,  _lowerCamelCase  : List[str]=0.0       ,  _lowerCamelCase  : Tuple=0.0       ,  _lowerCamelCase  : List[str]=0.02       ,  _lowerCamelCase  : Union[str, Any]=1.0       ,  _lowerCamelCase  : Optional[int]=False       ,  _lowerCamelCase  : List[str]="sine"       ,  _lowerCamelCase  : Optional[Any]="resnet50"       ,  _lowerCamelCase  : List[str]=True       ,  _lowerCamelCase  : Dict=False       ,  _lowerCamelCase  : Optional[int]=2       ,  _lowerCamelCase  : Dict=5       ,  _lowerCamelCase  : List[Any]=2       ,  _lowerCamelCase  : Any=1       ,  _lowerCamelCase  : Any=1       ,  _lowerCamelCase  : Dict=2       ,  _lowerCamelCase  : Dict=5       ,  _lowerCamelCase  : Dict=2       ,  _lowerCamelCase  : Union[str, Any]=0.25       ,  **_lowerCamelCase  : Tuple       ,  ):






         '''simple docstring'''



         if backbone_config is not None and use_timm_backbone:
               raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""							)

         if not use_timm_backbone:
               if backbone_config is None:
                     logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""							)
                     __lowerCamelCase     :      Any       						=						CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]							)
               elif isinstance(_lowerCamelCase       ,  _lowerCamelCase							):
                     __lowerCamelCase     :      str       						=						backbone_config.get("""model_type"""							)
                     __lowerCamelCase     :      Optional[Any]       						=						CONFIG_MAPPING[backbone_model_type]
                     __lowerCamelCase     :      Optional[Any]       						=						config_class.from_dict(_lowerCamelCase							)

         __lowerCamelCase     :      Optional[int]       						=						use_timm_backbone
         __lowerCamelCase     :      Union[str, Any]       						=						backbone_config
         __lowerCamelCase     :      List[Any]       						=						num_channels
         __lowerCamelCase     :      str       						=						num_queries
         __lowerCamelCase     :      Dict       						=						d_model
         __lowerCamelCase     :      List[Any]       						=						encoder_ffn_dim
         __lowerCamelCase     :      Optional[Any]       						=						encoder_layers
         __lowerCamelCase     :      Any       						=						encoder_attention_heads
         __lowerCamelCase     :      Dict       						=						decoder_ffn_dim
         __lowerCamelCase     :      List[str]       						=						decoder_layers
         __lowerCamelCase     :      Tuple       						=						decoder_attention_heads
         __lowerCamelCase     :      List[Any]       						=						dropout
         __lowerCamelCase     :      Any       						=						attention_dropout
         __lowerCamelCase     :      Optional[Any]       						=						activation_dropout
         __lowerCamelCase     :      List[str]       						=						activation_function
         __lowerCamelCase     :      Optional[int]       						=						init_std
         __lowerCamelCase     :      Union[str, Any]       						=						init_xavier_std
         __lowerCamelCase     :      Union[str, Any]       						=						encoder_layerdrop
         __lowerCamelCase     :      int       						=						decoder_layerdrop
         __lowerCamelCase     :      Dict       						=						encoder_layers
         __lowerCamelCase     :      Tuple       						=						auxiliary_loss
         __lowerCamelCase     :      Any       						=						position_embedding_type
         __lowerCamelCase     :      Tuple       						=						backbone
         __lowerCamelCase     :      int       						=						use_pretrained_backbone
         __lowerCamelCase     :      Tuple       						=						dilation
         # Hungarian matcher
         __lowerCamelCase     :      Dict       						=						class_cost
         __lowerCamelCase     :      Optional[Any]       						=						bbox_cost
         __lowerCamelCase     :      Any       						=						giou_cost
         # Loss coefficients
         __lowerCamelCase     :      Union[str, Any]       						=						mask_loss_coefficient
         __lowerCamelCase     :      List[Any]       						=						dice_loss_coefficient
         __lowerCamelCase     :      Any       						=						cls_loss_coefficient
         __lowerCamelCase     :      str       						=						bbox_loss_coefficient
         __lowerCamelCase     :      Dict       						=						giou_loss_coefficient
         __lowerCamelCase     :      List[str]       						=						focal_alpha
         super().__init__(is_encoder_decoder=_lowerCamelCase       ,  **_lowerCamelCase							)

   @property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return self.encoder_attention_heads

   @property
   def   _snake_case    (  self  : Optional[Any]							):






         '''simple docstring'''



         return self.d_model


   def   _snake_case    (  self  : int							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						copy.deepcopy(self.__dict__							)
         if self.backbone_config is not None:
               __lowerCamelCase     :      Dict       						=						self.backbone_config.to_dict()
         __lowerCamelCase     :      int       						=						self.__class__.model_type
         return output



class 							_UpperCamelCase (						A						):

   '''simple docstring'''







   a_				:							Dict    = version.parse("1.11"						)

   @property
   def   _snake_case    (  self  : Any							):






         '''simple docstring'''



         return OrderedDict(
             [
                 ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
                 ("""pixel_mask""", {0: """batch"""}),
             ]							)

   @property
   def   _snake_case    (  self  : Union[str, Any]							):






         '''simple docstring'''



         return 1E-5


   @property
   def   _snake_case    (  self  : Tuple							):






         '''simple docstring'''



         return 1_2



