





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 :  Dict										=  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       A__     ( __snake_case    ):





      def __init__(				self ,							*A_ ,							A_=None ,							A_=None ,							A_=None ,							**A_     ):
       '''simple docstring'''




       super().__init__(*A_ ,							**A_     )
       UpperCamelCase			:      Tuple 			=     eval_examples
       UpperCamelCase			:      Dict 			=     post_process_function
       UpperCamelCase			:      Union[str, Any] 			=     quant_trainer_args
       UpperCamelCase			:      Any 			=     128  # default number of calibration samples





      def  __UpperCamelCase(				self ,							A_=None     ):
       '''simple docstring'''




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

       UpperCamelCase			:      Dict 			=     self._remove_unused_columns(A_ ,							description="Calibration"     )

       return DataLoader(
           A_ ,							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=A_ ,							)





      def  __UpperCamelCase(				self ,							A_=None     ):
       '''simple docstring'''




       UpperCamelCase			:      Any 			=     self.train_dataset if calib_dataset is None else calib_dataset
       UpperCamelCase			:      Dict 			=     self.get_calib_dataloader(A_     )

       UpperCamelCase			:      Optional[int] 			=     self.model
       quant_trainer.configure_model(A_ ,							self.quant_trainer_args ,							calib=A_     )
       model.eval()
       quant_trainer.enable_calibration(A_     )

       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(A_     ):
        # Prediction step
        UpperCamelCase			,					UpperCamelCase			,					UpperCamelCase			:      List[Any] 			=     self.prediction_step(A_ ,							A_ ,							prediction_loss_only=A_     )
        if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
         break

       quant_trainer.finish_calibration(A_ ,							self.quant_trainer_args     )
       UpperCamelCase			:      str 			=     model





      def  __UpperCamelCase(				self ,							A_=None ,							A_=None ,							A_=None ,							A_ = "eval"     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[Any] 			=     self.eval_dataset if eval_dataset is None else eval_dataset
       UpperCamelCase			:      Any 			=     self.get_eval_dataloader(A_     )
       UpperCamelCase			:      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.
       UpperCamelCase			:      List[Any] 			=     self.compute_metrics
       UpperCamelCase			:      Dict 			=     None
       UpperCamelCase			:      Any 			=     self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       try:
        UpperCamelCase			:      int 			=     eval_loop(
            A_ ,							description="Evaluation" ,							prediction_loss_only=True if compute_metrics is None else None ,							ignore_keys=A_ ,							)
       finally:
        UpperCamelCase			:      Union[str, Any] 			=     compute_metrics

       if self.post_process_function is not None and self.compute_metrics is not None:
        UpperCamelCase			:      List[str] 			=     self.post_process_function(A_ ,							A_ ,							output.predictions     )
        UpperCamelCase			:      Dict 			=     self.compute_metrics(A_     )

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

        self.log(A_     )
       else:
        UpperCamelCase			:      List[Any] 			=     {}

       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()     )

       UpperCamelCase			:      Tuple 			=     self.callback_handler.on_evaluate(self.args ,							self.state ,							self.control ,							A_     )
       return metrics





      def  __UpperCamelCase(				self ,							A_ ,							A_ ,							A_=None ,							A_ = "test"     ):
       '''simple docstring'''




       UpperCamelCase			:      Optional[int] 			=     self.get_test_dataloader(A_     )

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

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

       UpperCamelCase			:      List[str] 			=     self.post_process_function(A_ ,							A_ ,							output.predictions ,							"predict"     )
       UpperCamelCase			:      Optional[Any] 			=     self.compute_metrics(A_     )

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()     ):
        if not key.startswith(F"""{metric_key_prefix}_"""     ):
         UpperCamelCase			:      Tuple 			=     metrics.pop(A_     )

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





      def  __UpperCamelCase(				self ,							A_="./"     ):
       '''simple docstring'''




       UpperCamelCase			:      str 			=     self.eval_dataset
       UpperCamelCase			:      Dict 			=     self.get_eval_dataloader(A_     )

       UpperCamelCase			:      List[str] 			=     next(iter(A_     )     )

       # saving device - to make it consistent
       UpperCamelCase			:      Tuple 			=     torch.device("cuda" if torch.cuda.is_available() else "cpu"     )

       # convert to tuple
       UpperCamelCase			:      Union[str, Any] 			=     tuple(v.to(A_     ) for k, v in batch.items()     )

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

       UpperCamelCase			:      str 			=     True

       UpperCamelCase			:      int 			=     self.model.to(A_     )

       model.eval()
       model.float()

       UpperCamelCase			:      Tuple 			=     model.module if hasattr(A_ ,							"module"     ) else model
       quant_trainer.configure_model(A_ ,							self.quant_trainer_args     )

       UpperCamelCase			:      int 			=     os.path.join(A_ ,							"model.onnx"     )
       logger.info(F"""exporting model to {output_model_file}"""     )

       UpperCamelCase			:      List[Any] 			=     {0: "batch_size", 1: "seq_len"}

       torch.onnx.export(
           A_ ,							A_ ,							A_ ,							export_params=A_ ,							opset_version=13 ,							do_constant_folding=A_ ,							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=A_ ,							)
       logger.info("onnx export finished"     )







import math

import tensorflow as tf
from packaging import version



def 	A_       (  _lowerCAmelCase  )		->				Any:
 UpperCamelCase			:      List[Any] 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      Any 			=     0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0  )							,     x.dtype  )  ))

 return x * cdf



def 	A_       (  _lowerCAmelCase  )		->				Dict:
 UpperCamelCase			:      Union[str, Any] 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(math.pi							,     x.dtype  )
 UpperCamelCase			:      Optional[Any] 			=     tf.cast(0.044_715							,     x.dtype  )
 UpperCamelCase			:      int 			=     0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi  ) * (x + coeff * tf.pow(_lowerCAmelCase							,     3  ))  ))

 return x * cdf



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 UpperCamelCase			:      str 			=     tf.convert_to_tensor(_lowerCAmelCase  )

 return x * tf.tanh(tf.math.softplus(_lowerCAmelCase  )  )



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 UpperCamelCase			:      Tuple 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(0.044_715							,     x.dtype  )
 UpperCamelCase			:      Optional[Any] 			=     tf.cast(0.7_978_845_608							,     x.dtype  )

 return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)  ))



def 	A_       (  _lowerCAmelCase  )		->				Optional[Any]:
 UpperCamelCase			:      Any 			=     tf.convert_to_tensor(_lowerCAmelCase  )
 UpperCamelCase			:      List[Any] 			=     tf.cast(1.702							,     x.dtype  )
 return x * tf.math.sigmoid(coeff * x  )



def 	A_       (  _lowerCAmelCase  )		->				List[Any]:
 return tf.clip_by_value(_gelu(_lowerCAmelCase  )							,     -10							,     10  )



def 	A_       (  _lowerCAmelCase							,     _lowerCAmelCase=-1  )		->				str:
 UpperCamelCase			,					UpperCamelCase			:      List[Any] 			=     tf.split(_lowerCAmelCase							,     2							,     axis=_lowerCAmelCase  )
 return a * tf.math.sigmoid(_lowerCAmelCase  )


if version.parse(tf.version.VERSION) >= version.parse("""2.4"""):



    def 	A_       (  _lowerCAmelCase  )		->				Any:
     return tf.keras.activations.gelu(_lowerCAmelCase							,     approximate=_lowerCAmelCase  )

    __lowerCamelCase :  Optional[int]										=  tf.keras.activations.gelu


    __lowerCamelCase :  int										=  approximate_gelu_wrap
else:
    __lowerCamelCase :  List[Any]										=  _gelu
    __lowerCamelCase :  Optional[Any]										=  _gelu_new


__lowerCamelCase :  Any										=  {
    """gelu""": gelu,
    """gelu_10""": gelu_aa,
    """gelu_fast""": gelu_fast,
    """gelu_new""": gelu_new,
    """glu""": glu,
    """mish""": mish,
    """quick_gelu""": quick_gelu,
    """relu""": tf.keras.activations.relu,
    """sigmoid""": tf.keras.activations.sigmoid,
    """silu""": tf.keras.activations.swish,
    """swish""": tf.keras.activations.swish,
    """tanh""": tf.keras.activations.tanh,
}



def 	A_       (  _lowerCAmelCase  )		->				Optional[Any]:
 if activation_string in ACTaFN:
  return ACTaFN[activation_string]
 else:
  raise KeyError(F"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()  )}"""  )
