
    +h#                     d   S SK r S SKJrJrJrJrJrJr  S SKr	S SK
r
S SKJr  S SKJrJrJrJr  SSKJrJr  SSKJr  SSKJrJr  SS	KJrJrJrJr  SS
KJ r J!r!J"r"  SSK#J$r$  SSK%J&r&  SSK'J(r(J)r)J*r*J+r+J,r,J-r-J.r.  SSK/J0r0  SSK1J2r2J3r3  SSK4J5r5  SSK6J7r7  SSK8J9r9  \*" 5       (       a  S SK:J;s  J<r=  Sr>OSr>\+R~                  " \@5      rASrB S#S\
R                  S\\
R                     S\E4S jjrF    S$S\\G   S\\\E\
R                  4      S\\\G      S\\\I      4S  jjrJ " S! S"\2\3\\\\\95	      rKg)%    N)AnyCallableDictListOptionalUnion)version)CLIPImageProcessorCLIPTextModelCLIPTokenizerCLIPVisionModelWithProjection   )MultiPipelineCallbacksPipelineCallback)
FrozenDict)PipelineImageInputVaeImageProcessor)FromSingleFileMixinIPAdapterMixinStableDiffusionLoraLoaderMixinTextualInversionLoaderMixin)AutoencoderKLImageProjectionUNet2DConditionModel)adjust_lora_scale_text_encoder)KarrasDiffusionSchedulers)USE_PEFT_BACKEND	deprecateis_torch_xla_availableloggingreplace_example_docstringscale_lora_layersunscale_lora_layers)randn_tensor   )DiffusionPipelineStableDiffusionMixin)StableDiffusionPipelineOutput)StableDiffusionSafetyChecker   )PAGMixinTFa  
    Examples:
        ```py
        >>> import torch
        >>> from diffusers import AutoPipelineForImage2Image
        >>> from diffusers.utils import load_image

        >>> pipe = AutoPipelineForImage2Image.from_pretrained(
        ...     "runwayml/stable-diffusion-v1-5",
        ...     torch_dtype=torch.float16,
        ...     enable_pag=True,
        ... )
        >>> pipe = pipe.to("cuda")
        >>> url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"

        >>> init_image = load_image(url).convert("RGB")
        >>> prompt = "a photo of an astronaut riding a horse on mars"
        >>> image = pipe(prompt, image=init_image, pag_scale=0.3).images[0]
        ```
encoder_output	generatorsample_modec                    [        U S5      (       a!  US:X  a  U R                  R                  U5      $ [        U S5      (       a   US:X  a  U R                  R                  5       $ [        U S5      (       a  U R                  $ [        S5      e)Nlatent_distsampleargmaxlatentsz3Could not access latents of provided encoder_output)hasattrr0   r1   moder3   AttributeError)r,   r-   r.   s      i/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/pag/pipeline_pag_sd_img2img.pyretrieve_latentsr8   O   s}     ~}--+2I))00;;		/	/K84K))..00		+	+%%%RSS    num_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`List[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr<   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r<   r;   r=   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r=   r;   r;    )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r<   len)	schedulerr:   r;   r<   r=   kwargsaccepts_timestepsaccept_sigmass           r7   retrieve_timestepsrM   ]   s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))r9   c            1          ^  \ rS rSrSrSr/ SQrS/r/ SQr   SIS	\	S
\
S\S\S\S\S\S\S\S\\\\   4   4U 4S jjjr     SJS\\R0                     S\\R0                     S\\   S\\   4S jjrSKS jrS rS rS r      SLS jr S r!SKS jr"S\RF                  4S \R0                  S!\S"\RH                  S#\R0                  4S$ jjr%\&S% 5       r'\&S& 5       r(\&S' 5       r)\&S( 5       r*\&S) 5       r+\&S* 5       r,\RZ                  " 5       \." \/5      SSS+S,SSS-SS.S/SSSSSS0SSSSS1/S2S/4S3\\\\   4   S4\0S5\S6\\   S7\\   S8\\   S9\\   S:\\\\\   4      S;\\   S<\\   S=\\\Rb                  \\Rb                     4      S\\R0                     S\\R0                     S>\\0   S?\\\R0                        S@\\   SA\SB\\2\\34      S\SC\\\4\\\2/S4   \5\64      SD\\   SE\SF\4.SG jj5       5       r7SHr8U =r9$ )M!StableDiffusionPAGImg2ImgPipeline   a  
Pipeline for text-guided image-to-image generation using Stable Diffusion.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods
implemented for all pipelines (downloading, saving, running on a particular device, etc.).

The pipeline also inherits the following loading methods:
    - [`~loaders.TextualInversionLoaderMixin.load_textual_inversion`] for loading textual inversion embeddings
    - [`~loaders.StableDiffusionLoraLoaderMixin.load_lora_weights`] for loading LoRA weights
    - [`~loaders.StableDiffusionLoraLoaderMixin.save_lora_weights`] for saving LoRA weights
    - [`~loaders.FromSingleFileMixin.from_single_file`] for loading `.ckpt` files
    - [`~loaders.IPAdapterMixin.load_ip_adapter`] for loading IP Adapters

Args:
    vae ([`AutoencoderKL`]):
        Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
    text_encoder ([`~transformers.CLIPTextModel`]):
        Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
    tokenizer ([`~transformers.CLIPTokenizer`]):
        A `CLIPTokenizer` to tokenize text.
    unet ([`UNet2DConditionModel`]):
        A `UNet2DConditionModel` to denoise the encoded image latents.
    scheduler ([`SchedulerMixin`]):
        A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
        [`DDIMScheduler`], [`LMSDiscreteScheduler`], or [`PNDMScheduler`].
    safety_checker ([`StableDiffusionSafetyChecker`]):
        Classification module that estimates whether generated images could be considered offensive or harmful.
        Please refer to the [model card](https://huggingface.co/runwayml/stable-diffusion-v1-5) for more details
        about a model's potential harms.
    feature_extractor ([`~transformers.CLIPImageProcessor`]):
        A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
z&text_encoder->image_encoder->unet->vae)safety_checkerfeature_extractorimage_encoderrQ   )r3   prompt_embedsnegative_prompt_embedsNTvaetext_encoder	tokenizerunetrI   rR   rS   requires_safety_checkerpag_applied_layersc                 z  > [         TU ]  5         Ubn  [        UR                  SS5      S:w  aS  SU SUR                  R                   S3n[        SSUSS	9  [        UR                  5      nSUS'   [        U5      Ul        UbV  [        UR                  S
S5      SL a<  SU S3n[        SSUSS	9  [        UR                  5      nSUS
'   [        U5      Ul        Uc*  U	(       a#  [        R                  SU R                   S35        Ub  Uc  [        S5      eUS L=(       a|    [        UR                  S5      =(       a_    [        R                  " [        R                  " UR                  R                   5      R"                  5      [        R                  " S5      :  nUS L=(       a6    [        UR                  S5      =(       a    UR                  R$                  S:  nU(       a?  U(       a8  Sn[        SSUSS	9  [        UR                  5      nSUS'   [        U5      Ul        U R'                  UUUUUUUUS9  [        U SS 5      (       a/  S[)        U R*                  R                  R,                  5      S-
  -  OSU l        [1        U R.                  S9U l        U R5                  U	S9  U R7                  U
5        g )Nsteps_offsetr*   z*The configuration file of this scheduler: z; is outdated. `steps_offset` should be set to 1 instead of a(  . Please make sure to update the config accordingly as leaving `steps_offset` might led to incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json` filezsteps_offset!=11.0.0Fstandard_warnclip_sampleTa   has not set the configuration `clip_sample`. `clip_sample` should be set to False in the configuration file. Please make sure to update the config accordingly as not setting `clip_sample` in the config might lead to incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it would be very nice if you could open a Pull request for the `scheduler/scheduler_config.json` filezclip_sample not setz)You have disabled the safety checker for a   by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .zMake sure to define a feature extractor when loading {self.__class__} if you want to use the safety checker. If you do not want to use the safety checker, you can pass `'safety_checker=None'` instead._diffusers_versionz
0.9.0.dev0sample_size@   a  The configuration file of the unet has set the default `sample_size` to smaller than 64 which seems highly unlikely. If your checkpoint is a fine-tuned version of any of the following: 
- CompVis/stable-diffusion-v1-4 
- CompVis/stable-diffusion-v1-3 
- CompVis/stable-diffusion-v1-2 
- CompVis/stable-diffusion-v1-1 
- runwayml/stable-diffusion-v1-5 
- runwayml/stable-diffusion-inpainting 
 you should change 'sample_size' to 64 in the configuration file. Please make sure to update the config accordingly as leaving `sample_size=32` in the config might lead to incorrect results in future versions. If you have downloaded this checkpoint from the Hugging Face Hub, it would be very nice if you could open a Pull request for the `unet/config.json` filezsample_size<64)rV   rW   rX   rY   rI   rQ   rR   rS   rV   r%      )vae_scale_factor)rZ   )super__init__getattrconfigr]   r   dictr   _internal_dictloggerwarningrG   r@   r4   r	   parserb   base_versionrc   register_modulesrH   rV   block_out_channelsrf   r   image_processorregister_to_configset_pag_applied_layers)selfrV   rW   rX   rY   rI   rQ   rR   rS   rZ   r[   deprecation_message
new_configis_unet_version_less_0_9_0is_unet_sample_size_less_64rG   s                  r7   rh   *StableDiffusionPAGImg2ImgPipeline.__init__   s    	 WY-=-=~q%QUV%V<YK H22;2B2B2O2O1P Q   '2EUZ[i../J)*J~&'1*'=I$ WY-=-=}e%TX\%\<YK Hh h   +W6IY^_i../J(-J}%'1*'=I$!&=NN;DNN;K Lj j %*;*Cx   x%9:xgmmDKK,J,JKXXY\c\i\ijv\ww 	# em!DeI`I`ceIe 	$ &*E/   &1DTYZdkk*J(*J}%",Z"8D%)/' 	 		
 W^^bdikoVpVpc$((//*L*L&MPQ&Q Rvw0$BWBWX8OP##$67r9   rT   rU   
lora_scale	clip_skipc
                 
   UbS  [        U [        5      (       a>  Xl        [        (       d  [	        U R
                  U5        O[        U R
                  U5        Ub  [        U[        5      (       a  Sn
O3Ub!  [        U[        5      (       a  [        U5      n
OUR                  S   n
UGc  [        U [        5      (       a  U R                  XR                  5      nU R                  USU R                  R                  SSS9nUR                  nU R                  USSS	9R                  nUR                  S
   UR                  S
   :  a  [         R"                  " X5      (       dj  U R                  R%                  USS2U R                  R                  S-
  S
24   5      n[&        R)                  SU R                  R                   SU 35        [+        U R
                  R,                  S5      (       aA  U R
                  R,                  R.                  (       a  UR0                  R3                  U5      nOSnU	c%  U R                  UR3                  U5      US9nUS   nOQU R                  UR3                  U5      USS9nUS
   U	S-   *    nU R
                  R4                  R7                  U5      nU R
                  b  U R
                  R8                  nO0U R:                  b  U R:                  R8                  nOUR8                  nUR3                  UUS9nUR                  u  nnnUR=                  SUS5      nUR?                  UU-  US
5      nU(       Ga  UGc|  Uc  S/U
-  nOUb;  [A        U5      [A        U5      La$  [C        S[A        U5       S[A        U5       S35      e[        U[        5      (       a  U/nO2U
[        U5      :w  a!  [E        SU S[        U5       SU SU
 S3	5      eUn[        U [        5      (       a  U R                  UU R                  5      nUR                  S   nU R                  USUSSS9n[+        U R
                  R,                  S5      (       aA  U R
                  R,                  R.                  (       a  UR0                  R3                  U5      nOSnU R                  UR                  R3                  U5      US9nUS   nU(       aG  UR                  S   nUR3                  UUS9nUR=                  SUS5      nUR?                  X-  US
5      nU R
                  b6  [        U [        5      (       a!  [        (       a  [G        U R
                  U5        Xg4$ )a,  
Encodes the prompt into text encoder hidden states.

Args:
    prompt (`str` or `List[str]`, *optional*):
        prompt to be encoded
    device: (`torch.device`):
        torch device
    num_images_per_prompt (`int`):
        number of images that should be generated per prompt
    do_classifier_free_guidance (`bool`):
        whether to use classifier free guidance or not
    negative_prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation. If not defined, one has to pass
        `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
        less than `1`).
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, negative_prompt_embeds will be generated from `negative_prompt` input
        argument.
    lora_scale (`float`, *optional*):
        A LoRA scale that will be applied to all LoRA layers of the text encoder if LoRA layers are loaded.
    clip_skip (`int`, *optional*):
        Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
        the output of the pre-final layer will be used for computing the prompt embeddings.
Nr*   r   
max_lengthTpt)paddingr   
truncationreturn_tensorslongest)r   r   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: use_attention_mask)attention_mask)r   output_hidden_states)dtyper;    z?`negative_prompt` should be the same type to `prompt`, but got z != .z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.)$
isinstancer   _lora_scaler   r   rW   r"   strlistrH   shaper   maybe_convert_promptrX   model_max_length	input_idstorchequalbatch_decoderm   rn   r4   rj   r   r   to
text_modelfinal_layer_normr   rY   repeatviewtype	TypeErrorr@   r#   )rv   promptr;   num_images_per_promptdo_classifier_free_guidancenegative_promptrT   rU   r|   r}   
batch_sizetext_inputstext_input_idsuntruncated_idsremoved_textr   prompt_embeds_dtypebs_embedseq_len_uncond_tokensr   uncond_inputs                          r7   encode_prompt/StableDiffusionPAGImg2ImgPipeline.encode_prompt+  sQ   V !j7U&V&V) $#.t/@/@*M!$"3"3Z@*VS"9"9JJvt$<$<VJ&,,Q/J $ ;<<226>>J..$>>::# ) K )22N"nnVYW[n\ffO$$R(N,@,@,DDU[[N N  $~~::#At~~'F'F'JR'O$OP  778	,Q
 t((//1EFF4K\K\KcKcKvKv!,!;!;!>!>v!F!%  $ 1 1.2C2CF2K\j 1 k -a 0 $ 1 1"%%f-ncg !2 ! !.b 1IM2B C
 !% 1 1 < < M Mm \("&"3"3"9"9YY""&))//"/"5"5%((/B6(R,22'1%,,Q0EqI%**86K+KWVXY '+A+I&!#z 1#VD<Q(QUVZ[jVkUl mV~Q(  OS11!0 1s?33 )/)::J3K_J` ax/
| <33  !0 $ ;<< $ 9 9- X&,,Q/J>>$%# * L t((//1EFF4K\K\KcKcKvKv!-!<!<!?!?!G!%%)%6%6&&))&1- &7 &" &<A%>"&,2215G%;%>%>EXag%>%h"%;%B%B1F[]^%_"%;%@%@Acelnp%q"($ >??DTDT#D$5$5zB44r9   c                 d   [        U R                  R                  5       5      R                  n[	        U[
        R                  5      (       d  U R                  USS9R                  nUR                  X%S9nU(       aq  U R                  USS9R                  S   nUR                  USS9nU R                  [
        R                  " U5      SS9R                  S   nUR                  USS9nXg4$ U R                  U5      R                  nUR                  USS9n[
        R                  " U5      n	X4$ )	Nr   r   r;   r   T)r   r   dim)nextrS   rE   r   r   r   TensorrR   pixel_valuesr   hidden_statesrepeat_interleave
zeros_likeimage_embeds)
rv   imager;   r   r   r   image_enc_hidden_statesuncond_image_enc_hidden_statesr   uncond_image_embedss
             r7   encode_image.StableDiffusionPAGImg2ImgPipeline.encode_image  s?   T''2245;;%..**5*FSSE4&*&8&8UY&8&Z&h&hik&l#&=&O&OPekl&O&m#-1-?-?  'd .@ .mB. * .L-]-]%1 .^ .* +JJ--e4AAL'99:OUV9WL"'"2"2<"@44r9   c                 
   / nU(       a  / nUGc&  [        U[        5      (       d  U/n[        U5      [        U R                  R                  R
                  5      :w  aB  [        S[        U5       S[        U R                  R                  R
                  5       S35      e[        XR                  R                  R
                  5       Hh  u  p[        U	[        5      (       + n
U R                  XSU
5      u  pUR                  US S S 24   5        U(       d  MP  WR                  US S S 24   5        Mj     OEU H?  nU(       a$  UR                  S5      u  pWR                  U5        UR                  U5        MA     / n[        U5       Hw  u  p[        R                  " U/U-  SS9nU(       a2  [        R                  " WU   /U-  SS9n[        R                  " X/SS9nUR                  US9nUR                  U5        My     U$ )	NzK`ip_adapter_image` must have same length as the number of IP Adapters. Got z images and z IP Adapters.r*   r%   r   r   )r;   )r   r   rH   rY   encoder_hid_projimage_projection_layersr@   zipr   r   appendchunk	enumerater   catr   )rv   ip_adapter_imageip_adapter_image_embedsr;   r   r   r   negative_image_embedssingle_ip_adapter_imageimage_proj_layeroutput_hidden_statesingle_image_embedssingle_negative_image_embedsis                 r7   prepare_ip_adapter_image_embedsAStableDiffusionPAGImg2ImgPipeline.prepare_ip_adapter_image_embeds  s9    &$&!"*.55$4#5 #$DII,F,F,^,^(__ abefvbwax  yE  FI  JN  JS  JS  Jd  Jd  J|  J|  F}  E~  ~K  L  >A ))"<"<"T"T>9' +55E*W&W#DHDUDU+Q8KEA# ##$7a$@A..)001MdTUg1VW> (?#.H[HaHabcHdE0)001MN##$78	 (? #%&/&="A"'))-@,ADY,Y_`"a*/4yy:OPQ:R9SVk9kqr/s,&+ii1M0cij&k#"5"8"8"8"G#**+>? '> '&r9   c                 `   U R                   c  S nX4$ [        R                  " U5      (       a  U R                  R	                  USS9nOU R                  R                  U5      nU R                  USS9R                  U5      nU R                  XR                  R                  U5      S9u  pX4$ )Npil)output_typer   r   )images
clip_input)	rQ   r   	is_tensorrs   postprocessnumpy_to_pilrR   r   r   )rv   r   r;   r   has_nsfw_conceptfeature_extractor_inputsafety_checker_inputs          r7   run_safety_checker4StableDiffusionPAGImg2ImgPipeline.run_safety_checker)  s    &# && u%%*.*>*>*J*J5^c*J*d'*.*>*>*K*KE*R'#'#9#9:Qbf#9#g#j#jkq#r &*&9&9)J)J)M)Me)T ': '#E &&r9   c                 n   S[        [        R                  " U R                  R                  5      R
                  R                  5       5      ;   n0 nU(       a  X$S'   S[        [        R                  " U R                  R                  5      R
                  R                  5       5      ;   nU(       a  XS'   U$ )Netar-   )rA   rB   rC   rI   steprE   rF   )rv   r-   r   accepts_etaextra_step_kwargsaccepts_generators         r7   prepare_extra_step_kwargs;StableDiffusionPAGImg2ImgPipeline.prepare_extra_step_kwargs8  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r9   c	           
      n  ^  US:  d  US:  a  [        SU 35      eUbW  [        U 4S jU 5       5      (       d=  [        ST R                   SU V	s/ s H  oT R                  ;  d  M  U	PM     sn	 35      eUb  Ub  [        SU SU S	35      eUc  Uc  [        S
5      eUbA  [        U[        5      (       d,  [        U[
        5      (       d  [        S[        U5       35      eUb  Ub  [        SU SU S	35      eUbC  Ub@  UR                  UR                  :w  a&  [        SUR                   SUR                   S35      eUb  Ub  [        S5      eUb\  [        U[
        5      (       d  [        S[        U5       35      eUS   R                  S;  a  [        SUS   R                   S35      eg g s  sn	f )Nr   r*   z2The value of strength should in [0.0, 1.0] but is c              3   @   >#    U  H  oTR                   ;   v   M     g 7fN)_callback_tensor_inputs).0krv   s     r7   	<genexpr>AStableDiffusionPAGImg2ImgPipeline.check_inputs.<locals>.<genexpr>W  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z2. Please make sure to only forward one of the two.zeProvide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined.z2`prompt` has to be of type `str` or `list` but is z'Cannot forward both `negative_prompt`: z and `negative_prompt_embeds`: zu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` r   zProvide either `ip_adapter_image` or `ip_adapter_image_embeds`. Cannot leave both `ip_adapter_image` and `ip_adapter_image_embeds` defined.z:`ip_adapter_image_embeds` has to be of type `list` but is )r      zF`ip_adapter_image_embeds` has to be a list of 3D or 4D tensors but is D)	r@   allr   r   r   r   r   r   ndim)
rv   r   strengthr   rT   rU   r   r   "callback_on_step_end_tensor_inputsr   s
   `         r7   check_inputs.StableDiffusionPAGImg2ImgPipeline.check_inputsI  s~    a<8a<QRZQ[\]]-9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa&+A+M9/9J K*++]_ 
 $)?)K""&<&B&BB --:-@-@,A B.445Q8  ',C,O ^  #.5t<< PQUVmQnPop  )+00> \]tuv]w]|]|\}}~  ? /C pHs   F2'F2c                 N   [        [        X-  5      U5      n[        X-
  S5      nU R                  R                  XPR                  R
                  -  S  n[        U R                  S5      (       a1  U R                  R                  XPR                  R
                  -  5        XaU-
  4$ )Nr   set_begin_index)minintmaxrI   r<   orderr4   r   )rv   r:   r   r;   init_timestept_startr<   s          r7   get_timesteps/StableDiffusionPAGImg2ImgPipeline.get_timesteps  s    C 3 >?ATU)91=NN,,W~~7K7K-K-MN	4>>#455NN**7^^5I5I+IJ777r9   c                 ,   [        U[        R                  [        R                  R                  [
        45      (       d  [        S[        U5       35      eUR                  XeS9nX4-  nUR                  S   S:X  a  UnGO[        U[
        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[        U[
        5      (       a  UR                  S   U:  a>  X1R                  S   -  S:X  a)  [        R                  " U/X1R                  S   -  -  SS	9nOGUR                  S   U:  a4  X1R                  S   -  S:w  a  [        S
UR                  S    SU S35      e[        U5       V	s/ s H-  n	[        U R                  R                  XU	S-    5      Xy   S9PM/     nn	[        R                  " USS	9nO#[        U R                  R                  U5      US9nU R                  R                   R"                  U-  nX8R                  S   :  ab  X8R                  S   -  S:X  aM  SU SUR                  S    S3n
[%        SSU
SS9  X8R                  S   -  n[        R                  " U/U-  SS	9nO\X8R                  S   :  a4  X8R                  S   -  S:w  a  [        S
UR                  S    SU S35      e[        R                  " U/SS	9nUR                  n['        XXeS9nU R(                  R+                  XU5      nUnU$ s  sn	f )NzK`image` has to be of type `torch.Tensor`, `PIL.Image.Image` or list but is r   r*   r   z/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.r   r   z'Cannot duplicate `image` of batch size z to effective batch_size  )r-   zYou have passed z# text prompts (`prompt`), but only a    initial images (`image`). Initial images are now duplicating to match the number of text prompts. Note that this behavior is deprecated and will be removed in a version 1.0.0. Please make sure to update your script to pass as many initial images as text prompts to suppress this warning.zlen(prompt) != len(image)r^   Fr_   z to z text prompts.)r-   r;   r   )r   r   r   PILImager   r@   r   r   r   rH   r   ranger8   rV   encoderj   scaling_factorr   r$   rI   	add_noise)rv   r   timestepr   r   r   r;   r-   init_latentsr   rw   additional_image_per_promptr   noiser3   s                  r7   prepare_latents1StableDiffusionPAGImg2ImgPipeline.prepare_latents  s)   %%,,		!FGG]^bch^i]jk  47
;;q>Q L )T**s9~/K Ec)nEU V  *|+km 
 It,,;;q>J.:A3NRS3S!IIug{{1~1M&NTUVE[[^j0Z++a.5PTU5U$A%++a.AQQjkujvvwx  #:. . %TXX__Uq1u5E%FR[R^_.     %yy1=/0FR[\88??99LHL**1--*?Q?QRS?T2TXY2Y #:,.QR^RdRdefRgQh ih h   17<O_de*48J8J18M*M' 99l^6Q%QWXYL,,Q//JASASTUAV4VZ[4[9,:L:LQ:O9PPTU_T``no  !99l^;L""UT ~~//XNG s   94Li   wembedding_dimr   returnc                 r   [        UR                  5      S:X  d   eUS-  nUS-  n[        R                  " [        R                  " S5      5      US-
  -  n[        R
                  " [        R                  " XCS9U* -  5      nUR                  U5      SS2S4   USSS24   -  n[        R                  " [        R                  " U5      [        R                  " U5      /SS9nUS-  S:X  a*  [        R                  R                  R                  US5      nUR                  UR                  S	   U4:X  d   eU$ )
a,  
See https://github.com/google-research/vdm/blob/dc27b98a554f65cdc654b800da5aa1846545d41b/model_vdm.py#L298

Args:
    w (`torch.Tensor`):
        Generate embedding vectors with a specified guidance scale to subsequently enrich timestep embeddings.
    embedding_dim (`int`, *optional*, defaults to 512):
        Dimension of the embeddings to generate.
    dtype (`torch.dtype`, *optional*, defaults to `torch.float32`):
        Data type of the generated embeddings.

Returns:
    `torch.Tensor`: Embedding vectors with shape `(len(w), embedding_dim)`.
r*   g     @@r%   g     @)r   Nr   )r   r*   r   )rH   r   r   logtensorexparanger   r   sincosnn
functionalpad)rv   r  r  r   half_dimembs         r7   get_guidance_scale_embedding>StableDiffusionPAGImg2ImgPipeline.get_guidance_scale_embedding  s    " 177|q   J A%iiW-.(Q,?iiX;sdBCdd5k!T'"Sq\1ii338a@1!((%%))#v6CyyQWWQZ7777
r9   c                     U R                   $ r   )_guidance_scalerv   s    r7   guidance_scale0StableDiffusionPAGImg2ImgPipeline.guidance_scale  s    ###r9   c                     U R                   $ r   )
_clip_skipr%  s    r7   r}   +StableDiffusionPAGImg2ImgPipeline.clip_skip      r9   c                 r    U R                   S:  =(       a"    U R                  R                  R                  S L $ )Nr*   )r$  rY   rj   time_cond_proj_dimr%  s    r7   r   =StableDiffusionPAGImg2ImgPipeline.do_classifier_free_guidance  s.    ##a'WDII,<,<,O,OSW,WWr9   c                     U R                   $ r   )_cross_attention_kwargsr%  s    r7   cross_attention_kwargs8StableDiffusionPAGImg2ImgPipeline.cross_attention_kwargs  s    +++r9   c                     U R                   $ r   )_num_timestepsr%  s    r7   num_timesteps/StableDiffusionPAGImg2ImgPipeline.num_timesteps  s    """r9   c                     U R                   $ r   )
_interruptr%  s    r7   	interrupt+StableDiffusionPAGImg2ImgPipeline.interrupt
  r+  r9   g?2   g      @r*   g        r   r3   g      @r   r   r   r:   r<   r=   r&  r   r   r   r-   r   r   r   return_dictr1  callback_on_step_endr   	pag_scalepag_adaptive_scalec                    [        U[        [        45      (       a  UR                  nU R	                  UUUUUUUU5        Xpl        UU l        UU l        SU l        UU l	        UU l
        Ub  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [        U5      nOUR                  S   nU R                  nU R                   b  U R                   R#                  SS5      OSnU R%                  UUU	U R&                  UUUUU R(                  S9	u  pU R*                  (       a  U R-                  XU R&                  5      nO(U R&                  (       a  [.        R0                  " X/5      nUc  Ub  U R3                  UUUUU	-  U R&                  5      n[5        U5       H  u  nnSnU R&                  (       a  UR7                  S5      u  nnU R*                  (       a  U R-                  UUU R&                  5      nO(U R&                  (       a  [.        R0                  " UU/SS9nUR9                  U5      nUUU'   M     U R:                  R=                  U5      n[?        U R@                  UUXV5      u  pTU RC                  XCU5      u  pTUSS RE                  UU	-  5      nU RG                  UUUU	URH                  UU5      nU RK                  X5      n Uc  Ub  S	W0OSn!Sn"U RL                  RN                  RP                  b{  [.        RR                  " U RT                  S-
  5      RE                  UU	-  5      n#U RW                  U#U RL                  RN                  RP                  S
9R9                  UURH                  S9n"[        U5      X@R@                  RX                  -  -
  n$U R*                  (       a:  U RL                  RZ                  n%U R]                  U R^                  U R&                  S9  [        U5      U l0        [c        [        U5      X@R@                  RX                  -  -
  S5      n$U Re                  US9 n&[5        U5       GH  u  nn'U Rf                  (       a  M  [.        R0                  " U/UR                  S   UR                  S   -  -  5      n(U R@                  Ri                  U(U'5      n(Ub  UU!S	'   U RM                  U(U'UU"U R                   U!SS9S   n)U R*                  (       a)  U Rk                  U)U R&                  U RT                  U'5      n)O:U R&                  (       a)  U)R7                  S5      u  n*n+U*U RT                  U+U*-
  -  -   n)U R@                  Rl                  " U)U'U40 U DSS0D6S   nUb\  0 n,U H  n-[o        5       U-   U,U-'   M     U" U UU'U,5      n.U.Rq                  SU5      nU.Rq                  SU5      nU.Rq                  SU5      nU[        U5      S-
  :X  d)  US-   U$:  a0  US-   U R@                  RX                  -  S:X  a  U&Rs                  5         [t        (       d  GM  [v        Rx                  " 5         GM     SSS5        US:X  d`  U Rz                  R}                  UU Rz                  RN                  R~                  -  SUS9S   nU R                  UUURH                  5      u  nn/OUnSn/U/c  S/UR                  S   -  n0OU/ V1s/ s H
  n1U1(       + PM     n0n1U R:                  R                  UUU0S9nU R                  5         U R*                  (       a  U RL                  R                  W%5        U(       d  UU/4$ [        UU/S9$ ! , (       d  f       GN= fs  sn1f )uU  
The call function to the pipeline for generation.

Args:
    prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts to guide image generation. If not defined, you need to pass `prompt_embeds`.
    image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `List[torch.Tensor]`, `List[PIL.Image.Image]`, or `List[np.ndarray]`):
        `Image`, numpy array or tensor representing an image batch to be used as the starting point. For both
        numpy array and pytorch tensor, the expected value range is between `[0, 1]` If it's a tensor or a list
        or tensors, the expected shape should be `(B, C, H, W)` or `(C, H, W)`. If it is a numpy array or a
        list of arrays, the expected shape should be `(B, H, W, C)` or `(H, W, C)` It can also accept image
        latents as `image`, but if passing latents directly it is not encoded again.
    strength (`float`, *optional*, defaults to 0.8):
        Indicates extent to transform the reference `image`. Must be between 0 and 1. `image` is used as a
        starting point and more noise is added the higher the `strength`. The number of denoising steps depends
        on the amount of noise initially added. When `strength` is 1, added noise is maximum and the denoising
        process runs for the full number of iterations specified in `num_inference_steps`. A value of 1
        essentially ignores `image`.
    num_inference_steps (`int`, *optional*, defaults to 50):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference. This parameter is modulated by `strength`.
    timesteps (`List[int]`, *optional*):
        Custom timesteps to use for the denoising process with schedulers which support a `timesteps` argument
        in their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is
        passed will be used. Must be in descending order.
    sigmas (`List[float]`, *optional*):
        Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
        their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
        will be used.
    guidance_scale (`float`, *optional*, defaults to 7.5):
        A higher guidance scale value encourages the model to generate images closely linked to the text
        `prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
    negative_prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts to guide what to not include in image generation. If not defined, you need to
        pass `negative_prompt_embeds` instead. Ignored when not using guidance (`guidance_scale < 1`).
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        The number of images to generate per prompt.
    eta (`float`, *optional*, defaults to 0.0):
        Corresponds to parameter eta (η) from the [DDIM](https://huggingface.co/papers/2010.02502) paper. Only
        applies to the [`~schedulers.DDIMScheduler`], and is ignored in other schedulers.
    generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
        A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
        generation deterministic.
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs (prompt weighting). If not
        provided, text embeddings are generated from the `prompt` input argument.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
        not provided, `negative_prompt_embeds` are generated from the `negative_prompt` input argument.
    ip_adapter_image: (`PipelineImageInput`, *optional*): Optional image input to work with IP Adapters.
    ip_adapter_image_embeds (`List[torch.Tensor]`, *optional*):
        Pre-generated image embeddings for IP-Adapter. It should be a list of length same as number of
        IP-adapters. Each element should be a tensor of shape `(batch_size, num_images, emb_dim)`. It should
        contain the negative image embedding if `do_classifier_free_guidance` is set to `True`. If not
        provided, embeddings are computed from the `ip_adapter_image` input argument.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generated image. Choose between `PIL.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] instead of a
        plain tuple.
    cross_attention_kwargs (`dict`, *optional*):
        A kwargs dictionary that if specified is passed along to the [`AttentionProcessor`] as defined in
        [`self.processor`](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).
    clip_skip (`int`, *optional*):
        Number of layers to be skipped from CLIP while computing the prompt embeddings. A value of 1 means that
        the output of the pre-final layer will be used for computing the prompt embeddings.
    callback_on_step_end (`Callable`, `PipelineCallback`, `MultiPipelineCallbacks`, *optional*):
        A function or a subclass of `PipelineCallback` or `MultiPipelineCallbacks` that is called at the end of
        each denoising step during the inference. with the following arguments: `callback_on_step_end(self:
        DiffusionPipeline, step: int, timestep: int, callback_kwargs: Dict)`. `callback_kwargs` will include a
        list of all tensors as specified by `callback_on_step_end_tensor_inputs`.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
        will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
        `._callback_tensor_inputs` attribute of your pipeline class.
    pag_scale (`float`, *optional*, defaults to 3.0):
        The scale factor for the perturbed attention guidance. If it is set to 0.0, the perturbed attention
        guidance will not be used.
    pag_adaptive_scale (`float`, *optional*, defaults to 0.0):
        The adaptive scale factor for the perturbed attention guidance. If it is set to 0.0, `pag_scale` is
        used.

Examples:

Returns:
    [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
        otherwise a `tuple` is returned where the first element is a list with the generated images and the
        second element is a list of `bool`s indicating whether the corresponding generated image contains
        "not-safe-for-work" (nsfw) content.
FNr*   r   scale)rT   rU   r|   r}   r%   r   r   )r  r   )r[   r   )total)encoder_hidden_statestimestep_condr1  added_cond_kwargsr<  r<  r3   rT   rU   latent)r<  r-   T)r   do_denormalize)r   nsfw_content_detected)Er   r   r   tensor_inputsr   r$  r)  r0  r8  
_pag_scale_pag_adaptive_scaler   r   rH   r   _execution_devicer1  getr   r   r}   do_perturbed_attention_guidance%_prepare_perturbed_attention_guidancer   r   r   r   r   r   rs   
preprocessrM   rI   r  r   r  r   r   rY   rj   r-  r  r&  r!  r   attn_processors_set_pag_attn_processorr[   r4  r   progress_barr9  scale_model_input#_apply_perturbed_attention_guidancer   localspopupdateXLA_AVAILABLExm	mark_steprV   decoder
  r   r   maybe_free_model_hooksset_attn_processorr(   )2rv   r   r   r   r:   r<   r=   r&  r   r   r   r-   rT   rU   r   r   r   r<  r1  r}   r=  r   r>  r?  r   r;   text_encoder_lora_scaler   r   r   latent_timestepr3   r   rE  rD  guidance_scale_tensornum_warmup_stepsoriginal_attn_procrS  tlatent_model_input
noise_prednoise_pred_uncondnoise_pred_textcallback_kwargsr   callback_outputsr   rG  has_nsfws2                                                     r7   __call__*StableDiffusionPAGImg2ImgPipeline.__call__  su   t *-=?U,VWW1E1S1S. 	"#.		
  .#'=$##5  *VS"9"9JJvt$<$<VJ&,,Q/J'' ?C>Y>Y>eD''++GT:ko 	  150B0B!,,'#9.nn 1C 
1
- // FFt7W7WM --!II'=&MNM'+B+N&*&J&J '2200'# $--D#E<(,%33:F:L:LQ:O7)<77#'#M#M$&;T=]=]$L 55#(99.C\-RXY#ZL+v6-9'* $F $$//6 *<NN/*
&	 *.););<O[a)b&	#BQ-..z<Q/QR &&!
 !::9J
  +/F/R \* 	 99..:$)LL1D1Dq1H$I$P$PQ[^sQs$t! ==%TYY5E5E5X5X > bgmmb4 
 y>,?..BVBV,VV//!%!:!:((#'#:#:,0,L,L )  ")ns9~0CnnFZFZ0ZZ\]^%89\!),1>> &+YYyM<O<OPQ<RV]VcVcdeVf<f/g%h"%)^^%E%EFXZ[%\" +68O%n5!YY&*7"/+/+F+F&7 % '  
 77!%!I!I"D$D$DdFYFY[\"J 559C9I9I!9L6%!2T5H5HO^oLo5p!pJ ..--j!WmHYmglmnop'3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM-=-A-ABZ\r-s*I**A9I/IqSTuX\XfXfXlXlNlpqNq '') =LLN_ - :d h&HHOOGdhhoo.L.L$LZ_ktOuE '+&=&=eV]M`M`&a#E#E##"Vekk!n4N;KL;Kx(l;KNL$$00K`n0o 	##%//II(();<+,,,EQabbW :9| Ms   :G0]/]]'
]$)
r)  r0  r$  r8  r   r4  rK  rJ  rs   rf   )NTmid)NNNNNr   )NNNNNN):__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seq_optional_components_exclude_from_cpu_offloadr   r   r   r   r   r   r)   r
   r   boolr   r   r   rh   r   r   r   floatr   r   r   r   r   r   r   r  r  float32r   r!  propertyr&  r}   r   r1  r5  r9  no_gradr!   EXAMPLE_DOC_STRINGr   	Generatorr   r   r   r   r   rl  __static_attributes____classcell__)rG   s   @r7   rO   rO      sD   B ES!1 2T 8<(,49a8a8 $a8 !	a8
 #a8 -a8 5a8 .a8 5a8 "&a8 "#tCy.1a8 a8T 049=&*#'t5  -t5 !) 6t5 UOt5 C=t5n52+'\'!* # $+/;|	8?F 58emm.1@E	< $ $   X X , , # #   ]]_12 )-$(-/#"*-;?/0"MQ049=9=@D%* ;? 9B$'5Fcc49n%Fc "Fc 	Fc
 &c]Fc 9Fc UFc !Fc "%T#Y"78Fc  (}Fc e_Fc E%//43H"HIJFc  -Fc !) 6Fc ##56Fc  "*$u||*<!=!Fc" c]#Fc$ %Fc& !)c3h 8'Fc( )Fc* '(Cd+T124DF\\]
+Fc0 -1I1Fc2 3Fc4 "5Fc 3 Fcr9   rO   )Nr1   )NNNN)LrB   typingr   r   r   r   r   r   	PIL.Imager  r   	packagingr	   transformersr
   r   r   r   	callbacksr   r   configuration_utilsr   rs   r   r   loadersr   r   r   r   modelsr   r   r   models.lorar   
schedulersr   utilsr   r   r   r    r!   r"   r#   utils.torch_utilsr$   pipeline_utilsr&   r'    stable_diffusion.pipeline_outputr(   stable_diffusion.safety_checkerr)   	pag_utilsr+   torch_xla.core.xla_modelcore	xla_modelrZ  rY  
get_loggerro  rm   r|  r   r}  r   r8   r   r;   rx  rM   rO   r?   r9   r7   <module>r     sH    = =    h h A - D w w J J 9 3   . D L J  ))MM			H	% 0 ck
TLL
T-5eoo-F
T\_
T  *.15%)$(8*!#8* U3,-.8* S	"	8*
 T%[!8*v~c"~cr9   