


"""simple docstring"""

import unittest

from transformers import DebertaVaConfig, 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 (
           DebertaVaForMaskedLM,
           DebertaVaForMultipleChoice,
           DebertaVaForQuestionAnswering,
           DebertaVaForSequenceClassification,
           DebertaVaForTokenClassification,
           DebertaVaModel,
       )
       from transformers.models.deberta_va.modeling_deberta_va import DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST





class    lowerCamelCase__						(		_a    ):
      def __init__(		self	:	str      ,       A_	:	Any      ,       A_	:	Union[str, Any]=1_3      ,       A_	:	str=7      ,       A_	:	Tuple=True      ,       A_	:	Any=True      ,       A_	:	Dict=True      ,       A_	:	str=True      ,       A_	:	Optional[Any]=9_9      ,       A_	:	int=3_2      ,       A_	:	Dict=5      ,       A_	:	Union[str, Any]=4      ,       A_	:	Tuple=3_7      ,       A_	:	Any="gelu"      ,       A_	:	List[str]=0.1      ,       A_	:	int=0.1      ,       A_	:	List[Any]=5_1_2      ,       A_	:	Optional[Any]=1_6      ,       A_	:	Optional[int]=2      ,       A_	:	Any=0.02      ,       A_	:	int=False      ,       A_	:	Any=True      ,       A_	:	Optional[Any]="None"      ,       A_	:	int=3      ,       A_	:	Any=4      ,       A_	:	Union[str, Any]=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 SCREAMING_SNAKE_CASE_	(		self	:	List[str]							):






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






       '''simple docstring'''


       return DebertaVaConfig(
           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 SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]      ,       A_	:	int							):






       '''simple docstring'''


       self.parent.assertListEqual(list(result.loss.size()							)      ,       []							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int      ,       A_	:	List[str]      ,       A_	:	List[Any]      ,       A_	:	str      ,       A_	:	str      ,       A_	:	Tuple      ,       A_	:	Tuple      ,       A_	:	Dict							):






       '''simple docstring'''


       __lowercase									=	DebertaVaModel(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	model(A_      ,       attention_mask=A_      ,       token_type_ids=A_							)[0]
       __lowercase									=	model(A_      ,       token_type_ids=A_							)[0]
       __lowercase									=	model(A_							)[0]

       self.parent.assertListEqual(list(sequence_output.size()							)      ,       [self.batch_size, self.seq_length, self.hidden_size]							)
      def SCREAMING_SNAKE_CASE_	(		self	:	List[Any]      ,       A_	:	List[str]      ,       A_	:	Any      ,       A_	:	str      ,       A_	:	Dict      ,       A_	:	Tuple      ,       A_	:	List[Any]      ,       A_	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	DebertaVaForMaskedLM(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	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 SCREAMING_SNAKE_CASE_	(		self	:	str      ,       A_	:	int      ,       A_	:	List[Any]      ,       A_	:	str      ,       A_	:	Any      ,       A_	:	Any      ,       A_	:	Dict      ,       A_	:	int							):






       '''simple docstring'''


       __lowercase									=	self.num_labels
       __lowercase									=	DebertaVaForSequenceClassification(A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	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 SCREAMING_SNAKE_CASE_	(		self	:	Dict      ,       A_	:	str      ,       A_	:	str      ,       A_	:	Dict      ,       A_	:	Tuple      ,       A_	:	List[Any]      ,       A_	:	Union[str, Any]      ,       A_	:	int							):






       '''simple docstring'''


       __lowercase									=	self.num_labels
       __lowercase									=	DebertaVaForTokenClassification(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	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 SCREAMING_SNAKE_CASE_	(		self	:	Tuple      ,       A_	:	Union[str, Any]      ,       A_	:	Dict      ,       A_	:	Optional[Any]      ,       A_	:	Tuple      ,       A_	:	int      ,       A_	:	Union[str, Any]      ,       A_	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	DebertaVaForQuestionAnswering(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	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 SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]      ,       A_	:	Tuple      ,       A_	:	Any      ,       A_	:	Optional[Any]      ,       A_	:	Tuple      ,       A_	:	Any      ,       A_	:	str      ,       A_	:	List[str]							):






       '''simple docstring'''


       __lowercase									=	DebertaVaForMultipleChoice(config=A_							)
       model.to(A_							)
       model.eval()
       __lowercase									=	input_ids.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	token_type_ids.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	input_mask.unsqueeze(1							).expand(-1      ,       self.num_choices      ,       -1							).contiguous()
       __lowercase									=	model(
           A_      ,       attention_mask=A_      ,       token_type_ids=A_      ,       labels=A_      ,       )
       self.parent.assertEqual(result.logits.shape      ,       (self.batch_size, self.num_choices)							)






      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''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    lowerCamelCase__						(		_a			,   _a			,   unittest.TestCase    ):
      a :     Optional[int]											=		(
          (
              DebertaVaModel,
              DebertaVaForMaskedLM,
              DebertaVaForSequenceClassification,
              DebertaVaForTokenClassification,
              DebertaVaForQuestionAnswering,
              DebertaVaForMultipleChoice,
          )
          if is_torch_available()
          else ()
      )
      a :     int											=		(
          {
              """feature-extraction""": DebertaVaModel,
              """fill-mask""": DebertaVaForMaskedLM,
              """question-answering""": DebertaVaForQuestionAnswering,
              """text-classification""": DebertaVaForSequenceClassification,
              """token-classification""": DebertaVaForTokenClassification,
              """zero-shot""": DebertaVaForSequenceClassification,
          }
          if is_torch_available()
          else {}
      )

      a :     Tuple											=		True
      a :     List[Any]											=		False
      a :     Dict											=		False
      a :     Any											=		False
      a :     Union[str, Any]											=		False
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	DebertaVaModelTester(self							)
       __lowercase									=	ConfigTester(self      ,       config_class=A_      ,       hidden_size=3_7							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict							):






       '''simple docstring'''


       self.config_tester.run_common_tests()
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_model(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Any							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_sequence_classification(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	List[str]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_masked_lm(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_question_answering(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_token_classification(*A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple							):






       '''simple docstring'''


       __lowercase									=	self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_deberta_for_multiple_choice(*A_							)






      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	List[str]							):






       '''simple docstring'''


       for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        __lowercase									=	DebertaVaModel.from_pretrained(A_							)
        self.assertIsNotNone(A_							)






@require_torch
@require_sentencepiece
@require_tokenizers
class    lowerCamelCase__						(		unittest.TestCase    ):
      @unittest.skip(reason="""Model not available yet"""							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       pass






      @slow
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict							):






       '''simple docstring'''


       __lowercase									=	DebertaVaModel.from_pretrained("""microsoft/deberta-v2-xlarge"""							)

       __lowercase									=	torch.tensor([[0, 3_1_4_1_4, 2_3_2, 3_2_8, 7_4_0, 1_1_4_0, 1_2_6_9_5, 6_9, 4_6_0_7_8, 1_5_8_8, 2]]							)
       __lowercase									=	torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
       with torch.no_grad():
        __lowercase									=	model(A_      ,       attention_mask=A_							)[0]
       # compare the actual values for a slice.
       __lowercase									=	torch.tensor(
           [[[0.23_56, 0.19_48, 0.03_69], [-0.10_63, 0.35_86, -0.51_52], [-0.63_99, -0.02_59, -0.25_25]]]							)
       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,       A_      ,       atol=1e-4							)      ,       F'''{output[:, 1:4, 1:4]}'''							)




"""simple docstring"""

import os
import shutil
import tempfile
from unittest import TestCase
from unittest.mock import patch

import numpy as np
from datasets import Dataset

from transformers.models.realm.configuration_realm import RealmConfig
from transformers.models.realm.retrieval_realm import _REALM_BLOCK_RECORDS_FILENAME, RealmRetriever
from transformers.models.realm.tokenization_realm import VOCAB_FILES_NAMES, RealmTokenizer





class    lowerCamelCase__						(		_a    ):
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	tempfile.mkdtemp()
       __lowercase									=	5

       # Realm tok
       __lowercase									=	[
           """[UNK]""",
           """[CLS]""",
           """[SEP]""",
           """[PAD]""",
           """[MASK]""",
           """test""",
           """question""",
           """this""",
           """is""",
           """the""",
           """first""",
           """second""",
           """third""",
           """fourth""",
           """fifth""",
           """record""",
           """want""",
           """##want""",
           """##ed""",
           """wa""",
           """un""",
           """runn""",
           """##ing""",
           """,""",
           """low""",
           """lowest""",
       ]
       __lowercase									=	os.path.join(self.tmpdirname      ,       """realm_tokenizer"""							)
       os.makedirs(A_      ,       exist_ok=A_							)
       __lowercase									=	os.path.join(A_      ,       VOCAB_FILES_NAMES["""vocab_file"""]							)
       with open(self.vocab_file      ,       """w"""      ,       encoding="""utf-8"""							) as vocab_writer:
        vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]							)							)

       __lowercase									=	os.path.join(self.tmpdirname      ,       """realm_block_records"""							)
       os.makedirs(A_      ,       exist_ok=A_							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       return RealmTokenizer.from_pretrained(os.path.join(self.tmpdirname      ,       """realm_tokenizer"""							)							)
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[int]							):






       '''simple docstring'''


       shutil.rmtree(self.tmpdirname							)
      def SCREAMING_SNAKE_CASE_	(		self	:	int							):






       '''simple docstring'''


       __lowercase									=	RealmConfig(num_block_records=self.num_block_records							)
       return config
      def SCREAMING_SNAKE_CASE_	(		self	:	Any							):






       '''simple docstring'''


       __lowercase									=	Dataset.from_dict(
           {
               """id""": ["""0""", """1"""],
               """question""": ["""foo""", """bar"""],
               """answers""": [["""Foo""", """Bar"""], ["""Bar"""]],
           }							)
       return dataset
      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	np.array(
           [
               B"""This is the first record""",
               B"""This is the second record""",
               B"""This is the third record""",
               B"""This is the fourth record""",
               B"""This is the fifth record""",
               B"""This is a longer longer longer record""",
           ]      ,       dtype=A_      ,       )
       return block_records
      def SCREAMING_SNAKE_CASE_	(		self	:	Union[str, Any]							):






       '''simple docstring'''


       __lowercase									=	RealmRetriever(
           block_records=self.get_dummy_block_records()      ,       tokenizer=self.get_tokenizer()      ,       )
       return retriever
      def SCREAMING_SNAKE_CASE_	(		self	:	Dict							):






       '''simple docstring'''


       __lowercase									=	self.get_config()
       __lowercase									=	self.get_dummy_retriever()
       __lowercase									=	retriever.tokenizer

       __lowercase									=	np.array([0, 3]      ,       dtype="""long"""							)
       __lowercase									=	tokenizer(["""Test question"""]							).input_ids
       __lowercase									=	tokenizer(
           ["""the fourth"""]      ,       add_special_tokens=A_      ,       return_token_type_ids=A_      ,       return_attention_mask=A_      ,       ).input_ids
       __lowercase									=	config.reader_seq_len

       __lowercase							,		__lowercase							,		__lowercase							,		__lowercase									=	retriever(
           A_      ,       A_      ,       answer_ids=A_      ,       max_length=A_      ,       return_tensors="""np"""							)

       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(len(A_							)      ,       2							)
       self.assertEqual(concat_inputs.input_ids.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.attention_mask.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.token_type_ids.shape      ,       (2, 1_0)							)
       self.assertEqual(concat_inputs.special_tokens_mask.shape      ,       (2, 1_0)							)
       self.assertEqual(
           tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[0]							)      ,       ["""[CLS]""", """test""", """question""", """[SEP]""", """this""", """is""", """the""", """first""", """record""", """[SEP]"""]      ,       )
       self.assertEqual(
           tokenizer.convert_ids_to_tokens(concat_inputs.input_ids[1]							)      ,       ["""[CLS]""", """test""", """question""", """[SEP]""", """this""", """is""", """the""", """fourth""", """record""", """[SEP]"""]      ,       )
      def SCREAMING_SNAKE_CASE_	(		self	:	str							):






       '''simple docstring'''


       __lowercase									=	self.get_config()
       __lowercase									=	self.get_dummy_retriever()
       __lowercase									=	retriever.tokenizer

       __lowercase									=	np.array([0, 3, 5]      ,       dtype="""long"""							)
       __lowercase									=	tokenizer(["""Test question"""]							).input_ids
       __lowercase									=	tokenizer(
           ["""the fourth""", """longer longer"""]      ,       add_special_tokens=A_      ,       return_token_type_ids=A_      ,       return_attention_mask=A_      ,       ).input_ids
       __lowercase									=	config.reader_seq_len

       __lowercase							,		__lowercase							,		__lowercase							,		__lowercase									=	retriever(
           A_      ,       A_      ,       answer_ids=A_      ,       max_length=A_      ,       return_tensors="""np"""							)

       self.assertEqual([False, True, True]      ,       A_							)
       self.assertEqual([[-1, -1, -1], [6, -1, -1], [6, 7, 8]]      ,       A_							)
       self.assertEqual([[-1, -1, -1], [7, -1, -1], [7, 8, 9]]      ,       A_							)






      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]							):






       '''simple docstring'''


       __lowercase									=	self.get_dummy_retriever()
       retriever.save_pretrained(os.path.join(self.tmpdirname      ,       """realm_block_records"""							)							)

       # Test local path
       __lowercase									=	retriever.from_pretrained(os.path.join(self.tmpdirname      ,       """realm_block_records"""							)							)
       self.assertEqual(retriever.block_records[0]      ,       B"""This is the first record"""							)

       # Test mocked remote path
       with patch("""transformers.models.realm.retrieval_realm.hf_hub_download"""							) as mock_hf_hub_download:
        __lowercase									=	os.path.join(
            os.path.join(self.tmpdirname      ,       """realm_block_records"""							)      ,       _REALM_BLOCK_RECORDS_FILENAME							)
        __lowercase									=	RealmRetriever.from_pretrained("""google/realm-cc-news-pretrained-openqa"""							)

       self.assertEqual(retriever.block_records[0]      ,       B"""This is the first record"""							)
