






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 							A_      (  tf.keras.layers.Layer     ):






 '''simple docstring'''




 def __init__(		self							,					_A							,					_A							,					_A = None							,					_A = None							,					_A = None							,					_A = "longest"							,					_A = True							,					_A = 512							,					_A = None							,					_A = True							,					_A = True							,					_A = True							,					**_A							,					)	->						str:

     """simple docstring"""



     super().__init__()
     if use_fast_bert_tokenizer:
         _UpperCAmelCase			: Optional[Any]     					=			FastBertTokenizer(
             _A							,					token_out_type=tf.intaa							,					lower_case_nfd_strip_accents=_A							,					**_A)
     else:
         _UpperCAmelCase			: Dict     					=			tf.lookup.StaticVocabularyTable(
             tf.lookup.KeyValueTensorInitializer(
                 keys=_A							,					key_dtype=tf.string							,					values=tf.range(tf.size(_A							,					out_type=tf.intaa)							,					dtype=tf.intaa)							,					value_dtype=tf.intaa							,					)							,					num_oov_buckets=1							,					)
         _UpperCAmelCase			: Union[str, Any]     					=			BertTokenizerLayer(
             _A							,					token_out_type=tf.intaa							,					lower_case=_A							,					**_A)

     _UpperCAmelCase			: int     					=			vocab_list
     _UpperCAmelCase			: str     					=			do_lower_case
     _UpperCAmelCase			: List[Any]     					=			cls_token_id or vocab_list.index('''[CLS]''')
     _UpperCAmelCase			: Tuple     					=			sep_token_id or vocab_list.index('''[SEP]''')
     _UpperCAmelCase			: Any     					=			pad_token_id or vocab_list.index('''[PAD]''')
     _UpperCAmelCase			: Dict     					=			ShrinkLongestTrimmer(max_length - 3							,					axis=1)  # Allow room for special tokens
     _UpperCAmelCase			: List[str]     					=			max_length
     _UpperCAmelCase			: Dict     					=			padding
     _UpperCAmelCase			: Dict     					=			truncation
     _UpperCAmelCase			: List[Any]     					=			pad_to_multiple_of
     _UpperCAmelCase			: Union[str, Any]     					=			return_token_type_ids
     _UpperCAmelCase			: List[Any]     					=			return_attention_mask


 @classmethod
 def 	snake_case__			(		cls							,					_A							,					**_A)	->						Tuple:  # noqa: F821

     """simple docstring"""



     _UpperCAmelCase			: int     					=			kwargs.pop('''do_lower_case'''							,					_A)
     _UpperCAmelCase			: Tuple     					=			tokenizer.do_lower_case if do_lower_case is None else do_lower_case
     _UpperCAmelCase			: Any     					=			kwargs.pop('''cls_token_id'''							,					_A)
     _UpperCAmelCase			: Optional[Any]     					=			tokenizer.cls_token_id if cls_token_id is None else cls_token_id
     _UpperCAmelCase			: int     					=			kwargs.pop('''sep_token_id'''							,					_A)
     _UpperCAmelCase			: List[str]     					=			tokenizer.sep_token_id if sep_token_id is None else sep_token_id
     _UpperCAmelCase			: List[Any]     					=			kwargs.pop('''pad_token_id'''							,					_A)
     _UpperCAmelCase			: str     					=			tokenizer.pad_token_id if pad_token_id is None else pad_token_id

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


 @classmethod
 def 	snake_case__			(		cls							,					_A							,					*_A							,					**_A)	->						Optional[int]:

     """simple docstring"""



     try:
         _UpperCAmelCase			: Dict     					=			BertTokenizer.from_pretrained(_A							,					*_A							,					**_A)
     except:  # noqa: E722
         from .tokenization_bert_fast import BertTokenizerFast

         _UpperCAmelCase			: List[str]     					=			BertTokenizerFast.from_pretrained(_A							,					*_A							,					**_A)
     return cls.from_tokenizer(_A							,					**_A)


 def 	snake_case__			(		self							,					_A)	->						str:

     """simple docstring"""



     if self.do_lower_case:
         _UpperCAmelCase			: Optional[int]     					=			case_fold_utfa(_A)
     _UpperCAmelCase			: str     					=			self.tf_tokenizer.tokenize(_A)
     return tokens.merge_dims(1							,					-1)


 def 	snake_case__			(		self							,					_A							,					_A=None							,					_A=None							,					_A=None							,					_A=None							,					_A=None							,					_A=None							,					_A=None							,					)	->						Optional[int]:

     """simple docstring"""



     if padding is None:
         _UpperCAmelCase			: int     					=			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			: Any     					=			self.max_length
     if truncation is None:
         _UpperCAmelCase			: Dict     					=			self.truncation
     if pad_to_multiple_of is None:
         _UpperCAmelCase			: List[str]     					=			self.pad_to_multiple_of
     if return_token_type_ids is None:
         _UpperCAmelCase			: List[Any]     					=			self.return_token_type_ids
     if return_attention_mask is None:
         _UpperCAmelCase			: Union[str, Any]     					=			self.return_attention_mask
     if not isinstance(_A							,					tf.Tensor):
         _UpperCAmelCase			: str     					=			tf.convert_to_tensor(_A)
     if text_pair is not None and not isinstance(_A							,					tf.Tensor):
         _UpperCAmelCase			: Dict     					=			tf.convert_to_tensor(_A)
     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			: Dict     					=			text[:, 0], text[:, 1]
     _UpperCAmelCase			: List[Any]     					=			self.unpaired_tokenize(_A)
     if text_pair is None:  # Unpaired text
         if truncation:
             _UpperCAmelCase			: str     					=			text[:, : max_length - 2]  # Allow room for special tokens
         _UpperCAmelCase					,	_UpperCAmelCase			: Union[str, Any]     					=			combine_segments(
             (text,)							,					start_of_sequence_id=self.cls_token_id							,					end_of_segment_id=self.sep_token_id)
     else:  # Paired text
         _UpperCAmelCase			: str     					=			self.unpaired_tokenize(_A)
         if truncation:
             _UpperCAmelCase					,	_UpperCAmelCase			: int     					=			self.paired_trimmer.trim([text, text_pair])
         _UpperCAmelCase					,	_UpperCAmelCase			: int     					=			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			: List[str]     					=			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			: List[str]     					=			pad_to_multiple_of * (-tf.math.floordiv(-pad_length							,					_A))
     else:
         _UpperCAmelCase			: Any     					=			max_length

     _UpperCAmelCase					,	_UpperCAmelCase			: str     					=			pad_model_inputs(_A							,					max_seq_length=_A							,					pad_value=self.pad_token_id)
     _UpperCAmelCase			: str     					=			{'''input_ids''': input_ids}
     if return_attention_mask:
         _UpperCAmelCase			: Optional[Any]     					=			attention_mask
     if return_token_type_ids:
         _UpperCAmelCase					,	_UpperCAmelCase			: Optional[Any]     					=			pad_model_inputs(
             _A							,					max_seq_length=_A							,					pad_value=self.pad_token_id)
         _UpperCAmelCase			: Any     					=			token_type_ids
     return output


 def 	snake_case__			(		self)	->						List[Any]:

     """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 collections import OrderedDict
from typing import Mapping

from packaging import version

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


SCREAMING_SNAKE_CASE								=  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE								=  {
    'MBZUAI/swiftformer-xs': 'https://huggingface.co/MBZUAI/swiftformer-xs/resolve/main/config.json',
}
class 							A_      (  __lowercase     ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:     Dict	=   "swiftformer"


 def __init__(		self							,					_A=3							,					_A=[3, 3, 6, 4]							,					_A=[48, 56, 112, 220]							,					_A=4							,					_A=[True, True, True, True]							,					_A="gelu"							,					_A=3							,					_A=2							,					_A=1							,					_A=0.0							,					_A=True							,					_A=1e-5							,					_A=1e-5							,					**_A							,					)	->						List[Any]:

     """simple docstring"""



     super().__init__(**_A)
     _UpperCAmelCase			: Tuple     					=			num_channels
     _UpperCAmelCase			: Optional[int]     					=			depths
     _UpperCAmelCase			: Union[str, Any]     					=			embed_dims
     _UpperCAmelCase			: Dict     					=			mlp_ratio
     _UpperCAmelCase			: Tuple     					=			downsamples
     _UpperCAmelCase			: str     					=			hidden_act
     _UpperCAmelCase			: int     					=			down_patch_size
     _UpperCAmelCase			: Dict     					=			down_stride
     _UpperCAmelCase			: Dict     					=			down_pad
     _UpperCAmelCase			: Tuple     					=			drop_path_rate
     _UpperCAmelCase			: Tuple     					=			use_layer_scale
     _UpperCAmelCase			: int     					=			layer_scale_init_value
     _UpperCAmelCase			: int     					=			batch_norm_eps



class 							A_      (  __lowercase     ):






 '''simple docstring'''




 _SCREAMING_SNAKE_CASE							:     List[Any]	=   version.parse("1.11"     )


 @property
 def 	snake_case__			(		self)	->						Mapping[str, Mapping[int, str]]:

     """simple docstring"""



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


 @property
 def 	snake_case__			(		self)	->						float:

     """simple docstring"""



     return 1e-4




