





"""simple docstring"""






from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand

def      __A   (      a_    :Dict)		->		List[str]:
				return ConvertCommand(
				    args.model_type ,							args.tf_checkpoint ,							args.pytorch_dump_output ,							args.config ,							args.finetuning_task_name)


A			      =	"""
transformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires
TensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.
"""






class 					__lowercase      (      a_				):







					'''simple docstring'''




					@staticmethod
					def 			_lowerCamelCase		(    _UpperCAmelCase  ):
									__a			:    int      							=	parser.add_parser(
									    '''convert'''						,      help='''CLI tool to run convert model from original author checkpoints to Transformers PyTorch checkpoints.'''						,      )
									train_parser.add_argument('''--model_type'''						,      type=_lowercase						,      required=_lowercase						,      help='''Model\'s type.'''  )
									train_parser.add_argument(
									    '''--tf_checkpoint'''						,      type=_lowercase						,      required=_lowercase						,      help='''TensorFlow checkpoint path or folder.'''  )
									train_parser.add_argument(
									    '''--pytorch_dump_output'''						,      type=_lowercase						,      required=_lowercase						,      help='''Path to the PyTorch saved model output.'''  )
									train_parser.add_argument('''--config'''						,      type=_lowercase						,      default=''''''						,      help='''Configuration file path or folder.'''  )
									train_parser.add_argument(
									    '''--finetuning_task_name'''						,      type=_lowercase						,      default=_lowercase						,      help='''Optional fine-tuning task name if the TF model was a finetuned model.'''						,      )
									train_parser.set_defaults(func=_lowercase  )







					def __init__(    self						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase						,      _UpperCAmelCase						,      *_UpperCAmelCase						,      ):
									__a			:    List[str]      							=	logging.get_logger('''transformers-cli/converting'''  )

									self._logger.info(f"""Loading model {model_type}"""  )
									__a			:    Any      							=	model_type
									__a			:    List[Any]      							=	tf_checkpoint
									__a			:    str      							=	pytorch_dump_output
									__a			:    Dict      							=	config
									__a			:    Union[str, Any]      							=	finetuning_task_name







					def 			_lowerCamelCase		(    self  ):
									if self._model_type == "albert":
													try:
																	from ..models.albert.convert_albert_original_tf_checkpoint_to_pytorch import (
																	    convert_tf_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_tf_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "bert":
													try:
																	from ..models.bert.convert_bert_original_tf_checkpoint_to_pytorch import (
																	    convert_tf_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_tf_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "funnel":
													try:
																	from ..models.funnel.convert_funnel_original_tf_checkpoint_to_pytorch import (
																	    convert_tf_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_tf_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "t5":
													try:
																	from ..models.ta.convert_ta_original_tf_checkpoint_to_pytorch import convert_tf_checkpoint_to_pytorch
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_tf_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "gpt":
													from ..models.openai.convert_openai_original_tf_checkpoint_to_pytorch import (
													    convert_openai_checkpoint_to_pytorch,
													)

													convert_openai_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "transfo_xl":
													try:
																	from ..models.transfo_xl.convert_transfo_xl_original_tf_checkpoint_to_pytorch import (
																	    convert_transfo_xl_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													if "ckpt" in self._tf_checkpoint.lower():
																	__a			:    Tuple      							=	self._tf_checkpoint
																	__a			:    str      							=	""""""
													else:
																	__a			:    Optional[int]      							=	self._tf_checkpoint
																	__a			:    int      							=	""""""
													convert_transfo_xl_checkpoint_to_pytorch(
													    _lowercase						,      self._config						,      self._pytorch_dump_output						,      _lowercase  )
									elif self._model_type == "gpt2":
													try:
																	from ..models.gpta.convert_gpta_original_tf_checkpoint_to_pytorch import (
																	    convert_gpta_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_gpta_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									elif self._model_type == "xlnet":
													try:
																	from ..models.xlnet.convert_xlnet_original_tf_checkpoint_to_pytorch import (
																	    convert_xlnet_checkpoint_to_pytorch,
																	)
													except ImportError:
																	raise ImportError(_lowercase  )

													convert_xlnet_checkpoint_to_pytorch(
													    self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output						,      self._finetuning_task_name  )
									elif self._model_type == "xlm":
													from ..models.xlm.convert_xlm_original_pytorch_checkpoint_to_pytorch import (
													    convert_xlm_checkpoint_to_pytorch,
													)

													convert_xlm_checkpoint_to_pytorch(self._tf_checkpoint						,      self._pytorch_dump_output  )
									elif self._model_type == "lxmert":
													from ..models.lxmert.convert_lxmert_original_tf_checkpoint_to_pytorch import (
													    convert_lxmert_checkpoint_to_pytorch,
													)

													convert_lxmert_checkpoint_to_pytorch(self._tf_checkpoint						,      self._pytorch_dump_output  )
									elif self._model_type == "rembert":
													from ..models.rembert.convert_rembert_tf_checkpoint_to_pytorch import (
													    convert_rembert_tf_checkpoint_to_pytorch,
													)

													convert_rembert_tf_checkpoint_to_pytorch(self._tf_checkpoint						,      self._config						,      self._pytorch_dump_output  )
									else:
													raise ValueError(
													    '''--model_type should be selected in the list [bert, gpt, gpt2, t5, transfo_xl, xlnet, xlm, lxmert]'''  )


import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class        A__(a_,  a_,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				Optional[Any]       					=			StableDiffusionSAGPipeline
						_A    :				Any       					=			TEXT_TO_IMAGE_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_BATCH_PARAMS
						_A    :				Dict       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				int       					=			TEXT_TO_IMAGE_IMAGE_PARAMS
						_A    :				Optional[int]       					=			False





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									torch.manual_seed(0    )
									a_						:						Tuple 							= UNetaDConditionModel(
									    block_out_channels=(32, 64)  ,      layers_per_block=2  ,      sample_size=32  ,      in_channels=4  ,      out_channels=4  ,      down_block_types=("""DownBlock2D""", """CrossAttnDownBlock2D""")  ,      up_block_types=("""CrossAttnUpBlock2D""", """UpBlock2D""")  ,      cross_attention_dim=32  ,      )
									a_						:						int 							= DDIMScheduler(
									    beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule="""scaled_linear"""  ,      clip_sample=_lowercase  ,      set_alpha_to_one=_lowercase  ,      )
									torch.manual_seed(0    )
									a_						:						Tuple 							= AutoencoderKL(
									    block_out_channels=[32, 64]  ,      in_channels=3  ,      out_channels=3  ,      down_block_types=["""DownEncoderBlock2D""", """DownEncoderBlock2D"""]  ,      up_block_types=["""UpDecoderBlock2D""", """UpDecoderBlock2D"""]  ,      latent_channels=4  ,      )
									torch.manual_seed(0    )
									a_						:						str 							= CLIPTextConfig(
									    bos_token_id=0  ,      eos_token_id=2  ,      hidden_size=32  ,      intermediate_size=37  ,      layer_norm_eps=1e-05  ,      num_attention_heads=4  ,      num_hidden_layers=5  ,      pad_token_id=1  ,      vocab_size=1_000  ,      )
									a_						:						Union[str, Any] 							= CLIPTextModel(_lowercase    )
									a_						:						int 							= CLIPTokenizer.from_pretrained("""hf-internal-testing/tiny-random-clip"""    )

									a_						:						Union[str, Any] 							= {
									    """unet""": unet,
									    """scheduler""": scheduler,
									    """vae""": vae,
									    """text_encoder""": text_encoder,
									    """tokenizer""": tokenizer,
									    """safety_checker""": None,
									    """feature_extractor""": None,
									}
									return components





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase=0    )     -> Dict:
									if str(_lowercase    ).startswith("""mps"""    ):
												a_						:						Optional[int] 							= torch.manual_seed(_lowercase    )
									else:
												a_						:						str 							= torch.Generator(device=_lowercase    ).manual_seed(_lowercase    )
									a_						:						Tuple 							= {
									    """prompt""": """.""",
									    """generator""": generator,
									    """num_inference_steps""": 2,
									    """guidance_scale""": 1.0,
									    """sag_scale""": 1.0,
									    """output_type""": """numpy""",
									}
									return inputs





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									super().test_inference_batch_single_identical(expected_max_diff=3e-3    )







@slow
@require_torch_gpu
class        A__(unittest.TestCase							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									# clean up the VRAM after each test
									super().tearDown()
									gc.collect()
									torch.cuda.empty_cache()





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Tuple 							= StableDiffusionSAGPipeline.from_pretrained("""CompVis/stable-diffusion-v1-4"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						Optional[int] 							= """."""
									a_						:						int 							= torch.manual_seed(0    )
									a_						:						Any 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Any 							= output.images

									a_						:						Optional[int] 							= image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									a_						:						str 							= np.array([0.1_5_6_8, 0.1_7_3_8, 0.1_6_9_5, 0.1_6_9_3, 0.1_5_0_7, 0.1_7_0_5, 0.1_5_4_7, 0.1_7_5_1, 0.1_9_4_9]    )

									assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						Optional[int] 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						List[str] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						int 							= """."""
									a_						:						Dict 							= torch.manual_seed(0    )
									a_						:						Union[str, Any] 							= sag_pipe(
									    [prompt]  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""    )

									a_						:						Optional[Any] 							= output.images

									a_						:						int 							= image[0, -3:, -3:, -1]

									assert image.shape == (1, 512, 512, 3)
									a_						:						Dict 							= np.array([0.3_4_5_9, 0.2_8_7_6, 0.2_5_3_7, 0.3_0_0_2, 0.2_6_7_1, 0.2_1_6_0, 0.3_0_2_6, 0.2_2_6_2, 0.2_3_7_1]    )

									assert np.abs(image_slice.flatten() - expected_slice    ).max() < 5e-2





						def 			UpperCamelCase__					(    self    )     -> Any:
									a_						:						int 							= StableDiffusionSAGPipeline.from_pretrained("""stabilityai/stable-diffusion-2-1-base"""    )
									a_						:						Optional[Any] 							= sag_pipe.to(_lowercase    )
									sag_pipe.set_progress_bar_config(disable=_lowercase    )

									a_						:						List[Any] 							= """."""
									a_						:						str 							= torch.manual_seed(0    )
									a_						:						int 							= sag_pipe(
									    [prompt]  ,      width=768  ,      height=512  ,      generator=_lowercase  ,      guidance_scale=7.5  ,      sag_scale=1.0  ,      num_inference_steps=20  ,      output_type="""np"""  ,      )

									a_						:						Any 							= output.images

									assert image.shape == (1, 512, 768, 3)


