






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					(       A__	):

				"""simple docstring"""




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

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




				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								lowercase__    =			ids_tensor([self.batch_size, self.seq_length],  self.vocab_size  )

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

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

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

								lowercase__    =			self.get_config()

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




				def 				lowercase__   (     self						:    Any  ):

								'''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 				lowercase__   (     self						:    int  ):

								'''simple docstring'''
								lowercase__    =			self.get_config()
								lowercase__    =			300
								return config




				def 				lowercase__   (     self						:    Optional[Any],  lowerCamelCase						:    Optional[Any]  ):

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




				def 				lowercase__   (     self						:    Tuple,  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    str,  lowerCamelCase						:    Dict,  lowerCamelCase						:    str,  lowerCamelCase						:    Optional[Any]  ):

								'''simple docstring'''
								lowercase__    =			DebertaModel(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase  )[0]
								lowercase__    =			model(lowerCamelCase,  token_type_ids=lowerCamelCase  )[0]
								lowercase__    =			model(lowerCamelCase  )[0]

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




				def 				lowercase__   (     self						:    Any,  lowerCamelCase						:    Any,  lowerCamelCase						:    int,  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    Optional[int],  lowerCamelCase						:    str  ):

								'''simple docstring'''
								lowercase__    =			DebertaForMaskedLM(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  labels=lowerCamelCase  )

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




				def 				lowercase__   (     self						:    List[str],  lowerCamelCase						:    Union[str, Any],  lowerCamelCase						:    List[str],  lowerCamelCase						:    List[str],  lowerCamelCase						:    Union[str, Any],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Union[str, Any]  ):

								'''simple docstring'''
								lowercase__    =			self.num_labels
								lowercase__    =			DebertaForSequenceClassification(lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  labels=lowerCamelCase  )
								self.parent.assertListEqual(list(result.logits.size()  ),  [self.batch_size, self.num_labels]  )
								self.check_loss_output(lowerCamelCase  )




				def 				lowercase__   (     self						:    str,  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Dict,  lowerCamelCase						:    str,  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    str,  lowerCamelCase						:    Optional[int]  ):

								'''simple docstring'''
								lowercase__    =			self.num_labels
								lowercase__    =			DebertaForTokenClassification(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  labels=lowerCamelCase  )
								self.parent.assertEqual(result.logits.shape,  (self.batch_size, self.seq_length, self.num_labels)  )




				def 				lowercase__   (     self						:    int,  lowerCamelCase						:    List[Any],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    Optional[Any],  lowerCamelCase						:    List[Any],  lowerCamelCase						:    List[str],  lowerCamelCase						:    Tuple,  lowerCamelCase						:    List[Any]  ):

								'''simple docstring'''
								lowercase__    =			DebertaForQuestionAnswering(config=lowerCamelCase  )
								model.to(lowerCamelCase  )
								model.eval()
								lowercase__    =			model(
								    lowerCamelCase,  attention_mask=lowerCamelCase,  token_type_ids=lowerCamelCase,  start_positions=lowerCamelCase,  end_positions=lowerCamelCase,  )
								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 				lowercase__   (     self						:    int  ):

								'''simple docstring'''
								lowercase__    =			self.prepare_config_and_inputs()
								(
								    (
								    lowercase__
								) ,						(
								    lowercase__
								) ,						(
								    lowercase__
								) ,						(
								    lowercase__
								) ,						(
								    lowercase__
								) ,						(
								    lowercase__
								) ,						(
								    lowercase__
								) ,						
								)    =			config_and_inputs
								lowercase__    =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
								return config, inputs_dict



@require_torch
class       _UpperCAmelCase					(       A__		,A__		,unittest.TestCase	):

				"""simple docstring"""




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

				lowercase__											=       True
				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False
				lowercase__											=       False




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			DebertaModelTester(self  )
								lowercase__    =			ConfigTester(self,  config_class=lowerCamelCase,  hidden_size=37  )




				def 				lowercase__   (     self						:    Any  ):

								'''simple docstring'''
								self.config_tester.run_common_tests()




				def 				lowercase__   (     self						:    List[str]  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*lowerCamelCase  )




				def 				lowercase__   (     self						:    List[Any]  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*lowerCamelCase  )




				def 				lowercase__   (     self						:    str  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*lowerCamelCase  )




				def 				lowercase__   (     self						:    str  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*lowerCamelCase  )




				def 				lowercase__   (     self						:    Optional[int]  ):

								'''simple docstring'''
								lowercase__    =			self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*lowerCamelCase  )




				@slow
				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												lowercase__    =			DebertaModel.from_pretrained(lowerCamelCase  )
												self.assertIsNotNone(lowerCamelCase  )



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

				"""simple docstring"""




				@unittest.skip(reason='''Model not available yet'''  )
				def 				lowercase__   (     self						:    Dict  ):

								'''simple docstring'''
								pass




				@slow
				def 				lowercase__   (     self						:    Dict  ):

								'''simple docstring'''
								lowercase__    =			DebertaModel.from_pretrained('''microsoft/deberta-base'''  )

								lowercase__    =			torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]  )
								lowercase__    =			torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]  )
								with torch.no_grad():
												lowercase__    =			model(lowerCamelCase,  attention_mask=lowerCamelCase  )[0]
								# compare the actual values for a slice.
								lowercase__    =			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],  lowerCamelCase,  atol=1E-4  ),  F"""{output[:, 1:4, 1:4]}"""  )












from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url
class       _UpperCAmelCase					(       A__	):

				"""simple docstring"""




				lowercase__											=       """"""
				lowercase__											=       """hf-legacy"""  # "hf://"" is reserved for hffs




				def __init__(     self						:    List[str],  lowerCamelCase						:    Optional[DatasetInfo] = None,  lowerCamelCase						:    Optional[str] = None,  **lowerCamelCase						:    str,  ):

								'''simple docstring'''
								super().__init__(self,  **lowerCamelCase  )
								lowercase__    =			repo_info
								lowercase__    =			token
								lowercase__    =			None




				def 				lowercase__   (     self						:    Optional[Any]  ):

								'''simple docstring'''
								if self.dir_cache is None:
												lowercase__    =			{}
												for hf_file in self.repo_info.siblings:
																# TODO(QL): add sizes
																lowercase__    =			{
																    '''name''': hf_file.rfilename,
																    '''size''': None,
																    '''type''': '''file''',
																}
																self.dir_cache.update(
																    {
																        str(lowerCamelCase  ): {'''name''': str(lowerCamelCase  ), '''size''': None, '''type''': '''directory'''}
																        for d in list(PurePosixPath(hf_file.rfilename  ).parents  )[:-1]
																    }  )




				def 				lowercase__   (     self						:    Any,  lowerCamelCase						:    str,  lowerCamelCase						:    str = "rb",  **lowerCamelCase						:    Any,  ):

								'''simple docstring'''
								if not isinstance(self.repo_info,  lowerCamelCase  ):
												raise NotImplementedError(F"""Open is only implemented for dataset repositories, but got {self.repo_info}"""  )
								lowercase__    =			hf_hub_url(self.repo_info.id,  lowerCamelCase,  revision=self.repo_info.sha  )
								return fsspec.open(
								    lowerCamelCase,  mode=lowerCamelCase,  headers=get_authentication_headers_for_url(lowerCamelCase,  use_auth_token=self.token  ),  client_kwargs={'''trust_env''': True},  ).open()




				def 				lowercase__   (     self						:    Dict,  lowerCamelCase						:    Any,  **lowerCamelCase						:    int  ):

								'''simple docstring'''
								self._get_dirs()
								lowercase__    =			self._strip_protocol(lowerCamelCase  )
								if path in self.dir_cache:
												return self.dir_cache[path]
								else:
												raise FileNotFoundError(lowerCamelCase  )




				def 				lowercase__   (     self						:    Optional[Any],  lowerCamelCase						:    List[str],  lowerCamelCase						:    Optional[int]=False,  **lowerCamelCase						:    str  ):

								'''simple docstring'''
								self._get_dirs()
								lowercase__    =			PurePosixPath(path.strip('''/'''  )  )
								lowercase__    =			{}
								for p, f in self.dir_cache.items():
												lowercase__    =			PurePosixPath(p.strip('''/'''  )  )
												lowercase__    =			p.parent
												if root == path:
																lowercase__    =			f
								lowercase__    =			list(paths.values()  )
								if detail:
												return out
								else:
												return sorted(f['''name'''] for f in out  )




