


"""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 			lowerCamelCase_		:





				"""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.02       ,		UpperCAmelCase__=False       ,		UpperCAmelCase__=True       ,		UpperCAmelCase__="None"       ,		UpperCAmelCase__=3       ,		UpperCAmelCase__=4       ,		UpperCAmelCase__=None       ,		):
								SCREAMING_SNAKE_CASE__     							=				parent
								SCREAMING_SNAKE_CASE__     							=				batch_size
								SCREAMING_SNAKE_CASE__     							=				seq_length
								SCREAMING_SNAKE_CASE__     							=				is_training
								SCREAMING_SNAKE_CASE__     							=				use_input_mask
								SCREAMING_SNAKE_CASE__     							=				use_token_type_ids
								SCREAMING_SNAKE_CASE__     							=				use_labels
								SCREAMING_SNAKE_CASE__     							=				vocab_size
								SCREAMING_SNAKE_CASE__     							=				hidden_size
								SCREAMING_SNAKE_CASE__     							=				num_hidden_layers
								SCREAMING_SNAKE_CASE__     							=				num_attention_heads
								SCREAMING_SNAKE_CASE__     							=				intermediate_size
								SCREAMING_SNAKE_CASE__     							=				hidden_act
								SCREAMING_SNAKE_CASE__     							=				hidden_dropout_prob
								SCREAMING_SNAKE_CASE__     							=				attention_probs_dropout_prob
								SCREAMING_SNAKE_CASE__     							=				max_position_embeddings
								SCREAMING_SNAKE_CASE__     							=				type_vocab_size
								SCREAMING_SNAKE_CASE__     							=				type_sequence_label_size
								SCREAMING_SNAKE_CASE__     							=				initializer_range
								SCREAMING_SNAKE_CASE__     							=				num_labels
								SCREAMING_SNAKE_CASE__     							=				num_choices
								SCREAMING_SNAKE_CASE__     							=				relative_attention
								SCREAMING_SNAKE_CASE__     							=				position_biased_input
								SCREAMING_SNAKE_CASE__     							=				pos_att_type
								SCREAMING_SNAKE_CASE__     							=				scope



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				ids_tensor([self.batch_size, self.seq_length]       ,		self.vocab_size     )

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

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

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

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

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



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaModel(config=UpperCAmelCase__     )
								SCREAMING_SNAKE_CASE__     							=				{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

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

								SCREAMING_SNAKE_CASE__     							=				model(UpperCAmelCase__     )

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



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



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				self.num_labels
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaForSequenceClassification(config=UpperCAmelCase__     )
								SCREAMING_SNAKE_CASE__     							=				{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

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



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



				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__       ,		UpperCAmelCase__     ):
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaForQuestionAnswering(config=UpperCAmelCase__     )
								SCREAMING_SNAKE_CASE__     							=				{
								    "input_ids": input_ids,
								    "attention_mask": input_mask,
								    "token_type_ids": token_type_ids,
								}

								SCREAMING_SNAKE_CASE__     							=				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   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				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__      
								)							,            
								)     							=				config_and_inputs
								SCREAMING_SNAKE_CASE__     							=				{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
								return config, inputs_dict


@require_tf
class 			lowerCamelCase_		(			lowercase       ,  lowercase       ,  unittest.TestCase   ):





				"""simple docstring"""







				_lowerCAmelCase      :      List[Any]      							=					(
				    (
				        TFDebertaVaModel,
				        TFDebertaVaForMaskedLM,
				        TFDebertaVaForQuestionAnswering,
				        TFDebertaVaForSequenceClassification,
				        TFDebertaVaForTokenClassification,
				    )
				    if is_tf_available()
				    else ()
				)
				_lowerCAmelCase      :      List[Any]      							=					(
				    {
				        "feature-extraction": TFDebertaVaModel,
				        "fill-mask": TFDebertaVaForMaskedLM,
				        "question-answering": TFDebertaVaForQuestionAnswering,
				        "text-classification": TFDebertaVaForSequenceClassification,
				        "token-classification": TFDebertaVaForTokenClassification,
				        "zero-shot": TFDebertaVaForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)

				_lowerCAmelCase      :      Optional[int]      							=					False
				_lowerCAmelCase      :      Dict      							=					False



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaModelTester(self     )
								SCREAMING_SNAKE_CASE__     							=				ConfigTester(self       ,		config_class=UpperCAmelCase__       ,		hidden_size=37     )



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



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*UpperCAmelCase__     )



				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*UpperCAmelCase__     )






				@slow
				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"     )
								self.assertIsNotNone(UpperCAmelCase__     )


@require_tf
class 			lowerCamelCase_		(			unittest.TestCase   ):





				"""simple docstring"""







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






				@slow
				def   lowerCAmelCase__		(						self     ):
								SCREAMING_SNAKE_CASE__     							=				TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"     )
								SCREAMING_SNAKE_CASE__     							=				tf.constant([[0, 3_1414, 232, 328, 740, 1140, 1_2695, 69, 4_6078, 1588, 2]]     )
								SCREAMING_SNAKE_CASE__     							=				tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]     )
								SCREAMING_SNAKE_CASE__     							=				model(UpperCAmelCase__       ,		attention_mask=UpperCAmelCase__     )[0]

								SCREAMING_SNAKE_CASE__     							=				tf.constant(
								    [[[0.2_356, 0.1_948, 0.0_369], [-0.1_063, 0.3_586, -0.5_152], [-0.6_399, -0.0_259, -0.2_525]]]     )
								tf.debugging.assert_near(output[:, 1:4, 1:4]       ,		UpperCAmelCase__       ,		atol=1e-4     )








"""simple docstring"""






import numpy as np
import torch
from imwatermark import WatermarkEncoder


# Copied from https://github.com/Stability-AI/generative-models/blob/613af104c6b85184091d42d374fef420eddb356d/scripts/demo/streamlit_helpers.py#L66
_lowerCamelCase     =  0b1011_0011_1110_1100_1001_0000_0111_1011_1011_0001_1001_1110
# bin(x)[2:] gives bits of x as str, use int to convert them to 0/1
_lowerCamelCase     =  [int(bit) for bit in bin(WATERMARK_MESSAGE)[2:]]


class 			lowerCamelCase_		:





				"""simple docstring"""







				def __init__(						self     ):
								SCREAMING_SNAKE_CASE__     							=				WATERMARK_BITS
								SCREAMING_SNAKE_CASE__     							=				WatermarkEncoder()

								self.encoder.set_watermark("bits"       ,		self.watermark     )






				def   lowerCAmelCase__		(						self       ,		UpperCAmelCase__     ):
								# can't encode images that are smaller than 256
								if images.shape[-1] < 256:
												return images

								SCREAMING_SNAKE_CASE__     							=				(255 * (images / 2 + 0.5)).cpu().permute(0       ,		2       ,		3       ,		1     ).float().numpy()

								SCREAMING_SNAKE_CASE__     							=				[self.encoder.encode(UpperCAmelCase__       ,		"dwtDct"     ) for image in images]

								SCREAMING_SNAKE_CASE__     							=				torch.from_numpy(np.array(UpperCAmelCase__     )     ).permute(0       ,		3       ,		1       ,		2     )

								SCREAMING_SNAKE_CASE__     							=				torch.clamp(2 * (images / 255 - 0.5)       ,		min=-1.0       ,		max=1.0     )
								return images
