




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




			def __init__(					self				,							a__				,							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__=2				,							a__=0.0_2				,							a__=False				,							a__=True				,							a__="None"				,							a__=3				,							a__=4				,							a__=None				,							)				->				int:

										'''simple docstring'''




										snake_case_						=      parent
										snake_case_						=      batch_size
										snake_case_						=      seq_length
										snake_case_						=      is_training
										snake_case_						=      use_input_mask
										snake_case_						=      use_token_type_ids
										snake_case_						=      use_labels
										snake_case_						=      vocab_size
										snake_case_						=      hidden_size
										snake_case_						=      num_hidden_layers
										snake_case_						=      num_attention_heads
										snake_case_						=      intermediate_size
										snake_case_						=      hidden_act
										snake_case_						=      hidden_dropout_prob
										snake_case_						=      attention_probs_dropout_prob
										snake_case_						=      max_position_embeddings
										snake_case_						=      type_vocab_size
										snake_case_						=      type_sequence_label_size
										snake_case_						=      initializer_range
										snake_case_						=      num_labels
										snake_case_						=      num_choices
										snake_case_						=      relative_attention
										snake_case_						=      position_biased_input
										snake_case_						=      pos_att_type
										snake_case_						=      scope




			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''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_						=      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=a__				,							)

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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				str:

										'''simple docstring'''




										snake_case_						=      TFDebertaVaModel(config=a__							)
										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(a__							)

										snake_case_						=      model(a__							)

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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Optional[int]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Any:

										'''simple docstring'''




										snake_case_						=      self.num_labels
										snake_case_						=      TFDebertaVaForSequenceClassification(config=a__							)
										snake_case_						=      {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

										snake_case_						=      model(a__							)
										self.parent.assertEqual(result.logits.shape				,							(self.batch_size, self.num_labels)							)




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Optional[Any]:

										'''simple docstring'''




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




			def    lowerCAmelCase__				(					self				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__				,							a__							)				->				Any:

										'''simple docstring'''




										snake_case_						=      TFDebertaVaForQuestionAnswering(config=a__							)
										snake_case_						=      {
										    "input_ids": input_ids,
										    "attention_mask": input_mask,
										    "token_type_ids": token_type_ids,
										}

										snake_case_						=      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    lowerCAmelCase__				(					self							)				->				Optional[Any]:

										'''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  _snake_case		(	lowercase_						,						lowercase_						,						unittest.TestCase							):
			lowerCAmelCase_     :      str						       =    (
			    (
			        TFDebertaVaModel,
			        TFDebertaVaForMaskedLM,
			        TFDebertaVaForQuestionAnswering,
			        TFDebertaVaForSequenceClassification,
			        TFDebertaVaForTokenClassification,
			    )
			    if is_tf_available()
			    else ()
			)
			lowerCAmelCase_     :      Optional[int]						       =    (
			    {
			        "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[Any]						       =    False
			lowerCAmelCase_     :      Optional[int]						       =    False




			def    lowerCAmelCase__				(					self							)				->				int:

										'''simple docstring'''




										snake_case_						=      TFDebertaVaModelTester(self							)
										snake_case_						=      ConfigTester(self				,							config_class=a__				,							hidden_size=37							)




			def    lowerCAmelCase__				(					self							)				->				List[Any]:

										'''simple docstring'''




										self.config_tester.run_common_tests()




			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''simple docstring'''




										snake_case_						=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*a__							)




			def    lowerCAmelCase__				(					self							)				->				List[Any]:

										'''simple docstring'''




										snake_case_						=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_masked_lm(*a__							)




			def    lowerCAmelCase__				(					self							)				->				List[str]:

										'''simple docstring'''




										snake_case_						=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_question_answering(*a__							)




			def    lowerCAmelCase__				(					self							)				->				Dict:

										'''simple docstring'''




										snake_case_						=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_sequence_classification(*a__							)




			def    lowerCAmelCase__				(					self							)				->				Union[str, Any]:

										'''simple docstring'''




										snake_case_						=      self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_token_classification(*a__							)




			@slow
			def    lowerCAmelCase__				(					self							)				->				Tuple:

										'''simple docstring'''




										snake_case_						=      TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"							)
										self.assertIsNotNone(a__							)




@require_tf
class  _snake_case		(	unittest.TestCase							):




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

										'''simple docstring'''




										pass




			@slow
			def    lowerCAmelCase__				(					self							)				->				str:

										'''simple docstring'''




										snake_case_						=      TFDebertaVaModel.from_pretrained("kamalkraj/deberta-v2-xlarge"							)
										snake_case_						=      tf.constant([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]							)
										snake_case_						=      tf.constant([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]							)
										snake_case_						=      model(a__				,							attention_mask=a__							)[0]

										snake_case_						=      tf.constant(
										    [[[0.2_3_5_6, 0.1_9_4_8, 0.0_3_6_9], [-0.1_0_6_3, 0.3_5_8_6, -0.5_1_5_2], [-0.6_3_9_9, -0.0_2_5_9, -0.2_5_2_5]]]							)
										tf.debugging.assert_near(output[:, 1:4, 1:4]				,							a__				,							atol=1e-4							)








"""simple docstring"""




import json
import os
import tempfile
import unittest

import numpy as np
from datasets import load_dataset

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import ImageGPTImageProcessor
class 					lowerCAmelCase	(					unittest.TestCase    ):




		'''simple docstring'''







		def __init__(	self						,       lowerCAmelCase__						,       lowerCAmelCase__=7						,       lowerCAmelCase__=3						,       lowerCAmelCase__=18						,       lowerCAmelCase__=30						,       lowerCAmelCase__=400						,       lowerCAmelCase__=True						,       lowerCAmelCase__=None						,       lowerCAmelCase__=True						,       )     ->							int:
								SCREAMING_SNAKE_CASE													=					size if size is not None else {'height': 18, 'width': 18}
								SCREAMING_SNAKE_CASE													=					parent
								SCREAMING_SNAKE_CASE													=					batch_size
								SCREAMING_SNAKE_CASE													=					num_channels
								SCREAMING_SNAKE_CASE													=					image_size
								SCREAMING_SNAKE_CASE													=					min_resolution
								SCREAMING_SNAKE_CASE													=					max_resolution
								SCREAMING_SNAKE_CASE													=					do_resize
								SCREAMING_SNAKE_CASE													=					size
								SCREAMING_SNAKE_CASE													=					do_normalize







		def 							__A						(	self				)     ->							Optional[Any]:
								return {
								    # here we create 2 clusters for the sake of simplicity
								    "clusters": np.asarray(
								        [
								            [0.88_66_44_36_34_03_32_03, 0.66_18_82_93_69_54_49_83, 0.38_91_74_64_01_78_68_04],
								            [-0.60_42_55_91_46_88_11_04, -0.0_22_95_00_88_60_52_84_69, 0.54_23_79_73_69_00_32_96],
								        ]				),
								    "do_resize": self.do_resize,
								    "size": self.size,
								    "do_normalize": self.do_normalize,
								}





@require_torch
@require_vision
class 					lowerCAmelCase	(					lowerCamelCase_	,    unittest.TestCase    ):




		'''simple docstring'''







		SCREAMING_SNAKE_CASE_						:     Union[str, Any]								=     ImageGPTImageProcessor if is_vision_available() else None







		def 							__A						(	self				)     ->							Optional[int]:
								SCREAMING_SNAKE_CASE													=					ImageGPTImageProcessingTester(self				)







		@property
		def 							__A						(	self				)     ->							Any:
								return self.image_processor_tester.prepare_image_processor_dict()







		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'clusters'				)				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_resize'				)				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'size'				)				)
								self.assertTrue(hasattr(lowerCAmelCase__						,       'do_normalize'				)				)







		def 							__A						(	self				)     ->							Tuple:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class.from_dict(self.image_processor_dict				)
								self.assertEqual(image_processor.size						,       {'height': 18, 'width': 18}				)

								SCREAMING_SNAKE_CASE													=					self.image_processing_class.from_dict(self.image_processor_dict						,       size=42				)
								self.assertEqual(image_processor.size						,       {'height': 42, 'width': 42}				)







		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)
								SCREAMING_SNAKE_CASE													=					json.loads(image_processor.to_json_string()				)
								for key, value in self.image_processor_dict.items():
														if key == "clusters":
																				self.assertTrue(np.array_equal(lowerCAmelCase__						,       obj[key]				)				)
														else:
																				self.assertEqual(obj[key]						,       lowerCAmelCase__				)







		def 							__A						(	self				)     ->							List[str]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)

								with tempfile.TemporaryDirectory() as tmpdirname:
														SCREAMING_SNAKE_CASE													=					os.path.join(lowerCAmelCase__						,       'image_processor.json'				)
														image_processor_first.to_json_file(lowerCAmelCase__				)
														SCREAMING_SNAKE_CASE													=					self.image_processing_class.from_json_file(lowerCAmelCase__				).to_dict()

								SCREAMING_SNAKE_CASE													=					image_processor_first.to_dict()
								for key, value in image_processor_first.items():
														if key == "clusters":
																				self.assertTrue(np.array_equal(lowerCAmelCase__						,       image_processor_second[key]				)				)
														else:
																				self.assertEqual(image_processor_first[key]						,       lowerCAmelCase__				)







		def 							__A						(	self				)     ->							Optional[Any]:
								SCREAMING_SNAKE_CASE													=					self.image_processing_class(**self.image_processor_dict				)

								with tempfile.TemporaryDirectory() as tmpdirname:
														image_processor_first.save_pretrained(lowerCAmelCase__				)
														SCREAMING_SNAKE_CASE													=					self.image_processing_class.from_pretrained(lowerCAmelCase__				).to_dict()

								SCREAMING_SNAKE_CASE													=					image_processor_first.to_dict()
								for key, value in image_processor_first.items():
														if key == "clusters":
																				self.assertTrue(np.array_equal(lowerCAmelCase__						,       image_processor_second[key]				)				)
														else:
																				self.assertEqual(image_processor_first[key]						,       lowerCAmelCase__				)







		@unittest.skip('ImageGPT requires clusters at initialization'				)
		def 							__A						(	self				)     ->							Optional[Any]:
								pass





def     lowercase			()   ->						Union[str, Any]:
						SCREAMING_SNAKE_CASE													=					load_dataset('hf-internal-testing/fixtures_image_utils' ,			split='test'  )

						SCREAMING_SNAKE_CASE													=					Image.open(dataset[4]['file']  )
						SCREAMING_SNAKE_CASE													=					Image.open(dataset[5]['file']  )

						SCREAMING_SNAKE_CASE													=					[imagea, imagea]

						return images


@require_vision
@require_torch
class 					lowerCAmelCase	(					unittest.TestCase    ):




		'''simple docstring'''







		@slow
		def 							__A						(	self				)     ->							List[Any]:
								SCREAMING_SNAKE_CASE													=					ImageGPTImageProcessor.from_pretrained('openai/imagegpt-small'				)

								SCREAMING_SNAKE_CASE													=					prepare_images()

								# test non-batched
								SCREAMING_SNAKE_CASE													=					image_processing(images[0]						,       return_tensors='pt'				)

								self.assertIsInstance(encoding.input_ids						,       torch.LongTensor				)
								self.assertEqual(encoding.input_ids.shape						,       (1, 1_024)				)

								SCREAMING_SNAKE_CASE													=					[306, 191, 191]
								self.assertEqual(encoding.input_ids[0, :3].tolist()						,       lowerCAmelCase__				)

								# test batched
								SCREAMING_SNAKE_CASE													=					image_processing(lowerCAmelCase__						,       return_tensors='pt'				)

								self.assertIsInstance(encoding.input_ids						,       torch.LongTensor				)
								self.assertEqual(encoding.input_ids.shape						,       (2, 1_024)				)

								SCREAMING_SNAKE_CASE													=					[303, 13, 13]
								self.assertEqual(encoding.input_ids[1, -3:].tolist()						,       lowerCAmelCase__				)


