


import os
import re
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

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


__UpperCamelCase				:  List[str]      					=							logging.get_logger(__name__)

__UpperCamelCase				:  Any      					=							{"""vocab_file""": """spiece.model"""}

__UpperCamelCase				:  Optional[Any]      					=							{
    """vocab_file""": {
        """google/bigbird-roberta-base""": """https://huggingface.co/google/bigbird-roberta-base/resolve/main/spiece.model""",
        """google/bigbird-roberta-large""": (
            """https://huggingface.co/google/bigbird-roberta-large/resolve/main/spiece.model"""
        ),
        """google/bigbird-base-trivia-itc""": (
            """https://huggingface.co/google/bigbird-base-trivia-itc/resolve/main/spiece.model"""
        ),
    }
}

__UpperCamelCase				:  List[str]      					=							{
    """google/bigbird-roberta-base""": 4096,
    """google/bigbird-roberta-large""": 4096,
    """google/bigbird-base-trivia-itc""": 4096,
}





class  lowerCAmelCase__(    __UpperCAmelCase						):






    '''simple docstring'''





    A_  :     List[Any]			= VOCAB_FILES_NAMES
    A_  :     str			= PRETRAINED_VOCAB_FILES_MAP
    A_  :     Any			= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    A_  :     Optional[Any]			= ['input_ids', 'attention_mask']
    A_  :     Any			= []





    def __init__(	self				:   Tuple       ,   __snake_case				:   str       ,   __snake_case				:   str="<unk>"       ,   __snake_case				:   Tuple="<s>"       ,   __snake_case				:   Union[str, Any]="</s>"       ,   __snake_case				:   int="<pad>"       ,   __snake_case				:   Tuple="[SEP]"       ,   __snake_case				:   Optional[int]="[MASK]"       ,   __snake_case				:   str="[CLS]"       ,   __snake_case				:   Optional[Any] = None       ,   **__snake_case				:   Dict       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      Union[str, Any]						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else bos_token
       UpperCAmelCase_							:      int						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else eos_token
       UpperCAmelCase_							:      List[str]						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else unk_token
       UpperCAmelCase_							:      int						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else pad_token
       UpperCAmelCase_							:      Optional[Any]						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else cls_token
       UpperCAmelCase_							:      List[str]						=   AddedToken(__snake_case       ,   lstrip=__snake_case       ,   rstrip=__snake_case	) if isinstance(__snake_case       ,   __snake_case	) else sep_token

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

       UpperCAmelCase_							:      Optional[Any]						=   {} if sp_model_kwargs is None else sp_model_kwargs

       super().__init__(
           bos_token=__snake_case       ,   eos_token=__snake_case       ,   unk_token=__snake_case       ,   pad_token=__snake_case       ,   sep_token=__snake_case       ,   mask_token=__snake_case       ,   cls_token=__snake_case       ,   sp_model_kwargs=self.sp_model_kwargs       ,   **__snake_case       ,   )

       UpperCAmelCase_							:      Optional[Any]						=   vocab_file

       UpperCAmelCase_							:      Union[str, Any]						=   spm.SentencePieceProcessor(**self.sp_model_kwargs	)
       self.sp_model.Load(__snake_case	)





    @property
    def 					_lowerCamelCase (	self				:   Tuple	):





       '''simple docstring'''
       return self.sp_model.get_piece_size()





    def 					_lowerCamelCase (	self				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   {self.convert_ids_to_tokens(__snake_case	): i for i in range(self.vocab_size	)}
       vocab.update(self.added_tokens_encoder	)
       return vocab





    def __getstate__(	self				:   Tuple	):





       '''simple docstring'''
       UpperCAmelCase_							:      List[str]						=   self.__dict__.copy()
       UpperCAmelCase_							:      Union[str, Any]						=   None
       return state





    def __setstate__(	self				:   Tuple       ,   __snake_case				:   Optional[int]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Dict						=   d

       # for backward compatibility
       if not hasattr(self       ,   '''sp_model_kwargs'''	):
          UpperCAmelCase_							:      List[str]						=   {}

       UpperCAmelCase_							:      List[Any]						=   spm.SentencePieceProcessor(**self.sp_model_kwargs	)
       self.sp_model.Load(self.vocab_file	)





    def 					_lowerCamelCase (	self				:   Any       ,   __snake_case				:   List[Any]	):





       '''simple docstring'''
       return self.sp_model.encode(__snake_case       ,   out_type=__snake_case	)





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





       '''simple docstring'''
       return self.sp_model.piece_to_id(__snake_case	)





    def 					_lowerCamelCase (	self				:   List[str]       ,   __snake_case				:   Dict	):





       '''simple docstring'''
       UpperCAmelCase_							:      Tuple						=   self.sp_model.IdToPiece(__snake_case	)
       return token





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   List[Any]	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[Any]						=   []
       UpperCAmelCase_							:      str						=   ''''''
       UpperCAmelCase_							:      str						=   False
       for token in tokens:
          # make sure that special tokens are not decoded using sentencepiece model
          if token in self.all_special_tokens:
             if not prev_is_special:
                out_string += " "
             out_string += self.sp_model.decode(__snake_case	) + token
             UpperCAmelCase_							:      Optional[Any]						=   True
             UpperCAmelCase_							:      str						=   []
          else:
             current_sub_tokens.append(__snake_case	)
             UpperCAmelCase_							:      Optional[Any]						=   False
       out_string += self.sp_model.decode(__snake_case	)
       return out_string.strip()





    def 					_lowerCamelCase (	self				:   str       ,   __snake_case				:   str       ,   __snake_case				:   Optional[Any] = False       ,   __snake_case				:   Dict = None       ,   __snake_case				:   Optional[int] = True       ,   **__snake_case				:   List[Any]       ,   ):





       '''simple docstring'''
       UpperCAmelCase_							:      int						=   kwargs.pop('''use_source_tokenizer'''       ,   __snake_case	)

       UpperCAmelCase_							:      Tuple						=   self.convert_ids_to_tokens(__snake_case       ,   skip_special_tokens=__snake_case	)

       # To avoid mixing byte-level and unicode for byte-level BPT
       # we need to build string separately for added tokens and byte-level tokens
       # cf. https://github.com/huggingface/transformers/issues/1133
       UpperCAmelCase_							:      List[Any]						=   []
       UpperCAmelCase_							:      List[str]						=   []
       for token in filtered_tokens:
          if skip_special_tokens and token in self.all_special_ids:
             continue
          if token in self.added_tokens_encoder:
             if current_sub_text:
                sub_texts.append(self.convert_tokens_to_string(__snake_case	)	)
                UpperCAmelCase_							:      Any						=   []
             sub_texts.append(__snake_case	)
          else:
             current_sub_text.append(__snake_case	)
       if current_sub_text:
          sub_texts.append(self.convert_tokens_to_string(__snake_case	)	)

       # Mimic the behavior of the Rust tokenizer:
       # No space before [MASK] and [SEP]
       if spaces_between_special_tokens:
          UpperCAmelCase_							:      Optional[int]						=   re.sub(R''' (\[(MASK|SEP)\])'''       ,   R'''\1'''       ,   ''' '''.join(__snake_case	)	)
       else:
          UpperCAmelCase_							:      List[str]						=   ''''''.join(__snake_case	)

       UpperCAmelCase_							:      int						=   (
           clean_up_tokenization_spaces
           if clean_up_tokenization_spaces is not None
           else self.clean_up_tokenization_spaces
       )
       if clean_up_tokenization_spaces:
          UpperCAmelCase_							:      Optional[Any]						=   self.clean_up_tokenization(__snake_case	)
          return clean_text
       else:
          return text





    def 					_lowerCamelCase (	self				:   Optional[int]       ,   __snake_case				:   str       ,   __snake_case				:   Dict = None	):





       '''simple docstring'''

       if not os.path.isdir(__snake_case	):
          logger.error(f'''Vocabulary path ({save_directory}) should be a directory'''	)
          return
       UpperCAmelCase_							:      int						=   os.path.join(
           __snake_case       ,   (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']	)

       if os.path.abspath(self.vocab_file	) != os.path.abspath(__snake_case	) and os.path.isfile(self.vocab_file	):
          copyfile(self.vocab_file       ,   __snake_case	)
       elif not os.path.isfile(self.vocab_file	):
          with open(__snake_case       ,   '''wb'''	) as fi:
             UpperCAmelCase_							:      str						=   self.sp_model.serialized_model_proto()
             fi.write(__snake_case	)

       return (out_vocab_file,)





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





       '''simple docstring'''

       if token_ids_a is None:
          return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
       UpperCAmelCase_							:      Union[str, Any]						=   [self.cls_token_id]
       UpperCAmelCase_							:      Any						=   [self.sep_token_id]
       return cls + token_ids_a + sep + token_ids_a + sep





    def 					_lowerCamelCase (	self				:   List[str]       ,   __snake_case				:   Optional[Any]       ,   __snake_case				:   List[Any] = None       ,   __snake_case				:   Tuple = False	):





       '''simple docstring'''

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

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





    def 					_lowerCamelCase (	self				:   List[str]       ,   __snake_case				:   List[str]       ,   __snake_case				:   Tuple = None	):





       '''simple docstring'''
       UpperCAmelCase_							:      Optional[int]						=   [self.sep_token_id]
       UpperCAmelCase_							:      Tuple						=   [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]



from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCamelCase				:  int      					=							logging.get_logger(__name__)

__UpperCamelCase				:  Union[str, Any]      					=							{
    'facebook/timesformer': 'https://huggingface.co/facebook/timesformer/resolve/main/config.json',
}





class  lowerCAmelCase__(    snake_case__						):






    '''simple docstring'''





    A_  :     str			= 'timesformer'





    def __init__(	self				:   int       ,   __snake_case				:   Any=224       ,   __snake_case				:   str=16       ,   __snake_case				:   Any=3       ,   __snake_case				:   List[Any]=8       ,   __snake_case				:   Dict=768       ,   __snake_case				:   Dict=12       ,   __snake_case				:   Tuple=12       ,   __snake_case				:   Dict=3_072       ,   __snake_case				:   str="gelu"       ,   __snake_case				:   Union[str, Any]=0.0       ,   __snake_case				:   List[Any]=0.0       ,   __snake_case				:   Tuple=0.02       ,   __snake_case				:   Optional[Any]=1E-6       ,   __snake_case				:   List[Any]=True       ,   __snake_case				:   List[str]="divided_space_time"       ,   __snake_case				:   Optional[int]=0       ,   **__snake_case				:   Dict       ,   ):





       '''simple docstring'''
       super().__init__(**__snake_case	)

       UpperCAmelCase_							:      Optional[int]						=   image_size
       UpperCAmelCase_							:      Optional[Any]						=   patch_size
       UpperCAmelCase_							:      Any						=   num_channels
       UpperCAmelCase_							:      int						=   num_frames

       UpperCAmelCase_							:      List[Any]						=   hidden_size
       UpperCAmelCase_							:      Any						=   num_hidden_layers
       UpperCAmelCase_							:      Dict						=   num_attention_heads
       UpperCAmelCase_							:      List[Any]						=   intermediate_size
       UpperCAmelCase_							:      str						=   hidden_act
       UpperCAmelCase_							:      List[str]						=   hidden_dropout_prob
       UpperCAmelCase_							:      List[str]						=   attention_probs_dropout_prob
       UpperCAmelCase_							:      Optional[int]						=   initializer_range
       UpperCAmelCase_							:      List[str]						=   layer_norm_eps
       UpperCAmelCase_							:      str						=   qkv_bias

       UpperCAmelCase_							:      Dict						=   attention_type
       UpperCAmelCase_							:      str						=   drop_path_rate