



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 SCREAMING_SNAKE_CASE_ (					_a   ):





					"""simple docstring"""


					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	,  ):
										lowerCamelCase__       =       parent
										lowerCamelCase__       =       batch_size
										lowerCamelCase__       =       seq_length
										lowerCamelCase__       =       is_training
										lowerCamelCase__       =       use_input_mask
										lowerCamelCase__       =       use_token_type_ids
										lowerCamelCase__       =       use_labels
										lowerCamelCase__       =       vocab_size
										lowerCamelCase__       =       hidden_size
										lowerCamelCase__       =       num_hidden_layers
										lowerCamelCase__       =       num_attention_heads
										lowerCamelCase__       =       intermediate_size
										lowerCamelCase__       =       hidden_act
										lowerCamelCase__       =       hidden_dropout_prob
										lowerCamelCase__       =       attention_probs_dropout_prob
										lowerCamelCase__       =       max_position_embeddings
										lowerCamelCase__       =       type_vocab_size
										lowerCamelCase__       =       type_sequence_label_size
										lowerCamelCase__       =       initializer_range
										lowerCamelCase__       =       num_labels
										lowerCamelCase__       =       num_choices
										lowerCamelCase__       =       relative_attention
										lowerCamelCase__       =       position_biased_input
										lowerCamelCase__       =       pos_att_type
										lowerCamelCase__       =       scope





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       ids_tensor([self.batch_size, self.seq_length]	,  self.vocab_size	)

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

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

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

										lowerCamelCase__       =       self.get_config()

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





					def 					__magic_name__ ( self	):
										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 					__magic_name__ ( self	,  _lowerCAmelCase	):
										self.parent.assertListEqual(list(result.loss.size()	)	,  []	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       DebertaVaModel(config=snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       model(snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	)[0]
										lowerCamelCase__       =       model(snake_case_	,  token_type_ids=snake_case_	)[0]
										lowerCamelCase__       =       model(snake_case_	)[0]

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       DebertaVaForMaskedLM(config=snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       model(snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	,  labels=snake_case_	)

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





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       self.num_labels
										lowerCamelCase__       =       DebertaVaForSequenceClassification(snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       model(snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	,  labels=snake_case_	)
										self.parent.assertListEqual(list(result.logits.size()	)	,  [self.batch_size, self.num_labels]	)
										self.check_loss_output(snake_case_	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       self.num_labels
										lowerCamelCase__       =       DebertaVaForTokenClassification(config=snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       model(snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	,  labels=snake_case_	)
										self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length, self.num_labels)	)





					def 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       DebertaVaForQuestionAnswering(config=snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       model(
										    snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	,  start_positions=snake_case_	,  end_positions=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 					__magic_name__ ( self	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	,  _lowerCAmelCase	):
										lowerCamelCase__       =       DebertaVaForMultipleChoice(config=snake_case_	)
										model.to(snake_case_	)
										model.eval()
										lowerCamelCase__       =       input_ids.unsqueeze(1	).expand(-1	,  self.num_choices	,  -1	).contiguous()
										lowerCamelCase__       =       token_type_ids.unsqueeze(1	).expand(-1	,  self.num_choices	,  -1	).contiguous()
										lowerCamelCase__       =       input_mask.unsqueeze(1	).expand(-1	,  self.num_choices	,  -1	).contiguous()
										lowerCamelCase__       =       model(
										    snake_case_	,  attention_mask=snake_case_	,  token_type_ids=snake_case_	,  labels=snake_case_	,  )
										self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.num_choices)	)






					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.prepare_config_and_inputs()
										(
										    (
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,		(
										    lowerCamelCase__						
										)    ,								
										)       =       config_and_inputs
										lowerCamelCase__       =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
										return config, inputs_dict

@require_torch
class SCREAMING_SNAKE_CASE_ (					_a							,       _a							,       unittest.TestCase   ):





					"""simple docstring"""


					A__				 =    (
					    (
					        DebertaVaModel,
					        DebertaVaForMaskedLM,
					        DebertaVaForSequenceClassification,
					        DebertaVaForTokenClassification,
					        DebertaVaForQuestionAnswering,
					        DebertaVaForMultipleChoice,
					    )
					    if is_torch_available()
					    else ()
					)
					A__				 =    (
					    {
					        """feature-extraction""": DebertaVaModel,
					        """fill-mask""": DebertaVaForMaskedLM,
					        """question-answering""": DebertaVaForQuestionAnswering,
					        """text-classification""": DebertaVaForSequenceClassification,
					        """token-classification""": DebertaVaForTokenClassification,
					        """zero-shot""": DebertaVaForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)

					A__				 =    True
					A__				 =    False
					A__				 =    False
					A__				 =    False
					A__				 =    False





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       DebertaVaModelTester(self	)
										lowerCamelCase__       =       ConfigTester(self	,  config_class=snake_case_	,  hidden_size=37	)





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





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_model(*snake_case_	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_sequence_classification(*snake_case_	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_masked_lm(*snake_case_	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_question_answering(*snake_case_	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_token_classification(*snake_case_	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_deberta_for_multiple_choice(*snake_case_	)






					@slow
					def 					__magic_name__ ( self	):
										for model_name in DEBERTA_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															lowerCamelCase__       =       DebertaVaModel.from_pretrained(snake_case_	)
															self.assertIsNotNone(snake_case_	)

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





					"""simple docstring"""


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






					@slow
					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       DebertaVaModel.from_pretrained("microsoft/deberta-v2-xlarge"	)

										lowerCamelCase__       =       torch.tensor([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]	)
										lowerCamelCase__       =       torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
										with torch.no_grad():
															lowerCamelCase__       =       model(snake_case_	,  attention_mask=snake_case_	)[0]
										# compare the actual values for a slice.
										lowerCamelCase__       =       torch.tensor(
										    [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]	)
										self.assertTrue(torch.allclose(output[:, 1:4, 1:4]	,  snake_case_	,  atol=1E-4	)	,  F"{output[:, 1:4, 1:4]}"	)








import json
import os
import shutil
import tempfile
import unittest

import numpy as np

from transformers import BertTokenizerFast
from transformers.models.bert.tokenization_bert import VOCAB_FILES_NAMES, BertTokenizer
from transformers.testing_utils import require_tokenizers, require_vision
from transformers.utils import IMAGE_PROCESSOR_NAME, is_vision_available


if is_vision_available():
						from PIL import Image

						from transformers import VisionTextDualEncoderProcessor, ViTImageProcessor

@require_tokenizers
@require_vision
class SCREAMING_SNAKE_CASE_ (					unittest.TestCase   ):





					"""simple docstring"""


					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       tempfile.mkdtemp()

										# fmt: off
										lowerCamelCase__       =       ["[UNK]", "[CLS]", "[SEP]", "[PAD]", "[MASK]", "want", "##want", "##ed", "wa", "un", "runn", "##ing", ",", "low", "lowest"]
										# fmt: on
										lowerCamelCase__       =       os.path.join(self.tmpdirname	,  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]	)	)

										lowerCamelCase__       =       {
										    "do_resize": True,
										    "size": {"height": 18, "width": 18},
										    "do_normalize": True,
										    "image_mean": [0.5, 0.5, 0.5],
										    "image_std": [0.5, 0.5, 0.5],
										}
										lowerCamelCase__       =       os.path.join(self.tmpdirname	,  _lowerCAmelCase	)
										with open(self.image_processor_file	,  "w"	,  encoding="utf-8"	) as fp:
															json.dump(_lowerCAmelCase	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	,  **_lowerCAmelCase	):
										return BertTokenizer.from_pretrained(self.tmpdirname	,  **_lowerCAmelCase	)





					def 					__magic_name__ ( self	,  **_lowerCAmelCase	):
										return ViTImageProcessor.from_pretrained(self.tmpdirname	,  **_lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										shutil.rmtree(self.tmpdirname	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       [np.random.randint(255	,  size=(3, 30, 400)	,  dtype=np.uinta	)]

										lowerCamelCase__       =       [Image.fromarray(np.moveaxis(_lowerCAmelCase	,  0	,  -1	)	) for x in image_inputs]

										return image_inputs





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_tokenizer()
										lowerCamelCase__       =       self.get_image_processor()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										processor.save_pretrained(self.tmpdirname	)
										lowerCamelCase__       =       VisionTextDualEncoderProcessor.from_pretrained(self.tmpdirname	)

										self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer.get_vocab()	)
										self.assertIsInstance(processor.tokenizer	,  (BertTokenizer, BertTokenizerFast)	)

										self.assertEqual(processor.image_processor.to_json_string()	,  image_processor.to_json_string()	)
										self.assertIsInstance(processor.image_processor	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       VisionTextDualEncoderProcessor(
										    tokenizer=self.get_tokenizer()	,  image_processor=self.get_image_processor()	)
										processor.save_pretrained(self.tmpdirname	)

										lowerCamelCase__       =       self.get_tokenizer(bos_token="(BOS)"	,  eos_token="(EOS)"	)
										lowerCamelCase__       =       self.get_image_processor(do_normalize=_lowerCAmelCase	,  padding_value=1.0	)

										lowerCamelCase__       =       VisionTextDualEncoderProcessor.from_pretrained(
										    self.tmpdirname	,  bos_token="(BOS)"	,  eos_token="(EOS)"	,  do_normalize=_lowerCAmelCase	,  padding_value=1.0	)

										self.assertEqual(processor.tokenizer.get_vocab()	,  tokenizer_add_kwargs.get_vocab()	)
										self.assertIsInstance(processor.tokenizer	,  (BertTokenizer, BertTokenizerFast)	)

										self.assertEqual(processor.image_processor.to_json_string()	,  image_processor_add_kwargs.to_json_string()	)
										self.assertIsInstance(processor.image_processor	,  _lowerCAmelCase	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       self.prepare_image_inputs()

										lowerCamelCase__       =       image_processor(_lowerCAmelCase	,  return_tensors="np"	)
										lowerCamelCase__       =       processor(images=_lowerCAmelCase	,  return_tensors="np"	)

										for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()	,  input_processor[key].sum()	,  delta=1E-2	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       "lower newer"

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	)

										lowerCamelCase__       =       tokenizer(_lowerCAmelCase	)

										for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]	,  encoded_processor[key]	)





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       "lower newer"
										lowerCamelCase__       =       self.prepare_image_inputs()

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	,  images=_lowerCAmelCase	)

										self.assertListEqual(list(inputs.keys()	)	,  ["input_ids", "token_type_ids", "attention_mask", "pixel_values"]	)

										# test if it raises when no input is passed
										with self.assertRaises(_lowerCAmelCase	):
															processor()





					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

										lowerCamelCase__       =       processor.batch_decode(_lowerCAmelCase	)
										lowerCamelCase__       =       tokenizer.batch_decode(_lowerCAmelCase	)

										self.assertListEqual(_lowerCAmelCase	,  _lowerCAmelCase	)






					def 					__magic_name__ ( self	):
										lowerCamelCase__       =       self.get_image_processor()
										lowerCamelCase__       =       self.get_tokenizer()

										lowerCamelCase__       =       VisionTextDualEncoderProcessor(tokenizer=_lowerCAmelCase	,  image_processor=_lowerCAmelCase	)

										lowerCamelCase__       =       "lower newer"
										lowerCamelCase__       =       self.prepare_image_inputs()

										lowerCamelCase__       =       processor(text=_lowerCAmelCase	,  images=_lowerCAmelCase	)

										self.assertListEqual(list(inputs.keys()	)	,  processor.model_input_names	)

