



"""simple docstring"""







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



			def __init__(			self							:				Optional[Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Dict=13   ,	_lowercase							:				Optional[int]=7   ,	_lowercase							:				Tuple=True   ,	_lowercase							:				List[str]=True   ,	_lowercase							:				Any=True   ,	_lowercase							:				List[Any]=True   ,	_lowercase							:				Any=99   ,	_lowercase							:				Optional[int]=32   ,	_lowercase							:				str=2   ,	_lowercase							:				List[Any]=4   ,	_lowercase							:				Any=37   ,	_lowercase							:				str="gelu"   ,	_lowercase							:				List[str]=0.1   ,	_lowercase							:				Optional[int]=0.1   ,	_lowercase							:				Dict=512   ,	_lowercase							:				List[str]=16   ,	_lowercase							:				Optional[Any]=2   ,	_lowercase							:				Tuple=0.02   ,	_lowercase							:				Tuple=3   ,	_lowercase							:				List[Any]=4   ,	_lowercase							:				List[Any]=None   ,	_lowercase							:				Dict=0   ,	)							->					Any:
									snake_case   :			str						    = parent
									snake_case   :			Optional[Any]						    = batch_size
									snake_case   :			int						    = seq_length
									snake_case   :			Any						    = is_training
									snake_case   :			Dict						    = use_input_mask
									snake_case   :			Optional[int]						    = use_token_type_ids
									snake_case   :			str						    = use_labels
									snake_case   :			int						    = vocab_size
									snake_case   :			List[Any]						    = hidden_size
									snake_case   :			Union[str, Any]						    = num_hidden_layers
									snake_case   :			Dict						    = num_attention_heads
									snake_case   :			List[Any]						    = intermediate_size
									snake_case   :			int						    = hidden_act
									snake_case   :			List[Any]						    = hidden_dropout_prob
									snake_case   :			List[str]						    = attention_probs_dropout_prob
									snake_case   :			Any						    = max_position_embeddings
									snake_case   :			Any						    = type_vocab_size
									snake_case   :			str						    = type_sequence_label_size
									snake_case   :			Tuple						    = initializer_range
									snake_case   :			Optional[int]						    = num_labels
									snake_case   :			Union[str, Any]						    = num_choices
									snake_case   :			List[str]						    = scope
									snake_case   :			Optional[int]						    = projection_dim



			def 				__lowercase					(			self							:				List[str]		)							->					Tuple:
									snake_case   :			Dict						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size		)

									snake_case   :			Union[str, Any]						    = None
									if self.use_input_mask:
															# follow test_modeling_tf_ctrl.py
															snake_case   :			str						    = random_attention_mask([self.batch_size, self.seq_length]		)

									snake_case   :			Union[str, Any]						    = None
									if self.use_token_type_ids:
															snake_case   :			List[Any]						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.type_vocab_size		)

									snake_case   :			Union[str, Any]						    = None
									snake_case   :			List[Any]						    = None
									snake_case   :			Optional[Any]						    = None
									if self.use_labels:
															snake_case   :			Any						    = ids_tensor([self.batch_size]   ,	self.type_sequence_label_size		)
															snake_case   :			Optional[Any]						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.num_labels		)
															snake_case   :			Union[str, Any]						    = ids_tensor([self.batch_size]   ,	self.num_choices		)

									snake_case   :			Any						    = 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   :			Optional[Any]						    = 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 				__lowercase					(			self							:				int   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Dict   ,	_lowercase							:				Any   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Tuple   ,	_lowercase							:				Tuple		)							->					Optional[Any]:
									snake_case   :			Tuple						    = TFDPRContextEncoder(config=_lowercase		)
									snake_case   :			str						    = model(_lowercase   ,	attention_mask=_lowercase   ,	token_type_ids=_lowercase		)
									snake_case   :			Tuple						    = model(_lowercase   ,	token_type_ids=_lowercase		)
									snake_case   :			str						    = model(_lowercase		)
									self.parent.assertEqual(result.pooler_output.shape   ,	(self.batch_size, self.projection_dim or self.hidden_size)		)



			def 				__lowercase					(			self							:				Optional[int]   ,	_lowercase							:				str   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				List[str]   ,	_lowercase							:				int		)							->					str:
									snake_case   :			Optional[Any]						    = TFDPRQuestionEncoder(config=_lowercase		)
									snake_case   :			Tuple						    = model(_lowercase   ,	attention_mask=_lowercase   ,	token_type_ids=_lowercase		)
									snake_case   :			List[Any]						    = model(_lowercase   ,	token_type_ids=_lowercase		)
									snake_case   :			Dict						    = model(_lowercase		)
									self.parent.assertEqual(result.pooler_output.shape   ,	(self.batch_size, self.projection_dim or self.hidden_size)		)



			def 				__lowercase					(			self							:				Optional[Any]   ,	_lowercase							:				Dict   ,	_lowercase							:				Dict   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Dict   ,	_lowercase							:				List[str]   ,	_lowercase							:				int   ,	_lowercase							:				Dict		)							->					Tuple:
									snake_case   :			Tuple						    = TFDPRReader(config=_lowercase		)
									snake_case   :			List[Any]						    = 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 				__lowercase					(			self							:				int		)							->					Optional[Any]:
									snake_case   :			List[str]						    = self.prepare_config_and_inputs()
									(
									    (
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			
									)   :			Dict						    = config_and_inputs
									snake_case   :			Union[str, Any]						    = {"input_ids": input_ids}
									return config, inputs_dict



@require_tf
class       _a  ( SCREAMING_SNAKE_CASE__     ,	SCREAMING_SNAKE_CASE__     ,	unittest.TestCase):
			__magic_name__     					=     (
			    (
			        TFDPRContextEncoder,
			        TFDPRQuestionEncoder,
			        TFDPRReader,
			    )
			    if is_tf_available()
			    else ()
			)
			__magic_name__     					=     {"""feature-extraction""": TFDPRQuestionEncoder} if is_tf_available() else {}

			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False



			def 				__lowercase					(			self							:				Any		)							->					Dict:
									snake_case   :			int						    = TFDPRModelTester(self		)
									snake_case   :			Optional[Any]						    = ConfigTester(self   ,	config_class=_lowercase   ,	hidden_size=37		)



			def 				__lowercase					(			self							:				List[str]		)							->					int:
									self.config_tester.run_common_tests()



			def 				__lowercase					(			self							:				int		)							->					Union[str, Any]:
									snake_case   :			str						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_context_encoder(*_lowercase		)



			def 				__lowercase					(			self							:				str		)							->					Optional[int]:
									snake_case   :			Dict						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_question_encoder(*_lowercase		)



			def 				__lowercase					(			self							:				Optional[int]		)							->					Dict:
									snake_case   :			Optional[Any]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_dpr_reader(*_lowercase		)



			@slow
			def 				__lowercase					(			self							:				List[str]		)							->					Tuple:
									for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			Tuple						    = TFDPRContextEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in TF_DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			Union[str, Any]						    = TFDPRContextEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in TF_DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			List[str]						    = TFDPRQuestionEncoder.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)

									for model_name in TF_DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :			Dict						    = TFDPRReader.from_pretrained(_lowercase		)
															self.assertIsNotNone(_lowercase		)



@require_tf
class       _a  ( unittest.TestCase):



			@slow
			def 				__lowercase					(			self							:				List[Any]		)							->					Dict:
									snake_case   :			List[str]						    = TFDPRQuestionEncoder.from_pretrained("facebook/dpr-question_encoder-single-nq-base"		)

									snake_case   :			Union[str, Any]						    = tf.constant(
									    [[101, 7592, 1010, 2003, 2026, 3899, 10140, 1029, 102]]		)  # [CLS] hello, is my dog cute? [SEP]
									snake_case   :			Optional[int]						    = model(_lowercase		)[0]  # embedding shape = (1, 768)
									# compare the actual values for a slice.
									snake_case   :			Any						    = 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		)		)








"""simple docstring"""







import contextlib
import importlib
import io
import unittest

import transformers

# Try to import everything from transformers to ensure every object can be loaded.
from transformers import *  # noqa F406
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, require_flax, require_tf, require_torch
from transformers.utils import ContextManagers, find_labels, is_flax_available, is_tf_available, is_torch_available


if is_torch_available():
			from transformers import BertForPreTraining, BertForQuestionAnswering, BertForSequenceClassification

if is_tf_available():
			from transformers import TFBertForPreTraining, TFBertForQuestionAnswering, TFBertForSequenceClassification

if is_flax_available():
			from transformers import FlaxBertForPreTraining, FlaxBertForQuestionAnswering, FlaxBertForSequenceClassification


A		=						DUMMY_UNKNOWN_IDENTIFIER
# An actual model hosted on huggingface.co

A		=						'main'
# Default branch name
A		=						'f2c752cfc5c0ab6f4bdec59acea69eefbee381c2'
# One particular commit (not the top of `main`)
A		=						'aaaaaaa'
# This commit does not exist, so we should 404.

A		=						'd9e9f15bc825e4b2c9249e9578f884bbcb5e3684'
# Sha-1 of config.json on the top of `main`, for checking purposes
A		=						'4b243c475af8d0a7754e87d7d096c92e5199ec2fe168a2ee7998e3b8e9bcb1d3'

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Welcome!" )
						yield
						print("Bye!" )

@contextlib.contextmanager
def  __SCREAMING_SNAKE_CASE		(			):
						"""simple docstring"""






						print("Bonjour!" )
						yield
						print("Au revoir!" )

class       _a  ( unittest.TestCase):



			def 				__lowercase					(			self							:				Dict		)							->					Optional[int]:
									# If the spec is missing, importlib would not be able to import the module dynamically.
									assert transformers.__spec__ is not None
									assert importlib.util.find_spec("transformers"		) is not None



class       _a  ( unittest.TestCase):



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Union[str, Any]   ,	_lowercase							:				Optional[int]		)							->					Optional[Any]:
									with ContextManagers([]		):
															print("Transformers are awesome!"		)
									# The print statement adds a new line at the end of the output
									self.assertEqual(mock_stdout.getvalue()   ,	"Transformers are awesome!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				Any   ,	_lowercase							:				Any		)							->					Dict:
									with ContextManagers([context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Welcome!\nTransformers are awesome!\nBye!\n"		)



			@unittest.mock.patch("sys.stdout"   ,	new_callable=io.StringIO		)
			def 				__lowercase					(			self							:				str   ,	_lowercase							:				List[Any]		)							->					Optional[int]:
									with ContextManagers([context_fr(), context_en()]		):
															print("Transformers are awesome!"		)
									# The output should be wrapped with an English and French welcome and goodbye
									self.assertEqual(mock_stdout.getvalue()   ,	"Bonjour!\nWelcome!\nTransformers are awesome!\nBye!\nAu revoir!\n"		)



			@require_torch
			def 				__lowercase					(			self							:				Any		)							->					Union[str, Any]:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_tf
			def 				__lowercase					(			self							:				Optional[Any]		)							->					int:
									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["labels", "next_sentence_label"]		)
									self.assertEqual(find_labels(_lowercase		)   ,	["start_positions", "end_positions"]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	["labels"]		)



			@require_flax
			def 				__lowercase					(			self							:				Tuple		)							->					List[Any]:
									# Flax models don't have labels
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)
									self.assertEqual(find_labels(_lowercase		)   ,	[]		)

									class       _a  ( SCREAMING_SNAKE_CASE__):
												pass



									self.assertEqual(find_labels(_lowercase		)   ,	[]		)



