






"""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        UpperCamelCase__ (				a_):







    """simple docstring"""







    def __init__(		self :	Dict				,    *UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[int]=None				,    UpperCamelCase_ :	str=None				,    **UpperCamelCase_ :	int ):





           '''simple docstring'''

           super().__init__(*UpperCamelCase_				,    **UpperCamelCase_ )
           __magic_name__      				=    eval_examples
           __magic_name__      				=    post_process_function







    def 				a__			(		self :	Optional[int]				,    UpperCamelCase_ :	Optional[Dataset] = None				,    UpperCamelCase_ :	str=None				,    UpperCamelCase_ :	Optional[List[str]] = None				,    UpperCamelCase_ :	str = "eval"				,    **UpperCamelCase_ :	int				,    ):





           '''simple docstring'''

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

           __magic_name__      				=    self.eval_dataset if eval_dataset is None else eval_dataset
           __magic_name__      				=    self.get_eval_dataloader(UpperCamelCase_ )
           __magic_name__      				=    self.eval_examples if eval_examples is None else eval_examples

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

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

           __magic_name__      				=    self.callback_handler.on_evaluate(self.args				,    self.state				,    self.control				,    UpperCamelCase_ )
           return metrics







    def 				a__			(		self :	Optional[int]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Tuple				,    UpperCamelCase_ :	List[str]=None				,    UpperCamelCase_ :	str = "test"				,    **UpperCamelCase_ :	Tuple ):





           '''simple docstring'''

           __magic_name__      				=    gen_kwargs.copy()

           __magic_name__      				=    self.get_test_dataloader(UpperCamelCase_ )

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

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

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







"""simple docstring"""
import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_mobilebert import MobileBertTokenizer


snake_case : Optional[Any]     		=		logging.get_logger(__name__)

snake_case : str     		=		{"""vocab_file""": """vocab.txt""", """tokenizer_file""": """tokenizer.json"""}

snake_case : List[Any]     		=		{
    """vocab_file""": {"""mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/vocab.txt"""},
    """tokenizer_file""": {
        """mobilebert-uncased""": """https://huggingface.co/google/mobilebert-uncased/resolve/main/tokenizer.json"""
    },
}

snake_case : Tuple     		=		{"""mobilebert-uncased""": 5_1_2}


snake_case : List[str]     		=		{}



class        UpperCamelCase__ (				a_):







    """simple docstring"""
    __UpperCAmelCase           = VOCAB_FILES_NAMES
    __UpperCAmelCase           = PRETRAINED_VOCAB_FILES_MAP
    __UpperCAmelCase           = PRETRAINED_INIT_CONFIGURATION
    __UpperCAmelCase           = PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
    __UpperCAmelCase           = MobileBertTokenizer







    def __init__(		self :	Optional[int]				,    UpperCamelCase_ :	Optional[int]=None				,    UpperCamelCase_ :	str=None				,    UpperCamelCase_ :	Tuple=True				,    UpperCamelCase_ :	Any="[UNK]"				,    UpperCamelCase_ :	str="[SEP]"				,    UpperCamelCase_ :	Any="[PAD]"				,    UpperCamelCase_ :	Dict="[CLS]"				,    UpperCamelCase_ :	str="[MASK]"				,    UpperCamelCase_ :	Union[str, Any]=True				,    UpperCamelCase_ :	Dict=None				,    **UpperCamelCase_ :	Dict				,    ):





           '''simple docstring'''

           super().__init__(
               UpperCamelCase_				,    tokenizer_file=UpperCamelCase_				,    do_lower_case=UpperCamelCase_				,    unk_token=UpperCamelCase_				,    sep_token=UpperCamelCase_				,    pad_token=UpperCamelCase_				,    cls_token=UpperCamelCase_				,    mask_token=UpperCamelCase_				,    tokenize_chinese_chars=UpperCamelCase_				,    strip_accents=UpperCamelCase_				,    **UpperCamelCase_				,    )

           __magic_name__      				=    json.loads(self.backend_tokenizer.normalizer.__getstate__() )
           if (
               normalizer_state.get('lowercase'				,    UpperCamelCase_ ) != do_lower_case
               or normalizer_state.get('strip_accents'				,    UpperCamelCase_ ) != strip_accents
               or normalizer_state.get('handle_chinese_chars'				,    UpperCamelCase_ ) != tokenize_chinese_chars
           ):
                  __magic_name__      				=    getattr(UpperCamelCase_				,    normalizer_state.pop('type' ) )
                  __magic_name__      				=    do_lower_case
                  __magic_name__      				=    strip_accents
                  __magic_name__      				=    tokenize_chinese_chars
                  __magic_name__      				=    normalizer_class(**UpperCamelCase_ )

           __magic_name__      				=    do_lower_case







    def 				a__			(		self :	Dict				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	List[Any]=None ):





           '''simple docstring'''

           __magic_name__      				=    [self.cls_token_id] + token_ids_a + [self.sep_token_id]

           if token_ids_a:
                  output += token_ids_a + [self.sep_token_id]

           return output







    def 				a__			(		self :	List[Any]				,    UpperCamelCase_ :	List[int]				,    UpperCamelCase_ :	Optional[List[int]] = None ):





           '''simple docstring'''

           __magic_name__      				=    [self.sep_token_id]
           __magic_name__      				=    [self.cls_token_id]
           if token_ids_a is None:
                  return len(cls + token_ids_a + sep ) * [0]
           return len(cls + token_ids_a + sep ) * [0] + len(token_ids_a + sep ) * [1]







    def 				a__			(		self :	List[str]				,    UpperCamelCase_ :	str				,    UpperCamelCase_ :	Optional[str] = None ):





           '''simple docstring'''

           __magic_name__      				=    self._tokenizer.model.save(UpperCamelCase_				,    name=UpperCamelCase_ )
           return tuple(UpperCamelCase_ )