




'''simple docstring'''



import argparse
from typing import Dict

import tensorflow as tf
import torch
from tqdm import tqdm

from transformers import BigBirdPegasusConfig, BigBirdPegasusForConditionalGeneration


__UpperCamelCase      :       Optional[Any]      			=      [
    # tf -> hf
    ('''/''', '''.'''),
    ('''layer_''', '''layers.'''),
    ('''kernel''', '''weight'''),
    ('''beta''', '''bias'''),
    ('''gamma''', '''weight'''),
    ('''pegasus''', '''model'''),
]
__UpperCamelCase      :       Optional[int]      			=      [
    ('''.output.dense''', '''.fc2'''),
    ('''intermediate.LayerNorm''', '''final_layer_norm'''),
    ('''intermediate.dense''', '''fc1'''),
]

__UpperCamelCase      :       Dict      			=      (
    INIT_COMMON
    + [
        ('''attention.self.LayerNorm''', '''self_attn_layer_norm'''),
        ('''attention.output.dense''', '''self_attn.out_proj'''),
        ('''attention.self''', '''self_attn'''),
        ('''attention.encdec.LayerNorm''', '''encoder_attn_layer_norm'''),
        ('''attention.encdec_output.dense''', '''encoder_attn.out_proj'''),
        ('''attention.encdec''', '''encoder_attn'''),
        ('''key''', '''k_proj'''),
        ('''value''', '''v_proj'''),
        ('''query''', '''q_proj'''),
        ('''decoder.LayerNorm''', '''decoder.layernorm_embedding'''),
    ]
    + END_COMMON
)

__UpperCamelCase      :       Dict      			=      (
    INIT_COMMON
    + [
        ('''embeddings.word_embeddings''', '''shared.weight'''),
        ('''embeddings.position_embeddings''', '''embed_positions.weight'''),
        ('''attention.self.LayerNorm''', '''self_attn_layer_norm'''),
        ('''attention.output.dense''', '''self_attn.output'''),
        ('''attention.self''', '''self_attn.self'''),
        ('''encoder.LayerNorm''', '''encoder.layernorm_embedding'''),
    ]
    + END_COMMON
)

__UpperCamelCase      :       int      			=      [
    '''encdec/key/bias''',
    '''encdec/query/bias''',
    '''encdec/value/bias''',
    '''self/key/bias''',
    '''self/query/bias''',
    '''self/value/bias''',
    '''encdec_output/dense/bias''',
    '''attention/output/dense/bias''',
]



def    __UpperCAmelCase	(				SCREAMING_SNAKE_CASE__:	List[str],					SCREAMING_SNAKE_CASE__:	Union[str, Any]    )  ->			List[str]:

   """simple docstring"""




   for tf_name, hf_name in patterns:
      __a				     =    k.replace(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
   return k



def    __UpperCAmelCase	(				SCREAMING_SNAKE_CASE__:	dict,					SCREAMING_SNAKE_CASE__:	dict    )  ->			List[str]:

   """simple docstring"""
   __a				     =    BigBirdPegasusConfig(**SCREAMING_SNAKE_CASE__    )
   __a				     =    BigBirdPegasusForConditionalGeneration(SCREAMING_SNAKE_CASE__    )
   __a				     =    torch_model.state_dict()
   __a				     =    {}

   # separating decoder weights
   __a				     =    {k: tf_weights[k] for k in tf_weights if k.startswith('pegasus/decoder'    )}
   __a				     =    {k: tf_weights[k] for k in tf_weights if not k.startswith('pegasus/decoder'    )}

   for k, v in tqdm(decoder_weights.items(),					'tf -> hf conversion'    ):
      __a				     =    [k.endswith(SCREAMING_SNAKE_CASE__    ) for ending in KEYS_TO_IGNORE]
      if any(SCREAMING_SNAKE_CASE__    ):
         continue
      __a				     =    DECODER_PATTERNS
      __a				     =    rename_state_dict_key(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
      if new_k not in state_dict:
         raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})"""    )
      if any(True if i in k else False for i in ['dense', 'query', 'key', 'value']    ):
         __a				     =    v.T
      __a				     =    torch.from_numpy(SCREAMING_SNAKE_CASE__    )
      assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}"""

   for k, v in tqdm(remaining_weights.items(),					'tf -> hf conversion'    ):
      __a				     =    [k.endswith(SCREAMING_SNAKE_CASE__    ) for ending in KEYS_TO_IGNORE]
      if any(SCREAMING_SNAKE_CASE__    ):
         continue
      __a				     =    REMAINING_PATTERNS
      __a				     =    rename_state_dict_key(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
      if new_k not in state_dict and k != "pegasus/embeddings/position_embeddings":
         raise ValueError(f"""could not find new key {new_k} in state dict. (converted from {k})"""    )
      if any(True if i in k else False for i in ['dense', 'query', 'key', 'value']    ):
         __a				     =    v.T
      __a				     =    torch.from_numpy(SCREAMING_SNAKE_CASE__    )
      if k != "pegasus/embeddings/position_embeddings":
         assert v.shape == state_dict[new_k].shape, f"""{new_k}, {k}, {v.shape}, {state_dict[new_k].shape}"""

   __a				     =    mapping['model.embed_positions.weight']
   __a				     =    mapping.pop('model.embed_positions.weight'    )
   __a     ,			__a				     =    torch_model.load_state_dict(SCREAMING_SNAKE_CASE__,					strict=SCREAMING_SNAKE_CASE__    )
   __a				     =    [
       k
       for k in missing
       if k
       not in [
           'final_logits_bias',
           'model.encoder.embed_tokens.weight',
           'model.decoder.embed_tokens.weight',
           'lm_head.weight',
       ]
   ]
   assert unexpected_missing == [], f"""no matches found for the following torch keys {unexpected_missing}"""
   assert extra == [], f"""no matches found for the following tf keys {extra}"""
   return torch_model



def    __UpperCAmelCase	(				SCREAMING_SNAKE_CASE__:	Tuple    )  ->			List[Any]:

   """simple docstring"""
   __a				     =    tf.train.list_variables(SCREAMING_SNAKE_CASE__    )
   __a				     =    {}
   __a				     =    ['global_step']
   for name, shape in tqdm(SCREAMING_SNAKE_CASE__,					desc='converting tf checkpoint to dict'    ):
      __a				     =    any(pat in name for pat in ignore_name    )
      if skip_key:
         continue
      __a				     =    tf.train.load_variable(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
      __a				     =    array
   return tf_weights



def    __UpperCAmelCase	(				SCREAMING_SNAKE_CASE__:	str,					SCREAMING_SNAKE_CASE__:	str,					SCREAMING_SNAKE_CASE__:	dict    )  ->			Union[str, Any]:

   """simple docstring"""
   __a				     =    get_tf_weights_as_numpy(SCREAMING_SNAKE_CASE__    )
   __a				     =    convert_bigbird_pegasus(SCREAMING_SNAKE_CASE__,					SCREAMING_SNAKE_CASE__    )
   torch_model.save_pretrained(SCREAMING_SNAKE_CASE__    )


if __name__ == "__main__":
 __UpperCamelCase      :       str      			=      argparse.ArgumentParser()
 parser.add_argument("""--tf_ckpt_path""", type=str, help="""passed to tf.train.list_variables""")
 parser.add_argument("""--save_dir""", default=None, type=str, help="""Path to the output PyTorch model.""")
 __UpperCamelCase      :       str      			=      parser.parse_args()
 __UpperCamelCase      :       Tuple      			=      {}
 convert_bigbird_pegasus_ckpt_to_pytorch(args.tf_ckpt_path, args.save_dir, config_update=config_update)







'''simple docstring'''



import datasets

from .evaluate import evaluate


__UpperCamelCase      :       int      			=      """\
@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}
}
"""

__UpperCamelCase      :       int      			=      """
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.
"""

__UpperCamelCase      :       str      			=      """
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 							__SCREAMING_SNAKE_CASE       (							datasets.Metric     ):




      def  __UpperCamelCase							(  self      )							->Optional[Any]:



         '''simple docstring'''
         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  __UpperCamelCase							(  self ,   lowerCamelCase ,   lowerCamelCase      )							->Union[str, Any]:



         '''simple docstring'''
         __a				     =    {prediction['id']: prediction['prediction_text'] for prediction in predictions}
         __a				     =    [
             {
                 'paragraphs': [
                     {
                         'qas': [
                             {
                                 'answers': [{'text': answer_text} for answer_text in ref['answers']['text']],
                                 'id': ref['id'],
                             }
                             for ref in references
                         ]
                     }
                 ]
             }
         ]
         __a				     =    evaluate(dataset=lowerCamelCase ,   predictions=lowerCamelCase      )
         return score