






'''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    _snake_case   :

					"""simple docstring"""





					def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__=13   , UpperCAmelCase__=7   , UpperCAmelCase__=True   , UpperCAmelCase__=True   , UpperCAmelCase__=True   , UpperCAmelCase__=True   , UpperCAmelCase__=99   , UpperCAmelCase__=32   , UpperCAmelCase__=2   , UpperCAmelCase__=4   , UpperCAmelCase__=37   , UpperCAmelCase__="gelu"   , UpperCAmelCase__=0.1   , UpperCAmelCase__=0.1   , UpperCAmelCase__=512   , UpperCAmelCase__=16   , UpperCAmelCase__=2   , UpperCAmelCase__=0.0_2   , UpperCAmelCase__=3   , UpperCAmelCase__=4   , UpperCAmelCase__=None   , )      ->	Tuple:
										a_									=   parent
										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_									=   512
										a_									=   16
										a_									=   False
										a_									=   -1
										a_									=   True
										a_									=   2
										a_									=   0.0_2
										a_									=   3
										a_									=   4
										a_									=   None




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	str:
										a_									=   ids_tensor([self.batch_size, self.seq_length]   , self.vocab_size     )

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

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

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

										a_									=   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=UpperCAmelCase__   , )

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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Optional[Any]:
										a_									=   TFDebertaModel(config=UpperCAmelCase__     )
										a_									=   {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

										a_									=   [input_ids, input_mask]
										a_									=   model(UpperCAmelCase__     )

										a_									=   model(UpperCAmelCase__     )

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




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	str:
										a_									=   TFDebertaForMaskedLM(config=UpperCAmelCase__     )
										a_									=   {
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}
										a_									=   model(UpperCAmelCase__     )
										self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.vocab_size)     )




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Union[str, Any]:
										a_									=   self.num_labels
										a_									=   TFDebertaForSequenceClassification(config=UpperCAmelCase__     )
										a_									=   {
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}

										a_									=   model(UpperCAmelCase__     )
										self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.num_labels)     )




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Optional[int]:
										a_									=   self.num_labels
										a_									=   TFDebertaForTokenClassification(config=UpperCAmelCase__     )
										a_									=   {
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}
										a_									=   model(UpperCAmelCase__     )
										self.parent.assertEqual(result.logits.shape   , (self.batch_size, self.seq_length, self.num_labels)     )




					def  __SCREAMING_SNAKE_CASE (     self   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__   , UpperCAmelCase__     )      ->	Union[str, Any]:
										a_									=   TFDebertaForQuestionAnswering(config=UpperCAmelCase__     )
										a_									=   {
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										}

										a_									=   model(UpperCAmelCase__     )
										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     )      ->	List[Any]:
										a_									=   self.prepare_config_and_inputs()
										(
										    (
										    a_
										)							,					(
										    a_
										)							,					(
										    a_
										)							,					(
										    a_
										)							,					(
										    a_
										)							,					(
										    a_
										)							,					(
										    a_
										)							,					
										)									=   config_and_inputs
										a_									=   {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict



@require_tf
class    _snake_case   (				snake_case     ,					snake_case     ,					unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      (
					    (
					        TFDebertaModel,
					        TFDebertaForMaskedLM,
					        TFDebertaForQuestionAnswering,
					        TFDebertaForSequenceClassification,
					        TFDebertaForTokenClassification,
					    )
					    if is_tf_available()
					    else ()
					)
					_UpperCamelCase		 =      (
					    {
					        "feature-extraction": TFDebertaModel,
					        "fill-mask": TFDebertaForMaskedLM,
					        "question-answering": TFDebertaForQuestionAnswering,
					        "text-classification": TFDebertaForSequenceClassification,
					        "token-classification": TFDebertaForTokenClassification,
					        "zero-shot": TFDebertaForSequenceClassification,
					    }
					    if is_tf_available()
					    else {}
					)

					_UpperCamelCase		 =      False
					_UpperCamelCase		 =      False




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										a_									=   TFDebertaModelTester(self     )
										a_									=   ConfigTester(self   , config_class=UpperCAmelCase__   , hidden_size=37     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										self.config_tester.run_common_tests()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[int]:
										a_									=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										a_									=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[int]:
										a_									=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										a_									=   self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__     )




					@slow
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	str:
										a_									=   TFDebertaModel.from_pretrained('kamalkraj/deberta-base'     )
										self.assertIsNotNone(UpperCAmelCase__     )



@require_tf
class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					@unittest.skip(reason='Model not available yet'     )
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										pass




					@slow
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	str:
										a_									=   TFDebertaModel.from_pretrained('kamalkraj/deberta-base'     )
										a_									=   tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]     )
										a_									=   tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
										a_									=   model(UpperCAmelCase__   , attention_mask=UpperCAmelCase__     )[0]

										a_									=   tf.constant(
										    [
										        [
										            [-0.5_9_8_5_5_8_9_6, -0.8_0_5_5_2_5_6_6, -0.8_4_6_2_1_3_5],
										            [1.4_4_8_4_0_2_5, -0.9_3_4_8_3_7_9_4, -0.8_0_5_9_3_0_8_5],
										            [0.3_1_2_2_7_4_1, 0.0_0_3_1_6_0_5_9, -1.4_1_3_1_3_7_7],
										        ]
										    ]     )
										tf.debugging.assert_near(output[:, 1:4, 1:4]   , UpperCAmelCase__   , atol=1e-4     )









'''simple docstring'''
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import ConvNextImageProcessor


class    _snake_case   (				unittest.TestCase					):

					"""simple docstring"""





					def __init__(     self   , UpperCAmelCase__   , UpperCAmelCase__=7   , UpperCAmelCase__=3   , UpperCAmelCase__=18   , UpperCAmelCase__=30   , UpperCAmelCase__=400   , UpperCAmelCase__=True   , UpperCAmelCase__=None   , UpperCAmelCase__=0.8_7_5   , UpperCAmelCase__=True   , UpperCAmelCase__=[0.5, 0.5, 0.5]   , UpperCAmelCase__=[0.5, 0.5, 0.5]   , )      ->	Optional[int]:
										a_									=   size if size is not None else {'shortest_edge': 20}
										a_									=   parent
										a_									=   batch_size
										a_									=   num_channels
										a_									=   image_size
										a_									=   min_resolution
										a_									=   max_resolution
										a_									=   do_resize
										a_									=   size
										a_									=   crop_pct
										a_									=   do_normalize
										a_									=   image_mean
										a_									=   image_std




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	List[str]:
										return {
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										    "do_normalize": self.do_normalize,
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "crop_pct": self.crop_pct,
										}



@require_torch
@require_vision
class    _snake_case   (				snake_case     ,					unittest.TestCase					):

					"""simple docstring"""





					_UpperCamelCase		 =      ConvNextImageProcessor if is_vision_available() else None




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   ConvNextImageProcessingTester(self     )




					@property
					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Dict:
										return self.image_processor_tester.prepare_image_processor_dict()




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Any:
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_resize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'size'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'crop_pct'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'do_normalize'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_mean'     )     )
										self.assertTrue(hasattr(UpperCAmelCase__   , 'image_std'     )     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										a_									=   self.image_processing_class.from_dict(self.image_processor_dict     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 20}     )

										a_									=   self.image_processing_class.from_dict(self.image_processor_dict   , size=42     )
										self.assertEqual(image_processor.size   , {'shortest_edge': 42}     )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Optional[Any]:
										pass




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	Tuple:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PIL images
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , Image.Image     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random numpy tensors
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__   , numpify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , np.ndarray     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )




					def  __SCREAMING_SNAKE_CASE (     self     )      ->	int:
										# Initialize image_processing
										a_									=   self.image_processing_class(**self.image_processor_dict     )
										# create random PyTorch tensors
										a_									=   prepare_image_inputs(self.image_processor_tester   , equal_resolution=UpperCAmelCase__   , torchify=UpperCAmelCase__     )
										for image in image_inputs:
															self.assertIsInstance(UpperCAmelCase__   , torch.Tensor     )

										# Test not batched input
										a_									=   image_processing(image_inputs[0]   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

										# Test batched
										a_									=   image_processing(UpperCAmelCase__   , return_tensors='pt'     ).pixel_values
										self.assertEqual(
										    encoded_images.shape   , (
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['shortest_edge'],
										        self.image_processor_tester.size['shortest_edge'],
										    )   , )

