



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






					'''simple docstring'''





					def __init__(       self:							Optional[int]						,     *snake_case:							Union[str, Any]						,     snake_case:							Dict=None						,     snake_case:							Optional[Any]=None						,     **snake_case:							Tuple						)   ->	str:
											super().__init__(*snake_case						,     **snake_case						)
											snake_case_ :List[Any]						=			eval_examples
											snake_case_ :Optional[Any]						=			post_process_function






					def    lowerCAmelCase_				(       self:							List[str]						,     snake_case:							str=None						,     snake_case:							List[Any]=None						,     snake_case:							Tuple=None						,     snake_case:							str = "eval"						)   ->	Union[str, Any]:
											snake_case_ :Dict						=			self.eval_dataset if eval_dataset is None else eval_dataset
											snake_case_ :str						=			self.get_eval_dataloader(snake_case						)
											snake_case_ :Union[str, Any]						=			self.eval_examples if eval_examples is None else eval_examples

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

											snake_case_ :Union[str, Any]						=			self.callback_handler.on_evaluate(self.args						,     self.state						,     self.control						,     snake_case						)
											return metrics






					def    lowerCAmelCase_				(       self:							int						,     snake_case:							Optional[Any]						,     snake_case:							int						,     snake_case:							Optional[Any]=None						,     snake_case:							str = "test"						)   ->	Dict:
											snake_case_ :List[str]						=			self.get_test_dataloader(snake_case						)

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

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








"""simple docstring"""
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


lowerCamelCase_					=      logging.get_logger(__name__)

lowerCamelCase_					=      {
    '''t5-small''': '''https://huggingface.co/t5-small/resolve/main/config.json''',
    '''t5-base''': '''https://huggingface.co/t5-base/resolve/main/config.json''',
    '''t5-large''': '''https://huggingface.co/t5-large/resolve/main/config.json''',
    '''t5-3b''': '''https://huggingface.co/t5-3b/resolve/main/config.json''',
    '''t5-11b''': '''https://huggingface.co/t5-11b/resolve/main/config.json''',
}







class 			UpperCamelCase_       (__A     ):
	__magic_name__	      =				'''t5'''
	__magic_name__	      =				['''past_key_values''']
	__magic_name__	      =				{'''hidden_size''': '''d_model''', '''num_attention_heads''': '''num_heads''', '''num_hidden_layers''': '''num_layers'''}





	def __init__(      self					: str	,	lowerCAmelCase_					: List[Any]=32_128	,	lowerCAmelCase_					: Tuple=512	,	lowerCAmelCase_					: Optional[int]=64	,	lowerCAmelCase_					: List[str]=2_048	,	lowerCAmelCase_					: Tuple=6	,	lowerCAmelCase_					: Dict=None	,	lowerCAmelCase_					: str=8	,	lowerCAmelCase_					: Optional[int]=32	,	lowerCAmelCase_					: Dict=128	,	lowerCAmelCase_					: Optional[Any]=0.1	,	lowerCAmelCase_					: str=1e-6	,	lowerCAmelCase_					: Dict=1.0	,	lowerCAmelCase_					: str="relu"	,	lowerCAmelCase_					: List[Any]=True	,	lowerCAmelCase_					: List[str]=True	,	lowerCAmelCase_					: Any=0	,	lowerCAmelCase_					: Tuple=1	,	**lowerCAmelCase_					: Optional[int]	,	)						->			int:
				UpperCAmelCase_     : int			     =					vocab_size
				UpperCAmelCase_     : Optional[Any]			     =					d_model
				UpperCAmelCase_     : str			     =					d_kv
				UpperCAmelCase_     : Any			     =					d_ff
				UpperCAmelCase_     : int			     =					num_layers
				UpperCAmelCase_     : Union[str, Any]			     =					(
				    num_decoder_layers if num_decoder_layers is not None else self.num_layers
				)  # default = symmetry
				UpperCAmelCase_     : Optional[Any]			     =					num_heads
				UpperCAmelCase_     : Any			     =					relative_attention_num_buckets
				UpperCAmelCase_     : Optional[Any]			     =					relative_attention_max_distance
				UpperCAmelCase_     : Optional[Any]			     =					dropout_rate
				UpperCAmelCase_     : Tuple			     =					layer_norm_epsilon
				UpperCAmelCase_     : int			     =					initializer_factor
				UpperCAmelCase_     : int			     =					feed_forward_proj
				UpperCAmelCase_     : str			     =					use_cache

				UpperCAmelCase_     : Tuple			     =					self.feed_forward_proj.split("-"						)
				UpperCAmelCase_     : List[Any]			     =					act_info[-1]
				UpperCAmelCase_     : Optional[int]			     =					act_info[0] == "gated"

				if len(lowerCAmelCase_						) > 1 and act_info[0] != "gated" or len(lowerCAmelCase_						) > 2:
							raise ValueError(
							    f"""`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."""
							    "Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. "
							    "'gated-gelu' or 'relu'"						)

				# for backwards compatibility
				if feed_forward_proj == "gated-gelu":
							UpperCAmelCase_     : int			     =					"gelu_new"

				super().__init__(
				    pad_token_id=lowerCAmelCase_	,	eos_token_id=lowerCAmelCase_	,	is_encoder_decoder=lowerCAmelCase_	,	**lowerCAmelCase_	,	)







class 			UpperCamelCase_       (__A     ):





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Dict						)						->			Mapping[str, Mapping[int, str]]:
				UpperCAmelCase_     : Any			     =					{
				    "input_ids": {0: "batch", 1: "encoder_sequence"},
				    "attention_mask": {0: "batch", 1: "encoder_sequence"},
				}
				if self.use_past:
							UpperCAmelCase_     : List[Any]			     =					"past_encoder_sequence + sequence"
							UpperCAmelCase_     : Union[str, Any]			     =					{0: "batch"}
							UpperCAmelCase_     : Optional[Any]			     =					{0: "batch", 1: "past_decoder_sequence + sequence"}
				else:
							UpperCAmelCase_     : List[Any]			     =					{0: "batch", 1: "decoder_sequence"}
							UpperCAmelCase_     : Tuple			     =					{0: "batch", 1: "decoder_sequence"}

				if self.use_past:
							self.fill_with_past_key_values_(lowerCAmelCase_	,	direction="inputs"						)

				return common_inputs





	@property
	def      _SCREAMING_SNAKE_CASE      (      self					: Any						)						->			int:
				return 13



