



"""simple docstring"""






import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, 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    (	__SCREAMING_SNAKE_CASE  ):



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



     """simple docstring"""
     super().__init__(*__lowerCamelCase							,       **__lowerCamelCase				)
     _snake_case   	=      eval_examples
     _snake_case   	=      post_process_function



   def    __UpperCAmelCase    (       self    :	int							,       __lowerCamelCase    :	Optional[Dataset] = None							,       __lowerCamelCase    :	List[Any]=None							,       __lowerCamelCase    :	Optional[List[str]] = None							,       __lowerCamelCase    :	str = "eval"							,       **__lowerCamelCase    :	int							,       ):



     """simple docstring"""
     _snake_case   	=      gen_kwargs.copy()
     _snake_case   	=      (
         gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length'''				) is not None else self.args.generation_max_length
     )
     _snake_case   	=      (
         gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams'''				) is not None else self.args.generation_num_beams
     )
     _snake_case   	=      gen_kwargs

     _snake_case   	=      self.eval_dataset if eval_dataset is None else eval_dataset
     _snake_case   	=      self.get_eval_dataloader(__lowerCamelCase				)
     _snake_case   	=      self.eval_examples if eval_examples is None else eval_examples

     # Temporarily disable metric computation, we will do it in the loop here.
     _snake_case   	=      self.compute_metrics
     _snake_case   	=      None
     _snake_case   	=      time.time()
     _snake_case   	=      self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
       _snake_case   	=      eval_loop(
           __lowerCamelCase							,       description='''Evaluation'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCamelCase							,       metric_key_prefix=__lowerCamelCase							,       )
     finally:
       _snake_case   	=      compute_metrics
     _snake_case   	=      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
       _snake_case   	=      self.post_process_function(__lowerCamelCase							,       __lowerCamelCase							,       __lowerCamelCase				)
       _snake_case   	=      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}_"""				):
           _snake_case   	=      metrics.pop(__lowerCamelCase				)

       metrics.update(output.metrics				)
     else:
       _snake_case   	=      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()				)

     _snake_case   	=      self.callback_handler.on_evaluate(self.args							,       self.state							,       self.control							,       __lowerCamelCase				)
     return metrics





   def    __UpperCAmelCase    (       self    :	Union[str, Any]							,       __lowerCamelCase    :	List[Any]							,       __lowerCamelCase    :	Dict							,       __lowerCamelCase    :	Dict=None							,       __lowerCamelCase    :	str = "test"							,       **__lowerCamelCase    :	int				):



     """simple docstring"""
     _snake_case   	=      gen_kwargs.copy()

     _snake_case   	=      self.get_test_dataloader(__lowerCamelCase				)

     # Temporarily disable metric computation, we will do it in the loop here.
     _snake_case   	=      self.compute_metrics
     _snake_case   	=      None
     _snake_case   	=      time.time()
     _snake_case   	=      self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
     try:
       _snake_case   	=      eval_loop(
           __lowerCamelCase							,       description='''Prediction'''							,       prediction_loss_only=True if compute_metrics is None else None							,       ignore_keys=__lowerCamelCase							,       metric_key_prefix=__lowerCamelCase							,       )
     finally:
       _snake_case   	=      compute_metrics

     _snake_case   	=      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

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










"""simple docstring"""






from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available


snake_case   					=				{'''tokenization_herbert''': ['''HerbertTokenizer''']}

try:
     if not is_tokenizers_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     snake_case   					=				['''HerbertTokenizerFast''']


if TYPE_CHECKING:
     from .tokenization_herbert import HerbertTokenizer

     try:
          if not is_tokenizers_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_herbert_fast import HerbertTokenizerFast

else:
     import sys

     snake_case   					=				_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)





