
import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


UpperCamelCase_	:    List[str]  					=       logging.get_logger(__name__)




@dataclass
class      __lowercase	:
	_A   			=      42
	_A   			=      42
	_A   			=      None
	_A   			=      None






	def 			_a(self							:   List[str]						)   -> Optional[Any]:
							return json.dumps(dataclasses.asdict(self						)			,  indent=2						) + "\n"




@dataclass(frozen=UpperCAmelCase_					)
class      __lowercase	:
	_A   			=      42
	_A   			=      None
	_A   			=      None
	_A   			=      None






	def 			_a(self							:   str						)   -> Tuple:
							return json.dumps(dataclasses.asdict(self						)						) + "\n"




class      __lowercase	:






	def 			_a(self							:   List[str]			,  snake_case							:   List[str]						)   -> str:
							raise NotImplementedError()






	def 			_a(self							:   List[Any]			,  snake_case							:   Optional[Any]						)   -> str:
							raise NotImplementedError()






	def 			_a(self							:   Optional[Any]			,  snake_case							:   str						)   -> Dict:
							raise NotImplementedError()






	def 			_a(self							:   int			,  snake_case							:   List[Any]						)   -> Optional[int]:
							raise NotImplementedError()






	def 			_a(self							:   Union[str, Any]						)   -> int:
							raise NotImplementedError()






	def 			_a(self							:   Any			,  snake_case							:   Dict						)   -> int:
							if len(self.get_labels()						) > 1:
													_lowercase					:  str    			=      self.get_labels()[int(example.label						)]
							return example






	@classmethod
	def 			_a(cls							:   List[str]			,  snake_case							:   List[str]			,  snake_case							:   List[Any]=None						)   -> int:
							with open(snake_case			,  "r"			,  encoding="utf-8-sig"						) as f:
													return list(csv.reader(snake_case			,  delimiter="\t"			,  quotechar=snake_case						)						)




class      __lowercase	(	UpperCAmelCase_					):






	def __init__(self							:   str			,  snake_case							:   Any=None			,  snake_case							:   Optional[int]=None			,  snake_case							:   Union[str, Any]="classification"			,  snake_case							:   Union[str, Any]=False						)   -> Optional[Any]:
							_lowercase					:  Dict    			=      [] if labels is None else labels
							_lowercase					:  str    			=      [] if examples is None else examples
							_lowercase					:  Optional[int]    			=      mode
							_lowercase					:  List[str]    			=      verbose






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






	def __getitem__(self							:   Union[str, Any]			,  snake_case							:   Dict						)   -> str:
							if isinstance(snake_case			,  snake_case						):
													return SingleSentenceClassificationProcessor(labels=self.labels			,  examples=self.examples[idx]						)
							return self.examples[idx]






	@classmethod
	def 			_a(cls							:   Union[str, Any]			,  snake_case							:   Any			,  snake_case							:   List[str]=""			,  snake_case							:   List[Any]=0			,  snake_case							:   List[Any]=1			,  snake_case							:   List[str]=None			,  snake_case							:   Optional[Any]=False			,  **snake_case							:   Dict						)   -> Any:
							_lowercase					:  str    			=      cls(**snake_case						)
							processor.add_examples_from_csv(
							    snake_case			,  split_name=snake_case			,  column_label=snake_case			,  column_text=snake_case			,  column_id=snake_case			,  skip_first_row=snake_case			,  overwrite_labels=snake_case			,  overwrite_examples=snake_case			,  )
							return processor






	@classmethod
	def 			_a(cls							:   str			,  snake_case							:   str			,  snake_case							:   Any=None			,  **snake_case							:   Union[str, Any]						)   -> Union[str, Any]:
							_lowercase					:  int    			=      cls(**snake_case						)
							processor.add_examples(snake_case			,  labels=snake_case						)
							return processor






	def 			_a(self							:   int			,  snake_case							:   List[str]			,  snake_case							:   Optional[Any]=""			,  snake_case							:   Optional[int]=0			,  snake_case							:   int=1			,  snake_case							:   List[Any]=None			,  snake_case							:   List[str]=False			,  snake_case							:   Optional[int]=False			,  snake_case							:   Dict=False			,  )   -> int:
							_lowercase					:  Any    			=      self._read_tsv(snake_case						)
							if skip_first_row:
													_lowercase					:  Tuple    			=      lines[1:]
							_lowercase					:  List[Any]    			=      []
							_lowercase					:  List[str]    			=      []
							_lowercase					:  List[str]    			=      []
							for i, line in enumerate(snake_case						):
													texts.append(line[column_text]						)
													labels.append(line[column_label]						)
													if column_id is not None:
																			ids.append(line[column_id]						)
													else:
																			_lowercase					:  List[str]    			=      F"""{split_name}-{i}""" if split_name else str(snake_case						)
																			ids.append(snake_case						)

							return self.add_examples(
							    snake_case			,  snake_case			,  snake_case			,  overwrite_labels=snake_case			,  overwrite_examples=snake_case						)






	def 			_a(self							:   str			,  snake_case							:   Dict			,  snake_case							:   str=None			,  snake_case							:   Optional[int]=None			,  snake_case							:   str=False			,  snake_case							:   Optional[Any]=False						)   -> Any:
							if labels is not None and len(snake_case						) != len(snake_case						):
													raise ValueError(
													    F"""Text and labels have mismatched lengths {len(snake_case						)} and {len(snake_case						)}"""						)
							if ids is not None and len(snake_case						) != len(snake_case						):
													raise ValueError(F"""Text and ids have mismatched lengths {len(snake_case						)} and {len(snake_case						)}"""						)
							if ids is None:
													_lowercase					:  Union[str, Any]    			=      [None] * len(snake_case						)
							if labels is None:
													_lowercase					:  Union[str, Any]    			=      [None] * len(snake_case						)
							_lowercase					:  Dict    			=      []
							_lowercase					:  List[Any]    			=      set()
							for text_or_text_and_label, label, guid in zip(snake_case			,  snake_case			,  snake_case						):
													if isinstance(snake_case			,  (tuple, list)						) and label is None:
																			_lowercase			,						_lowercase					:  Optional[Any]    			=      text_or_text_and_label
													else:
																			_lowercase					:  str    			=      text_or_text_and_label
													added_labels.add(snake_case						)
													examples.append(InputExample(guid=snake_case			,  text_a=snake_case			,  text_b=snake_case			,  label=snake_case						)						)

							# Update examples
							if overwrite_examples:
													_lowercase					:  int    			=      examples
							else:
													self.examples.extend(snake_case						)

							# Update labels
							if overwrite_labels:
													_lowercase					:  List[Any]    			=      list(snake_case						)
							else:
													_lowercase					:  int    			=      list(set(self.labels						).union(snake_case						)						)

							return self.examples






	def 			_a(self							:   str			,  snake_case							:   str			,  snake_case							:   str=None			,  snake_case							:   Optional[Any]=False			,  snake_case							:   Union[str, Any]=0			,  snake_case							:   Union[str, Any]=True			,  snake_case							:   Any=None			,  )   -> Any:
							if max_length is None:
													_lowercase					:  List[Any]    			=      tokenizer.max_len

							_lowercase					:  Optional[Any]    			=      {label: i for i, label in enumerate(self.labels						)}

							_lowercase					:  Optional[int]    			=      []
							for ex_index, example in enumerate(self.examples						):
													if ex_index % 1_0000 == 0:
																			logger.info(F"""Tokenizing example {ex_index}"""						)

													_lowercase					:  int    			=      tokenizer.encode(
													    example.text_a			,  add_special_tokens=snake_case			,  max_length=min(snake_case			,  tokenizer.max_len						)			,  )
													all_input_ids.append(snake_case						)

							_lowercase					:  Optional[Any]    			=      max(len(snake_case						) for input_ids in all_input_ids						)

							_lowercase					:  Tuple    			=      []
							for ex_index, (input_ids, example) in enumerate(zip(snake_case			,  self.examples						)						):
													if ex_index % 1_0000 == 0:
																			logger.info(F"""Writing example {ex_index}/{len(self.examples						)}"""						)
													# The mask has 1 for real tokens and 0 for padding tokens. Only real
													# tokens are attended to.
													_lowercase					:  int    			=      [1 if mask_padding_with_zero else 0] * len(snake_case						)

													# Zero-pad up to the sequence length.
													_lowercase					:  List[Any]    			=      batch_length - len(snake_case						)
													if pad_on_left:
																			_lowercase					:  List[str]    			=      ([pad_token] * padding_length) + input_ids
																			_lowercase					:  Union[str, Any]    			=      ([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
													else:
																			_lowercase					:  List[str]    			=      input_ids + ([pad_token] * padding_length)
																			_lowercase					:  Tuple    			=      attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

													if len(snake_case						) != batch_length:
																			raise ValueError(F"""Error with input length {len(snake_case						)} vs {batch_length}"""						)
													if len(snake_case						) != batch_length:
																			raise ValueError(F"""Error with input length {len(snake_case						)} vs {batch_length}"""						)

													if self.mode == "classification":
																			_lowercase					:  Union[str, Any]    			=      label_map[example.label]
													elif self.mode == "regression":
																			_lowercase					:  List[Any]    			=      float(example.label						)
													else:
																			raise ValueError(self.mode						)

													if ex_index < 5 and self.verbose:
																			logger.info("*** Example ***"						)
																			logger.info(F"""guid: {example.guid}"""						)
																			logger.info(F"""input_ids: {' '.join([str(snake_case						) for x in input_ids]						)}"""						)
																			logger.info(F"""attention_mask: {' '.join([str(snake_case						) for x in attention_mask]						)}"""						)
																			logger.info(F"""label: {example.label} (id = {label})"""						)

													features.append(InputFeatures(input_ids=snake_case			,  attention_mask=snake_case			,  label=snake_case						)						)

							if return_tensors is None:
													return features
							elif return_tensors == "tf":
													if not is_tf_available():
																			raise RuntimeError("return_tensors set to 'tf' but TensorFlow 2.0 can't be imported"						)
													import tensorflow as tf

													def gen():
																			for ex in features:
																									yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

													_lowercase					:  List[Any]    			=      tf.data.Dataset.from_generator(
													    snake_case			,  ({"input_ids": tf.intaa, "attention_mask": tf.intaa}, tf.intaa)			,  ({"input_ids": tf.TensorShape([None]						), "attention_mask": tf.TensorShape([None]						)}, tf.TensorShape([]						))			,  )
													return dataset
							elif return_tensors == "pt":
													if not is_torch_available():
																			raise RuntimeError("return_tensors set to 'pt' but PyTorch can't be imported"						)
													import torch
													from torch.utils.data import TensorDataset

													_lowercase					:  int    			=      torch.tensor([f.input_ids for f in features]			,  dtype=torch.long						)
													_lowercase					:  Optional[Any]    			=      torch.tensor([f.attention_mask for f in features]			,  dtype=torch.long						)
													if self.mode == "classification":
																			_lowercase					:  Optional[int]    			=      torch.tensor([f.label for f in features]			,  dtype=torch.long						)
													elif self.mode == "regression":
																			_lowercase					:  Optional[int]    			=      torch.tensor([f.label for f in features]			,  dtype=torch.float						)

													_lowercase					:  List[str]    			=      TensorDataset(snake_case			,  snake_case			,  snake_case						)
													return dataset
							else:
													raise ValueError("return_tensors should be one of 'tf' or 'pt'"						)






from __future__ import annotations

import unittest

from transformers import DebertaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
							import tensorflow as tf

							from transformers import (
							    TFDebertaForMaskedLM,
							    TFDebertaForQuestionAnswering,
							    TFDebertaForSequenceClassification,
							    TFDebertaForTokenClassification,
							    TFDebertaModel,
							)




class      __lowercase	:






	def __init__(self							:   Tuple			,  snake_case							:   Union[str, Any]			,  snake_case							:   List[Any]=13			,  snake_case							:   Union[str, Any]=7			,  snake_case							:   Union[str, Any]=True			,  snake_case							:   int=True			,  snake_case							:   Dict=True			,  snake_case							:   Union[str, Any]=True			,  snake_case							:   str=99			,  snake_case							:   str=32			,  snake_case							:   List[Any]=2			,  snake_case							:   Optional[int]=4			,  snake_case							:   Union[str, Any]=37			,  snake_case							:   int="gelu"			,  snake_case							:   Any=0.1			,  snake_case							:   str=0.1			,  snake_case							:   Dict=512			,  snake_case							:   Any=16			,  snake_case							:   Tuple=2			,  snake_case							:   Dict=0.02			,  snake_case							:   Tuple=3			,  snake_case							:   Dict=4			,  snake_case							:   Optional[Any]=None			,  )   -> str:
							_lowercase					:  Optional[int]    			=      parent
							_lowercase					:  Union[str, Any]    			=      13
							_lowercase					:  Dict    			=      7
							_lowercase					:  Any    			=      True
							_lowercase					:  Tuple    			=      True
							_lowercase					:  Optional[Any]    			=      True
							_lowercase					:  Optional[Any]    			=      True
							_lowercase					:  str    			=      99
							_lowercase					:  Optional[int]    			=      32
							_lowercase					:  int    			=      2
							_lowercase					:  Optional[int]    			=      4
							_lowercase					:  List[str]    			=      37
							_lowercase					:  Dict    			=      "gelu"
							_lowercase					:  str    			=      0.1
							_lowercase					:  Optional[int]    			=      0.1
							_lowercase					:  int    			=      512
							_lowercase					:  str    			=      16
							_lowercase					:  List[Any]    			=      False
							_lowercase					:  Dict    			=      -1
							_lowercase					:  Optional[Any]    			=      True
							_lowercase					:  Dict    			=      2
							_lowercase					:  Union[str, Any]    			=      0.02
							_lowercase					:  int    			=      3
							_lowercase					:  Tuple    			=      4
							_lowercase					:  Optional[Any]    			=      None






	def 			_a(self							:   List[str]						)   -> List[Any]:
							_lowercase					:  Optional[Any]    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.vocab_size						)

							_lowercase					:  Optional[Any]    			=      None
							if self.use_input_mask:
													_lowercase					:  int    			=      random_attention_mask([self.batch_size, self.seq_length]						)

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

							_lowercase					:  Optional[Any]    			=      None
							_lowercase					:  Tuple    			=      None
							_lowercase					:  List[str]    			=      None
							if self.use_labels:
													_lowercase					:  List[Any]    			=      ids_tensor([self.batch_size]			,  self.type_sequence_label_size						)
													_lowercase					:  Union[str, Any]    			=      ids_tensor([self.batch_size, self.seq_length]			,  self.num_labels						)

							_lowercase					:  Union[str, Any]    			=      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			,  relative_attention=self.relative_attention			,  max_relative_positions=self.max_relative_positions			,  position_biased_input=self.position_biased_input			,  initializer_range=self.initializer_range			,  return_dict=snake_case			,  )

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






	def 			_a(self							:   Dict			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   List[Any]			,  snake_case							:   str			,  snake_case							:   Optional[int]			,  snake_case							:   List[Any]			,  snake_case							:   Union[str, Any]						)   -> str:
							_lowercase					:  Tuple    			=      TFDebertaModel(config=snake_case						)
							_lowercase					:  Any    			=      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

							_lowercase					:  Tuple    			=      [input_ids, input_mask]
							_lowercase					:  Any    			=      model(snake_case						)

							_lowercase					:  Any    			=      model(snake_case						)

							self.parent.assertEqual(result.last_hidden_state.shape			,  (self.batch_size, self.seq_length, self.hidden_size)						)






	def 			_a(self							:   int			,  snake_case							:   int			,  snake_case							:   Union[str, Any]			,  snake_case							:   Tuple			,  snake_case							:   List[Any]			,  snake_case							:   Any			,  snake_case							:   Union[str, Any]			,  snake_case							:   int						)   -> int:
							_lowercase					:  int    			=      TFDebertaForMaskedLM(config=snake_case						)
							_lowercase					:  List[Any]    			=      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							_lowercase					:  int    			=      model(snake_case						)
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.vocab_size)						)






	def 			_a(self							:   str			,  snake_case							:   Dict			,  snake_case							:   Optional[int]			,  snake_case							:   int			,  snake_case							:   Tuple			,  snake_case							:   int			,  snake_case							:   Any			,  snake_case							:   Any						)   -> List[str]:
							_lowercase					:  Dict    			=      self.num_labels
							_lowercase					:  Tuple    			=      TFDebertaForSequenceClassification(config=snake_case						)
							_lowercase					:  Optional[Any]    			=      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							_lowercase					:  Dict    			=      model(snake_case						)
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.num_labels)						)






	def 			_a(self							:   Optional[Any]			,  snake_case							:   int			,  snake_case							:   Dict			,  snake_case							:   str			,  snake_case							:   str			,  snake_case							:   Optional[int]			,  snake_case							:   int			,  snake_case							:   List[str]						)   -> Dict:
							_lowercase					:  Union[str, Any]    			=      self.num_labels
							_lowercase					:  List[Any]    			=      TFDebertaForTokenClassification(config=snake_case						)
							_lowercase					:  Union[str, Any]    			=      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}
							_lowercase					:  str    			=      model(snake_case						)
							self.parent.assertEqual(result.logits.shape			,  (self.batch_size, self.seq_length, self.num_labels)						)






	def 			_a(self							:   Dict			,  snake_case							:   List[Any]			,  snake_case							:   Optional[Any]			,  snake_case							:   Optional[int]			,  snake_case							:   Union[str, Any]			,  snake_case							:   Dict			,  snake_case							:   str			,  snake_case							:   Optional[Any]						)   -> Optional[Any]:
							_lowercase					:  str    			=      TFDebertaForQuestionAnswering(config=snake_case						)
							_lowercase					:  Optional[Any]    			=      {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							}

							_lowercase					:  Tuple    			=      model(snake_case						)
							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 			_a(self							:   List[Any]						)   -> Optional[Any]:
							_lowercase					:  int    			=      self.prepare_config_and_inputs()
							(
							    (
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						(
							    _lowercase
							)			,						
							)					:  Optional[Any]    			=      config_and_inputs
							_lowercase					:  str    			=      {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
							return config, inputs_dict




@require_tf
class      __lowercase	(	__snake_case      ,   __snake_case      ,   unittest.TestCase					):
	_A   			=      (
	    (
	        TFDebertaModel,
	        TFDebertaForMaskedLM,
	        TFDebertaForQuestionAnswering,
	        TFDebertaForSequenceClassification,
	        TFDebertaForTokenClassification,
	    )
	    if is_tf_available()
	    else ()
	)
	_A   			=      (
	    {
	        "feature-extraction": TFDebertaModel,
	        "fill-mask": TFDebertaForMaskedLM,
	        "question-answering": TFDebertaForQuestionAnswering,
	        "text-classification": TFDebertaForSequenceClassification,
	        "token-classification": TFDebertaForTokenClassification,
	        "zero-shot": TFDebertaForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)

	_A   			=      False
	_A   			=      False






	def 			_a(self							:   int						)   -> List[Any]:
							_lowercase					:  List[str]    			=      TFDebertaModelTester(self						)
							_lowercase					:  Optional[int]    			=      ConfigTester(self			,  config_class=snake_case			,  hidden_size=37						)






	def 			_a(self							:   Any						)   -> Tuple:
							self.config_tester.run_common_tests()






	def 			_a(self							:   Optional[Any]						)   -> Dict:
							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*snake_case						)






	def 			_a(self							:   Optional[Any]						)   -> Any:
							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_masked_lm(*snake_case						)






	def 			_a(self							:   str						)   -> int:
							_lowercase					:  int    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_question_answering(*snake_case						)






	def 			_a(self							:   Any						)   -> List[Any]:
							_lowercase					:  Any    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_sequence_classification(*snake_case						)






	def 			_a(self							:   Tuple						)   -> str:
							_lowercase					:  Union[str, Any]    			=      self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_for_token_classification(*snake_case						)






	@slow
	def 			_a(self							:   Optional[Any]						)   -> int:
							_lowercase					:  Optional[int]    			=      TFDebertaModel.from_pretrained("kamalkraj/deberta-base"						)
							self.assertIsNotNone(snake_case						)




@require_tf
class      __lowercase	(	unittest.TestCase					):






	@unittest.skip(reason="Model not available yet"						)
	def 			_a(self							:   Optional[int]						)   -> int:
							pass






	@slow
	def 			_a(self							:   Optional[int]						)   -> Optional[int]:
							_lowercase					:  Optional[Any]    			=      TFDebertaModel.from_pretrained("kamalkraj/deberta-base"						)
							_lowercase					:  List[Any]    			=      tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]						)
							_lowercase					:  Union[str, Any]    			=      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]						)
							_lowercase					:  List[str]    			=      model(snake_case			,  attention_mask=snake_case						)[0]

							_lowercase					:  List[str]    			=      tf.constant(
							    [
							        [
							            [-0.59_85_58_96, -0.80_55_25_66, -0.8_46_21_35],
							            [1.4_48_40_25, -0.93_48_37_94, -0.80_59_30_85],
							            [0.3_12_27_41, 0.00_31_60_59, -1.4_13_13_77],
							        ]
							    ]						)
							tf.debugging.assert_near(output[:, 1:4, 1:4]			,  snake_case			,  atol=1e-4						)




