




"""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   a__					(						__magic_name__ ):
			lowercase_		=						"sequence-classification"


			def __init__(   self   :     Optional[Any]   ,	UpperCamelCase_   :     int):


						"""simple docstring"""




						if type(UpperCamelCase_) == dict:
									__UpperCAmelCase						:			Optional[int]												=       Namespace(**UpperCamelCase_)
						__UpperCAmelCase						:			Union[str, Any]												=       glue_output_modes[hparams.task]
						__UpperCAmelCase						:			Optional[Any]												=       glue_tasks_num_labels[hparams.task]

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


			def    a_  (   self   :     Optional[Any]   ,	**UpperCamelCase_   :     Optional[int]):


						"""simple docstring"""




						return self.model(**UpperCamelCase_)


			def    a_  (   self   :     Optional[Any]   ,	UpperCamelCase_   :     List[Any]   ,	UpperCamelCase_   :     int):


						"""simple docstring"""




						__UpperCAmelCase						:			Optional[int]												=       {"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}

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

						__UpperCAmelCase						:			Optional[Any]												=       self(**UpperCamelCase_)
						__UpperCAmelCase						:			int												=       outputs[0]

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


			def    a_  (   self   :     Any):


						"""simple docstring"""




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

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


			def    a_  (   self   :     Tuple   ,	UpperCamelCase_   :     str   ,	UpperCamelCase_   :     int   ,	UpperCamelCase_   :     bool = False):


						"""simple docstring"""




						__UpperCAmelCase						:			Dict												=       "dev" if mode == "test" else mode

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

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


			def    a_  (   self   :     List[Any]   ,	UpperCamelCase_   :     str   ,	UpperCamelCase_   :     Optional[Any]):


						"""simple docstring"""




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

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

						__UpperCAmelCase						:			Any												=       self(**UpperCamelCase_)
						__UpperCAmelCase						,  __UpperCAmelCase						:			Tuple												=       outputs[:2]
						__UpperCAmelCase						:			List[Any]												=       logits.detach().cpu().numpy()
						__UpperCAmelCase						:			List[Any]												=       inputs["labels"].detach().cpu().numpy()

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


			def    a_  (   self   :     List[Any]   ,	UpperCamelCase_   :     List[Any]):


						"""simple docstring"""




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

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

						__UpperCAmelCase						:			List[str]												=       np.concatenate([x["target"] for x in outputs]   ,	axis=0)
						__UpperCAmelCase						:			Optional[Any]												=       [[] for _ in range(out_label_ids.shape[0])]
						__UpperCAmelCase						:			Any												=       [[] for _ in range(out_label_ids.shape[0])]

						__UpperCAmelCase						:			Union[str, Any]												=       {**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task   ,	UpperCamelCase_   ,	UpperCamelCase_)}

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


			def    a_  (   self   :     Union[str, Any]   ,	UpperCamelCase_   :     list):


						"""simple docstring"""




						__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			Dict												=       self._eval_end(UpperCamelCase_)
						__UpperCAmelCase						:			List[Any]												=       ret["log"]
						return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}


			def    a_  (   self   :     List[str]   ,	UpperCamelCase_   :     str):


						"""simple docstring"""




						__UpperCAmelCase						,  __UpperCAmelCase						,  __UpperCAmelCase						:			Tuple												=       self._eval_end(UpperCamelCase_)
						__UpperCAmelCase						:			Any												=       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    a_  (   UpperCamelCase_   :     Any   ,	UpperCamelCase_   :     List[Any]):


						"""simple docstring"""




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


			"""simple docstring"""
			__UpperCAmelCase						:			str												=       argparse.ArgumentParser()
			add_generic_args(UpperCamelCase					,							os.getcwd()				)
			__UpperCAmelCase						:			int												=       GLUETransformer.add_model_specific_args(UpperCamelCase					,							os.getcwd()				)
			__UpperCAmelCase						:			List[str]												=       parser.parse_args()

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

			__UpperCAmelCase						:			Tuple												=       GLUETransformer(UpperCamelCase				)
			__UpperCAmelCase						:			int												=       generic_train(UpperCamelCase					,							UpperCamelCase				)

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


if __name__ == "__main__":
					main()









'''simple docstring'''




import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


lowercase__       : Any         =       '''hf-internal-testing/tiny-random-bert'''
lowercase__       : Optional[Any]         =       os.path.join(TRANSFORMERS_CACHE, '''models--hf-internal-testing--tiny-random-bert''')
lowercase__       : List[Any]         =       '''9b8c223d42b2188cb49d29af482996f9d0f3e5a6'''


class 					SCREAMING_SNAKE_CASE       (unittest.TestCase				):


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase)
          # Should have downloaded the file in here
          self.assertTrue(os.path.isdir(_UpperCAmelCase))
          # Cache should contain at least those three subfolders:
          for subfolder in ["blobs", "refs", "snapshots"]:
               self.assertTrue(os.path.isdir(os.path.join(_UpperCAmelCase		,      _UpperCAmelCase)))
          with open(os.path.join(_UpperCAmelCase		,      'refs'		,      'main')) as f:
               __A		:					Any						=			f.read()
          self.assertEqual(_UpperCAmelCase		,      os.path.join(_UpperCAmelCase		,      'snapshots'		,      _UpperCAmelCase		,      _UpperCAmelCase))
          self.assertTrue(os.path.isfile(_UpperCAmelCase))

          # File is cached at the same place the second time.
          __A		:					Tuple						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase)
          self.assertEqual(_UpperCAmelCase		,      _UpperCAmelCase)

          # Using a specific revision to test the full commit hash.
          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase		,      revision='9b8c223')
          self.assertEqual(_UpperCAmelCase		,      os.path.join(_UpperCAmelCase		,      'snapshots'		,      _UpperCAmelCase		,      _UpperCAmelCase))


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid model identifier'):
               __A		:					Dict						=			cached_file('tiny-random-bert'		,      _UpperCAmelCase)

          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid git identifier'):
               __A		:					Optional[int]						=			cached_file(_UpperCAmelCase		,      _UpperCAmelCase		,      revision='aaaa')

          with self.assertRaisesRegex(_UpperCAmelCase		,      'does not appear to have a file named'):
               __A		:					int						=			cached_file(_UpperCAmelCase		,      'conf')


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with self.assertRaisesRegex(_UpperCAmelCase		,      'does not appear to have a file named'):
               __A		:					Any						=			cached_file(_UpperCAmelCase		,      'conf')

          with open(os.path.join(_UpperCAmelCase		,      'refs'		,      'main')) as f:
               __A		:					Dict						=			f.read()
          self.assertTrue(os.path.isfile(os.path.join(_UpperCAmelCase		,      '.no_exist'		,      _UpperCAmelCase		,      'conf')))

          __A		:					List[Any]						=			cached_file(_UpperCAmelCase		,      'conf'		,      _raise_exceptions_for_missing_entries=_UpperCAmelCase)
          self.assertIsNone(_UpperCAmelCase)

          __A		:					str						=			cached_file(_UpperCAmelCase		,      'conf'		,      local_files_only=_UpperCAmelCase		,      _raise_exceptions_for_missing_entries=_UpperCAmelCase)
          self.assertIsNone(_UpperCAmelCase)

          __A		:					List[str]						=			mock.Mock()
          __A		:					Dict						=			500
          __A		:					List[str]						=			{}
          __A		:					List[Any]						=			HTTPError
          __A		:					Optional[Any]						=			{}

          # Under the mock environment we get a 500 error when trying to reach the tokenizer.
          with mock.patch('requests.Session.request'		,      return_value=_UpperCAmelCase) as mock_head:
               __A		:					Dict						=			cached_file(_UpperCAmelCase		,      'conf'		,      _raise_exceptions_for_connection_errors=_UpperCAmelCase)
               self.assertIsNone(_UpperCAmelCase)
               # This check we did call the fake head request
               mock_head.assert_called()


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.assertTrue(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))
          self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))
          self.assertFalse(has_file('hf-internal-testing/tiny-bert-pt-only'		,      _UpperCAmelCase))


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          self.assertIsNone(get_file_from_repo('bert-base-cased'		,      'ahah.txt'))

          # The function raises if the repository does not exist.
          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid model identifier'):
               get_file_from_repo('bert-base-case'		,      _UpperCAmelCase)

          # The function raises if the revision does not exist.
          with self.assertRaisesRegex(_UpperCAmelCase		,      'is not a valid git identifier'):
               get_file_from_repo('bert-base-cased'		,      _UpperCAmelCase		,      revision='ahaha')

          __A		:					List[str]						=			get_file_from_repo('bert-base-cased'		,      _UpperCAmelCase)
          # The name is the cached name which is not very easy to test, so instead we load the content.
          __A		:					List[str]						=			json.loads(open(_UpperCAmelCase		,      'r').read())
          self.assertEqual(config['hidden_size']		,      768)


     def 				SCREAMING_SNAKE_CASE    (		self):

          '''simple docstring'''






          with tempfile.TemporaryDirectory() as tmp_dir:
               __A		:					Tuple						=			Path(_UpperCAmelCase) / 'a.txt'
               filename.touch()
               self.assertEqual(get_file_from_repo(_UpperCAmelCase		,      'a.txt')		,      str(_UpperCAmelCase))

               self.assertIsNone(get_file_from_repo(_UpperCAmelCase		,      'b.txt'))