






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


lowerCamelCase			:					int         =	logging.getLogger(__name__)



@dataclass(frozen=a_			)
class 							_UpperCamelCase			:
      snake_case_  =							42
      snake_case_  =							42
      snake_case_  =							None
      snake_case_  =							None
      snake_case_  =							None



@dataclass(frozen=a_			)
class 							_UpperCamelCase			:
      snake_case_  =							42
      snake_case_  =							None
      snake_case_  =							None
      snake_case_  =							None
      snake_case_  =							None


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



 class 							_UpperCamelCase			(a_			):
       snake_case_  =							42


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

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

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

                      __lowerCAmelCase								=					(
                          processor.get_dev_examples(__UpperCamelCase	) if evaluate else processor.get_train_examples(__UpperCamelCase	)
                      )

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


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


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


       def  __UpperCAmelCase		(	self	)->	Optional[int]:
            return self.label_list


if is_tf_available():
 import tensorflow as tf



 class 							_UpperCamelCase			:
       snake_case_  =							42


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

            __lowerCAmelCase								=					processor.get_dev_examples(__UpperCamelCase	) if evaluate else processor.get_train_examples(__UpperCamelCase	)
            __lowerCAmelCase								=					hans_convert_examples_to_features(__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)

            def gen():
                 for ex_index, ex in tqdm.tqdm(enumerate(self.features	)					,						desc="convert examples to features"	):
                      if ex_index % 1_0_0_0_0 == 0:
                           logger.info("Writing example %d of %d" % (ex_index, len(__UpperCamelCase	))	)

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

            __lowerCAmelCase								=					tf.data.Dataset.from_generator(
                __UpperCamelCase					,						(
                    {
                        "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	)->	Optional[int]:
            return self.dataset


       def __len__(	self	)->	List[Any]:
            return len(self.features	)


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


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



class 							_UpperCamelCase			(a_			):


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase	)->	List[str]:
           return self._create_examples(self._read_tsv(os.path.join(__UpperCamelCase					,						"heuristics_train_set.txt"	)	)					,						"train"	)


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase	)->	Dict:
           return self._create_examples(self._read_tsv(os.path.join(__UpperCamelCase					,						"heuristics_evaluation_set.txt"	)	)					,						"dev"	)


      def  __UpperCAmelCase		(	self	)->	Optional[Any]:
           return ["contradiction", "entailment", "neutral"]


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase	)->	Optional[Any]:
           __lowerCAmelCase								=					[]
           for i, line in enumerate(__UpperCamelCase	):
                if i == 0:
                     continue
                __lowerCAmelCase								=					"%s-%s" % (set_type, line[0])
                __lowerCAmelCase								=					line[5]
                __lowerCAmelCase								=					line[6]
                __lowerCAmelCase								=					line[7][2:] if line[7].startswith("ex"	) else line[7]
                __lowerCAmelCase								=					line[0]
                examples.append(InputExample(guid=__UpperCamelCase					,						text_a=__UpperCamelCase					,						text_b=__UpperCamelCase					,						label=__UpperCamelCase					,						pairID=__UpperCamelCase	)	)
           return examples





def       __lowerCAmelCase      (						__snake_case					, __snake_case					, __snake_case					, __snake_case					, ):
     __lowerCAmelCase								=					{label: i for i, label in enumerate(__snake_case       )}

     __lowerCAmelCase								=					[]
     for ex_index, example in tqdm.tqdm(enumerate(__snake_case       )					, desc="convert examples to features"       ):
          if ex_index % 1_0000 == 0:
               logger.info("Writing example %d" % (ex_index)       )

          __lowerCAmelCase								=					tokenizer(
              example.text_a					, example.text_b					, add_special_tokens=__snake_case					, max_length=__snake_case					, padding="max_length"					, truncation=__snake_case					, return_overflowing_tokens=__snake_case					, )

          __lowerCAmelCase								=					label_map[example.label] if example.label in label_map else 0

          __lowerCAmelCase								=					int(example.pairID       )

          features.append(InputFeatures(**__snake_case					, label=__snake_case					, pairID=__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


lowerCamelCase			:					Union[str, Any]         =	{
    '''hans''': 3,
}



lowerCamelCase			:					Optional[int]         =	{
    '''hans''': HansProcessor,
}














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

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


lowerCamelCase			:					List[Any]         =	logging.get_logger(__name__)

lowerCamelCase			:					Union[str, Any]         =	{
    '''facebook/blenderbot_small-90M''': '''https://huggingface.co/facebook/blenderbot_small-90M/resolve/main/config.json''',
    # See all BlenderbotSmall models at https://huggingface.co/models?filter=blenderbot_small
}



class 							_UpperCamelCase			(a_			):
      snake_case_  =							"""blenderbot-small"""
      snake_case_  =							["""past_key_values"""]
      snake_case_  =							{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}


      def __init__(	self					,						__UpperCamelCase=5_0_2_6_5					,						__UpperCamelCase=5_1_2					,						__UpperCamelCase=8					,						__UpperCamelCase=2_0_4_8					,						__UpperCamelCase=1_6					,						__UpperCamelCase=8					,						__UpperCamelCase=2_0_4_8					,						__UpperCamelCase=1_6					,						__UpperCamelCase=0.0					,						__UpperCamelCase=0.0					,						__UpperCamelCase=True					,						__UpperCamelCase=True					,						__UpperCamelCase="gelu"					,						__UpperCamelCase=5_1_2					,						__UpperCamelCase=0.1					,						__UpperCamelCase=0.0					,						__UpperCamelCase=0.0					,						__UpperCamelCase=0.0_2					,						__UpperCamelCase=1					,						__UpperCamelCase=False					,						__UpperCamelCase=0					,						__UpperCamelCase=1					,						__UpperCamelCase=2					,						__UpperCamelCase=2					,						**__UpperCamelCase					,						)->	Any:
           __lowerCAmelCase								=					vocab_size
           __lowerCAmelCase								=					max_position_embeddings
           __lowerCAmelCase								=					d_model
           __lowerCAmelCase								=					encoder_ffn_dim
           __lowerCAmelCase								=					encoder_layers
           __lowerCAmelCase								=					encoder_attention_heads
           __lowerCAmelCase								=					decoder_ffn_dim
           __lowerCAmelCase								=					decoder_layers
           __lowerCAmelCase								=					decoder_attention_heads
           __lowerCAmelCase								=					dropout
           __lowerCAmelCase								=					attention_dropout
           __lowerCAmelCase								=					activation_dropout
           __lowerCAmelCase								=					activation_function
           __lowerCAmelCase								=					init_std
           __lowerCAmelCase								=					encoder_layerdrop
           __lowerCAmelCase								=					decoder_layerdrop
           __lowerCAmelCase								=					use_cache
           __lowerCAmelCase								=					encoder_layers
           __lowerCAmelCase								=					scale_embedding  # scale factor will be sqrt(d_model) if True

           super().__init__(
               pad_token_id=__UpperCamelCase					,						bos_token_id=__UpperCamelCase					,						eos_token_id=__UpperCamelCase					,						is_encoder_decoder=__UpperCamelCase					,						decoder_start_token_id=__UpperCamelCase					,						forced_eos_token_id=__UpperCamelCase					,						**__UpperCamelCase					,						)



class 							_UpperCamelCase			(a_			):


      @property
      def  __UpperCAmelCase		(	self	)->	Mapping[str, Mapping[int, str]]:
           if self.task in ["default", "seq2seq-lm"]:
                __lowerCAmelCase								=					OrderedDict(
                    [
                        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
                    ]	)

                if self.use_past:
                     __lowerCAmelCase								=					{0: "batch"}
                     __lowerCAmelCase								=					{0: "batch", 1: "past_decoder_sequence + sequence"}
                else:
                     __lowerCAmelCase								=					{0: "batch", 1: "decoder_sequence"}
                     __lowerCAmelCase								=					{0: "batch", 1: "decoder_sequence"}

                if self.use_past:
                     self.fill_with_past_key_values_(__UpperCamelCase					,						direction="inputs"	)
           elif self.task == "causal-lm":
                # TODO: figure this case out.
                __lowerCAmelCase								=					OrderedDict(
                    [
                        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
                    ]	)
                if self.use_past:
                     __lowerCAmelCase			, __lowerCAmelCase								=					self.num_layers
                     for i in range(__UpperCamelCase	):
                          __lowerCAmelCase								=					{0: "batch", 2: "past_sequence + sequence"}
                          __lowerCAmelCase								=					{0: "batch", 2: "past_sequence + sequence"}
           else:
                __lowerCAmelCase								=					OrderedDict(
                    [
                        ("input_ids", {0: "batch", 1: "encoder_sequence"}),
                        ("attention_mask", {0: "batch", 1: "encoder_sequence"}),
                        ("decoder_input_ids", {0: "batch", 1: "decoder_sequence"}),
                        ("decoder_attention_mask", {0: "batch", 1: "decoder_sequence"}),
                    ]	)

           return common_inputs


      @property
      def  __UpperCAmelCase		(	self	)->	Mapping[str, Mapping[int, str]]:
           if self.task in ["default", "seq2seq-lm"]:
                __lowerCAmelCase								=					super().outputs
           else:
                __lowerCAmelCase								=					super(__UpperCamelCase					,						self	).outputs
                if self.use_past:
                     __lowerCAmelCase			, __lowerCAmelCase								=					self.num_layers
                     for i in range(__UpperCamelCase	):
                          __lowerCAmelCase								=					{0: "batch", 2: "past_sequence + sequence"}
                          __lowerCAmelCase								=					{0: "batch", 2: "past_sequence + sequence"}
           return common_outputs


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           __lowerCAmelCase								=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               __UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)

           # Generate decoder inputs
           __lowerCAmelCase								=					seq_length if not self.use_past else 1
           __lowerCAmelCase								=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               __UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)
           __lowerCAmelCase								=					{F"""decoder_{name}""": tensor for name, tensor in decoder_inputs.items()}
           __lowerCAmelCase								=					dict(**__UpperCamelCase					,						**__UpperCamelCase	)

           if self.use_past:
                if not is_torch_available():
                     raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."	)
                else:
                     import torch
                __lowerCAmelCase			, __lowerCAmelCase								=					common_inputs["input_ids"].shape
                __lowerCAmelCase								=					common_inputs["decoder_input_ids"].shape[1]
                __lowerCAmelCase			, __lowerCAmelCase								=					self.num_attention_heads
                __lowerCAmelCase								=					(
                    batch,
                    num_encoder_attention_heads,
                    encoder_seq_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )
                __lowerCAmelCase								=					decoder_seq_length + 3
                __lowerCAmelCase								=					(
                    batch,
                    num_decoder_attention_heads,
                    decoder_past_length,
                    self._config.hidden_size // num_decoder_attention_heads,
                )

                __lowerCAmelCase								=					torch.cat(
                    [common_inputs["decoder_attention_mask"], torch.ones(__UpperCamelCase					,						__UpperCamelCase	)]					,						dim=1	)

                __lowerCAmelCase								=					[]
                # If the number of encoder and decoder layers are present in the model configuration, both are considered
                __lowerCAmelCase			, __lowerCAmelCase								=					self.num_layers
                __lowerCAmelCase								=					min(__UpperCamelCase					,						__UpperCamelCase	)
                __lowerCAmelCase								=					max(__UpperCamelCase					,						__UpperCamelCase	) - min_num_layers
                __lowerCAmelCase								=					"encoder" if num_encoder_layers > num_decoder_layers else "decoder"

                for _ in range(__UpperCamelCase	):
                     common_inputs["past_key_values"].append(
                         (
                             torch.zeros(__UpperCamelCase	),
                             torch.zeros(__UpperCamelCase	),
                             torch.zeros(__UpperCamelCase	),
                             torch.zeros(__UpperCamelCase	),
                         )	)
                # TODO: test this.
                __lowerCAmelCase								=					encoder_shape if remaining_side_name == "encoder" else decoder_shape
                for _ in range(__UpperCamelCase					,						__UpperCamelCase	):
                     common_inputs["past_key_values"].append((torch.zeros(__UpperCamelCase	), torch.zeros(__UpperCamelCase	))	)
           return common_inputs


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           __lowerCAmelCase								=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
               __UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)

           if self.use_past:
                if not is_torch_available():
                     raise ValueError("Cannot generate dummy past_keys inputs without PyTorch installed."	)
                else:
                     import torch
                __lowerCAmelCase			, __lowerCAmelCase								=					common_inputs["input_ids"].shape
                # Not using the same length for past_key_values
                __lowerCAmelCase								=					seqlen + 2
                __lowerCAmelCase			, __lowerCAmelCase								=					self.num_layers
                __lowerCAmelCase			, __lowerCAmelCase								=					self.num_attention_heads
                __lowerCAmelCase								=					(
                    batch,
                    num_encoder_attention_heads,
                    past_key_values_length,
                    self._config.hidden_size // num_encoder_attention_heads,
                )

                __lowerCAmelCase								=					common_inputs["attention_mask"].dtype
                __lowerCAmelCase								=					torch.cat(
                    [common_inputs["attention_mask"], torch.ones(__UpperCamelCase					,						__UpperCamelCase					,						dtype=__UpperCamelCase	)]					,						dim=1	)
                __lowerCAmelCase								=					[
                    (torch.zeros(__UpperCamelCase	), torch.zeros(__UpperCamelCase	)) for _ in range(__UpperCamelCase	)
                ]
           return common_inputs


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           # Copied from OnnxConfig.generate_dummy_inputs
           # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
           # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
           __lowerCAmelCase								=					compute_effective_axis_dimension(
               __UpperCamelCase					,						fixed_dimension=OnnxConfig.default_fixed_batch					,						num_token_to_add=0	)

           # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
           __lowerCAmelCase								=					tokenizer.num_special_tokens_to_add(__UpperCamelCase	)
           __lowerCAmelCase								=					compute_effective_axis_dimension(
               __UpperCamelCase					,						fixed_dimension=OnnxConfig.default_fixed_sequence					,						num_token_to_add=__UpperCamelCase	)

           # Generate dummy inputs according to compute batch and sequence
           __lowerCAmelCase								=					[" ".join([tokenizer.unk_token]	) * seq_length] * batch_size
           __lowerCAmelCase								=					dict(tokenizer(__UpperCamelCase					,						return_tensors=__UpperCamelCase	)	)
           return common_inputs


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase = -1					,						__UpperCamelCase = -1					,						__UpperCamelCase = False					,						__UpperCamelCase = None					,						)->	Mapping[str, Any]:
           if self.task in ["default", "seq2seq-lm"]:
                __lowerCAmelCase								=					self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                    __UpperCamelCase					,						batch_size=__UpperCamelCase					,						seq_length=__UpperCamelCase					,						is_pair=__UpperCamelCase					,						framework=__UpperCamelCase	)

           elif self.task == "causal-lm":
                __lowerCAmelCase								=					self._generate_dummy_inputs_for_causal_lm(
                    __UpperCamelCase					,						batch_size=__UpperCamelCase					,						seq_length=__UpperCamelCase					,						is_pair=__UpperCamelCase					,						framework=__UpperCamelCase	)
           else:
                __lowerCAmelCase								=					self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                    __UpperCamelCase					,						batch_size=__UpperCamelCase					,						seq_length=__UpperCamelCase					,						is_pair=__UpperCamelCase					,						framework=__UpperCamelCase	)

           return common_inputs


      def  __UpperCAmelCase		(	self					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)->	List[Any]:
           if self.task in ["default", "seq2seq-lm"]:
                __lowerCAmelCase								=					super()._flatten_past_key_values_(__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)
           else:
                __lowerCAmelCase								=					super(__UpperCamelCase					,						self	)._flatten_past_key_values_(
                    __UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase					,						__UpperCamelCase	)






