






import os
import shutil
from pathlib import Path
from typing import Optional, Union

import numpy as np
from huggingface_hub import hf_hub_download

from ..utils import ONNX_EXTERNAL_WEIGHTS_NAME, ONNX_WEIGHTS_NAME, is_onnx_available, logging


if is_onnx_available():
							import onnxruntime as ort


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

A__			:    List[Any]         =				{
    'tensor(bool)': np.bool_,
    'tensor(int8)': np.inta,
    'tensor(uint8)': np.uinta,
    'tensor(int16)': np.intaa,
    'tensor(uint16)': np.uintaa,
    'tensor(int32)': np.intaa,
    'tensor(uint32)': np.uintaa,
    'tensor(int64)': np.intaa,
    'tensor(uint64)': np.uintaa,
    'tensor(float16)': np.floataa,
    'tensor(float)': np.floataa,
    'tensor(double)': np.floataa,
}
class    _UpperCAmelCase      :

				"""simple docstring"""




				def __init__(  self		:					int,						lowerCamelCase		:					Dict=None,						**lowerCamelCase		:					Optional[Any]						):

								'''simple docstring'''
								logger.info('''`diffusers.OnnxRuntimeModel` is experimental and might change in the future.'''						)
								lowercase__     				=    model
								lowercase__     				=    kwargs.get('''model_save_dir''',						lowerCamelCase						)
								lowercase__     				=    kwargs.get('''latest_model_name''',						lowerCamelCase						)




				def __call__(  self		:					str,						**lowerCamelCase		:					Union[str, Any]						):

								'''simple docstring'''
								lowercase__     				=    {k: np.array(lowerCamelCase						) for k, v in kwargs.items()}
								return self.model.run(lowerCamelCase,						lowerCamelCase						)




				@staticmethod
				def        lowercase__     (  lowerCamelCase		:					Union[str, Path],						lowerCamelCase		:					Dict=None,						lowerCamelCase		:					List[Any]=None						):

								'''simple docstring'''
								if provider is None:
												logger.info('''No onnxruntime provider specified, using CPUExecutionProvider'''						)
												lowercase__     				=    '''CPUExecutionProvider'''

								return ort.InferenceSession(lowerCamelCase,						providers=[provider],						sess_options=lowerCamelCase						)




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					Union[str, Path],						lowerCamelCase		:					Optional[str] = None,						**lowerCamelCase		:					Dict						):

								'''simple docstring'''
								lowercase__     				=    file_name if file_name is not None else ONNX_WEIGHTS_NAME

								lowercase__     				=    self.model_save_dir.joinpath(self.latest_model_name						)
								lowercase__     				=    Path(lowerCamelCase						).joinpath(lowerCamelCase						)
								try:
												shutil.copyfile(lowerCamelCase,						lowerCamelCase						)
								except shutil.SameFileError:
												pass

								# copy external weights (for models >2GB)
								lowercase__     				=    self.model_save_dir.joinpath(lowerCamelCase						)
								if src_path.exists():
												lowercase__     				=    Path(lowerCamelCase						).joinpath(lowerCamelCase						)
												try:
																shutil.copyfile(lowerCamelCase,						lowerCamelCase						)
												except shutil.SameFileError:
																pass




				def        lowercase__     (  self		:					Tuple,						lowerCamelCase		:					Union[str, os.PathLike],						**lowerCamelCase		:					str,						):

								'''simple docstring'''
								if os.path.isfile(lowerCamelCase						):
												logger.error(F"""Provided path ({save_directory}) should be a directory, not a file"""						)
												return

								os.makedirs(lowerCamelCase,						exist_ok=lowerCamelCase						)

								# saving model weights/files
								self._save_pretrained(lowerCamelCase,						**lowerCamelCase						)




				@classmethod
				def        lowercase__     (  cls		:					Optional[int],						lowerCamelCase		:					Union[str, Path],						lowerCamelCase		:					Optional[Union[bool, str, None]] = None,						lowerCamelCase		:					Optional[Union[str, None]] = None,						lowerCamelCase		:					bool = False,						lowerCamelCase		:					Optional[str] = None,						lowerCamelCase		:					Optional[str] = None,						lowerCamelCase		:					Optional[str] = None,						lowerCamelCase		:					Optional["ort.SessionOptions"] = None,						**lowerCamelCase		:					List[Any],						):

								'''simple docstring'''
								lowercase__     				=    file_name if file_name is not None else ONNX_WEIGHTS_NAME
								# load model from local directory
								if os.path.isdir(lowerCamelCase						):
												lowercase__     				=    OnnxRuntimeModel.load_model(
												    os.path.join(lowerCamelCase,						lowerCamelCase						),						provider=lowerCamelCase,						sess_options=lowerCamelCase						)
												lowercase__     				=    Path(lowerCamelCase						)
								# load model from hub
								else:
												# download model
												lowercase__     				=    hf_hub_download(
												    repo_id=lowerCamelCase,						filename=lowerCamelCase,						use_auth_token=lowerCamelCase,						revision=lowerCamelCase,						cache_dir=lowerCamelCase,						force_download=lowerCamelCase,						)
												lowercase__     				=    Path(lowerCamelCase						).parent
												lowercase__     				=    Path(lowerCamelCase						).name
												lowercase__     				=    OnnxRuntimeModel.load_model(lowerCamelCase,						provider=lowerCamelCase,						sess_options=lowerCamelCase						)
								return cls(model=lowerCamelCase,						**lowerCamelCase						)




				@classmethod
				def        lowercase__     (  cls		:					List[Any],						lowerCamelCase		:					Union[str, Path],						lowerCamelCase		:					bool = True,						lowerCamelCase		:					Optional[str] = None,						lowerCamelCase		:					Optional[str] = None,						**lowerCamelCase		:					Dict,						):

								'''simple docstring'''
								lowercase__     				=    None
								if len(str(lowerCamelCase						).split('''@'''						)						) == 2:
												lowercase__				,		lowercase__     				=    model_id.split('''@'''						)

								return cls._from_pretrained(
								    model_id=lowerCamelCase,						revision=lowerCamelCase,						cache_dir=lowerCamelCase,						force_download=lowerCamelCase,						use_auth_token=lowerCamelCase,						**lowerCamelCase,						)












import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_bert import BertTokenizer


A__			:    Dict         =				logging.get_logger(__name__)

A__			:    Dict         =				{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

A__			:    Optional[int]         =				{
    'vocab_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/vocab.txt',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/vocab.txt',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/vocab.txt',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/vocab.txt',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/vocab.txt'
        ),
        'bert-base-multilingual-cased': 'https://huggingface.co/bert-base-multilingual-cased/resolve/main/vocab.txt',
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/vocab.txt',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/vocab.txt',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/vocab.txt'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/vocab.txt'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/vocab.txt'
        ),
        'bert-base-german-dbmdz-cased': 'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/vocab.txt',
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/vocab.txt'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/vocab.txt'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/vocab.txt'
        ),
    },
    'tokenizer_file': {
        'bert-base-uncased': 'https://huggingface.co/bert-base-uncased/resolve/main/tokenizer.json',
        'bert-large-uncased': 'https://huggingface.co/bert-large-uncased/resolve/main/tokenizer.json',
        'bert-base-cased': 'https://huggingface.co/bert-base-cased/resolve/main/tokenizer.json',
        'bert-large-cased': 'https://huggingface.co/bert-large-cased/resolve/main/tokenizer.json',
        'bert-base-multilingual-uncased': (
            'https://huggingface.co/bert-base-multilingual-uncased/resolve/main/tokenizer.json'
        ),
        'bert-base-multilingual-cased': (
            'https://huggingface.co/bert-base-multilingual-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-chinese': 'https://huggingface.co/bert-base-chinese/resolve/main/tokenizer.json',
        'bert-base-german-cased': 'https://huggingface.co/bert-base-german-cased/resolve/main/tokenizer.json',
        'bert-large-uncased-whole-word-masking': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking': (
            'https://huggingface.co/bert-large-cased-whole-word-masking/resolve/main/tokenizer.json'
        ),
        'bert-large-uncased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-uncased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-large-cased-whole-word-masking-finetuned-squad': (
            'https://huggingface.co/bert-large-cased-whole-word-masking-finetuned-squad/resolve/main/tokenizer.json'
        ),
        'bert-base-cased-finetuned-mrpc': (
            'https://huggingface.co/bert-base-cased-finetuned-mrpc/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-cased': (
            'https://huggingface.co/bert-base-german-dbmdz-cased/resolve/main/tokenizer.json'
        ),
        'bert-base-german-dbmdz-uncased': (
            'https://huggingface.co/bert-base-german-dbmdz-uncased/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-cased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-cased-v1/resolve/main/tokenizer.json'
        ),
        'TurkuNLP/bert-base-finnish-uncased-v1': (
            'https://huggingface.co/TurkuNLP/bert-base-finnish-uncased-v1/resolve/main/tokenizer.json'
        ),
        'wietsedv/bert-base-dutch-cased': (
            'https://huggingface.co/wietsedv/bert-base-dutch-cased/resolve/main/tokenizer.json'
        ),
    },
}

A__			:    List[str]         =				{
    'bert-base-uncased': 5_12,
    'bert-large-uncased': 5_12,
    'bert-base-cased': 5_12,
    'bert-large-cased': 5_12,
    'bert-base-multilingual-uncased': 5_12,
    'bert-base-multilingual-cased': 5_12,
    'bert-base-chinese': 5_12,
    'bert-base-german-cased': 5_12,
    'bert-large-uncased-whole-word-masking': 5_12,
    'bert-large-cased-whole-word-masking': 5_12,
    'bert-large-uncased-whole-word-masking-finetuned-squad': 5_12,
    'bert-large-cased-whole-word-masking-finetuned-squad': 5_12,
    'bert-base-cased-finetuned-mrpc': 5_12,
    'bert-base-german-dbmdz-cased': 5_12,
    'bert-base-german-dbmdz-uncased': 5_12,
    'TurkuNLP/bert-base-finnish-cased-v1': 5_12,
    'TurkuNLP/bert-base-finnish-uncased-v1': 5_12,
    'wietsedv/bert-base-dutch-cased': 5_12,
}

A__			:    Optional[int]         =				{
    'bert-base-uncased': {'do_lower_case': True},
    'bert-large-uncased': {'do_lower_case': True},
    'bert-base-cased': {'do_lower_case': False},
    'bert-large-cased': {'do_lower_case': False},
    'bert-base-multilingual-uncased': {'do_lower_case': True},
    'bert-base-multilingual-cased': {'do_lower_case': False},
    'bert-base-chinese': {'do_lower_case': False},
    'bert-base-german-cased': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking': {'do_lower_case': False},
    'bert-large-uncased-whole-word-masking-finetuned-squad': {'do_lower_case': True},
    'bert-large-cased-whole-word-masking-finetuned-squad': {'do_lower_case': False},
    'bert-base-cased-finetuned-mrpc': {'do_lower_case': False},
    'bert-base-german-dbmdz-cased': {'do_lower_case': False},
    'bert-base-german-dbmdz-uncased': {'do_lower_case': True},
    'TurkuNLP/bert-base-finnish-cased-v1': {'do_lower_case': False},
    'TurkuNLP/bert-base-finnish-uncased-v1': {'do_lower_case': True},
    'wietsedv/bert-base-dutch-cased': {'do_lower_case': False},
}
class    _UpperCAmelCase      ( A__			):

				"""simple docstring"""




				lowercase__       							= VOCAB_FILES_NAMES
				lowercase__       							= PRETRAINED_VOCAB_FILES_MAP
				lowercase__       							= PRETRAINED_INIT_CONFIGURATION
				lowercase__       							= PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				lowercase__       							= BertTokenizer




				def __init__(  self		:					Any,						lowerCamelCase		:					Optional[Any]=None,						lowerCamelCase		:					Any=None,						lowerCamelCase		:					Tuple=True,						lowerCamelCase		:					Dict="[UNK]",						lowerCamelCase		:					Any="[SEP]",						lowerCamelCase		:					List[Any]="[PAD]",						lowerCamelCase		:					Optional[Any]="[CLS]",						lowerCamelCase		:					Dict="[MASK]",						lowerCamelCase		:					List[Any]=True,						lowerCamelCase		:					Tuple=None,						**lowerCamelCase		:					Dict,						):

								'''simple docstring'''
								super().__init__(
								    lowerCamelCase,						tokenizer_file=lowerCamelCase,						do_lower_case=lowerCamelCase,						unk_token=lowerCamelCase,						sep_token=lowerCamelCase,						pad_token=lowerCamelCase,						cls_token=lowerCamelCase,						mask_token=lowerCamelCase,						tokenize_chinese_chars=lowerCamelCase,						strip_accents=lowerCamelCase,						**lowerCamelCase,						)

								lowercase__     				=    json.loads(self.backend_tokenizer.normalizer.__getstate__()						)
								if (
								    normalizer_state.get('''lowercase''',						lowerCamelCase						) != do_lower_case
								    or normalizer_state.get('''strip_accents''',						lowerCamelCase						) != strip_accents
								    or normalizer_state.get('''handle_chinese_chars''',						lowerCamelCase						) != tokenize_chinese_chars
								):
												lowercase__     				=    getattr(lowerCamelCase,						normalizer_state.pop('''type'''						)						)
												lowercase__     				=    do_lower_case
												lowercase__     				=    strip_accents
												lowercase__     				=    tokenize_chinese_chars
												lowercase__     				=    normalizer_class(**lowerCamelCase						)

								lowercase__     				=    do_lower_case




				def        lowercase__     (  self		:					Any,						lowerCamelCase		:					List[Any],						lowerCamelCase		:					Dict=None						):

								'''simple docstring'''
								lowercase__     				=    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

								if token_ids_a:
												output += token_ids_a + [self.sep_token_id]

								return output




				def        lowercase__     (  self		:					List[Any],						lowerCamelCase		:					List[int],						lowerCamelCase		:					Optional[List[int]] = None						):

								'''simple docstring'''
								lowercase__     				=    [self.sep_token_id]
								lowercase__     				=    [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]




				def        lowercase__     (  self		:					Any,						lowerCamelCase		:					str,						lowerCamelCase		:					Optional[str] = None						):

								'''simple docstring'''
								lowercase__     				=    self._tokenizer.model.save(lowerCamelCase,						name=lowerCamelCase						)
								return tuple(lowerCamelCase						)




