


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







@dataclass(frozen=UpperCAmelCase__)
class 	lowerCAmelCase__			:




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE         =					42
     __SCREAMING_SNAKE_CASE         =					42
     __SCREAMING_SNAKE_CASE         =					None
     __SCREAMING_SNAKE_CASE         =					None
     __SCREAMING_SNAKE_CASE         =					None







@dataclass(frozen=UpperCAmelCase__)
class 	lowerCAmelCase__			:




     '''simple docstring'''
     __SCREAMING_SNAKE_CASE         =					42
     __SCREAMING_SNAKE_CASE         =					None
     __SCREAMING_SNAKE_CASE         =					None
     __SCREAMING_SNAKE_CASE         =					None
     __SCREAMING_SNAKE_CASE         =					None


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







    class 	lowerCAmelCase__			(							UpperCAmelCase__):




         '''simple docstring'''
         __SCREAMING_SNAKE_CASE         =					42


         def __init__(		self					,		__lowerCamelCase					,		__lowerCamelCase					,		__lowerCamelCase					,		__lowerCamelCase = None					,		__lowerCamelCase=False					,		__lowerCamelCase = False					,		)     ->							str:
                _A :			Tuple							       =					hans_processors[task]()

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

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

                              _A :			Optional[Any]							       =					(
                                  processor.get_dev_examples(__lowercase) if evaluate else processor.get_train_examples(__lowercase)
                              )

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


         def __getitem__(		self					,		__lowerCamelCase)     ->							Optional[int]:
                return self.features[i]


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


if is_tf_available():
    import tensorflow as tf







    class 	lowerCAmelCase__			:




         '''simple docstring'''
         __SCREAMING_SNAKE_CASE         =					42


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

                _A :			Tuple							       =					processor.get_dev_examples(__lowercase) if evaluate else processor.get_train_examples(__lowercase)
                _A :			int							       =					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 % 1_0_0_0_0 == 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,
                              )

                _A :			str							       =					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 					_lowerCamelCase					(		self)     ->							Optional[Any]:
                return self.dataset


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


         def __getitem__(		self					,		__lowerCamelCase)     ->							Dict:
                return self.features[i]


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







class 	lowerCAmelCase__			(							UpperCAmelCase__):




     '''simple docstring'''


     def 					_lowerCamelCase					(		self					,		__lowerCamelCase)     ->							List[Any]:
            return self._create_examples(self._read_tsv(os.path.join(__lowercase					,		"heuristics_train_set.txt"))					,		"train")


     def 					_lowerCamelCase					(		self					,		__lowerCamelCase)     ->							int:
            return self._create_examples(self._read_tsv(os.path.join(__lowercase					,		"heuristics_evaluation_set.txt"))					,		"dev")


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


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




def 				_UpperCAmelCase							(UpperCamelCase__	:    Union[str, Any]							,				UpperCamelCase__	:    List[Any]							,				UpperCamelCase__	:    str							,				UpperCamelCase__	:    Dict							,				):
       _A :			Optional[int]							       =					{label: i for i, label in enumerate(_A		)}

       _A :			Dict							       =					[]
       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)		)

              _A :			List[str]							       =					tokenizer(
                  example.text_a							,				example.text_b							,				add_special_tokens=_A							,				max_length=_A							,				padding="max_length"							,				truncation=_A							,				return_overflowing_tokens=_A							,				)

              _A :			List[Any]							       =					label_map[example.label] if example.label in label_map else 0

              _A :			List[str]							       =					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


lowerCAmelCase__ 						=					{
    "hans": 3,
}






lowerCAmelCase__ 						=					{
    "hans": HansProcessor,
}






import unittest

from transformers import DebertaConfig, is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch

		from transformers import (
		    DebertaForMaskedLM,
		    DebertaForQuestionAnswering,
		    DebertaForSequenceClassification,
		    DebertaForTokenClassification,
		    DebertaModel,
		)
		from transformers.models.deberta.modeling_deberta import DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST
class 						UpperCAmelCase  (							UpperCAmelCase__     ):







						'''simple docstring'''





						def __init__(		self				:	Dict			,      __lowercase				:	Any			,      __lowercase				:	str=13			,      __lowercase				:	Optional[Any]=7			,      __lowercase				:	Tuple=True			,      __lowercase				:	Optional[int]=True			,      __lowercase				:	List[str]=True			,      __lowercase				:	Dict=True			,      __lowercase				:	Any=99			,      __lowercase				:	Any=32			,      __lowercase				:	List[Any]=5			,      __lowercase				:	Any=4			,      __lowercase				:	List[str]=37			,      __lowercase				:	Union[str, Any]="gelu"			,      __lowercase				:	Union[str, Any]=0.1			,      __lowercase				:	str=0.1			,      __lowercase				:	List[str]=5_12			,      __lowercase				:	Tuple=16			,      __lowercase				:	List[str]=2			,      __lowercase				:	Tuple=0.02			,      __lowercase				:	Optional[Any]=False			,      __lowercase				:	Optional[int]=True			,      __lowercase				:	Union[str, Any]="None"			,      __lowercase				:	Union[str, Any]=3			,      __lowercase				:	int=4			,      __lowercase				:	Tuple=None			,      ):






							"""simple docstring"""






							snake_case_						       =	parent
							snake_case_						       =	batch_size
							snake_case_						       =	seq_length
							snake_case_						       =	is_training
							snake_case_						       =	use_input_mask
							snake_case_						       =	use_token_type_ids
							snake_case_						       =	use_labels
							snake_case_						       =	vocab_size
							snake_case_						       =	hidden_size
							snake_case_						       =	num_hidden_layers
							snake_case_						       =	num_attention_heads
							snake_case_						       =	intermediate_size
							snake_case_						       =	hidden_act
							snake_case_						       =	hidden_dropout_prob
							snake_case_						       =	attention_probs_dropout_prob
							snake_case_						       =	max_position_embeddings
							snake_case_						       =	type_vocab_size
							snake_case_						       =	type_sequence_label_size
							snake_case_						       =	initializer_range
							snake_case_						       =	num_labels
							snake_case_						       =	num_choices
							snake_case_						       =	relative_attention
							snake_case_						       =	position_biased_input
							snake_case_						       =	pos_att_type
							snake_case_						       =	scope






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.vocab_size		)

							snake_case_						       =	None
							if self.use_input_mask:
								snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      vocab_size=2		)

							snake_case_						       =	None
							if self.use_token_type_ids:
								snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.type_vocab_size		)

							snake_case_						       =	None
							snake_case_						       =	None
							snake_case_						       =	None
							if self.use_labels:
								snake_case_						       =	ids_tensor([self.batch_size]			,      self.type_sequence_label_size		)
								snake_case_						       =	ids_tensor([self.batch_size, self.seq_length]			,      self.num_labels		)
								snake_case_						       =	ids_tensor([self.batch_size]			,      self.num_choices		)

							snake_case_						       =	self.get_config()

							return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							return DebertaConfig(
							    vocab_size=self.vocab_size			,      hidden_size=self.hidden_size			,      num_hidden_layers=self.num_hidden_layers			,      num_attention_heads=self.num_attention_heads			,      intermediate_size=self.intermediate_size			,      hidden_act=self.hidden_act			,      hidden_dropout_prob=self.hidden_dropout_prob			,      attention_probs_dropout_prob=self.attention_probs_dropout_prob			,      max_position_embeddings=self.max_position_embeddings			,      type_vocab_size=self.type_vocab_size			,      initializer_range=self.initializer_range			,      relative_attention=self.relative_attention			,      position_biased_input=self.position_biased_input			,      pos_att_type=self.pos_att_type			,      )






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	self.get_config()
							snake_case_						       =	3_00
							return config






						def 			snake_case__		(		self				:	List[Any]			,      __lowercase				:	List[Any]		):






							"""simple docstring"""






							self.parent.assertListEqual(list(result.loss.size()		)			,      []		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	Tuple			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	Tuple			,      __lowercase				:	Dict			,      __lowercase				:	str			,      __lowercase				:	Optional[int]			,      __lowercase				:	int		):






							"""simple docstring"""






							snake_case_						       =	DebertaModel(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase			,      token_type_ids=__lowercase		)[0]
							snake_case_						       =	model(__lowercase			,      token_type_ids=__lowercase		)[0]
							snake_case_						       =	model(__lowercase		)[0]

							self.parent.assertListEqual(list(sequence_output.size()		)			,      [self.batch_size, self.seq_length, self.hidden_size]		)






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Any			,      __lowercase				:	Dict			,      __lowercase				:	Any			,      __lowercase				:	Any			,      __lowercase				:	Tuple			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	DebertaForMaskedLM(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase			,      token_type_ids=__lowercase			,      labels=__lowercase		)

							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.vocab_size)		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	Tuple			,      __lowercase				:	List[str]			,      __lowercase				:	Dict			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							snake_case_						       =	self.num_labels
							snake_case_						       =	DebertaForSequenceClassification(__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase			,      token_type_ids=__lowercase			,      labels=__lowercase		)
							self.parent.assertListEqual(list(result.logits.size()		)			,      [self.batch_size, self.num_labels]		)
							self.check_loss_output(__lowercase		)






						def 			snake_case__		(		self				:	List[Any]			,      __lowercase				:	List[str]			,      __lowercase				:	Any			,      __lowercase				:	List[Any]			,      __lowercase				:	str			,      __lowercase				:	List[str]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[int]		):






							"""simple docstring"""






							snake_case_						       =	self.num_labels
							snake_case_						       =	DebertaForTokenClassification(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase			,      token_type_ids=__lowercase			,      labels=__lowercase		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.num_labels)		)






						def 			snake_case__		(		self				:	List[str]			,      __lowercase				:	str			,      __lowercase				:	List[str]			,      __lowercase				:	Optional[int]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Optional[Any]			,      __lowercase				:	Union[str, Any]		):






							"""simple docstring"""






							snake_case_						       =	DebertaForQuestionAnswering(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(
							    __lowercase			,      attention_mask=__lowercase			,      token_type_ids=__lowercase			,      start_positions=__lowercase			,      end_positions=__lowercase			,      )
							self.parent.assertEqual(result.start_logits.shape			,      (self.batch_size, self.seq_length)		)
							self.parent.assertEqual(result.end_logits.shape			,      (self.batch_size, self.seq_length)		)






						def 			snake_case__		(		self				:	Tuple		):






							"""simple docstring"""






							snake_case_						       =	self.prepare_config_and_inputs()
							(
							    (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , 
							)						       =	config_and_inputs
							snake_case_						       =	{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict







@require_torch
class 						UpperCAmelCase  (							UpperCAmelCase__			,  UpperCAmelCase__			,  unittest.TestCase     ):







						'''simple docstring'''





						lowerCAmelCase_							=  (
						    (
						        DebertaModel,
						        DebertaForMaskedLM,
						        DebertaForSequenceClassification,
						        DebertaForTokenClassification,
						        DebertaForQuestionAnswering,
						    )
						    if is_torch_available()
						    else ()
						)
						lowerCAmelCase_							=  (
						    {
						        '''feature-extraction''': DebertaModel,
						        '''fill-mask''': DebertaForMaskedLM,
						        '''question-answering''': DebertaForQuestionAnswering,
						        '''text-classification''': DebertaForSequenceClassification,
						        '''token-classification''': DebertaForTokenClassification,
						        '''zero-shot''': DebertaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)

						lowerCAmelCase_							=  True
						lowerCAmelCase_							=  False
						lowerCAmelCase_							=  False
						lowerCAmelCase_							=  False
						lowerCAmelCase_							=  False






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	DebertaModelTester(self		)
							snake_case_						       =	ConfigTester(self			,      config_class=__lowercase			,      hidden_size=37		)






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							self.config_tester.run_common_tests()






						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_model(*__lowercase		)






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_sequence_classification(*__lowercase		)






						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_masked_lm(*__lowercase		)






						def 			snake_case__		(		self				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_question_answering(*__lowercase		)






						def 			snake_case__		(		self				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_deberta_for_token_classification(*__lowercase		)






						@slow
						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
								snake_case_						       =	DebertaModel.from_pretrained(__lowercase		)
								self.assertIsNotNone(__lowercase		)







@require_torch
@require_sentencepiece
@require_tokenizers
class 						UpperCAmelCase  (							unittest.TestCase     ):







						'''simple docstring'''





						@unittest.skip(reason="Model not available yet"		)
						def 			snake_case__		(		self				:	str		):






							"""simple docstring"""






							pass






						@slow
						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							snake_case_						       =	DebertaModel.from_pretrained("microsoft/deberta-base"		)

							snake_case_						       =	torch.tensor([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]		)
							snake_case_						       =	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
							with torch.no_grad():
								snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase		)[0]
							# compare the actual values for a slice.
							snake_case_						       =	torch.tensor(
							    [[[-0.5986, -0.8055, -0.8462], [1.4484, -0.9348, -0.8059], [0.3123, 0.0032, -1.4131]]]		)
							self.assertTrue(torch.allclose(output[:, 1:4, 1:4]			,      __lowercase			,      atol=1E-4		)			,      f"{output[:, 1:4, 1:4]}"		)
