




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


UpperCamelCase_    	=				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				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






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







             '''simple docstring'''







             super().__init__(*A,							**A  )
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						eval_examples
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						post_process_function
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						quant_trainer_args
             SCREAMING_SNAKE_CASE   :      Tuple    					=						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.'  )
             SCREAMING_SNAKE_CASE   :      List[str]    					=						calib_dataset if calib_dataset is not None else self.calib_dataset

             SCREAMING_SNAKE_CASE   :      Tuple    					=						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'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.train_dataset if calib_dataset is None else calib_dataset
             SCREAMING_SNAKE_CASE   :      Any    					=						self.get_calib_dataloader(A  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						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
                   SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      List[str]    					=						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  )
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						model







       def        UpperCamelCase_							(      self,							A=None,							A=None,							A=None,							A = "eval"  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.eval_dataset if eval_dataset is None else eval_dataset
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						self.get_eval_dataloader(A  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						self.eval_examples if eval_examples is None else eval_examples

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

             if self.post_process_function is not None and self.compute_metrics is not None:
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						self.post_process_function(A,							A,							output.predictions  )
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						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}_"  ):
                               SCREAMING_SNAKE_CASE   :      List[Any]    					=						metrics.pop(A  )

                   self.log(A  )
             else:
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						{}

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

             SCREAMING_SNAKE_CASE   :      Dict    					=						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'''







             SCREAMING_SNAKE_CASE   :      str    					=						self.get_test_dataloader(A  )

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

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

             SCREAMING_SNAKE_CASE   :      int    					=						self.post_process_function(A,							A,							output.predictions,							'predict'  )
             SCREAMING_SNAKE_CASE   :      Union[str, 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}_"  ):
                         SCREAMING_SNAKE_CASE   :      Optional[int]    					=						metrics.pop(A  )

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







       def        UpperCamelCase_							(      self,							A="./"  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						self.eval_dataset
             SCREAMING_SNAKE_CASE   :      int    					=						self.get_eval_dataloader(A  )

             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						next(iter(A  )  )

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

             # convert to tuple
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						tuple(v.to(A  ) for k, v in batch.items()  )

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

             SCREAMING_SNAKE_CASE   :      List[str]    					=						True

             SCREAMING_SNAKE_CASE   :      Dict    					=						self.model.to(A  )

             model.eval()
             model.float()

             SCREAMING_SNAKE_CASE   :      int    					=						model.module if hasattr(A,							'module'  ) else model
             quant_trainer.configure_model(A,							self.quant_trainer_args  )

             SCREAMING_SNAKE_CASE   :      Any    					=						os.path.join(A,							'model.onnx'  )
             logger.info(F"exporting model to {output_model_file}"  )

             SCREAMING_SNAKE_CASE   :      List[str]    					=						{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'  )








'''simple docstring'''



from typing import List, Optional, TypeVar

from .arrow_dataset import Dataset, _concatenate_map_style_datasets, _interleave_map_style_datasets
from .dataset_dict import DatasetDict, IterableDatasetDict
from .info import DatasetInfo
from .iterable_dataset import IterableDataset, _concatenate_iterable_datasets, _interleave_iterable_datasets
from .splits import NamedSplit
from .utils import logging
from .utils.py_utils import Literal


UpperCamelCase_    	=				logging.get_logger(__name__)


UpperCamelCase_    	=				TypeVar("DatasetType", Dataset, IterableDataset)




def   lowercase__(			__UpperCamelCase:					List[DatasetType]      ,__UpperCamelCase:					Optional[List[float]] = None      ,__UpperCamelCase:					Optional[int] = None      ,__UpperCamelCase:					Optional[DatasetInfo] = None      ,__UpperCamelCase:					Optional[NamedSplit] = None      ,__UpperCamelCase:					Literal["first_exhausted", "all_exhausted"] = "first_exhausted"      ,):

      """simple docstring"""






      from .arrow_dataset import Dataset
      from .iterable_dataset import IterableDataset

      if not datasets:
            raise ValueError('Unable to interleave an empty list of datasets.'    )
      for i, dataset in enumerate(__UpperCamelCase    ):
            if not isinstance(__UpperCamelCase      ,(Dataset, IterableDataset)    ):
                  if isinstance(__UpperCamelCase      ,(DatasetDict, IterableDatasetDict)    ):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'    )
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCamelCase    )}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase    )    )}']"    )
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase    ).__name__}."    )
            if i == 0:
                  SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						(
                      (Dataset, IterableDataset) if isinstance(__UpperCamelCase      ,__UpperCamelCase    ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCamelCase      ,__UpperCamelCase    ):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."    )
      if stopping_strategy not in ["first_exhausted", "all_exhausted"]:
            raise ValueError(f"{stopping_strategy} is not supported. Please enter a valid stopping_strategy."    )
      if dataset_type is Dataset:
            return _interleave_map_style_datasets(
                __UpperCamelCase      ,__UpperCamelCase      ,__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,stopping_strategy=__UpperCamelCase    )
      else:
            return _interleave_iterable_datasets(
                __UpperCamelCase      ,__UpperCamelCase      ,__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,stopping_strategy=__UpperCamelCase    )





def   lowercase__(			__UpperCamelCase:					List[DatasetType]      ,__UpperCamelCase:					Optional[DatasetInfo] = None      ,__UpperCamelCase:					Optional[NamedSplit] = None      ,__UpperCamelCase:					int = 0      ,):

      """simple docstring"""






      if not dsets:
            raise ValueError('Unable to concatenate an empty list of datasets.'    )
      for i, dataset in enumerate(__UpperCamelCase    ):
            if not isinstance(__UpperCamelCase      ,(Dataset, IterableDataset)    ):
                  if isinstance(__UpperCamelCase      ,(DatasetDict, IterableDatasetDict)    ):
                        if not dataset:
                              raise ValueError(
                                  f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} "
                                  'is an empty dataset dictionary.'    )
                        raise ValueError(
                            f"Dataset at position {i} has at least one split: {list(__UpperCamelCase    )}\n"
                            f"Please pick one to interleave with the other datasets, for example: dataset['{next(iter(__UpperCamelCase    )    )}']"    )
                  raise ValueError(
                      f"Expected a list of Dataset objects or a list of IterableDataset objects, but element at position {i} is a {type(__UpperCamelCase    ).__name__}."    )
            if i == 0:
                  SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      int    					=						(
                      (Dataset, IterableDataset) if isinstance(__UpperCamelCase      ,__UpperCamelCase    ) else (IterableDataset, Dataset)
                  )
            elif not isinstance(__UpperCamelCase      ,__UpperCamelCase    ):
                  raise ValueError(
                      f"Unable to interleave a {dataset_type.__name__} (at position 0) with a {other_type.__name__} (at position {i}). Expected a list of Dataset objects or a list of IterableDataset objects."    )
      if dataset_type is Dataset:
            return _concatenate_map_style_datasets(__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,axis=__UpperCamelCase    )
      else:
            return _concatenate_iterable_datasets(__UpperCamelCase      ,info=__UpperCamelCase      ,split=__UpperCamelCase      ,axis=__UpperCamelCase    )


