



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



 '''simple docstring'''







 def __init__(self				,     *A				,     A=None				,     A=None				,     **A )					->  List[str]:


      """simple docstring"""



      super().__init__(*_a				,     **_a )
      _a											=						eval_examples
      _a											=						post_process_function



 def       a__						(self				,     A=None				,     A=None				,     A=None				,     A = "eval" )					->  Optional[int]:


      """simple docstring"""



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

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

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys() ):
                if not key.startswith(f'''{metric_key_prefix}_''' ):
                     _a											=						metrics.pop(_a )
           metrics.update(output.metrics )
      else:
           _a											=						output.metrics

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

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

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



 def       a__						(self				,     A				,     A				,     A=None				,     A = "test" )					->  List[Any]:


      """simple docstring"""



      _a											=						self.get_test_dataloader(_a )

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

      _a											=						self.post_process_function(_a				,     _a				,     output.predictions				,     '''predict''' )
      _a											=						self.compute_metrics(_a )

      # Prefix all keys with metric_key_prefix + '_'
      for key in list(metrics.keys() ):
           if not key.startswith(f'''{metric_key_prefix}_''' ):
                _a											=						metrics.pop(_a )
      metrics.update(output.metrics )
      return PredictionOutput(predictions=predictions.predictions				,     label_ids=predictions.label_ids				,     metrics=_a )








'''simple docstring'''





import pytest

import datasets


# Import fixture modules as plugins
lowercase_	 =						["tests.fixtures.files", "tests.fixtures.hub", "tests.fixtures.fsspec"]




def lowerCAmelCase						(__A						,			__A):



     """simple docstring"""



     for item in items:
          if any(marker in item.keywords for marker in ['''integration''', '''unit''']):
               continue
          item.add_marker(pytest.mark.unit)




def lowerCAmelCase						(__A):



     """simple docstring"""



     config.addinivalue_line('''markers'''						,			'''torchaudio_latest: mark test to run with torchaudio>=0.12''')




@pytest.fixture(autouse=__A)
def lowerCAmelCase						(__A						,			__A):



     """simple docstring"""



     _a											=						tmp_path_factory.getbasetemp() / '''cache'''
     _a											=						test_hf_cache_home / '''datasets'''
     _a											=						test_hf_cache_home / '''metrics'''
     _a											=						test_hf_cache_home / '''modules'''
     monkeypatch.setattr('''datasets.config.HF_DATASETS_CACHE'''						,			str(__A))
     monkeypatch.setattr('''datasets.config.HF_METRICS_CACHE'''						,			str(__A))
     monkeypatch.setattr('''datasets.config.HF_MODULES_CACHE'''						,			str(__A))
     _a											=						test_hf_datasets_cache / '''downloads'''
     monkeypatch.setattr('''datasets.config.DOWNLOADED_DATASETS_PATH'''						,			str(__A))
     _a											=						test_hf_datasets_cache / '''downloads''' / '''extracted'''
     monkeypatch.setattr('''datasets.config.EXTRACTED_DATASETS_PATH'''						,			str(__A))




@pytest.fixture(autouse=__A						,			scope='''session''')
def lowerCAmelCase						():



     """simple docstring"""



     datasets.disable_progress_bar()




@pytest.fixture(autouse=__A)
def lowerCAmelCase						(__A):



     """simple docstring"""



     monkeypatch.setattr('''datasets.config.HF_UPDATE_DOWNLOAD_COUNTS'''						,			__A)




@pytest.fixture
def lowerCAmelCase						(__A):



     """simple docstring"""



     monkeypatch.setattr('''sqlalchemy.util.deprecations.SILENCE_UBER_WARNING'''						,			__A)



