




"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


a      =							{
    'configuration_whisper': ['WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'WhisperConfig', 'WhisperOnnxConfig'],
    'feature_extraction_whisper': ['WhisperFeatureExtractor'],
    'processing_whisper': ['WhisperProcessor'],
    'tokenization_whisper': ['WhisperTokenizer'],
}

try:
		if not is_tokenizers_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a      =							['WhisperTokenizerFast']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a      =							[
		    'WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'WhisperForConditionalGeneration',
		    'WhisperModel',
		    'WhisperPreTrainedModel',
		    'WhisperForAudioClassification',
		]

try:
		if not is_tf_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a      =							[
		    'TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST',
		    'TFWhisperForConditionalGeneration',
		    'TFWhisperModel',
		    'TFWhisperPreTrainedModel',
		]

try:
		if not is_flax_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		a      =							[
		    'FlaxWhisperForConditionalGeneration',
		    'FlaxWhisperModel',
		    'FlaxWhisperPreTrainedModel',
		    'FlaxWhisperForAudioClassification',
		]


if TYPE_CHECKING:
		from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
		from .feature_extraction_whisper import WhisperFeatureExtractor
		from .processing_whisper import WhisperProcessor
		from .tokenization_whisper import WhisperTokenizer

		try:
				if not is_tokenizers_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .tokenization_whisper_fast import WhisperTokenizerFast

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_whisper import (
				    WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    WhisperForAudioClassification,
				    WhisperForConditionalGeneration,
				    WhisperModel,
				    WhisperPreTrainedModel,
				)

		try:
				if not is_tf_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_tf_whisper import (
				    TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
				    TFWhisperForConditionalGeneration,
				    TFWhisperModel,
				    TFWhisperPreTrainedModel,
				)

		try:
				if not is_flax_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_flax_whisper import (
				    FlaxWhisperForAudioClassification,
				    FlaxWhisperForConditionalGeneration,
				    FlaxWhisperModel,
				    FlaxWhisperPreTrainedModel,
				)

else:
		import sys

		a      =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)











"""simple docstring"""







import argparse
import json

import numpy
import torch

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()





def 							lowercase					(snake_case__   : int      ,       snake_case__   : Union[str, Any] )     ->  List[Any]:


				'''simple docstring'''






				lowerCAmelCase  =				torch.load(snake_case__      ,       map_location="""cpu""" )

				lowerCAmelCase  =				chkpt["""model"""]

				# We have the base model one level deeper than the original XLM repository
				lowerCAmelCase  =				{}
				for k, v in state_dict.items():
								if "pred_layer" in k:
												lowerCAmelCase  =				v
								else:
												lowerCAmelCase  =				v

				lowerCAmelCase  =				chkpt["""params"""]
				lowerCAmelCase  =				{n: v for n, v in config.items() if not isinstance(snake_case__      ,       (torch.FloatTensor, numpy.ndarray) )}

				lowerCAmelCase  =				chkpt["""dico_word2id"""]
				lowerCAmelCase  =				{s + """</w>""" if s.find("""@@""" ) == -1 and i > 13 else s.replace("""@@"""      ,       """""" ): i for s, i in vocab.items()}

				# Save pytorch-model
				lowerCAmelCase  =				pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
				lowerCAmelCase  =				pytorch_dump_folder_path + """/""" + CONFIG_NAME
				lowerCAmelCase  =				pytorch_dump_folder_path + """/""" + VOCAB_FILES_NAMES["""vocab_file"""]

				print(f'''Save PyTorch model to {pytorch_weights_dump_path}''' )
				torch.save(snake_case__      ,       snake_case__ )

				print(f'''Save configuration file to {pytorch_config_dump_path}''' )
				with open(snake_case__      ,       """w"""      ,       encoding="""utf-8""" ) as f:
								f.write(json.dumps(snake_case__      ,       indent=2 ) + """\n""" )

				print(f'''Save vocab file to {pytorch_config_dump_path}''' )
				with open(snake_case__      ,       """w"""      ,       encoding="""utf-8""" ) as f:
								f.write(json.dumps(snake_case__      ,       indent=2 ) + """\n""" )


if __name__ == "__main__":
		a      =							argparse.ArgumentParser()
		# Required parameters
		parser.add_argument(
		    '--xlm_checkpoint_path', default=None, type=str, required=True, help='Path the official PyTorch dump.'
		)
		parser.add_argument(
		    '--pytorch_dump_folder_path', default=None, type=str, required=True, help='Path to the output PyTorch model.'
		)
		a      =							parser.parse_args()
		convert_xlm_checkpoint_to_pytorch(args.xlm_checkpoint_path, args.pytorch_dump_folder_path)





