





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    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''

 def __init__(self				:   str       , *UpperCAmelCase_				:   Any       , UpperCAmelCase_				:   Any=None       , UpperCAmelCase_				:   List[Any]=None       , **UpperCAmelCase_				:   Optional[int])       ->List[Any]:



   '''simple docstring'''



   super().__init__(*UpperCAmelCase_       , **UpperCAmelCase_)
   lowerCamelCase__:      Any			  =eval_examples
   lowerCamelCase__:      Dict			  =post_process_function

 def 							SCREAMING_SNAKE_CASE_					(self				:   Dict       , UpperCAmelCase_				:   int=None       , UpperCAmelCase_				:   Any=None       , UpperCAmelCase_				:   Optional[Any]=None       , UpperCAmelCase_				:   str = "eval")       ->List[Any]:



   '''simple docstring'''



   lowerCamelCase__:      Any			  =self.eval_dataset if eval_dataset is None else eval_dataset
   lowerCamelCase__:      Tuple			  =self.get_eval_dataloader(UpperCAmelCase_)
   lowerCamelCase__:      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__:      Optional[Any]			  =self.compute_metrics
   lowerCamelCase__:      Tuple			  =None
   lowerCamelCase__:      str			  =self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
   lowerCamelCase__:      List[str]			  =time.time()
   try:
     lowerCamelCase__:      Union[str, Any]			  =eval_loop(
         UpperCAmelCase_       , description="Evaluation"       , prediction_loss_only=True if compute_metrics is None else None       , ignore_keys=UpperCAmelCase_       , metric_key_prefix=UpperCAmelCase_       , )
   finally:
     lowerCamelCase__:      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(
           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
     lowerCamelCase__:      Optional[int]			  =self.post_process_function(UpperCAmelCase_       , UpperCAmelCase_       , output.predictions)
     lowerCamelCase__:      Tuple			  =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}_"""):
         lowerCamelCase__:      str			  =metrics.pop(UpperCAmelCase_)
     metrics.update(output.metrics)
   else:
     lowerCamelCase__:      Any			  =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())

   lowerCamelCase__:      List[Any]			  =self.callback_handler.on_evaluate(self.args       , self.state       , self.control       , UpperCAmelCase_)
   return metrics







 def 							SCREAMING_SNAKE_CASE_					(self				:   str       , UpperCAmelCase_				:   Any       , UpperCAmelCase_				:   Dict       , UpperCAmelCase_				:   Dict=None       , UpperCAmelCase_				:   str = "test")       ->Union[str, Any]:



   '''simple docstring'''



   lowerCamelCase__:      List[Any]			  =self.get_test_dataloader(UpperCAmelCase_)

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

   lowerCamelCase__:      Union[str, Any]			  =self.post_process_function(UpperCAmelCase_       , UpperCAmelCase_       , output.predictions       , "predict")
   lowerCamelCase__:      List[str]			  =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}_"""):
       lowerCamelCase__:      Optional[Any]			  =metrics.pop(UpperCAmelCase_)
   metrics.update(output.metrics)
   return PredictionOutput(predictions=predictions.predictions       , label_ids=predictions.label_ids       , metrics=UpperCAmelCase_)








import logging

from transformers.configuration_utils import PretrainedConfig


__A									=				logging.getLogger(__name__)



class    _SCREAMING_SNAKE_CASE						(	__SCREAMING_SNAKE_CASE    ):







 '''simple docstring'''
 lowercase_		=			"masked_bert"

 def __init__(self				:   Dict       , UpperCAmelCase_				:   Any=30_522       , UpperCAmelCase_				:   List[Any]=768       , UpperCAmelCase_				:   Optional[Any]=12       , UpperCAmelCase_				:   str=12       , UpperCAmelCase_				:   Tuple=3_072       , UpperCAmelCase_				:   str="gelu"       , UpperCAmelCase_				:   Any=0.1       , UpperCAmelCase_				:   Tuple=0.1       , UpperCAmelCase_				:   Optional[Any]=512       , UpperCAmelCase_				:   Union[str, Any]=2       , UpperCAmelCase_				:   str=0.02       , UpperCAmelCase_				:   str=1E-1_2       , UpperCAmelCase_				:   Union[str, Any]=0       , UpperCAmelCase_				:   str="topK"       , UpperCAmelCase_				:   List[str]="constant"       , UpperCAmelCase_				:   str=0.0       , **UpperCAmelCase_				:   int       , )       ->List[Any]:



   '''simple docstring'''



   super().__init__(pad_token_id=UpperCAmelCase_       , **UpperCAmelCase_)

   lowerCamelCase__:      Optional[int]			  =vocab_size
   lowerCamelCase__:      Dict			  =hidden_size
   lowerCamelCase__:      Optional[int]			  =num_hidden_layers
   lowerCamelCase__:      Any			  =num_attention_heads
   lowerCamelCase__:      List[Any]			  =hidden_act
   lowerCamelCase__:      str			  =intermediate_size
   lowerCamelCase__:      Dict			  =hidden_dropout_prob
   lowerCamelCase__:      str			  =attention_probs_dropout_prob
   lowerCamelCase__:      int			  =max_position_embeddings
   lowerCamelCase__:      Tuple			  =type_vocab_size
   lowerCamelCase__:      str			  =initializer_range
   lowerCamelCase__:      List[Any]			  =layer_norm_eps
   lowerCamelCase__:      str			  =pruning_method
   lowerCamelCase__:      Union[str, Any]			  =mask_init
   lowerCamelCase__:      Optional[Any]			  =mask_scale

