


'''simple docstring'''



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 						_snake_case   (       a__		):




					def __init__( self							,							_lowerCamelCase							,							_lowerCamelCase=13							,							_lowerCamelCase=7							,							_lowerCamelCase=True							,							_lowerCamelCase=True							,							_lowerCamelCase=True							,							_lowerCamelCase=True							,							_lowerCamelCase=99							,							_lowerCamelCase=32							,							_lowerCamelCase=5							,							_lowerCamelCase=4							,							_lowerCamelCase=37							,							_lowerCamelCase="gelu"							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.1							,							_lowerCamelCase=512							,							_lowerCamelCase=16							,							_lowerCamelCase=2							,							_lowerCamelCase=0.02							,							_lowerCamelCase=False							,							_lowerCamelCase=True							,							_lowerCamelCase="None"							,							_lowerCamelCase=3							,							_lowerCamelCase=4							,							_lowerCamelCase=None							,							):
								UpperCAmelCase__					:   List[str]       				=       parent
								UpperCAmelCase__					:   Tuple       				=       batch_size
								UpperCAmelCase__					:   Any       				=       seq_length
								UpperCAmelCase__					:   List[str]       				=       is_training
								UpperCAmelCase__					:   Tuple       				=       use_input_mask
								UpperCAmelCase__					:   Optional[int]       				=       use_token_type_ids
								UpperCAmelCase__					:   Any       				=       use_labels
								UpperCAmelCase__					:   int       				=       vocab_size
								UpperCAmelCase__					:   List[str]       				=       hidden_size
								UpperCAmelCase__					:   List[Any]       				=       num_hidden_layers
								UpperCAmelCase__					:   Optional[int]       				=       num_attention_heads
								UpperCAmelCase__					:   List[Any]       				=       intermediate_size
								UpperCAmelCase__					:   Union[str, Any]       				=       hidden_act
								UpperCAmelCase__					:   int       				=       hidden_dropout_prob
								UpperCAmelCase__					:   str       				=       attention_probs_dropout_prob
								UpperCAmelCase__					:   str       				=       max_position_embeddings
								UpperCAmelCase__					:   Any       				=       type_vocab_size
								UpperCAmelCase__					:   Optional[int]       				=       type_sequence_label_size
								UpperCAmelCase__					:   int       				=       initializer_range
								UpperCAmelCase__					:   Union[str, Any]       				=       num_labels
								UpperCAmelCase__					:   Dict       				=       num_choices
								UpperCAmelCase__					:   Union[str, Any]       				=       relative_attention
								UpperCAmelCase__					:   List[str]       				=       position_biased_input
								UpperCAmelCase__					:   List[str]       				=       pos_att_type
								UpperCAmelCase__					:   Dict       				=       scope




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)

								UpperCAmelCase__					:   Union[str, Any]       				=       None
								if self.use_input_mask:
											UpperCAmelCase__					:   Tuple       				=       ids_tensor([self.batch_size, self.seq_length]							,							vocab_size=2)

								UpperCAmelCase__					:   Optional[int]       				=       None
								if self.use_token_type_ids:
											UpperCAmelCase__					:   Any       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.type_vocab_size)

								UpperCAmelCase__					:   str       				=       None
								UpperCAmelCase__					:   Dict       				=       None
								UpperCAmelCase__					:   Dict       				=       None
								if self.use_labels:
											UpperCAmelCase__					:   str       				=       ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
											UpperCAmelCase__					:   Tuple       				=       ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels)
											UpperCAmelCase__					:   Tuple       				=       ids_tensor([self.batch_size]							,							self.num_choices)

								UpperCAmelCase__					:   List[Any]       				=       self.get_config()

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




					def 			snake_case__  ( self):
								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):
								UpperCAmelCase__					:   Tuple       				=       self.get_config()
								UpperCAmelCase__					:   Tuple       				=       300
								return config




					def 			snake_case__  ( self							,							_lowerCamelCase):
								self.parent.assertListEqual(list(result.loss.size())							,							[])




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__					:   Optional[int]       				=       DebertaModel(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__					:   str       				=       model(_lowerCamelCase							,							attention_mask=_lowerCamelCase							,							token_type_ids=_lowerCamelCase)[0]
								UpperCAmelCase__					:   int       				=       model(_lowerCamelCase							,							token_type_ids=_lowerCamelCase)[0]
								UpperCAmelCase__					:   Union[str, Any]       				=       model(_lowerCamelCase)[0]

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




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__					:   str       				=       DebertaForMaskedLM(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__					:   str       				=       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 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__					:   Optional[Any]       				=       self.num_labels
								UpperCAmelCase__					:   Union[str, Any]       				=       DebertaForSequenceClassification(_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__					:   Dict       				=       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 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__					:   str       				=       self.num_labels
								UpperCAmelCase__					:   Optional[int]       				=       DebertaForTokenClassification(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__					:   Optional[Any]       				=       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 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								UpperCAmelCase__					:   List[Any]       				=       DebertaForQuestionAnswering(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__					:   Optional[int]       				=       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 			snake_case__  ( self):
								UpperCAmelCase__					:   str       				=       self.prepare_config_and_inputs()
								(
								    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    (
								    UpperCAmelCase__
								)				,    
								)					:   Optional[Any]       				=       config_and_inputs
								UpperCAmelCase__					:   Union[str, Any]       				=       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
								return config, inputs_dict


@require_torch
class 						_snake_case   (       a__    ,				a__    ,				unittest.TestCase		):
					lowerCAmelCase :List[Any]						=   (
					    (
					        DebertaModel,
					        DebertaForMaskedLM,
					        DebertaForSequenceClassification,
					        DebertaForTokenClassification,
					        DebertaForQuestionAnswering,
					    )
					    if is_torch_available()
					    else ()
					)
					lowerCAmelCase :Any						=   (
					    {
					        '''feature-extraction''': DebertaModel,
					        '''fill-mask''': DebertaForMaskedLM,
					        '''question-answering''': DebertaForQuestionAnswering,
					        '''text-classification''': DebertaForSequenceClassification,
					        '''token-classification''': DebertaForTokenClassification,
					        '''zero-shot''': DebertaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					lowerCAmelCase :List[Any]						=   True
					lowerCAmelCase :List[str]						=   False
					lowerCAmelCase :int						=   False
					lowerCAmelCase :Dict						=   False
					lowerCAmelCase :List[Any]						=   False




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   str       				=       DebertaModelTester(self)
								UpperCAmelCase__					:   Dict       				=       ConfigTester(self							,							config_class=_lowerCamelCase							,							hidden_size=37)




					def 			snake_case__  ( self):
								self.config_tester.run_common_tests()




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_model(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Tuple       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_sequence_classification(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_masked_lm(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_question_answering(*_lowerCamelCase)




					def 			snake_case__  ( self):
								UpperCAmelCase__					:   Optional[Any]       				=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_deberta_for_token_classification(*_lowerCamelCase)




					@slow
					def 			snake_case__  ( self):
								for model_name in DEBERTA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCAmelCase__					:   Dict       				=       DebertaModel.from_pretrained(_lowerCamelCase)
											self.assertIsNotNone(_lowerCamelCase)


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




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




					@slow
					def 			snake_case__  ( self):
								UpperCAmelCase__					:   List[str]       				=       DebertaModel.from_pretrained("""microsoft/deberta-base""")

								UpperCAmelCase__					:   Union[str, Any]       				=       torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]])
								UpperCAmelCase__					:   str       				=       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]])
								with torch.no_grad():
											UpperCAmelCase__					:   Optional[Any]       				=       model(_lowerCamelCase							,							attention_mask=_lowerCamelCase)[0]
								# compare the actual values for a slice.
								UpperCAmelCase__					:   int       				=       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]}''')



'''simple docstring'''



import os
from typing import Optional

import fsspec
from fsspec.archive import AbstractArchiveFileSystem
from fsspec.utils import DEFAULT_BLOCK_SIZE


class 						_snake_case   (       a__		):
					lowerCAmelCase :Optional[int]						=   ''''''
					lowerCAmelCase :str						=   (
					    None  # protocol passed in prefix to the url. ex: "gzip", for gzip://file.txt::http://foo.bar/file.txt.gz
					)
					lowerCAmelCase :str						=   None  # compression type in fsspec. ex: "gzip"
					lowerCAmelCase :str						=   None  # extension of the filename to strip. ex: "".gz" to get file.txt from file.txt.gz




					def __init__( self							,							_lowerCamelCase = ""							,							_lowerCamelCase = None							,							_lowerCamelCase = None							,							**_lowerCamelCase):
								super().__init__(self							,							**_lowerCamelCase)
								# always open as "rb" since fsspec can then use the TextIOWrapper to make it work for "r" mode
								UpperCAmelCase__					:   Optional[Any]       				=       fsspec.open(
								    _lowerCamelCase							,							mode="""rb"""							,							protocol=_lowerCamelCase							,							compression=self.compression							,							client_kwargs={
								        """requote_redirect_url""": False,  # see https://github.com/huggingface/datasets/pull/5459
								        """trust_env""": True,  # Enable reading proxy env variables.
								        **(target_options or {}).pop("""client_kwargs"""							,							{}),  # To avoid issues if it was already passed.
								    }							,							**(target_options or {})							,							)
								UpperCAmelCase__					:   List[Any]       				=       os.path.basename(self.file.path.split("""::""")[0])
								UpperCAmelCase__					:   Dict       				=       (
								    self.compressed_name[: self.compressed_name.rindex(""".""")]
								    if """.""" in self.compressed_name
								    else self.compressed_name
								)
								UpperCAmelCase__					:   Tuple       				=       None




					@classmethod
					def 			snake_case__  ( cls							,							_lowerCamelCase):
								# compressed file paths are always relative to the archive root
								return super()._strip_protocol(_lowerCamelCase).lstrip("""/""")




					def 			snake_case__  ( self):
								if self.dir_cache is None:
											UpperCAmelCase__					:   Optional[Any]       				=       {**self.file.fs.info(self.file.path), """name""": self.uncompressed_name}
											UpperCAmelCase__					:   Union[str, Any]       				=       {f["""name"""]: f}




					def 			snake_case__  ( self							,							_lowerCamelCase):
								return self.file.open().read()




					def 			snake_case__  ( self							,							_lowerCamelCase							,							_lowerCamelCase = "rb"							,							_lowerCamelCase=None							,							_lowerCamelCase=True							,							_lowerCamelCase=None							,							**_lowerCamelCase							,							):
								UpperCAmelCase__					:   List[str]       				=       self._strip_protocol(_lowerCamelCase)
								if mode != "rb":
											raise ValueError(f'''Tried to read with mode {mode} on file {self.file.path} opened with mode \'rb\'''')
								return self.file.open()


class 						_snake_case   (       a__		):
					lowerCAmelCase :Dict						=   '''bz2'''
					lowerCAmelCase :List[str]						=   '''bz2'''
					lowerCAmelCase :Dict						=   '''.bz2'''


class 						_snake_case   (       a__		):
					lowerCAmelCase :int						=   '''gzip'''
					lowerCAmelCase :Tuple						=   '''gzip'''
					lowerCAmelCase :str						=   '''.gz'''


class 						_snake_case   (       a__		):
					lowerCAmelCase :List[str]						=   '''lz4'''
					lowerCAmelCase :Any						=   '''lz4'''
					lowerCAmelCase :int						=   '''.lz4'''


class 						_snake_case   (       a__		):
					lowerCAmelCase :Union[str, Any]						=   '''xz'''
					lowerCAmelCase :int						=   '''xz'''
					lowerCAmelCase :List[Any]						=   '''.xz'''


class 						_snake_case   (       a__		):
					lowerCAmelCase :Tuple						=   '''zstd'''
					lowerCAmelCase :List[str]						=   '''zstd'''
					lowerCAmelCase :Union[str, Any]						=   '''.zst'''




					def __init__( self							,							_lowerCamelCase							,							_lowerCamelCase = "rb"							,							_lowerCamelCase = None							,							_lowerCamelCase = None							,							_lowerCamelCase = DEFAULT_BLOCK_SIZE							,							**_lowerCamelCase							,							):
								super().__init__(
								    fo=_lowerCamelCase							,							mode=_lowerCamelCase							,							target_protocol=_lowerCamelCase							,							target_options=_lowerCamelCase							,							block_size=_lowerCamelCase							,							**_lowerCamelCase							,							)
								# We need to wrap the zstd decompressor to avoid this error in fsspec==2021.7.0 and zstandard==0.15.2:
								#
								# File "/Users/user/.virtualenvs/hf-datasets/lib/python3.7/site-packages/fsspec/core.py", line 145, in open
								#     out.close = close
								# AttributeError: 'zstd.ZstdDecompressionReader' object attribute 'close' is read-only
								#
								# see https://github.com/intake/filesystem_spec/issues/725
								UpperCAmelCase__					:   Dict       				=       self.file.__enter__


								class 						_snake_case   :




													def __init__( self							,							_lowerCamelCase):
																UpperCAmelCase__					:   Optional[int]       				=       file_




													def __enter__( self):
																self._file.__enter__()
																return self




													def __exit__( self							,							*_lowerCamelCase							,							**_lowerCamelCase):
																self._file.__exit__(*_lowerCamelCase							,							**_lowerCamelCase)




													def __iter__( self):
																return iter(self._file)




													def 			snake_case__  ( self):
																return next(self._file)




													def __getattr__( self							,							_lowerCamelCase):
																return getattr(self._file							,							_lowerCamelCase)

								def fixed_enter(*_lowerCamelCase							,							**_lowerCamelCase):
											return WrappedFile(_enter(*_lowerCamelCase							,							**_lowerCamelCase))

								UpperCAmelCase__					:   List[Any]       				=       fixed_enter