


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 							_UpperCamelCase (						A						):

   '''simple docstring'''







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






         '''simple docstring'''



         super().__init__(*_lowerCamelCase       ,  **_lowerCamelCase							)
         __lowerCamelCase     :      Tuple       						=						eval_examples
         __lowerCamelCase     :      Tuple       						=						post_process_function

   def   _snake_case    (  self  : Union[str, Any]       ,  _lowerCamelCase  : Any=None       ,  _lowerCamelCase  : Optional[int]=None       ,  _lowerCamelCase  : int=None       ,  _lowerCamelCase  : str = "eval"							):






         '''simple docstring'''



         __lowerCamelCase     :      str       						=						self.eval_dataset if eval_dataset is None else eval_dataset
         __lowerCamelCase     :      Tuple       						=						self.get_eval_dataloader(_lowerCamelCase							)
         __lowerCamelCase     :      Any       						=						self.eval_examples if eval_examples is None else eval_examples

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

               # Prefix all keys with metric_key_prefix + '_'
               for key in list(metrics.keys()							):
                     if not key.startswith(F"""{metric_key_prefix}_"""							):
                           __lowerCamelCase     :      Tuple       						=						metrics.pop(_lowerCamelCase							)
               metrics.update(output.metrics							)
         else:
               __lowerCamelCase     :      List[Any]       						=						output.metrics

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

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

         __lowerCamelCase     :      str       						=						self.callback_handler.on_evaluate(self.args       ,  self.state       ,  self.control       ,  _lowerCamelCase							)
         return metrics


   def   _snake_case    (  self  : Optional[int]       ,  _lowerCamelCase  : Tuple       ,  _lowerCamelCase  : Dict       ,  _lowerCamelCase  : Dict=None       ,  _lowerCamelCase  : str = "test"							):






         '''simple docstring'''



         __lowerCamelCase     :      int       						=						self.get_test_dataloader(_lowerCamelCase							)

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

         __lowerCamelCase     :      Dict       						=						self.post_process_function(_lowerCamelCase       ,  _lowerCamelCase       ,  output.predictions       ,  """predict"""							)
         __lowerCamelCase     :      Dict       						=						self.compute_metrics(_lowerCamelCase							)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()							):
               if not key.startswith(F"""{metric_key_prefix}_"""							):
                     __lowerCamelCase     :      List[Any]       						=						metrics.pop(_lowerCamelCase							)
         metrics.update(output.metrics							)
         return PredictionOutput(predictions=predictions.predictions       ,  label_ids=predictions.label_ids       ,  metrics=_lowerCamelCase							)







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


__UpperCamelCase    :						Any     =							{
    'configuration_timesformer': ['TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP', 'TimesformerConfig'],
}

try:
    if not is_torch_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    __UpperCamelCase    :						int     =							[
        'TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST',
        'TimesformerModel',
        'TimesformerForVideoClassification',
        'TimesformerPreTrainedModel',
    ]

if TYPE_CHECKING:
    from .configuration_timesformer import TIMESFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, TimesformerConfig

    try:
        if not is_torch_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .modeling_timesformer import (
            TIMESFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
            TimesformerForVideoClassification,
            TimesformerModel,
            TimesformerPreTrainedModel,
        )

else:
    import sys

    __UpperCamelCase    :						List[Any]     =							_LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)



