

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


__SCREAMING_SNAKE_CASE     =						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__     (					_lowerCAmelCase						):




     """simple docstring"""





     def __init__(		self						:	str      ,						*A__						:	Dict      ,						A__						:	Any=None      ,						A__						:	List[str]=None      ,						A__						:	List[str]=None      ,						**A__						:	Union[str, Any]	)       ->			Union[str, Any]:
          '''simple docstring'''




          super().__init__(*UpperCAmelCase_      ,						**UpperCAmelCase_	)
          a__   :      str     =						eval_examples
          a__   :      List[Any]     =						post_process_function
          a__   :      List[Any]     =						quant_trainer_args
          a__   :      List[Any]     =						1_2_8  # default number of calibration samples







     def 			__lowerCAmelCase	(		self						:	Dict      ,						A__						:	str=None	)       ->			List[str]:
          '''simple docstring'''




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

          a__   :      Dict     =						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 			__lowerCAmelCase	(		self						:	List[str]      ,						A__						:	Tuple=None	)       ->			Union[str, Any]:
          '''simple docstring'''




          a__   :      Optional[int]     =						self.train_dataset if calib_dataset is None else calib_dataset
          a__   :      Optional[int]     =						self.get_calib_dataloader(UpperCAmelCase_	)

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







     def 			__lowerCAmelCase	(		self						:	List[Any]      ,						A__						:	Optional[Any]=None      ,						A__						:	str=None      ,						A__						:	List[str]=None      ,						A__						:	str = "eval"	)       ->			Tuple:
          '''simple docstring'''




          a__   :      str     =						self.eval_dataset if eval_dataset is None else eval_dataset
          a__   :      List[str]     =						self.get_eval_dataloader(UpperCAmelCase_	)
          a__   :      int     =						self.eval_examples if eval_examples is None else eval_examples

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

          if self.post_process_function is not None and self.compute_metrics is not None:
               a__   :      int     =						self.post_process_function(UpperCAmelCase_      ,						UpperCAmelCase_      ,						output.predictions	)
               a__   :      Union[str, Any]     =						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}_'	):
                         a__   :      Union[str, Any]     =						metrics.pop(UpperCAmelCase_	)

               self.log(UpperCAmelCase_	)
          else:
               a__   :      Dict     =						{}

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

          a__   :      Optional[Any]     =						self.callback_handler.on_evaluate(self.args      ,						self.state      ,						self.control      ,						UpperCAmelCase_	)
          return metrics







     def 			__lowerCAmelCase	(		self						:	Optional[int]      ,						A__						:	Tuple      ,						A__						:	str      ,						A__						:	Tuple=None      ,						A__						:	str = "test"	)       ->			List[str]:
          '''simple docstring'''




          a__   :      str     =						self.get_test_dataloader(UpperCAmelCase_	)

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

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

          a__   :      Optional[int]     =						self.post_process_function(UpperCAmelCase_      ,						UpperCAmelCase_      ,						output.predictions      ,						'''predict'''	)
          a__   :      List[Any]     =						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}_'	):
                    a__   :      List[Any]     =						metrics.pop(UpperCAmelCase_	)

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







     def 			__lowerCAmelCase	(		self						:	List[Any]      ,						A__						:	Union[str, Any]="./"	)       ->			Optional[int]:
          '''simple docstring'''




          a__   :      List[str]     =						self.eval_dataset
          a__   :      Optional[Any]     =						self.get_eval_dataloader(UpperCAmelCase_	)

          a__   :      Optional[Any]     =						next(iter(UpperCAmelCase_	)	)

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

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

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

          a__   :      str     =						True

          a__   :      Tuple     =						self.model.to(UpperCAmelCase_	)

          model.eval()
          model.float()

          a__   :      List[Any]     =						model.module if hasattr(UpperCAmelCase_      ,						'''module'''	) else model
          quant_trainer.configure_model(UpperCAmelCase_      ,						self.quant_trainer_args	)

          a__   :      Tuple     =						os.path.join(UpperCAmelCase_      ,						'''model.onnx'''	)
          logger.info(F'exporting model to {output_model_file}'	)

          a__   :      int     =						{0: '''batch_size''', 1: '''seq_len'''}

          torch.onnx.export(
              UpperCAmelCase_      ,						UpperCAmelCase_      ,						UpperCAmelCase_      ,						export_params=UpperCAmelCase_      ,						opset_version=1_3      ,						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"""


import warnings

from ...utils import logging
from .image_processing_yolos import YolosImageProcessor


SCREAMING_SNAKE_CASE			  =  logging.get_logger(__name__)



class 	__a	(   _lowerCAmelCase						):






       def __init__(	self			:				str							,   *UpperCAmelCase_			:				str							,   **UpperCAmelCase_			:				Any      )->	None:







              """simple docstring"""
              warnings.warn(
                  "The class YolosFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please"
                  " use YolosImageProcessor instead."							,   UpperCAmelCase_							,   )
              super().__init__(*UpperCAmelCase_							,   **UpperCAmelCase_      )





