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







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

					"""simple docstring"""






					super().__init__(*lowerCamelCase_			,					**lowerCamelCase_						)
					_lowercase :    Union[str, Any]							=							eval_examples
					_lowercase :    List[Any]							=							post_process_function







				def 							__UpperCAmelCase					( self    :       Optional[Any]			,					lowerCamelCase_    :       Optional[Any]=None			,					lowerCamelCase_    :       Union[str, Any]=None			,					lowerCamelCase_    :       List[Any]=None			,					lowerCamelCase_    :       str = "eval"						):

					"""simple docstring"""






					_lowercase :    Dict							=							self.eval_dataset if eval_dataset is None else eval_dataset
					_lowercase :    Tuple							=							self.get_eval_dataloader(lowerCamelCase_						)
					_lowercase :    Optional[int]							=							self.eval_examples if eval_examples is None else eval_examples

					# Temporarily disable metric computation, we will do it in the loop here.
					_lowercase :    Tuple							=							self.compute_metrics
					_lowercase :    int							=							None
					_lowercase :    Union[str, Any]							=							self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
					_lowercase :    List[str]							=							time.time()
					try:
						_lowercase :    Any							=							eval_loop(
						    lowerCamelCase_			,					description='Evaluation'			,					prediction_loss_only=True if compute_metrics is None else None			,					ignore_keys=lowerCamelCase_			,					metric_key_prefix=lowerCamelCase_			,					)
					finally:
						_lowercase :    Optional[int]							=							compute_metrics
					_lowercase :    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(
					        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
						_lowercase :    Any							=							self.post_process_function(lowerCamelCase_			,					lowerCamelCase_			,					output.predictions						)
						_lowercase :    Union[str, Any]							=							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}_'''						):
								_lowercase :    Optional[Any]							=							metrics.pop(lowerCamelCase_						)
						metrics.update(output.metrics						)
					else:
						_lowercase :    List[str]							=							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()						)

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







				def 							__UpperCAmelCase					( self    :       List[Any]			,					lowerCamelCase_    :       List[str]			,					lowerCamelCase_    :       int			,					lowerCamelCase_    :       Optional[int]=None			,					lowerCamelCase_    :       str = "test"						):

					"""simple docstring"""






					_lowercase :    Dict							=							self.get_test_dataloader(lowerCamelCase_						)

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

					_lowercase :    Tuple							=							self.post_process_function(lowerCamelCase_			,					lowerCamelCase_			,					output.predictions			,					'predict'						)
					_lowercase :    Dict							=							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}_'''						):
							_lowercase :    List[str]							=							metrics.pop(lowerCamelCase_						)
					metrics.update(output.metrics						)
					return PredictionOutput(predictions=predictions.predictions			,					label_ids=predictions.label_ids			,					metrics=lowerCamelCase_						)




"""simple docstring"""







def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	return not any(
	    neighbour == 1 and colored_vertices[i] == color
	    for i, neighbour in enumerate(__UpperCAmelCase			)			)


def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	if index == len(__UpperCAmelCase			):
		return True

	# Recursive Step
	for i in range(__UpperCAmelCase			):
		if valid_coloring(graph[index]     ,__UpperCAmelCase     ,__UpperCAmelCase			):
			# Color current vertex
			_lowercase :    str							=							i
			# Validate coloring
			if util_color(__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase     ,index + 1			):
				return True
			# Backtrack
			_lowercase :    List[str]							=							-1
	return False







def      __lowerCAmelCase(		__UpperCAmelCase     ,__UpperCAmelCase			):






	"""simple docstring"""





	_lowercase :    Any							=							[-1] * len(__UpperCAmelCase			)

	if util_color(__UpperCAmelCase     ,__UpperCAmelCase     ,__UpperCAmelCase     ,0			):
		return colored_vertices

	return []



