


'''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				,				_lowerCamelCase				,				_lowerCamelCase=13				,				_lowerCamelCase=7				,				_lowerCamelCase=True				,				_lowerCamelCase=True				,				_lowerCamelCase=True				,				_lowerCamelCase=True				,				_lowerCamelCase=99				,				_lowerCamelCase=32				,				_lowerCamelCase=2				,				_lowerCamelCase=4				,				_lowerCamelCase=37				,				_lowerCamelCase="gelu"				,				_lowerCamelCase=0.1				,				_lowerCamelCase=0.1				,				_lowerCamelCase=512				,				_lowerCamelCase=16				,				_lowerCamelCase=2				,				_lowerCamelCase=0.02				,				_lowerCamelCase=False				,				_lowerCamelCase=True				,				_lowerCamelCase="None"				,				_lowerCamelCase=3				,				_lowerCamelCase=4				,				_lowerCamelCase=None				,				):
								UpperCAmelCase__	:       Tuple   =  parent
								UpperCAmelCase__	:       int   =  batch_size
								UpperCAmelCase__	:       Tuple   =  seq_length
								UpperCAmelCase__	:       Optional[Any]   =  is_training
								UpperCAmelCase__	:       int   =  use_input_mask
								UpperCAmelCase__	:       Dict   =  use_token_type_ids
								UpperCAmelCase__	:       List[str]   =  use_labels
								UpperCAmelCase__	:       Optional[Any]   =  vocab_size
								UpperCAmelCase__	:       Dict   =  hidden_size
								UpperCAmelCase__	:       Optional[int]   =  num_hidden_layers
								UpperCAmelCase__	:       List[Any]   =  num_attention_heads
								UpperCAmelCase__	:       str   =  intermediate_size
								UpperCAmelCase__	:       Optional[int]   =  hidden_act
								UpperCAmelCase__	:       Tuple   =  hidden_dropout_prob
								UpperCAmelCase__	:       int   =  attention_probs_dropout_prob
								UpperCAmelCase__	:       int   =  max_position_embeddings
								UpperCAmelCase__	:       Optional[int]   =  type_vocab_size
								UpperCAmelCase__	:       Any   =  type_sequence_label_size
								UpperCAmelCase__	:       Tuple   =  initializer_range
								UpperCAmelCase__	:       Any   =  num_labels
								UpperCAmelCase__	:       Tuple   =  num_choices
								UpperCAmelCase__	:       int   =  relative_attention
								UpperCAmelCase__	:       Tuple   =  position_biased_input
								UpperCAmelCase__	:       Union[str, Any]   =  pos_att_type
								UpperCAmelCase__	:       Tuple   =  scope




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

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

								UpperCAmelCase__	:       int   =  None
								if self.use_token_type_ids:
											UpperCAmelCase__	:       Optional[Any]   =  ids_tensor([self.batch_size, self.seq_length]				,				self.type_vocab_size)

								UpperCAmelCase__	:       int   =  None
								UpperCAmelCase__	:       List[Any]   =  None
								UpperCAmelCase__	:       Dict   =  None
								if self.use_labels:
											UpperCAmelCase__	:       List[str]   =  ids_tensor([self.batch_size]				,				self.type_sequence_label_size)
											UpperCAmelCase__	:       Union[str, Any]   =  ids_tensor([self.batch_size, self.seq_length]				,				self.num_labels)

								UpperCAmelCase__	:       Dict   =  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 					snake_case__    (   self				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase):
								UpperCAmelCase__	:       Optional[int]   =  TFDebertaVaModel(config=__UpperCAmelCase)
								UpperCAmelCase__	:       List[Any]   =  {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}

								UpperCAmelCase__	:       Union[str, Any]   =  [input_ids, input_mask]
								UpperCAmelCase__	:       Union[str, Any]   =  model(__UpperCAmelCase)

								UpperCAmelCase__	:       Optional[Any]   =  model(__UpperCAmelCase)

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




					def 					snake_case__    (   self				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase):
								UpperCAmelCase__	:       Optional[Any]   =  TFDebertaVaForMaskedLM(config=__UpperCAmelCase)
								UpperCAmelCase__	:       Optional[int]   =  {
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'token_type_ids': token_type_ids,
								}
								UpperCAmelCase__	:       List[Any]   =  model(__UpperCAmelCase)
								self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.vocab_size))




					def 					snake_case__    (   self				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase):
								UpperCAmelCase__	:       Dict   =  self.num_labels
								UpperCAmelCase__	:       Dict   =  TFDebertaVaForSequenceClassification(config=__UpperCAmelCase)
								UpperCAmelCase__	:       Optional[int]   =  {
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'token_type_ids': token_type_ids,
								}

								UpperCAmelCase__	:       Union[str, Any]   =  model(__UpperCAmelCase)
								self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.num_labels))




					def 					snake_case__    (   self				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase):
								UpperCAmelCase__	:       int   =  self.num_labels
								UpperCAmelCase__	:       List[Any]   =  TFDebertaVaForTokenClassification(config=__UpperCAmelCase)
								UpperCAmelCase__	:       Union[str, Any]   =  {
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'token_type_ids': token_type_ids,
								}
								UpperCAmelCase__	:       List[str]   =  model(__UpperCAmelCase)
								self.parent.assertEqual(result.logits.shape				,				(self.batch_size, self.seq_length, self.num_labels))




					def 					snake_case__    (   self				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase				,				_lowerCamelCase):
								UpperCAmelCase__	:       List[Any]   =  TFDebertaVaForQuestionAnswering(config=__UpperCAmelCase)
								UpperCAmelCase__	:       Optional[Any]   =  {
								    'input_ids': input_ids,
								    'attention_mask': input_mask,
								    'token_type_ids': token_type_ids,
								}

								UpperCAmelCase__	:       Optional[int]   =  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 					snake_case__    (   self):
								UpperCAmelCase__	:       Union[str, Any]   =  self.prepare_config_and_inputs()
								(
								    UpperCAmelCase__							
								)	:       Tuple   =  config_and_inputs
								UpperCAmelCase__	:       Tuple   =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
								return config, inputs_dict


@require_tf
class 		_snake_case       (  a__							, a__							, unittest.TestCase						):
					lowerCAmelCase   :List[str]				=       (
					    (
					        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   :List[str]				=       False
					lowerCAmelCase   :Optional[Any]				=       False




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       List[str]   =  TFDebertaVaModelTester(self)
								UpperCAmelCase__	:       str   =  ConfigTester(self				,				config_class=__UpperCAmelCase				,				hidden_size=37)




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




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       int   =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__UpperCAmelCase)




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       List[str]   =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_lm(*__UpperCAmelCase)




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       Optional[Any]   =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_question_answering(*__UpperCAmelCase)




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       Tuple   =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_sequence_classification(*__UpperCAmelCase)




					def 					snake_case__    (   self):
								UpperCAmelCase__	:       Tuple   =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_token_classification(*__UpperCAmelCase)




					@slow
					def 					snake_case__    (   self):
								UpperCAmelCase__	:       Optional[int]   =  TFDebertaVaModel.from_pretrained("""kamalkraj/deberta-v2-xlarge""")
								self.assertIsNotNone(__UpperCAmelCase)


@require_tf
class 		_snake_case       (  unittest.TestCase						):




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




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

								UpperCAmelCase__	:       Optional[Any]   =  tf.constant(
								    [[[0.2356, 0.1948, 0.0369], [-0.1063, 0.3586, -0.5152], [-0.6399, -0.0259, -0.2525]]])
								tf.debugging.assert_near(output[:, 1:4, 1:4]				,				__UpperCAmelCase				,				atol=1e-4)






"""simple docstring"""





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import (
      AutoProcessor,
      BertTokenizerFast,
      BlipImageProcessor,
      GPTaTokenizer,
      InstructBlipProcessor,
      PreTrainedTokenizerFast,
  )






@require_vision
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				tempfile.mkdtemp()

         lowerCAmelCase__     :List[Any]											=				BlipImageProcessor()
         lowerCAmelCase__     :Union[str, Any]											=				GPTaTokenizer.from_pretrained('hf-internal-testing/tiny-random-GPT2Model' )
         lowerCAmelCase__     :Union[str, Any]											=				BertTokenizerFast.from_pretrained('hf-internal-testing/tiny-random-bert' )

         lowerCAmelCase__     :List[str]											=				InstructBlipProcessor(__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase )

         processor.save_pretrained(self.tmpdirname )






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).tokenizer






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).image_processor






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).qformer_tokenizer






       def 	snake_case		(       self ):



         '''simple docstring'''

         shutil.rmtree(self.tmpdirname )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				[np.random.randint(2_5_5       ,					size=(3, 3_0, 4_0_0)       ,					dtype=np.uinta )]

         lowerCAmelCase__     :Dict											=				[Image.fromarray(np.moveaxis(__UpperCAmelCase       ,					0       ,					-1 ) ) for x in image_inputs]

         return image_inputs






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				InstructBlipProcessor(
             tokenizer=self.get_tokenizer()       ,					image_processor=self.get_image_processor()       ,					qformer_tokenizer=self.get_qformer_tokenizer()       ,					)
         processor.save_pretrained(self.tmpdirname )

         lowerCAmelCase__     :Tuple											=				self.get_tokenizer(bos_token='(BOS)'       ,					eos_token='(EOS)' )
         lowerCAmelCase__     :str											=				self.get_image_processor(do_normalize=__UpperCAmelCase       ,					padding_value=1.0 )

         lowerCAmelCase__     :Dict											=				InstructBlipProcessor.from_pretrained(
             self.tmpdirname       ,					bos_token='(BOS)'       ,					eos_token='(EOS)'       ,					do_normalize=__UpperCAmelCase       ,					padding_value=1.0 )

         self.assertEqual(processor.tokenizer.get_vocab()       ,					tokenizer_add_kwargs.get_vocab() )
         self.assertIsInstance(processor.tokenizer       ,					__UpperCAmelCase )

         self.assertEqual(processor.image_processor.to_json_string()       ,					image_processor_add_kwargs.to_json_string() )
         self.assertIsInstance(processor.image_processor       ,					__UpperCAmelCase )
         self.assertIsInstance(processor.qformer_tokenizer       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				self.get_image_processor()
         lowerCAmelCase__     :List[Any]											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :str											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Dict											=				self.prepare_image_inputs()

         lowerCAmelCase__     :List[str]											=				image_processor(__UpperCAmelCase       ,					return_tensors='np' )
         lowerCAmelCase__     :Optional[Any]											=				processor(images=__UpperCAmelCase       ,					return_tensors='np' )

         for key in input_feat_extract.keys():
           self.assertAlmostEqual(input_feat_extract[key].sum()       ,					input_processor[key].sum()       ,					delta=1E-2 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Any											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Optional[int]											=				'lower newer'

         lowerCAmelCase__     :Dict											=				processor(text=__UpperCAmelCase )

         lowerCAmelCase__     :Union[str, Any]											=				tokenizer(__UpperCAmelCase       ,					return_token_type_ids=__UpperCAmelCase )
         lowerCAmelCase__     :Tuple											=				qformer_tokenizer(__UpperCAmelCase       ,					return_token_type_ids=__UpperCAmelCase )

         for key in encoded_tokens.keys():
           self.assertListEqual(encoded_tokens[key]       ,					encoded_processor[key] )

         for key in encoded_tokens_qformer.keys():
           self.assertListEqual(encoded_tokens_qformer[key]       ,					encoded_processor['qformer_' + key] )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :str											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Dict											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Union[str, Any]											=				'lower newer'
         lowerCAmelCase__     :Dict											=				self.prepare_image_inputs()

         lowerCAmelCase__     :Tuple											=				processor(text=__UpperCAmelCase       ,					images=__UpperCAmelCase )

         self.assertListEqual(
             list(inputs.keys() )       ,					['input_ids', 'attention_mask', 'qformer_input_ids', 'qformer_attention_mask', 'pixel_values']       ,					)

         # test if it raises when no input is passed
         with pytest.raises(__UpperCAmelCase ):
           processor()






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				self.get_image_processor()
         lowerCAmelCase__     :Dict											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :int											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :List[str]											=				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         lowerCAmelCase__     :Tuple											=				processor.batch_decode(__UpperCAmelCase )
         lowerCAmelCase__     :Optional[int]											=				tokenizer.batch_decode(__UpperCAmelCase )

         self.assertListEqual(__UpperCAmelCase       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :Dict											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Optional[Any]											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Optional[int]											=				'lower newer'
         lowerCAmelCase__     :Optional[int]											=				self.prepare_image_inputs()

         lowerCAmelCase__     :int											=				processor(text=__UpperCAmelCase       ,					images=__UpperCAmelCase )

         self.assertListEqual(
             list(inputs.keys() )       ,					['input_ids', 'attention_mask', 'qformer_input_ids', 'qformer_attention_mask', 'pixel_values']       ,					)
