





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,
				)








import gc
import unittest

from diffusers import FlaxControlNetModel, FlaxStableDiffusionControlNetPipeline
from diffusers.utils import is_flax_available, load_image, slow
from diffusers.utils.testing_utils import require_flax


if is_flax_available():
    import jax
    import jax.numpy as jnp
    from flax.jax_utils import replicate
    from flax.training.common_utils import shard

@slow
@require_flax
class 		SCREAMING_SNAKE_CASE__    (	unittest.TestCase  ):


      def        a			(self	:				int				):





         """simple docstring"""



         super().tearDown()
         gc.collect()


      def        a			(self	:				Dict				):





         """simple docstring"""



         __snake_case	,     __snake_case								=       FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-canny'''    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case	,     __snake_case								=       FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''    ,      controlnet=a__    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case								=       controlnet_params

         __snake_case								=       '''bird'''
         __snake_case								=       jax.device_count()
         __snake_case								=       pipe.prepare_text_inputs([prompts] * num_samples				)

         __snake_case								=       load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'''				)
         __snake_case								=       pipe.prepare_image_inputs([canny_image] * num_samples				)

         __snake_case								=       jax.random.PRNGKey(0				)
         __snake_case								=       jax.random.split(a__    ,      jax.device_count()				)

         __snake_case								=       replicate(a__				)
         __snake_case								=       shard(a__				)
         __snake_case								=       shard(a__				)

         __snake_case								=       pipe(
             prompt_ids=a__    ,      image=a__    ,      params=a__    ,      prng_seed=a__    ,      num_inference_steps=50    ,      jit=a__    ,      ).images
         assert images.shape == (jax.device_count(), 1, 768, 512, 3)

         __snake_case								=       images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]				)
         __snake_case								=       images[0, 253:256, 253:256, -1]

         __snake_case								=       jnp.asarray(jax.device_get(image_slice.flatten()				)				)
         __snake_case								=       jnp.array(
             [0.1_6_7_9_6_9, 0.1_1_6_6_9_9, 0.0_8_1_5_4_3, 0.1_5_4_2_9_7, 0.1_3_2_8_1_2, 0.1_0_8_8_8_7, 0.1_6_9_9_2_2, 0.1_6_9_9_2_2, 0.2_0_5_0_7_8]				)
         print(f"""output_slice: {output_slice}"""				)
         assert jnp.abs(output_slice - expected_slice				).max() < 1E-2


      def        a			(self	:				Dict				):





         """simple docstring"""



         __snake_case	,     __snake_case								=       FlaxControlNetModel.from_pretrained(
             '''lllyasviel/sd-controlnet-openpose'''    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case	,     __snake_case								=       FlaxStableDiffusionControlNetPipeline.from_pretrained(
             '''runwayml/stable-diffusion-v1-5'''    ,      controlnet=a__    ,      from_pt=a__    ,      dtype=jnp.bfloataa				)
         __snake_case								=       controlnet_params

         __snake_case								=       '''Chef in the kitchen'''
         __snake_case								=       jax.device_count()
         __snake_case								=       pipe.prepare_text_inputs([prompts] * num_samples				)

         __snake_case								=       load_image(
             '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/pose.png'''				)
         __snake_case								=       pipe.prepare_image_inputs([pose_image] * num_samples				)

         __snake_case								=       jax.random.PRNGKey(0				)
         __snake_case								=       jax.random.split(a__    ,      jax.device_count()				)

         __snake_case								=       replicate(a__				)
         __snake_case								=       shard(a__				)
         __snake_case								=       shard(a__				)

         __snake_case								=       pipe(
             prompt_ids=a__    ,      image=a__    ,      params=a__    ,      prng_seed=a__    ,      num_inference_steps=50    ,      jit=a__    ,      ).images
         assert images.shape == (jax.device_count(), 1, 768, 512, 3)

         __snake_case								=       images.reshape((images.shape[0] * images.shape[1],) + images.shape[-3:]				)
         __snake_case								=       images[0, 253:256, 253:256, -1]

         __snake_case								=       jnp.asarray(jax.device_get(image_slice.flatten()				)				)
         __snake_case								=       jnp.array(
             [[0.2_7_1_4_8_4, 0.2_6_1_7_1_9, 0.2_7_5_3_9_1, 0.2_7_7_3_4_4, 0.2_7_9_2_9_7, 0.2_9_1_0_1_6, 0.2_9_4_9_2_2, 0.3_0_2_7_3_4, 0.3_0_2_7_3_4]]				)
         print(f"""output_slice: {output_slice}"""				)
         assert jnp.abs(output_slice - expected_slice				).max() < 1E-2

