





import colorsys

from PIL import Image  # type: ignore






def lowerCamelCase__   (							snake_case_     :  float	,				snake_case_     :  float	,				snake_case_     :  int	)				->    float:
   __snake_case								=       x
   __snake_case								=       y
   for step in range(lowercase__	):  # noqa: B007
      __snake_case								=       a * a - b * b + x
      __snake_case								=       2 * a * b + y
      __snake_case								=       a_new

      # divergence happens for all complex number with an absolute value
      # greater than 4
      if a * a + b * b > 4:
         break
   return step / (max_step - 1)






def lowerCamelCase__   (							snake_case_     :  float	)				->    tuple:





   if distance == 1:
      return (0, 0, 0)
   else:
      return (255, 255, 255)






def lowerCamelCase__   (							snake_case_     :  float	)				->    tuple:





   if distance == 1:
      return (0, 0, 0)
   else:
      return tuple(round(i * 255	) for i in colorsys.hsv_to_rgb(lowercase__	,				1	,				1	)	)






def lowerCamelCase__   (							snake_case_     :  int = 800	,				snake_case_     :  int = 600	,				snake_case_     :  float = -0.6	,				snake_case_     :  float = 0	,				snake_case_     :  float = 3.2	,				snake_case_     :  int = 50	,				snake_case_     :  bool = True	,				)				->    Image.Image:
   __snake_case								=       Image.new('''RGB'''	,				(image_width, image_height)	)
   __snake_case								=       img.load()

   # loop through the image-coordinates
   for image_x in range(lowercase__	):
      for image_y in range(lowercase__	):
         # determine the figure-coordinates based on the image-coordinates
         __snake_case								=       figure_width / image_width * image_height
         __snake_case								=       figure_center_x + (image_x / image_width - 0.5) * figure_width
         __snake_case								=       figure_center_y + (image_y / image_height - 0.5) * figure_height

         __snake_case								=       get_distance(lowercase__	,				lowercase__	,				lowercase__	)

         # color the corresponding pixel based on the selected coloring-function
         if use_distance_color_coding:
            __snake_case								=       get_color_coded_rgb(lowercase__	)
         else:
            __snake_case								=       get_black_and_white_rgb(lowercase__	)

   return img


if __name__ == "__main__":
      import doctest

      doctest.testmod()

      # colored version, full figure
      snake_case_           =      get_image()

      # uncomment for colored version, different section, zoomed in
      # img = get_image(figure_center_x = -0.6, figure_center_y = -0.4,
      # figure_width = 0.8)

      # uncomment for black and white version, full figure
      # img = get_image(use_distance_color_coding = False)

      # uncomment to save the image
      # img.save("mandelbrot.png")

      img.show()








import argparse
import os
import pickle
import sys

import torch

from transformers import TransfoXLConfig, TransfoXLLMHeadModel, load_tf_weights_in_transfo_xl
from transformers.models.transfo_xl import tokenization_transfo_xl as data_utils
from transformers.models.transfo_xl.tokenization_transfo_xl import CORPUS_NAME, VOCAB_FILES_NAMES
from transformers.utils import CONFIG_NAME, WEIGHTS_NAME, logging


logging.set_verbosity_info()

# We do this to be able to load python 2 datasets pickles
# See e.g. https://stackoverflow.com/questions/2121874/python-pickling-after-changing-a-modules-directory/2121918#2121918
snake_case_           =      data_utils.TransfoXLTokenizer
snake_case_           =      data_utils.TransfoXLCorpus
snake_case_           =      data_utils
snake_case_           =      data_utils






def lowerCamelCase__   (							snake_case_     :  Union[str, Any]	,				snake_case_     :  int	,				snake_case_     :  List[Any]	,				snake_case_     :  int	)				->    Dict:
   if transfo_xl_dataset_file:
      # Convert a pre-processed corpus (see original TensorFlow repo)
      with open(snake_case_	,				'''rb'''	) as fp:
         __snake_case								=       pickle.load(snake_case_	,				encoding='''latin1'''	)
      # Save vocabulary and dataset cache as Dictionaries (should be better than pickles for the long-term)
      __snake_case								=       pytorch_dump_folder_path + '''/''' + VOCAB_FILES_NAMES['''pretrained_vocab_file''']
      print(f"""Save vocabulary to {pytorch_vocab_dump_path}"""	)
      __snake_case								=       corpus.vocab.__dict__
      torch.save(snake_case_	,				snake_case_	)

      __snake_case								=       corpus.__dict__
      corpus_dict_no_vocab.pop('''vocab'''	,				snake_case_	)
      __snake_case								=       pytorch_dump_folder_path + '''/''' + CORPUS_NAME
      print(f"""Save dataset to {pytorch_dataset_dump_path}"""	)
      torch.save(snake_case_	,				snake_case_	)

   if tf_checkpoint_path:
      # Convert a pre-trained TensorFlow model
      __snake_case								=       os.path.abspath(snake_case_	)
      __snake_case								=       os.path.abspath(snake_case_	)

      print(f"""Converting Transformer XL checkpoint from {tf_path} with config at {config_path}."""	)
      # Initialise PyTorch model
      if transfo_xl_config_file == "":
         __snake_case								=       TransfoXLConfig()
      else:
         __snake_case								=       TransfoXLConfig.from_json_file(snake_case_	)
      print(f"""Building PyTorch model from configuration: {config}"""	)
      __snake_case								=       TransfoXLLMHeadModel(snake_case_	)

      __snake_case								=       load_tf_weights_in_transfo_xl(snake_case_	,				snake_case_	,				snake_case_	)
      # Save pytorch-model
      __snake_case								=       os.path.join(snake_case_	,				snake_case_	)
      __snake_case								=       os.path.join(snake_case_	,				snake_case_	)
      print(f"""Save PyTorch model to {os.path.abspath(snake_case_	)}"""	)
      torch.save(model.state_dict()	,				snake_case_	)
      print(f"""Save configuration file to {os.path.abspath(snake_case_	)}"""	)
      with open(snake_case_	,				'''w'''	,				encoding='''utf-8'''	) as f:
         f.write(config.to_json_string()	)


if __name__ == "__main__":
    snake_case_           =      argparse.ArgumentParser()
    parser.add_argument(
        '--pytorch_dump_folder_path',
        default=None,
        type=str,
        required=True,
        help='Path to the folder to store the PyTorch model or dataset/vocab.',
    )
    parser.add_argument(
        '--tf_checkpoint_path',
        default='',
        type=str,
        help='An optional path to a TensorFlow checkpoint path to be converted.',
    )
    parser.add_argument(
        '--transfo_xl_config_file',
        default='',
        type=str,
        help=(
            'An optional config json file corresponding to the pre-trained BERT model. \n'
            'This specifies the model architecture.'
        ),
    )
    parser.add_argument(
        '--transfo_xl_dataset_file',
        default='',
        type=str,
        help='An optional dataset file to be converted in a vocabulary.',
    )
    snake_case_           =      parser.parse_args()
    convert_transfo_xl_checkpoint_to_pytorch(
        args.tf_checkpoint_path,
        args.transfo_xl_config_file,
        args.pytorch_dump_folder_path,
        args.transfo_xl_dataset_file,
    )

