

'''simple docstring'''



import argparse
import glob
import logging
import os
import time
from argparse import Namespace

import numpy as np
import torch
from lightning_base import BaseTransformer, add_generic_args, generic_train
from torch.utils.data import DataLoader, TensorDataset

from transformers import glue_compute_metrics as compute_metrics
from transformers import glue_convert_examples_to_features as convert_examples_to_features
from transformers import glue_output_modes, glue_tasks_num_labels
from transformers import glue_processors as processors


__snake_case			:     List[Any]										=      logging.getLogger(__name__)



class lowerCamelCase			(  lowercase_						):



			'''simple docstring'''






			__snake_case        =	'sequence-classification'


			def __init__(				self	:						List[str]		,      lowerCAmelCase_	:						Union[str, Any]			)      ->		Optional[int]:





									'''simple docstring'''
									if type(lowerCAmelCase_			) == dict:
															A__					:       Union[str, Any]     =Namespace(**lowerCAmelCase_			)
									A__					:       List[str]     =glue_output_modes[hparams.task]
									A__					:       Tuple     =glue_tasks_num_labels[hparams.task]

									super().__init__(lowerCAmelCase_		,      lowerCAmelCase_		,      self.mode			)


			def lowercase__						(				self	:						int		,      **lowerCAmelCase_	:						Tuple			)      ->		List[Any]:





									'''simple docstring'''
									return self.model(**lowerCAmelCase_			)


			def lowercase__						(				self	:						Any		,      lowerCAmelCase_	:						List[str]		,      lowerCAmelCase_	:						Tuple			)      ->		Optional[Any]:





									'''simple docstring'''
									A__					:       List[Any]     ={"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

									if self.config.model_type not in ["distilbert", "bart"]:
															A__					:       Optional[Any]     =batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None

									A__					:       int     =self(**lowerCAmelCase_			)
									A__					:       List[Any]     =outputs[0]

									A__					:       Any     =self.trainer.lr_schedulers[0]["""scheduler"""]
									A__					:       int     ={"""loss""": loss, """rate""": lr_scheduler.get_last_lr()[-1]}
									return {"loss": loss, "log": tensorboard_logs}


			def lowercase__						(				self	:						Dict			)      ->		List[str]:





									'''simple docstring'''
									A__					:       Any     =self.hparams
									A__					:       str     =processors[args.task]()
									A__					:       str     =processor.get_labels()

									for mode in ["train", "dev"]:
															A__					:       str     =self._feature_file(lowerCAmelCase_			)
															if os.path.exists(lowerCAmelCase_			) and not args.overwrite_cache:
																					logger.info("""Loading features from cached file %s"""		,      lowerCAmelCase_			)
															else:
																					logger.info("""Creating features from dataset file at %s"""		,      args.data_dir			)
																					A__					:       List[Any]     =(
																					    processor.get_dev_examples(args.data_dir			)
																					    if mode == """dev"""
																					    else processor.get_train_examples(args.data_dir			)
																					)
																					A__					:       Tuple     =convert_examples_to_features(
																					    lowerCAmelCase_		,      self.tokenizer		,      max_length=args.max_seq_length		,      label_list=self.labels		,      output_mode=args.glue_output_mode		,      )
																					logger.info("""Saving features into cached file %s"""		,      lowerCAmelCase_			)
																					torch.save(lowerCAmelCase_		,      lowerCAmelCase_			)


			def lowercase__						(				self	:						List[Any]		,      lowerCAmelCase_	:						str		,      lowerCAmelCase_	:						int		,      lowerCAmelCase_	:						bool = False			)      ->		DataLoader:





									'''simple docstring'''
									A__					:       int     ="""dev""" if mode == """test""" else mode

									A__					:       Optional[Any]     =self._feature_file(lowerCAmelCase_			)
									logger.info("""Loading features from cached file %s"""		,      lowerCAmelCase_			)
									A__					:       Tuple     =torch.load(lowerCAmelCase_			)
									A__					:       Optional[Any]     =torch.tensor([f.input_ids for f in features]		,      dtype=torch.long			)
									A__					:       str     =torch.tensor([f.attention_mask for f in features]		,      dtype=torch.long			)
									A__					:       Dict     =torch.tensor([f.token_type_ids for f in features]		,      dtype=torch.long			)
									if self.hparams.glue_output_mode == "classification":
															A__					:       Union[str, Any]     =torch.tensor([f.label for f in features]		,      dtype=torch.long			)
									elif self.hparams.glue_output_mode == "regression":
															A__					:       Union[str, Any]     =torch.tensor([f.label for f in features]		,      dtype=torch.float			)

									return DataLoader(
									    TensorDataset(lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_			)		,      batch_size=lowerCAmelCase_		,      shuffle=lowerCAmelCase_		,      )


			def lowercase__						(				self	:						Dict		,      lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						Union[str, Any]			)      ->		Tuple:





									'''simple docstring'''
									A__					:       str     ={"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

									if self.config.model_type not in ["distilbert", "bart"]:
															A__					:       Optional[Any]     =batch[2] if self.config.model_type in ["""bert""", """xlnet""", """albert"""] else None

									A__					:       Tuple     =self(**lowerCAmelCase_			)
									A__   ,						A__					:       Dict     =outputs[:2]
									A__					:       Tuple     =logits.detach().cpu().numpy()
									A__					:       Tuple     =inputs["""labels"""].detach().cpu().numpy()

									return {"val_loss": tmp_eval_loss.detach().cpu(), "pred": preds, "target": out_label_ids}


			def lowercase__						(				self	:						int		,      lowerCAmelCase_	:						str			)      ->		tuple:





									'''simple docstring'''
									A__					:       Optional[int]     =torch.stack([x["""val_loss"""] for x in outputs]			).mean().detach().cpu().item()
									A__					:       Tuple     =np.concatenate([x["""pred"""] for x in outputs]		,      axis=0			)

									if self.hparams.glue_output_mode == "classification":
															A__					:       List[str]     =np.argmax(lowerCAmelCase_		,      axis=1			)
									elif self.hparams.glue_output_mode == "regression":
															A__					:       Optional[Any]     =np.squeeze(lowerCAmelCase_			)

									A__					:       Any     =np.concatenate([x["""target"""] for x in outputs]		,      axis=0			)
									A__					:       str     =[[] for _ in range(out_label_ids.shape[0]			)]
									A__					:       str     =[[] for _ in range(out_label_ids.shape[0]			)]

									A__					:       Optional[Any]     ={**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task		,      lowerCAmelCase_		,      lowerCAmelCase_			)}

									A__					:       List[str]     =dict(results.items()			)
									A__					:       Union[str, Any]     =results
									return ret, preds_list, out_label_list


			def lowercase__						(				self	:						str		,      lowerCAmelCase_	:						list			)      ->		dict:





									'''simple docstring'''
									A__   ,						A__   ,						A__					:       List[str]     =self._eval_end(lowerCAmelCase_			)
									A__					:       Tuple     =ret["""log"""]
									return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}


			def lowercase__						(				self	:						List[Any]		,      lowerCAmelCase_	:						Union[str, Any]			)      ->		dict:





									'''simple docstring'''
									A__   ,						A__   ,						A__					:       Optional[Any]     =self._eval_end(lowerCAmelCase_			)
									A__					:       Optional[int]     =ret["""log"""]
									# `val_loss` is the key returned by `self._eval_end()` but actually refers to `test_loss`
									return {"avg_test_loss": logs["val_loss"], "log": logs, "progress_bar": logs}



			@staticmethod
			def lowercase__						(				lowerCAmelCase_	:						Union[str, Any]		,      lowerCAmelCase_	:						List[Any]			)      ->		Tuple:





									'''simple docstring'''
									BaseTransformer.add_model_specific_args(lowerCAmelCase_		,      lowerCAmelCase_			)
									parser.add_argument(
									    """--max_seq_length"""		,      default=1_28		,      type=lowerCAmelCase_		,      help=(
									        """The maximum total input sequence length after tokenization. Sequences longer """
									        """than this will be truncated, sequences shorter will be padded."""
									    )		,      )

									parser.add_argument(
									    """--task"""		,      default=""""""		,      type=lowerCAmelCase_		,      required=lowerCAmelCase_		,      help="""The GLUE task to run"""		,      )
									parser.add_argument(
									    """--gpus"""		,      default=0		,      type=lowerCAmelCase_		,      help="""The number of GPUs allocated for this, it is by default 0 meaning none"""		,      )

									parser.add_argument(
									    """--overwrite_cache"""		,      action="""store_true"""		,      help="""Overwrite the cached training and evaluation sets"""			)

									return parser


def 						__lowerCamelCase	(       )	->							List[Any]:






						"""simple docstring"""





						A__					:       Tuple     =argparse.ArgumentParser()
						add_generic_args(__snake_case,		os.getcwd()	)
						A__					:       Dict     =GLUETransformer.add_model_specific_args(__snake_case,		os.getcwd()	)
						A__					:       str     =parser.parse_args()

						# If output_dir not provided, a folder will be generated in pwd
						if args.output_dir is None:
												A__					:       Union[str, Any]     =os.path.join(
												    """./results""",		f"{args.task}_{time.strftime('%Y%m%d_%H%M%S'	)}",		)
												os.makedirs(args.output_dir	)

						A__					:       Tuple     =GLUETransformer(__snake_case	)
						A__					:       Dict     =generic_train(__snake_case,		__snake_case	)

						# Optionally, predict on dev set and write to output_dir
						if args.do_predict:
												A__					:       Dict     =sorted(glob.glob(os.path.join(args.output_dir,		"""checkpoint-epoch=*.ckpt"""	),		recursive=__snake_case	)	)
												A__					:       List[str]     =model.load_from_checkpoint(checkpoints[-1]	)
												return trainer.test(__snake_case	)


if __name__ == "__main__":
		main()




'''simple docstring'''



from argparse import ArgumentParser

from .add_new_model import AddNewModelCommand
from .add_new_model_like import AddNewModelLikeCommand
from .convert import ConvertCommand
from .download import DownloadCommand
from .env import EnvironmentCommand
from .lfs import LfsCommands
from .pt_to_tf import PTtoTFCommand
from .run import RunCommand
from .serving import ServeCommand
from .user import UserCommands
def 						__lowerCamelCase	(       )	->							List[Any]:






						"""simple docstring"""





						A__					:       List[Any]     =ArgumentParser("""Transformers CLI tool""",		usage="""transformers-cli <command> [<args>]"""	)
						A__					:       List[Any]     =parser.add_subparsers(help="""transformers-cli command helpers"""	)

						# Register commands
						ConvertCommand.register_subcommand(__snake_case	)
						DownloadCommand.register_subcommand(__snake_case	)
						EnvironmentCommand.register_subcommand(__snake_case	)
						RunCommand.register_subcommand(__snake_case	)
						ServeCommand.register_subcommand(__snake_case	)
						UserCommands.register_subcommand(__snake_case	)
						AddNewModelCommand.register_subcommand(__snake_case	)
						AddNewModelLikeCommand.register_subcommand(__snake_case	)
						LfsCommands.register_subcommand(__snake_case	)
						PTtoTFCommand.register_subcommand(__snake_case	)

						# Let's go
						A__					:       List[str]     =parser.parse_args()

						if not hasattr(__snake_case,		"""func"""	):
												parser.print_help()
												exit(1	)

						# Run
						A__					:       Optional[Any]     =args.func(__snake_case	)
						service.run()


if __name__ == "__main__":
		main()

