





"""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      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=13      ,    __SCREAMING_SNAKE_CASE=7      ,    __SCREAMING_SNAKE_CASE=True      ,    __SCREAMING_SNAKE_CASE=True      ,    __SCREAMING_SNAKE_CASE=True      ,    __SCREAMING_SNAKE_CASE=True      ,    __SCREAMING_SNAKE_CASE=99      ,    __SCREAMING_SNAKE_CASE=32      ,    __SCREAMING_SNAKE_CASE=2      ,    __SCREAMING_SNAKE_CASE=4      ,    __SCREAMING_SNAKE_CASE=37      ,    __SCREAMING_SNAKE_CASE="gelu"      ,    __SCREAMING_SNAKE_CASE=0.1      ,    __SCREAMING_SNAKE_CASE=0.1      ,    __SCREAMING_SNAKE_CASE=512      ,    __SCREAMING_SNAKE_CASE=16      ,    __SCREAMING_SNAKE_CASE=2      ,    __SCREAMING_SNAKE_CASE=0.02      ,    __SCREAMING_SNAKE_CASE=3      ,    __SCREAMING_SNAKE_CASE=4      ,    __SCREAMING_SNAKE_CASE=None      ,    ):






        '''simple docstring'''

        snake_case__										= parent
        snake_case__										= 13
        snake_case__										= 7
        snake_case__										= True
        snake_case__										= True
        snake_case__										= True
        snake_case__										= True
        snake_case__										= 99
        snake_case__										= 32
        snake_case__										= 2
        snake_case__										= 4
        snake_case__										= 37
        snake_case__										= "gelu"
        snake_case__										= 0.1
        snake_case__										= 0.1
        snake_case__										= 512
        snake_case__										= 16
        snake_case__										= False
        snake_case__										= -1
        snake_case__										= True
        snake_case__										= 2
        snake_case__										= 0.02
        snake_case__										= 3
        snake_case__										= 4
        snake_case__										= None

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= ids_tensor([self.batch_size, self.seq_length]      ,    self.vocab_size   )

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

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

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

        snake_case__										= 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=__SCREAMING_SNAKE_CASE      ,    )

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

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        snake_case__										= TFDebertaModel(config=__SCREAMING_SNAKE_CASE   )
        snake_case__										= {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

        snake_case__										= [input_ids, input_mask]
        snake_case__										= model(__SCREAMING_SNAKE_CASE   )

        snake_case__										= model(__SCREAMING_SNAKE_CASE   )

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

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        snake_case__										= TFDebertaForMaskedLM(config=__SCREAMING_SNAKE_CASE   )
        snake_case__										= {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        snake_case__										= model(__SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.seq_length, self.vocab_size)   )

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        snake_case__										= self.num_labels
        snake_case__										= TFDebertaForSequenceClassification(config=__SCREAMING_SNAKE_CASE   )
        snake_case__										= {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        snake_case__										= model(__SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)   )

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        snake_case__										= self.num_labels
        snake_case__										= TFDebertaForTokenClassification(config=__SCREAMING_SNAKE_CASE   )
        snake_case__										= {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }
        snake_case__										= model(__SCREAMING_SNAKE_CASE   )
        self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.seq_length, self.num_labels)   )

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE   ):






        '''simple docstring'''

        snake_case__										= TFDebertaForQuestionAnswering(config=__SCREAMING_SNAKE_CASE   )
        snake_case__										= {
            "input_ids": input_ids,
            "attention_mask": input_mask,
            "token_type_ids": token_type_ids,
        }

        snake_case__										= model(__SCREAMING_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 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

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



@require_tf
class    lowercase_ (   __lowerCAmelCase ,__lowerCAmelCase ,unittest.TestCase							):
       snake_case__ :							Tuple 							=   (
           (
               TFDebertaModel,
               TFDebertaForMaskedLM,
               TFDebertaForQuestionAnswering,
               TFDebertaForSequenceClassification,
               TFDebertaForTokenClassification,
           )
           if is_tf_available()
           else ()
       )
       snake_case__ :							Any 							=   (
           {
               """feature-extraction""": TFDebertaModel,
               """fill-mask""": TFDebertaForMaskedLM,
               """question-answering""": TFDebertaForQuestionAnswering,
               """text-classification""": TFDebertaForSequenceClassification,
               """token-classification""": TFDebertaForTokenClassification,
               """zero-shot""": TFDebertaForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )

       snake_case__ :							int 							=   False
       snake_case__ :							List[Any] 							=   False

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= TFDebertaModelTester(self   )
        snake_case__										= ConfigTester(self      ,    config_class=__SCREAMING_SNAKE_CASE      ,    hidden_size=37   )

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        self.config_tester.run_common_tests()

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE   )

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*__SCREAMING_SNAKE_CASE   )

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*__SCREAMING_SNAKE_CASE   )

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*__SCREAMING_SNAKE_CASE   )

       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*__SCREAMING_SNAKE_CASE   )

       @slow
       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= TFDebertaModel.from_pretrained("kamalkraj/deberta-base"   )
        self.assertIsNotNone(__SCREAMING_SNAKE_CASE   )



@require_tf
class    lowercase_ (   unittest.TestCase							):

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






        '''simple docstring'''

        pass

       @slow
       def 						_SCREAMING_SNAKE_CASE(	self   ):






        '''simple docstring'''

        snake_case__										= TFDebertaModel.from_pretrained("kamalkraj/deberta-base"   )
        snake_case__										= tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]   )
        snake_case__										= tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]   )
        snake_case__										= model(__SCREAMING_SNAKE_CASE      ,    attention_mask=__SCREAMING_SNAKE_CASE   )[0]

        snake_case__										= tf.constant(
            [
                [
                    [-0.59855896, -0.80552566, -0.8462135],
                    [1.4484025, -0.93483794, -0.80593085],
                    [0.3122741, 0.00316059, -1.4131377],
                ]
            ]   )
        tf.debugging.assert_near(output[:, 1:4, 1:4]      ,    __SCREAMING_SNAKE_CASE      ,    atol=1e-4   )








"""simple docstring"""


from typing import List

import PIL
import torch
from PIL import Image

from ...configuration_utils import ConfigMixin
from ...models.modeling_utils import ModelMixin
from ...utils import PIL_INTERPOLATION



class    lowercase_ (   __lowerCAmelCase ,__lowerCAmelCase							):

       def __init__(	self   ):






        '''simple docstring'''

        super().__init__()

        self.register_buffer("watermark_image"      ,    torch.zeros((62, 62, 4)   )   )
        snake_case__										= None

       def 						_SCREAMING_SNAKE_CASE(	self      ,    __SCREAMING_SNAKE_CASE      ,    __SCREAMING_SNAKE_CASE=None   ):






        '''simple docstring'''

        snake_case__										= images[0].height
        snake_case__										= images[0].width

        snake_case__										= sample_size or h

        snake_case__										= min(h / sample_size      ,    w / sample_size   )
        snake_case__							,      snake_case__										= (int(h / coef   ), int(w / coef   )) if coef < 1 else (h, w)

        snake_case__							,      snake_case__										= 1024**2, img_w * img_h
        snake_case__										= (Sa / Sa) ** 0.5
        snake_case__							,      snake_case__							,      snake_case__										= int(K * 62   ), img_w - int(14 * K   ), img_h - int(14 * K   )

        if self.watermark_image_as_pil is None:
         snake_case__										= self.watermark_image.to(torch.uinta   ).cpu().numpy()
         snake_case__										= Image.fromarray(__SCREAMING_SNAKE_CASE      ,    mode="RGBA"   )
         snake_case__										= watermark_image

        snake_case__										= self.watermark_image_as_pil.resize(
            (wm_size, wm_size)      ,    PIL_INTERPOLATION["bicubic"]      ,    reducing_gap=__SCREAMING_SNAKE_CASE   )

        for pil_img in images:
         pil_img.paste(__SCREAMING_SNAKE_CASE      ,    box=(wm_x - wm_size, wm_y - wm_size, wm_x, wm_y)      ,    mask=wm_img.split()[-1]   )

        return images

