

from __future__ import annotations

import unittest

from transformers import 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 numpy
 import tensorflow as tf

 from transformers import (
     TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
     TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
     BertConfig,
     DPRConfig,
     TFDPRContextEncoder,
     TFDPRQuestionEncoder,
     TFDPRReader,
 )



class        lowercase		:



 '''simple docstring'''


 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.02			,			__a=3			,			__a=4			,			__a=None			,			__a=0			,			)					->   Any:






  """simple docstring"""





  UpperCAmelCase__     							=							parent
  UpperCAmelCase__     							=							batch_size
  UpperCAmelCase__     							=							seq_length
  UpperCAmelCase__     							=							is_training
  UpperCAmelCase__     							=							use_input_mask
  UpperCAmelCase__     							=							use_token_type_ids
  UpperCAmelCase__     							=							use_labels
  UpperCAmelCase__     							=							vocab_size
  UpperCAmelCase__     							=							hidden_size
  UpperCAmelCase__     							=							num_hidden_layers
  UpperCAmelCase__     							=							num_attention_heads
  UpperCAmelCase__     							=							intermediate_size
  UpperCAmelCase__     							=							hidden_act
  UpperCAmelCase__     							=							hidden_dropout_prob
  UpperCAmelCase__     							=							attention_probs_dropout_prob
  UpperCAmelCase__     							=							max_position_embeddings
  UpperCAmelCase__     							=							type_vocab_size
  UpperCAmelCase__     							=							type_sequence_label_size
  UpperCAmelCase__     							=							initializer_range
  UpperCAmelCase__     							=							num_labels
  UpperCAmelCase__     							=							num_choices
  UpperCAmelCase__     							=							scope
  UpperCAmelCase__     							=							projection_dim




 def     UpperCamelCase__				(self					)					->   Dict:






  """simple docstring"""





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

  UpperCAmelCase__     							=							None
  if self.use_input_mask:
   # follow test_modeling_tf_ctrl.py
   UpperCAmelCase__     							=							random_attention_mask([self.batch_size, self.seq_length]					)

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

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

  UpperCAmelCase__     							=							BertConfig(
      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			,			is_decoder=A_			,			initializer_range=self.initializer_range			,			)
  UpperCAmelCase__     							=							DPRConfig(projection_dim=self.projection_dim			,			**config.to_dict()					)

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




 def     UpperCamelCase__				(self			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a					)					->   str:






  """simple docstring"""





  UpperCAmelCase__     							=							TFDPRContextEncoder(config=A_					)
  UpperCAmelCase__     							=							model(A_			,			attention_mask=A_			,			token_type_ids=A_					)
  UpperCAmelCase__     							=							model(A_			,			token_type_ids=A_					)
  UpperCAmelCase__     							=							model(A_					)
  self.parent.assertEqual(result.pooler_output.shape			,			(self.batch_size, self.projection_dim or self.hidden_size)					)




 def     UpperCamelCase__				(self			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a					)					->   str:






  """simple docstring"""





  UpperCAmelCase__     							=							TFDPRQuestionEncoder(config=A_					)
  UpperCAmelCase__     							=							model(A_			,			attention_mask=A_			,			token_type_ids=A_					)
  UpperCAmelCase__     							=							model(A_			,			token_type_ids=A_					)
  UpperCAmelCase__     							=							model(A_					)
  self.parent.assertEqual(result.pooler_output.shape			,			(self.batch_size, self.projection_dim or self.hidden_size)					)




 def     UpperCamelCase__				(self			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a			,			__a					)					->   int:






  """simple docstring"""





  UpperCAmelCase__     							=							TFDPRReader(config=A_					)
  UpperCAmelCase__     							=							model(A_			,			attention_mask=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)					)
  self.parent.assertEqual(result.relevance_logits.shape			,			(self.batch_size,)					)




 def     UpperCamelCase__				(self					)					->   str:






  """simple docstring"""





  UpperCAmelCase__     							=							self.prepare_config_and_inputs()
  (
      UpperCAmelCase__					
  )     							=							config_and_inputs
  UpperCAmelCase__     							=							{'''input_ids''': input_ids}
  return config, inputs_dict







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



 '''simple docstring'''


 __SCREAMING_SNAKE_CASE   				=							(
     (
         TFDPRContextEncoder,
         TFDPRQuestionEncoder,
         TFDPRReader,
     )
     if is_tf_available()
     else ()
 )
 __SCREAMING_SNAKE_CASE   				=							{"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

 __SCREAMING_SNAKE_CASE   				=							False
 __SCREAMING_SNAKE_CASE   				=							False
 __SCREAMING_SNAKE_CASE   				=							False
 __SCREAMING_SNAKE_CASE   				=							False
 __SCREAMING_SNAKE_CASE   				=							False




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






  """simple docstring"""





  UpperCAmelCase__     							=							TFDPRModelTester(self					)
  UpperCAmelCase__     							=							ConfigTester(self			,			config_class=A_			,			hidden_size=37					)




 def     UpperCamelCase__				(self					)					->   Dict:






  """simple docstring"""





  self.config_tester.run_common_tests()




 def     UpperCamelCase__				(self					)					->   Dict:






  """simple docstring"""





  UpperCAmelCase__     							=							self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_dpr_context_encoder(*A_					)




 def     UpperCamelCase__				(self					)					->   Any:






  """simple docstring"""





  UpperCAmelCase__     							=							self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_dpr_question_encoder(*A_					)




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






  """simple docstring"""





  UpperCAmelCase__     							=							self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_dpr_reader(*A_					)




 @slow
 def     UpperCamelCase__				(self					)					->   Tuple:






  """simple docstring"""





  for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   UpperCAmelCase__     							=							TFDPRContextEncoder.from_pretrained(A_					)
   self.assertIsNotNone(A_					)

  for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   UpperCAmelCase__     							=							TFDPRContextEncoder.from_pretrained(A_					)
   self.assertIsNotNone(A_					)

  for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   UpperCAmelCase__     							=							TFDPRQuestionEncoder.from_pretrained(A_					)
   self.assertIsNotNone(A_					)

  for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   UpperCAmelCase__     							=							TFDPRReader.from_pretrained(A_					)
   self.assertIsNotNone(A_					)







@require_tf
class        lowercase		(			unittest.TestCase   ):



 '''simple docstring'''


 @slow
 def     UpperCamelCase__				(self					)					->   List[Any]:






  """simple docstring"""





  UpperCAmelCase__     							=							TFDPRQuestionEncoder.from_pretrained('facebook/dpr-question_encoder-single-nq-base'					)

  UpperCAmelCase__     							=							tf.constant(
      [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]					)  # [CLS] hello, is my dog cute? [SEP]
  UpperCAmelCase__     							=							model(A_					)[0]  # embedding shape = (1, 768)
  # compare the actual values for a slice.
  UpperCAmelCase__     							=							tf.constant(
      [
          [
              0.03_23_62_53,
              0.12_75_33_35,
              0.16_81_85_09,
              0.00_27_97_86,
              0.3_89_69_33,
              0.24_26_49_45,
              0.2_17_89_71,
              -0.02_33_52_27,
              -0.08_48_19_59,
              -0.14_32_41_17,
          ]
      ]					)
  self.assertTrue(numpy.allclose(output[:, :10].numpy()			,			expected_slice.numpy()			,			atol=1E-4					)					)





"""simple docstring"""




import warnings

from ...utils import logging
from .image_processing_segformer import SegformerImageProcessor


A__ :       List[Any]				= logging.get_logger(__name__)


class       __magic_name__							(							SCREAMING_SNAKE_CASE__			):
       def __init__(  self			,					*A_			,					**A_							)  ->						None:





         """simple docstring"""




         warnings.warn(
             '''The class SegformerFeatureExtractor is deprecated and will be removed in version 5 of Transformers.'''
             ''' Please use SegformerImageProcessor instead.'''			,					A_			,					)
         super().__init__(*A_			,					**A_							)
