





'''simple docstring'''

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 							__lowerCAmelCase	(       __magic_name__					):






       """simple docstring"""





       def __init__(				self  :						Optional[Any]			,					*lowerCAmelCase__  :						Union[str, Any]			,					lowerCAmelCase__  :						Any=None			,					lowerCAmelCase__  :						Tuple=None			,					**lowerCAmelCase__  :						Any	)       ->	List[str]:

            '''simple docstring'''




            super().__init__(*lowerCAmelCase__			,					**lowerCAmelCase__	)
            _UpperCamelCase							       =			eval_examples
            _UpperCamelCase							       =			post_process_function





       def snake_case__		(				self  :						List[Any]			,					lowerCAmelCase__  :						Tuple=None			,					lowerCAmelCase__  :						Any=None			,					lowerCAmelCase__  :						str=None			,					lowerCAmelCase__  :						str = "eval"	)       ->	Union[str, Any]:

            '''simple docstring'''




            _UpperCamelCase							       =			self.eval_dataset if eval_dataset is None else eval_dataset
            _UpperCamelCase							       =			self.get_eval_dataloader(lowerCAmelCase__	)
            _UpperCamelCase							       =			self.eval_examples if eval_examples is None else eval_examples

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

                 # Prefix all keys with metric_key_prefix + '_'
                 for key in list(metrics.keys()	):
                      if not key.startswith(f"""{metric_key_prefix}_"""	):
                           _UpperCamelCase							       =			metrics.pop(lowerCAmelCase__	)
                 metrics.update(output.metrics	)
            else:
                 _UpperCamelCase							       =			output.metrics

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

            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()	)

            _UpperCamelCase							       =			self.callback_handler.on_evaluate(self.args			,					self.state			,					self.control			,					lowerCAmelCase__	)
            return metrics







       def snake_case__		(				self  :						Dict			,					lowerCAmelCase__  :						Optional[Any]			,					lowerCAmelCase__  :						Optional[int]			,					lowerCAmelCase__  :						int=None			,					lowerCAmelCase__  :						str = "test"	)       ->	str:

            '''simple docstring'''




            _UpperCamelCase							       =			self.get_test_dataloader(lowerCAmelCase__	)

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

            _UpperCamelCase							       =			self.post_process_function(lowerCAmelCase__			,					lowerCAmelCase__			,					output.predictions			,					'''predict'''	)
            _UpperCamelCase							       =			self.compute_metrics(lowerCAmelCase__	)

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






def 			lowerCamelCase__			(       _a):
	if not isinstance(_a				,      _a):
		SCREAMING_SNAKE_CASE :   Tuple					  =							f"Input value of [number={number}] must be an integer"
		raise TypeError(_a)
	if number < 0:
		return False
	SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							number * number
	while number > 0:
		if number % 10 != number_square % 10:
			return False
		number //= 10
		number_square //= 10
	return True


if __name__ == "__main__":
			import doctest

			doctest.testmod()