
'''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__     (			__lowerCamelCase):






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







       '''simple docstring'''



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






 def 		lowercase_  (	self     :Any		, _A     :Optional[Dataset] = None		, _A     :List[str]=None		, _A     :Optional[List[str]] = None		, _A     :str = "eval"		, **_A     :int		, ) ->	int:







       '''simple docstring'''



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

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

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

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

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






 def 		lowercase_  (	self     :List[str]		, _A     :Dict		, _A     :Optional[Any]		, _A     :List[Any]=None		, _A     :str = "test"		, **_A     :Dict	) ->	List[str]:







       '''simple docstring'''



       __A						=	gen_kwargs.copy()

       __A						=	self.get_test_dataloader(UpperCamelCase_	)

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

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

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












from transformers import DistilBertTokenizer, DistilBertTokenizerFast
from transformers.testing_utils import require_tokenizers, slow

from ..bert.test_tokenization_bert import BertTokenizationTest





@require_tokenizers
class     lowerCamelCase__(					__lowerCamelCase):
      UpperCAmelCase__      :	Dict    		=   DistilBertTokenizer
      UpperCAmelCase__      :	Dict    		=   DistilBertTokenizerFast
      UpperCAmelCase__      :	Tuple    		=   True
      @slow
      def 			lowerCAmelCase__						(				self: Tuple   ):
             __lowerCamelCase   =						DistilBertTokenizer.from_pretrained("""distilbert-base-uncased"""   )

             __lowerCamelCase   =						tokenizer.encode("""sequence builders"""    ,       add_special_tokens=UpperCamelCase_   )
             __lowerCamelCase   =						tokenizer.encode("""multi-sequence build"""    ,       add_special_tokens=UpperCamelCase_   )

             __lowerCamelCase   =						tokenizer.build_inputs_with_special_tokens(UpperCamelCase_   )
             __lowerCamelCase   =						tokenizer.build_inputs_with_special_tokens(UpperCamelCase_    ,       UpperCamelCase_   )

             assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
             assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + text_a + [
                 tokenizer.sep_token_id
             ]

