"""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				:		Tuple	   =       logging.getLogger(__name__)
class 						__A							(			SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:      List[str]		 =		"sequence-classification"






     def __init__(		self    ,					a__  ):
       if type(a__  ) == dict:
         _lowerCAmelCase						:	Optional[Any]        =					Namespace(**a__  )
       _lowerCAmelCase						:	Tuple        =					glue_output_modes[hparams.task]
       _lowerCAmelCase						:	str        =					glue_tasks_num_labels[hparams.task]

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






     def    __A  (		self    ,					**a__  ):
       return self.model(**a__  )






     def    __A  (		self    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	Union[str, Any]        =					{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

       _lowerCAmelCase						:	List[Any]        =					self(**a__  )
       _lowerCAmelCase						:	Any        =					outputs[0]

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






     def    __A  (		self  ):
       _lowerCAmelCase						:	List[str]        =					self.hparams
       _lowerCAmelCase						:	Optional[Any]        =					processors[args.task]()
       _lowerCAmelCase						:	List[str]        =					processor.get_labels()

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






     def    __A  (		self    ,					a__    ,					a__    ,					a__ = False  ):
       _lowerCAmelCase						:	int        =					"""dev""" if mode == """test""" else mode

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

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






     def    __A  (		self    ,					a__    ,					a__  ):
       _lowerCAmelCase						:	Dict        =					{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

       _lowerCAmelCase						:	Tuple        =					self(**a__  )
       _lowerCAmelCase      ,     _lowerCAmelCase						:	Optional[int]        =					outputs[:2]
       _lowerCAmelCase						:	str        =					logits.detach().cpu().numpy()
       _lowerCAmelCase						:	int        =					inputs["""labels"""].detach().cpu().numpy()

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






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

       if self.hparams.glue_output_mode == "classification":
         _lowerCAmelCase						:	str        =					np.argmax(a__    ,					axis=1  )
       elif self.hparams.glue_output_mode == "regression":
         _lowerCAmelCase						:	List[Any]        =					np.squeeze(a__  )

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

       _lowerCAmelCase						:	List[Any]        =					{**{"""val_loss""": val_loss_mean}, **compute_metrics(self.hparams.task    ,					a__    ,					a__  )}

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






     def    __A  (		self    ,					a__  ):
       _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase						:	Optional[Any]        =					self._eval_end(a__  )
       _lowerCAmelCase						:	Dict        =					ret["""log"""]
       return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}






     def    __A  (		self    ,					a__  ):
       _lowerCAmelCase      ,     _lowerCAmelCase      ,     _lowerCAmelCase						:	List[str]        =					self._eval_end(a__  )
       _lowerCAmelCase						:	str        =					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  (		a__    ,					a__  ):
       BaseTransformer.add_model_specific_args(a__    ,					a__  )
       parser.add_argument(
           """--max_seq_length"""    ,					default=128    ,					type=a__    ,					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=a__    ,					required=a__    ,					help="""The GLUE task to run"""    ,					)
       parser.add_argument(
           """--gpus"""    ,					default=0    ,					type=a__    ,					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 							SCREAMING_SNAKE_CASE					(      )	->				Union[str, Any]:
  _lowerCAmelCase						:	Optional[Any]        =					argparse.ArgumentParser()
  add_generic_args(_lowerCamelCase      ,os.getcwd()     )
  _lowerCAmelCase						:	Optional[Any]        =					GLUETransformer.add_model_specific_args(_lowerCamelCase      ,os.getcwd()     )
  _lowerCAmelCase						:	Dict        =					parser.parse_args()

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

  _lowerCAmelCase						:	Union[str, Any]        =					GLUETransformer(_lowerCamelCase     )
  _lowerCAmelCase						:	List[Any]        =					generic_train(_lowerCamelCase      ,_lowerCamelCase     )

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


if __name__ == "__main__":
     main()





"""simple docstring"""




from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a				:		Any	   =       logging.get_logger(__name__)

_a				:		Any	   =       {
    'uclanlp/visualbert-vqa': 'https://huggingface.co/uclanlp/visualbert-vqa/resolve/main/config.json',
    'uclanlp/visualbert-vqa-pre': 'https://huggingface.co/uclanlp/visualbert-vqa-pre/resolve/main/config.json',
    'uclanlp/visualbert-vqa-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vqa-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-vcr': 'https://huggingface.co/uclanlp/visualbert-vcr/resolve/main/config.json',
    'uclanlp/visualbert-vcr-pre': 'https://huggingface.co/uclanlp/visualbert-vcr-pre/resolve/main/config.json',
    'uclanlp/visualbert-vcr-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-vcr-coco-pre/resolve/main/config.json'
    ),
    'uclanlp/visualbert-nlvr2': 'https://huggingface.co/uclanlp/visualbert-nlvr2/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-pre': 'https://huggingface.co/uclanlp/visualbert-nlvr2-pre/resolve/main/config.json',
    'uclanlp/visualbert-nlvr2-coco-pre': (
        'https://huggingface.co/uclanlp/visualbert-nlvr2-coco-pre/resolve/main/config.json'
    )
    # See all VisualBERT models at https://huggingface.co/models?filter=visual_bert
}
class 						__A							(			SCREAMING_SNAKE_CASE_		):
     _UpperCamelCase							:      List[Any]		 =		"visual_bert"






     def __init__(		self    ,					a__=30522    ,					a__=768    ,					a__=512    ,					a__=12    ,					a__=12    ,					a__=3072    ,					a__="gelu"    ,					a__=0.1    ,					a__=0.1    ,					a__=512    ,					a__=2    ,					a__=0.0_2    ,					a__=1e-12    ,					a__=False    ,					a__=True    ,					a__=1    ,					a__=0    ,					a__=2    ,					**a__    ,					):
       super().__init__(pad_token_id=a__    ,					bos_token_id=a__    ,					eos_token_id=a__    ,					**a__  )

       _lowerCAmelCase						:	Optional[Any]        =					vocab_size
       _lowerCAmelCase						:	int        =					max_position_embeddings
       _lowerCAmelCase						:	Optional[Any]        =					hidden_size
       _lowerCAmelCase						:	Optional[int]        =					visual_embedding_dim
       _lowerCAmelCase						:	Optional[int]        =					num_hidden_layers
       _lowerCAmelCase						:	List[Any]        =					num_attention_heads
       _lowerCAmelCase						:	Any        =					intermediate_size
       _lowerCAmelCase						:	List[Any]        =					hidden_act
       _lowerCAmelCase						:	int        =					hidden_dropout_prob
       _lowerCAmelCase						:	Optional[Any]        =					attention_probs_dropout_prob
       _lowerCAmelCase						:	Any        =					initializer_range
       _lowerCAmelCase						:	List[Any]        =					type_vocab_size
       _lowerCAmelCase						:	Union[str, Any]        =					layer_norm_eps
       _lowerCAmelCase						:	Optional[Any]        =					bypass_transformer
       _lowerCAmelCase						:	List[Any]        =					special_visual_initialize




