


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


__snake_case    					=					logging.getLogger(__name__)

class 				_SCREAMING_SNAKE_CASE				(							__UpperCAmelCase  ):
   """simple docstring"""



   _a   :       List[str]									=    '''sequence-classification'''


   def __init__(						self ,     lowerCamelCase__						)      ->							List[Any]:
    if type(lowerCamelCase__						) == dict:
     lowercase__       :       Dict				=						Namespace(**lowerCamelCase__						)
    lowercase__       :       Any				=						glue_output_modes[hparams.task]
    lowercase__       :       Union[str, Any]				=						glue_tasks_num_labels[hparams.task]

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


   def 					UpperCAmelCase__(						self ,     **lowerCamelCase__						)      ->							int:
    return self.model(**lowerCamelCase__						)


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							Any:
    lowercase__       :       Any				=						{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

    lowercase__       :       List[Any]				=						self(**lowerCamelCase__						)
    lowercase__       :       Dict				=						outputs[0]

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


   def 					UpperCAmelCase__(						self						)      ->							int:
    lowercase__       :       int				=						self.hparams
    lowercase__       :       int				=						processors[args.task]()
    lowercase__       :       List[str]				=						processor.get_labels()

    for mode in ["train", "dev"]:
     lowercase__       :       str				=						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						)
      lowercase__       :       Union[str, Any]				=						(
          processor.get_dev_examples(args.data_dir						)
          if mode == """dev"""
          else processor.get_train_examples(args.data_dir						)
      )
      lowercase__       :       Any				=						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 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__ ,     lowerCamelCase__ = False						)      ->							DataLoader:
    lowercase__       :       Optional[int]				=						"""dev""" if mode == """test""" else mode

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

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


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__ ,     lowerCamelCase__						)      ->							Dict:
    lowercase__       :       Union[str, Any]				=						{"""input_ids""": batch[0], """attention_mask""": batch[1], """labels""": batch[3]}

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

    lowercase__       :       Tuple				=						self(**lowerCamelCase__						)
    lowercase__	,       lowercase__       :       int				=						outputs[:2]
    lowercase__       :       Any				=						logits.detach().cpu().numpy()
    lowercase__       :       int				=						inputs["""labels"""].detach().cpu().numpy()

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


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__						)      ->							tuple:
    lowercase__       :       Dict				=						torch.stack([x["""val_loss"""] for x in outputs]						).mean().detach().cpu().item()
    lowercase__       :       List[str]				=						np.concatenate([x["""pred"""] for x in outputs] ,     axis=0						)

    if self.hparams.glue_output_mode == "classification":
     lowercase__       :       Dict				=						np.argmax(lowerCamelCase__ ,     axis=1						)
    elif self.hparams.glue_output_mode == "regression":
     lowercase__       :       Optional[Any]				=						np.squeeze(lowerCamelCase__						)

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

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

    lowercase__       :       List[Any]				=						dict(results.items()						)
    lowercase__       :       Optional[int]				=						results
    return ret, preds_list, out_label_list


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__						)      ->							dict:
    lowercase__	,       lowercase__	,       lowercase__       :       Tuple				=						self._eval_end(lowerCamelCase__						)
    lowercase__       :       Dict				=						ret["""log"""]
    return {"val_loss": logs["val_loss"], "log": logs, "progress_bar": logs}


   def 					UpperCAmelCase__(						self ,     lowerCamelCase__						)      ->							dict:
    lowercase__	,       lowercase__	,       lowercase__       :       Tuple				=						self._eval_end(lowerCamelCase__						)
    lowercase__       :       Optional[int]				=						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__(						lowerCamelCase__ ,     lowerCamelCase__						)      ->							str:
    BaseTransformer.add_model_specific_args(lowerCamelCase__ ,     lowerCamelCase__						)
    parser.add_argument(
        """--max_seq_length""" ,     default=128 ,     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    _lowerCamelCase   (     ):
 lowercase__       :       Optional[int]				=						argparse.ArgumentParser()
 add_generic_args(lowerCamelCase__       ,		os.getcwd()  )
 lowercase__       :       Tuple				=						GLUETransformer.add_model_specific_args(lowerCamelCase__       ,		os.getcwd()  )
 lowercase__       :       str				=						parser.parse_args()

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

 lowercase__       :       Optional[Any]				=						GLUETransformer(lowerCamelCase__  )
 lowercase__       :       Optional[int]				=						generic_train(lowerCamelCase__       ,		lowerCamelCase__  )

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


if __name__ == "__main__":
   main()



"""simple docstring"""


import argparse
import collections
import os
import re

from transformers.utils import direct_transformers_import


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_table.py
__snake_case    					=					'src/transformers'
__snake_case    					=					'docs/source/en'
__snake_case    					=					'.'


def    _lowerCamelCase   (     lowerCamelCase__	:							Union[str, Any]       ,		lowerCamelCase__	:							Optional[Any]       ,		lowerCamelCase__	:							Any  ):
 with open(lowerCamelCase__       ,		"""r"""       ,		encoding="""utf-8"""       ,		newline="""\n"""  ) as f:
  lowercase__       :       List[str]				=						f.readlines()
 # Find the start prompt.
 lowercase__       :       str				=						0
 while not lines[start_index].startswith(lowerCamelCase__  ):
  start_index += 1
 start_index += 1

 lowercase__       :       Optional[Any]				=						start_index
 while not lines[end_index].startswith(lowerCamelCase__  ):
  end_index += 1
 end_index -= 1

 while len(lines[start_index]  ) <= 1:
  start_index += 1
 while len(lines[end_index]  ) <= 1:
  end_index -= 1
 end_index += 1
 return "".join(lines[start_index:end_index]  ), start_index, end_index, lines


# Add here suffixes that are used to identify models, separated by |
__snake_case    					=					'Model|Encoder|Decoder|ForConditionalGeneration'
# Regexes that match TF/Flax/PT model names.
__snake_case    					=					re.compile(R'TF(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
__snake_case    					=					re.compile(R'Flax(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')
# Will match any TF or Flax model too so need to be in an else branch afterthe two previous regexes.
__snake_case    					=					re.compile(R'(.*)(?:Model|Encoder|Decoder|ForConditionalGeneration)')


# This is to make sure the transformers module imported is the one in the repo.
__snake_case    					=					direct_transformers_import(TRANSFORMERS_PATH)


def    _lowerCamelCase   (     lowerCamelCase__	:							List[Any]  ):
 lowercase__       :       Union[str, Any]				=						re.finditer(""".+?(?:(?<=[a-z])(?=[A-Z])|(?<=[A-Z])(?=[A-Z][a-z])|$)"""       ,		lowerCamelCase__  )
 return [m.group(0  ) for m in matches]


def    _lowerCamelCase   (     lowerCamelCase__	:							Optional[int]       ,		lowerCamelCase__	:							int  ):
 lowercase__       :       Any				=						2 if text == """✅""" or text == """❌""" else len(lowerCamelCase__  )
 lowercase__       :       int				=						(width - text_length) // 2
 lowercase__       :       List[str]				=						width - text_length - left_indent
 return " " * left_indent + text + " " * right_indent


def    _lowerCamelCase   (     ):
 lowercase__       :       List[str]				=						transformers_module.models.auto.configuration_auto.CONFIG_MAPPING_NAMES
 lowercase__       :       Optional[int]				=						{
     name: config_maping_names[code]
     for code, name in transformers_module.MODEL_NAMES_MAPPING.items()
     if code in config_maping_names
 }
 lowercase__       :       str				=						{name: config.replace("""Config"""       ,		""""""  ) for name, config in model_name_to_config.items()}

 # Dictionaries flagging if each model prefix has a slow/fast tokenizer, backend in PT/TF/Flax.
 lowercase__       :       Tuple				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Optional[Any]				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Optional[int]				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Dict				=						collections.defaultdict(lowerCamelCase__  )
 lowercase__       :       Union[str, Any]				=						collections.defaultdict(lowerCamelCase__  )

 # Let's lookup through all transformers object (once).
 for attr_name in dir(lowerCamelCase__  ):
  lowercase__       :       Optional[Any]				=						None
  if attr_name.endswith("""Tokenizer"""  ):
   lowercase__       :       List[Any]				=						slow_tokenizers
   lowercase__       :       List[str]				=						attr_name[:-9]
  elif attr_name.endswith("""TokenizerFast"""  ):
   lowercase__       :       Tuple				=						fast_tokenizers
   lowercase__       :       str				=						attr_name[:-13]
  elif _re_tf_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       Tuple				=						tf_models
   lowercase__       :       List[Any]				=						_re_tf_models.match(lowerCamelCase__  ).groups()[0]
  elif _re_flax_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       List[Any]				=						flax_models
   lowercase__       :       List[Any]				=						_re_flax_models.match(lowerCamelCase__  ).groups()[0]
  elif _re_pt_models.match(lowerCamelCase__  ) is not None:
   lowercase__       :       Union[str, Any]				=						pt_models
   lowercase__       :       Optional[int]				=						_re_pt_models.match(lowerCamelCase__  ).groups()[0]

  if lookup_dict is not None:
   while len(lowerCamelCase__  ) > 0:
    if attr_name in model_name_to_prefix.values():
     lowercase__       :       Tuple				=						True
     break
    # Try again after removing the last word in the name
    lowercase__       :       Union[str, Any]				=						"""""".join(camel_case_split(lowerCamelCase__  )[:-1]  )

    # Let's build that table!
 lowercase__       :       Union[str, Any]				=						list(model_name_to_config.keys()  )
 model_names.sort(key=str.lower  )
 lowercase__       :       List[Any]				=						["""Model""", """Tokenizer slow""", """Tokenizer fast""", """PyTorch support""", """TensorFlow support""", """Flax Support"""]
 # We'll need widths to properly display everything in the center (+2 is to leave one extra space on each side).
 lowercase__       :       str				=						[len(lowerCamelCase__  ) + 2 for c in columns]
 lowercase__       :       Tuple				=						max([len(lowerCamelCase__  ) for name in model_names]  ) + 2

 # Build the table per se
 lowercase__       :       List[Any]				=						"""|""" + """|""".join([_center_text(lowerCamelCase__       ,		lowerCamelCase__  ) for c, w in zip(lowerCamelCase__       ,		lowerCamelCase__  )]  ) + """|\n"""
 # Use ":-----:" format to center-aligned table cell texts
 table += "|" + "|".join([""":""" + """-""" * (w - 2) + """:""" for w in widths]  ) + "|\n"

 lowercase__       :       List[Any]				=						{True: """✅""", False: """❌"""}
 for name in model_names:
  lowercase__       :       int				=						model_name_to_prefix[name]
  lowercase__       :       List[str]				=						[
      name,
      check[slow_tokenizers[prefix]],
      check[fast_tokenizers[prefix]],
      check[pt_models[prefix]],
      check[tf_models[prefix]],
      check[flax_models[prefix]],
  ]
  table += "|" + "|".join([_center_text(lowerCamelCase__       ,		lowerCamelCase__  ) for l, w in zip(lowerCamelCase__       ,		lowerCamelCase__  )]  ) + "|\n"
 return table


def    _lowerCamelCase   (     lowerCamelCase__	:							Union[str, Any]=False  ):
 lowercase__	,       lowercase__	,       lowercase__	,       lowercase__       :       str				=						_find_text_in_file(
     filename=os.path.join(lowerCamelCase__       ,		"""index.md"""  )       ,		start_prompt="""<!--This table is updated automatically from the auto modules"""       ,		end_prompt="""<!-- End table-->"""       ,		)
 lowercase__       :       Union[str, Any]				=						get_model_table_from_auto_modules()

 if current_table != new_table:
  if overwrite:
   with open(os.path.join(lowerCamelCase__       ,		"""index.md"""  )       ,		"""w"""       ,		encoding="""utf-8"""       ,		newline="""\n"""  ) as f:
    f.writelines(lines[:start_index] + [new_table] + lines[end_index:]  )
  else:
   raise ValueError(
       """The model table in the `index.md` has not been updated. Run `make fix-copies` to fix this."""  )


if __name__ == "__main__":
   __snake_case    					=					argparse.ArgumentParser()
   parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
   __snake_case    					=					parser.parse_args()

   check_model_table(args.fix_and_overwrite)