






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







 def __init__(		self					:   str							,   *SCREAMING_SNAKE_CASE					:   List[str]							,   SCREAMING_SNAKE_CASE					:   List[str]=None							,   SCREAMING_SNAKE_CASE					:   List[Any]=None							,   **SCREAMING_SNAKE_CASE					:   Any						):


      '''simple docstring'''






      super().__init__(*SCREAMING_SNAKE_CASE							,   **SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Dict       				=  eval_examples
      UpperCamelCase__			:   Dict       				=  post_process_function







 def 					__lowercase					(		self					:   List[Any]							,   SCREAMING_SNAKE_CASE					:   Optional[int]=None							,   SCREAMING_SNAKE_CASE					:   Any=None							,   SCREAMING_SNAKE_CASE					:   List[Any]=None							,   SCREAMING_SNAKE_CASE					:   str = "eval"						):


      '''simple docstring'''






      UpperCamelCase__			:   Optional[Any]       				=  self.eval_dataset if eval_dataset is None else eval_dataset
      UpperCamelCase__			:   List[str]       				=  self.get_eval_dataloader(SCREAMING_SNAKE_CASE						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.eval_examples if eval_examples is None else eval_examples

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

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

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

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

      UpperCamelCase__			:   int       				=  self.callback_handler.on_evaluate(self.args							,   self.state							,   self.control							,   SCREAMING_SNAKE_CASE						)
      return metrics







 def 					__lowercase					(		self					:   Dict							,   SCREAMING_SNAKE_CASE					:   Dict							,   SCREAMING_SNAKE_CASE					:   int							,   SCREAMING_SNAKE_CASE					:   Dict=None							,   SCREAMING_SNAKE_CASE					:   str = "test"						):


      '''simple docstring'''






      UpperCamelCase__			:   int       				=  self.get_test_dataloader(SCREAMING_SNAKE_CASE						)

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

      UpperCamelCase__			:   Any       				=  self.post_process_function(SCREAMING_SNAKE_CASE							,   SCREAMING_SNAKE_CASE							,   output.predictions							,   "predict"						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.compute_metrics(SCREAMING_SNAKE_CASE						)

      # Prefix all keys with metric_key_prefix + '_'
      for key in list(metrics.keys()						):
           if not key.startswith(F'{metric_key_prefix}_'						):
                UpperCamelCase__			:   Optional[int]       				=  metrics.pop(SCREAMING_SNAKE_CASE						)
      metrics.update(output.metrics						)
      return PredictionOutput(predictions=predictions.predictions							,   label_ids=predictions.label_ids							,   metrics=SCREAMING_SNAKE_CASE						)







import unittest

from transformers import load_tool
from transformers.utils import is_torch_available


if is_torch_available():
 import torch

from transformers.testing_utils import require_torch

from .test_tools_common import ToolTesterMixin





@require_torch
class 			__a  (    unittest.TestCase       ,   A__       ):







 def 					__lowercase					(		self					:   Any						):


      '''simple docstring'''






      UpperCamelCase__			:   Dict       				=  load_tool("text-to-speech"						)
      self.tool.setup()







 def 					__lowercase					(		self					:   Union[str, Any]						):


      '''simple docstring'''






      torch.manual_seed(0						)
      UpperCamelCase__			:   Union[str, Any]       				=  self.tool("hey"						)
      UpperCamelCase__			:   Any       				=  result.to_raw()
      self.assertTrue(
          torch.allclose(
              resulting_tensor[:3]							,   torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5]						)							,   )						)







 def 					__lowercase					(		self					:   List[Any]						):


      '''simple docstring'''






      torch.manual_seed(0						)
      UpperCamelCase__			:   List[Any]       				=  self.tool("hey"						)
      UpperCamelCase__			:   int       				=  result.to_raw()
      self.assertTrue(
          torch.allclose(
              resulting_tensor[:3]							,   torch.tensor([-0.0_0_0_5_9_6_6_6_6_8_8_3_2_1_1_5_8_2_9, -0.0_0_0_3_6_5_7_6_4_0_1_9_0_7_9_5_0_6_4, -0.0_0_0_1_3_4_3_9_5_0_2_7_9_9_8_8_3_4_8_5]						)							,   )						)