


"""simple docstring"""







import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


lowerCAmelCase__				    =       logging.getLogger(__name__)

if is_torch_tpu_available(check_device=False):
     import torch_xla.core.xla_model as xm
     import torch_xla.debug.metrics as met


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


       def __init__(						self		,					*snake_case__		,					snake_case__=None		,					snake_case__=None		,					snake_case__=None		,					**snake_case__						):






         """simple docstring"""





         super().__init__(*snake_case__		,					**snake_case__						)
         lowerCAmelCase					:					List[Any]								=							eval_examples
         lowerCAmelCase					:					str								=							post_process_function
         lowerCAmelCase					:					List[Any]								=							quant_trainer_args
         lowerCAmelCase					:					int								=							128  # default number of calibration samples

       def 	lowercase__							(						self		,					snake_case__=None						):






         """simple docstring"""





         if calib_dataset is None and self.calib_dataset is None:
           raise ValueError("Trainer: calibration requires an calib_dataset."						)
         lowerCAmelCase					:					Dict								=							calib_dataset if calib_dataset is not None else self.calib_dataset

         lowerCAmelCase					:					Tuple								=							self._remove_unused_columns(snake_case__		,					description="Calibration"						)

         return DataLoader(
             snake_case__		,					batch_size=self.args.eval_batch_size		,					collate_fn=self.data_collator		,					drop_last=self.args.dataloader_drop_last		,					num_workers=self.args.dataloader_num_workers		,					pin_memory=self.args.dataloader_pin_memory		,					shuffle=snake_case__		,					)

       def 	lowercase__							(						self		,					snake_case__=None						):






         """simple docstring"""





         lowerCAmelCase					:					str								=							self.train_dataset if calib_dataset is None else calib_dataset
         lowerCAmelCase					:					Any								=							self.get_calib_dataloader(snake_case__						)

         lowerCAmelCase					:					Any								=							self.model
         quant_trainer.configure_model(snake_case__		,					self.quant_trainer_args		,					calib=snake_case__						)
         model.eval()
         quant_trainer.enable_calibration(snake_case__						)

         logger.info("***** Running calibration *****"						)
         logger.info(f"""  Num examples = {self.calib_num}"""						)
         logger.info(f"""  Batch size = {calib_dataloader.batch_size}"""						)

         for step, inputs in enumerate(snake_case__						):
           # Prediction step
           lowerCAmelCase						,  lowerCAmelCase						,  lowerCAmelCase					:					Dict								=							self.prediction_step(snake_case__		,					snake_case__		,					prediction_loss_only=snake_case__						)
           if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
             break

         quant_trainer.finish_calibration(snake_case__		,					self.quant_trainer_args						)
         lowerCAmelCase					:					Union[str, Any]								=							model

       def 	lowercase__							(						self		,					snake_case__=None		,					snake_case__=None		,					snake_case__=None		,					snake_case__ = "eval"						):






         """simple docstring"""





         lowerCAmelCase					:					Optional[Any]								=							self.eval_dataset if eval_dataset is None else eval_dataset
         lowerCAmelCase					:					str								=							self.get_eval_dataloader(snake_case__						)
         lowerCAmelCase					:					List[Any]								=							self.eval_examples if eval_examples is None else eval_examples

         # Temporarily disable metric computation, we will do it in the loop here.
         lowerCAmelCase					:					Optional[Any]								=							self.compute_metrics
         lowerCAmelCase					:					int								=							None
         lowerCAmelCase					:					Optional[Any]								=							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
           lowerCAmelCase					:					Tuple								=							eval_loop(
               snake_case__		,					description="Evaluation"		,					prediction_loss_only=True if compute_metrics is None else None		,					ignore_keys=snake_case__		,					)
         finally:
           lowerCAmelCase					:					List[str]								=							compute_metrics

         if self.post_process_function is not None and self.compute_metrics is not None:
           lowerCAmelCase					:					Optional[int]								=							self.post_process_function(snake_case__		,					snake_case__		,					output.predictions						)
           lowerCAmelCase					:					str								=							self.compute_metrics(snake_case__						)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()						):
             if not key.startswith(f"""{metric_key_prefix}_"""						):
               lowerCAmelCase					:					Optional[int]								=							metrics.pop(snake_case__						)

           self.log(snake_case__						)
         else:
           lowerCAmelCase					:					Tuple								=							{}

         if self.args.tpu_metrics_debug or self.args.debug:
           # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
           xm.master_print(met.metrics_report()						)

         lowerCAmelCase					:					Union[str, Any]								=							self.callback_handler.on_evaluate(self.args		,					self.state		,					self.control		,					snake_case__						)
         return metrics

       def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__=None		,					snake_case__ = "test"						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.get_test_dataloader(snake_case__						)

         # Temporarily disable metric computation, we will do it in the loop here.
         lowerCAmelCase					:					List[Any]								=							self.compute_metrics
         lowerCAmelCase					:					int								=							None
         lowerCAmelCase					:					Tuple								=							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
           lowerCAmelCase					:					str								=							eval_loop(
               snake_case__		,					description="Prediction"		,					prediction_loss_only=True if compute_metrics is None else None		,					ignore_keys=snake_case__		,					)
         finally:
           lowerCAmelCase					:					str								=							compute_metrics

         if self.post_process_function is None or self.compute_metrics is None:
           return output

         lowerCAmelCase					:					Any								=							self.post_process_function(snake_case__		,					snake_case__		,					output.predictions		,					"predict"						)
         lowerCAmelCase					:					Any								=							self.compute_metrics(snake_case__						)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()						):
           if not key.startswith(f"""{metric_key_prefix}_"""						):
             lowerCAmelCase					:					str								=							metrics.pop(snake_case__						)

         return PredictionOutput(predictions=predictions.predictions		,					label_ids=predictions.label_ids		,					metrics=snake_case__						)







       def 	lowercase__							(						self		,					snake_case__="./"						):






         """simple docstring"""





         lowerCAmelCase					:					List[str]								=							self.eval_dataset
         lowerCAmelCase					:					Tuple								=							self.get_eval_dataloader(snake_case__						)

         lowerCAmelCase					:					Union[str, Any]								=							next(iter(snake_case__						)						)

         # saving device - to make it consistent
         lowerCAmelCase					:					Optional[Any]								=							torch.device("cuda" if torch.cuda.is_available() else "cpu"						)

         # convert to tuple
         lowerCAmelCase					:					Dict								=							tuple(v.to(snake_case__						) for k, v in batch.items()						)

         logger.info("Converting model to be onnx compatible"						)
         from pytorch_quantization.nn import TensorQuantizer

         lowerCAmelCase					:					Tuple								=							True

         lowerCAmelCase					:					Optional[int]								=							self.model.to(snake_case__						)

         model.eval()
         model.float()

         lowerCAmelCase					:					Union[str, Any]								=							model.module if hasattr(snake_case__		,					"module"						) else model
         quant_trainer.configure_model(snake_case__		,					self.quant_trainer_args						)

         lowerCAmelCase					:					str								=							os.path.join(snake_case__		,					"model.onnx"						)
         logger.info(f"""exporting model to {output_model_file}"""						)

         lowerCAmelCase					:					Optional[Any]								=							{0: "batch_size", 1: "seq_len"}

         torch.onnx.export(
             snake_case__		,					snake_case__		,					snake_case__		,					export_params=snake_case__		,					opset_version=13		,					do_constant_folding=snake_case__		,					input_names=["input_ids", "attention_mask", "token_type_ids"]		,					output_names=["output_start_logits", "output_end_logits"]		,					dynamic_axes={
                 "input_ids": axes,
                 "attention_mask": axes,
                 "token_type_ids": axes,
                 "output_start_logits": axes,
                 "output_end_logits": axes,
             }		,					verbose=snake_case__		,					)
         logger.info("onnx export finished"						)









"""simple docstring"""







import re

from filelock import FileLock


try:
     import nltk

     lowerCAmelCase__				    =       True
except (ImportError, ModuleNotFoundError):
     lowerCAmelCase__				    =       False

if NLTK_AVAILABLE:
     with FileLock('''.lock''') as lock:
          nltk.download('''punkt''', quiet=True)
def  a__      (    SCREAMING_SNAKE_CASE   :   str    ):


  '''simple docstring'''







  re.sub("<n>"    ,				""    ,				SCREAMING_SNAKE_CASE    )  # remove pegasus newline char
  assert NLTK_AVAILABLE, "nltk must be installed to separate newlines between sentences. (pip install nltk)"
  return "\n".join(nltk.sent_tokenize(SCREAMING_SNAKE_CASE    )    )





