





import math
from timeit import timeit


def 			lowerCamelCase_		(					lowerCAmelCase:		int )->						int:
  _snake_case      : Union[str, Any]														=		0
  _snake_case      : Tuple														=		abs(lowerCAmelCase )
  while True:
    _snake_case      : Union[str, Any]														=		n // 10
    digits += 1
    if n == 0:
      break
  return digits


def 			lowerCamelCase_		(					lowerCAmelCase:		int )->						int:
  return 1 if n == 0 else math.floor(math.log(abs(lowerCAmelCase )			,							10 ) + 1 )


def 			lowerCamelCase_		(					lowerCAmelCase:		int )->						int:
  return len(str(abs(lowerCAmelCase ) ) )


def 			lowerCamelCase_		(					)->						None:
  from collections.abc import Callable

  def benchmark_a_function(lowerCAmelCase:		Callable			,							lowerCAmelCase:		int ) -> None:
    _snake_case      : List[Any]														=		F"""{func.__name__}({value})"""
    _snake_case      : str														=		timeit(F"""__main__.{call}"""			,							setup='import __main__' )
    print(F"""{call}: {func(lowerCAmelCase )} -- {timing} seconds""" )

  for value in (26_21_44, 11_25_89_99_06_84_26_24, 1_26_76_50_60_02_28_22_94_01_49_67_03_20_53_76):
    for func in (num_digits, num_digits_fast, num_digits_faster):
      benchmark_a_function(lowerCAmelCase			,							lowerCAmelCase )
    print()


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      benchmark()











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_		(					lowerCAmelCase:		Optional[Any]			,							lowerCAmelCase:		Tuple			,							lowerCAmelCase:		List[Any] )->						Dict:
  # Construct configuration depending on size
  _snake_case      : Tuple														=		{'small': (5_12, 8, 24), 'medium': (10_24, 8, 36), 'large': (15_36, 16, 48)}
  _snake_case							,    _snake_case							,    _snake_case      : Optional[Any]														=		MODELS[model_size]  # set model hyperparameters
  _snake_case      : Union[str, Any]														=		ImageGPTConfig(n_embd=lowerCAmelCase			,							n_layer=lowerCAmelCase			,							n_head=lowerCAmelCase )
  _snake_case      : int														=		ImageGPTForCausalLM(lowerCAmelCase )

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

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


if __name__ == "__main__":
      lowerCAmelCase_        =			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."""
      )
      lowerCAmelCase_        =			parser.parse_args()
      convert_imagegpt_checkpoint_to_pytorch(
          args.imagegpt_checkpoint_path, args.model_size, args.pytorch_dump_folder_path
      )




