





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


A_												=     logging.getLogger(__name__)

@dataclass(frozen=lowercase_   )
class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					A__				 =    42
					A__				 =    42
					A__				 =    None
					A__				 =    None
					A__				 =    None

@dataclass(frozen=lowercase_   )
class SCREAMING_SNAKE_CASE_ :





					"""simple docstring"""


					A__				 =    42
					A__				 =    None
					A__				 =    None
					A__				 =    None
					A__				 =    None


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

						class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





											"""simple docstring"""


											A__				 =    42





											def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase = None	,  _lowerCAmelCase=False	,  _lowerCAmelCase = False	,  ):
																lowerCamelCase__       =       hans_processors[task]()

																lowerCamelCase__       =       os.path.join(
																    _lowerCAmelCase	,  "cached_{}_{}_{}_{}".format(
																        "dev" if evaluate else "train"	,  tokenizer.__class__.__name__	,  str(_lowerCAmelCase	)	,  _lowerCAmelCase	,  )	,  )
																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(_lowerCAmelCase	):
																					if os.path.exists(_lowerCAmelCase	) and not overwrite_cache:
																										logger.info(F"Loading features from cached file {cached_features_file}"	)
																										lowerCamelCase__       =       torch.load(_lowerCAmelCase	)
																					else:
																										logger.info(F"Creating features from dataset file at {data_dir}"	)

																										lowerCamelCase__       =       (
																										    processor.get_dev_examples(_lowerCAmelCase	) if evaluate else processor.get_train_examples(_lowerCAmelCase	)
																										)

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





											def __len__( self	):
																return len(self.features	)





											def __getitem__( self	,  _lowerCAmelCase	):
																return self.features[i]






											def 					__magic_name__ ( self	):
																return self.label_list


if is_tf_available():
						import tensorflow as tf

						class SCREAMING_SNAKE_CASE_ :





											"""simple docstring"""


											A__				 =    42





											def __init__( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase = 128	,  _lowerCAmelCase=False	,  _lowerCAmelCase = False	,  ):
																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(_lowerCAmelCase	) if evaluate else processor.get_train_examples(_lowerCAmelCase	)
																lowerCamelCase__       =       hans_convert_examples_to_features(_lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	)

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

																										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(
																    _lowerCAmelCase	,  (
																        {
																            "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 					__magic_name__ ( self	):
																return self.dataset





											def __len__( self	):
																return len(self.features	)





											def __getitem__( self	,  _lowerCAmelCase	):
																return self.features[i]






											def 					__magic_name__ ( self	):
																return self.label_list

class SCREAMING_SNAKE_CASE_ (					lowercase_   ):





					"""simple docstring"""


					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										return self._create_examples(self._read_tsv(os.path.join(_lowerCAmelCase	,  "heuristics_train_set.txt"	)	)	,  "train"	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	):
										return self._create_examples(self._read_tsv(os.path.join(_lowerCAmelCase	,  "heuristics_evaluation_set.txt"	)	)	,  "dev"	)





					def 					__magic_name__ ( self	):
										return ["contradiction", "entailment", "neutral"]






					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       []
										for i, line in enumerate(_lowerCAmelCase	):
															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=_lowerCAmelCase	,  text_a=_lowerCAmelCase	,  text_b=_lowerCAmelCase	,  label=_lowerCAmelCase	,  pairID=_lowerCAmelCase	)	)
										return examples





def    __UpperCamelCase      (    a,     a,     a,     a,     ) ->List[str]:
					lowerCamelCase__       =       {label: i for i, label in enumerate(a)}

					lowerCamelCase__       =       []
					for ex_index, example in tqdm.tqdm(enumerate(a),     desc="convert examples to features"):
										if ex_index % 10000 == 0:
															logger.info("Writing example %d" % (ex_index))

										lowerCamelCase__       =       tokenizer(
										    example.text_a,     example.text_b,     add_special_tokens=a,     max_length=a,     padding="max_length",     truncation=a,     return_overflowing_tokens=a,     )

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

										lowerCamelCase__       =       int(example.pairID)

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

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

					return features


A_												=     {
    "hans": 3,
}







A_												=     {
    "hans": HansProcessor,
}








def    __UpperCamelCase      (    a,     a) ->Optional[int]:
					lowerCamelCase__       =       [1]
					for i in range(2,     a):
										factorials.append(factorials[-1] * i)
					assert 0 <= k < factorials[-1] * n, "k out of bounds"

					lowerCamelCase__       =       []
					lowerCamelCase__       =       list(range(a))

					# Find permutation
					while factorials:
										lowerCamelCase__       =       factorials.pop()
										lowerCamelCase__    ,		lowerCamelCase__       =       divmod(a,     a)
										permutation.append(elements[number])
										elements.remove(elements[number])
					permutation.append(elements[0])

					return permutation


if __name__ == "__main__":
						import doctest

						doctest.testmod()

