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





	'''simple docstring'''



	def __init__(  self	:    Dict			,  lowercase	:    List[Any]			,  lowercase	:    Tuple=13			,  lowercase	:    Optional[Any]=7			,  lowercase	:    Tuple=True			,  lowercase	:    Optional[Any]=True			,  lowercase	:    str=True			,  lowercase	:    Dict=True			,  lowercase	:    List[str]=99			,  lowercase	:    Tuple=32			,  lowercase	:    str=2			,  lowercase	:    Any=4			,  lowercase	:    Tuple=37			,  lowercase	:    List[str]="gelu"			,  lowercase	:    Dict=0.1			,  lowercase	:    Any=0.1			,  lowercase	:    List[str]=512			,  lowercase	:    Optional[int]=16			,  lowercase	:    str=2			,  lowercase	:    Dict=0.02			,  lowercase	:    Any=3			,  lowercase	:    List[Any]=4			,  lowercase	:    List[Any]=None			,  lowercase	:    Optional[Any]=0			,  ):


				'''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		=			scope
				_snake_case		=			projection_dim



	def  A							(  self	:    int	):


				'''simple docstring'''


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

				_snake_case		=			None
				if self.use_input_mask:
							# follow test_modeling_tf_ctrl.py
							_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		=			ids_tensor([self.batch_size]			,  self.num_choices	)

				_snake_case		=			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=lowercase			,  initializer_range=self.initializer_range			,  )
				_snake_case		=			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  A							(  self	:    Union[str, Any]			,  lowercase	:    Dict			,  lowercase	:    List[Any]			,  lowercase	:    Tuple			,  lowercase	:    Dict			,  lowercase	:    List[str]			,  lowercase	:    Union[str, Any]			,  lowercase	:    Any	):


				'''simple docstring'''


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



	def  A							(  self	:    List[str]			,  lowercase	:    Optional[int]			,  lowercase	:    int			,  lowercase	:    str			,  lowercase	:    Dict			,  lowercase	:    int			,  lowercase	:    Union[str, Any]			,  lowercase	:    List[Any]	):


				'''simple docstring'''


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



	def  A							(  self	:    List[str]			,  lowercase	:    Optional[int]			,  lowercase	:    Union[str, Any]			,  lowercase	:    Union[str, Any]			,  lowercase	:    Optional[Any]			,  lowercase	:    Optional[Any]			,  lowercase	:    List[str]			,  lowercase	:    List[str]	):


				'''simple docstring'''


				_snake_case		=			TFDPRReader(config=lowercase	)
				_snake_case		=			model(lowercase			,  attention_mask=lowercase	)

				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  A							(  self	:    List[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}
				return config, inputs_dict




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





	'''simple docstring'''



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

	_UpperCAmelCase       :		str       =					False
	_UpperCAmelCase       :		Optional[int]       =					False
	_UpperCAmelCase       :		Union[str, Any]       =					False
	_UpperCAmelCase       :		Tuple       =					False
	_UpperCAmelCase       :		Union[str, Any]       =					False



	def  A							(  self	:    Optional[Any]	):


				'''simple docstring'''


				_snake_case		=			TFDPRModelTester(self	)
				_snake_case		=			ConfigTester(self			,  config_class=lowercase			,  hidden_size=37	)



	def  A							(  self	:    Union[str, Any]	):


				'''simple docstring'''


				self.config_tester.run_common_tests()



	def  A							(  self	:    List[str]	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_context_encoder(*lowercase	)



	def  A							(  self	:    Dict	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_question_encoder(*lowercase	)



	def  A							(  self	:    Dict	):


				'''simple docstring'''


				_snake_case		=			self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_dpr_reader(*lowercase	)





	@slow
	def  A							(  self	:    Any	):


				'''simple docstring'''


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

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

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

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




@require_tf
class    SCREAMING_SNAKE_CASE__     (	unittest.TestCase					):





	'''simple docstring'''



	@slow
	def  A							(  self	:    Any	):


				'''simple docstring'''


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

				_snake_case		=			tf.constant(
				    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]	)  # [CLS] hello, is my dog cute? [SEP]
				_snake_case		=			model(lowercase	)[0]  # embedding shape = (1, 768)
				# compare the actual values for a slice.
				_snake_case		=			tf.constant(
				    [
				        [
				            0.03236253,
				            0.12753335,
				            0.16818509,
				            0.00279786,
				            0.3896933,
				            0.24264945,
				            0.2178971,
				            -0.02335227,
				            -0.08481959,
				            -0.14324117,
				        ]
				    ]	)
				self.assertTrue(numpy.allclose(output[:, :10].numpy()			,  expected_slice.numpy()			,  atol=1E-4	)	)

import warnings

from ...utils import logging
from .image_processing_chinese_clip import ChineseCLIPImageProcessor


_lowerCamelCase       :				Dict       =			logging.get_logger(__name__)




class    SCREAMING_SNAKE_CASE__     (	UpperCAmelCase					):





	'''simple docstring'''



	def __init__(  self	:    Tuple			,  *lowercase	:    Optional[int]			,  **lowercase	:    Any	):


				'''simple docstring'''


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