






'''simple docstring'''

from argparse import ArgumentParser, Namespace

from ..utils import logging
from . import BaseTransformersCLICommand







def 					lowercase__				(   __UpperCamelCase					)->				str:
      return ConvertCommand(
          args.model_type	,		args.tf_checkpoint	,		args.pytorch_dump_output	,		args.config	,		args.finetuning_task_name					)


SCREAMING_SNAKE_CASE__											=     '\ntransformers can only be used from the commandline to convert TensorFlow models in PyTorch, In that case, it requires\nTensorFlow to be installed. Please see https://www.tensorflow.org/install/ for installation instructions.\n'






class        a_		(    lowerCamelCase       ):

  @staticmethod
  def 						A__		(   _SCREAMING_SNAKE_CASE   )						->   Optional[int]:



        """simple docstring"""






        UpperCamelCase       						=      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=_SCREAMING_SNAKE_CASE    ,	required=_SCREAMING_SNAKE_CASE    ,	help="""Model's type."""   )
        train_parser.add_argument(
            """--tf_checkpoint"""    ,	type=_SCREAMING_SNAKE_CASE    ,	required=_SCREAMING_SNAKE_CASE    ,	help="""TensorFlow checkpoint path or folder."""   )
        train_parser.add_argument(
            """--pytorch_dump_output"""    ,	type=_SCREAMING_SNAKE_CASE    ,	required=_SCREAMING_SNAKE_CASE    ,	help="""Path to the PyTorch saved model output."""   )
        train_parser.add_argument("""--config"""    ,	type=_SCREAMING_SNAKE_CASE    ,	default=""""""    ,	help="""Configuration file path or folder."""   )
        train_parser.add_argument(
            """--finetuning_task_name"""    ,	type=_SCREAMING_SNAKE_CASE    ,	default=_SCREAMING_SNAKE_CASE    ,	help="""Optional fine-tuning task name if the TF model was a finetuned model."""    ,	)
        train_parser.set_defaults(func=_SCREAMING_SNAKE_CASE   )

  def __init__(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	*_SCREAMING_SNAKE_CASE    ,	)						->   Optional[Any]:



        """simple docstring"""






        UpperCamelCase       						=      logging.get_logger("""transformers-cli/converting"""   )

        self._logger.info(F"Loading model {model_type}"   )
        UpperCamelCase       						=      model_type
        UpperCamelCase       						=      tf_checkpoint
        UpperCamelCase       						=      pytorch_dump_output
        UpperCamelCase       						=      config
        UpperCamelCase       						=      finetuning_task_name


  def 						A__		(   self   )						->   Dict:



        """simple docstring"""






        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(_SCREAMING_SNAKE_CASE   )

              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(_SCREAMING_SNAKE_CASE   )

              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(_SCREAMING_SNAKE_CASE   )

              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(_SCREAMING_SNAKE_CASE   )

              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(_SCREAMING_SNAKE_CASE   )

              if "ckpt" in self._tf_checkpoint.lower():
                    UpperCamelCase       						=      self._tf_checkpoint
                    UpperCamelCase       						=      """"""
              else:
                    UpperCamelCase       						=      self._tf_checkpoint
                    UpperCamelCase       						=      """"""
              convert_transfo_xl_checkpoint_to_pytorch(
                  _SCREAMING_SNAKE_CASE    ,	self._config    ,	self._pytorch_dump_output    ,	_SCREAMING_SNAKE_CASE   )
        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(_SCREAMING_SNAKE_CASE   )

              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(_SCREAMING_SNAKE_CASE   )

              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]"""   )








'''simple docstring'''

from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-1'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-2'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-3'
SCREAMING_SNAKE_CASE__											=     'CompVis/stable-diffusion-v1-4'






class        a_		(    lowerCamelCase       ):

  def __init__(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = True    ,	)						->   Any:



        """simple docstring"""






        super()._init_()

        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline.from_pretrained(_SCREAMING_SNAKE_CASE   )
        UpperCamelCase       						=      StableDiffusionPipeline(
            vae=_SCREAMING_SNAKE_CASE    ,	text_encoder=_SCREAMING_SNAKE_CASE    ,	tokenizer=_SCREAMING_SNAKE_CASE    ,	unet=_SCREAMING_SNAKE_CASE    ,	scheduler=_SCREAMING_SNAKE_CASE    ,	safety_checker=_SCREAMING_SNAKE_CASE    ,	feature_extractor=_SCREAMING_SNAKE_CASE    ,	requires_safety_checker=_SCREAMING_SNAKE_CASE    ,	)

        self.register_modules(pipelinea=self.pipea    ,	pipelinea=self.pipea    ,	pipelinea=self.pipea    ,	pipelinea=self.pipea   )

  @property
  def 						A__		(   self   )						->   Dict[str, Any]:



        """simple docstring"""






        return {k: getattr(self    ,	_SCREAMING_SNAKE_CASE   ) for k in self.config.keys() if not k.startswith("""_"""   )}

  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE = "auto"   )						->   Optional[Any]:



        """simple docstring"""






        if slice_size == "auto":
              # half the attention head size is usually a good trade-off between
              # speed and memory
              UpperCamelCase       						=      self.unet.config.attention_head_dim // 2
        self.unet.set_attention_slice(_SCREAMING_SNAKE_CASE   )

  def 						A__		(   self   )						->   Tuple:



        """simple docstring"""






        self.enable_attention_slicing(_SCREAMING_SNAKE_CASE   )

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[Any]:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[Any]:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   Dict:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   str:



        """simple docstring"""






        return self.pipea(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)


  @torch.no_grad()
  def 						A__		(   self    ,	_SCREAMING_SNAKE_CASE    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 512    ,	_SCREAMING_SNAKE_CASE = 50    ,	_SCREAMING_SNAKE_CASE = 7.5    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	_SCREAMING_SNAKE_CASE = 0.0    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = "pil"    ,	_SCREAMING_SNAKE_CASE = True    ,	_SCREAMING_SNAKE_CASE = None    ,	_SCREAMING_SNAKE_CASE = 1    ,	**_SCREAMING_SNAKE_CASE    ,	)						->   List[str]:



        """simple docstring"""






        UpperCamelCase       						=      """cuda""" if torch.cuda.is_available() else """cpu"""
        self.to(_SCREAMING_SNAKE_CASE   )

        # Checks if the height and width are divisible by 8 or not
        if height % 8 != 0 or width % 8 != 0:
              raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}."   )

        # Get first result from Stable Diffusion Checkpoint v1.1
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.2
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.3
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get first result from Stable Diffusion Checkpoint v1.4
        UpperCamelCase       						=      self.textaimg_sda_a(
            prompt=_SCREAMING_SNAKE_CASE    ,	height=_SCREAMING_SNAKE_CASE    ,	width=_SCREAMING_SNAKE_CASE    ,	num_inference_steps=_SCREAMING_SNAKE_CASE    ,	guidance_scale=_SCREAMING_SNAKE_CASE    ,	negative_prompt=_SCREAMING_SNAKE_CASE    ,	num_images_per_prompt=_SCREAMING_SNAKE_CASE    ,	eta=_SCREAMING_SNAKE_CASE    ,	generator=_SCREAMING_SNAKE_CASE    ,	latents=_SCREAMING_SNAKE_CASE    ,	output_type=_SCREAMING_SNAKE_CASE    ,	return_dict=_SCREAMING_SNAKE_CASE    ,	callback=_SCREAMING_SNAKE_CASE    ,	callback_steps=_SCREAMING_SNAKE_CASE    ,	**_SCREAMING_SNAKE_CASE    ,	)

        # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
        return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]   )
