





import os
from typing import List, Union

import tensorflow as tf
from tensorflow_text import BertTokenizer as BertTokenizerLayer
from tensorflow_text import FastBertTokenizer, ShrinkLongestTrimmer, case_fold_utfa, combine_segments, pad_model_inputs

from .tokenization_bert import BertTokenizer



class 							_UpperCamelCase    (		tf.keras.layers.Layer   ):

       def __init__(						self:       Optional[int]    ,      _SCREAMING_SNAKE_CASE:       List    ,      _SCREAMING_SNAKE_CASE:       bool    ,      _SCREAMING_SNAKE_CASE:       int = None    ,      _SCREAMING_SNAKE_CASE:       int = None    ,      _SCREAMING_SNAKE_CASE:       int = None    ,      _SCREAMING_SNAKE_CASE:       str = "longest"    ,      _SCREAMING_SNAKE_CASE:       bool = True    ,      _SCREAMING_SNAKE_CASE:       int = 512    ,      _SCREAMING_SNAKE_CASE:       int = None    ,      _SCREAMING_SNAKE_CASE:       bool = True    ,      _SCREAMING_SNAKE_CASE:       bool = True    ,      _SCREAMING_SNAKE_CASE:       bool = True    ,      **_SCREAMING_SNAKE_CASE:       List[str]    ,      )				->       Optional[int]:




              """simple docstring"""


              super().__init__()
              if use_fast_bert_tokenizer:
                     UpperCamelCase_      				=     FastBertTokenizer(
                         _SCREAMING_SNAKE_CASE    ,      token_out_type=tf.intaa    ,      lower_case_nfd_strip_accents=_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)
              else:
                     UpperCamelCase_      				=     tf.lookup.StaticVocabularyTable(
                         tf.lookup.KeyValueTensorInitializer(
                             keys=_SCREAMING_SNAKE_CASE    ,      key_dtype=tf.string    ,      values=tf.range(tf.size(_SCREAMING_SNAKE_CASE    ,      out_type=tf.intaa						)    ,      dtype=tf.intaa						)    ,      value_dtype=tf.intaa    ,      )    ,      num_oov_buckets=1    ,      )
                     UpperCamelCase_      				=     BertTokenizerLayer(
                         _SCREAMING_SNAKE_CASE    ,      token_out_type=tf.intaa    ,      lower_case=_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     vocab_list
              UpperCamelCase_      				=     do_lower_case
              UpperCamelCase_      				=     cls_token_id or vocab_list.index("[CLS]"						)
              UpperCamelCase_      				=     sep_token_id or vocab_list.index("[SEP]"						)
              UpperCamelCase_      				=     pad_token_id or vocab_list.index("[PAD]"						)
              UpperCamelCase_      				=     ShrinkLongestTrimmer(max_length - 3    ,      axis=1						)  # Allow room for special tokens
              UpperCamelCase_      				=     max_length
              UpperCamelCase_      				=     padding
              UpperCamelCase_      				=     truncation
              UpperCamelCase_      				=     pad_to_multiple_of
              UpperCamelCase_      				=     return_token_type_ids
              UpperCamelCase_      				=     return_attention_mask

       @classmethod
       def lowercase       (						cls:       str    ,      _SCREAMING_SNAKE_CASE:       "PreTrainedTokenizerBase"    ,      **_SCREAMING_SNAKE_CASE:       Dict						)				->       Optional[Any]:  # noqa: F821




              """simple docstring"""


              UpperCamelCase_      				=     kwargs.pop("do_lower_case"    ,      _SCREAMING_SNAKE_CASE						)
              UpperCamelCase_      				=     tokenizer.do_lower_case if do_lower_case is None else do_lower_case
              UpperCamelCase_      				=     kwargs.pop("cls_token_id"    ,      _SCREAMING_SNAKE_CASE						)
              UpperCamelCase_      				=     tokenizer.cls_token_id if cls_token_id is None else cls_token_id
              UpperCamelCase_      				=     kwargs.pop("sep_token_id"    ,      _SCREAMING_SNAKE_CASE						)
              UpperCamelCase_      				=     tokenizer.sep_token_id if sep_token_id is None else sep_token_id
              UpperCamelCase_      				=     kwargs.pop("pad_token_id"    ,      _SCREAMING_SNAKE_CASE						)
              UpperCamelCase_      				=     tokenizer.pad_token_id if pad_token_id is None else pad_token_id

              UpperCamelCase_      				=     tokenizer.get_vocab()
              UpperCamelCase_      				=     sorted([(wordpiece, idx) for wordpiece, idx in vocab.items()]    ,      key=lambda _SCREAMING_SNAKE_CASE						: x[1]						)
              UpperCamelCase_      				=     [entry[0] for entry in vocab]
              return cls(
                  vocab_list=_SCREAMING_SNAKE_CASE    ,      do_lower_case=_SCREAMING_SNAKE_CASE    ,      cls_token_id=_SCREAMING_SNAKE_CASE    ,      sep_token_id=_SCREAMING_SNAKE_CASE    ,      pad_token_id=_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE    ,      )

       @classmethod
       def lowercase       (						cls:       Tuple    ,      _SCREAMING_SNAKE_CASE:       Union[str, os.PathLike]    ,      *_SCREAMING_SNAKE_CASE:       Any    ,      **_SCREAMING_SNAKE_CASE:       Union[str, Any]						)				->       Any:




              """simple docstring"""


              try:
                     UpperCamelCase_      				=     BertTokenizer.from_pretrained(_SCREAMING_SNAKE_CASE    ,      *_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)
              except:  # noqa: E722
                     from .tokenization_bert_fast import BertTokenizerFast

                     UpperCamelCase_      				=     BertTokenizerFast.from_pretrained(_SCREAMING_SNAKE_CASE    ,      *_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)
              return cls.from_tokenizer(_SCREAMING_SNAKE_CASE    ,      **_SCREAMING_SNAKE_CASE						)

       def lowercase       (						self:       Optional[int]    ,      _SCREAMING_SNAKE_CASE:       Tuple						)				->       List[Any]:




              """simple docstring"""


              if self.do_lower_case:
                     UpperCamelCase_      				=     case_fold_utfa(_SCREAMING_SNAKE_CASE						)
              UpperCamelCase_      				=     self.tf_tokenizer.tokenize(_SCREAMING_SNAKE_CASE						)
              return tokens.merge_dims(1    ,      -1						)

       def lowercase       (						self:       List[Any]    ,      _SCREAMING_SNAKE_CASE:       List[Any]    ,      _SCREAMING_SNAKE_CASE:       Dict=None    ,      _SCREAMING_SNAKE_CASE:       Any=None    ,      _SCREAMING_SNAKE_CASE:       Union[str, Any]=None    ,      _SCREAMING_SNAKE_CASE:       Tuple=None    ,      _SCREAMING_SNAKE_CASE:       Dict=None    ,      _SCREAMING_SNAKE_CASE:       List[str]=None    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=None    ,      )				->       Union[str, Any]:




              """simple docstring"""


              if padding is None:
                     UpperCamelCase_      				=     self.padding
              if padding not in ("longest", "max_length"):
                     raise ValueError("Padding must be either 'longest' or 'max_length'!"						)
              if max_length is not None and text_pair is not None:
                     # Because we have to instantiate a Trimmer to do it properly
                     raise ValueError("max_length cannot be overridden at call time when truncating paired texts!"						)
              if max_length is None:
                     UpperCamelCase_      				=     self.max_length
              if truncation is None:
                     UpperCamelCase_      				=     self.truncation
              if pad_to_multiple_of is None:
                     UpperCamelCase_      				=     self.pad_to_multiple_of
              if return_token_type_ids is None:
                     UpperCamelCase_      				=     self.return_token_type_ids
              if return_attention_mask is None:
                     UpperCamelCase_      				=     self.return_attention_mask
              if not isinstance(_SCREAMING_SNAKE_CASE    ,      tf.Tensor						):
                     UpperCamelCase_      				=     tf.convert_to_tensor(_SCREAMING_SNAKE_CASE						)
              if text_pair is not None and not isinstance(_SCREAMING_SNAKE_CASE    ,      tf.Tensor						):
                     UpperCamelCase_      				=     tf.convert_to_tensor(_SCREAMING_SNAKE_CASE						)
              if text_pair is not None:
                     if text.shape.rank > 1:
                            raise ValueError("text argument should not be multidimensional when a text pair is supplied!"						)
                     if text_pair.shape.rank > 1:
                            raise ValueError("text_pair should not be multidimensional!"						)
              if text.shape.rank == 2:
                     UpperCamelCase_				,       UpperCamelCase_      				=     text[:, 0], text[:, 1]
              UpperCamelCase_      				=     self.unpaired_tokenize(_SCREAMING_SNAKE_CASE						)
              if text_pair is None:  # Unpaired text
                     if truncation:
                            UpperCamelCase_      				=     text[:, : max_length - 2]  # Allow room for special tokens
                     UpperCamelCase_				,       UpperCamelCase_      				=     combine_segments(
                         (text,)    ,      start_of_sequence_id=self.cls_token_id    ,      end_of_segment_id=self.sep_token_id						)
              else:  # Paired text
                     UpperCamelCase_      				=     self.unpaired_tokenize(_SCREAMING_SNAKE_CASE						)
                     if truncation:
                            UpperCamelCase_				,       UpperCamelCase_      				=     self.paired_trimmer.trim([text, text_pair]						)
                     UpperCamelCase_				,       UpperCamelCase_      				=     combine_segments(
                         (text, text_pair)    ,      start_of_sequence_id=self.cls_token_id    ,      end_of_segment_id=self.sep_token_id						)
              if padding == "longest":
                     UpperCamelCase_      				=     input_ids.bounding_shape(axis=1						)
                     if pad_to_multiple_of is not None:
                            # No ceiling division in tensorflow, so we negate floordiv instead
                            UpperCamelCase_      				=     pad_to_multiple_of * (-tf.math.floordiv(-pad_length    ,      _SCREAMING_SNAKE_CASE						))
              else:
                     UpperCamelCase_      				=     max_length

              UpperCamelCase_				,       UpperCamelCase_      				=     pad_model_inputs(_SCREAMING_SNAKE_CASE    ,      max_seq_length=_SCREAMING_SNAKE_CASE    ,      pad_value=self.pad_token_id						)
              UpperCamelCase_      				=     {"input_ids": input_ids}
              if return_attention_mask:
                     UpperCamelCase_      				=     attention_mask
              if return_token_type_ids:
                     UpperCamelCase_				,       UpperCamelCase_      				=     pad_model_inputs(
                         _SCREAMING_SNAKE_CASE    ,      max_seq_length=_SCREAMING_SNAKE_CASE    ,      pad_value=self.pad_token_id						)
                     UpperCamelCase_      				=     token_type_ids
              return output



       def lowercase       (						self:       Any						)				->       List[str]:




              """simple docstring"""


              return {
                  "vocab_list": self.vocab_list,
                  "do_lower_case": self.do_lower_case,
                  "cls_token_id": self.cls_token_id,
                  "sep_token_id": self.sep_token_id,
                  "pad_token_id": self.pad_token_id,
              }








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


_UpperCAmelCase				       = logging.get_logger(__name__)

_UpperCAmelCase				       = {
    'MCG-NJU/videomae-base': 'https://huggingface.co/MCG-NJU/videomae-base/resolve/main/config.json',
}



class 							_UpperCamelCase    (		lowerCAmelCase_   ):
       _UpperCamelCase :    List[Any]   			=      '''videomae'''

       def __init__(						self:       Optional[Any]    ,      _SCREAMING_SNAKE_CASE:       List[str]=224    ,      _SCREAMING_SNAKE_CASE:       Dict=16    ,      _SCREAMING_SNAKE_CASE:       List[Any]=3    ,      _SCREAMING_SNAKE_CASE:       Tuple=16    ,      _SCREAMING_SNAKE_CASE:       Tuple=2    ,      _SCREAMING_SNAKE_CASE:       Optional[Any]=768    ,      _SCREAMING_SNAKE_CASE:       str=12    ,      _SCREAMING_SNAKE_CASE:       List[Any]=12    ,      _SCREAMING_SNAKE_CASE:       Dict=3072    ,      _SCREAMING_SNAKE_CASE:       Tuple="gelu"    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       List[Any]=0.0    ,      _SCREAMING_SNAKE_CASE:       Tuple=0.02    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=1e-12    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       Dict=True    ,      _SCREAMING_SNAKE_CASE:       List[str]=6    ,      _SCREAMING_SNAKE_CASE:       Dict=384    ,      _SCREAMING_SNAKE_CASE:       Any=4    ,      _SCREAMING_SNAKE_CASE:       Dict=1536    ,      _SCREAMING_SNAKE_CASE:       Optional[int]=True    ,      **_SCREAMING_SNAKE_CASE:       str    ,      )				->       Dict:




              """simple docstring"""


              super().__init__(**_SCREAMING_SNAKE_CASE						)

              UpperCamelCase_      				=     image_size
              UpperCamelCase_      				=     patch_size
              UpperCamelCase_      				=     num_channels
              UpperCamelCase_      				=     num_frames
              UpperCamelCase_      				=     tubelet_size

              UpperCamelCase_      				=     hidden_size
              UpperCamelCase_      				=     num_hidden_layers
              UpperCamelCase_      				=     num_attention_heads
              UpperCamelCase_      				=     intermediate_size
              UpperCamelCase_      				=     hidden_act
              UpperCamelCase_      				=     hidden_dropout_prob
              UpperCamelCase_      				=     attention_probs_dropout_prob
              UpperCamelCase_      				=     initializer_range
              UpperCamelCase_      				=     layer_norm_eps
              UpperCamelCase_      				=     qkv_bias
              UpperCamelCase_      				=     use_mean_pooling

              UpperCamelCase_      				=     decoder_num_attention_heads
              UpperCamelCase_      				=     decoder_hidden_size
              UpperCamelCase_      				=     decoder_num_hidden_layers
              UpperCamelCase_      				=     decoder_intermediate_size
              UpperCamelCase_      				=     norm_pix_loss

