



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


lowercase__					:						Any							=			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  _UpperCAmelCase (    _snake_case):

 def __init__(  self	: str						,  *lowercase_	: Tuple						,  lowercase_	: List[Any]=None						,  lowercase_	: Optional[int]=None						,  lowercase_	: List[str]=None						,  **lowercase_	: List[str]       ):
     super().__init__(*lowerCAmelCase__						,  **lowerCAmelCase__       )
     snake_case_				:  Tuple       =			eval_examples
     snake_case_				:  str       =			post_process_function
     snake_case_				:  Dict       =			quant_trainer_args
     snake_case_				:  int       =			128  # default number of calibration samples

 def 		_snake_case      (  self	: int						,  lowercase_	: Dict=None       ):
     if calib_dataset is None and self.calib_dataset is None:
         raise ValueError('''Trainer: calibration requires an calib_dataset.'''       )
     snake_case_				:  Optional[Any]       =			calib_dataset if calib_dataset is not None else self.calib_dataset

     snake_case_				:  str       =			self._remove_unused_columns(lowerCAmelCase__						,  description='''Calibration'''       )

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

 def 		_snake_case      (  self	: Optional[int]						,  lowercase_	: Any=None       ):
     snake_case_				:  int       =			self.train_dataset if calib_dataset is None else calib_dataset
     snake_case_				:  Union[str, Any]       =			self.get_calib_dataloader(lowerCAmelCase__       )

     snake_case_				:  Tuple       =			self.model
     quant_trainer.configure_model(lowerCAmelCase__						,  self.quant_trainer_args						,  calib=lowerCAmelCase__       )
     model.eval()
     quant_trainer.enable_calibration(lowerCAmelCase__       )

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

     quant_trainer.finish_calibration(lowerCAmelCase__						,  self.quant_trainer_args       )
     snake_case_				:  Optional[int]       =			model

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: Optional[Any]=None						,  lowercase_	: int=None						,  lowercase_	: Tuple=None						,  lowercase_	: str = "eval"       ):
     snake_case_				:  Tuple       =			self.eval_dataset if eval_dataset is None else eval_dataset
     snake_case_				:  Union[str, Any]       =			self.get_eval_dataloader(lowerCAmelCase__       )
     snake_case_				:  int       =			self.eval_examples if eval_examples is None else eval_examples

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

     if self.post_process_function is not None and self.compute_metrics is not None:
         snake_case_				:  Optional[Any]       =			self.post_process_function(lowerCAmelCase__						,  lowerCAmelCase__						,  output.predictions       )
         snake_case_				:  Union[str, Any]       =			self.compute_metrics(lowerCAmelCase__       )

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

         self.log(lowerCAmelCase__       )
     else:
         snake_case_				:  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()       )

     snake_case_				:  str       =			self.callback_handler.on_evaluate(self.args						,  self.state						,  self.control						,  lowerCAmelCase__       )
     return metrics

 def 		_snake_case      (  self	: List[str]						,  lowercase_	: Any						,  lowercase_	: int						,  lowercase_	: str=None						,  lowercase_	: str = "test"       ):
     snake_case_				:  List[Any]       =			self.get_test_dataloader(lowerCAmelCase__       )

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

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

     snake_case_				:  Optional[int]       =			self.post_process_function(lowerCAmelCase__						,  lowerCAmelCase__						,  output.predictions						,  '''predict'''       )
     snake_case_				:  str       =			self.compute_metrics(lowerCAmelCase__       )

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

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






 def 		_snake_case      (  self	: Optional[int]						,  lowercase_	: Optional[int]="./"       ):
     snake_case_				:  str       =			self.eval_dataset
     snake_case_				:  Tuple       =			self.get_eval_dataloader(lowerCAmelCase__       )

     snake_case_				:  List[str]       =			next(iter(lowerCAmelCase__       )       )

     # saving device - to make it consistent
     snake_case_				:  str       =			torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''       )

     # convert to tuple
     snake_case_				:  Optional[Any]       =			tuple(v.to(lowerCAmelCase__       ) for k, v in batch.items()       )

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

     snake_case_				:  Union[str, Any]       =			True

     snake_case_				:  str       =			self.model.to(lowerCAmelCase__       )

     model.eval()
     model.float()

     snake_case_				:  Dict       =			model.module if hasattr(lowerCAmelCase__						,  '''module'''       ) else model
     quant_trainer.configure_model(lowerCAmelCase__						,  self.quant_trainer_args       )

     snake_case_				:  Union[str, Any]       =			os.path.join(lowerCAmelCase__						,  '''model.onnx'''       )
     logger.info(f"exporting model to {output_model_file}"       )

     snake_case_				:  Optional[int]       =			{0: '''batch_size''', 1: '''seq_len'''}

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










"""simple docstring"""

from argparse import ArgumentParser

from datasets.commands.convert import ConvertCommand
from datasets.commands.dummy_data import DummyDataCommand
from datasets.commands.env import EnvironmentCommand
from datasets.commands.run_beam import RunBeamCommand
from datasets.commands.test import TestCommand
from datasets.utils.logging import set_verbosity_info




def __lowercase    (							_a					):
    return {key.lstrip('''-'''					): value for key, value in zip(unknown_args[::2]	,    unknown_args[1::2]					)}




def __lowercase    (							):
    snake_case_				:  List[str]       =			ArgumentParser(
        '''HuggingFace Datasets CLI tool'''	,    usage='''datasets-cli <command> [<args>]'''	,    allow_abbrev=_a					)
    snake_case_				:  List[Any]       =			parser.add_subparsers(help='''datasets-cli command helpers'''					)
    set_verbosity_info()

    # Register commands
    ConvertCommand.register_subcommand(_a					)
    EnvironmentCommand.register_subcommand(_a					)
    TestCommand.register_subcommand(_a					)
    RunBeamCommand.register_subcommand(_a					)
    DummyDataCommand.register_subcommand(_a					)

    # Parse args
    snake_case_,			snake_case_				:  Optional[Any]       =			parser.parse_known_args()
    if not hasattr(_a	,    '''func'''					):
        parser.print_help()
        exit(1					)
    snake_case_				:  Optional[int]       =			parse_unknown_args(_a					)

    # Run
    snake_case_				:  Optional[int]       =			args.func(_a	,    **_a					)
    service.run()


if __name__ == "__main__":
     main()





