





import argparse

import torch

from transformers import GPTaConfig, GPTaModel, load_tf_weights_in_gpta
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()





def      lowerCAmelCase_ (       __a	,    __a	,    __a		)     ->		List[str]:







  """simple docstring"""





  if gpta_config_file == "":
    lowerCamelCase__:      Optional[Any]			  =GPTaConfig()
  else:
    lowerCamelCase__:      Optional[Any]			  =GPTaConfig.from_json_file(__a		)
  lowerCamelCase__:      Any			  =GPTaModel(__a		)

  # Load weights from numpy
  load_tf_weights_in_gpta(__a	,    __a	,    __a		)

  # Save pytorch-model
  lowerCamelCase__:      Any			  =pytorch_dump_folder_path + "/" + WEIGHTS_NAME
  lowerCamelCase__:      Optional[Any]			  =pytorch_dump_folder_path + "/" + CONFIG_NAME
  print(F"""Save PyTorch model to {pytorch_weights_dump_path}"""		)
  torch.save(model.state_dict()	,    __a		)
  print(F"""Save configuration file to {pytorch_config_dump_path}"""		)
  with open(__a	,    "w"	,    encoding="utf-8"		) as f:
    f.write(config.to_json_string()		)


if __name__ == "__main__":
     __A									=				argparse.ArgumentParser()
     # Required parameters
     parser.add_argument(
         "--gpt2_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
     )
     parser.add_argument(
         "--pytorch_dump_folder_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
     )
     parser.add_argument(
         "--gpt2_config_file",
         default="",
         type=str,
         help=(
             "An optional config json file corresponding to the pre-trained OpenAI model. \n"
             "This specifies the model architecture."
         ),
     )
     __A									=				parser.parse_args()
     convert_gpta_checkpoint_to_pytorch(args.gpta_checkpoint_path, args.gpta_config_file, args.pytorch_dump_folder_path)





import re

from flax.core.frozen_dict import freeze
from flax.traverse_util import flatten_dict, unflatten_dict
from jax.experimental import PartitionSpec as P


# Sentinels
lowerCAmelCase__      		= object()

# For specifying empty leaf dict `{}`
lowerCAmelCase__      		= object()
def 							_UpperCAmelCase				(UpperCamelCase__     :  str				,							UpperCamelCase__     :  Union[str, Any]						):
       _A      :       str				    = tuple((re.compile(x + "$"						) for x in qs)						)
       for i in range(len(UpperCamelCase__						) - len(UpperCamelCase__						) + 1						):
              _A      :       Tuple				    = [x.match(UpperCamelCase__						) for x, y in zip(UpperCamelCase__				,							ks[i:]						)]
              if matches and all(UpperCamelCase__						):
                     return True
       return False
def 							_UpperCAmelCase				(UpperCamelCase__     :  str						):
       def replace(UpperCamelCase__     :  List[str]				,							UpperCamelCase__     :  Optional[int]						):
              for rule, replacement in rules:
                     if _match(UpperCamelCase__				,							UpperCamelCase__						):
                            return replacement
              return val

       return replace
def 							_UpperCAmelCase				():
       return [
           # embeddings
           (("transformer", "wpe", "embedding"), P("mp"				,							UpperCamelCase__						)),
           (("transformer", "wte", "embedding"), P("mp"				,							UpperCamelCase__						)),
           # atention
           (("attention", "(q_proj|k_proj|v_proj)", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("attention", "out_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("attention", "out_proj", "bias"), None),
           # mlp
           (("mlp", "c_fc", "kernel"), P(UpperCamelCase__				,							"mp"						)),
           (("mlp", "c_fc", "bias"), P("mp"						)),
           (("mlp", "c_proj", "kernel"), P("mp"				,							UpperCamelCase__						)),
           (("mlp", "c_proj", "bias"), None),
           # layer norms
           ((r"ln_\d+", "bias"), None),
           ((r"\d+", r"ln_\d+", "scale"), None),
           (("ln_f", "bias"), None),
           (("ln_f", "scale"), None),
       ]






def 							_UpperCAmelCase				(UpperCamelCase__     :  List[str]						):
       _A      :       int				    = _get_partition_rules()
       _A      :       Optional[int]				    = _replacement_rules(UpperCamelCase__						)
       _A      :       Optional[int]				    = {k: _unmatched for k in flatten_dict(UpperCamelCase__						)}
       _A      :       List[str]				    = {k: replace(UpperCamelCase__				,							UpperCamelCase__						) for k, v in initd.items()}
       assert _unmatched not in result.values(), "Incomplete partition spec."
       return freeze(unflatten_dict(UpperCamelCase__						)						)
