





from __future__ import annotations

import copy
import tempfile
import unittest

from transformers import CONFIG_MAPPING, AutoConfig, BertConfig, GPTaConfig, TaConfig, TapasConfig, is_tf_available
from transformers.testing_utils import (
    DUMMY_UNKNOWN_IDENTIFIER,
    SMALL_MODEL_IDENTIFIER,
    RequestCounter,
    require_tensorflow_probability,
    require_tf,
    slow,
)

from ..bert.test_modeling_bert import BertModelTester


if is_tf_available():
		from transformers import (
		    TFAutoModel,
		    TFAutoModelForCausalLM,
		    TFAutoModelForMaskedLM,
		    TFAutoModelForPreTraining,
		    TFAutoModelForQuestionAnswering,
		    TFAutoModelForSeqaSeqLM,
		    TFAutoModelForSequenceClassification,
		    TFAutoModelForTableQuestionAnswering,
		    TFAutoModelForTokenClassification,
		    TFAutoModelWithLMHead,
		    TFBertForMaskedLM,
		    TFBertForPreTraining,
		    TFBertForQuestionAnswering,
		    TFBertForSequenceClassification,
		    TFBertModel,
		    TFFunnelBaseModel,
		    TFFunnelModel,
		    TFGPTaLMHeadModel,
		    TFRobertaForMaskedLM,
		    TFTaForConditionalGeneration,
		    TFTapasForQuestionAnswering,
		)
		from transformers.models.auto.modeling_tf_auto import (
		    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
		    TF_MODEL_FOR_MASKED_LM_MAPPING,
		    TF_MODEL_FOR_PRETRAINING_MAPPING,
		    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
		    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
		    TF_MODEL_MAPPING,
		)
		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
		from transformers.models.tapas.modeling_tf_tapas import TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST


class 						lowerCamelCase       (__lowerCamelCase      ):







							"""simple docstring"""




							UpperCAmelCase_      						=   "new-model"


if is_tf_available():


		class 						lowerCamelCase       (__lowerCamelCase      ):







									"""simple docstring"""




									UpperCAmelCase_      						=   NewModelConfig


@require_tf
class 						lowerCamelCase       (unittest.TestCase      ):







							"""simple docstring"""




							@slow
							def  A_     ( self     :					Tuple						)				->			int:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Any     =						"bert-base-cased"
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
														self.assertIsNotNone(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

														SCREAMING_SNAKE_CASE__       :      int     =						TFAutoModel.from_pretrained(_UpperCAmelCase						)
														self.assertIsNotNone(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					Tuple						)				->			Any:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						"bert-base-cased"
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
														self.assertIsNotNone(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						TFAutoModelForPreTraining.from_pretrained(_UpperCAmelCase						)
														self.assertIsNotNone(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					Dict						)				->			Optional[Any]:





														"""simple docstring"""
														for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					SCREAMING_SNAKE_CASE__       :      Any     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModelForCausalLM.from_pretrained(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Optional[int]     =						TFAutoModelForCausalLM.from_pretrained(_UpperCAmelCase,			output_loading_info=_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					str						)				->			List[str]:





														"""simple docstring"""
														for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					SCREAMING_SNAKE_CASE__       :      Any     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFAutoModelWithLMHead.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					Tuple						)				->			Tuple:





														"""simple docstring"""
														for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					SCREAMING_SNAKE_CASE__       :      Any     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						TFAutoModelForMaskedLM.from_pretrained(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModelForMaskedLM.from_pretrained(_UpperCAmelCase,			output_loading_info=_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






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





														"""simple docstring"""
														for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					SCREAMING_SNAKE_CASE__       :      Tuple     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						TFAutoModelForSeqaSeqLM.from_pretrained(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Any     =						TFAutoModelForSeqaSeqLM.from_pretrained(_UpperCAmelCase,			output_loading_info=_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					Dict						)				->			str:





														"""simple docstring"""
														# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														for model_name in ["bert-base-uncased"]:
																					SCREAMING_SNAKE_CASE__       :      Any     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModelForSequenceClassification.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							def  A_     ( self     :					List[str]						)				->			Dict:





														"""simple docstring"""
														# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														for model_name in ["bert-base-uncased"]:
																					SCREAMING_SNAKE_CASE__       :      Tuple     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						TFAutoModelForQuestionAnswering.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							@slow
							@require_tensorflow_probability
							def  A_     ( self     :					int						)				->			Dict:





														"""simple docstring"""
														for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
																					SCREAMING_SNAKE_CASE__       :      Any     =						AutoConfig.from_pretrained(_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModelForTableQuestionAnswering.from_pretrained(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModelForTableQuestionAnswering.from_pretrained(
																					    _UpperCAmelCase,			output_loading_info=_UpperCAmelCase						)
																					self.assertIsNotNone(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							def  A_     ( self     :					Any						)				->			Optional[int]:





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Dict     =						TFAutoModelWithLMHead.from_pretrained(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)
														self.assertEqual(model.num_parameters(),			1_4_4_1_0						)
														self.assertEqual(model.num_parameters(only_trainable=_UpperCAmelCase						),			1_4_4_1_0						)






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





														"""simple docstring"""
														SCREAMING_SNAKE_CASE__       :      Dict     =						TFAutoModelWithLMHead.from_pretrained(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)
														self.assertEqual(model.num_parameters(),			1_4_4_1_0						)
														self.assertEqual(model.num_parameters(only_trainable=_UpperCAmelCase						),			1_4_4_1_0						)






							def  A_     ( self     :					Optional[Any]						)				->			str:





														"""simple docstring"""
														# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModel.from_pretrained("sgugger/funnel-random-tiny"						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

														SCREAMING_SNAKE_CASE__       :      int     =						copy.deepcopy(model.config						)
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						["FunnelBaseModel"]
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFAutoModel.from_config(_UpperCAmelCase						)
														self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

														with tempfile.TemporaryDirectory() as tmp_dir:
																					model.save_pretrained(_UpperCAmelCase						)
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFAutoModel.from_pretrained(_UpperCAmelCase						)
																					self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)






							def  A_     ( self     :					List[str]						)				->			Any:





														"""simple docstring"""
														try:
																					AutoConfig.register("new-model",			_UpperCAmelCase						)

																					SCREAMING_SNAKE_CASE__       :      List[str]     =						[
																					    TFAutoModel,
																					    TFAutoModelForCausalLM,
																					    TFAutoModelForMaskedLM,
																					    TFAutoModelForPreTraining,
																					    TFAutoModelForQuestionAnswering,
																					    TFAutoModelForSequenceClassification,
																					    TFAutoModelForTokenClassification,
																					]

																					for auto_class in auto_classes:
																												with self.subTest(auto_class.__name__						):
																																			# Wrong config class will raise an error
																																			with self.assertRaises(_UpperCAmelCase						):
																																										auto_class.register(_UpperCAmelCase,			_UpperCAmelCase						)
																																			auto_class.register(_UpperCAmelCase,			_UpperCAmelCase						)
																																			# Trying to register something existing in the Transformers library will raise an error
																																			with self.assertRaises(_UpperCAmelCase						):
																																										auto_class.register(_UpperCAmelCase,			_UpperCAmelCase						)

																																			# Now that the config is registered, it can be used as any other config with the auto-API
																																			SCREAMING_SNAKE_CASE__       :      Any     =						BertModelTester(self						).get_config()
																																			SCREAMING_SNAKE_CASE__       :      Optional[int]     =						NewModelConfig(**tiny_config.to_dict()						)
																																			SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						auto_class.from_config(_UpperCAmelCase						)
																																			self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

																																			with tempfile.TemporaryDirectory() as tmp_dir:
																																										model.save_pretrained(_UpperCAmelCase						)
																																										SCREAMING_SNAKE_CASE__       :      Optional[int]     =						auto_class.from_pretrained(_UpperCAmelCase						)
																																										self.assertIsInstance(_UpperCAmelCase,			_UpperCAmelCase						)

														finally:
																					if "new-model" in CONFIG_MAPPING._extra_content:
																												del CONFIG_MAPPING._extra_content["new-model"]
																					for mapping in (
																					    TF_MODEL_MAPPING,
																					    TF_MODEL_FOR_PRETRAINING_MAPPING,
																					    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
																					    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
																					    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
																					    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
																					    TF_MODEL_FOR_MASKED_LM_MAPPING,
																					):
																												if NewModelConfig in mapping._extra_content:
																																			del mapping._extra_content[NewModelConfig]






							def  A_     ( self     :					List[Any]						)				->			List[str]:





														"""simple docstring"""
														with self.assertRaisesRegex(
														    _UpperCAmelCase,			"bert-base is not a local folder and is not a valid model identifier"						):
																					SCREAMING_SNAKE_CASE__       :      Optional[int]     =						TFAutoModel.from_pretrained("bert-base"						)






							def  A_     ( self     :					List[Any]						)				->			int:





														"""simple docstring"""
														with self.assertRaisesRegex(
														    _UpperCAmelCase,			r"aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)"						):
																					SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						TFAutoModel.from_pretrained(_UpperCAmelCase,			revision="aaaaaa"						)






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





														"""simple docstring"""
														with self.assertRaisesRegex(
														    _UpperCAmelCase,			"hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin",			):
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFAutoModel.from_pretrained("hf-internal-testing/config-no-model"						)






							def  A_     ( self     :					Tuple						)				->			int:





														"""simple docstring"""
														with self.assertRaisesRegex(_UpperCAmelCase,			"Use `from_pt=True` to load this model"						):
																					SCREAMING_SNAKE_CASE__       :      Dict     =						TFAutoModel.from_pretrained("hf-internal-testing/tiny-bert-pt-only"						)






							def  A_     ( self     :					int						)				->			Any:





														"""simple docstring"""
														# Make sure we have cached the model.
														SCREAMING_SNAKE_CASE__       :      Optional[Any]     =						TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"						)
														with RequestCounter() as counter:
																					SCREAMING_SNAKE_CASE__       :      str     =						TFAutoModel.from_pretrained("hf-internal-testing/tiny-random-bert"						)
																					self.assertEqual(counter.get_request_count,			0						)
																					self.assertEqual(counter.head_request_count,			1						)
																					self.assertEqual(counter.other_request_count,			0						)

														# With a sharded checkpoint
														SCREAMING_SNAKE_CASE__       :      List[Any]     =						TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"						)
														with RequestCounter() as counter:
																					SCREAMING_SNAKE_CASE__       :      int     =						TFAutoModel.from_pretrained("ArthurZ/tiny-random-bert-sharded"						)
																					self.assertEqual(counter.get_request_count,			0						)
																					self.assertEqual(counter.head_request_count,			1						)
																					self.assertEqual(counter.other_request_count,			0						)













from queue import PriorityQueue
from typing import Any

import numpy as np




def _a (  SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  set			,				SCREAMING_SNAKE_CASE__      :  set			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  PriorityQueue			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  float | int			,				)	->    float | int:

							'''simple docstring'''






							for nxt, d in graph[v]:
														if nxt in visited_forward:
																					continue
														SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						cst_fwd.get(SCREAMING_SNAKE_CASE__			,				np.inf       )
														SCREAMING_SNAKE_CASE__       :      Optional[int]     =						cst_fwd[v] + d
														if new_cost_f < old_cost_f:
																					queue.put((new_cost_f, nxt)       )
																					SCREAMING_SNAKE_CASE__       :      List[Any]     =						new_cost_f
																					SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						v
														if nxt in visited_backward:
																					if cst_fwd[v] + d + cst_bwd[nxt] < shortest_distance:
																												SCREAMING_SNAKE_CASE__       :      Optional[int]     =						cst_fwd[v] + d + cst_bwd[nxt]
							return shortest_distance




def _a (  SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  str			,				SCREAMING_SNAKE_CASE__      :  dict			,				SCREAMING_SNAKE_CASE__      :  dict       )	->    int:

							'''simple docstring'''






							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						-1

							SCREAMING_SNAKE_CASE__       :      List[str]     =						set()
							SCREAMING_SNAKE_CASE__       :      List[Any]     =						set()
							SCREAMING_SNAKE_CASE__       :      int     =						{source: 0}
							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						{destination: 0}
							SCREAMING_SNAKE_CASE__       :      List[Any]     =						{source: None}
							SCREAMING_SNAKE_CASE__       :      Union[str, Any]     =						{destination: None}
							SCREAMING_SNAKE_CASE__       :      PriorityQueue[Any]     =						PriorityQueue()
							SCREAMING_SNAKE_CASE__       :      PriorityQueue[Any]     =						PriorityQueue()

							SCREAMING_SNAKE_CASE__       :      Dict     =						np.inf

							queue_forward.put((0, source)       )
							queue_backward.put((0, destination)       )

							if source == destination:
														return 0

							while not queue_forward.empty() and not queue_backward.empty():
														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      List[str]     =						queue_forward.get()
														visited_forward.add(SCREAMING_SNAKE_CASE__       )

														SCREAMING_SNAKE_CASE__   ,SCREAMING_SNAKE_CASE__       :      List[Any]     =						queue_backward.get()
														visited_backward.add(SCREAMING_SNAKE_CASE__       )

														SCREAMING_SNAKE_CASE__       :      Any     =						pass_and_relaxation(
														    SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				)

														SCREAMING_SNAKE_CASE__       :      List[Any]     =						pass_and_relaxation(
														    SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				SCREAMING_SNAKE_CASE__			,				)

														if cst_fwd[v_fwd] + cst_bwd[v_bwd] >= shortest_distance:
																					break

							if shortest_distance != np.inf:
														SCREAMING_SNAKE_CASE__       :      int     =						shortest_distance
							return shortest_path_distance


_lowerCamelCase :    Optional[Any]  							= {
    '''B''': [['''C''', 1]],
    '''C''': [['''D''', 1]],
    '''D''': [['''F''', 1]],
    '''E''': [['''B''', 1], ['''G''', 2]],
    '''F''': [],
    '''G''': [['''F''', 1]],
}
_lowerCamelCase :    Tuple  							= {
    '''B''': [['''E''', 1]],
    '''C''': [['''B''', 1]],
    '''D''': [['''C''', 1]],
    '''F''': [['''D''', 1], ['''G''', 1]],
    '''E''': [[None, np.inf]],
    '''G''': [['''E''', 2]],
}

if __name__ == "__main__":
		import doctest

		doctest.testmod()






