


'''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				:  Optional[int]    		=      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     __lowerCAmelCase		(lowercase_	):





      '''simple docstring'''






      def __init__(self       :       int  ,		*UpperCamelCase       :       str  ,		UpperCamelCase       :       List[str]=None  ,		UpperCamelCase       :       str=None  ,		UpperCamelCase       :       List[str]=None  ,		**UpperCamelCase       :       Tuple					):

             '''simple docstring'''







             super().__init__(*UpperCamelCase  ,		**UpperCamelCase					)
             lowercase__      =					eval_examples
             lowercase__      =					post_process_function
             lowercase__      =					quant_trainer_args
             lowercase__      =					128  # default number of calibration samples



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       Optional[int]=None					):

             '''simple docstring'''







             if calib_dataset is None and self.calib_dataset is None:
                    raise ValueError('''Trainer: calibration requires an calib_dataset.'''					)
             lowercase__      =					calib_dataset if calib_dataset is not None else self.calib_dataset

             lowercase__      =					self._remove_unused_columns(UpperCamelCase  ,		description='''Calibration'''					)

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



      def  UpperCamelCase__						(self       :       Optional[int]  ,		UpperCamelCase       :       int=None					):

             '''simple docstring'''







             lowercase__      =					self.train_dataset if calib_dataset is None else calib_dataset
             lowercase__      =					self.get_calib_dataloader(UpperCamelCase					)

             lowercase__      =					self.model
             quant_trainer.configure_model(UpperCamelCase  ,		self.quant_trainer_args  ,		calib=UpperCamelCase					)
             model.eval()
             quant_trainer.enable_calibration(UpperCamelCase					)

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

             quant_trainer.finish_calibration(UpperCamelCase  ,		self.quant_trainer_args					)
             lowercase__      =					model



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       int=None  ,		UpperCamelCase       :       List[Any]=None  ,		UpperCamelCase       :       Optional[Any]=None  ,		UpperCamelCase       :       str = "eval"					):

             '''simple docstring'''







             lowercase__      =					self.eval_dataset if eval_dataset is None else eval_dataset
             lowercase__      =					self.get_eval_dataloader(UpperCamelCase					)
             lowercase__      =					self.eval_examples if eval_examples is None else eval_examples

             # Temporarily disable metric computation, we will do it in the loop here.
             lowercase__      =					self.compute_metrics
             lowercase__      =					None
             lowercase__      =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
             try:
                    lowercase__      =					eval_loop(
                        UpperCamelCase  ,		description='''Evaluation'''  ,		prediction_loss_only=True if compute_metrics is None else None  ,		ignore_keys=UpperCamelCase  ,		)
             finally:
                    lowercase__      =					compute_metrics

             if self.post_process_function is not None and self.compute_metrics is not None:
                    lowercase__      =					self.post_process_function(UpperCamelCase  ,		UpperCamelCase  ,		output.predictions					)
                    lowercase__      =					self.compute_metrics(UpperCamelCase					)

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

                    self.log(UpperCamelCase					)
             else:
                    lowercase__      =					{}

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

             lowercase__      =					self.callback_handler.on_evaluate(self.args  ,		self.state  ,		self.control  ,		UpperCamelCase					)
             return metrics



      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       List[Any]  ,		UpperCamelCase       :       int  ,		UpperCamelCase       :       List[Any]=None  ,		UpperCamelCase       :       str = "test"					):

             '''simple docstring'''







             lowercase__      =					self.get_test_dataloader(UpperCamelCase					)

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

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

             lowercase__      =					self.post_process_function(UpperCamelCase  ,		UpperCamelCase  ,		output.predictions  ,		'''predict'''					)
             lowercase__      =					self.compute_metrics(UpperCamelCase					)

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

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






      def  UpperCamelCase__						(self       :       Optional[Any]  ,		UpperCamelCase       :       Union[str, Any]="./"					):

             '''simple docstring'''







             lowercase__      =					self.eval_dataset
             lowercase__      =					self.get_eval_dataloader(UpperCamelCase					)

             lowercase__      =					next(iter(UpperCamelCase					)					)

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

             # convert to tuple
             lowercase__      =					tuple(v.to(UpperCamelCase					) for k, v in batch.items()					)

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

             lowercase__      =					True

             lowercase__      =					self.model.to(UpperCamelCase					)

             model.eval()
             model.float()

             lowercase__      =					model.module if hasattr(UpperCamelCase  ,		'''module'''					) else model
             quant_trainer.configure_model(UpperCamelCase  ,		self.quant_trainer_args					)

             lowercase__      =					os.path.join(UpperCamelCase  ,		'''model.onnx'''					)
             logger.info(f"exporting model to {output_model_file}"					)

             lowercase__      =					{0: '''batch_size''', 1: '''seq_len'''}

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








'''simple docstring'''

from __future__ import annotations

from typing import Any
class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       Optional[Any]  ,		UpperCamelCase       :       int = 6					):

             '''simple docstring'''







             lowercase__      =					None
             lowercase__      =					None
             self.create_linked_list(UpperCamelCase					)



      def  UpperCamelCase__						(self       :       List[str]  ,		UpperCamelCase       :       int					):

             '''simple docstring'''







             lowercase__      =					Node()
             lowercase__      =					current_node
             lowercase__      =					current_node
             lowercase__      =					current_node
             for _ in range(1  ,		UpperCamelCase					):
                    lowercase__      =					Node()
                    lowercase__      =					current_node
                    lowercase__      =					previous_node
                    lowercase__      =					current_node
             lowercase__      =					self.front
             lowercase__      =					previous_node



      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             return (
                 self.front == self.rear
                 and self.front is not None
                 and self.front.data is None
             )



      def  UpperCamelCase__						(self       :       Dict					):

             '''simple docstring'''







             self.check_can_perform_operation()
             return self.front.data if self.front else None



      def  UpperCamelCase__						(self       :       Any  ,		UpperCamelCase       :       Any					):

             '''simple docstring'''







             if self.rear is None:
                    return

             self.check_is_full()
             if not self.is_empty():
                    lowercase__      =					self.rear.next
             if self.rear:
                    lowercase__      =					data



      def  UpperCamelCase__						(self       :       List[Any]					):

             '''simple docstring'''







             self.check_can_perform_operation()
             if self.rear is None or self.front is None:
                    return None
             if self.front == self.rear:
                    lowercase__      =					self.front.data
                    lowercase__      =					None
                    return data

             lowercase__      =					self.front
             lowercase__      =					old_front.next
             lowercase__      =					old_front.data
             lowercase__      =					None
             return data



      def  UpperCamelCase__						(self       :       Union[str, Any]					):

             '''simple docstring'''







             if self.is_empty():
                    raise Exception('''Empty Queue'''					)






      def  UpperCamelCase__						(self       :       int					):

             '''simple docstring'''







             if self.rear and self.rear.next == self.front:
                    raise Exception('''Full Queue'''					)






class     __lowerCAmelCase		:





      '''simple docstring'''






      def __init__(self       :       Any					):

             '''simple docstring'''







             lowercase__      =					None
             lowercase__      =					None
             lowercase__      =					None


if __name__ == "__main__":
 import doctest

 doctest.testmod()




