



"""simple docstring"""






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 			__snake_case			(      _lowercase):




  def __init__(					self	:		Any		,   *__lowerCAmelCase	:		Any		,   __lowerCAmelCase	:		str=None		,   __lowerCAmelCase	:		Tuple=None		,   **__lowerCAmelCase	:		Any						):






         """simple docstring"""






         super().__init__(*__lowerCAmelCase		,   **__lowerCAmelCase						)
         _lowerCamelCase		:							Union[str, Any]            =					eval_examples
         _lowerCamelCase		:							Optional[int]            =					post_process_function




  def 		SCREAMING_SNAKE_CASE							(					self	:		Union[str, Any]		,   __lowerCAmelCase	:		str=None		,   __lowerCAmelCase	:		List[Any]=None		,   __lowerCAmelCase	:		Union[str, Any]=None		,   __lowerCAmelCase	:		str = "eval"						):






         """simple docstring"""






         _lowerCamelCase		:							Union[str, Any]            =					self.eval_dataset if eval_dataset is None else eval_dataset
         _lowerCamelCase		:							Any            =					self.get_eval_dataloader(__lowerCAmelCase						)
         _lowerCamelCase		:							Optional[int]            =					self.eval_examples if eval_examples is None else eval_examples

         # Temporarily disable metric computation, we will do it in the loop here.
         _lowerCamelCase		:							Dict            =					self.compute_metrics
         _lowerCamelCase		:							str            =					None
         _lowerCamelCase		:							Union[str, Any]            =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         _lowerCamelCase		:							List[Any]            =					time.time()
         try:
                _lowerCamelCase		:							Union[str, Any]            =					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		:							int            =					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		:							Optional[Any]            =					self.post_process_function(__lowerCAmelCase		,   __lowerCAmelCase		,   output.predictions						)
                _lowerCamelCase		:							Optional[int]            =					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		:							Tuple            =					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 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		Optional[Any]		,   __lowerCAmelCase	:		Dict		,   __lowerCAmelCase	:		Any=None		,   __lowerCAmelCase	:		str = "test"						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					self.get_test_dataloader(__lowerCAmelCase						)

         # Temporarily disable metric computation, we will do it in the loop here.
         _lowerCamelCase		:							List[str]            =					self.compute_metrics
         _lowerCamelCase		:							List[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		:							str            =					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		:							str            =					compute_metrics
         _lowerCamelCase		:							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 None or self.compute_metrics is None:
                return output

         _lowerCamelCase		:							str            =					self.post_process_function(__lowerCAmelCase		,   __lowerCAmelCase		,   output.predictions		,   '''predict'''						)
         _lowerCamelCase		:							Tuple            =					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						)
         return PredictionOutput(predictions=predictions.predictions		,   label_ids=predictions.label_ids		,   metrics=__lowerCAmelCase						)





'''simple docstring'''




from ..utils import DummyObject, requires_backends



class 							lowercase_						(		metaclass=a			):

  '''simple docstring'''





  __lowerCAmelCase       :			str     	= ["keras_nlp"]
  def __init__(  self ,			*a_ ,			**a_  )				->					Union[str, Any]:


    """simple docstring"""
    requires_backends(self ,			['keras_nlp']  )


