import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__lowercase										= logging.getLogger(__name__)


@dataclass(frozen=UpperCAmelCase_			)
class      lowerCamelCase_					:






    '''simple docstring'''






    a__     :						str
    a__     :						str
    a__     :						Optional[str]      =				None
    a__     :						Optional[str]      =				None
    a__     :						Optional[str]      =				None


@dataclass(frozen=UpperCAmelCase_			)
class      lowerCamelCase_					:






    '''simple docstring'''






    a__     :						List[int]
    a__     :						Optional[List[int]]      =				None
    a__     :						Optional[List[int]]      =				None
    a__     :						Optional[Union[int, float]]      =				None
    a__     :						Optional[int]      =				None


if is_torch_available():
    import torch
    from torch.utils.data import Dataset


    class      lowerCamelCase_					(       UpperCAmelCase_			):






        '''simple docstring'''






        a__     :						List[InputFeatures]







        def __init__(     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = None			,			__lowercase=False			,			__lowercase = False			,			)	->     Dict:
            __UpperCamelCase					:List[Any]        =		hans_processors[task]()

            __UpperCamelCase					:int        =		os.path.join(
                __lowercase			,			'''cached_{}_{}_{}_{}'''.format(
                    '''dev''' if evaluate else '''train'''			,			tokenizer.__class__.__name__			,			str(__lowercase)			,			__lowercase			,			)			,			)
            __UpperCamelCase					:List[str]        =		processor.get_labels()
            if tokenizer.__class__ in (
                RobertaTokenizer,
                RobertaTokenizerFast,
                XLMRobertaTokenizer,
                BartTokenizer,
                BartTokenizerFast,
            ):
                # HACK(label indices are swapped in RoBERTa pretrained model)
                __UpperCamelCase  ,	__UpperCamelCase					:str        =		label_list[2], label_list[1]
            __UpperCamelCase					:List[str]        =		label_list

            # Make sure only the first process in distributed training processes the dataset,
            # and the others will use the cache.
            __UpperCamelCase					:Dict        =		cached_features_file + '''.lock'''
            with FileLock(__lowercase):
                if os.path.exists(__lowercase) and not overwrite_cache:
                    logger.info(f"""Loading features from cached file {cached_features_file}""")
                    __UpperCamelCase					:int        =		torch.load(__lowercase)
                else:
                    logger.info(f"""Creating features from dataset file at {data_dir}""")

                    __UpperCamelCase					:Tuple        =		(
                        processor.get_dev_examples(__lowercase) if evaluate else processor.get_train_examples(__lowercase)
                    )

                    logger.info('''Training examples: %s'''			,			len(__lowercase))
                    __UpperCamelCase					:Any        =		hans_convert_examples_to_features(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)
                    logger.info('''Saving features into cached file %s'''			,			__lowercase)
                    torch.save(self.features			,			__lowercase)







        def __len__(     self)	->     Optional[int]:
            return len(self.features)







        def __getitem__(     self			,			__lowercase)	->     InputFeatures:
            return self.features[i]







        def       UpperCamelCase__       (     self)	->     Optional[Any]:
            return self.label_list


if is_tf_available():
    import tensorflow as tf


    class      lowerCamelCase_					:






        '''simple docstring'''






        a__     :						List[InputFeatures]







        def __init__(     self			,			__lowercase			,			__lowercase			,			__lowercase			,			__lowercase = 128			,			__lowercase=False			,			__lowercase = False			,			)	->     Any:
            __UpperCamelCase					:Any        =		hans_processors[task]()
            __UpperCamelCase					:List[str]        =		processor.get_labels()
            if tokenizer.__class__ in (
                RobertaTokenizer,
                RobertaTokenizerFast,
                XLMRobertaTokenizer,
                BartTokenizer,
                BartTokenizerFast,
            ):
                # HACK(label indices are swapped in RoBERTa pretrained model)
                __UpperCamelCase  ,	__UpperCamelCase					:List[Any]        =		label_list[2], label_list[1]
            __UpperCamelCase					:Tuple        =		label_list

            __UpperCamelCase					:int        =		processor.get_dev_examples(__lowercase) if evaluate else processor.get_train_examples(__lowercase)
            __UpperCamelCase					:Any        =		hans_convert_examples_to_features(__lowercase			,			__lowercase			,			__lowercase			,			__lowercase)

            def gen():
                for ex_index, ex in tqdm.tqdm(enumerate(self.features)			,			desc='''convert examples to features'''):
                    if ex_index % 10_000 == 0:
                        logger.info('''Writing example %d of %d''' % (ex_index, len(__lowercase)))

                    yield (
                        {
                            "example_id": 0,
                            "input_ids": ex.input_ids,
                            "attention_mask": ex.attention_mask,
                            "token_type_ids": ex.token_type_ids,
                        },
                        ex.label,
                    )

            __UpperCamelCase					:Union[str, Any]        =		tf.data.Dataset.from_generator(
                __lowercase			,			(
                    {
                        '''example_id''': tf.intaa,
                        '''input_ids''': tf.intaa,
                        '''attention_mask''': tf.intaa,
                        '''token_type_ids''': tf.intaa,
                    },
                    tf.intaa,
                )			,			(
                    {
                        '''example_id''': tf.TensorShape([]),
                        '''input_ids''': tf.TensorShape([None, None]),
                        '''attention_mask''': tf.TensorShape([None, None]),
                        '''token_type_ids''': tf.TensorShape([None, None]),
                    },
                    tf.TensorShape([]),
                )			,			)







        def       UpperCamelCase__       (     self)	->     Union[str, Any]:
            return self.dataset







        def __len__(     self)	->     str:
            return len(self.features)







        def __getitem__(     self			,			__lowercase)	->     InputFeatures:
            return self.features[i]







        def       UpperCamelCase__       (     self)	->     int:
            return self.label_list


class      lowerCamelCase_					(       UpperCAmelCase_			):






    '''simple docstring'''






    def       UpperCamelCase__       (     self			,			__lowercase)	->     Optional[int]:
        return self._create_examples(self._read_tsv(os.path.join(__lowercase			,			'''heuristics_train_set.txt'''))			,			'''train''')







    def       UpperCamelCase__       (     self			,			__lowercase)	->     Optional[int]:
        return self._create_examples(self._read_tsv(os.path.join(__lowercase			,			'''heuristics_evaluation_set.txt'''))			,			'''dev''')







    def       UpperCamelCase__       (     self)	->     List[str]:
        return ["contradiction", "entailment", "neutral"]







    def       UpperCamelCase__       (     self			,			__lowercase			,			__lowercase)	->     Tuple:
        __UpperCamelCase					:Union[str, Any]        =		[]
        for i, line in enumerate(__lowercase):
            if i == 0:
                continue
            __UpperCamelCase					:Dict        =		'''%s-%s''' % (set_type, line[0])
            __UpperCamelCase					:int        =		line[5]
            __UpperCamelCase					:Any        =		line[6]
            __UpperCamelCase					:Union[str, Any]        =		line[7][2:] if line[7].startswith('''ex''') else line[7]
            __UpperCamelCase					:Dict        =		line[0]
            examples.append(InputExample(guid=__lowercase			,			text_a=__lowercase			,			text_b=__lowercase			,			label=__lowercase			,			pairID=__lowercase))
        return examples



def lowerCamelCase  (       SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      SCREAMING_SNAKE_CASE  ,      ):
    '''simple docstring'''







    __UpperCamelCase					:str        =		{label: i for i, label in enumerate(SCREAMING_SNAKE_CASE     )}

    __UpperCamelCase					:List[Any]        =		[]
    for ex_index, example in tqdm.tqdm(enumerate(SCREAMING_SNAKE_CASE     )  ,      desc='''convert examples to features'''     ):
        if ex_index % 10_000 == 0:
            logger.info('''Writing example %d''' % (ex_index)     )

        __UpperCamelCase					:Any        =		tokenizer(
            example.text_a  ,      example.text_b  ,      add_special_tokens=SCREAMING_SNAKE_CASE  ,      max_length=SCREAMING_SNAKE_CASE  ,      padding='''max_length'''  ,      truncation=SCREAMING_SNAKE_CASE  ,      return_overflowing_tokens=SCREAMING_SNAKE_CASE  ,      )

        __UpperCamelCase					:Optional[int]        =		label_map[example.label] if example.label in label_map else 0

        __UpperCamelCase					:Union[str, Any]        =		int(example.pairID     )

        features.append(InputFeatures(**SCREAMING_SNAKE_CASE  ,      label=SCREAMING_SNAKE_CASE  ,      pairID=SCREAMING_SNAKE_CASE     )     )

    for i, example in enumerate(examples[:5]     ):
        logger.info('''*** Example ***'''     )
        logger.info(f"""guid: {example}"""     )
        logger.info(f"""features: {features[i]}"""     )

    return features


__lowercase										= {
    '''hans''': 3,
}
__lowercase										= {
    '''hans''': HansProcessor,
}






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





