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		(   UpperCamelCase_    ):
    def __init__(  self , *lowerCAmelCase , lowerCAmelCase=None , lowerCAmelCase=None , **lowerCAmelCase   )       ->					int:
     super().__init__(*UpperCamelCase__ , **UpperCamelCase__   )
     SCREAMING_SNAKE_CASE__:   str=      eval_examples
     SCREAMING_SNAKE_CASE__:   Tuple=      post_process_function
    def 						UpperCamelCase_				(  self , lowerCAmelCase=None , lowerCAmelCase=None , lowerCAmelCase=None , lowerCAmelCase = "eval"   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   List[str]=      self.eval_dataset if eval_dataset is None else eval_dataset
     SCREAMING_SNAKE_CASE__:   Any=      self.get_eval_dataloader(UpperCamelCase__   )
     SCREAMING_SNAKE_CASE__:   Dict=      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
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      time.time()
     try:
      SCREAMING_SNAKE_CASE__:   List[str]=      eval_loop(
          UpperCamelCase__ , description='''Evaluation''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase__ , metric_key_prefix=UpperCamelCase__ , )
     finally:
      SCREAMING_SNAKE_CASE__:   List[Any]=      compute_metrics
     SCREAMING_SNAKE_CASE__:   Any=      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
      SCREAMING_SNAKE_CASE__:   Dict=      self.post_process_function(UpperCamelCase__ , UpperCamelCase__ , output.predictions   )
      SCREAMING_SNAKE_CASE__:   Optional[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}_'   ):
        SCREAMING_SNAKE_CASE__:   List[str]=      metrics.pop(UpperCamelCase__   )
      metrics.update(output.metrics   )
     else:
      SCREAMING_SNAKE_CASE__:   int=      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()   )

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



    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , lowerCAmelCase = "test"   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   List[Any]=      self.get_test_dataloader(UpperCamelCase__   )

     # Temporarily disable metric computation, we will do it in the loop here.
     SCREAMING_SNAKE_CASE__:   int=      self.compute_metrics
     SCREAMING_SNAKE_CASE__:   Optional[int]=      None
     SCREAMING_SNAKE_CASE__:   Tuple=      self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      time.time()
     try:
      SCREAMING_SNAKE_CASE__:   Tuple=      eval_loop(
          UpperCamelCase__ , description='''Prediction''' , prediction_loss_only=True if compute_metrics is None else None , ignore_keys=UpperCamelCase__ , metric_key_prefix=UpperCamelCase__ , )
     finally:
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      compute_metrics
     SCREAMING_SNAKE_CASE__:   Any=      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

     SCREAMING_SNAKE_CASE__:   int=      self.post_process_function(UpperCamelCase__ , UpperCamelCase__ , output.predictions , '''predict'''   )
     SCREAMING_SNAKE_CASE__:   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}_'   ):
       SCREAMING_SNAKE_CASE__:   Dict=      metrics.pop(UpperCamelCase__   )
     metrics.update(output.metrics   )
     return PredictionOutput(predictions=predictions.predictions , label_ids=predictions.label_ids , metrics=UpperCamelCase__   )


from typing import TYPE_CHECKING

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase_     :   Dict 				=				{
    'configuration_autoformer': [
        'AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'AutoformerConfig',
    ],
}

try:
      if not is_torch_available():
            raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
      pass
else:
      lowercase_     :   str 				=				[
          'AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
          'AutoformerForPrediction',
          'AutoformerModel',
          'AutoformerPreTrainedModel',
      ]


if TYPE_CHECKING:
      from .configuration_autoformer import (
          AUTOFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
          AutoformerConfig,
      )

      try:
            if not is_torch_available():
                  raise OptionalDependencyNotAvailable()
      except OptionalDependencyNotAvailable:
            pass
      else:
            from .modeling_autoformer import (
                AUTOFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
                AutoformerForPrediction,
                AutoformerModel,
                AutoformerPreTrainedModel,
            )

else:
      import sys

      lowercase_     :   Tuple 				=				_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

