






import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_realm import RealmTokenizer


__lowercase										= logging.get_logger(__name__)

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

__lowercase										= {
    '''vocab_file''': {
        '''google/realm-cc-news-pretrained-embedder''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt'''
        ),
        '''google/realm-cc-news-pretrained-encoder''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt'''
        ),
        '''google/realm-cc-news-pretrained-scorer''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt'''
        ),
        '''google/realm-cc-news-pretrained-openqa''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt'''
        ),
        '''google/realm-orqa-nq-openqa''': '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt''',
        '''google/realm-orqa-nq-reader''': '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt''',
        '''google/realm-orqa-wq-openqa''': '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt''',
        '''google/realm-orqa-wq-reader''': '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt''',
    },
    '''tokenizer_file''': {
        '''google/realm-cc-news-pretrained-embedder''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont'''
        ),
        '''google/realm-cc-news-pretrained-encoder''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json'''
        ),
        '''google/realm-cc-news-pretrained-scorer''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json'''
        ),
        '''google/realm-cc-news-pretrained-openqa''': (
            '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json'''
        ),
        '''google/realm-orqa-nq-openqa''': (
            '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json'''
        ),
        '''google/realm-orqa-nq-reader''': (
            '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json'''
        ),
        '''google/realm-orqa-wq-openqa''': (
            '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json'''
        ),
        '''google/realm-orqa-wq-reader''': (
            '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json'''
        ),
    },
}

__lowercase										= {
    '''google/realm-cc-news-pretrained-embedder''': 512,
    '''google/realm-cc-news-pretrained-encoder''': 512,
    '''google/realm-cc-news-pretrained-scorer''': 512,
    '''google/realm-cc-news-pretrained-openqa''': 512,
    '''google/realm-orqa-nq-openqa''': 512,
    '''google/realm-orqa-nq-reader''': 512,
    '''google/realm-orqa-wq-openqa''': 512,
    '''google/realm-orqa-wq-reader''': 512,
}

__lowercase										= {
    '''google/realm-cc-news-pretrained-embedder''': {'''do_lower_case''': True},
    '''google/realm-cc-news-pretrained-encoder''': {'''do_lower_case''': True},
    '''google/realm-cc-news-pretrained-scorer''': {'''do_lower_case''': True},
    '''google/realm-cc-news-pretrained-openqa''': {'''do_lower_case''': True},
    '''google/realm-orqa-nq-openqa''': {'''do_lower_case''': True},
    '''google/realm-orqa-nq-reader''': {'''do_lower_case''': True},
    '''google/realm-orqa-wq-openqa''': {'''do_lower_case''': True},
    '''google/realm-orqa-wq-reader''': {'''do_lower_case''': True},
}


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						Dict      =				VOCAB_FILES_NAMES
    a__     :						Optional[Any]      =				PRETRAINED_VOCAB_FILES_MAP
    a__     :						Any      =				PRETRAINED_INIT_CONFIGURATION
    a__     :						str      =				PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    a__     :						Dict      =				RealmTokenizer







    def __init__(     self			,			__lowercase=None			,			__lowercase=None			,			__lowercase=True			,			__lowercase="[UNK]"			,			__lowercase="[SEP]"			,			__lowercase="[PAD]"			,			__lowercase="[CLS]"			,			__lowercase="[MASK]"			,			__lowercase=True			,			__lowercase=None			,			**__lowercase			,			)	->     Dict:
        super().__init__(
            __lowercase			,			tokenizer_file=__lowercase			,			do_lower_case=__lowercase			,			unk_token=__lowercase			,			sep_token=__lowercase			,			pad_token=__lowercase			,			cls_token=__lowercase			,			mask_token=__lowercase			,			tokenize_chinese_chars=__lowercase			,			strip_accents=__lowercase			,			**__lowercase			,			)

        __UpperCamelCase					:List[Any]        =		json.loads(self.backend_tokenizer.normalizer.__getstate__())
        if (
            normalizer_state.get('''lowercase'''			,			__lowercase) != do_lower_case
            or normalizer_state.get('''strip_accents'''			,			__lowercase) != strip_accents
            or normalizer_state.get('''handle_chinese_chars'''			,			__lowercase) != tokenize_chinese_chars
        ):
            __UpperCamelCase					:Any        =		getattr(__lowercase			,			normalizer_state.pop('''type'''))
            __UpperCamelCase					:List[Any]        =		do_lower_case
            __UpperCamelCase					:Optional[int]        =		strip_accents
            __UpperCamelCase					:List[str]        =		tokenize_chinese_chars
            __UpperCamelCase					:Optional[int]        =		normalizer_class(**__lowercase)

        __UpperCamelCase					:str        =		do_lower_case







    def       UpperCamelCase__       (     self			,			__lowercase			,			**__lowercase)	->     Optional[Any]:
        __UpperCamelCase					:Optional[Any]        =		PaddingStrategy.MAX_LENGTH

        __UpperCamelCase					:Optional[int]        =		text
        __UpperCamelCase					:Tuple        =		kwargs.pop('''text_pair'''			,			__lowercase)
        __UpperCamelCase					:str        =		kwargs.pop('''return_tensors'''			,			__lowercase)

        __UpperCamelCase					:str        =		{
            '''input_ids''': [],
            '''attention_mask''': [],
            '''token_type_ids''': [],
        }

        for idx, candidate_text in enumerate(__lowercase):
            if batch_text_pair is not None:
                __UpperCamelCase					:str        =		batch_text_pair[idx]
            else:
                __UpperCamelCase					:List[str]        =		None

            __UpperCamelCase					:Tuple        =		super().__call__(__lowercase			,			__lowercase			,			return_tensors=__lowercase			,			**__lowercase)

            __UpperCamelCase					:Any        =		encoded_candidates.get('''input_ids''')
            __UpperCamelCase					:Optional[Any]        =		encoded_candidates.get('''attention_mask''')
            __UpperCamelCase					:Any        =		encoded_candidates.get('''token_type_ids''')

            if encoded_input_ids is not None:
                output_data["input_ids"].append(__lowercase)
            if encoded_attention_mask is not None:
                output_data["attention_mask"].append(__lowercase)
            if encoded_token_type_ids is not None:
                output_data["token_type_ids"].append(__lowercase)

        __UpperCamelCase					:List[str]        =		{key: item for key, item in output_data.items() if len(__lowercase) != 0}

        return BatchEncoding(__lowercase			,			tensor_type=__lowercase)







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase=None)	->     Any:
        __UpperCamelCase					:Tuple        =		[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       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None)	->     List[int]:
        __UpperCamelCase					:Optional[int]        =		[self.sep_token_id]
        __UpperCamelCase					:Union[str, Any]        =		[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       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = None)	->     Tuple[str]:
        __UpperCamelCase					:Optional[int]        =		self._tokenizer.model.save(__lowercase			,			name=__lowercase)
        return tuple(__lowercase)






from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


__lowercase										= logging.get_logger(__name__)

__lowercase										= {
    '''EleutherAI/gpt-j-6B''': '''https://huggingface.co/EleutherAI/gpt-j-6B/resolve/main/config.json''',
    # See all GPT-J models at https://huggingface.co/models?filter=gpt_j
}


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    a__     :						Optional[Any]      =				"""gptj"""
    a__     :						Tuple      =				{
        """max_position_embeddings""": """n_positions""",
        """hidden_size""": """n_embd""",
        """num_attention_heads""": """n_head""",
        """num_hidden_layers""": """n_layer""",
    }







    def __init__(     self			,			__lowercase=50_400			,			__lowercase=2_048			,			__lowercase=4_096			,			__lowercase=28			,			__lowercase=16			,			__lowercase=64			,			__lowercase=None			,			__lowercase="gelu_new"			,			__lowercase=0.0			,			__lowercase=0.0			,			__lowercase=0.0			,			__lowercase=1E-5			,			__lowercase=0.02			,			__lowercase=True			,			__lowercase=50_256			,			__lowercase=50_256			,			__lowercase=False			,			**__lowercase			,			)	->     Tuple:
        __UpperCamelCase					:Any        =		vocab_size
        __UpperCamelCase					:Optional[int]        =		n_positions
        __UpperCamelCase					:Tuple        =		n_embd
        __UpperCamelCase					:int        =		n_layer
        __UpperCamelCase					:Any        =		n_head
        __UpperCamelCase					:Any        =		n_inner
        __UpperCamelCase					:Dict        =		rotary_dim
        __UpperCamelCase					:Tuple        =		activation_function
        __UpperCamelCase					:Optional[Any]        =		resid_pdrop
        __UpperCamelCase					:Any        =		embd_pdrop
        __UpperCamelCase					:List[str]        =		attn_pdrop
        __UpperCamelCase					:str        =		layer_norm_epsilon
        __UpperCamelCase					:List[Any]        =		initializer_range
        __UpperCamelCase					:Dict        =		use_cache

        __UpperCamelCase					:List[Any]        =		bos_token_id
        __UpperCamelCase					:Tuple        =		eos_token_id

        super().__init__(
            bos_token_id=__lowercase			,			eos_token_id=__lowercase			,			tie_word_embeddings=__lowercase			,			**__lowercase)


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def __init__(     self			,			__lowercase			,			__lowercase = "default"			,			__lowercase = None			,			__lowercase = False			,			)	->     Any:
        super().__init__(__lowercase			,			task=__lowercase			,			patching_specs=__lowercase			,			use_past=__lowercase)
        if not getattr(self._config			,			'''pad_token_id'''			,			__lowercase):
            # TODO: how to do that better?
            __UpperCamelCase					:Tuple        =		0







    @property
    def       UpperCamelCase__       (     self)	->     Mapping[str, Mapping[int, str]]:
        __UpperCamelCase					:Tuple        =		OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}})
        if self.use_past:
            self.fill_with_past_key_values_(__lowercase			,			direction='''inputs''')
            __UpperCamelCase					:str        =		{0: '''batch''', 1: '''past_sequence + sequence'''}
        else:
            __UpperCamelCase					:Any        =		{0: '''batch''', 1: '''sequence'''}

        return common_inputs







    @property
    def       UpperCamelCase__       (     self)	->     int:
        return self._config.n_layer







    @property
    def       UpperCamelCase__       (     self)	->     int:
        return self._config.n_head







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase = -1			,			__lowercase = -1			,			__lowercase = False			,			__lowercase = None			,			)	->     Mapping[str, Any]:
        __UpperCamelCase					:Optional[int]        =		super(__lowercase			,			self).generate_dummy_inputs(
            __lowercase			,			batch_size=__lowercase			,			seq_length=__lowercase			,			is_pair=__lowercase			,			framework=__lowercase)

        # We need to order the input in the way they appears in the forward()
        __UpperCamelCase					:int        =		OrderedDict({'''input_ids''': common_inputs['''input_ids''']})

        # Need to add the past_keys
        if self.use_past:
            if not is_torch_available():
                raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.''')
            else:
                import torch

                __UpperCamelCase  ,	__UpperCamelCase					:str        =		common_inputs['''input_ids'''].shape
                # Not using the same length for past_key_values
                __UpperCamelCase					:List[str]        =		seqlen + 2
                __UpperCamelCase					:Union[str, Any]        =		(
                    batch,
                    self.num_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // self.num_attention_heads,
                )
                __UpperCamelCase					:Tuple        =		[
                    (torch.zeros(__lowercase), torch.zeros(__lowercase)) for _ in range(self.num_layers)
                ]

        __UpperCamelCase					:Tuple        =		common_inputs['''attention_mask''']
        if self.use_past:
            __UpperCamelCase					:Tuple        =		ordered_inputs['''attention_mask'''].dtype
            __UpperCamelCase					:Optional[Any]        =		torch.cat(
                [ordered_inputs['''attention_mask'''], torch.ones(__lowercase			,			__lowercase			,			dtype=__lowercase)]			,			dim=1)

        return ordered_inputs







    @property
    def       UpperCamelCase__       (     self)	->     int:
        return 13





