

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

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
    if is_torch_available():
        import torch
    if is_tf_available():
        import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


UpperCamelCase__		   =    logging.get_logger(__name__)

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

UpperCamelCase__		   =    {
    '''vocab_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt''',
    },
}

UpperCamelCase__		   =    {
    '''Salesforce/codegen-350M-mono''': 2048,
}







@lru_cache()
def 	lowerCamelCase__			(   ):


       """simple docstring"""




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







def 	lowerCamelCase__			(   __A						:Optional[int]		):


       """simple docstring"""




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




class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       VOCAB_FILES_NAMES
       __SCREAMING_SNAKE_CASE													=       PRETRAINED_VOCAB_FILES_MAP
       __SCREAMING_SNAKE_CASE													=       PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
       __SCREAMING_SNAKE_CASE													=       ["input_ids", "attention_mask"]


       def __init__(		self			,   _UpperCamelCase			,   _UpperCamelCase			,   _UpperCamelCase="replace"			,   _UpperCamelCase="<|endoftext|>"			,   _UpperCamelCase="<|endoftext|>"			,   _UpperCamelCase="<|endoftext|>"			,   _UpperCamelCase=None			,   _UpperCamelCase=False			,   _UpperCamelCase=False			,   **_UpperCamelCase			,   )			->       int:






              """simple docstring"""


              __snake_case					  =       AddedToken(_UpperCamelCase			,   lstrip=_UpperCamelCase			,   rstrip=_UpperCamelCase      ) if isinstance(_UpperCamelCase			,   _UpperCamelCase      ) else bos_token
              __snake_case					  =       AddedToken(_UpperCamelCase			,   lstrip=_UpperCamelCase			,   rstrip=_UpperCamelCase      ) if isinstance(_UpperCamelCase			,   _UpperCamelCase      ) else eos_token
              __snake_case					  =       AddedToken(_UpperCamelCase			,   lstrip=_UpperCamelCase			,   rstrip=_UpperCamelCase      ) if isinstance(_UpperCamelCase			,   _UpperCamelCase      ) else unk_token
              __snake_case					  =       AddedToken(_UpperCamelCase			,   lstrip=_UpperCamelCase			,   rstrip=_UpperCamelCase      ) if isinstance(_UpperCamelCase			,   _UpperCamelCase      ) else pad_token
              super().__init__(
                  errors=_UpperCamelCase			,   unk_token=_UpperCamelCase			,   bos_token=_UpperCamelCase			,   eos_token=_UpperCamelCase			,   pad_token=_UpperCamelCase			,   add_prefix_space=_UpperCamelCase			,   add_bos_token=_UpperCamelCase			,   **_UpperCamelCase			,   )
              __snake_case					  =       add_bos_token

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

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


       @property
       def  a		(		self      )			->       int:






              """simple docstring"""


              return len(self.encoder      )


       def  a		(		self      )			->       Optional[Any]:






              """simple docstring"""


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


       def  a		(		self			,   _UpperCamelCase      )			->       Optional[int]:






              """simple docstring"""


              if token in self.cache:
                     return self.cache[token]
              __snake_case					  =       tuple(_UpperCamelCase      )
              __snake_case					  =       get_pairs(_UpperCamelCase      )

              if not pairs:
                     return token

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

                            if word[i] == first and i < len(_UpperCamelCase      ) - 1 and word[i + 1] == second:
                                   new_word.append(first + second      )
                                   i += 2
                            else:
                                   new_word.append(word[i]      )
                                   i += 1
                     __snake_case					  =       tuple(_UpperCamelCase      )
                     __snake_case					  =       new_word
                     if len(_UpperCamelCase      ) == 1:
                            break
                     else:
                            __snake_case					  =       get_pairs(_UpperCamelCase      )
              __snake_case					  =       """ """.join(_UpperCamelCase      )
              __snake_case					  =       word
              return word


       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase=None      )			->       Dict:






              """simple docstring"""


              if self.add_bos_token:
                     __snake_case					  =       [self.bos_token_id]
              else:
                     __snake_case					  =       []

              __snake_case					  =       bos_token_ids + token_ids_a

              if token_ids_a is None:
                     return output

              return output + bos_token_ids + token_ids_a


       def  a		(		self			,   _UpperCamelCase      )			->       Optional[Any]:






              """simple docstring"""


              __snake_case					  =       []
              for token in re.findall(self.pat			,   _UpperCamelCase      ):
                     __snake_case					  =       """""".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(_UpperCamelCase      ).split(""" """      )      )
              return bpe_tokens


       def  a		(		self			,   _UpperCamelCase      )			->       Dict:






              """simple docstring"""


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


       def  a		(		self			,   _UpperCamelCase      )			->       Union[str, Any]:






              """simple docstring"""


              return self.decoder.get(_UpperCamelCase      )


       def  a		(		self			,   _UpperCamelCase      )			->       Optional[int]:






              """simple docstring"""


              __snake_case					  =       """""".join(_UpperCamelCase      )
              __snake_case					  =       bytearray([self.byte_decoder[c] for c in text]      ).decode("""utf-8"""			,   errors=self.errors      )
              return text


       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase = None      )			->       Tuple[str]:






              """simple docstring"""


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

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

              __snake_case					  =       0
              with open(_UpperCamelCase			,   """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 _UpperCamelCase      : 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!"""      )
                                   __snake_case					  =       token_index
                            writer.write(""" """.join(_UpperCamelCase      ) + """\n"""      )
                            index += 1

              return vocab_file, merge_file


       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase=False			,   **_UpperCamelCase      )			->       Any:






              """simple docstring"""


              __snake_case					  =       kwargs.pop("""add_prefix_space"""			,   self.add_prefix_space      )
              if is_split_into_words or add_prefix_space:
                     __snake_case					  =       """ """ + text
              return (text, kwargs)


       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase = False			,   _UpperCamelCase = None			,   _UpperCamelCase = None			,   **_UpperCamelCase			,   )			->       str:






              """simple docstring"""


              __snake_case					  =       super()._decode(
                  token_ids=_UpperCamelCase			,   skip_special_tokens=_UpperCamelCase			,   clean_up_tokenization_spaces=_UpperCamelCase			,   **_UpperCamelCase			,   )

              if truncate_before_pattern is not None and len(_UpperCamelCase      ) > 0:
                     __snake_case					  =       self.truncate(_UpperCamelCase			,   _UpperCamelCase      )

              return decoded_text






       def  a		(		self			,   _UpperCamelCase			,   _UpperCamelCase      )			->       Union[str, Any]:






              """simple docstring"""


              def find_re(_UpperCamelCase			,   _UpperCamelCase			,   _UpperCamelCase      ):
                     __snake_case					  =       pattern.search(_UpperCamelCase			,   _UpperCamelCase      )
                     return m.start() if m else -1

              __snake_case					  =       [re.compile(_UpperCamelCase			,   re.MULTILINE      ) for pattern in truncate_before_pattern]

              __snake_case					  =       list(re.finditer("""^print"""			,   _UpperCamelCase			,   re.MULTILINE      )      )

              if len(_UpperCamelCase      ) > 1:
                     __snake_case					  =       completion[: prints[1].start()]

              __snake_case					  =       list(re.finditer("""^def"""			,   _UpperCamelCase			,   re.MULTILINE      )      )

              if len(_UpperCamelCase      ) > 1:
                     __snake_case					  =       completion[: defs[1].start()]

              __snake_case					  =       0

              __snake_case					  =       [
                  pos for pos in [find_re(_UpperCamelCase			,   _UpperCamelCase			,   _UpperCamelCase      ) for terminal in terminals] if pos != -1
              ]

              if len(_UpperCamelCase      ) > 0:
                     return completion[: min(_UpperCamelCase      )]
              else:
                     return completion




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


UpperCamelCase__		   =    logging.get_logger(__name__)

UpperCamelCase__		   =    {
    '''apple/mobilevitv2-1.0''': '''https://huggingface.co/apple/mobilevitv2-1.0/resolve/main/config.json''',
}

class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       "mobilevitv2"


       def __init__(		self			,   _UpperCamelCase=3			,   _UpperCamelCase=2_56			,   _UpperCamelCase=2			,   _UpperCamelCase=2.0			,   _UpperCamelCase="swish"			,   _UpperCamelCase=3			,   _UpperCamelCase=32			,   _UpperCamelCase=0.1			,   _UpperCamelCase=0.02			,   _UpperCamelCase=1E-5			,   _UpperCamelCase=5_12			,   _UpperCamelCase=[6, 12, 18]			,   _UpperCamelCase=0.1			,   _UpperCamelCase=2_55			,   _UpperCamelCase=[2, 4, 3]			,   _UpperCamelCase=[1_28, 1_92, 2_56]			,   _UpperCamelCase=1.0			,   _UpperCamelCase=2			,   _UpperCamelCase=0.0			,   _UpperCamelCase=0.0			,   **_UpperCamelCase			,   )			->       List[Any]:






              """simple docstring"""


              super().__init__(**_UpperCamelCase      )

              __snake_case					  =       num_channels
              __snake_case					  =       image_size
              __snake_case					  =       patch_size
              __snake_case					  =       expand_ratio
              __snake_case					  =       hidden_act
              __snake_case					  =       conv_kernel_size
              __snake_case					  =       output_stride
              __snake_case					  =       initializer_range
              __snake_case					  =       layer_norm_eps
              __snake_case					  =       n_attn_blocks
              __snake_case					  =       base_attn_unit_dims
              __snake_case					  =       width_multiplier
              __snake_case					  =       ffn_multiplier
              __snake_case					  =       ffn_dropout
              __snake_case					  =       attn_dropout
              __snake_case					  =       classifier_dropout_prob

              # decode head attributes for semantic segmentation
              __snake_case					  =       aspp_out_channels
              __snake_case					  =       atrous_rates
              __snake_case					  =       aspp_dropout_prob
              __snake_case					  =       semantic_loss_ignore_index






class 						__snake_case						(							snake_case__     ):
       """simple docstring"""







       __SCREAMING_SNAKE_CASE													=       version.parse("1.11"     )


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






              """simple docstring"""


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


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






              """simple docstring"""


              if self.task == "image-classification":
                     return OrderedDict([("""logits""", {0: """batch"""})]      )
              else:
                     return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]      )






       @property
       def  a		(		self      )			->       float:






              """simple docstring"""


              return 1E-4

