
"""simple docstring"""





import argparse
import os
from os.path import isfile

import torch

from transformers import GPTaConfig







def       __a	(						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase=0    ):
   # Format the message.
   if name is None:
      UpperCAmelCase_   :   str     	=       None
   else:
      UpperCAmelCase_   :   Tuple     	=       "." * max(0,						spaces - 2    ) + "# {:" + str(50 - spaces    ) + "s}"
      UpperCAmelCase_   :   Optional[int]     	=       fmt.format(__lowerCamelCase    )

   # Print and recurse (if needed).
   if isinstance(__lowerCamelCase,						__lowerCamelCase    ):
      if msg is not None:
         print(__lowerCamelCase    )
      for k in val.keys():
         recursive_print(__lowerCamelCase,						val[k],						spaces + 2    )
   elif isinstance(__lowerCamelCase,						torch.Tensor    ):
      print(__lowerCamelCase,						":",						val.size()    )
   else:
      print(__lowerCamelCase,						":",						__lowerCamelCase    )







def       __a	(						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase,						__lowerCamelCase    ):
   # Permutes layout of param tensor to [num_splits * num_heads * hidden_size, :]
   # for compatibility with later versions of NVIDIA Megatron-LM.
   # The inverse operation is performed inside Megatron-LM to read checkpoints:
   # https://github.com/NVIDIA/Megatron-LM/blob/v2.4/megatron/checkpointing.py#L209
   # If param is the weight tensor of the self-attention block, the returned tensor
   # will have to be transposed one more time to be read by HuggingFace GPT2.
   UpperCAmelCase_   :   Any     	=       param.size()
   # other versions store [num_heads * num_splits * hidden_size, :]
   UpperCAmelCase_   :   List[Any]     	=       (num_heads, num_splits, hidden_size) + input_shape[1:]
   UpperCAmelCase_   :   List[Any]     	=       param.view(*__lowerCamelCase    )
   UpperCAmelCase_   :   List[str]     	=       param.transpose(0,						1    ).contiguous()
   UpperCAmelCase_   :   Union[str, Any]     	=       param.view(*__lowerCamelCase    )
   return param







def       __a	(						__lowerCamelCase,						__lowerCamelCase    ):
   UpperCAmelCase_   :   Dict     	=       config.n_positions
   UpperCAmelCase_   :   Optional[int]     	=       config.n_layer
   UpperCAmelCase_   :   Any     	=       config.vocab_size
   UpperCAmelCase_   :   Optional[Any]     	=       config.n_head
   UpperCAmelCase_   :   Optional[int]     	=       config.n_embd // config.n_head

   UpperCAmelCase_   :   Union[str, Any]     	=       sd_megatron["model.language_model.embedding.word_embeddings.weight"][:vocab_size, :]
   UpperCAmelCase_   :   Union[str, Any]     	=       {
       "transformer.wte.weight": word_embeddings,
       "transformer.wpe.weight": sd_megatron["model.language_model.embedding.position_embeddings.weight"],
       "transformer.ln_f.weight": sd_megatron["model.language_model.encoder.final_layernorm.weight"],
       "transformer.ln_f.bias": sd_megatron["model.language_model.encoder.final_layernorm.bias"],
   }

   UpperCAmelCase_   :   Union[str, Any]     	=       "model.language_model.encoder.layers."
   for i in range(__lowerCamelCase    ):
      UpperCAmelCase_   :   Tuple     	=       torch.tril(torch.ones((n_positions, n_positions),						dtype=torch.bool    )    )
      UpperCAmelCase_   :   Optional[int]     	=       causal_mask.view(1,						1,						__lowerCamelCase,						__lowerCamelCase    )
      UpperCAmelCase_   :   Any     	=       causal_mask
      UpperCAmelCase_   :   List[Any]     	=       torch.tensor(-1E4,						dtype=torch.bfloataa    )

      UpperCAmelCase_   :   List[str]     	=       sd_megatron[f"""{pf}{i}.input_layernorm.weight"""]
      UpperCAmelCase_   :   Union[str, Any]     	=       sd_megatron[f"""{pf}{i}.input_layernorm.bias"""]

      UpperCAmelCase_   :   List[Any]     	=       sd_megatron[f"""{pf}{i}.self_attention.query_key_value.weight"""]
      UpperCAmelCase_   :   List[Any]     	=       fix_query_key_value_ordering(__lowerCamelCase,						3,						__lowerCamelCase,						__lowerCamelCase    )
      UpperCAmelCase_   :   Optional[Any]     	=       vala.transpose(0,						1    ).contiguous()

      UpperCAmelCase_   :   Any     	=       sd_megatron[f"""{pf}{i}.self_attention.query_key_value.bias"""]
      UpperCAmelCase_   :   Dict     	=       fix_query_key_value_ordering(__lowerCamelCase,						3,						__lowerCamelCase,						__lowerCamelCase    )
      UpperCAmelCase_   :   List[str]     	=       vala

      UpperCAmelCase_   :   List[Any]     	=       sd_megatron[f"""{pf}{i}.self_attention.dense.weight"""].transpose(
          0,						1    )
      UpperCAmelCase_   :   Union[str, Any]     	=       sd_megatron[f"""{pf}{i}.self_attention.dense.bias"""]
      UpperCAmelCase_   :   str     	=       sd_megatron[f"""{pf}{i}.post_attention_layernorm.weight"""]
      UpperCAmelCase_   :   Union[str, Any]     	=       sd_megatron[f"""{pf}{i}.post_attention_layernorm.bias"""]
      UpperCAmelCase_   :   List[Any]     	=       sd_megatron[f"""{pf}{i}.mlp.dense_h_to_4h.weight"""].transpose(0,						1    )
      UpperCAmelCase_   :   Any     	=       sd_megatron[f"""{pf}{i}.mlp.dense_h_to_4h.bias"""]
      UpperCAmelCase_   :   int     	=       sd_megatron[f"""{pf}{i}.mlp.dense_4h_to_h.weight"""].transpose(
          0,						1    )
      UpperCAmelCase_   :   Optional[Any]     	=       sd_megatron[f"""{pf}{i}.mlp.dense_4h_to_h.bias"""]

   # For LM head, transformers' wants the matrix to weight embeddings.
   UpperCAmelCase_   :   Tuple     	=       word_embeddings

   return sd_hf







def       __a	(						__lowerCamelCase,						__lowerCamelCase    ):
   UpperCAmelCase_   :   int     	=       6_4000
   UpperCAmelCase_   :   List[Any]     	=       config_megatron["encoder_seq_length"]
   UpperCAmelCase_   :   Dict     	=       config_megatron["hidden_size"]
   UpperCAmelCase_   :   List[Any]     	=       config_megatron["num_layers"]
   UpperCAmelCase_   :   Optional[Any]     	=       config_megatron["num_attention_heads"]
   UpperCAmelCase_   :   Optional[Any]     	=       config_megatron["ffn_hidden_size"]
   UpperCAmelCase_   :   List[str]     	=       "gelu"
   UpperCAmelCase_   :   Tuple     	=       0.1
   UpperCAmelCase_   :   Any     	=       0.1
   UpperCAmelCase_   :   Dict     	=       0.1
   UpperCAmelCase_   :   Optional[Any]     	=       config_megatron["layernorm_epsilon"]  # 1e-5
   UpperCAmelCase_   :   Dict     	=       config_megatron["init_method_std"]  # 0.02
   UpperCAmelCase_   :   Optional[int]     	=       config_megatron["apply_query_key_layer_scaling"]  # True
   UpperCAmelCase_   :   List[Any]     	=       True
   UpperCAmelCase_   :   str     	=       True

   # This identifies the 6.7B (7B) model which uses a different tokenizer
   if config_megatron["hidden_size"] == 4096:
      UpperCAmelCase_   :   Optional[int]     	=       1  # <|endoftext|>
      UpperCAmelCase_   :   Optional[Any]     	=       1  # <|endoftext|>
      UpperCAmelCase_   :   List[Any]     	=       0  # <unk>
   else:
      UpperCAmelCase_   :   str     	=       2  # <s>
      UpperCAmelCase_   :   List[str]     	=       3  # <|endoftext|>
      UpperCAmelCase_   :   List[Any]     	=       0  # <pad>

   return config_hf







def       __a	(						__lowerCamelCase    ):
   print(__lowerCamelCase    )

   UpperCAmelCase_   :   Optional[int]     	=       args.checkpoint_path
   UpperCAmelCase_   :   Dict     	=       args.save_path
   if isfile(__lowerCamelCase    ):
      raise FileNotFoundError(f"""ERROR! could not find file {checkpoint_path}"""    )

   # Load the model.
   UpperCAmelCase_   :   List[Any]     	=       torch.load(__lowerCamelCase,						map_location="cpu"    )

   # Load the config.
   UpperCAmelCase_   :   List[str]     	=       checkpoint["hyper_parameters"]["cfg"]
   UpperCAmelCase_   :   str     	=       GPTaConfig()
   UpperCAmelCase_   :   Tuple     	=       copy_config(config_hf=__lowerCamelCase,						config_megatron=__lowerCamelCase    )
   UpperCAmelCase_   :   List[str]     	=       ["GPT2LMHeadModel"]

   UpperCAmelCase_   :   int     	=       checkpoint["state_dict"]

   # Convert.
   print("Converting"    )
   UpperCAmelCase_   :   Optional[Any]     	=       convert_megatron_checkpoint(__lowerCamelCase,						__lowerCamelCase    )

   # Print the structure of converted state dict.
   if args.print_checkpoint_structure:
      recursive_print(__lowerCamelCase,						__lowerCamelCase    )

   UpperCAmelCase_   :   Tuple     	=       "GPTSw3Tokenizer"

   # Store the config to file.
   print("Saving config"    )
   config_hf.save_pretrained(__lowerCamelCase    )

   # Store the state_dict to file.
   UpperCAmelCase_   :   Tuple     	=       os.path.join(__lowerCamelCase,						"pytorch_model.bin"    )
   print(f"""Saving checkpoint to \"{output_checkpoint_file}\""""    )
   torch.save(__lowerCamelCase,						__lowerCamelCase    )


if __name__ == "__main__":
    _a									=				argparse.ArgumentParser()
    parser.add_argument(
        '--checkpoint_path',
        type=str,
        required=True,
        help='e.g. megatron_gpt--val_loss=2.42-step=38000-consumed_samples=54720000',
    )
    parser.add_argument('--save_path', type=str, required=True, help='e.g. /home/user/gpt-sw3/hf')
    parser.add_argument('--print-checkpoint-structure', action='store_true')
    _a									=				parser.parse_args()
    main(_args)








import argparse
import json
import re
import string
import sys

import numpy as np


UpperCAmelCase_   							=      0.5




def    lowerCamelCase__			(		A__			:   Optional[Any]    ,     A__			:   Union[str, Any]      ):





       '''simple docstring'''



       __lowerCamelCase  =		[""".""", """,""", """;""", """:"""]

       for token in remove_tokens:
              __lowerCamelCase  =		ground_truth.replace(A__    ,     """"""      )
              __lowerCamelCase  =		prediction.replace(A__    ,     """"""      )

       __lowerCamelCase,  __lowerCamelCase  =		ground_truth.lower(), prediction.lower()
       __lowerCamelCase,  __lowerCamelCase  =		ground_truth.replace("""/"""    ,     """ """      ), prediction.replace("""/"""    ,     """ """      )
       __lowerCamelCase,  __lowerCamelCase  =		set(ground_truth.split(""" """      )      ), set(prediction.split(""" """      )      )

       __lowerCamelCase  =		ground_truth.intersection(A__      )
       __lowerCamelCase  =		ground_truth.union(A__      )
       __lowerCamelCase  =		len(A__      ) / len(A__      )
       return jaccard




def    lowerCamelCase__			(		A__			:   Optional[Any]      ):





       '''simple docstring'''



       def remove_articles(A__			:   Tuple      ):
              return re.sub(R"""\b(a|an|the)\b"""    ,     """ """    ,     A__      )

       def white_space_fix(A__			:   Optional[Any]      ):
              return " ".join(text.split()      )

       def remove_punc(A__			:   Optional[Any]      ):
              __lowerCamelCase  =		set(string.punctuation      )
              return "".join(ch for ch in text if ch not in exclude      )

       def lower(A__			:   Tuple      ):
              return text.lower()

       return white_space_fix(remove_articles(remove_punc(lower(A__      )      )      )      )




def    lowerCamelCase__			(		A__			:   Any    ,     A__			:   Optional[Any]    ,     A__			:   Optional[Any]      ):





       '''simple docstring'''



       __lowerCamelCase,  __lowerCamelCase,  __lowerCamelCase  =		0, 0, 0

       __lowerCamelCase  =		"""Parties""" in qa_id

       # first check if ground truth is empty
       if len(A__      ) == 0:
              if len(A__      ) > 0:
                     fp += len(A__      )  # false positive for each one
       else:
              for ground_truth in ground_truths:
                     assert len(A__      ) > 0
                     # check if there is a match
                     __lowerCamelCase  =		False
                     for pred in predictions:
                            if substr_ok:
                                   __lowerCamelCase  =		get_jaccard(A__    ,     A__      ) >= IOU_THRESH or ground_truth in pred
                            else:
                                   __lowerCamelCase  =		get_jaccard(A__    ,     A__      ) >= IOU_THRESH
                            if is_match:
                                   __lowerCamelCase  =		True

                     if match_found:
                            tp += 1
                     else:
                            fn += 1

        # now also get any fps by looping through preds
              for pred in predictions:
                     # Check if there's a match. if so, don't count (don't want to double count based on the above)
                     # but if there's no match, then this is a false positive.
                     # (Note: we get the true positives in the above loop instead of this loop so that we don't double count
                     # multiple predictions that are matched with the same answer.)
                     __lowerCamelCase  =		False
                     for ground_truth in ground_truths:
                            assert len(A__      ) > 0
                            if substr_ok:
                                   __lowerCamelCase  =		get_jaccard(A__    ,     A__      ) >= IOU_THRESH or ground_truth in pred
                            else:
                                   __lowerCamelCase  =		get_jaccard(A__    ,     A__      ) >= IOU_THRESH
                            if is_match:
                                   __lowerCamelCase  =		True

                     if not match_found:
                            fp += 1

       __lowerCamelCase  =		tp / (tp + fp) if tp + fp > 0 else np.nan
       __lowerCamelCase  =		tp / (tp + fn) if tp + fn > 0 else np.nan

       return precision, recall




def    lowerCamelCase__			(		A__			:   int      ):





       '''simple docstring'''



       __lowerCamelCase  =		precisions[::-1]
       for i in range(1    ,     len(A__      )      ):
              __lowerCamelCase  =		max(precision_best[i - 1]    ,     precision_best[i]      )
       __lowerCamelCase  =		precision_best[::-1]
       return precisions




def    lowerCamelCase__			(		A__			:   str    ,     A__			:   Union[str, Any]      ):





       '''simple docstring'''



       __lowerCamelCase  =		process_precisions(A__      )
       __lowerCamelCase  =		np.trapz(A__    ,     A__      )
       if np.isnan(A__      ):
              return 0
       return aupr




def    lowerCamelCase__			(		A__			:   Optional[Any]    ,     A__			:   List[str]    ,     A__			:   int      ):





       '''simple docstring'''



       __lowerCamelCase  =		process_precisions(A__      )
       __lowerCamelCase  =		0
       for prec, recall in zip(A__    ,     A__      ):
              if recall >= recall_thresh:
                     __lowerCamelCase  =		prec
                     break
       return prec_at_recall




def    lowerCamelCase__			(		A__			:   Optional[Any]    ,     A__			:   List[Any]      ):





       '''simple docstring'''



       return normalize_answer(A__      ) == normalize_answer(A__      )




def    lowerCamelCase__			(		A__			:   Tuple    ,     A__			:   int    ,     A__			:   Dict      ):





       '''simple docstring'''



       __lowerCamelCase  =		0
       for pred in predictions:
              for ground_truth in ground_truths:
                     __lowerCamelCase  =		metric_fn(A__    ,     A__      )
                     if score == 1:  # break the loop when one prediction matches the ground truth
                            break
              if score == 1:
                     break
       return score




def    lowerCamelCase__			(		A__			:   Union[str, Any]    ,     A__			:   Optional[Any]      ):





       '''simple docstring'''



       __lowerCamelCase  =		__lowerCamelCase  =		__lowerCamelCase  =		0
       __lowerCamelCase  =		[]
       __lowerCamelCase  =		[]
       for article in dataset:
              for paragraph in article["paragraphs"]:
                     for qa in paragraph["qas"]:
                            total += 1
                            if qa["id"] not in predictions:
                                   __lowerCamelCase  =		"""Unanswered question """ + qa["""id"""] + """ will receive score 0."""
                                   print(A__    ,     file=sys.stderr      )
                                   continue
                            __lowerCamelCase  =		[x["""text"""] for x in qa["""answers"""]]
                            __lowerCamelCase  =		predictions[qa["""id"""]]
                            __lowerCamelCase,  __lowerCamelCase  =		compute_precision_recall(A__    ,     A__    ,     qa["""id"""]      )

                            precisions.append(A__      )
                            recalls.append(A__      )

                            if precision == 0 and recall == 0:
                                   fa += 0
                            else:
                                   fa += 2 * (precision * recall) / (precision + recall)

                            exact_match += metric_max_over_ground_truths(A__    ,     A__    ,     A__      )

       __lowerCamelCase  =		[x for _, x in sorted(zip(A__    ,     A__      )      )]
       recalls.sort()

       __lowerCamelCase  =		100.0 * fa / total
       __lowerCamelCase  =		100.0 * exact_match / total
       __lowerCamelCase  =		get_aupr(A__    ,     A__      )

       __lowerCamelCase  =		get_prec_at_recall(A__    ,     A__    ,     recall_thresh=0.9      )
       __lowerCamelCase  =		get_prec_at_recall(A__    ,     A__    ,     recall_thresh=0.8      )

       return {
           "exact_match": exact_match,
           "f1": fa,
           "aupr": aupr,
           "prec_at_80_recall": prec_at_aa_recall,
           "prec_at_90_recall": prec_at_aa_recall,
       }


if __name__ == "__main__":
       UpperCAmelCase_   							=      argparse.ArgumentParser(description='Evaluation for CUAD')
       parser.add_argument('dataset_file', help='Dataset file')
       parser.add_argument('prediction_file', help='Prediction File')
       UpperCAmelCase_   							=      parser.parse_args()
       with open(args.dataset_file) as dataset_file:
              UpperCAmelCase_   							=      json.load(dataset_file)
              UpperCAmelCase_   							=      dataset_json['data']
       with open(args.prediction_file) as prediction_file:
              UpperCAmelCase_   							=      json.load(prediction_file)
       print(json.dumps(evaluate(dataset, predictions)))

