





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


SCREAMING_SNAKE_CASE__					=   logging.getLogger(__name__)

class 		lowercase					(					_UpperCAmelCase					):
      _SCREAMING_SNAKE_CASE					=      'sequence-classification'





      def __init__(       self      ,						lowercase )		->     Optional[Any]:
            if type(lowercase ) == dict:
                  lowerCAmelCase  						=       Namespace(**lowercase )
            lowerCAmelCase  						=       glue_output_modes[hparams.task]
            lowerCAmelCase  						=       glue_tasks_num_labels[hparams.task]

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





      def     _snake_case			(       self      ,						**lowercase )		->     List[str]:
            return self.model(**lowercase )





      def     _snake_case			(       self      ,						lowercase      ,						lowercase )		->     List[str]:
            lowerCAmelCase  						=       {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

            lowerCAmelCase  						=       self(**lowercase )
            lowerCAmelCase  						=       outputs[0]

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





      def     _snake_case			(       self )		->     Dict:
            lowerCAmelCase  						=       self.hparams
            lowerCAmelCase  						=       processors[args.task]()
            lowerCAmelCase  						=       processor.get_labels()

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





      def     _snake_case			(       self      ,						lowercase      ,						lowercase      ,						lowercase = False )		->     DataLoader:
            lowerCAmelCase  						=       """dev""" if mode == """test""" else mode

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

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





      def     _snake_case			(       self      ,						lowercase      ,						lowercase )		->     str:
            lowerCAmelCase  						=       {"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

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

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





      def     _snake_case			(       self      ,						lowercase )		->     tuple:
            lowerCAmelCase  						=       torch.stack([x["""val_loss"""] for x in outputs] ).mean().detach().cpu().item()
            lowerCAmelCase  						=       np.concatenate([x["""pred"""] for x in outputs]      ,						axis=0 )

            if self.hparams.glue_output_mode == "classification":
                  lowerCAmelCase  						=       np.argmax(lowercase      ,						axis=1 )
            elif self.hparams.glue_output_mode == "regression":
                  lowerCAmelCase  						=       np.squeeze(lowercase )

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

            lowerCAmelCase  						=       {**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task      ,						lowercase      ,						lowercase )}

            lowerCAmelCase  						=       dict(results.items() )
            lowerCAmelCase  						=       results
            return ret, preds_list, out_label_list





      def     _snake_case			(       self      ,						lowercase )		->     dict:
            lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  						=       self._eval_end(lowercase )
            lowerCAmelCase  						=       ret["""log"""]
            return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}





      def     _snake_case			(       self      ,						lowercase )		->     dict:
            lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  						=       self._eval_end(lowercase )
            lowerCAmelCase  						=       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     _snake_case			(       lowercase      ,						lowercase )		->     Optional[Any]:
            BaseTransformer.add_model_specific_args(lowercase      ,						lowercase )
            parser.add_argument(
                """--max_seq_length"""      ,						default=128      ,						type=lowercase      ,						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=lowercase      ,						required=lowercase      ,						help="""The GLUE task to run"""      ,						)
            parser.add_argument(
                """--gpus"""      ,						default=0      ,						type=lowercase      ,						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 			UpperCAmelCase__							(  ):







      '''simple docstring'''




      lowerCAmelCase  						=       argparse.ArgumentParser()
      add_generic_args(SCREAMING_SNAKE_CASE							,						os.getcwd()			)
      lowerCAmelCase  						=       GLUETransformer.add_model_specific_args(SCREAMING_SNAKE_CASE							,						os.getcwd()			)
      lowerCAmelCase  						=       parser.parse_args()

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

      lowerCAmelCase  						=       GLUETransformer(SCREAMING_SNAKE_CASE			)
      lowerCAmelCase  						=       generic_train(SCREAMING_SNAKE_CASE							,						SCREAMING_SNAKE_CASE			)

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


if __name__ == "__main__":
       main()













"""simple docstring"""





from scipy.stats import pearsonr, spearmanr
from sklearn.metrics import fa_score, matthews_corrcoef

import datasets


SCREAMING_SNAKE_CASE__					=   "\\n@inproceedings{wang2019glue,\n  title={{GLUE}: A Multi-Task Benchmark and Analysis Platform for Natural Language Understanding},\n  author={Wang, Alex and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R.},\n  note={In the Proceedings of ICLR.},\n  year={2019}\n}\n"

SCREAMING_SNAKE_CASE__					=   "\\nGLUE, the General Language Understanding Evaluation benchmark\n(https://gluebenchmark.com/) is a collection of resources for training,\nevaluating, and analyzing natural language understanding systems.\n"

SCREAMING_SNAKE_CASE__					=   "\nCompute GLUE evaluation metric associated to each GLUE dataset.\nArgs:\n    predictions: list of predictions to score.\n        Each translation should be tokenized into a list of tokens.\n    references: list of lists of references for each translation.\n        Each reference should be tokenized into a list of tokens.\nReturns: depending on the GLUE subset, one or several of:\n    \"accuracy\": Accuracy\n    \"f1\": F1 score\n    \"pearson\": Pearson Correlation\n    \"spearmanr\": Spearman Correlation\n    \"matthews_correlation\": Matthew Correlation\nExamples:\n\n    >>> glue_metric = datasets.load_metric('glue', 'sst2')  # 'sst2' or any of [\"mnli\", \"mnli_mismatched\", \"mnli_matched\", \"qnli\", \"rte\", \"wnli\", \"hans\"]\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'mrpc')  # 'mrpc' or 'qqp'\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'accuracy': 1.0, 'f1': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'stsb')\n    >>> references = [0., 1., 2., 3., 4., 5.]\n    >>> predictions = [0., 1., 2., 3., 4., 5.]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print({\"pearson\": round(results[\"pearson\"], 2), \"spearmanr\": round(results[\"spearmanr\"], 2)})\n    {'pearson': 1.0, 'spearmanr': 1.0}\n\n    >>> glue_metric = datasets.load_metric('glue', 'cola')\n    >>> references = [0, 1]\n    >>> predictions = [0, 1]\n    >>> results = glue_metric.compute(predictions=predictions, references=references)\n    >>> print(results)\n    {'matthews_correlation': 1.0}\n"


def 			UpperCAmelCase__							(  SCREAMING_SNAKE_CASE     :						Tuple							,						SCREAMING_SNAKE_CASE     :						List[Any]			):







      '''simple docstring'''




      return float((preds == labels).mean()			)


def 			UpperCAmelCase__							(  SCREAMING_SNAKE_CASE     :						Optional[Any]							,						SCREAMING_SNAKE_CASE     :						Any			):







      '''simple docstring'''




      lowerCAmelCase  						=       simple_accuracy(SCREAMING_SNAKE_CASE							,						SCREAMING_SNAKE_CASE			)
      lowerCAmelCase  						=       float(fa_score(y_true=SCREAMING_SNAKE_CASE							,						y_pred=SCREAMING_SNAKE_CASE			)			)
      return {
          "accuracy": acc,
          "f1": fa,
      }


def 			UpperCAmelCase__							(  SCREAMING_SNAKE_CASE     :						List[str]							,						SCREAMING_SNAKE_CASE     :						Tuple			):







      '''simple docstring'''




      lowerCAmelCase  						=       float(pearsonr(SCREAMING_SNAKE_CASE							,						SCREAMING_SNAKE_CASE			)[0]			)
      lowerCAmelCase  						=       float(spearmanr(SCREAMING_SNAKE_CASE							,						SCREAMING_SNAKE_CASE			)[0]			)
      return {
          "pearson": pearson_corr,
          "spearmanr": spearman_corr,
      }






@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION     ,  _KWARGS_DESCRIPTION					)
class 		lowercase					(					datasets.Metric					):





      def     _snake_case			(       self )		->     List[str]:
            if self.config_name not in [
                "sst2",
                "mnli",
                "mnli_mismatched",
                "mnli_matched",
                "cola",
                "stsb",
                "mrpc",
                "qqp",
                "qnli",
                "rte",
                "wnli",
                "hans",
            ]:
                  raise KeyError(
                      """You should supply a configuration name selected in """
                      """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """
                      """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )
            return datasets.MetricInfo(
                description=_DESCRIPTION      ,						citation=_CITATION      ,						inputs_description=_KWARGS_DESCRIPTION      ,						features=datasets.Features(
                    {
                        """predictions""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ),
                        """references""": datasets.Value("""int64""" if self.config_name != """stsb""" else """float32""" ),
                    } )      ,						codebase_urls=[]      ,						reference_urls=[]      ,						format="""numpy"""      ,						)





      def     _snake_case			(       self      ,						lowercase      ,						lowercase )		->     Any:
            if self.config_name == "cola":
                  return {"matthews_correlation": matthews_corrcoef(lowercase      ,						lowercase )}
            elif self.config_name == "stsb":
                  return pearson_and_spearman(lowercase      ,						lowercase )
            elif self.config_name in ["mrpc", "qqp"]:
                  return acc_and_fa(lowercase      ,						lowercase )
            elif self.config_name in ["sst2", "mnli", "mnli_mismatched", "mnli_matched", "qnli", "rte", "wnli", "hans"]:
                  return {"accuracy": simple_accuracy(lowercase      ,						lowercase )}
            else:
                  raise KeyError(
                      """You should supply a configuration name selected in """
                      """[\"sst2\", \"mnli\", \"mnli_mismatched\", \"mnli_matched\", """
                      """\"cola\", \"stsb\", \"mrpc\", \"qqp\", \"qnli\", \"rte\", \"wnli\", \"hans\"]""" )






