

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_    (  a__		):


							def __init__( self      ,				*lowerCamelCase_      ,				lowerCamelCase_=None      ,				lowerCamelCase_=None      ,				**lowerCamelCase_						)							->   Union[str, Any]:



								"""simple docstring"""

								super().__init__(*__a      ,				**__a						)
								_UpperCamelCase			=   eval_examples
								_UpperCamelCase			=   post_process_function


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



								"""simple docstring"""

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

								_UpperCamelCase			=   self.eval_dataset if eval_dataset is None else eval_dataset
								_UpperCamelCase			=   self.get_eval_dataloader(__a						)
								_UpperCamelCase			=   self.eval_examples if eval_examples is None else eval_examples

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

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

								_UpperCamelCase			=   self.callback_handler.on_evaluate(self.args      ,				self.state      ,				self.control      ,				__a						)
								return metrics







							def  lowercase   ( self      ,				lowerCamelCase_      ,				lowerCamelCase_      ,				lowerCamelCase_=None      ,				lowerCamelCase_ = "test"      ,				**lowerCamelCase_						)							->   str:



								"""simple docstring"""

								_UpperCamelCase			=   gen_kwargs.copy()

								_UpperCamelCase			=   self.get_test_dataloader(__a						)

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

								_UpperCamelCase			=   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

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








from . import (
    albert,
    align,
    altclip,
    audio_spectrogram_transformer,
    auto,
    autoformer,
    bark,
    bart,
    barthez,
    bartpho,
    beit,
    bert,
    bert_generation,
    bert_japanese,
    bertweet,
    big_bird,
    bigbird_pegasus,
    biogpt,
    bit,
    blenderbot,
    blenderbot_small,
    blip,
    blip_a,
    bloom,
    bridgetower,
    byta,
    camembert,
    canine,
    chinese_clip,
    clap,
    clip,
    clipseg,
    codegen,
    conditional_detr,
    convbert,
    convnext,
    convnextva,
    cpm,
    cpmant,
    ctrl,
    cvt,
    dataavec,
    deberta,
    deberta_va,
    decision_transformer,
    deformable_detr,
    deit,
    deprecated,
    deta,
    detr,
    dialogpt,
    dinat,
    distilbert,
    dit,
    donut,
    dpr,
    dpt,
    efficientformer,
    efficientnet,
    electra,
    encodec,
    encoder_decoder,
    ernie,
    ernie_m,
    esm,
    falcon,
    flaubert,
    flava,
    fnet,
    focalnet,
    fsmt,
    funnel,
    git,
    glpn,
    gpta,
    gpt_bigcode,
    gpt_neo,
    gpt_neox,
    gpt_neox_japanese,
    gpt_swa,
    gptj,
    gptsan_japanese,
    graphormer,
    groupvit,
    herbert,
    hubert,
    ibert,
    imagegpt,
    informer,
    instructblip,
    jukebox,
    layoutlm,
    layoutlmva,
    layoutlmva,
    layoutxlm,
    led,
    levit,
    lilt,
    llama,
    longformer,
    longta,
    luke,
    lxmert,
    mam_aaa,
    marian,
    markuplm,
    maskaformer,
    maskformer,
    mbart,
    mbartaa,
    mega,
    megatron_bert,
    megatron_gpta,
    mgp_str,
    mluke,
    mobilebert,
    mobilenet_va,
    mobilenet_va,
    mobilevit,
    mobilevitva,
    mpnet,
    mra,
    mta,
    musicgen,
    mvp,
    nat,
    nezha,
    nllb,
    nllb_moe,
    nystromformer,
    oneformer,
    open_llama,
    openai,
    opt,
    owlvit,
    pegasus,
    pegasus_x,
    perceiver,
    phobert,
    pixastruct,
    plbart,
    poolformer,
    prophetnet,
    qdqbert,
    rag,
    realm,
    reformer,
    regnet,
    rembert,
    resnet,
    roberta,
    roberta_prelayernorm,
    roc_bert,
    roformer,
    rwkv,
    sam,
    segformer,
    sew,
    sew_d,
    speech_encoder_decoder,
    speech_to_text,
    speech_to_text_a,
    speechta,
    splinter,
    squeezebert,
    swiftformer,
    swin,
    swinasr,
    swinva,
    switch_transformers,
    ta,
    table_transformer,
    tapas,
    time_series_transformer,
    timesformer,
    timm_backbone,
    transfo_xl,
    trocr,
    tvlt,
    umta,
    unispeech,
    unispeech_sat,
    upernet,
    videomae,
    vilt,
    vision_encoder_decoder,
    vision_text_dual_encoder,
    visual_bert,
    vit,
    vit_hybrid,
    vit_mae,
    vit_msn,
    vivit,
    wavaveca,
    wavaveca_conformer,
    wavaveca_phoneme,
    wavaveca_with_lm,
    wavlm,
    whisper,
    x_clip,
    xglm,
    xlm,
    xlm_prophetnet,
    xlm_roberta,
    xlm_roberta_xl,
    xlnet,
    xmod,
    yolos,
    yoso,
)





