



"""simple docstring"""





from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand




def 	UpperCAmelCase     (		A	:						Namespace       ):

		'''simple docstring'''


		return ConvertCommand(
		    args.model_type     ,				args.tf_checkpoint     ,				args.pytorch_dump_output     ,				args.config     ,				args.finetuning_task_name       )


lowercase  							=					'''
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
'''






class 			lowercase__			(      A    ):
							'''simple docstring'''







							@staticmethod
							def        lowerCamelCase_   (  snake_case						)			->							str:
									_UpperCAmelCase							 =		parser.add_parser(
									    'convert'	,       help='CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'	,       )
									train_parser.add_argument('--model_type'	,       type=snake_case	,       required=snake_case	,       help='Model\'s type.'						)
									train_parser.add_argument(
									    '--tf_checkpoint'	,       type=snake_case	,       required=snake_case	,       help='TensorFlow checkpoint path or folder.'						)
									train_parser.add_argument(
									    '--pytorch_dump_output'	,       type=snake_case	,       required=snake_case	,       help='Path to the PyTorch saved model output.'						)
									train_parser.add_argument('--config'	,       type=snake_case	,       default=''	,       help='Configuration file path or folder.'						)
									train_parser.add_argument(
									    '--finetuning_task_name'	,       type=snake_case	,       default=snake_case	,       help='Optional fine-tuning task name if the TF model was a finetuned model.'	,       )
									train_parser.set_defaults(func=snake_case						)






							def __init__(  self	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       snake_case	,       *snake_case	,       )			->							str:
									_UpperCAmelCase							 =		logging.get_logger('transformers-cli/converting'						)

									self._logger.info(f'Loading model {model_type}'						)
									_UpperCAmelCase							 =		model_type
									_UpperCAmelCase							 =		tf_checkpoint
									_UpperCAmelCase							 =		pytorch_dump_output
									_UpperCAmelCase							 =		config
									_UpperCAmelCase							 =		finetuning_task_name






							def        lowerCamelCase_   (  self						)			->							Dict:
									if self._model_type == "albert":
											try:
													from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
													    convert_tf_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "bert":
											try:
													from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
													    convert_tf_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "funnel":
											try:
													from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
													    convert_tf_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "t5":
											try:
													from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
											except ImportError:
													raise ImportError(snake_case						)

											convert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "gpt":
											from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
											    convert_openai_checkpoint_to_pytorch,
											)

											convert_openai_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "transfo_xl":
											try:
													from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
													    convert_transfo_xl_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											if "ckpt" in self._tf_checkpoint.lower():
													_UpperCAmelCase							 =		self._tf_checkpoint
													_UpperCAmelCase							 =		''
											else:
													_UpperCAmelCase							 =		self._tf_checkpoint
													_UpperCAmelCase							 =		''
											convert_transfo_xl_checkpoint_to_pytorch(
											    snake_case	,       self._config	,       self._pytorch_dump_output	,       snake_case						)
									elif self._model_type == "gpt2":
											try:
													from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
													    convert_gpta_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									elif self._model_type == "xlnet":
											try:
													from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
													    convert_xlnet_checkpoint_to_pytorch,
													)
											except ImportError:
													raise ImportError(snake_case						)

											convert_xlnet_checkpoint_to_pytorch(
											    self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output	,       self._finetuning_task_name						)
									elif self._model_type == "xlm":
											from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
											    convert_xlm_checkpoint_to_pytorch,
											)

											convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint	,       self._pytorch_dump_output						)
									elif self._model_type == "lxmert":
											from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
											    convert_lxmert_checkpoint_to_pytorch,
											)

											convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint	,       self._pytorch_dump_output						)
									elif self._model_type == "rembert":
											from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
											    convert_rembert_tf_checkpoint_to_pytorch,
											)

											convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint	,       self._config	,       self._pytorch_dump_output						)
									else:
											raise ValueError(
											    '--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'						)







"""simple docstring"""





from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


lowercase  							=					logging.get_logger(__name__)


lowercase  							=					TypeVar('''DatasetType''', Dataset, IterableDataset)




def 	UpperCAmelCase     (		A	:						List[DatasetType]     ,				A	:						Optional[List[float]] = None     ,				A	:						Optional[int] = None     ,				A	:						Optional[DatasetInfo] = None     ,				A	:						Optional[NamedSplit] = None     ,				A	:						Literal["first_exhausted", "all_exhausted"] = "first_exhausted"     ,				):

		'''simple docstring'''


		from .arrow_dataset import Dataset
		from .iterable_dataset import IterableDataset

		if not datasets:
				raise ValueError('Unable to interleave an empty list of datasets.'       )
		for i, dataset in enumerate(A       ):
				if not isinstance(A     ,				(Dataset, IterableDataset)       ):
						if isinstance(A     ,				(DatasetDict, IterableDatasetDict)       ):
								if not dataset:
										raise ValueError(
										    f'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} '
										    'is an empty dataset dictionary.'       )
								raise ValueError(
								    f'Dataset at position {i} has at least one split: {list(A       )}\n'
								    f'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(A       )       )}\']'       )
						raise ValueError(
						    f'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(A       ).__name__}.'       )
				if i == 0:
						_UpperCAmelCase ,					_UpperCAmelCase							 =		(
						    (Dataset, IterableDataset) if isinstance(A     ,				A       ) else (IterableDataset, Dataset)
						)
				elif not isinstance(A     ,				A       ):
						raise ValueError(
						    f'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.'       )
		if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
				raise ValueError(f'{stopping_strategy} is not supported. Please enter a valid stopping_strategy.'       )
		if dataset_type is Dataset:
				return _interleave_map_style_datasets(
				    A     ,				A     ,				A     ,				info=A     ,				split=A     ,				stopping_strategy=A       )
		else:
				return _interleave_iterable_datasets(
				    A     ,				A     ,				A     ,				info=A     ,				split=A     ,				stopping_strategy=A       )






def 	UpperCAmelCase     (		A	:						List[DatasetType]     ,				A	:						Optional[DatasetInfo] = None     ,				A	:						Optional[NamedSplit] = None     ,				A	:						int = 0     ,				):

		'''simple docstring'''


		if not dsets:
				raise ValueError('Unable to concatenate an empty list of datasets.'       )
		for i, dataset in enumerate(A       ):
				if not isinstance(A     ,				(Dataset, IterableDataset)       ):
						if isinstance(A     ,				(DatasetDict, IterableDatasetDict)       ):
								if not dataset:
										raise ValueError(
										    f'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} '
										    'is an empty dataset dictionary.'       )
								raise ValueError(
								    f'Dataset at position {i} has at least one split: {list(A       )}\n'
								    f'Please pick one to interleave with the other datasets, for example: dataset[\'{next(iter(A       )       )}\']'       )
						raise ValueError(
						    f'Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(A       ).__name__}.'       )
				if i == 0:
						_UpperCAmelCase ,					_UpperCAmelCase							 =		(
						    (Dataset, IterableDataset) if isinstance(A     ,				A       ) else (IterableDataset, Dataset)
						)
				elif not isinstance(A     ,				A       ):
						raise ValueError(
						    f'Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects.'       )
		if dataset_type is Dataset:
				return _concatenate_map_style_datasets(A     ,				info=A     ,				split=A     ,				axis=A       )
		else:
				return _concatenate_iterable_datasets(A     ,				info=A     ,				split=A     ,				axis=A       )


