code: 
  
  
  
  
  
  '''simple docstring'''
  
  import argparse
  import logging
  import os
  from pathlib import Path
  from typing import Any, Dict
  
  import pytorch_lightning as pl
  from pytorch_lightning.utilities import rank_zero_info
  
  from transformers import (
      AdamW,
      AutoConfig,
      AutoModel,
      AutoModelForPreTraining,
      AutoModelForQuestionAnswering,
      AutoModelForSeqaSeqLM,
      AutoModelForSequenceClassification,
      AutoModelForTokenClassification,
      AutoModelWithLMHead,
      AutoTokenizer,
      PretrainedConfig,
      PreTrainedTokenizer,
  )
  from transformers.optimization import (
      Adafactor,
      get_cosine_schedule_with_warmup,
      get_cosine_with_hard_restarts_schedule_with_warmup,
      get_linear_schedule_with_warmup,
      get_polynomial_decay_schedule_with_warmup,
  )
  from transformers.utils.versions import require_version
  
  
  __lowerCAmelCase							:			Union[str, Any]				=logging.getLogger(__name__)
  
  require_version("pytorch_lightning>=1.0.4")
  
  __lowerCAmelCase							:			Union[str, Any]				={
      """base""": AutoModel,
      """sequence-classification""": AutoModelForSequenceClassification,
      """question-answering""": AutoModelForQuestionAnswering,
      """pretraining""": AutoModelForPreTraining,
      """token-classification""": AutoModelForTokenClassification,
      """language-modeling""": AutoModelWithLMHead,
      """summarization""": AutoModelForSeqaSeqLM,
      """translation""": AutoModelForSeqaSeqLM,
  }
  
  
  # update this and the import above to support new schedulers from transformers.optimization
  __lowerCAmelCase							:			List[Any]				={
      """linear""": get_linear_schedule_with_warmup,
      """cosine""": get_cosine_schedule_with_warmup,
      """cosine_w_restarts""": get_cosine_with_hard_restarts_schedule_with_warmup,
      """polynomial""": get_polynomial_decay_schedule_with_warmup,
      # '': get_constant_schedule,             # not supported for now
      # '': get_constant_schedule_with_warmup, # not supported for now
  }
  __lowerCAmelCase							:			Union[str, Any]				=sorted(arg_to_scheduler.keys())
  __lowerCAmelCase							:			Optional[Any]				="""{""" + """, """.join(arg_to_scheduler_choices) + """}"""
  
  
  
  
  
  
  class  UpperCAmelCase			( pl.LightningModule       ):
   def __init__(       self   :Optional[Any]		, lowercase_   :Optional[int]		, lowercase_   :List[str]=None		, lowercase_   :Optional[int]="base"		, lowercase_   :Tuple=None		, lowercase_   :Optional[Any]=None		, lowercase_   :List[Any]=None		, **lowercase_   :Union[str, Any]		, )->							List[Any]:
       super().__init__()
       # TODO: move to self.save_hyperparameters()
       # self.save_hyperparameters()
       # can also expand arguments into trainer signature for easier reading
  
       self.save_hyperparameters(lowerCAmelCase__							)
       A__       =    0
       A__       =    Path(self.hparams.output_dir							)
       A__       =    self.hparams.cache_dir if self.hparams.cache_dir else None
       if config is None:
           A__       =    AutoConfig.from_pretrained(
               self.hparams.config_name if self.hparams.config_name else self.hparams.model_name_or_path		, **({"num_labels": num_labels} if num_labels is not None else {})		, cache_dir=lowerCAmelCase__		, **lowerCAmelCase__		, )
       else:
           A__       =    config
  
       A__       =    ("encoder_layerdrop", "decoder_layerdrop", "dropout", "attention_dropout")
       for p in extra_model_params:
           if getattr(self.hparams		, lowerCAmelCase__		, lowerCAmelCase__							):
               assert hasattr(self.config		, lowerCAmelCase__							), F"model config doesn\'t have a `{p}` attribute"
               setattr(self.config		, lowerCAmelCase__		, getattr(self.hparams		, lowerCAmelCase__							)							)
  
       if tokenizer is None:
           A__       =    AutoTokenizer.from_pretrained(
               self.hparams.tokenizer_name if self.hparams.tokenizer_name else self.hparams.model_name_or_path		, cache_dir=lowerCAmelCase__		, )
       else:
           A__       =    tokenizer
       A__       =    MODEL_MODES[mode]
       if model is None:
           A__       =    self.model_type.from_pretrained(
               self.hparams.model_name_or_path		, from_tf=bool(".ckpt" in self.hparams.model_name_or_path							)		, config=self.config		, cache_dir=lowerCAmelCase__		, )
       else:
           A__       =    model
   def   UpperCAmelCase_							(       self   :Tuple		, *lowercase_   :Dict		, **lowercase_   :Dict							)->							List[str]:
       A__       =    self.model_type.from_pretrained(*lowerCAmelCase__		, **lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :Tuple							)->							List[Any]:
       A__       =    arg_to_scheduler[self.hparams.lr_scheduler]
       A__       =    get_schedule_func(
           self.opt		, num_warmup_steps=self.hparams.warmup_steps		, num_training_steps=self.total_steps()							)
       A__       =    {"scheduler": scheduler, "interval": "step", "frequency": 1}
       return scheduler
   def   UpperCAmelCase_							(       self   :Union[str, Any]							)->							Any:
       A__       =    self.model
       A__       =    ["bias", "LayerNorm.weight"]
       A__       =    [
           {
               "params": [p for n, p in model.named_parameters() if not any(nd in n for nd in no_decay							)],
               "weight_decay": self.hparams.weight_decay,
           },
           {
               "params": [p for n, p in model.named_parameters() if any(nd in n for nd in no_decay							)],
               "weight_decay": 0.0,
           },
       ]
       if self.hparams.adafactor:
           A__       =    Adafactor(
               lowerCAmelCase__		, lr=self.hparams.learning_rate		, scale_parameter=lowerCAmelCase__		, relative_step=lowerCAmelCase__							)
  
       else:
           A__       =    AdamW(
               lowerCAmelCase__		, lr=self.hparams.learning_rate		, eps=self.hparams.adam_epsilon							)
       A__       =    optimizer
  
       A__       =    self.get_lr_scheduler()
  
       return [optimizer], [scheduler]
   def   UpperCAmelCase_							(       self   :Any		, lowercase_   :Union[str, Any]		, lowercase_   :Dict							)->							Dict:
       return self.validation_step(lowerCAmelCase__		, lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :Any		, lowercase_   :List[str]							)->							str:
       return self.validation_end(lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :int							)->							int:
       A__       =    max(1		, self.hparams.gpus							)  # TODO: consider num_tpu_cores
       A__       =    self.hparams.train_batch_size * self.hparams.accumulate_grad_batches * num_devices
       return (self.dataset_size / effective_batch_size) * self.hparams.max_epochs
   def   UpperCAmelCase_							(       self   :Optional[Any]		, lowercase_   :Any							)->							Optional[int]:
  
  
  
  
       if mode == "test":
           A__       =    len(self.test_dataloader().dataset							)
       else:
           A__       =    self.get_dataloader("train"		, self.hparams.train_batch_size		, shuffle=lowerCAmelCase__							)
           A__       =    len(self.train_dataloader().dataset							)
   def   UpperCAmelCase_							(       self   :List[str]		, lowercase_   :str		, lowercase_   :List[Any]		, lowercase_   :Any = False							)->							List[Any]:
       raise NotImplementedError("You must implement this for your task"							)
   def   UpperCAmelCase_							(       self   :Optional[Any]							)->							Dict:
       return self.train_loader
   def   UpperCAmelCase_							(       self   :Tuple							)->							str:
       return self.get_dataloader("dev"		, self.hparams.eval_batch_size		, shuffle=lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :Optional[int]							)->							str:
       return self.get_dataloader("test"		, self.hparams.eval_batch_size		, shuffle=lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :str		, lowercase_   :List[str]							)->							List[Any]:
       return os.path.join(
           self.hparams.data_dir		, "cached_{}_{}_{}".format(
               lowerCAmelCase__		, list(filter(lowerCAmelCase__		, self.hparams.model_name_or_path.split("/"							)							)							).pop()		, str(self.hparams.max_seq_length							)		, )		, )
   @pl.utilities.rank_zero_only
   def   UpperCAmelCase_							(       self   :str		, lowercase_   :str							)->							None:
       A__       =    self.output_dir.joinpath("best_tfmr"							)
       A__       =    self.step_count
       self.model.save_pretrained(lowerCAmelCase__							)
       self.tokenizer.save_pretrained(lowerCAmelCase__							)
  
  
  
   @staticmethod
   def   UpperCAmelCase_							(       lowercase_   :List[Any]		, lowercase_   :str							)->							Tuple:
       parser.add_argument(
           "--model_name_or_path"		, default=lowerCAmelCase__		, type=lowerCAmelCase__		, required=lowerCAmelCase__		, help="Path to pretrained model or model identifier from huggingface.co/models"		, )
       parser.add_argument(
           "--config_name"		, default=""		, type=lowerCAmelCase__		, help="Pretrained config name or path if not the same as model_name"							)
       parser.add_argument(
           "--tokenizer_name"		, default=lowerCAmelCase__		, type=lowerCAmelCase__		, help="Pretrained tokenizer name or path if not the same as model_name"		, )
       parser.add_argument(
           "--cache_dir"		, default=""		, type=lowerCAmelCase__		, help="Where do you want to store the pre-trained models downloaded from huggingface.co"		, )
       parser.add_argument(
           "--encoder_layerdrop"		, type=lowerCAmelCase__		, help="Encoder layer dropout probability (Optional). Goes into model.config"		, )
       parser.add_argument(
           "--decoder_layerdrop"		, type=lowerCAmelCase__		, help="Decoder layer dropout probability (Optional). Goes into model.config"		, )
       parser.add_argument(
           "--dropout"		, type=lowerCAmelCase__		, help="Dropout probability (Optional). Goes into model.config"		, )
       parser.add_argument(
           "--attention_dropout"		, type=lowerCAmelCase__		, help="Attention dropout probability (Optional). Goes into model.config"		, )
       parser.add_argument("--learning_rate"		, default=5E-5		, type=lowerCAmelCase__		, help="The initial learning rate for Adam."							)
       parser.add_argument(
           "--lr_scheduler"		, default="linear"		, choices=lowerCAmelCase__		, metavar=lowerCAmelCase__		, type=lowerCAmelCase__		, help="Learning rate scheduler"		, )
       parser.add_argument("--weight_decay"		, default=0.0		, type=lowerCAmelCase__		, help="Weight decay if we apply some."							)
       parser.add_argument("--adam_epsilon"		, default=1E-8		, type=lowerCAmelCase__		, help="Epsilon for Adam optimizer."							)
       parser.add_argument("--warmup_steps"		, default=0		, type=lowerCAmelCase__		, help="Linear warmup over warmup_steps."							)
       parser.add_argument("--num_workers"		, default=4		, type=lowerCAmelCase__		, help="kwarg passed to DataLoader"							)
       parser.add_argument("--num_train_epochs"		, dest="max_epochs"		, default=3		, type=lowerCAmelCase__							)
       parser.add_argument("--train_batch_size"		, default=32		, type=lowerCAmelCase__							)
       parser.add_argument("--eval_batch_size"		, default=32		, type=lowerCAmelCase__							)
       parser.add_argument("--adafactor"		, action="store_true"							)
  
  
  
  
  
  
  class  UpperCAmelCase			( pl.Callback       ):
   def   UpperCAmelCase_							(       self   :int		, lowercase_   :int		, lowercase_   :Optional[int]							)->							str:
       A__       =    trainer.lr_schedulers[0]["scheduler"]
       A__       =    {F"lr_group_{i}": lr for i, lr in enumerate(lr_scheduler.get_lr()							)}
       pl_module.logger.log_metrics(lowerCAmelCase__							)
   def   UpperCAmelCase_							(       self   :Tuple		, lowercase_   :Any		, lowercase_   :Union[str, Any]							)->							Any:
       rank_zero_info("***** Validation results *****"							)
       A__       =    trainer.callback_metrics
       # Log results
       for key in sorted(lowerCAmelCase__							):
           if key not in ["log", "progress_bar"]:
               rank_zero_info("{} = {}\n".format(lowerCAmelCase__		, str(metrics[key]							)							)							)
  
  
  
   def   UpperCAmelCase_							(       self   :Union[str, Any]		, lowercase_   :List[Any]		, lowercase_   :Any							)->							Optional[int]:
       rank_zero_info("***** Test results *****"							)
       A__       =    trainer.callback_metrics
       # Log and save results to file
       A__       =    os.path.join(pl_module.hparams.output_dir		, "test_results.txt"							)
       with open(lowerCAmelCase__		, "w"							) as writer:
           for key in sorted(lowerCAmelCase__							):
               if key not in ["log", "progress_bar"]:
                   rank_zero_info("{} = {}\n".format(lowerCAmelCase__		, str(metrics[key]							)							)							)
                   writer.write("{} = {}\n".format(lowerCAmelCase__		, str(metrics[key]							)							)							)
  
  
  
  
  
  def 		UpperCamelCase       (							_lowerCamelCase      :      Union[str, Any]		, _lowerCamelCase      :      str ):
      parser.add_argument(
          "--output_dir"		, default=_lowerCamelCase		, type=_lowerCamelCase		, required=_lowerCamelCase		, help="The output directory where the model predictions and checkpoints will be written."		, )
      parser.add_argument(
          "--fp16"		, action="store_true"		, help="Whether to use 16-bit (mixed) precision (through NVIDIA apex) instead of 32-bit"		, )
  
      parser.add_argument(
          "--fp16_opt_level"		, type=_lowerCamelCase		, default="O2"		, help=(
              "For fp16: Apex AMP optimization level selected in ['O0', 'O1', 'O2', and 'O3']."
              "See details at https://nvidia.github.io/apex/amp.html"
          )		, )
      parser.add_argument("--n_tpu_cores"		, dest="tpu_cores"		, type=_lowerCamelCase )
      parser.add_argument("--max_grad_norm"		, dest="gradient_clip_val"		, default=1.0		, type=_lowerCamelCase		, help="Max gradient norm" )
      parser.add_argument("--do_train"		, action="store_true"		, help="Whether to run training." )
      parser.add_argument("--do_predict"		, action="store_true"		, help="Whether to run predictions on the test set." )
      parser.add_argument(
          "--gradient_accumulation_steps"		, dest="accumulate_grad_batches"		, type=_lowerCamelCase		, default=1		, help="Number of updates steps to accumulate before performing a backward/update pass."		, )
      parser.add_argument("--seed"		, type=_lowerCamelCase		, default=42		, help="random seed for initialization" )
      parser.add_argument(
          "--data_dir"		, default=_lowerCamelCase		, type=_lowerCamelCase		, required=_lowerCamelCase		, help="The input data dir. Should contain the training files for the CoNLL-2003 NER task."		, )
  
  
  
  
  
  def 		UpperCamelCase       (							_lowerCamelCase      :      BaseTransformer		, _lowerCamelCase      :      argparse.Namespace		, _lowerCamelCase      :      Dict=None		, _lowerCamelCase      :      Optional[Any]=True		, _lowerCamelCase      :      Any=[]		, _lowerCamelCase      :      str=None		, _lowerCamelCase      :      str=None		, **_lowerCamelCase      :      Any		, ):
      pl.seed_everything(args.seed )
  
      # init model
      A__       =    Path(model.hparams.output_dir )
      odir.mkdir(exist_ok=_lowerCamelCase )
  
      # add custom checkpoints
      if checkpoint_callback is None:
          A__       =    pl.callbacks.ModelCheckpoint(
              filepath=args.output_dir		, prefix="checkpoint"		, monitor="val_loss"		, mode="min"		, save_top_k=1 )
      if early_stopping_callback:
          extra_callbacks.append(_lowerCamelCase )
      if logging_callback is None:
          A__       =    LoggingCallback()
  
      A__       =    {}
  
      # TODO: remove with PyTorch 1.6 since pl uses native amp
      if args.fpaa:
          A__       =    16
          A__       =    args.fpaa_opt_level
  
      if args.gpus > 1:
          A__       =    "ddp"
  
      A__       =    args.accumulate_grad_batches
      A__       =    extra_train_kwargs.get("accelerator"		, _lowerCamelCase )
      A__       =    extra_train_kwargs.get("profiler"		, _lowerCamelCase )
  
      A__       =    pl.Trainer.from_argparse_args(
          _lowerCamelCase		, weights_summary=_lowerCamelCase		, callbacks=[logging_callback] + extra_callbacks		, logger=_lowerCamelCase		, checkpoint_callback=_lowerCamelCase		, **_lowerCamelCase		, )
  
      if args.do_train:
          trainer.fit(_lowerCamelCase )
  
      return trainer
  
  
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  import sacrebleu as scb
  from packaging import version
  
  import datasets
  
  
  __lowerCAmelCase							:			Tuple				="\\n@inproceedings{post-2018-call,\n    title = \"A Call for Clarity in Reporting {BLEU} Scores\",\n    author = \"Post, Matt\",\n    booktitle = \"Proceedings of the Third Conference on Machine Translation: Research Papers\",\n    month = oct,\n    year = \"2018\",\n    address = \"Belgium, Brussels\",\n    publisher = \"Association for Computational Linguistics\",\n    url = \"https://www.aclweb.org/anthology/W18-6319\",\n    pages = \"186--191\",\n}\n"
  
  __lowerCAmelCase							:			Dict				="\\nSacreBLEU provides hassle-free computation of shareable, comparable, and reproducible BLEU scores.\nInspired by Rico Sennrich's `multi-bleu-detok.perl`, it produces the official WMT scores but works with plain text.\nIt also knows all the standard test sets and handles downloading, processing, and tokenization for you.\n\nSee the [README.md] file at https://github.com/mjpost/sacreBLEU for more information.\n"
  
  __lowerCAmelCase							:			Dict				="\nProduces BLEU scores along with its sufficient statistics\nfrom a source against one or more references.\n\nArgs:\n    predictions (`list` of `str`): list of translations to score. Each translation should be tokenized into a list of tokens.\n    references (`list` of `list` of `str`): A list of lists of references. The contents of the first sub-list are the references for the first prediction, the contents of the second sub-list are for the second prediction, etc. Note that there must be the same number of references for each prediction (i.e. all sub-lists must be of the same length).\n    smooth_method (`str`): The smoothing method to use, defaults to `'exp'`. Possible values are:\n        - `'none'`: no smoothing\n        - `'floor'`: increment zero counts\n        - `'add-k'`: increment num/denom by k for n>1\n        - `'exp'`: exponential decay\n    smooth_value (`float`): The smoothing value. Only valid when `smooth_method='floor'` (in which case `smooth_value` defaults to `0.1`) or `smooth_method='add-k'` (in which case `smooth_value` defaults to `1`).\n    tokenize (`str`): Tokenization method to use for BLEU. If not provided, defaults to `'zh'` for Chinese, `'ja-mecab'` for Japanese and `'13a'` (mteval) otherwise. Possible values are:\n        - `'none'`: No tokenization.\n        - `'zh'`: Chinese tokenization.\n        - `'13a'`: mimics the `mteval-v13a` script from Moses.\n        - `'intl'`: International tokenization, mimics the `mteval-v14` script from Moses\n        - `'char'`: Language-agnostic character-level tokenization.\n        - `'ja-mecab'`: Japanese tokenization. Uses the [MeCab tokenizer](https://pypi.org/project/mecab-python3).\n    lowercase (`bool`): If `True`, lowercases the input, enabling case-insensitivity. Defaults to `False`.\n    force (`bool`): If `True`, insists that your tokenized input is actually detokenized. Defaults to `False`.\n    use_effective_order (`bool`): If `True`, stops including n-gram orders for which precision is 0. This should be `True`, if sentence-level BLEU will be computed. Defaults to `False`.\n\nReturns:\n    'score': BLEU score,\n    'counts': Counts,\n    'totals': Totals,\n    'precisions': Precisions,\n    'bp': Brevity penalty,\n    'sys_len': predictions length,\n    'ref_len': reference length,\n\nExamples:\n\n    Example 1:\n        >>> predictions = [\"hello there general kenobi\", \"foo bar foobar\"]\n        >>> references = [[\"hello there general kenobi\", \"hello there !\"], [\"foo bar foobar\", \"foo bar foobar\"]]\n        >>> sacrebleu = datasets.load_metric(\"sacrebleu\")\n        >>> results = sacrebleu.compute(predictions=predictions, references=references)\n        >>> print(list(results.keys()))\n        ['score', 'counts', 'totals', 'precisions', 'bp', 'sys_len', 'ref_len']\n        >>> print(round(results[\"score\"], 1))\n        100.0\n\n    Example 2:\n        >>> predictions = [\"hello there general kenobi\",\n        ...                 \"on our way to ankh morpork\"]\n        >>> references = [[\"hello there general kenobi\", \"hello there !\"],\n        ...                 [\"goodbye ankh morpork\", \"ankh morpork\"]]\n        >>> sacrebleu = datasets.load_metric(\"sacrebleu\")\n        >>> results = sacrebleu.compute(predictions=predictions,\n        ...                             references=references)\n        >>> print(list(results.keys()))\n        ['score', 'counts', 'totals', 'precisions', 'bp', 'sys_len', 'ref_len']\n        >>> print(round(results[\"score\"], 1))\n        39.8\n"
  
  
  
  
  
  
  @datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION    , _KWARGS_DESCRIPTION       )
  class      UpperCAmelCase			( datasets.Metric       ):
   def   UpperCAmelCase_							(       self   :int							)->							Union[str, Any]:
       if version.parse(scb.__version__							) < version.parse("1.4.12"							):
           raise ImportWarning(
               "To use `sacrebleu`, the module `sacrebleu>=1.4.12` is required, and the current version of `sacrebleu` doesn't match this condition.\n"
               "You can install it with `pip install \"sacrebleu>=1.4.12\"`."							)
       return datasets.MetricInfo(
           description=_DESCRIPTION		, citation=_CITATION		, homepage="https://github.com/mjpost/sacreBLEU"		, inputs_description=_KWARGS_DESCRIPTION		, features=datasets.Features(
               {
                   "predictions": datasets.Value("string"		, id="sequence"							),
                   "references": datasets.Sequence(datasets.Value("string"		, id="sequence"							)		, id="references"							),
               }							)		, codebase_urls=["https://github.com/mjpost/sacreBLEU"]		, reference_urls=[
               "https://github.com/mjpost/sacreBLEU",
               "https://en.wikipedia.org/wiki/BLEU",
               "https://towardsdatascience.com/evaluating-text-output-in-nlp-bleu-at-your-own-risk-e8609665a213",
           ]		, )
  
  
  
   def   UpperCAmelCase_							(       self   :Tuple		, lowercase_   :Optional[int]		, lowercase_   :Optional[Any]		, lowercase_   :List[str]="exp"		, lowercase_   :str=None		, lowercase_   :Tuple=False		, lowercase_   :Any=False		, lowercase_   :Optional[Any]=None		, lowercase_   :Any=False		, )->							Tuple:
       A__       =    len(references[0]							)
       if any(len(lowercase_							) != references_per_prediction for refs in references							):
           raise ValueError("Sacrebleu requires the same number of references for each prediction"							)
       A__       =    [[refs[i] for refs in references] for i in range(lowercase_							)]
       A__       =    scb.corpus_bleu(
           lowercase_		, lowercase_		, smooth_method=lowercase_		, smooth_value=lowercase_		, force=lowercase_		, lowercase=lowercase_		, use_effective_order=lowercase_		, **({"tokenize": tokenize} if tokenize else {})		, )
       A__       =    {
           "score": output.score,
           "counts": output.counts,
           "totals": output.totals,
           "precisions": output.precisions,
           "bp": output.bp,
           "sys_len": output.sys_len,
           "ref_len": output.ref_len,
       }
       return output_dict
  
  
  
  