






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


a_					= 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      (			snake_case__				):




 def __init__(      self	:							List[str]					,      *__lowercase	:							Any					,      __lowercase	:							str=None					,      __lowercase	:							int=None					,      __lowercase	:							Tuple=None					,      **__lowercase	:							Tuple      )	->						str:
       super().__init__(*lowercase_					,      **lowercase_      )
       SCREAMING_SNAKE_CASE__     :				List[str]									=eval_examples
       SCREAMING_SNAKE_CASE__     :				str									=post_process_function
       SCREAMING_SNAKE_CASE__     :				int									=quant_trainer_args
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=1_28  # default number of calibration samples




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							Union[str, Any]=None      )	->						Tuple:
       if calib_dataset is None and self.calib_dataset is None:
             raise ValueError('''Trainer: calibration requires an calib_dataset.'''      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=calib_dataset if calib_dataset is not None else self.calib_dataset

       SCREAMING_SNAKE_CASE__     :				str									=self._remove_unused_columns(lowercase_					,      description='''Calibration'''      )

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




 def      __magic_name__	(      self	:							List[Any]					,      __lowercase	:							Optional[int]=None      )	->						Optional[Any]:
       SCREAMING_SNAKE_CASE__     :				Dict									=self.train_dataset if calib_dataset is None else calib_dataset
       SCREAMING_SNAKE_CASE__     :				str									=self.get_calib_dataloader(lowercase_      )

       SCREAMING_SNAKE_CASE__     :				Any									=self.model
       quant_trainer.configure_model(lowercase_					,      self.quant_trainer_args					,      calib=lowercase_      )
       model.eval()
       quant_trainer.enable_calibration(lowercase_      )

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

       quant_trainer.finish_calibration(lowercase_					,      self.quant_trainer_args      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=model




 def      __magic_name__	(      self	:							Any					,      __lowercase	:							Dict=None					,      __lowercase	:							Optional[int]=None					,      __lowercase	:							Optional[Any]=None					,      __lowercase	:							Optional[int] = "eval"      )	->						Dict:
       SCREAMING_SNAKE_CASE__     :				List[str]									=self.eval_dataset if eval_dataset is None else eval_dataset
       SCREAMING_SNAKE_CASE__     :				Any									=self.get_eval_dataloader(lowercase_      )
       SCREAMING_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.
       SCREAMING_SNAKE_CASE__     :				List[str]									=self.compute_metrics
       SCREAMING_SNAKE_CASE__     :				str									=None
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       try:
             SCREAMING_SNAKE_CASE__     :				Any									=eval_loop(
                 lowercase_					,      description='''Evaluation'''					,      prediction_loss_only=True if compute_metrics is None else None					,      ignore_keys=lowercase_					,      )
       finally:
             SCREAMING_SNAKE_CASE__     :				Any									=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(lowercase_					,      lowercase_					,      output.predictions      )
             SCREAMING_SNAKE_CASE__     :				Any									=self.compute_metrics(lowercase_      )

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

             self.log(lowercase_      )
       else:
             SCREAMING_SNAKE_CASE__     :				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()      )

       SCREAMING_SNAKE_CASE__     :				int									=self.callback_handler.on_evaluate(self.args					,      self.state					,      self.control					,      lowercase_      )
       return metrics




 def      __magic_name__	(      self	:							Optional[int]					,      __lowercase	:							int					,      __lowercase	:							Optional[int]					,      __lowercase	:							Tuple=None					,      __lowercase	:							Union[str, Any] = "test"      )	->						List[Any]:
       SCREAMING_SNAKE_CASE__     :				Dict									=self.get_test_dataloader(lowercase_      )

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

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

       SCREAMING_SNAKE_CASE__     :				List[str]									=self.post_process_function(lowercase_					,      lowercase_					,      output.predictions					,      '''predict'''      )
       SCREAMING_SNAKE_CASE__     :				Any									=self.compute_metrics(lowercase_      )

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

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




 def      __magic_name__	(      self	:							Dict					,      __lowercase	:							Optional[int]="./"      )	->						Union[str, Any]:
       SCREAMING_SNAKE_CASE__     :				int									=self.eval_dataset
       SCREAMING_SNAKE_CASE__     :				Any									=self.get_eval_dataloader(lowercase_      )

       SCREAMING_SNAKE_CASE__     :				int									=next(iter(lowercase_      )      )

       # saving device - to make it consistent
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''      )

       # convert to tuple
       SCREAMING_SNAKE_CASE__     :				str									=tuple(v.to(lowercase_      ) for k, v in batch.items()      )

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

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=True

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=self.model.to(lowercase_      )

       model.eval()
       model.float()

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=model.module if hasattr(lowercase_					,      '''module'''      ) else model
       quant_trainer.configure_model(lowercase_					,      self.quant_trainer_args      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=os.path.join(lowercase_					,      '''model.onnx'''      )
       logger.info(F"exporting model to {output_model_file}"      )

       SCREAMING_SNAKE_CASE__     :				Dict									={0: "batch_size", 1: "seq_len"}

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













'''simple docstring'''
import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
  raise RuntimeError('At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training')

# TF training parameters
a_					= False
a_					= False





def  _a(  UpperCamelCase__				:						Namespace							):




      '''simple docstring'''






      return TrainCommand(UpperCamelCase__							)







class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):




 @staticmethod
 def      __magic_name__	(      __lowercase	:							ArgumentParser      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=parser.add_parser('''train'''					,      help='''CLI tool to train a model on a task.'''      )

       train_parser.add_argument(
           '''--train_data'''					,      type=__lowercase					,      required=__lowercase					,      help='''path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.'''					,      )
       train_parser.add_argument(
           '''--column_label'''					,      type=__lowercase					,      default=0					,      help='''Column of the dataset csv file with example labels.'''      )
       train_parser.add_argument(
           '''--column_text'''					,      type=__lowercase					,      default=1					,      help='''Column of the dataset csv file with example texts.'''      )
       train_parser.add_argument(
           '''--column_id'''					,      type=__lowercase					,      default=2					,      help='''Column of the dataset csv file with example ids.'''      )
       train_parser.add_argument(
           '''--skip_first_row'''					,      action='''store_true'''					,      help='''Skip the first row of the csv file (headers).'''      )

       train_parser.add_argument('''--validation_data'''					,      type=__lowercase					,      default=''''''					,      help='''path to validation dataset.'''      )
       train_parser.add_argument(
           '''--validation_split'''					,      type=__lowercase					,      default=0.1					,      help='''if validation dataset is not provided, fraction of train dataset to use as validation dataset.'''					,      )

       train_parser.add_argument('''--output'''					,      type=__lowercase					,      default='''./'''					,      help='''path to saved the trained model.'''      )

       train_parser.add_argument(
           '''--task'''					,      type=__lowercase					,      default='''text_classification'''					,      help='''Task to train the model on.'''      )
       train_parser.add_argument(
           '''--model'''					,      type=__lowercase					,      default='''bert-base-uncased'''					,      help='''Model\'s name or path to stored model.'''      )
       train_parser.add_argument('''--train_batch_size'''					,      type=__lowercase					,      default=32					,      help='''Batch size for training.'''      )
       train_parser.add_argument('''--valid_batch_size'''					,      type=__lowercase					,      default=64					,      help='''Batch size for validation.'''      )
       train_parser.add_argument('''--learning_rate'''					,      type=__lowercase					,      default=3e-5					,      help='''Learning rate.'''      )
       train_parser.add_argument('''--adam_epsilon'''					,      type=__lowercase					,      default=1e-08					,      help='''Epsilon for Adam optimizer.'''      )
       train_parser.set_defaults(func=__lowercase      )




 def __init__(      self	:							Tuple					,      __lowercase	:							Namespace      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=logging.get_logger('''transformers-cli/training'''      )

       SCREAMING_SNAKE_CASE__     :				int									='''tf''' if is_tf_available() else '''torch'''

       os.makedirs(args.output					,      exist_ok=__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Any									=args.output

       SCREAMING_SNAKE_CASE__     :				str									=args.column_label
       SCREAMING_SNAKE_CASE__     :				List[Any]									=args.column_text
       SCREAMING_SNAKE_CASE__     :				Tuple									=args.column_id

       self.logger.info(F"Loading {args.task} pipeline for {args.model}"      )
       if args.task == "text_classification":
             SCREAMING_SNAKE_CASE__     :				List[str]									=TextClassificationPipeline.from_pretrained(args.model      )
       elif args.task == "token_classification":
             raise NotImplementedError
       elif args.task == "question_answering":
             raise NotImplementedError

       self.logger.info(F"Loading dataset from {args.train_data}"      )
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=Processor.create_from_csv(
           args.train_data					,      column_label=args.column_label					,      column_text=args.column_text					,      column_id=args.column_id					,      skip_first_row=args.skip_first_row					,      )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=None
       if args.validation_data:
             self.logger.info(F"Loading validation dataset from {args.validation_data}"      )
             SCREAMING_SNAKE_CASE__     :				List[Any]									=Processor.create_from_csv(
                 args.validation_data					,      column_label=args.column_label					,      column_text=args.column_text					,      column_id=args.column_id					,      skip_first_row=args.skip_first_row					,      )

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=args.validation_split
       SCREAMING_SNAKE_CASE__     :				List[Any]									=args.train_batch_size
       SCREAMING_SNAKE_CASE__     :				Any									=args.valid_batch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=args.learning_rate
       SCREAMING_SNAKE_CASE__     :				int									=args.adam_epsilon




 def      __magic_name__	(      self	:							Any      )	->						str:
       if self.framework == "tf":
             return self.run_tf()
       return self.run_torch()




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       raise NotImplementedError




 def      __magic_name__	(      self	:							Dict      )	->						List[Any]:
       self.pipeline.fit(
           self.train_dataset					,      validation_data=self.valid_dataset					,      validation_split=self.validation_split					,      learning_rate=self.learning_rate					,      adam_epsilon=self.adam_epsilon					,      train_batch_size=self.train_batch_size					,      valid_batch_size=self.valid_batch_size					,      )

       # Save trained pipeline
       self.pipeline.save_pretrained(self.output      )