



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    SCREAMING_SNAKE_CASE			(							lowerCamelCase__					):





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

											'''simple docstring'''
											super().__init__(*__lowercase      ,				**__lowercase	)
											__a       =		eval_examples
											__a       =		post_process_function





							def 						UpperCamelCase_					(   self		:		Tuple      ,				__lowercase		:		List[Any]=None      ,				__lowercase		:		Any=None      ,				__lowercase		:		List[str]=None      ,				__lowercase		:		str = "eval"	):

											'''simple docstring'''
											__a       =		self.eval_dataset if eval_dataset is None else eval_dataset
											__a       =		self.get_eval_dataloader(__lowercase	)
											__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       =		self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											__a       =		time.time()
											try:
															__a       =		eval_loop(
															    __lowercase      ,				description="""Evaluation"""      ,				prediction_loss_only=True if compute_metrics is None else None      ,				ignore_keys=__lowercase      ,				metric_key_prefix=__lowercase      ,				)
											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(
											        __lowercase      ,				__lowercase      ,				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(__lowercase      ,				__lowercase      ,				output.predictions	)
															__a       =		self.compute_metrics(__lowercase	)

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

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

											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      ,				__lowercase	)
											return metrics







							def 						UpperCamelCase_					(   self		:		str      ,				__lowercase		:		Dict      ,				__lowercase		:		str      ,				__lowercase		:		List[str]=None      ,				__lowercase		:		str = "test"	):

											'''simple docstring'''
											__a       =		self.get_test_dataloader(__lowercase	)

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

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





import itertools
import string
from collections.abc import Generator, Iterable

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			Iterable[str]				,     _SCREAMING_SNAKE_CASE       :			int    ):
				"""simple docstring"""







				__a       =		iter(_SCREAMING_SNAKE_CASE    )
				while True:
								__a       =		tuple(itertools.islice(_SCREAMING_SNAKE_CASE				,     _SCREAMING_SNAKE_CASE    )    )
								if not chunk:
												return
								yield chunk

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		"""""".join([c.upper() for c in dirty if c in string.ascii_letters]    )
				__a       =		""""""

				if len(_SCREAMING_SNAKE_CASE    ) < 2:
								return dirty

				for i in range(len(_SCREAMING_SNAKE_CASE    ) - 1    ):
								clean += dirty[i]

								if dirty[i] == dirty[i + 1]:
												clean += "X"

				clean += dirty[-1]

				if len(_SCREAMING_SNAKE_CASE    ) & 1:
								clean += "X"

				return clean

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		"""ABCDEFGHIKLMNOPQRSTUVWXYZ"""
				# we're using a list instead of a '2d' array because it makes the math
				# for setting up the table and doing the actual encoding/decoding simpler
				__a       =		[]

				# copy key chars into the table if they are in `alphabet` ignoring duplicates
				for char in key.upper():
								if char not in table and char in alphabet:
												table.append(_SCREAMING_SNAKE_CASE    )

    # fill the rest of the table in with the remaining alphabet chars
				for char in alphabet:
								if char not in table:
												table.append(_SCREAMING_SNAKE_CASE    )

				return table

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str				,     _SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		generate_table(_SCREAMING_SNAKE_CASE    )
				__a       =		prepare_input(_SCREAMING_SNAKE_CASE    )
				__a       =		""""""

				# https://en.wikipedia.org/wiki/Playfair_cipher#Description
				for chara, chara in chunker(_SCREAMING_SNAKE_CASE				,     2    ):
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )

								if rowa == rowa:
												ciphertext += table[rowa * 5 + (cola + 1) % 5]
												ciphertext += table[rowa * 5 + (cola + 1) % 5]
								elif cola == cola:
												ciphertext += table[((rowa + 1) % 5) * 5 + cola]
												ciphertext += table[((rowa + 1) % 5) * 5 + cola]
								else:  # rectangle
												ciphertext += table[rowa * 5 + cola]
												ciphertext += table[rowa * 5 + cola]

				return ciphertext

def   lowerCAmelCase__						(							_SCREAMING_SNAKE_CASE       :			str				,     _SCREAMING_SNAKE_CASE       :			str    ):
				"""simple docstring"""







				__a       =		generate_table(_SCREAMING_SNAKE_CASE    )
				__a       =		""""""

				# https://en.wikipedia.org/wiki/Playfair_cipher#Description
				for chara, chara in chunker(_SCREAMING_SNAKE_CASE				,     2    ):
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )
								__a     ,					__a       =		divmod(table.index(_SCREAMING_SNAKE_CASE    )				,     5    )

								if rowa == rowa:
												plaintext += table[rowa * 5 + (cola - 1) % 5]
												plaintext += table[rowa * 5 + (cola - 1) % 5]
								elif cola == cola:
												plaintext += table[((rowa - 1) % 5) * 5 + cola]
												plaintext += table[((rowa - 1) % 5) * 5 + cola]
								else:  # rectangle
												plaintext += table[rowa * 5 + cola]
												plaintext += table[rowa * 5 + cola]

				return plaintext
