


"""simple docstring"""







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       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       def __init__(						self		,     *snake_case__		,     snake_case__=None		,     snake_case__=None		,     **snake_case__						):






         """simple docstring"""





         super().__init__(*snake_case__		,     **snake_case__						)
         lowerCAmelCase     :     Tuple								=							eval_examples
         lowerCAmelCase     :     int								=							post_process_function

       def        lowercase__							(						self		,     snake_case__ = None		,     snake_case__=None		,     snake_case__ = None		,     snake_case__ = "eval"		,     **snake_case__		,     ):






         """simple docstring"""





         lowerCAmelCase     :     List[Any]								=							gen_kwargs.copy()
         lowerCAmelCase     :     Dict								=							(
             gen_kwargs["max_length"] if gen_kwargs.get("max_length"						) is not None else self.args.generation_max_length
         )
         lowerCAmelCase     :     str								=							(
             gen_kwargs["num_beams"] if gen_kwargs.get("num_beams"						) is not None else self.args.generation_num_beams
         )
         lowerCAmelCase     :     List[str]								=							gen_kwargs

         lowerCAmelCase     :     Optional[Any]								=							self.eval_dataset if eval_dataset is None else eval_dataset
         lowerCAmelCase     :     Dict								=							self.get_eval_dataloader(snake_case__						)
         lowerCAmelCase     :     Union[str, Any]								=							self.eval_examples if eval_examples is None else eval_examples

         # Temporarily disable metric computation, we will do it in the loop here.
         lowerCAmelCase     :     Any								=							self.compute_metrics
         lowerCAmelCase     :     Dict								=							None
         lowerCAmelCase     :     Optional[int]								=							time.time()
         lowerCAmelCase     :     Tuple								=							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
           lowerCAmelCase     :     List[Any]								=							eval_loop(
               snake_case__		,     description="Evaluation"		,     prediction_loss_only=True if compute_metrics is None else None		,     ignore_keys=snake_case__		,     metric_key_prefix=snake_case__		,     )
         finally:
           lowerCAmelCase     :     Dict								=							compute_metrics
         lowerCAmelCase     :     List[str]								=							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(
                 snake_case__		,     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
           lowerCAmelCase     :     Optional[int]								=							self.post_process_function(snake_case__		,     snake_case__		,     snake_case__						)
           lowerCAmelCase     :     Union[str, Any]								=							self.compute_metrics(snake_case__						)

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()						):
             if not key.startswith(f"""{metric_key_prefix}_"""						):
               lowerCAmelCase     :     str								=							metrics.pop(snake_case__						)

           metrics.update(output.metrics						)
         else:
           lowerCAmelCase     :     int								=							output.metrics

         if self.args.should_log:
           # Only the main node log the results by default
           self.log(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()						)

         lowerCAmelCase     :     Dict								=							self.callback_handler.on_evaluate(self.args		,     self.state		,     self.control		,     snake_case__						)
         return metrics







       def        lowercase__							(						self		,     snake_case__		,     snake_case__		,     snake_case__=None		,     snake_case__ = "test"		,     **snake_case__						):






         """simple docstring"""





         lowerCAmelCase     :     Union[str, Any]								=							gen_kwargs.copy()

         lowerCAmelCase     :     Tuple								=							self.get_test_dataloader(snake_case__						)

         # Temporarily disable metric computation, we will do it in the loop here.
         lowerCAmelCase     :     List[Any]								=							self.compute_metrics
         lowerCAmelCase     :     Optional[int]								=							None
         lowerCAmelCase     :     Any								=							time.time()
         lowerCAmelCase     :     int								=							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
         try:
           lowerCAmelCase     :     str								=							eval_loop(
               snake_case__		,     description="Prediction"		,     prediction_loss_only=True if compute_metrics is None else None		,     ignore_keys=snake_case__		,     metric_key_prefix=snake_case__		,     )
         finally:
           lowerCAmelCase     :     Optional[Any]								=							compute_metrics

         lowerCAmelCase     :     int								=							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(
                 snake_case__		,     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

         lowerCAmelCase     :     Any								=							self.post_process_function(snake_case__		,     snake_case__		,     snake_case__		,     "predict"						)
         lowerCAmelCase     :     Union[str, Any]								=							self.compute_metrics(snake_case__						)

         # Prefix all keys with metric_key_prefix + '_'
         for key in list(metrics.keys()						):
           if not key.startswith(f"""{metric_key_prefix}_"""						):
             lowerCAmelCase     :     Optional[int]								=							metrics.pop(snake_case__						)
         metrics.update(output.metrics						)
         return PredictionOutput(predictions=predictions.predictions		,     label_ids=predictions.label_ids		,     metrics=snake_case__						)









"""simple docstring"""







from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCAmelCase__      				=   logging.get_logger(__name__)

lowerCAmelCase__      				=   {
    '''facebook/data2vec-vision-base-ft''': (
        '''https://huggingface.co/facebook/data2vec-vision-base-ft/resolve/main/config.json'''
    ),
}


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				List[Any]   ="data2vec-vision"

       def __init__(						self		,     snake_case__=768		,     snake_case__=12		,     snake_case__=12		,     snake_case__=3_072		,     snake_case__="gelu"		,     snake_case__=0.0		,     snake_case__=0.0		,     snake_case__=0.02		,     snake_case__=1e-12		,     snake_case__=224		,     snake_case__=16		,     snake_case__=3		,     snake_case__=False		,     snake_case__=False		,     snake_case__=False		,     snake_case__=False		,     snake_case__=0.1		,     snake_case__=0.1		,     snake_case__=True		,     snake_case__=[3, 5, 7, 11]		,     snake_case__=[1, 2, 3, 6]		,     snake_case__=True		,     snake_case__=0.4		,     snake_case__=256		,     snake_case__=1		,     snake_case__=False		,     snake_case__=255		,     **snake_case__		,     ):






         """simple docstring"""





         super().__init__(**snake_case__						)

         lowerCAmelCase     :     Tuple								=							hidden_size
         lowerCAmelCase     :     List[Any]								=							num_hidden_layers
         lowerCAmelCase     :     Tuple								=							num_attention_heads
         lowerCAmelCase     :     Optional[int]								=							intermediate_size
         lowerCAmelCase     :     Optional[int]								=							hidden_act
         lowerCAmelCase     :     Dict								=							hidden_dropout_prob
         lowerCAmelCase     :     Optional[Any]								=							attention_probs_dropout_prob
         lowerCAmelCase     :     int								=							initializer_range
         lowerCAmelCase     :     Dict								=							layer_norm_eps

         lowerCAmelCase     :     Optional[int]								=							image_size
         lowerCAmelCase     :     Optional[Any]								=							patch_size
         lowerCAmelCase     :     Optional[Any]								=							num_channels
         lowerCAmelCase     :     Union[str, Any]								=							use_mask_token
         lowerCAmelCase     :     str								=							use_absolute_position_embeddings
         lowerCAmelCase     :     Any								=							use_relative_position_bias
         lowerCAmelCase     :     List[str]								=							use_shared_relative_position_bias
         lowerCAmelCase     :     str								=							layer_scale_init_value
         lowerCAmelCase     :     Union[str, Any]								=							drop_path_rate
         lowerCAmelCase     :     Any								=							use_mean_pooling
         # decode head attributes (semantic segmentation)
         lowerCAmelCase     :     Optional[int]								=							out_indices
         lowerCAmelCase     :     Union[str, Any]								=							pool_scales
         # auxiliary head attributes (semantic segmentation)
         lowerCAmelCase     :     str								=							use_auxiliary_head
         lowerCAmelCase     :     int								=							auxiliary_loss_weight
         lowerCAmelCase     :     Tuple								=							auxiliary_channels
         lowerCAmelCase     :     List[str]								=							auxiliary_num_convs
         lowerCAmelCase     :     Tuple								=							auxiliary_concat_input
         lowerCAmelCase     :     List[str]								=							semantic_loss_ignore_index


class       SCREAMING_SNAKE_CASE__				(		lowercase				):
       """simple docstring"""


       a  :				Union[str, Any]   =version.parse("1.11"				)

       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return OrderedDict(
             [
                 ("pixel_values", {0: "batch", 1: "num_channels", 2: "height", 3: "width"}),
             ]						)







       @property
       def        lowercase__							(						self						):






         """simple docstring"""





         return 1e-4





