



from __future__ import annotations

import unittest

from transformers import is_tf_available, is_torch_available
from transformers.testing_utils import DUMMY_UNKNOWN_IDENTIFIER, SMALL_MODEL_IDENTIFIER, is_pt_tf_cross_test, slow


if is_tf_available():
				from transformers import (
				    AutoConfig,
				    BertConfig,
				    GPTaConfig,
				    TaConfig,
				    TFAutoModel,
				    TFAutoModelForCausalLM,
				    TFAutoModelForMaskedLM,
				    TFAutoModelForPreTraining,
				    TFAutoModelForQuestionAnswering,
				    TFAutoModelForSeqaSeqLM,
				    TFAutoModelForSequenceClassification,
				    TFAutoModelWithLMHead,
				    TFBertForMaskedLM,
				    TFBertForPreTraining,
				    TFBertForQuestionAnswering,
				    TFBertForSequenceClassification,
				    TFBertModel,
				    TFGPTaLMHeadModel,
				    TFRobertaForMaskedLM,
				    TFTaForConditionalGeneration,
				)
				from transformers.models.bert.modeling_tf_bert import TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST
				from transformers.models.gpta.modeling_tf_gpta import TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST
				from transformers.models.ta.modeling_tf_ta import TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST

if is_torch_available():
				from transformers import (
				    AutoModel,
				    AutoModelForCausalLM,
				    AutoModelForMaskedLM,
				    AutoModelForPreTraining,
				    AutoModelForQuestionAnswering,
				    AutoModelForSeqaSeqLM,
				    AutoModelForSequenceClassification,
				    AutoModelWithLMHead,
				    BertForMaskedLM,
				    BertForPreTraining,
				    BertForQuestionAnswering,
				    BertForSequenceClassification,
				    BertModel,
				    GPTaLMHeadModel,
				    RobertaForMaskedLM,
				    TaForConditionalGeneration,
				)






@is_pt_tf_cross_test
class 						__a (     unittest.TestCase				):



	@slow
	def 		UpperCamelCase							(						self							:    Union[str, Any])->							List[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModel.from_pretrained(snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModel.from_pretrained(snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    List[Any])->							str:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForPreTraining.from_pretrained(snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForPreTraining.from_pretrained(snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    List[str])->							str:
						for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForCausalLM.from_pretrained(snake_case_				,	from_pt=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =TFAutoModelForCausalLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForCausalLM.from_pretrained(snake_case_				,	from_tf=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =AutoModelForCausalLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    Dict)->							Optional[Any]:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelWithLMHead.from_pretrained(snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelWithLMHead.from_pretrained(snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    Dict)->							int:
						for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForMaskedLM.from_pretrained(snake_case_				,	from_pt=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =TFAutoModelForMaskedLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForMaskedLM.from_pretrained(snake_case_				,	from_tf=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =AutoModelForMaskedLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    Dict)->							Dict:
						for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForSeqaSeqLM.from_pretrained(snake_case_				,	from_pt=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =TFAutoModelForSeqaSeqLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForSeqaSeqLM.from_pretrained(snake_case_				,	from_tf=snake_case_)
											__lowerCAmelCase   ,	__lowerCAmelCase	   =AutoModelForSeqaSeqLM.from_pretrained(
											    snake_case_				,	output_loading_info=snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    str)->							Optional[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForSequenceClassification.from_pretrained(snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForSequenceClassification.from_pretrained(snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	@slow
	def 		UpperCamelCase							(						self							:    Optional[int])->							Optional[Any]:
						# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						for model_name in ["bert-base-uncased"]:
											__lowerCAmelCase	   =AutoConfig.from_pretrained(snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =TFAutoModelForQuestionAnswering.from_pretrained(snake_case_				,	from_pt=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)

											__lowerCAmelCase	   =AutoModelForQuestionAnswering.from_pretrained(snake_case_				,	from_tf=snake_case_)
											self.assertIsNotNone(snake_case_)
											self.assertIsInstance(snake_case_				,	snake_case_)



	def 		UpperCamelCase							(						self							:    List[str])->							Union[str, Any]:
						__lowerCAmelCase	   =TFAutoModelWithLMHead.from_pretrained(snake_case_				,	from_pt=snake_case_)
						self.assertIsInstance(snake_case_				,	snake_case_)
						self.assertEqual(model.num_parameters()				,	1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=snake_case_)				,	1_44_10)

						__lowerCAmelCase	   =AutoModelWithLMHead.from_pretrained(snake_case_				,	from_tf=snake_case_)
						self.assertIsInstance(snake_case_				,	snake_case_)
						self.assertEqual(model.num_parameters()				,	1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=snake_case_)				,	1_44_10)



	def 		UpperCamelCase							(						self							:    Optional[int])->							Optional[int]:
						__lowerCAmelCase	   =TFAutoModelWithLMHead.from_pretrained(snake_case_				,	from_pt=snake_case_)
						self.assertIsInstance(snake_case_				,	snake_case_)
						self.assertEqual(model.num_parameters()				,	1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=snake_case_)				,	1_44_10)

						__lowerCAmelCase	   =AutoModelWithLMHead.from_pretrained(snake_case_				,	from_tf=snake_case_)
						self.assertIsInstance(snake_case_				,	snake_case_)
						self.assertEqual(model.num_parameters()				,	1_44_10)
						self.assertEqual(model.num_parameters(only_trainable=snake_case_)				,	1_44_10)









from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowercase_	       =			logging.get_logger(__name__)

lowercase_	       =			{
    '''google/realm-cc-news-pretrained-embedder''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-encoder''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-scorer''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json'''
    ),
    '''google/realm-cc-news-pretrained-openqa''': (
        '''https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json'''
    ),
    '''google/realm-orqa-nq-openqa''': '''https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json''',
    '''google/realm-orqa-nq-reader''': '''https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json''',
    '''google/realm-orqa-wq-openqa''': '''https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json''',
    '''google/realm-orqa-wq-reader''': '''https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json''',
    # See all REALM models at https://huggingface.co/models?filter=realm
}






class 						__a (     SCREAMING_SNAKE_CASE				):
	SCREAMING_SNAKE_CASE       =       "realm"



	def __init__(						self							:    Tuple				,	snake_case_							:    Tuple=3_05_22				,	snake_case_							:    List[Any]=7_68				,	snake_case_							:    Any=1_28				,	snake_case_							:    Union[str, Any]=12				,	snake_case_							:    List[str]=12				,	snake_case_							:    Any=8				,	snake_case_							:    Any=30_72				,	snake_case_							:    int="gelu_new"				,	snake_case_							:    Optional[int]=0.1				,	snake_case_							:    Tuple=0.1				,	snake_case_							:    List[Any]=5_12				,	snake_case_							:    List[Any]=2				,	snake_case_							:    Any=0.0_2				,	snake_case_							:    List[str]=1e-12				,	snake_case_							:    Optional[Any]=2_56				,	snake_case_							:    Dict=10				,	snake_case_							:    str=1e-3				,	snake_case_							:    List[str]=5				,	snake_case_							:    Optional[Any]=3_20				,	snake_case_							:    Optional[Any]=13_35_37_18				,	snake_case_							:    Tuple=50_00				,	snake_case_							:    Union[str, Any]=1				,	snake_case_							:    Union[str, Any]=0				,	snake_case_							:    List[str]=2				,	**snake_case_							:    List[str]				,	)->							str:
						super().__init__(pad_token_id=snake_case_				,	bos_token_id=snake_case_				,	eos_token_id=snake_case_				,	**snake_case_)

						# Common config
						__lowerCAmelCase	   =vocab_size
						__lowerCAmelCase	   =max_position_embeddings
						__lowerCAmelCase	   =hidden_size
						__lowerCAmelCase	   =retriever_proj_size
						__lowerCAmelCase	   =num_hidden_layers
						__lowerCAmelCase	   =num_attention_heads
						__lowerCAmelCase	   =num_candidates
						__lowerCAmelCase	   =intermediate_size
						__lowerCAmelCase	   =hidden_act
						__lowerCAmelCase	   =hidden_dropout_prob
						__lowerCAmelCase	   =attention_probs_dropout_prob
						__lowerCAmelCase	   =initializer_range
						__lowerCAmelCase	   =type_vocab_size
						__lowerCAmelCase	   =layer_norm_eps

						# Reader config
						__lowerCAmelCase	   =span_hidden_size
						__lowerCAmelCase	   =max_span_width
						__lowerCAmelCase	   =reader_layer_norm_eps
						__lowerCAmelCase	   =reader_beam_size
						__lowerCAmelCase	   =reader_seq_len

						# Retrieval config
						__lowerCAmelCase	   =num_block_records
						__lowerCAmelCase	   =searcher_beam_size




