


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


lowerCAmelCase__				    =       logging.getLogger(__name__)


class 				SCREAMING_SNAKE_CASE__    (		snake_case__    ):
							"""simple docstring"""


							a  :    Tuple       ="sequence-classification"

							def __init__(						self		,					snake_case__						):






									"""simple docstring"""





									if type(UpperCAmelCase_						) == dict:
											lowerCAmelCase					:					List[Any]								=							Namespace(**UpperCAmelCase_						)
									lowerCAmelCase					:					Union[str, Any]								=							glue_output_modes[hparams.task]
									lowerCAmelCase					:					List[str]								=							glue_tasks_num_labels[hparams.task]

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

							def 	lowercase__							(						self		,					**snake_case__						):






									"""simple docstring"""





									return self.model(**UpperCAmelCase_						)

							def 	lowercase__							(						self		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					Dict								=							{"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}

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

									lowerCAmelCase					:					Any								=							self(**UpperCAmelCase_						)
									lowerCAmelCase					:					Union[str, Any]								=							outputs[0]

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

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					List[str]								=							self.hparams
									lowerCAmelCase					:					int								=							processors[args.task]()
									lowerCAmelCase					:					Any								=							processor.get_labels()

									for mode in ["train", "dev"]:
											lowerCAmelCase					:					Tuple								=							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						)
													lowerCAmelCase					:					Optional[Any]								=							(
													    processor.get_dev_examples(args.data_dir						)
													    if mode == "dev"
													    else processor.get_train_examples(args.data_dir						)
													)
													lowerCAmelCase					:					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 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__ = False						):






									"""simple docstring"""





									lowerCAmelCase					:					Union[str, Any]								=							"dev" if mode == "test" else mode

									lowerCAmelCase					:					int								=							self._feature_file(UpperCAmelCase_						)
									logger.info("Loading features from cached file %s"		,					UpperCAmelCase_						)
									lowerCAmelCase					:					Union[str, Any]								=							torch.load(UpperCAmelCase_						)
									lowerCAmelCase					:					List[Any]								=							torch.tensor([f.input_ids for f in features]		,					dtype=torch.long						)
									lowerCAmelCase					:					Any								=							torch.tensor([f.attention_mask for f in features]		,					dtype=torch.long						)
									lowerCAmelCase					:					int								=							torch.tensor([f.token_type_ids for f in features]		,					dtype=torch.long						)
									if self.hparams.glue_output_mode == "classification":
											lowerCAmelCase					:					List[str]								=							torch.tensor([f.label for f in features]		,					dtype=torch.long						)
									elif self.hparams.glue_output_mode == "regression":
											lowerCAmelCase					:					List[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 	lowercase__							(						self		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					Tuple								=							{"input_ids": batch[0], "attention_mask": batch[1], "labels": batch[3]}

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

									lowerCAmelCase					:					Any								=							self(**UpperCAmelCase_						)
									lowerCAmelCase					:					Dict								=							outputs[:2]
									lowerCAmelCase					:					Union[str, Any]								=							logits.detach().cpu().numpy()
									lowerCAmelCase					:					Optional[Any]								=							inputs["labels"].detach().cpu().numpy()

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

							def 	lowercase__							(						self		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[int]								=							torch.stack([x["val_loss"] for x in outputs]						).mean().detach().cpu().item()
									lowerCAmelCase					:					Tuple								=							np.concatenate([x["pred"] for x in outputs]		,					axis=0						)

									if self.hparams.glue_output_mode == "classification":
											lowerCAmelCase					:					Optional[Any]								=							np.argmax(UpperCAmelCase_		,					axis=1						)
									elif self.hparams.glue_output_mode == "regression":
											lowerCAmelCase					:					Optional[int]								=							np.squeeze(UpperCAmelCase_						)

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

									lowerCAmelCase					:					Optional[int]								=							{**{"val_loss": val_loss_mean}, **compute_metrics(self.hparams.task		,					UpperCAmelCase_		,					UpperCAmelCase_						)}

									lowerCAmelCase					:					int								=							dict(results.items()						)
									lowerCAmelCase					:					Union[str, Any]								=							results
									return ret, preds_list, out_label_list

							def 	lowercase__							(						self		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							self._eval_end(UpperCAmelCase_						)
									lowerCAmelCase					:					List[Any]								=							ret["log"]
									return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}

							def 	lowercase__							(						self		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					int								=							self._eval_end(UpperCAmelCase_						)
									lowerCAmelCase					:					Union[str, 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 	lowercase__							(						snake_case__		,					snake_case__						):






									"""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  a__      (    ):


		'''simple docstring'''







		lowerCAmelCase					:					Optional[Any]								=							argparse.ArgumentParser()
		add_generic_args(_UpperCAmelCase    ,				os.getcwd()    )
		lowerCAmelCase					:					Optional[Any]								=							GLUETransformer.add_model_specific_args(_UpperCAmelCase    ,				os.getcwd()    )
		lowerCAmelCase					:					str								=							parser.parse_args()

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

		lowerCAmelCase					:					List[Any]								=							GLUETransformer(_UpperCAmelCase    )
		lowerCAmelCase					:					List[str]								=							generic_train(_UpperCAmelCase    ,				_UpperCAmelCase    )

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


if __name__ == "__main__":
					main()









"""simple docstring"""







import unittest

from datasets import load_dataset

from transformers import BloomTokenizerFast
from transformers.testing_utils import require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin


@require_tokenizers
class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   unittest.TestCase    ):
       """simple docstring"""


       a  :    List[str]       =None
       a  :    List[Any]       =BloomTokenizerFast
       a  :    Optional[int]       =BloomTokenizerFast
       a  :    Optional[Any]       =True
       a  :    Dict       =False
       a  :    Optional[Any]       ="tokenizer_file"
       a  :    Optional[int]       ={"bos_token": "<s>", "eos_token": "</s>", "unk_token": "<unk>", "pad_token": "<pad>"}

       def 	lowercase__							(						self						):






         """simple docstring"""





         super().setUp()
         lowerCAmelCase					:					Tuple								=							BloomTokenizerFast.from_pretrained("bigscience/tokenizer"						)
         tokenizer.save_pretrained(self.tmpdirname						)

       def 	lowercase__							(						self		,					**snake_case__						):






         """simple docstring"""





         kwargs.update(self.special_tokens_map						)
         return BloomTokenizerFast.from_pretrained(self.tmpdirname		,					**snake_case__						)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.get_rust_tokenizer()

         lowerCAmelCase					:					List[Any]								=							["The quick brown fox</s>", "jumps over the lazy dog</s>"]
         lowerCAmelCase					:					str								=							[[2_175, 23_714, 73_173, 144_252, 2], [77, 132_619, 3_478, 368, 109_586, 35_433, 2]]

         lowerCAmelCase					:					Optional[int]								=							tokenizer.batch_encode_plus(snake_case__						)["input_ids"]
         self.assertListEqual(snake_case__		,					snake_case__						)

         lowerCAmelCase					:					Optional[int]								=							tokenizer.batch_decode(snake_case__						)
         self.assertListEqual(snake_case__		,					snake_case__						)

       def 	lowercase__							(						self		,					snake_case__=6						):






         """simple docstring"""





         for tokenizer, pretrained_name, kwargs in self.tokenizers_list:
           with self.subTest(f"""{tokenizer.__class__.__name__} ({pretrained_name})"""						):
             lowerCAmelCase					:					Tuple								=							self.rust_tokenizer_class.from_pretrained(snake_case__		,					**snake_case__						)
             # tokenizer_r.pad_token = None # Hotfixing padding = None
             # Simple input
             lowerCAmelCase					:					str								=							"This is a simple input"
             lowerCAmelCase					:					Tuple								=							["This is a simple input 1", "This is a simple input 2"]
             lowerCAmelCase					:					Any								=							("This is a simple input", "This is a pair")
             lowerCAmelCase					:					Tuple								=							[
                 ("This is a simple input 1", "This is a simple input 2"),
                 ("This is a simple pair 1", "This is a simple pair 2"),
             ]

             # Simple input tests
             try:
               tokenizer_r.encode(snake_case__		,					max_length=snake_case__						)
               tokenizer_r.encode_plus(snake_case__		,					max_length=snake_case__						)

               tokenizer_r.batch_encode_plus(snake_case__		,					max_length=snake_case__						)
               tokenizer_r.encode(snake_case__		,					max_length=snake_case__						)
               tokenizer_r.batch_encode_plus(snake_case__		,					max_length=snake_case__						)
             except ValueError:
               self.fail("Bloom Tokenizer should be able to deal with padding"						)

             lowerCAmelCase					:					Tuple								=							None  # Hotfixing padding = None
             self.assertRaises(snake_case__		,					tokenizer_r.encode		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"						)

             # Simple input
             self.assertRaises(snake_case__		,					tokenizer_r.encode_plus		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"						)

             # Simple input
             self.assertRaises(
                 snake_case__		,					tokenizer_r.batch_encode_plus		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"		,					)

             # Pair input
             self.assertRaises(snake_case__		,					tokenizer_r.encode		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"						)

             # Pair input
             self.assertRaises(snake_case__		,					tokenizer_r.encode_plus		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"						)

             # Pair input
             self.assertRaises(
                 snake_case__		,					tokenizer_r.batch_encode_plus		,					snake_case__		,					max_length=snake_case__		,					padding="max_length"		,					)

       def 	lowercase__							(						self						):






         """simple docstring"""





         lowerCAmelCase					:					int								=							self.get_rust_tokenizer()
         lowerCAmelCase					:					int								=							load_dataset("xnli"		,					"all_languages"		,					split="test"		,					streaming=snake_case__						)

         lowerCAmelCase					:					Tuple								=							next(iter(snake_case__						)						)["premise"]  # pick up one data
         lowerCAmelCase					:					Optional[Any]								=							list(sample_data.values()						)

         lowerCAmelCase					:					int								=							list(map(tokenizer.encode		,					snake_case__						)						)
         lowerCAmelCase					:					List[Any]								=							[tokenizer.decode(snake_case__		,					clean_up_tokenization_spaces=snake_case__						) for x in output_tokens]
         self.assertListEqual(snake_case__		,					snake_case__						)







       def 	lowercase__							(						self						):






         """simple docstring"""





         self.assertGreaterEqual(len(self.tokenizer_class.pretrained_vocab_files_map						)		,					1						)
         self.assertGreaterEqual(len(list(self.tokenizer_class.pretrained_vocab_files_map.values()						)[0]						)		,					1						)





