


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 			__lowerCAmelCase				:


	'''simple docstring'''





	def __init__(	self:	List[Any],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Any=13,				lowerCamelCase_:	Optional[Any]=7,				lowerCamelCase_:	int=True,				lowerCamelCase_:	Tuple=True,				lowerCamelCase_:	Union[str, Any]=True,				lowerCamelCase_:	List[Any]=True,				lowerCamelCase_:	List[str]=99,				lowerCamelCase_:	Optional[int]=32,				lowerCamelCase_:	int=2,				lowerCamelCase_:	Dict=4,				lowerCamelCase_:	Union[str, Any]=37,				lowerCamelCase_:	int="gelu",				lowerCamelCase_:	Tuple=0.1,				lowerCamelCase_:	Optional[int]=0.1,				lowerCamelCase_:	List[Any]=512,				lowerCamelCase_:	str=16,				lowerCamelCase_:	str=2,				lowerCamelCase_:	Optional[Any]=0.0_2,				lowerCamelCase_:	str=3,				lowerCamelCase_:	List[str]=4,				lowerCamelCase_:	Optional[int]=None,				):
			lowercase__				:			int            =		parent
			lowercase__				:			int            =		13
			lowercase__				:			Optional[int]            =		7
			lowercase__				:			Optional[Any]            =		True
			lowercase__				:			Tuple            =		True
			lowercase__				:			str            =		True
			lowercase__				:			Union[str, Any]            =		True
			lowercase__				:			Optional[Any]            =		99
			lowercase__				:			int            =		32
			lowercase__				:			Any            =		2
			lowercase__				:			List[Any]            =		4
			lowercase__				:			Optional[int]            =		37
			lowercase__				:			List[Any]            =		"gelu"
			lowercase__				:			Dict            =		0.1
			lowercase__				:			Optional[Any]            =		0.1
			lowercase__				:			int            =		512
			lowercase__				:			Any            =		16
			lowercase__				:			int            =		False
			lowercase__				:			Dict            =		-1
			lowercase__				:			int            =		True
			lowercase__				:			int            =		2
			lowercase__				:			int            =		0.0_2
			lowercase__				:			Optional[int]            =		3
			lowercase__				:			Any            =		4
			lowercase__				:			List[Any]            =		None

	def       snake_case__(	self:	List[str] ):
			lowercase__				:			List[str]            =		ids_tensor([self.batch_size, self.seq_length],				self.vocab_size )

			lowercase__				:			Any            =		None
			if self.use_input_mask:
					lowercase__				:			str            =		random_attention_mask([self.batch_size, self.seq_length] )

			lowercase__				:			Any            =		None
			if self.use_token_type_ids:
					lowercase__				:			List[str]            =		ids_tensor([self.batch_size, self.seq_length],				self.type_vocab_size )

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

			lowercase__				:			int            =		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=lowerCAmelCase__,				)

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

	def       snake_case__(	self:	List[Any],				lowerCamelCase_:	List[str],				lowerCamelCase_:	Any,				lowerCamelCase_:	Dict,				lowerCamelCase_:	str,				lowerCamelCase_:	List[str],				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Optional[int] ):
			lowercase__				:			Optional[int]            =		TFDebertaModel(config=lowerCAmelCase__ )
			lowercase__				:			Any            =		{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

			lowercase__				:			Any            =		[input_ids, input_mask]
			lowercase__				:			Tuple            =		model(lowerCAmelCase__ )

			lowercase__				:			str            =		model(lowerCAmelCase__ )

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

	def       snake_case__(	self:	Optional[int],				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	Dict,				lowerCamelCase_:	str,				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Union[str, Any] ):
			lowercase__				:			Any            =		TFDebertaForMaskedLM(config=lowerCAmelCase__ )
			lowercase__				:			str            =		{
			    "input_ids": input_ids,
			    "attention_mask": input_mask,
			    "token_type_ids": token_type_ids,
			}
			lowercase__				:			Optional[int]            =		model(lowerCAmelCase__ )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.seq_length, self.vocab_size) )

	def       snake_case__(	self:	List[Any],				lowerCamelCase_:	Optional[int],				lowerCamelCase_:	int,				lowerCamelCase_:	Tuple,				lowerCamelCase_:	str,				lowerCamelCase_:	str,				lowerCamelCase_:	int,				lowerCamelCase_:	int ):
			lowercase__				:			List[Any]            =		self.num_labels
			lowercase__				:			List[Any]            =		TFDebertaForSequenceClassification(config=lowerCAmelCase__ )
			lowercase__				:			Union[str, Any]            =		{
			    "input_ids": input_ids,
			    "attention_mask": input_mask,
			    "token_type_ids": token_type_ids,
			}

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

	def       snake_case__(	self:	Optional[Any],				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	List[Any],				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	str,				lowerCamelCase_:	Tuple ):
			lowercase__				:			Any            =		self.num_labels
			lowercase__				:			Tuple            =		TFDebertaForTokenClassification(config=lowerCAmelCase__ )
			lowercase__				:			Optional[int]            =		{
			    "input_ids": input_ids,
			    "attention_mask": input_mask,
			    "token_type_ids": token_type_ids,
			}
			lowercase__				:			Optional[int]            =		model(lowerCAmelCase__ )
			self.parent.assertEqual(result.logits.shape,				(self.batch_size, self.seq_length, self.num_labels) )

	def       snake_case__(	self:	Optional[Any],				lowerCamelCase_:	Dict,				lowerCamelCase_:	Optional[Any],				lowerCamelCase_:	Union[str, Any],				lowerCamelCase_:	str,				lowerCamelCase_:	Dict,				lowerCamelCase_:	Dict,				lowerCamelCase_:	str ):
			lowercase__				:			Optional[int]            =		TFDebertaForQuestionAnswering(config=lowerCAmelCase__ )
			lowercase__				:			int            =		{
			    "input_ids": input_ids,
			    "attention_mask": input_mask,
			    "token_type_ids": token_type_ids,
			}

			lowercase__				:			Optional[int]            =		model(lowerCAmelCase__ )
			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:	Union[str, Any] ):
			lowercase__				:			Dict            =		self.prepare_config_and_inputs()
			(
			    lowercase__							
			)				:			int            =		config_and_inputs
			lowercase__				:			List[str]            =		{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
			return config, inputs_dict


@require_tf
class 			__lowerCAmelCase				(					UpperCamelCase__      ,						UpperCamelCase__      ,						unittest.TestCase			):


	'''simple docstring'''





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

	_A       	=							False
	_A       	=							False

	def       snake_case__(	self:	int ):
			lowercase__				:			int            =		TFDebertaModelTester(self )
			lowercase__				:			Tuple            =		ConfigTester(self,				config_class=lowerCAmelCase__,				hidden_size=37 )

	def       snake_case__(	self:	int ):
			self.config_tester.run_common_tests()

	def       snake_case__(	self:	Optional[Any] ):
			lowercase__				:			List[str]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_model(*lowerCAmelCase__ )

	def       snake_case__(	self:	Any ):
			lowercase__				:			Tuple            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_masked_lm(*lowerCAmelCase__ )

	def       snake_case__(	self:	Any ):
			lowercase__				:			Tuple            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_question_answering(*lowerCAmelCase__ )

	def       snake_case__(	self:	Dict ):
			lowercase__				:			int            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_sequence_classification(*lowerCAmelCase__ )

	def       snake_case__(	self:	List[Any] ):
			lowercase__				:			List[Any]            =		self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_for_token_classification(*lowerCAmelCase__ )





	@slow
	def       snake_case__(	self:	Union[str, Any] ):
			lowercase__				:			Any            =		TFDebertaModel.from_pretrained('kamalkraj/deberta-base' )
			self.assertIsNotNone(lowerCAmelCase__ )


@require_tf
class 			__lowerCAmelCase				(					unittest.TestCase			):


	'''simple docstring'''





	@unittest.skip(reason='Model not available yet' )
	def       snake_case__(	self:	int ):
			pass





	@slow
	def       snake_case__(	self:	Any ):
			lowercase__				:			List[Any]            =		TFDebertaModel.from_pretrained('kamalkraj/deberta-base' )
			lowercase__				:			Optional[int]            =		tf.constant([[0, 31414, 232, 328, 740, 1140, 12695, 69, 46078, 1588, 2]] )
			lowercase__				:			List[str]            =		tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
			lowercase__				:			Tuple            =		model(lowerCAmelCase__,				attention_mask=lowerCAmelCase__ )[0]

			lowercase__				:			Dict            =		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],				lowerCAmelCase__,				atol=1E-4 )









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 			__lowerCAmelCase				(					_UpperCamelCase      ,						_UpperCamelCase			):


	'''simple docstring'''





	def __init__(	self:	Union[str, Any] ):
			super().__init__()

			self.register_buffer('watermark_image',				torch.zeros((62, 62, 4) ) )
			lowercase__				:			int            =		None





	def       snake_case__(	self:	Optional[int],				lowerCamelCase_:	List[PIL.Image.Image],				lowerCamelCase_:	Dict=None ):
			# copied from https://github.com/deep-floyd/IF/blob/b77482e36ca2031cb94dbca1001fc1e6400bf4ab/deepfloyd_if/modules/base.py#L287

			lowercase__				:			str            =		images[0].height
			lowercase__				:			Optional[Any]            =		images[0].width

			lowercase__				:			List[Any]            =		sample_size or h

			lowercase__				:			Any            =		min(h / sample_size,				w / sample_size )
			lowercase__      ,							lowercase__				:			Optional[int]            =		(int(h / coef ), int(w / coef )) if coef < 1 else (h, w)

			lowercase__      ,							lowercase__				:			Any            =		1024**2, img_w * img_h
			lowercase__				:			Union[str, Any]            =		(Sa / Sa) ** 0.5
			lowercase__      ,							lowercase__      ,							lowercase__				:			Optional[int]            =		int(K * 62 ), img_w - int(14 * K ), img_h - int(14 * K )

			if self.watermark_image_as_pil is None:
					lowercase__				:			Tuple            =		self.watermark_image.to(torch.uinta ).cpu().numpy()
					lowercase__				:			str            =		Image.fromarray(lowerCamelCase_,				mode='RGBA' )
					lowercase__				:			Tuple            =		watermark_image

			lowercase__				:			Optional[int]            =		self.watermark_image_as_pil.resize(
			    (wm_size, wm_size),				PIL_INTERPOLATION['bicubic'],				reducing_gap=lowerCamelCase_ )

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

			return images


