






"""simple docstring"""

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    lowerCamelCase__						(		__SCREAMING_SNAKE_CASE    ):
      def __init__(		self	:	List[Any]      ,       *A_	:	List[Any]      ,       A_	:	Optional[Any]=None      ,       A_	:	int=None      ,       **A_	:	Tuple							):






       '''simple docstring'''


       super().__init__(*_a      ,       **_a							)
       __lowercase									=	eval_examples
       __lowercase									=	post_process_function
      def SCREAMING_SNAKE_CASE_	(		self	:	Tuple      ,       A_	:	Tuple=None      ,       A_	:	Union[str, Any]=None      ,       A_	:	List[Any]=None      ,       A_	:	str = "eval"							):






       '''simple docstring'''


       __lowercase									=	self.eval_dataset if eval_dataset is None else eval_dataset
       __lowercase									=	self.get_eval_dataloader(_a							)
       __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									=	self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
       __lowercase									=	time.time()
       try:
        __lowercase									=	eval_loop(
            _a      ,       description="""Evaluation"""      ,       prediction_loss_only=True if compute_metrics is None else None      ,       ignore_keys=_a      ,       metric_key_prefix=_a      ,       )
       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(
               _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
        __lowercase									=	self.post_process_function(_a      ,       _a      ,       output.predictions							)
        __lowercase									=	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}_'''							):
          __lowercase									=	metrics.pop(_a							)
        metrics.update(output.metrics							)
       else:
        __lowercase									=	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()							)

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






      def SCREAMING_SNAKE_CASE_	(		self	:	Optional[Any]      ,       A_	:	Dict      ,       A_	:	List[Any]      ,       A_	:	List[Any]=None      ,       A_	:	str = "test"							):






       '''simple docstring'''


       __lowercase									=	self.get_test_dataloader(_a							)

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

       __lowercase									=	self.post_process_function(_a      ,       _a      ,       output.predictions      ,       """predict"""							)
       __lowercase									=	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}_'''							):
         __lowercase									=	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_tf_available, is_torch_available


UpperCAmelCase__  							={
    "configuration_data2vec_audio": ["DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP", "Data2VecAudioConfig"],
    "configuration_data2vec_text": [
        "DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Data2VecTextConfig",
        "Data2VecTextOnnxConfig",
    ],
    "configuration_data2vec_vision": [
        "DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP",
        "Data2VecVisionConfig",
        "Data2VecVisionOnnxConfig",
    ],
}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       UpperCAmelCase__  							=[
           "DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST",
           "Data2VecAudioForAudioFrameClassification",
           "Data2VecAudioForCTC",
           "Data2VecAudioForSequenceClassification",
           "Data2VecAudioForXVector",
           "Data2VecAudioModel",
           "Data2VecAudioPreTrainedModel",
       ]
       UpperCAmelCase__  							=[
           "DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "Data2VecTextForCausalLM",
           "Data2VecTextForMaskedLM",
           "Data2VecTextForMultipleChoice",
           "Data2VecTextForQuestionAnswering",
           "Data2VecTextForSequenceClassification",
           "Data2VecTextForTokenClassification",
           "Data2VecTextModel",
           "Data2VecTextPreTrainedModel",
       ]
       UpperCAmelCase__  							=[
           "DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST",
           "Data2VecVisionForImageClassification",
           "Data2VecVisionForMaskedImageModeling",
           "Data2VecVisionForSemanticSegmentation",
           "Data2VecVisionModel",
           "Data2VecVisionPreTrainedModel",
       ]

if is_tf_available():
       UpperCAmelCase__  							=[
           "TFData2VecVisionForImageClassification",
           "TFData2VecVisionForSemanticSegmentation",
           "TFData2VecVisionModel",
           "TFData2VecVisionPreTrainedModel",
       ]

if TYPE_CHECKING:
       from .configuration_dataavec_audio import DATA2VEC_AUDIO_PRETRAINED_CONFIG_ARCHIVE_MAP, DataaVecAudioConfig
       from .configuration_dataavec_text import (
           DATA2VEC_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP,
           DataaVecTextConfig,
           DataaVecTextOnnxConfig,
       )
       from .configuration_dataavec_vision import (
           DATA2VEC_VISION_PRETRAINED_CONFIG_ARCHIVE_MAP,
           DataaVecVisionConfig,
           DataaVecVisionOnnxConfig,
       )

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_dataavec_audio import (
                  DATA2VEC_AUDIO_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DataaVecAudioForAudioFrameClassification,
                  DataaVecAudioForCTC,
                  DataaVecAudioForSequenceClassification,
                  DataaVecAudioForXVector,
                  DataaVecAudioModel,
                  DataaVecAudioPreTrainedModel,
              )
              from .modeling_dataavec_text import (
                  DATA2VEC_TEXT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DataaVecTextForCausalLM,
                  DataaVecTextForMaskedLM,
                  DataaVecTextForMultipleChoice,
                  DataaVecTextForQuestionAnswering,
                  DataaVecTextForSequenceClassification,
                  DataaVecTextForTokenClassification,
                  DataaVecTextModel,
                  DataaVecTextPreTrainedModel,
              )
              from .modeling_dataavec_vision import (
                  DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST,
                  DataaVecVisionForImageClassification,
                  DataaVecVisionForMaskedImageModeling,
                  DataaVecVisionForSemanticSegmentation,
                  DataaVecVisionModel,
                  DataaVecVisionPreTrainedModel,
              )
       if is_tf_available():
              from .modeling_tf_dataavec_vision import (
                  TFDataaVecVisionForImageClassification,
                  TFDataaVecVisionForSemanticSegmentation,
                  TFDataaVecVisionModel,
                  TFDataaVecVisionPreTrainedModel,
              )

else:
       import sys

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