


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        __lowercase       (							_UpperCAmelCase  ):



		"""simple docstring"""

		def __init__( self		,      *A		,      A=None		,      A=None		,      **A	)  -> int:







					'''simple docstring'''







					super().__init__(*A		,      **A	)
					lowerCamelCase    		=  eval_examples
					lowerCamelCase    		=  post_process_function







		def 					__A   ( self		,      A = None		,      A=None		,      A = None		,      A = "eval"		,      **A		,      )  -> Dict[str, float]:







					'''simple docstring'''







					lowerCamelCase    		=  gen_kwargs.copy()
					lowerCamelCase    		=  (
					    gen_kwargs["""max_length"""] if gen_kwargs.get("""max_length"""	) is not None else self.args.generation_max_length
					)
					lowerCamelCase    		=  (
					    gen_kwargs["""num_beams"""] if gen_kwargs.get("""num_beams"""	) is not None else self.args.generation_num_beams
					)
					lowerCamelCase    		=  gen_kwargs

					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    		=  time.time()
					lowerCamelCase    		=  self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
					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		,      A	)
								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 					__A   ( self		,      A		,      A		,      A=None		,      A = "test"		,      **A	)  -> int:







					'''simple docstring'''







					lowerCamelCase    		=  gen_kwargs.copy()

					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    		=  time.time()
					lowerCamelCase    		=  self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
					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		,      A		,      """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 ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE__   =  logging.get_logger(__name__)

SCREAMING_SNAKE_CASE__   =  {
    "facebook/s2t-wav2vec2-large-en-de": (
        "https://huggingface.co/facebook/s2t-wav2vec2-large-en-de/resolve/main/config.json"
    ),
    # See all Speech2Text models at https://huggingface.co/models?filter=speech2text2
}

class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = 'speech_to_text_2'
      _SCREAMING_SNAKE_CASE							      = ['past_key_values']
      _SCREAMING_SNAKE_CASE							      = {'num_attention_heads': 'decoder_attention_heads', 'hidden_size': 'd_model'}





      def __init__(			self ,		lowercase=10_000 ,		lowercase=6 ,		lowercase=2_048 ,		lowercase=4 ,		lowercase=0.0 ,		lowercase=True ,		lowercase="relu" ,		lowercase=256 ,		lowercase=0.1 ,		lowercase=0.0 ,		lowercase=0.0 ,		lowercase=0.02 ,		lowercase=2 ,		lowercase=True ,		lowercase=1 ,		lowercase=0 ,		lowercase=2 ,		lowercase=1_024 ,		**lowercase ,		)						->	Optional[int]:
            lowerCAmelCase    		=			vocab_size
            lowerCAmelCase    		=			d_model
            lowerCAmelCase    		=			decoder_ffn_dim
            lowerCAmelCase    		=			decoder_layers
            lowerCAmelCase    		=			decoder_attention_heads
            lowerCAmelCase    		=			dropout
            lowerCAmelCase    		=			attention_dropout
            lowerCAmelCase    		=			activation_dropout
            lowerCAmelCase    		=			activation_function
            lowerCAmelCase    		=			init_std
            lowerCAmelCase    		=			decoder_layerdrop
            lowerCAmelCase    		=			use_cache
            lowerCAmelCase    		=			decoder_layers
            lowerCAmelCase    		=			scale_embedding  # scale factor will be sqrt(d_model) if True
            lowerCAmelCase    		=			max_target_positions

            super().__init__(
                pad_token_id=lowercase ,		bos_token_id=lowercase ,		eos_token_id=lowercase ,		decoder_start_token_id=lowercase ,		**lowercase ,		)






