


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									=		logging.getLogger(__name__)



class      __lowerCamelCase       (lowerCAmelCase__		):
       _lowercase											=			'''sequence-classification'''







       def __init__(       self:      Any,A_:      Any ):






            '''simple docstring'''




            if type(__lowerCAmelCase ) == dict:
                 __UpperCamelCase      =       Namespace(**__lowerCAmelCase )
            __UpperCamelCase      =       glue_output_modes[hparams.task]
            __UpperCamelCase      =       glue_tasks_num_labels[hparams.task]

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







       def 						snake_case_     (       self:      Dict,**A_:      Dict ):






            '''simple docstring'''




            return self.model(**__lowerCAmelCase )







       def 						snake_case_     (       self:      Union[str, Any],A_:      Optional[int],A_:      Optional[int] ):






            '''simple docstring'''




            __UpperCamelCase      =       {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[3]}

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

            __UpperCamelCase      =       self(**__lowerCAmelCase )
            __UpperCamelCase      =       outputs[0]

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







       def 						snake_case_     (       self:      List[Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       self.hparams
            __UpperCamelCase      =       processors[args.task]()
            __UpperCamelCase      =       processor.get_labels()

            for mode in ["train", "dev"]:
                 __UpperCamelCase      =       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 )
                      __UpperCamelCase      =       (
                          processor.get_dev_examples(args.data_dir )
                          if mode == 'dev'
                          else processor.get_train_examples(args.data_dir )
                      )
                      __UpperCamelCase      =       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 						snake_case_     (       self:      int,A_:      str,A_:      Tuple,A_:      Any = False ):






            '''simple docstring'''




            __UpperCamelCase      =       'dev' if mode == 'test' else mode

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

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







       def 						snake_case_     (       self:      Optional[Any],A_:      Union[str, Any],A_:      Union[str, Any] ):






            '''simple docstring'''




            __UpperCamelCase      =       {'input_ids': batch[0], 'attention_mask': batch[1], 'labels': batch[3]}

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

            __UpperCamelCase      =       self(**__lowerCAmelCase )
            __UpperCamelCase,  __UpperCamelCase      =       outputs[:2]
            __UpperCamelCase      =       logits.detach().cpu().numpy()
            __UpperCamelCase      =       inputs['labels'].detach().cpu().numpy()

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







       def 						snake_case_     (       self:      Optional[Any],A_:      Optional[int] ):






            '''simple docstring'''




            __UpperCamelCase      =       torch.stack([x['val_loss'] for x in outputs] ).mean().detach().cpu().item()
            __UpperCamelCase      =       np.concatenate([x['pred'] for x in outputs],axis=0 )

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

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

            __UpperCamelCase      =       {**{'val_loss': val_loss_mean}, **compute_metrics(self.hparams.task,__lowerCAmelCase,__lowerCAmelCase )}

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







       def 						snake_case_     (       self:      Optional[int],A_:      Dict ):






            '''simple docstring'''




            __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase      =       self._eval_end(__lowerCAmelCase )
            __UpperCamelCase      =       ret['log']
            return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}







       def 						snake_case_     (       self:      List[str],A_:      Tuple ):






            '''simple docstring'''




            __UpperCamelCase,  __UpperCamelCase,  __UpperCamelCase      =       self._eval_end(__lowerCAmelCase )
            __UpperCamelCase      =       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_     (       A_:      Optional[Any],A_:      Dict ):






            '''simple docstring'''




            BaseTransformer.add_model_specific_args(__lowerCAmelCase,__lowerCAmelCase )
            parser.add_argument(
                '--max_seq_length',default=128,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 						_A						(						) ->      Optional[Any]:


     """simple docstring"""


     __UpperCamelCase      =       argparse.ArgumentParser()
     add_generic_args(_A ,   os.getcwd()      )
     __UpperCamelCase      =       GLUETransformer.add_model_specific_args(_A ,   os.getcwd()      )
     __UpperCamelCase      =       parser.parse_args()

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

     __UpperCamelCase      =       GLUETransformer(_A      )
     __UpperCamelCase      =       generic_train(_A ,   _A      )

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


if __name__ == "__main__":
    main()



'''simple docstring'''




import os
import string
import sys


__lowercase          =  1 << 8

__lowercase          =  {
    '''tab''': ord('''\t'''),
    '''newline''': ord('''\r'''),
    '''esc''': 2_7,
    '''up''': 6_5 + ARROW_KEY_FLAG,
    '''down''': 6_6 + ARROW_KEY_FLAG,
    '''right''': 6_7 + ARROW_KEY_FLAG,
    '''left''': 6_8 + ARROW_KEY_FLAG,
    '''mod_int''': 9_1,
    '''undefined''': sys.maxsize,
    '''interrupt''': 3,
    '''insert''': 5_0,
    '''delete''': 5_1,
    '''pg_up''': 5_3,
    '''pg_down''': 5_4,
}

__lowercase          =  KEYMAP['''up''']
__lowercase          =  KEYMAP['''left''']

if sys.platform == "win32":
						__lowercase          =  []
						__lowercase          =  {
						    B'''\xe0H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
						    B'''\x00H''': KEYMAP['''up'''] - ARROW_KEY_FLAG,
						    B'''\xe0P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
						    B'''\x00P''': KEYMAP['''down'''] - ARROW_KEY_FLAG,
						    B'''\xe0M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
						    B'''\x00M''': KEYMAP['''right'''] - ARROW_KEY_FLAG,
						    B'''\xe0K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
						    B'''\x00K''': KEYMAP['''left'''] - ARROW_KEY_FLAG,
						}

for i in range(1_0):
						__lowercase          =  ord(str(i))






def 				snake_case__							(			)    ->	List[Any]:

		'''simple docstring'''


		if os.name == "nt":
				import msvcrt

				lowerCAmelCase   					=  """mbcs"""
				# Flush the keyboard buffer
				while msvcrt.kbhit():
						msvcrt.getch()
				if len(_A ) == 0:
						# Read the keystroke
						lowerCAmelCase   					=  msvcrt.getch()

						# If it is a prefix char, get second part
						if ch in (b"\x00", b"\xe0"):
								lowerCAmelCase   					=  ch + msvcrt.getch()
								# Translate actual Win chars to bullet char types
								try:
										lowerCAmelCase   					=  chr(WIN_KEYMAP[cha] )
										WIN_CH_BUFFER.append(chr(KEYMAP["""mod_int"""] ) )
										WIN_CH_BUFFER.append(_A )
										if ord(_A ) in (
										    KEYMAP["insert"] - 1 << 9,
										    KEYMAP["delete"] - 1 << 9,
										    KEYMAP["pg_up"] - 1 << 9,
										    KEYMAP["pg_down"] - 1 << 9,
										):
												WIN_CH_BUFFER.append(chr(126 ) )
										lowerCAmelCase   					=  chr(KEYMAP["""esc"""] )
								except KeyError:
										lowerCAmelCase   					=  cha[1]
						else:
								lowerCAmelCase   					=  ch.decode(_A )
				else:
						lowerCAmelCase   					=  WIN_CH_BUFFER.pop(0 )
		elif os.name == "posix":
				import termios
				import tty

				lowerCAmelCase   					=  sys.stdin.fileno()
				lowerCAmelCase   					=  termios.tcgetattr(_A )
				try:
						tty.setraw(_A )
						lowerCAmelCase   					=  sys.stdin.read(1 )
				finally:
						termios.tcsetattr(_A      ,   termios.TCSADRAIN      ,   _A )
		return ch






def 				snake_case__							(			)    ->	Tuple:

		'''simple docstring'''


		lowerCAmelCase   					=  get_raw_chars()
		if ord(_A ) in [KEYMAP["interrupt"], KEYMAP["newline"]]:
				return char

		elif ord(_A ) == KEYMAP["esc"]:
				lowerCAmelCase   					=  get_raw_chars()
				if ord(_A ) == KEYMAP["mod_int"]:
						lowerCAmelCase   					=  get_raw_chars()
						if ord(_A ) >= KEYMAP["arrow_begin"] - ARROW_KEY_FLAG and ord(_A ) <= KEYMAP["arrow_end"] - ARROW_KEY_FLAG:
								return chr(ord(_A ) + ARROW_KEY_FLAG )
						else:
								return KEYMAP["undefined"]
				else:
						return get_raw_chars()

		else:
				if char in string.printable:
						return char
				else:
						return KEYMAP["undefined"]




