


import math
import time

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput, speed_metrics


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





     def __init__(self		,     *UpperCAmelCase		,     UpperCAmelCase=None		,     UpperCAmelCase=None		,     **UpperCAmelCase       )     ->   List[Any]:
      super().__init__(*UpperCAmelCase		,     **UpperCAmelCase       )
      _lowercase							      =eval_examples
      _lowercase							      =post_process_function





     def      __A					(self		,     UpperCAmelCase=None		,     UpperCAmelCase=None		,     UpperCAmelCase=None		,     UpperCAmelCase = "eval"       )     ->   str:
      _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
      _lowercase							      =time.time()
      try:
       _lowercase							      =eval_loop(
           UpperCAmelCase		,     description='''Evaluation'''		,     prediction_loss_only=True if compute_metrics is None else None		,     ignore_keys=UpperCAmelCase		,     metric_key_prefix=UpperCAmelCase		,     )
      finally:
       _lowercase							      =compute_metrics
      _lowercase							      =self.args.eval_batch_size * self.args.world_size
      if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
       start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
      output.metrics.update(
          speed_metrics(
              UpperCAmelCase		,     UpperCAmelCase		,     num_samples=output.num_samples		,     num_steps=math.ceil(output.num_samples / total_batch_size       )		,     )       )
      if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
       # Only the main node write the results by default
       _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       )
       metrics.update(output.metrics       )
      else:
       _lowercase							      =output.metrics

      if self.args.should_log:
       # Only the main node log the results by default
       self.log(UpperCAmelCase       )

      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      __A					(self		,     UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase=None		,     UpperCAmelCase = "test"       )     ->   Union[str, Any]:
      _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
      _lowercase							      =time.time()
      try:
       _lowercase							      =eval_loop(
           UpperCAmelCase		,     description='''Prediction'''		,     prediction_loss_only=True if compute_metrics is None else None		,     ignore_keys=UpperCAmelCase		,     metric_key_prefix=UpperCAmelCase		,     )
      finally:
       _lowercase							      =compute_metrics
      _lowercase							      =self.args.eval_batch_size * self.args.world_size
      if f"{metric_key_prefix}_jit_compilation_time" in output.metrics:
       start_time += output.metrics[f"{metric_key_prefix}_jit_compilation_time"]
      output.metrics.update(
          speed_metrics(
              UpperCAmelCase		,     UpperCAmelCase		,     num_samples=output.num_samples		,     num_steps=math.ceil(output.num_samples / total_batch_size       )		,     )       )

      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       )
      metrics.update(output.metrics       )
      return PredictionOutput(predictions=predictions.predictions		,     label_ids=predictions.label_ids		,     metrics=UpperCAmelCase       )







import warnings

from ...utils import logging
from .image_processing_donut import DonutImageProcessor


lowerCamelCase				:		Any     =   logging.get_logger(__name__)







class   A(						UpperCamelCase   ):



 '''simple docstring'''


 def __init__(				self					:  Dict						,							*A_					:  Optional[int]						,							**A_					:  int  )   ->	None:

       """simple docstring"""






       warnings.warn(
           'The class DonutFeatureExtractor is deprecated and will be removed in version 5 of Transformers. Please'
           ' use DonutImageProcessor instead.'						,							A_						,							)
       super().__init__(*A_						,							**A_  )



