


'''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			(	a_	):
    """simple docstring"""


    def __init__(  self      :							Optional[Any]				,							*SCREAMING_SNAKE_CASE      :							Optional[Any]				,							SCREAMING_SNAKE_CASE      :							Optional[int]=None				,							SCREAMING_SNAKE_CASE      :							Any=None				,							**SCREAMING_SNAKE_CASE      :							Dict):
           super().__init__(*SCREAMING_SNAKE_CASE				,							**SCREAMING_SNAKE_CASE)
           _A					:     Any     = eval_examples
           _A					:     Union[str, Any]     = post_process_function


    def        A   (  self      :							Dict				,							SCREAMING_SNAKE_CASE      :							Any=None				,							SCREAMING_SNAKE_CASE      :							Dict=None				,							SCREAMING_SNAKE_CASE      :							Tuple=None				,							SCREAMING_SNAKE_CASE      :							str = "eval"):
           _A					:     Optional[Any]     = self.eval_dataset if eval_dataset is None else eval_dataset
           _A					:     Tuple     = self.get_eval_dataloader(SCREAMING_SNAKE_CASE)
           _A					:     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.
           _A					:     str     = self.compute_metrics
           _A					:     Union[str, Any]     = None
           _A					:     List[str]     = self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           _A					:     List[str]     = time.time()
           try:
                  _A					:     Dict     = eval_loop(
                      SCREAMING_SNAKE_CASE				,							description='Evaluation'				,							prediction_loss_only=True if compute_metrics is None else None				,							ignore_keys=SCREAMING_SNAKE_CASE				,							metric_key_prefix=SCREAMING_SNAKE_CASE				,							)
           finally:
                  _A					:     Optional[int]     = compute_metrics
           _A					:     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(
                   SCREAMING_SNAKE_CASE				,							SCREAMING_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
                  _A					:     int     = self.post_process_function(SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE				,							output.predictions)
                  _A					:     Optional[Any]     = self.compute_metrics(SCREAMING_SNAKE_CASE)

                  # Prefix all keys with metric_key_prefix + '_'
                  for key in list(metrics.keys()):
                         if not key.startswith(F'{metric_key_prefix}_'):
                                _A					:     Optional[int]     = metrics.pop(SCREAMING_SNAKE_CASE)
                  metrics.update(output.metrics)
           else:
                  _A					:     List[Any]     = output.metrics

           if self.args.should_log:
                  # Only the main node log the results by default
                  self.log(SCREAMING_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())

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


    def        A   (  self      :							Tuple				,							SCREAMING_SNAKE_CASE      :							Tuple				,							SCREAMING_SNAKE_CASE      :							Dict				,							SCREAMING_SNAKE_CASE      :							List[str]=None				,							SCREAMING_SNAKE_CASE      :							str = "test"):
           _A					:     List[str]     = self.get_test_dataloader(SCREAMING_SNAKE_CASE)

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

           _A					:     str     = self.post_process_function(SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE				,							output.predictions				,							'predict')
           _A					:     Optional[int]     = self.compute_metrics(SCREAMING_SNAKE_CASE)

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










'''simple docstring'''







from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import TensorType, logging


if TYPE_CHECKING:
   from ...onnx.config import PatchingSpec
   from ...tokenization_utils_base import PreTrainedTokenizerBase


A				:					Tuple    						=			logging.get_logger(__name__)

A				:					Tuple    						=			{
    '''allenai/longformer-base-4096''': '''https://huggingface.co/allenai/longformer-base-4096/resolve/main/config.json''',
    '''allenai/longformer-large-4096''': '''https://huggingface.co/allenai/longformer-large-4096/resolve/main/config.json''',
    '''allenai/longformer-large-4096-finetuned-triviaqa''': (
        '''https://huggingface.co/allenai/longformer-large-4096-finetuned-triviaqa/resolve/main/config.json'''
    ),
    '''allenai/longformer-base-4096-extra.pos.embd.only''': (
        '''https://huggingface.co/allenai/longformer-base-4096-extra.pos.embd.only/resolve/main/config.json'''
    ),
    '''allenai/longformer-large-4096-extra.pos.embd.only''': (
        '''https://huggingface.co/allenai/longformer-large-4096-extra.pos.embd.only/resolve/main/config.json'''
    ),
}


class 			__lowerCamelCase			(	a_	):
    """simple docstring"""
    a													=   "longformer"


    def __init__(  self      :							Any				,							SCREAMING_SNAKE_CASE      :							Union[List[int], int] = 512				,							SCREAMING_SNAKE_CASE      :							int = 2				,							SCREAMING_SNAKE_CASE      :							int = 1				,							SCREAMING_SNAKE_CASE      :							int = 0				,							SCREAMING_SNAKE_CASE      :							int = 2				,							SCREAMING_SNAKE_CASE      :							int = 30522				,							SCREAMING_SNAKE_CASE      :							int = 768				,							SCREAMING_SNAKE_CASE      :							int = 12				,							SCREAMING_SNAKE_CASE      :							int = 12				,							SCREAMING_SNAKE_CASE      :							int = 3072				,							SCREAMING_SNAKE_CASE      :							str = "gelu"				,							SCREAMING_SNAKE_CASE      :							float = 0.1				,							SCREAMING_SNAKE_CASE      :							float = 0.1				,							SCREAMING_SNAKE_CASE      :							int = 512				,							SCREAMING_SNAKE_CASE      :							int = 2				,							SCREAMING_SNAKE_CASE      :							float = 0.02				,							SCREAMING_SNAKE_CASE      :							float = 1e-12				,							SCREAMING_SNAKE_CASE      :							bool = False				,							**SCREAMING_SNAKE_CASE      :							List[Any]				,							):
           super().__init__(pad_token_id=SCREAMING_SNAKE_CASE				,							**SCREAMING_SNAKE_CASE)

           _A					:     List[Any]     = attention_window
           _A					:     int     = sep_token_id
           _A					:     Tuple     = bos_token_id
           _A					:     Any     = eos_token_id
           _A					:     List[str]     = vocab_size
           _A					:     Any     = hidden_size
           _A					:     Optional[int]     = num_hidden_layers
           _A					:     int     = num_attention_heads
           _A					:     Dict     = hidden_act
           _A					:     List[Any]     = intermediate_size
           _A					:     int     = hidden_dropout_prob
           _A					:     Optional[int]     = attention_probs_dropout_prob
           _A					:     int     = max_position_embeddings
           _A					:     Any     = type_vocab_size
           _A					:     Dict     = initializer_range
           _A					:     Any     = layer_norm_eps
           _A					:     List[Any]     = onnx_export




class 			__lowerCamelCase			(	a_	):
    """simple docstring"""


    def __init__(  self      :							Optional[Any]				,							SCREAMING_SNAKE_CASE      :							"PretrainedConfig"				,							SCREAMING_SNAKE_CASE      :							str = "default"				,							SCREAMING_SNAKE_CASE      :							"List[PatchingSpec]" = None):
           super().__init__(SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE				,							SCREAMING_SNAKE_CASE)
           _A					:     Optional[Any]     = True


    @property
    def        A   (  self      :							List[str]):
           if self.task == "multiple-choice":
                  _A					:     Tuple     = {0: 'batch', 1: 'choice', 2: 'sequence'}
           else:
                  _A					:     List[Any]     = {0: 'batch', 1: 'sequence'}
           return OrderedDict(
               [
                   ('input_ids', dynamic_axis),
                   ('attention_mask', dynamic_axis),
                   ('global_attention_mask', dynamic_axis),
               ])


    @property
    def        A   (  self      :							str):
           _A					:     int     = super().outputs
           if self.task == "default":
                  _A					:     str     = {0: 'batch'}
           return outputs


    @property
    def        A   (  self      :							List[Any]):
           return 1e-4


    @property
    def        A   (  self      :							Dict):
           # needs to be >= 14 to support tril operator
           return max(super().default_onnx_opset				,							14)


    def        A   (  self      :							str				,							SCREAMING_SNAKE_CASE      :							"PreTrainedTokenizerBase"				,							SCREAMING_SNAKE_CASE      :							int = -1				,							SCREAMING_SNAKE_CASE      :							int = -1				,							SCREAMING_SNAKE_CASE      :							bool = False				,							SCREAMING_SNAKE_CASE      :							Optional[TensorType] = None				,							):
           _A					:     Union[str, Any]     = super().generate_dummy_inputs(
               preprocessor=SCREAMING_SNAKE_CASE				,							batch_size=SCREAMING_SNAKE_CASE				,							seq_length=SCREAMING_SNAKE_CASE				,							is_pair=SCREAMING_SNAKE_CASE				,							framework=SCREAMING_SNAKE_CASE)
           import torch

           # for some reason, replacing this code by inputs["global_attention_mask"] = torch.randint(2, inputs["input_ids"].shape, dtype=torch.int64)
           # makes the export fail randomly
           _A					:     Tuple     = torch.zeros_like(inputs['input_ids'])
           # make every second token global
           _A					:     Dict     = 1

           return inputs






