




'''simple docstring'''





from __future__ import annotations

import unittest

from transformers import DebertaConfig, 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 (
           TFDebertaForMaskedLM,
           TFDebertaForQuestionAnswering,
           TFDebertaForSequenceClassification,
           TFDebertaForTokenClassification,
           TFDebertaModel,
       )






class     lowercase  :






      def __init__(self			,	a__			,	a__=1_3			,	a__=7			,	a__=True			,	a__=True			,	a__=True			,	a__=True			,	a__=9_9			,	a__=3_2			,	a__=2			,	a__=4			,	a__=3_7			,	a__="gelu"			,	a__=0.1			,	a__=0.1			,	a__=5_1_2			,	a__=1_6			,	a__=2			,	a__=0.02			,	a__=3			,	a__=4			,	a__=None			,	)							-> Any:
         SCREAMING_SNAKE_CASE__      :					int							    =			parent
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			1_3
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			7
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			True
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			True
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			True
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			True
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			9_9
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			3_2
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			2
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			4
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			3_7
         SCREAMING_SNAKE_CASE__      :					Dict							    =			'''gelu'''
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			0.1
         SCREAMING_SNAKE_CASE__      :					Dict							    =			0.1
         SCREAMING_SNAKE_CASE__      :					Any							    =			5_1_2
         SCREAMING_SNAKE_CASE__      :					Any							    =			1_6
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			False
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			-1
         SCREAMING_SNAKE_CASE__      :					int							    =			True
         SCREAMING_SNAKE_CASE__      :					int							    =			2
         SCREAMING_SNAKE_CASE__      :					Any							    =			0.02
         SCREAMING_SNAKE_CASE__      :					Any							    =			3
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			4
         SCREAMING_SNAKE_CASE__      :					int							    =			None






      def  A__(self       )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			ids_tensor([self.batch_size, self.seq_length]			,	self.vocab_size       )

         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			None
         if self.use_input_mask:
            SCREAMING_SNAKE_CASE__      :					Tuple							    =			random_attention_mask([self.batch_size, self.seq_length]       )

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

         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			None
         SCREAMING_SNAKE_CASE__      :					Any							    =			None
         SCREAMING_SNAKE_CASE__      :					Any							    =			None
         if self.use_labels:
            SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			ids_tensor([self.batch_size]			,	self.type_sequence_label_size       )
            SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			ids_tensor([self.batch_size, self.seq_length]			,	self.num_labels       )

         SCREAMING_SNAKE_CASE__      :					Tuple							    =			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			,	relative_attention=self.relative_attention			,	max_relative_positions=self.max_relative_positions			,	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  A__(self			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__       )							-> Any:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			TFDebertaModel(config=a__       )
         SCREAMING_SNAKE_CASE__      :					Any							    =			{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}

         SCREAMING_SNAKE_CASE__      :					Dict							    =			[input_ids, input_mask]
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			model(a__       )

         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			model(a__       )

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






      def  A__(self			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__       )							-> Tuple:
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			TFDebertaForMaskedLM(config=a__       )
         SCREAMING_SNAKE_CASE__      :					Any							    =			{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			model(a__       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.vocab_size)       )






      def  A__(self			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__       )							-> int:
         SCREAMING_SNAKE_CASE__      :					str							    =			self.num_labels
         SCREAMING_SNAKE_CASE__      :					Dict							    =			TFDebertaForSequenceClassification(config=a__       )
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }

         SCREAMING_SNAKE_CASE__      :					str							    =			model(a__       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.num_labels)       )






      def  A__(self			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			self.num_labels
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			TFDebertaForTokenClassification(config=a__       )
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }
         SCREAMING_SNAKE_CASE__      :					Dict							    =			model(a__       )
         self.parent.assertEqual(result.logits.shape			,	(self.batch_size, self.seq_length, self.num_labels)       )






      def  A__(self			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__			,	a__       )							-> List[str]:
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			TFDebertaForQuestionAnswering(config=a__       )
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			{
             '''input_ids''': input_ids,
             '''attention_mask''': input_mask,
             '''token_type_ids''': token_type_ids,
         }

         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			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  A__(self       )							-> Any:
         SCREAMING_SNAKE_CASE__      :					Any							    =			self.prepare_config_and_inputs()
         (
             (
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					(
             SCREAMING_SNAKE_CASE__
         )    ,					
         )      :					Union[str, Any]							    =			config_and_inputs
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
         return config, inputs_dict






@require_tf
class     lowercase  ( UpperCAmelCase_					,      UpperCAmelCase_					,      unittest.TestCase):
      __lowercase					=	(
          (
              TFDebertaModel,
              TFDebertaForMaskedLM,
              TFDebertaForQuestionAnswering,
              TFDebertaForSequenceClassification,
              TFDebertaForTokenClassification,
          )
          if is_tf_available()
          else ()
      )
      __lowercase					=	(
          {
              '''feature-extraction''': TFDebertaModel,
              '''fill-mask''': TFDebertaForMaskedLM,
              '''question-answering''': TFDebertaForQuestionAnswering,
              '''text-classification''': TFDebertaForSequenceClassification,
              '''token-classification''': TFDebertaForTokenClassification,
              '''zero-shot''': TFDebertaForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )

      __lowercase					=	False
      __lowercase					=	False






      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			TFDebertaModelTester(self       )
         SCREAMING_SNAKE_CASE__      :					Any							    =			ConfigTester(self			,	config_class=a__			,	hidden_size=3_7       )






      def  A__(self       )							-> Tuple:
         self.config_tester.run_common_tests()






      def  A__(self       )							-> List[Any]:
         SCREAMING_SNAKE_CASE__      :					Any							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*a__       )






      def  A__(self       )							-> Any:
         SCREAMING_SNAKE_CASE__      :					str							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*a__       )






      def  A__(self       )							-> List[str]:
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_question_answering(*a__       )






      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					str							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_sequence_classification(*a__       )






      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Any							    =			self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_token_classification(*a__       )






      @slow
      def  A__(self       )							-> int:
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''       )
         self.assertIsNotNone(a__       )






@require_tf
class     lowercase  ( unittest.TestCase):






      @unittest.skip(reason='''Model not available yet'''       )
      def  A__(self       )							-> Optional[int]:
         pass






      @slow
      def  A__(self       )							-> str:
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			TFDebertaModel.from_pretrained('''kamalkraj/deberta-base'''       )
         SCREAMING_SNAKE_CASE__      :					int							    =			tf.constant([[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]]       )
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			model(a__			,	attention_mask=a__       )[0]

         SCREAMING_SNAKE_CASE__      :					Any							    =			tf.constant(
             [
                 [
                     [-0.59_85_58_96, -0.80_55_25_66, -0.8_46_21_35],
                     [1.4_48_40_25, -0.93_48_37_94, -0.80_59_30_85],
                     [0.3_12_27_41, 0.00_31_60_59, -1.4_13_13_77],
                 ]
             ]       )
         tf.debugging.assert_near(output[:, 1:4, 1:4]			,	a__			,	atol=1E-4       )







'''simple docstring'''





import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
       from PIL import Image
else:






       class     lowercase  :






             @staticmethod
             def  A__(*a__			,	**a__       )							-> Union[str, Any]:
                pass






@is_pipeline_test
@require_torch_or_tf
@require_vision
class     lowercase  ( unittest.TestCase):
      __lowercase					=	MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
      __lowercase					=	TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING






      def  A__(self			,	a__			,	a__			,	a__       )							-> Dict:
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			ImageClassificationPipeline(model=a__			,	image_processor=a__			,	top_k=2       )
         SCREAMING_SNAKE_CASE__      :					List[Any]							    =			[
             Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       ),
             '''http://images.cocodataset.org/val2017/000000039769.jpg''',
         ]
         return image_classifier, examples






      def  A__(self			,	a__			,	a__       )							-> List[Any]:
         SCREAMING_SNAKE_CASE__      :					int							    =			image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )

         self.assertEqual(
             a__			,	[
                 {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
             ]			,	)

         import datasets

         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''			,	'''image'''			,	split='''test'''       )

         # Accepts URL + PIL.Image + lists
         SCREAMING_SNAKE_CASE__      :					Tuple							    =			image_classifier(
             [
                 Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       ),
                 '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                 # RGBA
                 dataset[0]['''file'''],
                 # LA
                 dataset[1]['''file'''],
                 # L
                 dataset[2]['''file'''],
             ]       )
         self.assertEqual(
             a__			,	[
                 [
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 ],
                 [
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 ],
                 [
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 ],
                 [
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 ],
                 [
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                     {'''score''': ANY(a__       ), '''label''': ANY(a__       )},
                 ],
             ]			,	)






      @require_torch
      def  A__(self       )							-> List[str]:
         SCREAMING_SNAKE_CASE__      :					List[str]							    =			'''hf-internal-testing/tiny-random-vit'''
         SCREAMING_SNAKE_CASE__      :					Any							    =			pipeline('''image-classification'''			,	model=a__       )

         SCREAMING_SNAKE_CASE__      :					Any							    =			image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}]			,	)

         SCREAMING_SNAKE_CASE__      :					Any							    =			image_classifier(
             [
                 '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                 '''http://images.cocodataset.org/val2017/000000039769.jpg''',
             ]			,	top_k=2			,	)
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[
                 [{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}],
                 [{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}],
             ]			,	)






      @require_tf
      def  A__(self       )							-> Optional[int]:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			'''hf-internal-testing/tiny-random-vit'''
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			pipeline('''image-classification'''			,	model=a__			,	framework='''tf'''       )

         SCREAMING_SNAKE_CASE__      :					List[str]							    =			image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}]			,	)

         SCREAMING_SNAKE_CASE__      :					Dict							    =			image_classifier(
             [
                 '''http://images.cocodataset.org/val2017/000000039769.jpg''',
                 '''http://images.cocodataset.org/val2017/000000039769.jpg''',
             ]			,	top_k=2			,	)
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[
                 [{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}],
                 [{'''label''': '''LABEL_1''', '''score''': 0.5_74}, {'''label''': '''LABEL_0''', '''score''': 0.4_26}],
             ]			,	)






      def  A__(self       )							-> Union[str, Any]:
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			PreTrainedTokenizer()

         # Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			pipeline(
             '''image-classification'''			,	model='''hf-internal-testing/tiny-random-vit'''			,	tokenizer=a__       )

         self.assertIs(image_classifier.tokenizer			,	a__       )






      @slow
      @require_torch
      def  A__(self       )							-> int:
         # Perceiver is not tested by `run_pipeline_test` properly.
         # That is because the type of feature_extractor and model preprocessor need to be kept
         # in sync, which is not the case in the current design
         SCREAMING_SNAKE_CASE__      :					Union[str, Any]							    =			pipeline('''image-classification'''			,	model='''deepmind/vision-perceiver-conv'''       )
         SCREAMING_SNAKE_CASE__      :					str							    =			image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[
                 {'''score''': 0.43_85, '''label''': '''tabby, tabby cat'''},
                 {'''score''': 0.3_21, '''label''': '''tiger cat'''},
                 {'''score''': 0.05_02, '''label''': '''Egyptian cat'''},
                 {'''score''': 0.01_37, '''label''': '''crib, cot'''},
                 {'''score''': 0.0_07, '''label''': '''radiator'''},
             ]			,	)

         SCREAMING_SNAKE_CASE__      :					str							    =			pipeline('''image-classification'''			,	model='''deepmind/vision-perceiver-fourier'''       )
         SCREAMING_SNAKE_CASE__      :					Optional[Any]							    =			image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[
                 {'''score''': 0.56_58, '''label''': '''tabby, tabby cat'''},
                 {'''score''': 0.13_09, '''label''': '''tiger cat'''},
                 {'''score''': 0.07_22, '''label''': '''Egyptian cat'''},
                 {'''score''': 0.07_07, '''label''': '''remote control, remote'''},
                 {'''score''': 0.00_82, '''label''': '''computer keyboard, keypad'''},
             ]			,	)

         SCREAMING_SNAKE_CASE__      :					Dict							    =			pipeline('''image-classification'''			,	model='''deepmind/vision-perceiver-learned'''       )
         SCREAMING_SNAKE_CASE__      :					Optional[int]							    =			image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''       )
         self.assertEqual(
             nested_simplify(a__			,	decimals=4       )			,	[
                 {'''score''': 0.30_22, '''label''': '''tabby, tabby cat'''},
                 {'''score''': 0.23_62, '''label''': '''Egyptian cat'''},
                 {'''score''': 0.18_56, '''label''': '''tiger cat'''},
                 {'''score''': 0.03_24, '''label''': '''remote control, remote'''},
                 {'''score''': 0.00_96, '''label''': '''quilt, comforter, comfort, puff'''},
             ]			,	)

