



from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_torch_available,
)


a_      :      List[Any]    =     {
    'configuration_speecht5': [
        'SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP',
        'SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP',
        'SpeechT5Config',
        'SpeechT5HifiGanConfig',
    ],
    'feature_extraction_speecht5': ['SpeechT5FeatureExtractor'],
    'processing_speecht5': ['SpeechT5Processor'],
}

try:
     if not is_sentencepiece_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_      :      Tuple    =     ['SpeechT5Tokenizer']

try:
     if not is_torch_available():
          raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
     pass
else:
     a_      :      Union[str, Any]    =     [
         'SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST',
         'SpeechT5ForSpeechToText',
         'SpeechT5ForSpeechToSpeech',
         'SpeechT5ForTextToSpeech',
         'SpeechT5Model',
         'SpeechT5PreTrainedModel',
         'SpeechT5HifiGan',
     ]

if TYPE_CHECKING:
     from .configuration_speechta import (
         SPEECHT5_PRETRAINED_CONFIG_ARCHIVE_MAP,
         SPEECHT5_PRETRAINED_HIFIGAN_CONFIG_ARCHIVE_MAP,
         SpeechTaConfig,
         SpeechTaHifiGanConfig,
     )
     from .feature_extraction_speechta import SpeechTaFeatureExtractor
     from .processing_speechta import SpeechTaProcessor

     try:
          if not is_sentencepiece_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .tokenization_speechta import SpeechTaTokenizer

     try:
          if not is_torch_available():
               raise OptionalDependencyNotAvailable()
     except OptionalDependencyNotAvailable:
          pass
     else:
          from .modeling_speechta import (
              SPEECHT5_PRETRAINED_MODEL_ARCHIVE_LIST,
              SpeechTaForSpeechToSpeech,
              SpeechTaForSpeechToText,
              SpeechTaForTextToSpeech,
              SpeechTaHifiGan,
              SpeechTaModel,
              SpeechTaPreTrainedModel,
          )

else:
     import sys

     a_      :      List[str]    =     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)










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

 """simple docstring"""



 def __init__(	self		,				*SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE=None		,				**SCREAMING_SNAKE_CASE      ) ->			Tuple:
  super().__init__(*SCREAMING_SNAKE_CASE		,				**SCREAMING_SNAKE_CASE      )
  a__												= eval_examples
  a__												= post_process_function

 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE = None		,				SCREAMING_SNAKE_CASE = "eval"		,				**SCREAMING_SNAKE_CASE		,				) ->			Dict[str, float]:
  a__												= gen_kwargs.copy()
  a__												= (
      gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length'''      ) is not None else self.args.generation_max_length
  )
  a__												= (
      gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams'''      ) is not None else self.args.generation_num_beams
  )
  a__												= gen_kwargs

  a__												= self.eval_dataset if eval_dataset is None else eval_dataset
  a__												= self.get_eval_dataloader(SCREAMING_SNAKE_CASE      )
  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__												= time.time()
  a__												= self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
  try:
   a__												= 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:
   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(
          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
   a__												= self.post_process_function(SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE      )
   a__												= 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}_"      ):
     a__												= metrics.pop(SCREAMING_SNAKE_CASE      )

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

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



 def 						_UpperCAmelCase       (	self		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE		,				SCREAMING_SNAKE_CASE=None		,				SCREAMING_SNAKE_CASE = "test"		,				**SCREAMING_SNAKE_CASE      ) ->			List[str]:
  a__												= gen_kwargs.copy()

  a__												= self.get_test_dataloader(SCREAMING_SNAKE_CASE      )

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

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





