
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 __snake_case		(				a_					):
    def __init__(  self		,*snake_case		,snake_case=None		,snake_case=None		,**snake_case				):



           '''simple docstring'''







           super().__init__(*snake_case		,**snake_case				)
           lowercase							:					Tuple	     =				eval_examples
           lowercase							:					Any	     =				post_process_function
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case=None		,snake_case=None		,snake_case=None		,snake_case = "eval"				):



           '''simple docstring'''







           lowercase							:					Optional[Any]	     =				self.eval_dataset if eval_dataset is None else eval_dataset
           lowercase							:					Tuple	     =				self.get_eval_dataloader(snake_case				)
           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							:					Optional[int]	     =				self.compute_metrics
           lowercase							:					List[Any]	     =				None
           lowercase							:					Any	     =				self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           lowercase							:					Optional[int]	     =				time.time()
           try:
                  lowercase							:					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							:					Dict	     =				compute_metrics
           lowercase							:					Tuple	     =				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							:					Union[str, Any]	     =				self.post_process_function(snake_case		,snake_case		,output.predictions				)
                  lowercase							:					Dict	     =				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							:					List[str]	     =				metrics.pop(snake_case				)
                  metrics.update(output.metrics				)
           else:
                  lowercase							:					List[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							:					str	     =				self.callback_handler.on_evaluate(self.args		,self.state		,self.control		,snake_case				)
           return metrics
    def 				_SCREAMING_SNAKE_CASE	(  self		,snake_case		,snake_case		,snake_case=None		,snake_case = "test"				):



           '''simple docstring'''







           lowercase							:					Optional[int]	     =				self.get_test_dataloader(snake_case				)

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



from typing import Mapping

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


lowercase  : List[str]						   =							logging.get_logger(__name__)

lowercase  : Tuple						   =							{
    """google/umt5-small""": """https://huggingface.co/google/umt5-small/resolve/main/config.json""",
    # See all umt5 models at https://huggingface.co/models?filter=umt5
}

class __snake_case		(				lowerCAmelCase					):
    _a		:						List[str]=    "umt5"
    _a		:						Optional[Any]=    ["past_key_values"]
    def __init__(  self		,snake_case=250112		,snake_case=512		,snake_case=64		,snake_case=1024		,snake_case=8		,snake_case=None		,snake_case=6		,snake_case=32		,snake_case=128		,snake_case=0.1		,snake_case=1e-6		,snake_case=1.0		,snake_case="gated-gelu"		,snake_case=True		,snake_case=True		,snake_case="T5Tokenizer"		,snake_case=True		,snake_case=0		,snake_case=1		,snake_case=0		,**snake_case		,):



           '''simple docstring'''







           super().__init__(
               is_encoder_decoder=snake_case		,tokenizer_class=snake_case		,tie_word_embeddings=snake_case		,pad_token_id=snake_case		,eos_token_id=snake_case		,decoder_start_token_id=snake_case		,**snake_case		,)
           lowercase							:					List[str]	     =				vocab_size
           lowercase							:					Optional[Any]	     =				d_model
           lowercase							:					int	     =				d_kv
           lowercase							:					List[Any]	     =				d_ff
           lowercase							:					Dict	     =				num_layers
           lowercase							:					int	     =				(
               num_decoder_layers if num_decoder_layers is not None else self.num_layers
           )  # default = symmetry
           lowercase							:					List[Any]	     =				num_heads
           lowercase							:					Optional[Any]	     =				relative_attention_num_buckets
           lowercase							:					Dict	     =				relative_attention_max_distance
           lowercase							:					Dict	     =				dropout_rate
           lowercase							:					Any	     =				layer_norm_epsilon
           lowercase							:					Any	     =				initializer_factor
           lowercase							:					Union[str, Any]	     =				feed_forward_proj
           lowercase							:					Optional[Any]	     =				use_cache

           lowercase							:					Dict	     =				self.feed_forward_proj.split("""-"""				)
           lowercase							:					List[Any]	     =				act_info[-1]
           lowercase							:					Any	     =				act_info[0] == """gated"""

           if len(snake_case				) > 1 and act_info[0] != "gated" or len(snake_case				) > 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'"""				)

           if feed_forward_proj == "gated-gelu":
                  lowercase							:					int	     =				"""gelu_new"""
    @property
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return self.d_model
    @property
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return self.num_heads
    @property
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return self.num_layers






class __snake_case		(				lowerCAmelCase					):
    @property
    # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.inputs
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           lowercase							:					int	     =				{
               """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
               """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
           }
           if self.use_past:
                  lowercase							:					List[Any]	     =				"""past_encoder_sequence + sequence"""
                  lowercase							:					Dict	     =				{0: """batch"""}
                  lowercase							:					Union[str, Any]	     =				{0: """batch""", 1: """past_decoder_sequence + sequence"""}
           else:
                  lowercase							:					Optional[Any]	     =				{0: """batch""", 1: """decoder_sequence"""}
                  lowercase							:					List[Any]	     =				{0: """batch""", 1: """decoder_sequence"""}

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

           return common_inputs
    @property
    # Copied from transformers.models.t5.configuration_t5.T5OnnxConfig.default_onnx_opset
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return 13
    @property
    def 				_SCREAMING_SNAKE_CASE	(  self				):



           '''simple docstring'''







           return 5e-4
