





import argparse

import torch

from transformers import ImageGPTConfig, ImageGPTForCausalLM, load_tf_weights_in_imagegpt
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()






def 				_lowerCAmelCase			(	_a :       Tuple       , _a :       Optional[Any]       , _a :       Union[str, Any]      )						->      List[Any]:
     # Construct configuration depending on size
     lowerCAmelCase_       :							Dict			 =		{"""small""": (5_12, 8, 24), """medium""": (10_24, 8, 36), """large""": (15_36, 16, 48)}
     lowerCAmelCase_ ,	lowerCAmelCase_ ,	lowerCAmelCase_       :							Tuple			 =		MODELS[model_size]  # set model hyperparameters
     lowerCAmelCase_       :							Any			 =		ImageGPTConfig(n_embd=_a       , n_layer=_a       , n_head=_a      )
     lowerCAmelCase_       :							Optional[int]			 =		ImageGPTForCausalLM(_a      )

     # Load weights from numpy
     load_tf_weights_in_imagegpt(_a       , _a       , _a      )

     # Save pytorch-model
     lowerCAmelCase_       :							List[str]			 =		pytorch_dump_folder_path + """/""" + WEIGHTS_NAME
     lowerCAmelCase_       :							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__":
      UpperCAmelCase_       : Union[str, Any]							=    argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          """--imagegpt_checkpoint_path""",
          default=None,
          type=str,
          required=True,
          help="""Path to the TensorFlow checkpoint path.""",
      )
      parser.add_argument(
          """--model_size""",
          default=None,
          type=str,
          required=True,
          help="""Size of the model (can be either 'small', 'medium' or 'large').""",
      )
      parser.add_argument(
          """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
      )
      UpperCAmelCase_       : List[str]							=    parser.parse_args()
      convert_imagegpt_checkpoint_to_pytorch(
          args.imagegpt_checkpoint_path, args.model_size, args.pytorch_dump_folder_path
      )







def 				_lowerCAmelCase			(	_a :       str      )						->      int:
     lowerCAmelCase_       :							Dict			 =		0
     for plain_chr in plain_text:
          lowerCAmelCase_       :							List[Any]			 =		(
              ord(_a      ) + (hash_value << 6) + (hash_value << 16) - hash_value
          )
     return hash_value
