

'''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        a__  (							lowerCamelCase_  ):


   def __init__(			self      ,    *_UpperCamelCase      ,    _UpperCamelCase=None      ,    _UpperCamelCase=None      ,    **_UpperCamelCase  ):





      """simple docstring"""





      super().__init__(*_UpperCamelCase      ,    **_UpperCamelCase  )
      _lowercase      :				str      					=       eval_examples
      _lowercase      :				List[str]      					=       post_process_function


   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase=None      ,    _UpperCamelCase=None      ,    _UpperCamelCase=None      ,    _UpperCamelCase = "eval"  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       self.eval_dataset if eval_dataset is None else eval_dataset
      _lowercase      :				str      					=       self.get_eval_dataloader(_UpperCamelCase  )
      _lowercase      :				int      					=       self.eval_examples if eval_examples is None else eval_examples

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

      _lowercase      :				Union[str, Any]      					=       self.callback_handler.on_evaluate(self.args      ,    self.state      ,    self.control      ,    _UpperCamelCase  )
      return metrics






   def 						_lowerCamelCase       (			self      ,    _UpperCamelCase      ,    _UpperCamelCase      ,    _UpperCamelCase=None      ,    _UpperCamelCase = "test"  ):





      """simple docstring"""





      _lowercase      :				Optional[Any]      					=       self.get_test_dataloader(_UpperCamelCase  )

      # Temporarily disable metric computation, we will do it in the loop here.
      _lowercase      :				Tuple      					=       self.compute_metrics
      _lowercase      :				Optional[Any]      					=       None
      _lowercase      :				Dict      					=       self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      _lowercase      :				Optional[Any]      					=       time.time()
      try:
         _lowercase      :				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:
         _lowercase      :				str      					=       compute_metrics
      _lowercase      :				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

      _lowercase      :				Optional[Any]      					=       self.post_process_function(_UpperCamelCase      ,    _UpperCamelCase      ,    output.predictions      ,    "predict"  )
      _lowercase      :				str      					=       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      :				List[str]      					=       metrics.pop(_UpperCamelCase  )
      metrics.update(output.metrics  )
      return PredictionOutput(predictions=predictions.predictions      ,    label_ids=predictions.label_ids      ,    metrics=_UpperCamelCase  )









'''simple docstring'''
import logging

from transformers import PretrainedConfig


_snake_case         =  logging.getLogger(__name__)


_snake_case         =  {
    'bertabs-finetuned-cnndm': 'https://huggingface.co/remi/bertabs-finetuned-cnndm-extractive-abstractive-summarization/resolve/main/config.json',
}


class        a__  (							lowerCamelCase_  ):
   _SCREAMING_SNAKE_CASE   :					List[str]     	=							'bertabs'


   def __init__(			self      ,    _UpperCamelCase=30522      ,    _UpperCamelCase=512      ,    _UpperCamelCase=6      ,    _UpperCamelCase=512      ,    _UpperCamelCase=8      ,    _UpperCamelCase=512      ,    _UpperCamelCase=0.2      ,    _UpperCamelCase=6      ,    _UpperCamelCase=768      ,    _UpperCamelCase=8      ,    _UpperCamelCase=2048      ,    _UpperCamelCase=0.2      ,    **_UpperCamelCase      ,    ):





      """simple docstring"""





      super().__init__(**_UpperCamelCase  )

      _lowercase      :				List[str]      					=       vocab_size
      _lowercase      :				Tuple      					=       max_pos

      _lowercase      :				int      					=       enc_layers
      _lowercase      :				str      					=       enc_hidden_size
      _lowercase      :				Optional[Any]      					=       enc_heads
      _lowercase      :				Union[str, Any]      					=       enc_ff_size
      _lowercase      :				Tuple      					=       enc_dropout

      _lowercase      :				Dict      					=       dec_layers
      _lowercase      :				List[str]      					=       dec_hidden_size
      _lowercase      :				List[str]      					=       dec_heads
      _lowercase      :				str      					=       dec_ff_size
      _lowercase      :				str      					=       dec_dropout






