'''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


_A			:      Optional[int]					      =				'''CompVis/stable-diffusion-v1-1'''
_A			:      List[Any]					      =				'''CompVis/stable-diffusion-v1-2'''
_A			:      Optional[Any]					      =				'''CompVis/stable-diffusion-v1-3'''
_A			:      int					      =				'''CompVis/stable-diffusion-v1-4'''







class 				_lowercase     (				UpperCAmelCase__			):


 '''simple docstring'''






 def __init__(      self   :	Tuple      ,       SCREAMING_SNAKE_CASE__   :	AutoencoderKL      ,       SCREAMING_SNAKE_CASE__   :	CLIPTextModel      ,       SCREAMING_SNAKE_CASE__   :	CLIPTokenizer      ,       SCREAMING_SNAKE_CASE__   :	UNetaDConditionModel      ,       SCREAMING_SNAKE_CASE__   :	Union[DDIMScheduler, PNDMScheduler, LMSDiscreteScheduler]      ,       SCREAMING_SNAKE_CASE__   :	StableDiffusionSafetyChecker      ,       SCREAMING_SNAKE_CASE__   :	CLIPImageProcessor      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       )  ->    List[Any]:
  super()._init_()

  __lowerCAmelCase											=						StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						StableDiffusionPipeline.from_pretrained(SCREAMING_SNAKE_CASE__							)
  __lowerCAmelCase											=						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   :	List[Any]							)  ->    Dict[str, Any]:
  return {k: getattr(self      ,       SCREAMING_SNAKE_CASE__							) for k in self.config.keys() if not k.startswith("""_"""							)}
 def a					(      self   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, int]] = "auto"							)  ->    Optional[int]:
  if slice_size == "auto":
   # half the attention head size is usually a good trade-off between
   # speed and memory
   __lowerCAmelCase											=						self.unet.config.attention_head_dim // 2
  self.unet.set_attention_slice(SCREAMING_SNAKE_CASE__							)
 def a					(      self   :	Union[str, Any]							)  ->    Optional[int]:
  self.enable_attention_slicing(SCREAMING_SNAKE_CASE__							)
 @torch.no_grad()
 def a					(      self   :	int      ,       SCREAMING_SNAKE_CASE__   :	Union[str, List[str]]      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 50      ,       SCREAMING_SNAKE_CASE__   :	float = 7.5      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, List[str]]] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[int] = 1      ,       SCREAMING_SNAKE_CASE__   :	float = 0.0      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.Generator] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.FloatTensor] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[str] = "pil"      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       SCREAMING_SNAKE_CASE__   :	int = 1      ,       **SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       )  ->    List[str]:
  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   :	str      ,       SCREAMING_SNAKE_CASE__   :	Union[str, List[str]]      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 50      ,       SCREAMING_SNAKE_CASE__   :	float = 7.5      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, List[str]]] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[int] = 1      ,       SCREAMING_SNAKE_CASE__   :	float = 0.0      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.Generator] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.FloatTensor] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[str] = "pil"      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       SCREAMING_SNAKE_CASE__   :	int = 1      ,       **SCREAMING_SNAKE_CASE__   :	Tuple      ,       )  ->    List[str]:
  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   :	Dict      ,       SCREAMING_SNAKE_CASE__   :	Union[str, List[str]]      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 50      ,       SCREAMING_SNAKE_CASE__   :	float = 7.5      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, List[str]]] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[int] = 1      ,       SCREAMING_SNAKE_CASE__   :	float = 0.0      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.Generator] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.FloatTensor] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[str] = "pil"      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       SCREAMING_SNAKE_CASE__   :	int = 1      ,       **SCREAMING_SNAKE_CASE__   :	Tuple      ,       )  ->    Union[str, Any]:
  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   :	List[Any]      ,       SCREAMING_SNAKE_CASE__   :	Union[str, List[str]]      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 50      ,       SCREAMING_SNAKE_CASE__   :	float = 7.5      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, List[str]]] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[int] = 1      ,       SCREAMING_SNAKE_CASE__   :	float = 0.0      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.Generator] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.FloatTensor] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[str] = "pil"      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       SCREAMING_SNAKE_CASE__   :	int = 1      ,       **SCREAMING_SNAKE_CASE__   :	Optional[int]      ,       )  ->    List[Any]:
  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   :	int      ,       SCREAMING_SNAKE_CASE__   :	Union[str, List[str]]      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 5_12      ,       SCREAMING_SNAKE_CASE__   :	int = 50      ,       SCREAMING_SNAKE_CASE__   :	float = 7.5      ,       SCREAMING_SNAKE_CASE__   :	Optional[Union[str, List[str]]] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[int] = 1      ,       SCREAMING_SNAKE_CASE__   :	float = 0.0      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.Generator] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[torch.FloatTensor] = None      ,       SCREAMING_SNAKE_CASE__   :	Optional[str] = "pil"      ,       SCREAMING_SNAKE_CASE__   :	bool = True      ,       SCREAMING_SNAKE_CASE__   :	Optional[Callable[[int, int, torch.FloatTensor], None]] = None      ,       SCREAMING_SNAKE_CASE__   :	int = 1      ,       **SCREAMING_SNAKE_CASE__   :	int      ,       )  ->    List[str]:
  __lowerCAmelCase											=						"""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
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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
  __lowerCAmelCase											=						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]]							)







'''simple docstring'''

import argparse

import torch
from torch import nn

from transformers import MaMaaaConfig, MaMaaaForConditionalGeneration







def      UpperCamelCase_						(					snake_case_       :       Any      )					->     Optional[Any]:






 '''simple docstring'''

 __lowerCAmelCase											=						[
     """encoder.version""",
     """decoder.version""",
     """model.encoder.version""",
     """model.decoder.version""",
     """decoder.output_projection.weight""",
     """_float_tensor""",
     """encoder.embed_positions._float_tensor""",
     """decoder.embed_positions._float_tensor""",
 ]
 for k in ignore_keys:
  state_dict.pop(snake_case_						,		snake_case_      )







def      UpperCamelCase_						(					snake_case_       :       Optional[Any]      )					->     List[Any]:






 '''simple docstring'''

 __lowerCAmelCase							,       __lowerCAmelCase											=						emb.weight.shape
 __lowerCAmelCase											=						nn.Linear(snake_case_						,		snake_case_						,		bias=snake_case_      )
 __lowerCAmelCase											=						emb.weight.data
 return lin_layer







def      UpperCamelCase_						(					snake_case_       :       Any      )					->     Any:






 '''simple docstring'''

 __lowerCAmelCase											=						torch.load(snake_case_						,		map_location="""cpu"""      )
 __lowerCAmelCase											=						mam_aaa["""args"""] or mam_aaa["""cfg"""]["""model"""]
 __lowerCAmelCase											=						mam_aaa["""model"""]
 remove_ignore_keys_(snake_case_      )
 __lowerCAmelCase											=						state_dict["""encoder.embed_tokens.weight"""].shape[0]

 __lowerCAmelCase											=						MaMaaaConfig(
     vocab_size=snake_case_						,		max_position_embeddings=10_24						,		encoder_layers=args.encoder_layers						,		decoder_layers=args.decoder_layers						,		encoder_attention_heads=args.encoder_attention_heads						,		decoder_attention_heads=args.decoder_attention_heads						,		encoder_ffn_dim=args.encoder_ffn_embed_dim						,		decoder_ffn_dim=args.decoder_ffn_embed_dim						,		d_model=args.encoder_embed_dim						,		encoder_layerdrop=args.encoder_layerdrop						,		decoder_layerdrop=args.decoder_layerdrop						,		dropout=args.dropout						,		attention_dropout=args.attention_dropout						,		activation_dropout=args.activation_dropout						,		activation_function="""relu"""						,		)

 __lowerCAmelCase											=						state_dict["""decoder.embed_tokens.weight"""]
 __lowerCAmelCase											=						MaMaaaForConditionalGeneration(snake_case_      )
 model.model.load_state_dict(snake_case_						,		strict=snake_case_      )
 __lowerCAmelCase											=						make_linear_from_emb(model.model.shared      )

 return model


if __name__ == "__main__":
 _A			:      Union[str, Any]					      =				argparse.ArgumentParser()
 # Required parameters
 parser.add_argument('''fairseq_path''', type=str, help='''path to a model.pt on local filesystem.''')
 parser.add_argument('''pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
 _A			:      str					      =				parser.parse_args()
 _A			:      Optional[int]					      =				convert_fairseq_mamaaa_checkpoint_from_disk(args.fairseq_pathß)
 model.save_pretrained(args.pytorch_dump_folder_path)






