





'''simple docstring'''
import datasets

from .evaluate import evaluate


a__							:							List[str] ='''\
@article{hendrycks2021cuad,
      title={CUAD: An Expert-Annotated NLP Dataset for Legal Contract Review},
      author={Dan Hendrycks and Collin Burns and Anya Chen and Spencer Ball},
      journal={arXiv preprint arXiv:2103.06268},
      year={2021}
}
'''

a__							:							Dict ='''
This metric wrap the official scoring script for version 1 of the Contract
Understanding Atticus Dataset (CUAD).
Contract Understanding Atticus Dataset (CUAD) v1 is a corpus of more than 13,000 labels in 510
commercial legal contracts that have been manually labeled to identify 41 categories of important
clauses that lawyers look for when reviewing contracts in connection with corporate transactions.
'''

a__							:							List[Any] ='''
Computes CUAD scores (EM, F1, AUPR, Precision@80%Recall, and Precision@90%Recall).
Args:
    predictions: List of question-answers dictionaries with the following key-values:
        - \'id\': id of the question-answer pair as given in the references (see below)
        - \'prediction_text\': list of possible texts for the answer, as a list of strings
        depending on a threshold on the confidence probability of each prediction.
    references: List of question-answers dictionaries with the following key-values:
        - \'id\': id of the question-answer pair (see above),
        - \'answers\': a Dict in the CUAD dataset format
            {
                \'text\': list of possible texts for the answer, as a list of strings
                \'answer_start\': list of start positions for the answer, as a list of ints
            }
            Note that answer_start values are not taken into account to compute the metric.
Returns:
    \'exact_match\': Exact match (the normalized answer exactly match the gold answer)
    \'f1\': The F-score of predicted tokens versus the gold answer
    \'aupr\': Area Under the Precision-Recall curve
    \'prec_at_80_recall\': Precision at 80% recall
    \'prec_at_90_recall\': Precision at 90% recall
Examples:
    >>> predictions = [{\'prediction_text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\'], \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]
    >>> references = [{\'answers\': {\'answer_start\': [143, 49], \'text\': [\'The seller:\', \'The buyer/End-User: Shenzhen LOHAS Supply Chain Management Co., Ltd.\']}, \'id\': \'LohaCompanyltd_20191209_F-1_EX-10.16_11917878_EX-10.16_Supply Agreement__Parties\'}]
    >>> cuad_metric = datasets.load_metric("cuad")
    >>> results = cuad_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 100.0, \'f1\': 100.0, \'aupr\': 0.0, \'prec_at_80_recall\': 1.0, \'prec_at_90_recall\': 1.0}
'''

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




 """simple docstring"""



 def        _lowerCamelCase					(       self       :							Optional[int]	):
        return datasets.MetricInfo(
            description=_DESCRIPTION							,	citation=_CITATION							,	inputs_description=_KWARGS_DESCRIPTION							,	features=datasets.Features(
                {
                    'predictions': {
                        'id': datasets.Value('string'	),
                        'prediction_text': datasets.features.Sequence(datasets.Value('string'	)	),
                    },
                    'references': {
                        'id': datasets.Value('string'	),
                        'answers': datasets.features.Sequence(
                            {
                                'text': datasets.Value('string'	),
                                'answer_start': datasets.Value('int32'	),
                            }	),
                    },
                }	)							,	codebase_urls=['https://www.atticusprojectai.org/cuad']							,	reference_urls=['https://www.atticusprojectai.org/cuad']							,	)






 def        _lowerCamelCase					(       self       :							str							,	__A       :							Optional[Any]							,	__A       :							int	):
        __UpperCamelCase       =  {prediction['id']: prediction['prediction_text'] for prediction in predictions}
        __UpperCamelCase       =  [
            {
                'paragraphs': [
                    {
                        'qas': [
                            {
                                'answers': [{'text': answer_text} for answer_text in ref['answers']['text']],
                                'id': ref['id'],
                            }
                            for ref in references
                        ]
                    }
                ]
            }
        ]
        __UpperCamelCase       =  evaluate(dataset=__A							,	predictions=__A	)
        return score







'''simple docstring'''
from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast
from ...utils import logging


a__							:							Any =logging.get_logger(__name__)

a__							:							Optional[Any] ={
    '''EleutherAI/gpt-neo-1.3B''': '''https://huggingface.co/EleutherAI/gpt-neo-1.3B/resolve/main/config.json''',
    # See all GPTNeo models at https://huggingface.co/models?filter=gpt_neo
}

class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 SCREAMING_SNAKE_CASE_	:					Dict					="gpt_neo"
 SCREAMING_SNAKE_CASE_	:					Optional[int]					=["past_key_values"]
 SCREAMING_SNAKE_CASE_	:					List[Any]					={"num_attention_heads": "num_heads", "num_hidden_layers": "num_layers"}






 def __init__(       self       :							Union[str, Any]							,	__A       :							Union[str, Any]=5_0_2_5_7							,	__A       :							Any=2_0_4_8							,	__A       :							Optional[Any]=2_0_4_8							,	__A       :							Any=2_4							,	__A       :							Union[str, Any]=[[["global", "local"], 1_2]]							,	__A       :							str=1_6							,	__A       :							Optional[int]=None							,	__A       :							Union[str, Any]=2_5_6							,	__A       :							Any="gelu_new"							,	__A       :							Dict=0.0							,	__A       :							Optional[int]=0.0							,	__A       :							int=0.0							,	__A       :							List[str]=0.1							,	__A       :							Any=1e-5							,	__A       :							int=0.02							,	__A       :							List[str]=True							,	__A       :							Tuple=5_0_2_5_6							,	__A       :							Optional[Any]=5_0_2_5_6							,	**__A       :							Optional[Any]							,	):
        __UpperCamelCase       =  vocab_size
        __UpperCamelCase       =  max_position_embeddings
        __UpperCamelCase       =  hidden_size
        __UpperCamelCase       =  num_layers
        __UpperCamelCase       =  num_heads
        __UpperCamelCase       =  intermediate_size
        __UpperCamelCase       =  window_size
        __UpperCamelCase       =  activation_function
        __UpperCamelCase       =  resid_dropout
        __UpperCamelCase       =  embed_dropout
        __UpperCamelCase       =  attention_dropout
        __UpperCamelCase       =  classifier_dropout
        __UpperCamelCase       =  layer_norm_epsilon
        __UpperCamelCase       =  initializer_range
        __UpperCamelCase       =  use_cache

        __UpperCamelCase       =  bos_token_id
        __UpperCamelCase       =  eos_token_id

        __UpperCamelCase       =  attention_types
        __UpperCamelCase       =  self.expand_attention_types_params(__A	)

        if len(self.attention_layers	) != self.num_layers:
               raise ValueError(
                   'Configuration for convolutional module is incorrect. '
                   'It is required that `len(config.attention_layers)` == `config.num_layers` '
                   f'''but is `len(config.attention_layers) = {len(self.attention_layers	)}`, '''
                   f'''`config.num_layers = {self.num_layers}`. '''
                   '`config.attention_layers` is prepared using `config.attention_types`. '
                   'Please verify the value of `config.attention_types` argument.'	)

        super().__init__(bos_token_id=__A							,	eos_token_id=__A							,	**__A	)






 @staticmethod
 def        _lowerCamelCase					(       __A       :							Tuple	):
        __UpperCamelCase       =  []
        for item in attention_types:
               for _ in range(item[1]	):
                      attentions.extend(item[0]	)
        return attentions






def 				lowercase__   (   __lowercase						:  Tuple				,  __lowercase						:  Any				,  __lowercase						:  Union[str, Any]				,  __lowercase						:  List[str]     ) ->      Any:



       """simple docstring"""






       import torch

       __UpperCamelCase       =  input.size()
       __UpperCamelCase       =  len(__lowercase     )
       __UpperCamelCase       =  shape[dimension]

       __UpperCamelCase       =  torch.arange(0				,  __lowercase				,  __lowercase     )
       __UpperCamelCase       =  torch.div(sizedim - size				,  __lowercase				,  rounding_mode='floor'     ) + 1
       __UpperCamelCase       =  torch.arange(__lowercase     ) + low_indices[:min_length][:, None]

       __UpperCamelCase       =  [slice(__lowercase     )] * rank
       __UpperCamelCase       =  indices
       __UpperCamelCase       =  input[s]

       __UpperCamelCase       =  list(range(0				,  rank + 1     )     )
       perm.append(perm.pop(dimension + 1     )     )

       return sliced.permute(__lowercase     )
def 				lowercase__   (   __lowercase						:  Union[str, Any]				,  __lowercase						:  Optional[int]     ) ->      Optional[int]:



       """simple docstring"""






       import torch

       __UpperCamelCase       =  torch.arange(1				,  __lowercase     )
       __UpperCamelCase       =  torch.remainder(__lowercase				,  __lowercase     )
       __UpperCamelCase       =  remainders == 0
       __UpperCamelCase       =  candidates[divisor_indices]
       __UpperCamelCase       =  torch.max(__lowercase     )
       return largest_divisor, torch.div(__lowercase				,  __lowercase				,  rounding_mode='floor'     )


class      snake_case     (    __lowerCamelCase       ):




 """simple docstring"""



 @property
 def        _lowerCamelCase					(       self       :							Tuple	):
        __UpperCamelCase       =  OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}	)
        if self.use_past:
               self.fill_with_past_key_values_(__A							,	direction='inputs'	)
               __UpperCamelCase       =  {0: 'batch', 1: 'past_sequence + sequence'}
        else:
               __UpperCamelCase       =  {0: 'batch', 1: 'sequence'}

        return common_inputs






 @property
 def        _lowerCamelCase					(       self       :							int	):
        return self._config.num_heads






 def        _lowerCamelCase					(       self       :							List[str]							,	__A       :							PreTrainedTokenizer							,	__A       :							int = -1							,	__A       :							int = -1							,	__A       :							bool = False							,	__A       :							Optional[TensorType] = None							,	):
        __UpperCamelCase       =  super(__A							,	self	).generate_dummy_inputs(
            __A							,	batch_size=__A							,	seq_length=__A							,	is_pair=__A							,	framework=__A	)

        # We need to order the input in the way they appears in the forward()
        __UpperCamelCase       =  OrderedDict({'input_ids': common_inputs['input_ids']}	)

        # Need to add the past_keys
        if self.use_past:
               if not is_torch_available():
                      raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'	)
               else:
                      import torch

                      __UpperCamelCase       ,						__UpperCamelCase       =  common_inputs['input_ids'].shape
                      # Not using the same length for past_key_values
                      __UpperCamelCase       =  seqlen + 2
                      __UpperCamelCase       =  (
                          batch,
                          self.num_attention_heads,
                          past_key_values_length,
                          self._config.hidden_size // self.num_attention_heads,
                      )
                      __UpperCamelCase       =  [
                          (torch.zeros(__A	), torch.zeros(__A	)) for _ in range(self.num_layers	)
                      ]

        __UpperCamelCase       =  common_inputs['attention_mask']
        if self.use_past:
               __UpperCamelCase       =  ordered_inputs['attention_mask'].dtype
               __UpperCamelCase       =  torch.cat(
                   [ordered_inputs['attention_mask'], torch.ones(__A							,	__A							,	dtype=__A	)]							,	dim=1	)

        return ordered_inputs






 @property
 def        _lowerCamelCase					(       self       :							Dict	):
        return 1_3
