






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


_UpperCAmelCase			:		List[str]   				=    logging.getLogger(__name__)
class  lowerCAmelCase_       (				snake_case__					):
							UpperCamelCase_		:List[Any]		      =    'sequence-classification'




							def __init__(   self     :   Optional[int] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]	):
											if type(SCREAMING_SNAKE_CASE_	) == dict:
															lowerCAmelCase__      			=						Namespace(**SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						glue_output_modes[hparams.task]
											lowerCAmelCase__      			=						glue_tasks_num_labels[hparams.task]

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




							def 						__snake_case					(   self     :   List[Any] ,							**SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											return self.model(**SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											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(**SCREAMING_SNAKE_CASE_	)
											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     :   List[Any]	):
											lowerCAmelCase__      			=						self.hparams
											lowerCAmelCase__      			=						processors[args.task]()
											lowerCAmelCase__      			=						processor.get_labels()

											for mode in ["train", "dev"]:
															lowerCAmelCase__      			=						self._feature_file(SCREAMING_SNAKE_CASE_	)
															if os.path.exists(SCREAMING_SNAKE_CASE_	) and not args.overwrite_cache:
																			logger.info('''Loading features from cached file %s''' ,							SCREAMING_SNAKE_CASE_	)
															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(
																			    SCREAMING_SNAKE_CASE_ ,							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''' ,							SCREAMING_SNAKE_CASE_	)
																			torch.save(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)




							def 						__snake_case					(   self     :   Optional[Any] ,							SCREAMING_SNAKE_CASE_     :   str ,							SCREAMING_SNAKE_CASE_     :   int ,							SCREAMING_SNAKE_CASE_     :   bool = False	):
											lowerCAmelCase__      			=						'''dev''' if mode == '''test''' else mode

											lowerCAmelCase__      			=						self._feature_file(SCREAMING_SNAKE_CASE_	)
											logger.info('''Loading features from cached file %s''' ,							SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						torch.load(SCREAMING_SNAKE_CASE_	)
											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(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	) ,							batch_size=SCREAMING_SNAKE_CASE_ ,							shuffle=SCREAMING_SNAKE_CASE_ ,							)




							def 						__snake_case					(   self     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Any	):
											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(**SCREAMING_SNAKE_CASE_	)
											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     :   List[Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[Any]	):
											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(SCREAMING_SNAKE_CASE_ ,							axis=1	)
											elif self.hparams.glue_output_mode == "regression":
															lowerCAmelCase__      			=						np.squeeze(SCREAMING_SNAKE_CASE_	)

											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 ,							SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)}

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




							def 						__snake_case					(   self     :   Any ,							SCREAMING_SNAKE_CASE_     :   list	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self._eval_end(SCREAMING_SNAKE_CASE_	)
											lowerCAmelCase__      			=						ret['''log''']
											return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}




							def 						__snake_case					(   self     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   List[Any]	):
											lowerCAmelCase__			,		lowerCAmelCase__			,		lowerCAmelCase__      			=						self._eval_end(SCREAMING_SNAKE_CASE_	)
											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					(   SCREAMING_SNAKE_CASE_     :   Union[str, Any] ,							SCREAMING_SNAKE_CASE_     :   Optional[int]	):
											BaseTransformer.add_model_specific_args(SCREAMING_SNAKE_CASE_ ,							SCREAMING_SNAKE_CASE_	)
											parser.add_argument(
											    '''--max_seq_length''' ,							default=128 ,							type=SCREAMING_SNAKE_CASE_ ,							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=SCREAMING_SNAKE_CASE_ ,							required=SCREAMING_SNAKE_CASE_ ,							help='''The GLUE task to run''' ,							)
											parser.add_argument(
											    '''--gpus''' ,							default=0 ,							type=SCREAMING_SNAKE_CASE_ ,							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_		()		->    str:


				'''simple docstring'''




				lowerCAmelCase__      			=						argparse.ArgumentParser()
				add_generic_args(lowercase__     ,					os.getcwd()       )
				lowerCAmelCase__      			=						GLUETransformer.add_model_specific_args(lowercase__     ,					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(lowercase__       )
				lowerCAmelCase__      			=						generic_train(lowercase__     ,					lowercase__       )

				# 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=lowercase__       )       )
								lowerCAmelCase__      			=						model.load_from_checkpoint(checkpoints[-1]       )
								return trainer.test(lowercase__       )


if __name__ == "__main__":
			main()













from typing import Optional

import pyspark

from .. import Features, NamedSplit
from ..download import DownloadMode
from ..packaged_modules.spark.spark import Spark
from .abc import AbstractDatasetReader
class  lowerCAmelCase_       (				snake_case__					):




							def __init__(   self     :   str ,							SCREAMING_SNAKE_CASE_     :   pyspark.sql.DataFrame ,							SCREAMING_SNAKE_CASE_     :   Optional[NamedSplit] = None ,							SCREAMING_SNAKE_CASE_     :   Optional[Features] = None ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   str = None ,							SCREAMING_SNAKE_CASE_     :   bool = False ,							SCREAMING_SNAKE_CASE_     :   str = None ,							SCREAMING_SNAKE_CASE_     :   bool = True ,							SCREAMING_SNAKE_CASE_     :   str = "arrow" ,							**SCREAMING_SNAKE_CASE_     :   Optional[Any] ,							):
											super().__init__(
											    split=SCREAMING_SNAKE_CASE_ ,							features=SCREAMING_SNAKE_CASE_ ,							cache_dir=SCREAMING_SNAKE_CASE_ ,							keep_in_memory=SCREAMING_SNAKE_CASE_ ,							streaming=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_ ,							)
											lowerCAmelCase__      			=						load_from_cache_file
											lowerCAmelCase__      			=						file_format
											lowerCAmelCase__      			=						Spark(
											    df=SCREAMING_SNAKE_CASE_ ,							features=SCREAMING_SNAKE_CASE_ ,							cache_dir=SCREAMING_SNAKE_CASE_ ,							working_dir=SCREAMING_SNAKE_CASE_ ,							**SCREAMING_SNAKE_CASE_ ,							)




							def 						__snake_case					(   self     :   List[str]	):
											if self.streaming:
															return self.builder.as_streaming_dataset(split=self.split	)
											lowerCAmelCase__      			=						None if self._load_from_cache_file else DownloadMode.FORCE_REDOWNLOAD
											self.builder.download_and_prepare(
											    download_mode=SCREAMING_SNAKE_CASE_ ,							file_format=self._file_format ,							)
											return self.builder.as_dataset(split=self.split	)





