


"""simple docstring"""


from __future__ import annotations

import unittest

from transformers import DebertaVaConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers import (
	    TFDebertaVaForMaskedLM,
	    TFDebertaVaForQuestionAnswering,
	    TFDebertaVaForSequenceClassification,
	    TFDebertaVaForTokenClassification,
	    TFDebertaVaModel,
	)
class     _UpperCamelCase    :
							'''simple docstring'''







							def __init__(	self       ,  __a       ,  __a=13       ,  __a=7       ,  __a=True       ,  __a=True       ,  __a=True       ,  __a=True       ,  __a=99       ,  __a=32       ,  __a=2       ,  __a=4       ,  __a=37       ,  __a="gelu"       ,  __a=0.1       ,  __a=0.1       ,  __a=5_12       ,  __a=16       ,  __a=2       ,  __a=0.0_2       ,  __a=False       ,  __a=True       ,  __a="None"       ,  __a=3       ,  __a=4       ,  __a=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      snake_case							(	self      ):
										__lowerCAmelCase							       =     ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size      )

										__lowerCAmelCase							       =     None
										if self.use_input_mask:
													__lowerCAmelCase							       =     random_attention_mask([self.batch_size, self.seq_length]      )

										__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							       =     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       ,  relative_attention=self.relative_attention       ,  position_biased_input=self.position_biased_input       ,  initializer_range=self.initializer_range       ,  return_dict=__a       ,  )

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

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     TFDebertaVaModel(config=__a      )
										__lowerCAmelCase							       =     {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

										__lowerCAmelCase							       =     [input_ids, input_mask]
										__lowerCAmelCase							       =     model(__a      )

										__lowerCAmelCase							       =     model(__a      )

										self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     TFDebertaVaForMaskedLM(config=__a      )
										__lowerCAmelCase							       =     {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}
										__lowerCAmelCase							       =     model(__a      )
										self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     self.num_labels
										__lowerCAmelCase							       =     TFDebertaVaForSequenceClassification(config=__a      )
										__lowerCAmelCase							       =     {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

										__lowerCAmelCase							       =     model(__a      )
										self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.num_labels)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     self.num_labels
										__lowerCAmelCase							       =     TFDebertaVaForTokenClassification(config=__a      )
										__lowerCAmelCase							       =     {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}
										__lowerCAmelCase							       =     model(__a      )
										self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.num_labels)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     TFDebertaVaForQuestionAnswering(config=__a      )
										__lowerCAmelCase							       =     {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

										__lowerCAmelCase							       =     model(__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      snake_case							(	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_tf
class     _UpperCamelCase    (  lowerCAmelCase__			,lowerCAmelCase__			,unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       Tuple							 =(
							    (
							        TFDebertaVaModel,
							        TFDebertaVaForMaskedLM,
							        TFDebertaVaForQuestionAnswering,
							        TFDebertaVaForSequenceClassification,
							        TFDebertaVaForTokenClassification,
							    )
							    if is_tf_available()
							    else ()
							)
							__UpperCAmelCase		:       str							 =(
							    {
							        """feature-extraction""": TFDebertaVaModel,
							        """fill-mask""": TFDebertaVaForMaskedLM,
							        """question-answering""": TFDebertaVaForQuestionAnswering,
							        """text-classification""": TFDebertaVaForSequenceClassification,
							        """token-classification""": TFDebertaVaForTokenClassification,
							        """zero-shot""": TFDebertaVaForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)

							__UpperCAmelCase		:       Optional[int]							 =False
							__UpperCAmelCase		:       int							 =False

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     TFDebertaVaModelTester(self      )
										__lowerCAmelCase							       =     ConfigTester(self       ,  config_class=__a       ,  hidden_size=37      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*__a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*__a      )



							@slow
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"      )
										self.assertIsNotNone(__a      )
@require_tf
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







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



							@slow
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"      )
										__lowerCAmelCase							       =     tf.constant([[0, 3_14_14, 2_32, 3_28, 7_40, 11_40, 1_26_95, 69, 4_60_78, 15_88, 2]]      )
										__lowerCAmelCase							       =     tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]      )
										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a      )[0]

										__lowerCAmelCase							       =     tf.constant(
										    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]      )
										tf.debugging.assert_near(output[:, 1:4, 1:4]       ,  __a       ,  atol=1e-4      )








"""simple docstring"""


import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:
	class     _UpperCamelCase    :
								'''simple docstring'''







								@staticmethod
								def      snake_case							(	*__a       ,  **__a      ):
											pass
@is_pipeline_test
@require_torch
@require_vision
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     [
										    {
										        "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      ),
										        "question": "How many cats are there?",
										    },
										    {
										        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
										        "question": "How many cats are there?",
										    },
										]
										return vqa_pipeline, examples

							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     vqa_pipeline(__a       ,  top_k=1      )
										self.assertEqual(
										    __a       ,  [
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										    ]       ,  )

							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question="How many cats are there?"       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

							@slow
							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="dandelin/vilt-b32-finetuned-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question=__a       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline(
										    [{"image": image, "question": question}, {"image": image, "question": question}]       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]] * 2       ,  )



							@require_tf
							@unittest.skip("Visual question answering not implemented in TF"      )
							def      snake_case							(	self      ):
										pass




