

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


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

_lowercase   :	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      _UpperCAmelCase   (							_lowerCAmelCase       ):
	a__		: Dict					=					"longformer"



	def __init__(  self     :       List[str]			,    _lowercase     :       Union[List[int], int] = 5_12			,    _lowercase     :       int = 2			,    _lowercase     :       int = 1			,    _lowercase     :       int = 0			,    _lowercase     :       int = 2			,    _lowercase     :       int = 3_05_22			,    _lowercase     :       int = 7_68			,    _lowercase     :       int = 12			,    _lowercase     :       int = 12			,    _lowercase     :       int = 30_72			,    _lowercase     :       str = "gelu"			,    _lowercase     :       float = 0.1			,    _lowercase     :       float = 0.1			,    _lowercase     :       int = 5_12			,    _lowercase     :       int = 2			,    _lowercase     :       float = 0.02			,    _lowercase     :       float = 1E-12			,    _lowercase     :       bool = False			,    **_lowercase     :       List[str]			,    ):
		super().__init__(pad_token_id=_lowercase			,    **_lowercase						)

		__UpperCAmelCase      		= attention_window
		__UpperCAmelCase      		= sep_token_id
		__UpperCAmelCase      		= bos_token_id
		__UpperCAmelCase      		= eos_token_id
		__UpperCAmelCase      		= vocab_size
		__UpperCAmelCase      		= hidden_size
		__UpperCAmelCase      		= num_hidden_layers
		__UpperCAmelCase      		= num_attention_heads
		__UpperCAmelCase      		= hidden_act
		__UpperCAmelCase      		= intermediate_size
		__UpperCAmelCase      		= hidden_dropout_prob
		__UpperCAmelCase      		= attention_probs_dropout_prob
		__UpperCAmelCase      		= max_position_embeddings
		__UpperCAmelCase      		= type_vocab_size
		__UpperCAmelCase      		= initializer_range
		__UpperCAmelCase      		= layer_norm_eps
		__UpperCAmelCase      		= onnx_export






class      _UpperCAmelCase   (							_lowerCAmelCase       ):



	def __init__(  self     :       List[str]			,    _lowercase     :       "PretrainedConfig"			,    _lowercase     :       str = "default"			,    _lowercase     :       "List[PatchingSpec]" = None						):
		super().__init__(_lowercase			,    _lowercase			,    _lowercase						)
		__UpperCAmelCase      		= True



	@property
	def        a  (  self     :       Dict						):
		if self.task == "multiple-choice":
			__UpperCAmelCase      		= {0: '''batch''', 1: '''choice''', 2: '''sequence'''}
		else:
			__UpperCAmelCase      		= {0: '''batch''', 1: '''sequence'''}
		return OrderedDict(
		    [
		        ('''input_ids''', dynamic_axis),
		        ('''attention_mask''', dynamic_axis),
		        ('''global_attention_mask''', dynamic_axis),
		    ]						)



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



	@property
	def        a  (  self     :       Any						):
		return 1E-4



	@property
	def        a  (  self     :       Optional[Any]						):
		# needs to be >= 14 to support tril operator
		return max(super().default_onnx_opset			,    14						)



	def        a  (  self     :       List[str]			,    _lowercase     :       "PreTrainedTokenizerBase"			,    _lowercase     :       int = -1			,    _lowercase     :       int = -1			,    _lowercase     :       bool = False			,    _lowercase     :       Optional[TensorType] = None			,    ):
		__UpperCAmelCase      		= super().generate_dummy_inputs(
		    preprocessor=_lowercase			,    batch_size=_lowercase			,    seq_length=_lowercase			,    is_pair=_lowercase			,    framework=_lowercase						)
		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
		__UpperCAmelCase      		= torch.zeros_like(inputs['''input_ids''']						)
		# make every second token global
		__UpperCAmelCase      		= 1

		return inputs






import numpy as np

import datasets


__a							     =					'\nCompute the Mahalanobis Distance\n\nMahalonobis distance is the distance between a point and a distribution.\nAnd not between two distinct points. It is effectively a multivariate equivalent of the Euclidean distance.\nIt was introduced by Prof. P. C. Mahalanobis in 1936\nand has been used in various statistical applications ever since\n[source: https://www.machinelearningplus.com/statistics/mahalanobis-distance/]\n'

__a							     =					'\\n@article{de2000mahalanobis,\n  title={The mahalanobis distance},\n  author={De Maesschalck, Roy and Jouan-Rimbaud, Delphine and Massart, D{\'e}sir{\'e} L},\n  journal={Chemometrics and intelligent laboratory systems},\n  volume={50},\n  number={1},\n  pages={1--18},\n  year={2000},\n  publisher={Elsevier}\n}\n'

__a							     =					'\nArgs:\n    X: List of datapoints to be compared with the `reference_distribution`.\n    reference_distribution: List of datapoints from the reference distribution we want to compare to.\nReturns:\n    mahalanobis: The Mahalonobis distance for each datapoint in `X`.\nExamples:\n\n    >>> mahalanobis_metric = datasets.load_metric("mahalanobis")\n    >>> results = mahalanobis_metric.compute(reference_distribution=[[0, 1], [1, 0]], X=[[0, 1]])\n    >>> print(results)\n    {\'mahalanobis\': array([0.5])}\n'

@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION							,		_KWARGS_DESCRIPTION  )
class     __a(	datasets.Metric  ):






    """simple docstring"""





    def 				a__ ( self    )					->						Union[str, Any]:
         return datasets.MetricInfo(
             description=_DESCRIPTION  ,citation=_CITATION  ,inputs_description=_KWARGS_DESCRIPTION  ,features=datasets.Features(
                 {
                     '''X''': datasets.Sequence(datasets.Value('''float'''  ,id='''sequence'''    )  ,id='''X'''    ),
                 }    )  ,)





    def 				a__ ( self  ,_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )					->						Any:
         # convert to numpy arrays
         UpperCAmelCase_     :				str  =     np.array(_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				List[Any]  =     np.array(_SCREAMING_SNAKE_CASE    )

         # Assert that arrays are 2D
         if len(X.shape    ) != 2:
              raise ValueError('''Expected `X` to be a 2D vector'''    )
         if len(reference_distribution.shape    ) != 2:
              raise ValueError('''Expected `reference_distribution` to be a 2D vector'''    )
         if reference_distribution.shape[0] < 2:
              raise ValueError(
                  '''Expected `reference_distribution` to be a 2D vector with more than one element in the first dimension'''    )

         # Get mahalanobis distance for each prediction
         UpperCAmelCase_     :				List[str]  =     X - np.mean(_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Dict  =     np.cov(reference_distribution.T    )
         try:
              UpperCAmelCase_     :				Any  =     np.linalg.inv(_SCREAMING_SNAKE_CASE    )
         except np.linalg.LinAlgError:
              UpperCAmelCase_     :				List[str]  =     np.linalg.pinv(_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				Union[str, Any]  =     np.dot(_SCREAMING_SNAKE_CASE  ,_SCREAMING_SNAKE_CASE    )
         UpperCAmelCase_     :				List[Any]  =     np.dot(_SCREAMING_SNAKE_CASE  ,X_minus_mu.T    ).diagonal()

         return {"mahalanobis": mahal_dist}