




'''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		(	lowercase_							):




			def __init__(					self				,							a__				,							a__=13				,							a__=7				,							a__=True				,							a__=True				,							a__=True				,							a__=True				,							a__=99				,							a__=32				,							a__=5				,							a__=4				,							a__=37				,							a__="gelu"				,							a__=0.1				,							a__=0.1				,							a__=512				,							a__=16				,							a__=2				,							a__=0.0_2				,							a__=False				,							a__=True				,							a__="None"				,							a__=3				,							a__=4				,							a__=None				,							)				->				List[Any]:

										'''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    lowerCAmelCase__				(					self							)				->				Tuple:

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

										'''simple docstring'''




										snake_case_						=      self.get_config()
										snake_case_						=      300
										return config




			def    lowerCAmelCase__				(					self				,							a__							)				->				List[str]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Any:

										'''simple docstring'''




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

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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Any:

										'''simple docstring'''




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

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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Union[str, Any]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Optional[int]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Optional[int]:

										'''simple docstring'''




										snake_case_						=      DebertaForQuestionAnswering(config=a__							)
										model.to(a__							)
										model.eval()
										snake_case_						=      model(
										    a__				,							attention_mask=a__				,							token_type_ids=a__				,							start_positions=a__				,							end_positions=a__				,							)
										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    lowerCAmelCase__				(					self							)				->				Dict:

										'''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  _snake_case		(	lowercase_						,						lowercase_						,						unittest.TestCase							):
			lowerCAmelCase_     :      Optional[int]						       =    (
			    (
			        DebertaModel,
			        DebertaForMaskedLM,
			        DebertaForSequenceClassification,
			        DebertaForTokenClassification,
			        DebertaForQuestionAnswering,
			    )
			    if is_torch_available()
			    else ()
			)
			lowerCAmelCase_     :      Dict						       =    (
			    {
			        "feature-extraction": DebertaModel,
			        "fill-mask": DebertaForMaskedLM,
			        "question-answering": DebertaForQuestionAnswering,
			        "text-classification": DebertaForSequenceClassification,
			        "token-classification": DebertaForTokenClassification,
			        "zero-shot": DebertaForSequenceClassification,
			    }
			    if is_torch_available()
			    else {}
			)

			lowerCAmelCase_     :      Optional[int]						       =    True
			lowerCAmelCase_     :      Union[str, Any]						       =    False
			lowerCAmelCase_     :      Tuple						       =    False
			lowerCAmelCase_     :      Optional[int]						       =    False
			lowerCAmelCase_     :      Tuple						       =    False




			def    lowerCAmelCase__				(					self							)				->				int:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self							)				->				int:

										'''simple docstring'''




										self.config_tester.run_common_tests()




			def    lowerCAmelCase__				(					self							)				->				Dict:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self							)				->				Dict:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self							)				->				Optional[int]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self							)				->				str:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''simple docstring'''




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




			@slow
			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''simple docstring'''




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




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




			@unittest.skip(reason="Model not available yet"							)
			def    lowerCAmelCase__				(					self							)				->				List[Any]:

										'''simple docstring'''




										pass




			@slow
			def    lowerCAmelCase__				(					self							)				->				Optional[int]:

										'''simple docstring'''




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

										snake_case_						=      torch.tensor([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]							)
										snake_case_						=      torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
										with torch.no_grad():
																	snake_case_						=      model(a__				,							attention_mask=a__							)[0]
										# compare the actual values for a slice.
										snake_case_						=      torch.tensor(
										    [[[-0.5_9_8_6, -0.8_0_5_5, -0.8_4_6_2], [1.4_4_8_4, -0.9_3_4_8, -0.8_0_5_9], [0.3_1_2_3, 0.0_0_3_2, -1.4_1_3_1]]]							)
										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]				,							a__				,							atol=1e-4							)				,							F'{output[:, 1:4, 1:4]}'							)










"""simple docstring"""


import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ....tokenization_utils_fast import PreTrainedTokenizerFast
from ....utils import logging
from .tokenization_retribert import RetriBertTokenizer


A_						=				logging.get_logger(__name__)

A_						=				{'''vocab_file''': '''vocab.txt''', '''tokenizer_file''': '''tokenizer.json'''}

A_						=				{
    '''vocab_file''': {
        '''yjernite/retribert-base-uncased''': (
            '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/vocab.txt'''
        ),
    },
    '''tokenizer_file''': {
        '''yjernite/retribert-base-uncased''': (
            '''https://huggingface.co/yjernite/retribert-base-uncased/resolve/main/tokenizer.json'''
        ),
    },
}

A_						=				{
    '''yjernite/retribert-base-uncased''': 5_12,
}


A_						=				{
    '''yjernite/retribert-base-uncased''': {'''do_lower_case''': True},
}






class 					lowercase(     __a ):







 '''simple docstring'''





 lowercase__ 					=					VOCAB_FILES_NAMES
 lowercase__ 					=					PRETRAINED_VOCAB_FILES_MAP
 lowercase__ 					=					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 lowercase__ 					=					PRETRAINED_INIT_CONFIGURATION
 lowercase__ 					=					RetriBertTokenizer
 lowercase__ 					=					["input_ids", "attention_mask"]


 def __init__(				self:		int,      a_:		int=None,      a_:		Dict=None,      a_:		Any=True,      a_:		int="[UNK]",      a_:		Any="[SEP]",      a_:		List[Any]="[PAD]",      a_:		List[Any]="[CLS]",      a_:		str="[MASK]",      a_:		Dict=True,      a_:		Optional[int]=None,      **a_:		Tuple,      ):




   '''simple docstring'''



   super().__init__(
       a_,      tokenizer_file=a_,      do_lower_case=a_,      unk_token=a_,      sep_token=a_,      pad_token=a_,      cls_token=a_,      mask_token=a_,      tokenize_chinese_chars=a_,      strip_accents=a_,      **a_,      )

   _snake_case							:		List[Any]								=  json.loads(self.backend_tokenizer.normalizer.__getstate__()		)
   if (
       normalizer_state.get("""lowercase""",      a_		) != do_lower_case
       or normalizer_state.get("""strip_accents""",      a_		) != strip_accents
       or normalizer_state.get("""handle_chinese_chars""",      a_		) != tokenize_chinese_chars
   ):
     _snake_case							:		Dict								=  getattr(a_,      normalizer_state.pop("""type"""		)		)
     _snake_case							:		List[Any]								=  do_lower_case
     _snake_case							:		List[str]								=  strip_accents
     _snake_case							:		Tuple								=  tokenize_chinese_chars
     _snake_case							:		Tuple								=  normalizer_class(**a_		)

   _snake_case							:		List[str]								=  do_lower_case


 def  UpperCamelCase_   (				self:		Any,      a_:		str,      a_:		Optional[int]=None		):




   '''simple docstring'''



   _snake_case							:		Optional[Any]								=  [self.cls_token_id] + token_ids_a + [self.sep_token_id]

   if token_ids_a:
     output += token_ids_a + [self.sep_token_id]

   return output


 def  UpperCamelCase_   (				self:		List[str],      a_:		List[int],      a_:		Optional[List[int]] = None		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  [self.sep_token_id]
   _snake_case							:		List[str]								=  [self.cls_token_id]
   if token_ids_a is None:
     return len(cls + token_ids_a + sep		) * [0]
   return len(cls + token_ids_a + sep		) * [0] + len(token_ids_a + sep		) * [1]


 def  UpperCamelCase_   (				self:		Dict,      a_:		str,      a_:		Optional[str] = None		):




   '''simple docstring'''



   _snake_case							:		Union[str, Any]								=  self._tokenizer.model.save(a_,      name=a_		)
   return tuple(a_		)


