





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






		'''simple docstring'''






		def __init__(							self,   *snake_case__,   snake_case__=None,   snake_case__=None,   **snake_case__  )       -> Any:






									"""simple docstring"""
									super().__init__(*snake_case__,   **snake_case__  )
									lowercase_	:   str  				=     eval_examples
									lowercase_	:   List[Any]  				=     post_process_function






		def 						snake_case__				(							self,   snake_case__=None,   snake_case__=None,   snake_case__=None,   snake_case__ = "eval"  )       -> Optional[Any]:






									"""simple docstring"""
									lowercase_	:   Optional[Any]  				=     self.eval_dataset if eval_dataset is None else eval_dataset
									lowercase_	:   Optional[Any]  				=     self.get_eval_dataloader(snake_case__  )
									lowercase_	:   Optional[Any]  				=     self.eval_examples if eval_examples is None else eval_examples

									# Temporarily disable metric computation, we will do it in the loop here.
									lowercase_	:   Dict  				=     self.compute_metrics
									lowercase_	:   List[Any]  				=     None
									lowercase_	:   Optional[Any]  				=     self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
									lowercase_	:   str  				=     time.time()
									try:
																lowercase_	:   Optional[int]  				=     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:
																lowercase_	:   Optional[Any]  				=     compute_metrics
									lowercase_	:   Dict  				=     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
																lowercase_	:   Optional[Any]  				=     self.post_process_function(snake_case__,   snake_case__,   output.predictions  )
																lowercase_	:   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}_"""  ):
																														lowercase_	:   Tuple  				=     metrics.pop(snake_case__  )
																metrics.update(output.metrics  )
									else:
																lowercase_	:   Any  				=     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()  )

									lowercase_	:   Any  				=     self.callback_handler.on_evaluate(self.args,   self.state,   self.control,   snake_case__  )
									return metrics






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






									"""simple docstring"""
									lowercase_	:   Any  				=     self.get_test_dataloader(snake_case__  )

									# Temporarily disable metric computation, we will do it in the loop here.
									lowercase_	:   Dict  				=     self.compute_metrics
									lowercase_	:   Tuple  				=     None
									lowercase_	:   int  				=     self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
									lowercase_	:   Union[str, Any]  				=     time.time()
									try:
																lowercase_	:   Optional[int]  				=     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:
																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(
									        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

									lowercase_	:   Dict  				=     self.post_process_function(snake_case__,   snake_case__,   output.predictions,   """predict"""  )
									lowercase_	:   str  				=     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}_"""  ):
																							lowercase_	:   Union[str, Any]  				=     metrics.pop(snake_case__  )
									metrics.update(output.metrics  )
									return PredictionOutput(predictions=predictions.predictions,   label_ids=predictions.label_ids,   metrics=snake_case__  )






from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCAmelCase_							=				logging.get_logger(__name__)

UpperCAmelCase_							=				{
    """google/canine-s""": """https://huggingface.co/google/canine-s/resolve/main/config.json""",
    # See all CANINE models at https://huggingface.co/models?filter=canine
}
class     UpperCamelCase__					(     lowerCamelCase__				):






		'''simple docstring'''






		__a       :       Optional[int]       =	"""canine"""






		def __init__(							self,   snake_case__=7_68,   snake_case__=12,   snake_case__=12,   snake_case__=30_72,   snake_case__="gelu",   snake_case__=0.1,   snake_case__=0.1,   snake_case__=1_63_84,   snake_case__=16,   snake_case__=0.02,   snake_case__=1E-12,   snake_case__=0,   snake_case__=0XE_0_0_0,   snake_case__=0XE_0_0_1,   snake_case__=4,   snake_case__=4,   snake_case__=8,   snake_case__=1_63_84,   snake_case__=1_28,   **snake_case__,   )       -> Optional[int]:






									"""simple docstring"""
									super().__init__(pad_token_id=snake_case__,   bos_token_id=snake_case__,   eos_token_id=snake_case__,   **snake_case__  )

									lowercase_	:   int  				=     max_position_embeddings
									lowercase_	:   Union[str, Any]  				=     hidden_size
									lowercase_	:   Optional[Any]  				=     num_hidden_layers
									lowercase_	:   str  				=     num_attention_heads
									lowercase_	:   List[Any]  				=     intermediate_size
									lowercase_	:   Optional[Any]  				=     hidden_act
									lowercase_	:   List[str]  				=     hidden_dropout_prob
									lowercase_	:   Any  				=     attention_probs_dropout_prob
									lowercase_	:   Union[str, Any]  				=     initializer_range
									lowercase_	:   Optional[Any]  				=     type_vocab_size
									lowercase_	:   Dict  				=     layer_norm_eps

									# Character config:
									lowercase_	:   Optional[int]  				=     downsampling_rate
									lowercase_	:   List[str]  				=     upsampling_kernel_size
									lowercase_	:   int  				=     num_hash_functions
									lowercase_	:   List[str]  				=     num_hash_buckets
									lowercase_	:   List[str]  				=     local_transformer_stride