"""simple docstring"""







import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    WavaVecaConfig,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaForCTC,
    WavaVecaForPreTraining,
    WavaVecaProcessor,
    logging,
)
from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification


logging.set_verbosity_info()
_a					:					Dict=			logging.get_logger(__name__)

_a					:					int=			{
    "post_extract_proj": "feature_projection.projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "self_attn.k_proj": "encoder.layers.*.attention.k_proj",
    "self_attn.v_proj": "encoder.layers.*.attention.v_proj",
    "self_attn.q_proj": "encoder.layers.*.attention.q_proj",
    "self_attn.out_proj": "encoder.layers.*.attention.out_proj",
    "self_attn_layer_norm": "encoder.layers.*.layer_norm",
    "fc1": "encoder.layers.*.feed_forward.intermediate_dense",
    "fc2": "encoder.layers.*.feed_forward.output_dense",
    "final_layer_norm": "encoder.layers.*.final_layer_norm",
    "encoder.layer_norm": "encoder.layer_norm",
    "adapter_layer": "encoder.layers.*.adapter_layer",
    "w2v_model.layer_norm": "feature_projection.layer_norm",
    "quantizer.weight_proj": "quantizer.weight_proj",
    "quantizer.vars": "quantizer.codevectors",
    "project_q": "project_q",
    "final_proj": "project_hid",
    "w2v_encoder.proj": "lm_head",
    "mask_emb": "masked_spec_embed",
    "pooling_layer.linear": "projector",
    "pooling_layer.projection": "classifier",
}
_a					:					Tuple=			[
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
    "projector",
    "classifier",
]
def   __UpperCAmelCase    (   UpperCAmelCase_   :    str  )							->      str:

	'''simple docstring'''





	__snake_case      :							List[str]						=			{}
	with open(UpperCAmelCase_      ,							'r'  ) as file:
		for line_number, line in enumerate(UpperCAmelCase_  ):
			__snake_case      :							str						=			line.strip()
			if line:
				__snake_case      :							str						=			line.split()
				__snake_case      :							Optional[int]						=			line_number
				__snake_case      :							Dict						=			words[0]
				__snake_case      :							Optional[int]						=			value
	return result
def   __UpperCAmelCase    (   UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    Union[str, Any]      ,							UpperCAmelCase_   :    Any      ,							UpperCAmelCase_   :    Any      ,							UpperCAmelCase_   :    Any  )							->      str:

	'''simple docstring'''





	for attribute in key.split('.'  ):
		__snake_case      :							str						=			getattr(UpperCAmelCase_      ,							UpperCAmelCase_  )

	__snake_case      :							str						=			None
	for param_key in PARAM_MAPPING.keys():
		if full_name.endswith(UpperCAmelCase_  ):
			__snake_case      :							str						=			PARAM_MAPPING[full_name.split('.'  )[-1]]
			__snake_case      :							Optional[Any]						=			'param'

	if weight_type is not None and weight_type != "param":
		__snake_case      :							Optional[Any]						=			getattr(UpperCAmelCase_      ,							UpperCAmelCase_  ).shape
	elif weight_type is not None and weight_type == "param":
		__snake_case      :							Tuple						=			hf_pointer
		for attribute in hf_param_name.split('.'  ):
			__snake_case      :							Any						=			getattr(UpperCAmelCase_      ,							UpperCAmelCase_  )
		__snake_case      :							str						=			shape_pointer.shape

		# let's reduce dimension
		__snake_case      :							List[Any]						=			value[0]
	else:
		__snake_case      :							Union[str, Any]						=			hf_pointer.shape

	if hf_shape != value.shape:
		raise ValueError(
		    F"Shape of hf {key + '.' + weight_type if weight_type is not None else ''} is {hf_shape}, but should be"
		    F" {value.shape} for {full_name}"  )

	if weight_type == "weight":
		__snake_case      :							int						=			value
	elif weight_type == "weight_g":
		__snake_case      :							Optional[int]						=			value
	elif weight_type == "weight_v":
		__snake_case      :							Optional[int]						=			value
	elif weight_type == "bias":
		__snake_case      :							List[Any]						=			value
	elif weight_type == "param":
		for attribute in hf_param_name.split('.'  ):
			__snake_case      :							int						=			getattr(UpperCAmelCase_      ,							UpperCAmelCase_  )
		__snake_case      :							Optional[Any]						=			value
	else:
		__snake_case      :							Union[str, Any]						=			value

	logger.info(F"{key + '.' + weight_type if weight_type is not None else ''} was initialized from {full_name}."  )
def   __UpperCAmelCase    (   UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    List[str]      ,							UpperCAmelCase_   :    Optional[Any]      ,							UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    List[str]  )							->      Dict:

	'''simple docstring'''





	__snake_case      :							str						=			None
	for param_key in PARAM_MAPPING.keys():
		if full_name.endswith(UpperCAmelCase_  ):
			__snake_case      :							List[str]						=			PARAM_MAPPING[full_name.split('.'  )[-1]]
			__snake_case      :							Optional[int]						=			'param'

	if weight_type is not None and weight_type != "param":
		__snake_case      :							Dict						=			'.'.join([key, weight_type]  )
	elif weight_type is not None and weight_type == "param":
		__snake_case      :							int						=			'.'.join([key, hf_param_name]  )
	else:
		__snake_case      :							Tuple						=			key

	__snake_case      :							Optional[int]						=			value if 'lm_head' in full_key else value[0]


_a					:					str=			{
    "W_a": "linear_1.weight",
    "W_b": "linear_2.weight",
    "b_a": "linear_1.bias",
    "b_b": "linear_2.bias",
    "ln_W": "norm.weight",
    "ln_b": "norm.bias",
}
def   __UpperCAmelCase    (   UpperCAmelCase_   :    List[Any]      ,							UpperCAmelCase_   :    List[str]      ,							UpperCAmelCase_   :    str=None      ,							UpperCAmelCase_   :    List[Any]=None  )							->      Optional[Any]:

	'''simple docstring'''





	__snake_case      :							Optional[Any]						=			False
	for key, mapped_key in MAPPING.items():
		__snake_case      :							Optional[Any]						=			'wav2vec2.' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
		if key in name or key.split('w2v_model.'  )[-1] == name.split('.'  )[0]:
			__snake_case      :							Tuple						=			True
			if "*" in mapped_key:
				__snake_case      :							Optional[Any]						=			name.split(UpperCAmelCase_  )[0].split('.'  )[-2]
				__snake_case      :							Union[str, Any]						=			mapped_key.replace('*'      ,							UpperCAmelCase_  )
			if "weight_g" in name:
				__snake_case      :							Optional[Any]						=			'weight_g'
			elif "weight_v" in name:
				__snake_case      :							int						=			'weight_v'
			elif "bias" in name:
				__snake_case      :							str						=			'bias'
			elif "weight" in name:
				# TODO: don't match quantizer.weight_proj
				__snake_case      :							Optional[int]						=			'weight'
			else:
				__snake_case      :							Union[str, Any]						=			None
			if hf_dict is not None:
				rename_dict(UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_  )
			else:
				set_recursively(UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_  )
			return is_used
	return is_used
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Dict      ,							UpperCAmelCase_   :    Optional[Any]      ,							UpperCAmelCase_   :    Dict  )							->      Optional[Any]:

	'''simple docstring'''





	__snake_case      :							Tuple						=			[]
	__snake_case      :							Union[str, Any]						=			fairseq_model.state_dict()

	__snake_case      :							Union[str, Any]						=			hf_model.wavaveca.feature_extractor

	for name, value in fairseq_dict.items():
		__snake_case      :							int						=			False
		if "conv_layers" in name:
			load_conv_layer(
			    UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_      ,							hf_model.config.feat_extract_norm == 'group'      ,							)
			__snake_case      :							List[str]						=			True
		else:
			__snake_case      :							int						=			load_wavaveca_layer(UpperCAmelCase_      ,							UpperCAmelCase_      ,							UpperCAmelCase_  )
		if not is_used:
			unused_weights.append(UpperCAmelCase_  )

	logger.warning(F"Unused weights: {unused_weights}"  )
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Any      ,							UpperCAmelCase_   :    Optional[Any]      ,							UpperCAmelCase_   :    int      ,							UpperCAmelCase_   :    Optional[Any]      ,							UpperCAmelCase_   :    List[str]  )							->      Optional[Any]:

	'''simple docstring'''





	__snake_case      :							Any						=			full_name.split('conv_layers.'  )[-1]
	__snake_case      :							Tuple						=			name.split('.'  )
	__snake_case      :							Optional[Any]						=			int(items[0]  )
	__snake_case      :							List[Any]						=			int(items[1]  )

	if type_id == 0:
		if "bias" in name:
			if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
				raise ValueError(
				    F"{full_name} has size {value.shape}, but"
				    F" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found."  )
			__snake_case      :							int						=			value
			logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."  )
		elif "weight" in name:
			if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
				raise ValueError(
				    F"{full_name} has size {value.shape}, but"
				    F" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found."  )
			__snake_case      :							int						=			value
			logger.info(F"Feat extract conv layer {layer_id} was initialized from {full_name}."  )
	elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
		if "bias" in name:
			if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
				raise ValueError(
				    F"{full_name} has size {value.shape}, but"
				    F" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found."  )
			__snake_case      :							str						=			value
			logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."  )
		elif "weight" in name:
			if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
				raise ValueError(
				    F"{full_name} has size {value.shape}, but"
				    F" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found."  )
			__snake_case      :							List[Any]						=			value
			logger.info(F"Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."  )
	else:
		unused_weights.append(UpperCAmelCase_  )
@torch.no_grad()
def   __UpperCAmelCase    (   UpperCAmelCase_   :    Dict      ,							UpperCAmelCase_   :    Tuple      ,							UpperCAmelCase_   :    str=None      ,							UpperCAmelCase_   :    Optional[Any]=None      ,							UpperCAmelCase_   :    Dict=True      ,							UpperCAmelCase_   :    Union[str, Any]=False  )							->      Tuple:

	'''simple docstring'''





	if config_path is not None:
		__snake_case      :							Optional[int]						=			WavaVecaConfig.from_pretrained(UpperCAmelCase_  )
	else:
		__snake_case      :							Dict						=			WavaVecaConfig()

	if is_seq_class:
		__snake_case      :							Any						=			read_txt_into_dict(UpperCAmelCase_  )
		__snake_case      :							int						=			idalabel
		__snake_case      :							List[Any]						=			WavaVecaForSequenceClassification(UpperCAmelCase_  )
		__snake_case      :							int						=			WavaVecaFeatureExtractor(
		    feature_size=1      ,							sampling_rate=1_60_00      ,							padding_value=0      ,							do_normalize=UpperCAmelCase_      ,							return_attention_mask=UpperCAmelCase_      ,							)
		feature_extractor.save_pretrained(UpperCAmelCase_  )

	elif is_finetuned:
		if dict_path:
			__snake_case      :							Any						=			Dictionary.load(UpperCAmelCase_  )

			# important change bos & pad token id since CTC symbol is <pad> and
			# not <s> as in fairseq
			__snake_case      :							List[Any]						=			target_dict.pad_index
			__snake_case      :							int						=			target_dict.bos_index
			__snake_case      :							int						=			target_dict.eos_index
			__snake_case      :							str						=			len(target_dict.symbols  )
			__snake_case      :							List[Any]						=			os.path.join(UpperCAmelCase_      ,							'vocab.json'  )
			if not os.path.isdir(UpperCAmelCase_  ):
				logger.error('--pytorch_dump_folder_path ({}) should be a directory'.format(UpperCAmelCase_  )  )
				return
			os.makedirs(UpperCAmelCase_      ,							exist_ok=UpperCAmelCase_  )
			__snake_case      :							int						=			target_dict.indices

			# fairseq has the <pad> and <s> switched
			__snake_case      :							Optional[int]						=			0
			__snake_case      :							str						=			1
			with open(UpperCAmelCase_      ,							'w'      ,							encoding='utf-8'  ) as vocab_handle:
				json.dump(UpperCAmelCase_      ,							UpperCAmelCase_  )
			__snake_case      :							Union[str, Any]						=			WavaVecaCTCTokenizer(
			    UpperCAmelCase_      ,							unk_token=target_dict.unk_word      ,							pad_token=target_dict.pad_word      ,							bos_token=target_dict.bos_word      ,							eos_token=target_dict.eos_word      ,							word_delimiter_token='|'      ,							do_lower_case=UpperCAmelCase_      ,							)
			__snake_case      :							Optional[Any]						=			True if config.feat_extract_norm == 'layer' else False
			__snake_case      :							int						=			WavaVecaFeatureExtractor(
			    feature_size=1      ,							sampling_rate=1_60_00      ,							padding_value=0      ,							do_normalize=UpperCAmelCase_      ,							return_attention_mask=UpperCAmelCase_      ,							)
			__snake_case      :							Tuple						=			WavaVecaProcessor(feature_extractor=UpperCAmelCase_      ,							tokenizer=UpperCAmelCase_  )
			processor.save_pretrained(UpperCAmelCase_  )

		__snake_case      :							Optional[Any]						=			WavaVecaForCTC(UpperCAmelCase_  )
	else:
		__snake_case      :							Union[str, Any]						=			WavaVecaForPreTraining(UpperCAmelCase_  )

	if is_finetuned or is_seq_class:
		__snake_case		,    __snake_case		,    __snake_case      :							Optional[int]						=			fairseq.checkpoint_utils.load_model_ensemble_and_task(
		    [checkpoint_path]      ,							arg_overrides={'data': '/'.join(dict_path.split('/'  )[:-1]  )}  )
	else:
		__snake_case      :							int						=			argparse.Namespace(task='audio_pretraining'  )
		__snake_case      :							List[str]						=			fairseq.tasks.setup_task(UpperCAmelCase_  )

		__snake_case		,    __snake_case		,    __snake_case      :							List[str]						=			fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]      ,							task=UpperCAmelCase_  )

	__snake_case      :							Optional[Any]						=			model[0].eval()

	recursively_load_weights(UpperCAmelCase_      ,							UpperCAmelCase_      ,							not is_finetuned  )

	hf_wavavec.save_pretrained(UpperCAmelCase_  )


if __name__ == "__main__":
	_a					:					Any=			argparse.ArgumentParser()
	parser.add_argument("--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model.")
	parser.add_argument("--checkpoint_path", default=None, type=str, help="Path to fairseq checkpoint")
	parser.add_argument("--dict_path", default=None, type=str, help="Path to dict of fine-tuned model")
	parser.add_argument("--config_path", default=None, type=str, help="Path to hf config.json of model to convert")
	parser.add_argument(
	    "--not_finetuned", action="store_true", help="Whether the model to convert is a fine-tuned model or not"
	)
	parser.add_argument(
	    "--is_seq_class",
	    action="store_true",
	    help="Whether the model to convert is a fine-tuned sequence classification model or not",
	)
	_a					:					Optional[Any]=			parser.parse_args()

	_a					:					List[str]=			not args.not_finetuned and not args.is_seq_class
	convert_wavaveca_checkpoint(
	    args.checkpoint_path,
	    args.pytorch_dump_folder_path,
	    args.config_path,
	    args.dict_path,
	    is_finetuned,
	    args.is_seq_class,
	)






"""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      (   lowercase						):
				UpperCAmelCase    :				Optional[int]   =		"""new-model"""


if is_tf_available():

	class 		UpperCamelCase      (   lowercase						):
					UpperCAmelCase    :				List[str]   =		NewModelConfig

@require_tf
class 		UpperCamelCase      (   unittest.TestCase						):



				@slow
				def  _lowercase					(self    :				List[str])   ->     Dict:
					__snake_case      :							Any						=			'bert-base-cased'
					__snake_case      :							Optional[Any]						=			AutoConfig.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)

					__snake_case      :							Union[str, Any]						=			TFAutoModel.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				List[Any])   ->     str:
					__snake_case      :							Optional[int]						=			'bert-base-cased'
					__snake_case      :							List[Any]						=			AutoConfig.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)

					__snake_case      :							Dict						=			TFAutoModelForPreTraining.from_pretrained(_A)
					self.assertIsNotNone(_A)
					self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Any)   ->     List[str]:
					for model_name in TF_GPT2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							str						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							List[str]						=			TFAutoModelForCausalLM.from_pretrained(_A)
						__snake_case		,    __snake_case      :							List[str]						=			TFAutoModelForCausalLM.from_pretrained(_A     ,  output_loading_info=_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Tuple)   ->     Dict:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Union[str, Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Union[str, Any]						=			TFAutoModelWithLMHead.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[int]:
					for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Union[str, Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

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



				@slow
				def  _lowercase					(self    :				str)   ->     Union[str, Any]:
					for model_name in TF_T5_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Dict						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							str						=			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  _lowercase					(self    :				str)   ->     str:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						__snake_case      :							Tuple						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Tuple						=			TFAutoModelForSequenceClassification.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				def  _lowercase					(self    :				Optional[Any])   ->     Optional[int]:
					# for model_name in TF_BERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
					for model_name in ["bert-base-uncased"]:
						__snake_case      :							List[str]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							Any						=			TFAutoModelForQuestionAnswering.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)



				@slow
				@require_tensorflow_probability
				def  _lowercase					(self    :				List[Any])   ->     List[str]:
					for model_name in TF_TAPAS_PRETRAINED_MODEL_ARCHIVE_LIST[5:6]:
						__snake_case      :							Optional[Any]						=			AutoConfig.from_pretrained(_A)
						self.assertIsNotNone(_A)
						self.assertIsInstance(_A     ,  _A)

						__snake_case      :							int						=			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  _lowercase					(self    :				Optional[Any])   ->     Optional[Any]:
					__snake_case      :							Optional[int]						=			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  _lowercase					(self    :				Any)   ->     List[str]:
					__snake_case      :							List[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  _lowercase					(self    :				Optional[Any])   ->     str:
					# For the auto model mapping, FunnelConfig has two models: FunnelModel and FunnelBaseModel
					__snake_case      :							Optional[Any]						=			TFAutoModel.from_pretrained('sgugger/funnel-random-tiny')
					self.assertIsInstance(_A     ,  _A)

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

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



				def  _lowercase					(self    :				List[Any])   ->     int:
					try:
						AutoConfig.register('new-model'     ,  _A)

						__snake_case      :							int						=			[
						    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      :							Optional[int]						=			NewModelConfig(**tiny_config.to_dict())
								__snake_case      :							List[str]						=			auto_class.from_config(_A)
								self.assertIsInstance(_A     ,  _A)

								with tempfile.TemporaryDirectory() as tmp_dir:
									model.save_pretrained(_A)
									__snake_case      :							Tuple						=			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  _lowercase					(self    :				Optional[int])   ->     Union[str, Any]:
					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  _lowercase					(self    :				str)   ->     str:
					with self.assertRaisesRegex(
					    _A     ,  r'aaaaaa is not a valid git identifier \(branch name, tag name or commit id\)'):
						__snake_case      :							Optional[Any]						=			TFAutoModel.from_pretrained(_A     ,  revision='aaaaaa')



				def  _lowercase					(self    :				int)   ->     Any:
					with self.assertRaisesRegex(
					    _A     ,  'hf-internal-testing/config-no-model does not appear to have a file named pytorch_model.bin'     ,  ):
						__snake_case      :							List[str]						=			TFAutoModel.from_pretrained('hf-internal-testing/config-no-model')



				def  _lowercase					(self    :				Optional[Any])   ->     Any:
					with self.assertRaisesRegex(_A     ,  'Use `from_pt=True` to load this model'):
						__snake_case      :							List[str]						=			TFAutoModel.from_pretrained('hf-internal-testing/tiny-bert-pt-only')






				def  _lowercase					(self    :				str)   ->     Any:
					# Make sure we have cached the model.
					__snake_case      :							str						=			TFAutoModel.from_pretrained('hf-internal-testing/tiny-random-bert')
					with RequestCounter() as counter:
						__snake_case      :							List[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
					__snake_case      :							Optional[int]						=			TFAutoModel.from_pretrained('ArthurZ/tiny-random-bert-sharded')
					with RequestCounter() as counter:
						__snake_case      :							Any						=			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)





