


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


lowerCAmelCase__ 							=  logging.getLogger(__name__)


class  SCREAMING_SNAKE_CASE__							(						__snake_case							):
       """simple docstring"""


       a    :							int  ="sequence-classification"

       def __init__(		self						,	snake_case__		):






         """simple docstring"""





         if type(a__		) == dict:
           lowerCAmelCase	:	Optional[int]								=				Namespace(**a__		)
         lowerCAmelCase	:	List[Any]								=				glue_output_modes[hparams.task]
         lowerCAmelCase	:	List[str]								=				glue_tasks_num_labels[hparams.task]

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

       def 			lowercase__				(		self						,	**snake_case__		):






         """simple docstring"""





         return self.model(**a__		)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	Tuple								=				{"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}

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

         lowerCAmelCase	:	Dict								=				self(**a__		)
         lowerCAmelCase	:	Dict								=				outputs[0]

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

       def 			lowercase__				(		self		):






         """simple docstring"""





         lowerCAmelCase	:	List[Any]								=				self.hparams
         lowerCAmelCase	:	Dict								=				processors[args.task]()
         lowerCAmelCase	:	Dict								=				processor.get_labels()

         for mode in ["train", "dev"]:
           lowerCAmelCase	:	Optional[int]								=				self._feature_file(a__		)
           if os.path.exists(a__		) and not args.overwrite_cache:
             logger.info("Loading features from cached file %s"						,	a__		)
           else:
             logger.info("Creating features from dataset file at %s"						,	args.data_dir		)
             lowerCAmelCase	:	Tuple								=				(
                 processor.get_dev_examples(args.data_dir		)
                 if mode == "dev"
                 else processor.get_train_examples(args.data_dir		)
             )
             lowerCAmelCase	:	List[str]								=				convert_examples_to_features(
                 a__						,	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"						,	a__		)
             torch.save(a__						,	a__		)

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__						,	snake_case__ = False		):






         """simple docstring"""





         lowerCAmelCase	:	Union[str, Any]								=				"dev" if mode == "test" else mode

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

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

       def 			lowercase__				(		self						,	snake_case__						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	List[Any]								=				{"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}

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

         lowerCAmelCase	:	str								=				self(**a__		)
         lowerCAmelCase		,    lowerCAmelCase	:	int								=				outputs[:2]
         lowerCAmelCase	:	int								=				logits.detach().cpu().numpy()
         lowerCAmelCase	:	Dict								=				inputs["labels"].detach().cpu().numpy()

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

       def 			lowercase__				(		self						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase	:	List[Any]								=				torch.stack([x["val_loss"] for x in outputs]		).mean().detach().cpu().item()
         lowerCAmelCase	:	List[Any]								=				np.concatenate([x["pred"] for x in outputs]						,	axis=0		)

         if self.hparams.glue_output_mode == "classification":
           lowerCAmelCase	:	str								=				np.argmax(a__						,	axis=1		)
         elif self.hparams.glue_output_mode == "regression":
           lowerCAmelCase	:	Union[str, Any]								=				np.squeeze(a__		)

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

         lowerCAmelCase	:	Optional[int]								=				{**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task						,	a__						,	a__		)}

         lowerCAmelCase	:	Optional[Any]								=				dict(results.items()		)
         lowerCAmelCase	:	Optional[int]								=				results
         return ret, preds_list, out_label_list

       def 			lowercase__				(		self						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase	:	List[str]								=				self._eval_end(a__		)
         lowerCAmelCase	:	Optional[Any]								=				ret["log"]
         return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}

       def 			lowercase__				(		self						,	snake_case__		):






         """simple docstring"""





         lowerCAmelCase		,    lowerCAmelCase		,    lowerCAmelCase	:	Optional[int]								=				self._eval_end(a__		)
         lowerCAmelCase	:	str								=				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__				(		snake_case__						,	snake_case__		):






         """simple docstring"""





         BaseTransformer.add_model_specific_args(a__						,	a__		)
         parser.add_argument(
             "--max_seq_length"						,	default=128						,	type=a__						,	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=a__						,	required=a__						,	help="The GLUE task to run"						,	)
         parser.add_argument(
             "--gpus"						,	default=0						,	type=a__						,	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    a__     (							):


  '''simple docstring'''







  lowerCAmelCase	:	str								=				argparse.ArgumentParser()
  add_generic_args(UpperCamelCase__							, os.getcwd()							)
  lowerCAmelCase	:	Tuple								=				GLUETransformer.add_model_specific_args(UpperCamelCase__							, os.getcwd()							)
  lowerCAmelCase	:	Tuple								=				parser.parse_args()

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

  lowerCAmelCase	:	List[str]								=				GLUETransformer(UpperCamelCase__							)
  lowerCAmelCase	:	Union[str, Any]								=				generic_train(UpperCamelCase__							, UpperCamelCase__							)

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


if __name__ == "__main__":
     main()









import numpy as np

from transformers import Pipeline





def 	_lowerCAmelCase					( UpperCamelCase__:  Optional[Any]    )			-> Optional[int]:





	"""simple docstring"""







	A             =					np.max(UpperCamelCase__				,       axis=-1				,       keepdims=UpperCamelCase__    )
	A             =					np.exp(outputs - maxes    )
	return shifted_exp / shifted_exp.sum(axis=-1				,       keepdims=UpperCamelCase__    )






class    _UpperCamelCase     (       __snake_case					):

				"""simple docstring"""







				def   _UpperCAmelCase       (    self					, **a__ )						->					Union[str, Any]:
					A             =					{}
					if "second_text" in kwargs:
						A             =					kwargs["""second_text"""]
					return preprocess_kwargs, {}, {}

				def   _UpperCAmelCase       (    self					, a__					, a__=None )						->					str:
					return self.tokenizer(a__					, text_pair=a__					, return_tensors=self.framework )

				def   _UpperCAmelCase       (    self					, a__ )						->					Tuple:
					return self.model(**a__ )

				def   _UpperCAmelCase       (    self					, a__ )						->					Optional[int]:
					A             =					model_outputs.logits[0].numpy()
					A             =					softmax(a__ )

					A             =					np.argmax(a__ )
					A             =					self.model.config.idalabel[best_class]
					A             =					probabilities[best_class].item()
					A             =					logits.tolist()
					return {"label": label, "score": score, "logits": logits}



