





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	):







					'''simple docstring'''

					def __init__(    self     :	List[Any]			,       __lowercase     :	List			,       __lowercase     :	bool			,       __lowercase     :	int = None			,       __lowercase     :	int = None			,       __lowercase     :	int = None			,       __lowercase     :	str = "longest"			,       __lowercase     :	bool = True			,       __lowercase     :	int = 5_12			,       __lowercase     :	int = None			,       __lowercase     :	bool = True			,       __lowercase     :	bool = True			,       __lowercase     :	bool = True			,       **__lowercase     :	Union[str, Any]			,       ):






						'''simple docstring'''







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

						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 							SCREAMING_SNAKE_CASE				(    cls     :	Optional[Any]			,       __lowercase     :	"PreTrainedTokenizerBase"			,       **__lowercase     :	int		):  # noqa: F821






						'''simple docstring'''







						UpperCAmelCase_		    =  kwargs.pop("""do_lower_case"""			,       __lowercase		)
						UpperCAmelCase_		    =  tokenizer.do_lower_case if do_lower_case is None else do_lower_case
						UpperCAmelCase_		    =  kwargs.pop("""cls_token_id"""			,       __lowercase		)
						UpperCAmelCase_		    =  tokenizer.cls_token_id if cls_token_id is None else cls_token_id
						UpperCAmelCase_		    =  kwargs.pop("""sep_token_id"""			,       __lowercase		)
						UpperCAmelCase_		    =  tokenizer.sep_token_id if sep_token_id is None else sep_token_id
						UpperCAmelCase_		    =  kwargs.pop("""pad_token_id"""			,       __lowercase		)
						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 __lowercase		: x[1]		)
						UpperCAmelCase_		    =  [entry[0] for entry in vocab]
						return cls(
						    vocab_list=__lowercase			,       do_lower_case=__lowercase			,       cls_token_id=__lowercase			,       sep_token_id=__lowercase			,       pad_token_id=__lowercase			,       **__lowercase			,       )





					@classmethod
					def 							SCREAMING_SNAKE_CASE				(    cls     :	Dict			,       __lowercase     :	Union[str, os.PathLike]			,       *__lowercase     :	Optional[Any]			,       **__lowercase     :	List[str]		):






						'''simple docstring'''







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

							UpperCAmelCase_		    =  BertTokenizerFast.from_pretrained(__lowercase			,       *__lowercase			,       **__lowercase		)
						return cls.from_tokenizer(__lowercase			,       **__lowercase		)





					def 							SCREAMING_SNAKE_CASE				(    self     :	int			,       __lowercase     :	List[str]		):






						'''simple docstring'''







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





					def 							SCREAMING_SNAKE_CASE				(    self     :	List[Any]			,       __lowercase     :	Union[str, Any]			,       __lowercase     :	Dict=None			,       __lowercase     :	List[str]=None			,       __lowercase     :	Tuple=None			,       __lowercase     :	List[Any]=None			,       __lowercase     :	Any=None			,       __lowercase     :	Optional[Any]=None			,       __lowercase     :	Tuple=None			,       ):






						'''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(__lowercase			,       tf.Tensor		):
							UpperCAmelCase_		    =  tf.convert_to_tensor(__lowercase		)
						if text_pair is not None and not isinstance(__lowercase			,       tf.Tensor		):
							UpperCAmelCase_		    =  tf.convert_to_tensor(__lowercase		)
						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(__lowercase		)
						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(__lowercase		)
							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			,       __lowercase		))
						else:
							UpperCAmelCase_		    =  max_length

						UpperCAmelCase_    ,						UpperCAmelCase_		    =  pad_model_inputs(__lowercase			,       max_seq_length=__lowercase			,       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(
							    __lowercase			,       max_seq_length=__lowercase			,       pad_value=self.pad_token_id		)
							UpperCAmelCase_		    =  token_type_ids
						return output





					def 							SCREAMING_SNAKE_CASE				(    self     :	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 typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_torch_available,
    is_vision_available,
)


UpperCamelCase__ :			Union[str, Any]       	=			{
    """configuration_mobilevitv2""": [
        """MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP""",
        """MobileViTV2Config""",
        """MobileViTV2OnnxConfig""",
    ],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				UpperCamelCase__ :			int       	=			[
				    """MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """MobileViTV2ForImageClassification""",
				    """MobileViTV2ForSemanticSegmentation""",
				    """MobileViTV2Model""",
				    """MobileViTV2PreTrainedModel""",
				]

if TYPE_CHECKING:
				from .configuration_mobilevitva import (
				    MOBILEVITV2_PRETRAINED_CONFIG_ARCHIVE_MAP,
				    MobileViTVaConfig,
				    MobileViTVaOnnxConfig,
				)

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_mobilevitva import (
								    MOBILEVITV2_PRETRAINED_MODEL_ARCHIVE_LIST,
								    MobileViTVaForImageClassification,
								    MobileViTVaForSemanticSegmentation,
								    MobileViTVaModel,
								    MobileViTVaPreTrainedModel,
								)

else:
				import sys

				UpperCamelCase__ :			List[Any]       	=			_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)


