


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


__UpperCAmelCase					       =							logging.getLogger(__name__)




class 	a__       (							a__						):





							'''simple docstring'''






							lowercase__    :     List[str]					=   "sequence-classification"







							def __init__(	self      ,					lowerCamelCase_     ) ->							List[Any]:
														if type(lowerCamelCase_     ) == dict:
																					lowerCAmelCase__      =			Namespace(**lowerCamelCase_     )
														lowerCAmelCase__      =			glue_output_modes[hparams.task]
														lowerCAmelCase__      =			glue_tasks_num_labels[hparams.task]

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					**lowerCamelCase_     ) ->							List[Any]:
														return self.model(**lowerCamelCase_     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							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(**lowerCamelCase_     )
														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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			self.hparams
														lowerCAmelCase__      =			processors[args.task]()
														lowerCAmelCase__      =			processor.get_labels()

														for mode in ["train", "dev"]:
																					lowerCAmelCase__      =			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     )
																												lowerCAmelCase__      =			(
																												    processor.get_dev_examples(args.data_dir     )
																												    if mode == '''dev'''
																												    else processor.get_train_examples(args.data_dir     )
																												)
																												lowerCAmelCase__      =			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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_      ,					lowerCamelCase_ = False     ) ->							DataLoader:
														lowerCAmelCase__      =			'''dev''' if mode == '''test''' else mode

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_      ,					lowerCamelCase_     ) ->							Optional[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(**lowerCamelCase_     )
														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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							tuple:
														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(lowerCamelCase_      ,					axis=1     )
														elif self.hparams.glue_output_mode == "regression":
																					lowerCAmelCase__      =			np.squeeze(lowerCamelCase_     )

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

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







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							dict:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self._eval_end(lowerCamelCase_     )
														lowerCAmelCase__      =			ret['''log''']
														return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							dict:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			self._eval_end(lowerCamelCase_     )
														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 						__SCREAMING_SNAKE_CASE							(	lowerCamelCase_      ,					lowerCamelCase_     ) ->							Union[str, Any]:
														BaseTransformer.add_model_specific_args(lowerCamelCase_      ,					lowerCamelCase_     )
														parser.add_argument(
														    '''--max_seq_length'''      ,					default=1_28      ,					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       _snake_case     (  )		->					List[Any]:
							lowerCAmelCase__      =			argparse.ArgumentParser()
							add_generic_args(A				,					os.getcwd() )
							lowerCAmelCase__      =			GLUETransformer.add_model_specific_args(A				,					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(A )
							lowerCAmelCase__      =			generic_train(A				,					A )

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


if __name__ == "__main__":
		main()










'''simple docstring'''






from manim import *




class 	a__       (							a__						):





							'''simple docstring'''






							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			Rectangle(height=0.5      ,					width=0.5     )
														lowerCAmelCase__      =			Rectangle(height=0.46      ,					width=0.46     ).set_stroke(width=0     )

														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			VGroup(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			Text('''CPU'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														cpu.move_to([-2.5, -0.5, 0]     )
														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[mem.copy() for i in range(1     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )
														lowerCAmelCase__      =			Text('''GPU'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														gpu.align_to(lowerCamelCase_      ,					lowerCamelCase_     )
														gpu.set_x(gpu.get_x() - 1     )

														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[mem.copy() for i in range(6     )]
														lowerCAmelCase__      =			VGroup(*lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0     )

														lowerCAmelCase__      =			Text('''Model'''      ,					font_size=24     )
														lowerCAmelCase__      =			Group(lowerCamelCase_      ,					lowerCamelCase_     ).arrange(lowerCamelCase_      ,					buff=0.5      ,					aligned_edge=lowerCamelCase_     )
														model.move_to([3, -1.0, 0]     )

														self.play(
														    Create(lowerCamelCase_      ,					run_time=1     )      ,					Create(lowerCamelCase_      ,					run_time=1     )      ,					Create(lowerCamelCase_      ,					run_time=1     )      ,					)

														lowerCAmelCase__      =			MarkupText(
														    F"""First, an empty model skeleton is loaded\ninto <span fgcolor='{YELLOW}'>memory</span> without using much RAM."""      ,					font_size=24      ,					)

														lowerCAmelCase__      =			Square(side_length=2.2     )
														key.move_to([-5, 2, 0]     )

														lowerCAmelCase__      =			MarkupText(
														    F"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""      ,					font_size=18      ,					)

														key_text.move_to([-5, 2.4, 0]     )

														step_a.move_to([2, 2, 0]     )
														self.play(Write(lowerCamelCase_      ,					run_time=2.5     )      ,					Write(lowerCamelCase_     )      ,					Write(lowerCamelCase_     )     )

														self.add(lowerCamelCase_     )

														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														lowerCAmelCase__      =			[]
														for i, rect in enumerate(lowerCamelCase_     ):
																					lowerCAmelCase__      =			Rectangle(height=0.46      ,					width=0.46     ).set_stroke(width=0.0     ).set_fill(lowerCamelCase_      ,					opacity=0.7     )
																					cpu_target.move_to(lowerCamelCase_     )
																					cpu_target.generate_target()
																					lowerCAmelCase__      =			0.46 / 4
																					lowerCAmelCase__      =			0.46 / 3

																					if i == 0:
																												cpu_target.target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT     )      ,					buff=0.02      ,					direction=lowerCamelCase_     )
																												cpu_target.target.set_x(cpu_target.target.get_x() + 0.1     )
																					elif i == 3:
																												cpu_target.target.next_to(cpu_targs[0].target      ,					direction=lowerCamelCase_      ,					buff=0.0     )
																					else:
																												cpu_target.target.next_to(cpu_targs[i - 1].target      ,					direction=lowerCamelCase_      ,					buff=0.0     )
																					cpu_targs.append(lowerCamelCase_     )

																					first_animations.append(rect.animate(run_time=0.5     ).set_stroke(lowerCamelCase_     )     )
																					second_animations.append(MoveToTarget(lowerCamelCase_      ,					run_time=1.5     )     )

														self.play(*lowerCamelCase_     )
														self.play(*lowerCamelCase_     )

														self.wait()