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


_a														=		logging.getLogger(__name__)






class    _UpperCAmelCase(   _A     ):
		lowercase__      =				'sequence-classification'



		def __init__( self    ,		__a)	->	Tuple:






							'''simple docstring'''
							if type(__SCREAMING_SNAKE_CASE) == dict:
												_UpperCamelCase										=  Namespace(**__SCREAMING_SNAKE_CASE)
							_UpperCamelCase										=  glue_output_modes[hparams.task]
							_UpperCamelCase										=  glue_tasks_num_labels[hparams.task]

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



		def 						UpperCAmelCase							( self    ,		**__a)	->	Optional[int]:






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



		def 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	str:






							'''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(**__SCREAMING_SNAKE_CASE)
							_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 						UpperCAmelCase							( self)	->	Dict:






							'''simple docstring'''
							_UpperCamelCase										=  self.hparams
							_UpperCamelCase										=  processors[args.task]()
							_UpperCamelCase										=  processor.get_labels()

							for mode in ["train", "dev"]:
												_UpperCamelCase										=  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)
																	_UpperCamelCase										=  (
																	    processor.get_dev_examples(args.data_dir)
																	    if mode == '''dev'''
																	    else processor.get_train_examples(args.data_dir)
																	)
																	_UpperCamelCase										=  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 						UpperCAmelCase							( self    ,		__a    ,		__a    ,		__a = False)	->	int:






							'''simple docstring'''
							_UpperCamelCase										=  '''dev''' if mode == '''test''' else mode

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



		def 						UpperCAmelCase							( self    ,		__a    ,		__a)	->	Tuple:






							'''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(**__SCREAMING_SNAKE_CASE)
							_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 						UpperCAmelCase							( self    ,		__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(__SCREAMING_SNAKE_CASE    ,		axis=1)
							elif self.hparams.glue_output_mode == "regression":
												_UpperCamelCase										=  np.squeeze(__SCREAMING_SNAKE_CASE)

							_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    ,		__SCREAMING_SNAKE_CASE    ,		__SCREAMING_SNAKE_CASE)}

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



		def 						UpperCAmelCase							( self    ,		__a)	->	List[Any]:






							'''simple docstring'''
							_UpperCamelCase			,  _UpperCamelCase			,  _UpperCamelCase										=  self._eval_end(__SCREAMING_SNAKE_CASE)
							_UpperCamelCase										=  ret['''log''']
							return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}



		def 						UpperCAmelCase							( self    ,		__a)	->	List[Any]:






							'''simple docstring'''
							_UpperCamelCase			,  _UpperCamelCase			,  _UpperCamelCase										=  self._eval_end(__SCREAMING_SNAKE_CASE)
							_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 						UpperCAmelCase							( __a    ,		__a)	->	str:






							'''simple docstring'''
							BaseTransformer.add_model_specific_args(__SCREAMING_SNAKE_CASE    ,		__SCREAMING_SNAKE_CASE)
							parser.add_argument(
							    '''--max_seq_length'''    ,		default=1_28    ,		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__		(      )							->		Optional[Any]:


					"""simple docstring"""





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

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


if __name__ == "__main__":
				main()








"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


_a														=		{
    """configuration_trocr""": ["""TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP""", """TrOCRConfig"""],
    """processing_trocr""": ["""TrOCRProcessor"""],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				_a														=		[
				    """TROCR_PRETRAINED_MODEL_ARCHIVE_LIST""",
				    """TrOCRForCausalLM""",
				    """TrOCRPreTrainedModel""",
				]


if TYPE_CHECKING:
				from .configuration_trocr import TROCR_PRETRAINED_CONFIG_ARCHIVE_MAP, TrOCRConfig
				from .processing_trocr import TrOCRProcessor

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_trocr import TROCR_PRETRAINED_MODEL_ARCHIVE_LIST, TrOCRForCausalLM, TrOCRPreTrainedModel

else:
				import sys

				_a														=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






