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__			):






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





         """simple docstring"""



         super().__init__(*a  ,    **a   )
         __lowerCamelCase          =  eval_examples
         __lowerCamelCase          =  post_process_function






     def        SCREAMING_SNAKE_CASE__       (			self		:		str  ,    a		:		int=None  ,    a		:		Union[str, Any]=None  ,    a		:		Any=None  ,    a		:		str = "eval"   ):





         """simple docstring"""



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

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

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






     def        SCREAMING_SNAKE_CASE__       (			self		:		Tuple  ,    a		:		List[str]  ,    a		:		List[str]  ,    a		:		List[str]=None  ,    a		:		str = "test"   ):





         """simple docstring"""



         __lowerCamelCase          =  self.get_test_dataloader(a   )

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

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

'''simple docstring'''




from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__UpperCAmelCase												={
    "configuration_whisper": ["WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP", "WhisperConfig", "WhisperOnnxConfig"],
    "feature_extraction_whisper": ["WhisperFeatureExtractor"],
    "processing_whisper": ["WhisperProcessor"],
    "tokenization_whisper": ["WhisperTokenizer"],
}

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase												=["WhisperTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase												=[
      "WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST",
      "WhisperForConditionalGeneration",
      "WhisperModel",
      "WhisperPreTrainedModel",
      "WhisperForAudioClassification",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase												=[
      "TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFWhisperForConditionalGeneration",
      "TFWhisperModel",
      "TFWhisperPreTrainedModel",
  ]

try:
  if not is_flax_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __UpperCAmelCase												=[
      "FlaxWhisperForConditionalGeneration",
      "FlaxWhisperModel",
      "FlaxWhisperPreTrainedModel",
      "FlaxWhisperForAudioClassification",
  ]


if TYPE_CHECKING:
  from .configuration_whisper import WHISPER_PRETRAINED_CONFIG_ARCHIVE_MAP, WhisperConfig, WhisperOnnxConfig
  from .feature_extraction_whisper import WhisperFeatureExtractor
  from .processing_whisper import WhisperProcessor
  from .tokenization_whisper import WhisperTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_whisper_fast import WhisperTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_whisper import (
        WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
        WhisperForAudioClassification,
        WhisperForConditionalGeneration,
        WhisperModel,
        WhisperPreTrainedModel,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_whisper import (
        TF_WHISPER_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFWhisperForConditionalGeneration,
        TFWhisperModel,
        TFWhisperPreTrainedModel,
    )

  try:
    if not is_flax_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_flax_whisper import (
        FlaxWhisperForAudioClassification,
        FlaxWhisperForConditionalGeneration,
        FlaxWhisperModel,
        FlaxWhisperPreTrainedModel,
    )

else:
  import sys

  __UpperCAmelCase												=_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)
