

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  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 def __init__(      self							:      Optional[Any], *UpperCAmelCase__							:      List[str], UpperCAmelCase__							:      Dict=None, UpperCAmelCase__							:      Optional[Any]=None, **UpperCAmelCase__							:      int     ):
    super().__init__(*UpperCAmelCase__, **UpperCAmelCase__     )
    __lowercase								=						eval_examples
    __lowercase								=						post_process_function


 def  _lowercase			(      self							:      List[str], UpperCAmelCase__							:      Optional[Dataset] = None, UpperCAmelCase__							:      str=None, UpperCAmelCase__							:      Optional[List[str]] = None, UpperCAmelCase__							:      str = "eval", **UpperCAmelCase__							:      List[Any], ):
    __lowercase								=						gen_kwargs.copy()
    __lowercase								=						(
        gen_kwargs["max_length"] if gen_kwargs.get("max_length"     ) is not None else self.args.generation_max_length
    )
    __lowercase								=						(
        gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"     ) is not None else self.args.generation_num_beams
    )
    __lowercase								=						gen_kwargs

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

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

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()     ):
          if not key.startswith(F"""{metric_key_prefix}_"""     ):
             __lowercase								=						metrics.pop(UpperCAmelCase__     )

       metrics.update(output.metrics     )
    else:
       __lowercase								=						output.metrics

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

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

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



 def  _lowercase			(      self							:      str, UpperCAmelCase__							:      int, UpperCAmelCase__							:      Tuple, UpperCAmelCase__							:      Optional[int]=None, UpperCAmelCase__							:      str = "test", **UpperCAmelCase__							:      str     ):
    __lowercase								=						gen_kwargs.copy()

    __lowercase								=						self.get_test_dataloader(UpperCAmelCase__     )

    # Temporarily disable metric computation, we will do it in the loop here.
    __lowercase								=						self.compute_metrics
    __lowercase								=						None
    __lowercase								=						time.time()
    __lowercase								=						self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
    try:
       __lowercase								=						eval_loop(
           UpperCAmelCase__, description="Prediction", prediction_loss_only=True if compute_metrics is None else None, ignore_keys=UpperCAmelCase__, metric_key_prefix=UpperCAmelCase__, )
    finally:
       __lowercase								=						compute_metrics

    __lowercase								=						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(
            UpperCAmelCase__, UpperCAmelCase__, 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

    __lowercase								=						self.post_process_function(UpperCAmelCase__, UpperCAmelCase__, UpperCAmelCase__, "predict"     )
    __lowercase								=						self.compute_metrics(UpperCAmelCase__     )

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







"""simple docstring"""





from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a							=							logging.get_logger(__name__)

_a							=							{
    'MIT/ast-finetuned-audioset-10-10-0.4593': (
        'https://huggingface.co/MIT/ast-finetuned-audioset-10-10-0.4593/resolve/main/config.json'
    ),
}




class  _lowerCAmelCase  (     lowercase       ):







 """simple docstring"""







 __UpperCAmelCase     : Any       		=     "audio-spectrogram-transformer"


 def __init__(      self							:      Optional[Any], UpperCAmelCase__							:      Dict=7_6_8, UpperCAmelCase__							:      Optional[Any]=1_2, UpperCAmelCase__							:      Optional[int]=1_2, UpperCAmelCase__							:      List[str]=3_0_7_2, UpperCAmelCase__							:      List[str]="gelu", UpperCAmelCase__							:      Dict=0.0, UpperCAmelCase__							:      str=0.0, UpperCAmelCase__							:      Any=0.02, UpperCAmelCase__							:      Any=1E-12, UpperCAmelCase__							:      Dict=1_6, UpperCAmelCase__							:      Optional[int]=True, UpperCAmelCase__							:      Union[str, Any]=1_0, UpperCAmelCase__							:      List[Any]=1_0, UpperCAmelCase__							:      Optional[int]=1_0_2_4, UpperCAmelCase__							:      List[str]=1_2_8, **UpperCAmelCase__							:      List[Any], ):
    super().__init__(**UpperCAmelCase__     )

    __lowercase								=						hidden_size
    __lowercase								=						num_hidden_layers
    __lowercase								=						num_attention_heads
    __lowercase								=						intermediate_size
    __lowercase								=						hidden_act
    __lowercase								=						hidden_dropout_prob
    __lowercase								=						attention_probs_dropout_prob
    __lowercase								=						initializer_range
    __lowercase								=						layer_norm_eps
    __lowercase								=						patch_size
    __lowercase								=						qkv_bias
    __lowercase								=						frequency_stride
    __lowercase								=						time_stride
    __lowercase								=						max_length
    __lowercase								=						num_mel_bins




