


"""simple docstring"""
import tempfile
import unittest

from transformers import SPIECE_UNDERLINE, BatchEncoding, PLBartTokenizer, is_torch_available
from transformers.testing_utils import (
    get_tests_dir,
    nested_simplify,
    require_sentencepiece,
    require_tokenizers,
    require_torch,
)

from ...test_tokenization_common import TokenizerTesterMixin


SCREAMING_SNAKE_CASE :    Dict									=				get_tests_dir("""fixtures/test_sentencepiece.model""")


if is_torch_available():
			from transformers.models.plbart.modeling_plbart import shift_tokens_right

SCREAMING_SNAKE_CASE :    Optional[int]									=				5_0003
SCREAMING_SNAKE_CASE :    str									=				5_0002



@require_sentencepiece
@require_tokenizers
class 			_UpperCAmelCase (							__snake_case,				unittest.TestCase				):






		'''simple docstring'''

		lowerCamelCase__				=PLBartTokenizer
		lowerCamelCase__				=None
		lowerCamelCase__				=False



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			super().setUp()

			# We have a SentencePiece fixture for testing
			__snake_case	:						List[Any]						=				PLBartTokenizer(a_				,       language_codes='''base'''				,       keep_accents=a_					)
			tokenizer.save_pretrained(self.tmpdirname					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				PLBartTokenizer(a_				,       language_codes='''base'''				,       keep_accents=a_					)

			__snake_case	:						List[Any]						=				tokenizer.tokenize('''This is a test'''					)
			self.assertListEqual(a_				,       ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']					)

			self.assertListEqual(
			    tokenizer.convert_tokens_to_ids(a_					)				,       [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]]				,       )

			__snake_case	:						Union[str, Any]						=				tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
			self.assertListEqual(
			    a_				,       [
			        SPIECE_UNDERLINE + '''I''',
			        SPIECE_UNDERLINE + '''was''',
			        SPIECE_UNDERLINE + '''b''',
			        '''or''',
			        '''n''',
			        SPIECE_UNDERLINE + '''in''',
			        SPIECE_UNDERLINE + '''''',
			        '''9''',
			        '''2''',
			        '''0''',
			        '''0''',
			        '''0''',
			        ''',''',
			        SPIECE_UNDERLINE + '''and''',
			        SPIECE_UNDERLINE + '''this''',
			        SPIECE_UNDERLINE + '''is''',
			        SPIECE_UNDERLINE + '''f''',
			        '''al''',
			        '''s''',
			        '''é''',
			        '''.''',
			    ]				,       )
			__snake_case	:						Dict						=				tokenizer.convert_tokens_to_ids(a_					)
			self.assertListEqual(
			    a_				,       [
			        value + tokenizer.fairseq_offset
			        for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4]
			    ]				,       )

			__snake_case	:						List[str]						=				tokenizer.convert_ids_to_tokens(a_					)
			self.assertListEqual(
			    a_				,       [
			        SPIECE_UNDERLINE + '''I''',
			        SPIECE_UNDERLINE + '''was''',
			        SPIECE_UNDERLINE + '''b''',
			        '''or''',
			        '''n''',
			        SPIECE_UNDERLINE + '''in''',
			        SPIECE_UNDERLINE + '''''',
			        '''<unk>''',
			        '''2''',
			        '''0''',
			        '''0''',
			        '''0''',
			        ''',''',
			        SPIECE_UNDERLINE + '''and''',
			        SPIECE_UNDERLINE + '''this''',
			        SPIECE_UNDERLINE + '''is''',
			        SPIECE_UNDERLINE + '''f''',
			        '''al''',
			        '''s''',
			        '''<unk>''',
			        '''.''',
			    ]				,       )

			__snake_case	:						List[Any]						=				tokenizer.vocab_size
			__snake_case	:						List[Any]						=				[tokenizer.convert_ids_to_tokens(a_					) for x in range(end - 4				,       a_					)]

			self.assertListEqual(a_				,       ['''__java__''', '''__python__''', '''__en_XX__''', '''<mask>''']					)

			__snake_case	:						int						=				'''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go'''
			__snake_case	:						List[str]						=				tokenizer(a_					).input_ids
			self.assertEqual(
			    tokenizer.decode(a_				,       skip_special_tokens=a_				,       clean_up_tokenization_spaces=a_					)				,       a_				,       )



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				PLBartTokenizer(a_				,       language_codes='''multi'''				,       keep_accents=a_					)

			__snake_case	:						int						=				tokenizer.tokenize('''This is a test'''					)
			self.assertListEqual(a_				,       ['''▁This''', '''▁is''', '''▁a''', '''▁t''', '''est''']					)

			self.assertListEqual(
			    tokenizer.convert_tokens_to_ids(a_					)				,       [value + tokenizer.fairseq_offset for value in [2_85, 46, 10, 1_70, 3_82]]				,       )

			__snake_case	:						str						=				tokenizer.tokenize('''I was born in 92000, and this is falsé.'''					)
			self.assertListEqual(
			    a_				,       [
			        SPIECE_UNDERLINE + '''I''',
			        SPIECE_UNDERLINE + '''was''',
			        SPIECE_UNDERLINE + '''b''',
			        '''or''',
			        '''n''',
			        SPIECE_UNDERLINE + '''in''',
			        SPIECE_UNDERLINE + '''''',
			        '''9''',
			        '''2''',
			        '''0''',
			        '''0''',
			        '''0''',
			        ''',''',
			        SPIECE_UNDERLINE + '''and''',
			        SPIECE_UNDERLINE + '''this''',
			        SPIECE_UNDERLINE + '''is''',
			        SPIECE_UNDERLINE + '''f''',
			        '''al''',
			        '''s''',
			        '''é''',
			        '''.''',
			    ]				,       )
			__snake_case	:						Union[str, Any]						=				tokenizer.convert_tokens_to_ids(a_					)
			self.assertListEqual(
			    a_				,       [
			        value + tokenizer.fairseq_offset
			        for value in [8, 21, 84, 55, 24, 19, 7, 2, 6_02, 3_47, 3_47, 3_47, 3, 12, 66, 46, 72, 80, 6, 2, 4]
			    ]				,       )

			__snake_case	:						Any						=				tokenizer.convert_ids_to_tokens(a_					)
			self.assertListEqual(
			    a_				,       [
			        SPIECE_UNDERLINE + '''I''',
			        SPIECE_UNDERLINE + '''was''',
			        SPIECE_UNDERLINE + '''b''',
			        '''or''',
			        '''n''',
			        SPIECE_UNDERLINE + '''in''',
			        SPIECE_UNDERLINE + '''''',
			        '''<unk>''',
			        '''2''',
			        '''0''',
			        '''0''',
			        '''0''',
			        ''',''',
			        SPIECE_UNDERLINE + '''and''',
			        SPIECE_UNDERLINE + '''this''',
			        SPIECE_UNDERLINE + '''is''',
			        SPIECE_UNDERLINE + '''f''',
			        '''al''',
			        '''s''',
			        '''<unk>''',
			        '''.''',
			    ]				,       )
			__snake_case	:						List[Any]						=				tokenizer.vocab_size
			__snake_case	:						List[Any]						=				[tokenizer.convert_ids_to_tokens(a_					) for x in range(end - 7				,       a_					)]

			self.assertListEqual(
			    a_				,       ['''__java__''', '''__python__''', '''__en_XX__''', '''__javascript__''', '''__php__''', '''__ruby__''', '''__go__''']					)
			__snake_case	:						Dict						=				'''java.lang.Exception, python.lang.Exception, javascript, php, ruby, go'''
			__snake_case	:						int						=				tokenizer(a_					).input_ids
			self.assertEqual(
			    tokenizer.decode(a_				,       skip_special_tokens=a_				,       clean_up_tokenization_spaces=a_					)				,       a_				,       )



@require_torch
@require_sentencepiece
@require_tokenizers
class 			_UpperCAmelCase (							unittest.TestCase				):






		'''simple docstring'''

		lowerCamelCase__				='uclanlp/plbart-python-en_XX'
		lowerCamelCase__				=[
		    'def maximum(a,b,c):NEW_LINE_INDENTreturn max([a,b,c])',
		    'def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])',
		]
		lowerCamelCase__				=[
		    'Returns the maximum value of a b c.',
		    'Sums the values of a b c.',
		]
		lowerCamelCase__				=[
		    134,
		    5452,
		    33460,
		    33441,
		    33463,
		    33465,
		    33463,
		    33449,
		    988,
		    20,
		    33456,
		    19,
		    33456,
		    771,
		    39,
		    4258,
		    889,
		    3318,
		    33441,
		    33463,
		    33465,
		    33463,
		    33449,
		    2471,
		    2,
		    PYTHON_CODE,
		]



		@classmethod
		def 				SCREAMING_SNAKE_CASE  (cls					):





			'''simple docstring'''

			__snake_case	:						PLBartTokenizer						=				PLBartTokenizer.from_pretrained(
			    cls.checkpoint_name				,       language_codes='''base'''				,       src_lang='''python'''				,       tgt_lang='''en_XX'''					)
			__snake_case	:						Optional[int]						=				1
			return cls



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__java__''']				,       5_00_01					)
			self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__python__''']				,       5_00_02					)
			self.assertEqual(self.tokenizer.fairseq_tokens_to_ids['''__en_XX__''']				,       5_00_03					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						int						=				self.tokenizer.batch_encode_plus(self.src_text					).input_ids[0]
			self.assertListEqual(self.expected_src_tokens				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.assertIn(a_				,       self.tokenizer.all_special_ids					)
			__snake_case	:						Optional[int]						=				[EN_CODE, 90_37, 3_34_42, 57, 7_52, 1_53, 14, 56, 18, 9, 2]
			__snake_case	:						Tuple						=				self.tokenizer.decode(a_				,       skip_special_tokens=a_					)
			__snake_case	:						Optional[int]						=				self.tokenizer.decode(generated_ids[1:]				,       skip_special_tokens=a_					)
			self.assertEqual(a_				,       a_					)
			self.assertNotIn(self.tokenizer.eos_token				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Tuple						=				['''def sum(a,b,c):NEW_LINE_INDENTreturn sum([a,b,c])''' * 20]
			self.assertIsInstance(src_text[0]				,       a_					)
			__snake_case	:						int						=				10
			__snake_case	:						Optional[Any]						=				self.tokenizer(a_				,       max_length=a_				,       truncation=a_					).input_ids[0]
			self.assertEqual(ids[-2]				,       2					)
			self.assertEqual(ids[-1]				,       a_					)
			self.assertEqual(len(a_					)				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			self.assertListEqual(self.tokenizer.convert_tokens_to_ids(['''<mask>''', '''__java__''']					)				,       [5_00_04, 5_00_01]					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Union[str, Any]						=				tempfile.mkdtemp()
			__snake_case	:						str						=				self.tokenizer.fairseq_tokens_to_ids
			self.tokenizer.save_pretrained(a_					)
			__snake_case	:						List[str]						=				PLBartTokenizer.from_pretrained(a_					)
			self.assertDictEqual(new_tok.fairseq_tokens_to_ids				,       a_					)



		@require_torch
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				self.tokenizer(self.src_text				,       text_target=self.tgt_text				,       padding=a_				,       return_tensors='''pt'''					)
			__snake_case	:						Optional[int]						=				shift_tokens_right(batch['''labels''']				,       self.tokenizer.pad_token_id					)

			# fairseq batch: https://gist.github.com/sshleifer/cba08bc2109361a74ac3760a7e30e4f4
			self.assertEqual(batch.input_ids[1][-2:].tolist()				,       [2, PYTHON_CODE]					)
			self.assertEqual(batch.decoder_input_ids[1][0]				,       a_					)
			self.assertEqual(batch.decoder_input_ids[1][-1]				,       2					)
			self.assertEqual(batch.labels[1][-2:].tolist()				,       [2, EN_CODE]					)



		@require_torch
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				self.tokenizer(
			    self.src_text				,       text_target=self.tgt_text				,       padding=a_				,       truncation=a_				,       max_length=len(self.expected_src_tokens					)				,       return_tensors='''pt'''				,       )
			__snake_case	:						List[str]						=				shift_tokens_right(batch['''labels''']				,       self.tokenizer.pad_token_id					)

			self.assertIsInstance(a_				,       a_					)

			self.assertEqual((2, 26)				,       batch.input_ids.shape					)
			self.assertEqual((2, 26)				,       batch.attention_mask.shape					)
			__snake_case	:						Optional[Any]						=				batch.input_ids.tolist()[0]
			self.assertListEqual(self.expected_src_tokens				,       a_					)
			self.assertEqual(2				,       batch.decoder_input_ids[0, -1]					)  # EOS
			# Test that special tokens are reset
			self.assertEqual(self.tokenizer.prefix_tokens				,       []					)
			self.assertEqual(self.tokenizer.suffix_tokens				,       [self.tokenizer.eos_token_id, PYTHON_CODE]					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						str						=				self.tokenizer(self.src_text				,       padding=a_				,       truncation=a_				,       max_length=3				,       return_tensors='''pt'''					)
			__snake_case	:						Tuple						=				self.tokenizer(
			    text_target=self.tgt_text				,       padding=a_				,       truncation=a_				,       max_length=10				,       return_tensors='''pt'''					)
			__snake_case	:						Optional[int]						=				targets['''input_ids''']
			__snake_case	:						Optional[Any]						=				shift_tokens_right(a_				,       self.tokenizer.pad_token_id					)

			self.assertEqual(batch.input_ids.shape[1]				,       3					)
			self.assertEqual(batch.decoder_input_ids.shape[1]				,       10					)



		@require_torch
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						List[str]						=				self.tokenizer._build_translation_inputs(
			    '''A test'''				,       return_tensors='''pt'''				,       src_lang='''en_XX'''				,       tgt_lang='''java'''					)

			self.assertEqual(
			    nested_simplify(a_					)				,       {
			        # A, test, EOS, en_XX
			        '''input_ids''': [[1_50, 2_42, 2, 5_00_03]],
			        '''attention_mask''': [[1, 1, 1, 1]],
			        # java
			        '''forced_bos_token_id''': 5_00_01,
			    }				,       )









"""simple docstring"""
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 			_UpperCAmelCase (							__snake_case				):






		'''simple docstring'''

		lowerCamelCase__				='new-model'


if is_tf_available():



			class 			_UpperCAmelCase (							__snake_case				):






					'''simple docstring'''

					lowerCamelCase__				=NewModelConfig



@require_tf
class 			_UpperCAmelCase (							unittest.TestCase				):






		'''simple docstring'''

		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				'''bert-base-cased'''
			__snake_case	:						Dict						=				AutoConfig.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						int						=				TFAutoModel.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[int]						=				'''bert-base-cased'''
			__snake_case	:						Optional[Any]						=				AutoConfig.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						List[Any]						=				TFAutoModelForPreTraining.from_pretrained(a_					)
			self.assertIsNotNone(a_					)
			self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						int						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Dict						=				TFAutoModelForCausalLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						int						=				TFAutoModelForCausalLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						Tuple						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Tuple						=				TFAutoModelWithLMHead.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						List[str]						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Optional[Any]						=				TFAutoModelForMaskedLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						int						=				TFAutoModelForMaskedLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				__snake_case	:						int						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						List[Any]						=				TFAutoModelForSeqaSeqLM.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						Union[str, Any]						=				TFAutoModelForSeqaSeqLM.from_pretrained(a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in ["bert-base-uncased"]:
				__snake_case	:						Any						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Dict						=				TFAutoModelForSequenceClassification.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in ["bert-base-uncased"]:
				__snake_case	:						Optional[int]						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						Optional[Any]						=				TFAutoModelForQuestionAnswering.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		@slow
		@require_tensorflow_probability
		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
				__snake_case	:						Dict						=				AutoConfig.from_pretrained(a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)

				__snake_case	:						List[str]						=				TFAutoModelForTableQuestionAnswering.from_pretrained(a_					)
				__snake_case	,							__snake_case	:						Dict						=				TFAutoModelForTableQuestionAnswering.from_pretrained(
				    a_				,       output_loading_info=a_					)
				self.assertIsNotNone(a_					)
				self.assertIsInstance(a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Any						=				TFAutoModelWithLMHead.from_pretrained(a_					)
			self.assertIsInstance(a_				,       a_					)
			self.assertEqual(model.num_parameters()				,       1_44_10					)
			self.assertEqual(model.num_parameters(only_trainable=a_					)				,       1_44_10					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Union[str, Any]						=				TFAutoModelWithLMHead.from_pretrained(a_					)
			self.assertIsInstance(a_				,       a_					)
			self.assertEqual(model.num_parameters()				,       1_44_10					)
			self.assertEqual(model.num_parameters(only_trainable=a_					)				,       1_44_10					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''sgugger/funnel-random-tiny'''					)
			self.assertIsInstance(a_				,       a_					)

			__snake_case	:						Optional[Any]						=				copy.deepcopy(model.config					)
			__snake_case	:						int						=				['''FunnelBaseModel''']
			__snake_case	:						Any						=				TFAutoModel.from_config(a_					)
			self.assertIsInstance(a_				,       a_					)

			with tempfile.TemporaryDirectory() as tmp_dir:
				model.save_pretrained(a_					)
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained(a_					)
				self.assertIsInstance(a_				,       a_					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			try:
				AutoConfig.register('''new-model'''				,       a_					)

				__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(a_					):
							auto_class.register(a_				,       a_					)
						auto_class.register(a_				,       a_					)
						# Trying to register something existing in the Transformers library will raise an error
						with self.assertRaises(a_					):
							auto_class.register(a_				,       a_					)

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

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

			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 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       '''bert-base is not a local folder and is not a valid model identifier'''					):
				__snake_case	:						Any						=				TFAutoModel.from_pretrained('''bert-base'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       R'''aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'''					):
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained(a_				,       revision='''aaaaaa'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(
			    a_				,       '''hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'''				,       ):
				__snake_case	:						Any						=				TFAutoModel.from_pretrained('''hf-internal-testing/config-no-model'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			with self.assertRaisesRegex(a_				,       '''Use `from_pt=True` to load this model'''					):
				__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''hf-internal-testing/tiny-bert-pt-only'''					)



		def 				SCREAMING_SNAKE_CASE  (self					):





			'''simple docstring'''

			__snake_case	:						Optional[Any]						=				TFAutoModel.from_pretrained('''hf-internal-testing/tiny-random-bert'''					)
			with RequestCounter() as counter:
				__snake_case	:						List[Any]						=				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
			__snake_case	:						Dict						=				TFAutoModel.from_pretrained('''ArthurZ/tiny-random-bert-sharded'''					)
			with RequestCounter() as counter:
				__snake_case	:						Dict						=				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					)





