


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



     """simple docstring"""






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

            '''simple docstring'''

            super().__init__(*snake_case__   ,						**snake_case__ )
            _lowerCAmelCase     :				Optional[Any]       =							eval_examples
            _lowerCAmelCase     :				List[Any]       =							post_process_function







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

            '''simple docstring'''

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

            _lowerCAmelCase     :				Any       =							self.eval_dataset if eval_dataset is None else eval_dataset
            _lowerCAmelCase     :				int       =							self.get_eval_dataloader(snake_case__ )
            _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     :				Dict       =							self.compute_metrics
            _lowerCAmelCase     :				Optional[Any]       =							None
            _lowerCAmelCase     :				Tuple       =							time.time()
            _lowerCAmelCase     :				Tuple       =							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
            try:
                   _lowerCAmelCase     :				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     :				List[Any]       =							compute_metrics
            _lowerCAmelCase     :				Tuple       =							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     :				Union[str, Any]       =							self.post_process_function(snake_case__   ,						snake_case__   ,						snake_case__ )
                   _lowerCAmelCase     :				Optional[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[Any]       =							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     :				Optional[int]       =							self.callback_handler.on_evaluate(self.args   ,						self.state   ,						self.control   ,						snake_case__ )
            return metrics







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

            '''simple docstring'''

            _lowerCAmelCase     :				Tuple       =							gen_kwargs.copy()

            _lowerCAmelCase     :				Union[str, Any]       =							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[Any]       =							None
            _lowerCAmelCase     :				Dict       =							time.time()
            _lowerCAmelCase     :				Union[str, Any]       =							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
            try:
                   _lowerCAmelCase     :				List[Any]       =							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     :				Optional[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(
                    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     :				int       =							self.post_process_function(snake_case__   ,						snake_case__   ,						snake_case__   ,						'predict' )
            _lowerCAmelCase     :				Dict       =							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     :				Dict       =							metrics.pop(snake_case__ )
            metrics.update(output.metrics )
            return PredictionOutput(predictions=predictions.predictions   ,						label_ids=predictions.label_ids   ,						metrics=snake_case__ )





'''simple docstring'''







import unittest

from transformers import RoFormerTokenizer, RoFormerTokenizerFast
from transformers.testing_utils import require_rjieba, require_tokenizers

from ...test_tokenization_common import TokenizerTesterMixin
@require_rjieba
@require_tokenizers
class 						UpperCamelCase__       (			SCREAMING_SNAKE_CASE_     ,       unittest.TestCase						):



     """simple docstring"""






     __magic_name__           =     RoFormerTokenizer
     __magic_name__           =     RoFormerTokenizerFast
     __magic_name__           =     True
     __magic_name__           =     True







     def a						(			self ):

            '''simple docstring'''

            super().setUp()







     def a						(			self   ,						**snake_case__ ):

            '''simple docstring'''

            return self.tokenizer_class.from_pretrained('junnyu/roformer_chinese_base'   ,						**snake_case__ )







     def a						(			self   ,						**snake_case__ ):

            '''simple docstring'''

            return self.rust_tokenizer_class.from_pretrained('junnyu/roformer_chinese_base'   ,						**snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[Any]       =							'永和服装饰品有限公司,今天天气非常好'
            _lowerCAmelCase     :				str       =							'永和 服装 饰品 有限公司 , 今 天 天 气 非常 好'
            return input_text, output_text







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				Optional[int]       =							self.get_tokenizer()
            _lowerCAmelCase      ,  _lowerCAmelCase     :				int       =							self.get_chinese_input_output_texts()
            _lowerCAmelCase     :				List[Any]       =							tokenizer.tokenize(snake_case__ )

            self.assertListEqual(snake_case__   ,						output_text.split() )

            _lowerCAmelCase     :				Optional[Any]       =							tokens + [tokenizer.unk_token]
            _lowerCAmelCase     :				List[str]       =							[2_2943, 2_1332, 3_4431, 4_5904, 117, 306, 1231, 1231, 2653, 3_3994, 1266, 100]
            self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ )   ,						snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            _lowerCAmelCase     :				List[Any]       =							self.get_rust_tokenizer()
            _lowerCAmelCase      ,  _lowerCAmelCase     :				Any       =							self.get_chinese_input_output_texts()
            _lowerCAmelCase     :				Optional[int]       =							tokenizer.tokenize(snake_case__ )
            self.assertListEqual(snake_case__   ,						output_text.split() )
            _lowerCAmelCase     :				Tuple       =							tokens + [tokenizer.unk_token]
            _lowerCAmelCase     :				List[Any]       =							[2_2943, 2_1332, 3_4431, 4_5904, 117, 306, 1231, 1231, 2653, 3_3994, 1266, 100]
            self.assertListEqual(tokenizer.convert_tokens_to_ids(snake_case__ )   ,						snake_case__ )







     def a						(			self ):

            '''simple docstring'''

            pass







     def a						(			self ):

            '''simple docstring'''

            pass







     def a						(			self ):

            '''simple docstring'''

            pass

