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   snake_case_       (		__A       ):






   def __init__(      self				:     Any	,						*lowercase_				:     int	,						lowercase_				:     Tuple=None	,						lowercase_				:     Union[str, Any]=None	,						**lowercase_				:     int  )					->						List[str]:
       super().__init__(*lowercase_	,						**lowercase_  )
       lowercase__ :   Optional[int]    					=				eval_examples
       lowercase__ :   Optional[Any]    					=				post_process_function






   def       __UpperCamelCase     (      self				:     List[str]	,						lowercase_				:     Union[str, Any]=None	,						lowercase_				:     Union[str, Any]=None	,						lowercase_				:     List[str]=None	,						lowercase_				:     str = "eval"  )					->						int:
       lowercase__ :   List[str]    					=				self.eval_dataset if eval_dataset is None else eval_dataset
       lowercase__ :   Optional[int]    					=				self.get_eval_dataloader(lowercase_  )
       lowercase__ :   Optional[int]    					=				self.eval_examples if eval_examples is None else eval_examples

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

           # Prefix all keys with metric_key_prefix + '_'
           for key in list(metrics.keys()  ):
               if not key.startswith(F'''{metric_key_prefix}_'''  ):
                   lowercase__ :   Dict    					=				metrics.pop(lowercase_  )
           metrics.update(output.metrics  )
       else:
           lowercase__ :   str    					=				output.metrics

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

       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__ :   int    					=				self.callback_handler.on_evaluate(self.args	,						self.state	,						self.control	,						lowercase_  )
       return metrics







   def       __UpperCamelCase     (      self				:     List[Any]	,						lowercase_				:     int	,						lowercase_				:     Dict	,						lowercase_				:     Optional[int]=None	,						lowercase_				:     str = "test"  )					->						Dict:
       lowercase__ :   str    					=				self.get_test_dataloader(lowercase_  )

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

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






from collections import OrderedDict
from typing import Mapping

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


snake_case_			  = logging.get_logger(__name__)

snake_case_			  = {
    '''distilbert-base-uncased''': '''https://huggingface.co/distilbert-base-uncased/resolve/main/config.json''',
    '''distilbert-base-uncased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-uncased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-cased''': '''https://huggingface.co/distilbert-base-cased/resolve/main/config.json''',
    '''distilbert-base-cased-distilled-squad''': (
        '''https://huggingface.co/distilbert-base-cased-distilled-squad/resolve/main/config.json'''
    ),
    '''distilbert-base-german-cased''': '''https://huggingface.co/distilbert-base-german-cased/resolve/main/config.json''',
    '''distilbert-base-multilingual-cased''': (
        '''https://huggingface.co/distilbert-base-multilingual-cased/resolve/main/config.json'''
    ),
    '''distilbert-base-uncased-finetuned-sst-2-english''': (
        '''https://huggingface.co/distilbert-base-uncased-finetuned-sst-2-english/resolve/main/config.json'''
    ),
}


class       SCREAMING_SNAKE_CASE__					(__snake_case     ):
    __lowerCamelCase						:       Any														=     """distilbert"""
    __lowerCamelCase						:       List[Any]														=     {
        """hidden_size""": """dim""",
        """num_attention_heads""": """n_heads""",
        """num_hidden_layers""": """n_layers""",
    }





    def __init__(      self						,   a=3_0522						,   a=512						,   a=False						,   a=6						,   a=12						,   a=768						,   a=4 * 768						,   a=0.1						,   a=0.1						,   a="gelu"						,   a=0.02						,   a=0.1						,   a=0.2						,   a=0						,   **a						,   ):
         lowercase__    :   int      =       vocab_size
         lowercase__    :   int      =       max_position_embeddings
         lowercase__    :   List[str]      =       sinusoidal_pos_embds
         lowercase__    :   Tuple      =       n_layers
         lowercase__    :   Optional[int]      =       n_heads
         lowercase__    :   List[Any]      =       dim
         lowercase__    :   Any      =       hidden_dim
         lowercase__    :   Union[str, Any]      =       dropout
         lowercase__    :   Optional[int]      =       attention_dropout
         lowercase__    :   List[Any]      =       activation
         lowercase__    :   Union[str, Any]      =       initializer_range
         lowercase__    :   Dict      =       qa_dropout
         lowercase__    :   Tuple      =       seq_classif_dropout
         super().__init__(**a						,   pad_token_id=a)



class       SCREAMING_SNAKE_CASE__					(__snake_case     ):





    @property
    def       snake_case_				(      self):
         if self.task == "multiple-choice":
              lowercase__    :   List[str]      =       {0: 'batch', 1: 'choice', 2: 'sequence'}
         else:
              lowercase__    :   Tuple      =       {0: 'batch', 1: 'sequence'}
         return OrderedDict(
             [
                 ('input_ids', dynamic_axis),
                 ('attention_mask', dynamic_axis),
             ])

