



import logging
import os
from dataclasses import dataclass, field
from typing import Dict, Optional

import numpy as np
from utils_multiple_choice import MultipleChoiceDataset, Split, processors

import transformers
from transformers import (
    AutoConfig,
    AutoModelForMultipleChoice,
    AutoTokenizer,
    DataCollatorWithPadding,
    EvalPrediction,
    HfArgumentParser,
    Trainer,
    TrainingArguments,
    set_seed,
)
from transformers.trainer_utils import is_main_process


UpperCAmelCase   :							List[str]							 =logging.getLogger(__name__)



def    _lowerCAmelCase    (_lowerCAmelCase   ,	_lowerCAmelCase):
					return (preds == labels).mean()


@dataclass
class       _lowercase   :
				'''simple docstring'''







				lowercase__   						=						field(
				    metadata={"""help""": """Path to pretrained model or model identifier from huggingface.co/models"""}      )
				lowercase__   						=						field(
				    default=a_      ,   metadata={"""help""": """Pretrained config name or path if not the same as model_name"""}      )
				lowercase__   						=						field(
				    default=a_      ,   metadata={"""help""": """Pretrained tokenizer name or path if not the same as model_name"""}      )
				lowercase__   						=						field(
				    default=a_      ,   metadata={"""help""": """Where do you want to store the pretrained models downloaded from huggingface.co"""}      ,   )




@dataclass
class       _lowercase   :
				'''simple docstring'''







				lowercase__   						=						field(metadata={"""help""": """The name of the task to train on: """ + """, """.join(processors.keys()      )}      )
				lowercase__   						=						field(metadata={"""help""": """Should contain the data files for the task."""}      )
				lowercase__   						=						field(
				    default=1_28      ,   metadata={
				        """help""": (
				            """The maximum total input sequence length after tokenization. Sequences longer """
				            """than this will be truncated, sequences shorter will be padded."""
				        )
				    }      ,   )
				lowercase__   						=						field(
				    default=a_      ,   metadata={"""help""": """Overwrite the cached training and evaluation sets"""}      )




def    _lowerCAmelCase    ():
					# See all possible arguments in src/transformers/training_args.py
					# or by passing the --help flag to this script.
					# We now keep distinct sets of args, for a cleaner separation of concerns.

					UpperCamelCase_		  =   HfArgumentParser((ModelArguments, DataTrainingArguments, TrainingArguments))
					UpperCamelCase_  ,    UpperCamelCase_  ,    UpperCamelCase_		  =   parser.parse_args_into_dataclasses()

					if (
					    os.path.exists(training_args.output_dir)
					    and os.listdir(training_args.output_dir)
					    and training_args.do_train
					    and not training_args.overwrite_output_dir
					):
										raise ValueError(
										    f"""Output directory ({training_args.output_dir}) already exists and is not empty. Use"""
										    " --overwrite_output_dir to overcome.")

					# Setup logging
					logging.basicConfig(
					    format="%(asctime)s - %(levelname)s - %(name)s - %(message)s"   ,	datefmt="%m/%d/%Y %H:%M:%S"   ,	level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN   ,	)
					logger.warning(
					    "Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s"   ,	training_args.local_rank   ,	training_args.device   ,	training_args.n_gpu   ,	bool(training_args.local_rank != -1)   ,	training_args.fpaa   ,	)
					# Set the verbosity to info of the Transformers logger (on main process only):
					if is_main_process(training_args.local_rank):
										transformers.utils.logging.set_verbosity_info()
										transformers.utils.logging.enable_default_handler()
										transformers.utils.logging.enable_explicit_format()
					logger.info("Training/evaluation parameters %s"   ,	_lowerCAmelCase)

					# Set seed
					set_seed(training_args.seed)

					try:
										UpperCamelCase_		  =   processors[data_args.task_name]()
										UpperCamelCase_		  =   processor.get_labels()
										UpperCamelCase_		  =   len(_lowerCAmelCase)
					except KeyError:
										raise ValueError("Task not found: %s" % (data_args.task_name))

					# Load pretrained model and tokenizer
					#
					# Distributed training:
					# The .from_pretrained methods guarantee that only one local process can concurrently
					# download model & vocab.

					UpperCamelCase_		  =   AutoConfig.from_pretrained(
					    model_args.config_name if model_args.config_name else model_args.model_name_or_path   ,	num_labels=_lowerCAmelCase   ,	finetuning_task=data_args.task_name   ,	cache_dir=model_args.cache_dir   ,	)
					UpperCamelCase_		  =   AutoTokenizer.from_pretrained(
					    model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path   ,	cache_dir=model_args.cache_dir   ,	)
					UpperCamelCase_		  =   AutoModelForMultipleChoice.from_pretrained(
					    model_args.model_name_or_path   ,	from_tf=bool(".ckpt" in model_args.model_name_or_path)   ,	config=_lowerCAmelCase   ,	cache_dir=model_args.cache_dir   ,	)

					# Get datasets
					UpperCamelCase_		  =   (
					    MultipleChoiceDataset(
					        data_dir=data_args.data_dir   ,	tokenizer=_lowerCAmelCase   ,	task=data_args.task_name   ,	max_seq_length=data_args.max_seq_length   ,	overwrite_cache=data_args.overwrite_cache   ,	mode=Split.train   ,	)
					    if training_args.do_train
					    else None
					)
					UpperCamelCase_		  =   (
					    MultipleChoiceDataset(
					        data_dir=data_args.data_dir   ,	tokenizer=_lowerCAmelCase   ,	task=data_args.task_name   ,	max_seq_length=data_args.max_seq_length   ,	overwrite_cache=data_args.overwrite_cache   ,	mode=Split.dev   ,	)
					    if training_args.do_eval
					    else None
					)

					def compute_metrics(_lowerCAmelCase) -> Dict:
										UpperCamelCase_		  =   np.argmax(p.predictions   ,	axis=1)
										return {"acc": simple_accuracy(_lowerCAmelCase   ,	p.label_ids)}

					# Data collator
					UpperCamelCase_		  =   DataCollatorWithPadding(_lowerCAmelCase   ,	pad_to_multiple_of=8) if training_args.fpaa else None

					# Initialize our Trainer
					UpperCamelCase_		  =   Trainer(
					    model=_lowerCAmelCase   ,	args=_lowerCAmelCase   ,	train_dataset=_lowerCAmelCase   ,	eval_dataset=_lowerCAmelCase   ,	compute_metrics=_lowerCAmelCase   ,	data_collator=_lowerCAmelCase   ,	)

					# Training
					if training_args.do_train:
										trainer.train(
										    model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path) else None)
										trainer.save_model()
										# For convenience, we also re-save the tokenizer to the same directory,
										# so that you can share your model easily on huggingface.co/models =)
										if trainer.is_world_master():
															tokenizer.save_pretrained(training_args.output_dir)

    # Evaluation
					UpperCamelCase_		  =   {}
					if training_args.do_eval:
										logger.info("*** Evaluate ***")

										UpperCamelCase_		  =   trainer.evaluate()

										UpperCamelCase_		  =   os.path.join(training_args.output_dir   ,	"eval_results.txt")
										if trainer.is_world_master():
															with open(_lowerCAmelCase   ,	"w") as writer:
																				logger.info("***** Eval results *****")
																				for key, value in result.items():
																									logger.info("  %s = %s"   ,	_lowerCAmelCase   ,	_lowerCAmelCase)
																									writer.write("%s = %s\n" % (key, value))

																				results.update(_lowerCAmelCase)

					return results



def    _lowerCAmelCase    (_lowerCAmelCase):
					# For xla_spawn (TPUs)
					main()


if __name__ == "__main__":
	main()





from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand



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


UpperCAmelCase   :							Dict							 ="""
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__    ):

									'''simple docstring'''


									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__     , ):

									'''simple docstring'''


									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    ):

									'''simple docstring'''


									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]"    )
