

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							:  Any            =						logging.getLogger(__name__)



@dataclass(frozen=UpperCAmelCase_     )
class   __lowercase    :




			"""simple docstring"""







			_UpperCAmelCase       :							str
			_UpperCAmelCase       :							str
			_UpperCAmelCase       :							Optional[str]      							=					None
			_UpperCAmelCase       :							Optional[str]      							=					None
			_UpperCAmelCase       :							Optional[str]      							=					None







@dataclass(frozen=UpperCAmelCase_     )
class   __lowercase    :




			"""simple docstring"""







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


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



						class   __lowercase    (			UpperCAmelCase_     ):




									"""simple docstring"""







									_UpperCAmelCase       :							List[InputFeatures]



									def __init__(			self  :					int       ,						lowerCAmelCase__  :					str       ,						lowerCAmelCase__  :					PreTrainedTokenizer       ,						lowerCAmelCase__  :					str       ,						lowerCAmelCase__  :					Optional[int] = None       ,						lowerCAmelCase__  :					int=False       ,						lowerCAmelCase__  :					bool = False       ,						):
															SCREAMING_SNAKE_CASE_:			List[str]  		=	hans_processors[task]()

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

															# Make sure only the first process in distributed training processes the dataset,
															# and the others will use the cache.
															SCREAMING_SNAKE_CASE_:			Tuple  		=	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}")
																											SCREAMING_SNAKE_CASE_:			Union[str, Any]  		=	torch.load(lowerCAmelCase__)
																					else:
																											logger.info(F"Creating features from dataset file at {data_dir}")

																											SCREAMING_SNAKE_CASE_:			Tuple  		=	(
																											    processor.get_dev_examples(lowerCAmelCase__) if evaluate else processor.get_train_examples(lowerCAmelCase__)
																											)

																											logger.info("Training examples: %s"       ,						len(lowerCAmelCase__))
																											SCREAMING_SNAKE_CASE_:			List[Any]  		=	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  :					Any):
															return len(self.features)



									def __getitem__(			self  :					str       ,						lowerCAmelCase__  :					Union[str, Any]):
															return self.features[i]




									def 	_SCREAMING_SNAKE_CASE      (			self  :					Dict):
															return self.label_list


if is_tf_available():
						import tensorflow as tf



						class   __lowercase    :




									"""simple docstring"""







									_UpperCAmelCase       :							List[InputFeatures]



									def __init__(			self  :					List[str]       ,						lowerCAmelCase__  :					str       ,						lowerCAmelCase__  :					PreTrainedTokenizer       ,						lowerCAmelCase__  :					str       ,						lowerCAmelCase__  :					Optional[int] = 128       ,						lowerCAmelCase__  :					Optional[Any]=False       ,						lowerCAmelCase__  :					bool = False       ,						):
															SCREAMING_SNAKE_CASE_:			List[str]  		=	hans_processors[task]()
															SCREAMING_SNAKE_CASE_:			int  		=	processor.get_labels()
															if tokenizer.__class__ in (
															    RobertaTokenizer,
															    RobertaTokenizerFast,
															    XLMRobertaTokenizer,
															    BartTokenizer,
															    BartTokenizerFast,
															):
																					# HACK(label indices are swapped in RoBERTa pretrained model)
																					SCREAMING_SNAKE_CASE_		,   SCREAMING_SNAKE_CASE_:			Any  		=	label_list[2], label_list[1]
															SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	label_list

															SCREAMING_SNAKE_CASE_:			List[Any]  		=	processor.get_dev_examples(lowerCAmelCase__) if evaluate else processor.get_train_examples(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	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,
																											)

															SCREAMING_SNAKE_CASE_:			List[Any]  		=	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 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
															return self.dataset



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



									def __getitem__(			self  :					List[str]       ,						lowerCAmelCase__  :					Optional[Any]):
															return self.features[i]




									def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
															return self.label_list



class   __lowercase    (			UpperCAmelCase_     ):




			"""simple docstring"""







			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]       ,						lowerCAmelCase__  :					Union[str, Any]):
									return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase__       ,						"heuristics_train_set.txt"))       ,						"train")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Union[str, Any]       ,						lowerCAmelCase__  :					Optional[int]):
									return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase__       ,						"heuristics_evaluation_set.txt"))       ,						"dev")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Optional[Any]):
									return ["contradiction", "entailment", "neutral"]




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







def       A_				(   _UpperCAmelCase    ,     _UpperCAmelCase    ,     _UpperCAmelCase    ,     _UpperCAmelCase    ,     ):
						SCREAMING_SNAKE_CASE_:			Optional[int]  		=	{label: i for i, label in enumerate(_UpperCAmelCase   )}

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

												SCREAMING_SNAKE_CASE_:			int  		=	tokenizer(
												    example.text_a    ,     example.text_b    ,     add_special_tokens=_UpperCAmelCase    ,     max_length=_UpperCAmelCase    ,     padding="max_length"    ,     truncation=_UpperCAmelCase    ,     return_overflowing_tokens=_UpperCAmelCase    ,     )

												SCREAMING_SNAKE_CASE_:			str  		=	label_map[example.label] if example.label in label_map else 0

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

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

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

						return features


lowerCAmelCase							:  List[str]            =						{
    """hans""": 3,
}



lowerCAmelCase							:  List[str]            =						{
    """hans""": HansProcessor,
}









import importlib
import json
import os
import sys
import tempfile
import unittest
from pathlib import Path

import transformers
import transformers.models.auto
from transformers.models.auto.configuration_auto import CONFIG_MAPPING, AutoConfig
from transformers.models.bert.configuration_bert import BertConfig
from transformers.models.roberta.configuration_roberta import RobertaConfig
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, get_tests_dir


sys.path.append(str(Path(__file__).parent.parent.parent.parent / """utils"""))

from test_module.custom_configuration import CustomConfig  # noqa E402


lowerCAmelCase							:  Union[str, Any]            =						get_tests_dir("""fixtures/dummy-config.json""")



class   __lowercase    (			unittest.TestCase     ):




			"""simple docstring"""







			def 	_SCREAMING_SNAKE_CASE      (			self  :					Tuple):
									SCREAMING_SNAKE_CASE_:			Dict  		=	0



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Any):
									self.assertIsNotNone(transformers.models.auto.__spec__)
									self.assertIsNotNone(importlib.util.find_spec("transformers.models.auto"))



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	AutoConfig.from_pretrained("bert-base-uncased")
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					str):
									SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					int):
									SCREAMING_SNAKE_CASE_:			Tuple  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Union[str, Any]):
									SCREAMING_SNAKE_CASE_:			List[Any]  		=	AutoConfig.for_model("roberta")
									self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									with tempfile.TemporaryDirectory() as tmp_dir:
															# This model name contains bert and roberta, but roberta ends up being picked.
															SCREAMING_SNAKE_CASE_:			int  		=	os.path.join(lowerCAmelCase__       ,						"fake-roberta")
															os.makedirs(lowerCAmelCase__       ,						exist_ok=lowerCAmelCase__)
															with open(os.path.join(lowerCAmelCase__       ,						"config.json")       ,						"w") as f:
																					f.write(json.dumps({}))
															SCREAMING_SNAKE_CASE_:			Any  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
															self.assertEqual(type(lowerCAmelCase__)       ,						lowerCAmelCase__)



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Optional[int]):
									try:
															AutoConfig.register("custom"       ,						lowerCAmelCase__)
															# Wrong model type will raise an error
															with self.assertRaises(lowerCAmelCase__):
																					AutoConfig.register("model"       ,						lowerCAmelCase__)
															# Trying to register something existing in the Transformers library will raise an error
															with self.assertRaises(lowerCAmelCase__):
																					AutoConfig.register("bert"       ,						lowerCAmelCase__)

															# Now that the config is registered, it can be used as any other config with the auto-API
															SCREAMING_SNAKE_CASE_:			List[Any]  		=	CustomConfig()
															with tempfile.TemporaryDirectory() as tmp_dir:
																					config.save_pretrained(lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_:			Tuple  		=	AutoConfig.from_pretrained(lowerCAmelCase__)
																					self.assertIsInstance(lowerCAmelCase__       ,						lowerCAmelCase__)

									finally:
															if "custom" in CONFIG_MAPPING._extra_content:
																					del CONFIG_MAPPING._extra_content["custom"]



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						"bert-base is not a local folder and is not a valid model identifier"):
															SCREAMING_SNAKE_CASE_:			List[str]  		=	AutoConfig.from_pretrained("bert-base")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						R"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"):
															SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained(lowerCAmelCase__       ,						revision="aaaaaa")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					Optional[int]):
									with self.assertRaisesRegex(
									    lowerCAmelCase__       ,						"hf-internal-testing/no-config-test-repo does not appear to have a file named config.json."       ,						):
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/no-config-test-repo")



			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[str]):
									# If remote code is not set, we will time out when asking whether to load the model.
									with self.assertRaises(lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model")
									# If remote code is disabled, we can't load this config.
									with self.assertRaises(lowerCAmelCase__):
															SCREAMING_SNAKE_CASE_:			Optional[Any]  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:			str  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
									self.assertEqual(config.__class__.__name__       ,						"NewModelConfig")

									# Test config can be reloaded.
									with tempfile.TemporaryDirectory() as tmp_dir:
															config.save_pretrained(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_:			Any  		=	AutoConfig.from_pretrained(lowerCAmelCase__       ,						trust_remote_code=lowerCAmelCase__)
									self.assertEqual(reloaded_config.__class__.__name__       ,						"NewModelConfig")




			def 	_SCREAMING_SNAKE_CASE      (			self  :					List[Any]):



									class   __lowercase    (			UpperCAmelCase_     ):




												"""simple docstring"""







												_UpperCAmelCase       :							int      							=					'''new-model'''

									try:
															AutoConfig.register("new-model"       ,						lowerCAmelCase__)
															# If remote code is not set, the default is to use local
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model")
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfigLocal")

															# If remote code is disabled, we load the local one.
															SCREAMING_SNAKE_CASE_:			Optional[int]  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfigLocal")

															# If remote is enabled, we load from the Hub
															SCREAMING_SNAKE_CASE_:			int  		=	AutoConfig.from_pretrained("hf-internal-testing/test_dynamic_model"       ,						trust_remote_code=lowerCAmelCase__)
															self.assertEqual(config.__class__.__name__       ,						"NewModelConfig")

									finally:
															if "new-model" in CONFIG_MAPPING._extra_content:
																					del CONFIG_MAPPING._extra_content["new-model"]






