
    
3jPA             
       :   S SK r S SKJrJr  S SKrS SKrS SKrS SK	J
s  Jr  S SKJrJrJrJrJr  S SKJr  SSKJr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#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/J0r0  SSK1J2r2J3r3J4r4  SSK5J6r6J7r7  SSK8J9r9  \" 5       (       a  SSK:J;r;  SSK*J<r<  \<" 5       (       a  S SK=J>s  J?r@  SrAOSrA\-R                  " \C5      rDSrE    SS\FS-  S\G\R                  -  S-  S\I\F   S-  S\I\J   S-  4S jjrK " S S\6\7\\\\5      rLg)    N)AnyCallable)CLIPImageProcessorCLIPTextModelCLIPTextModelWithProjectionCLIPTokenizerCLIPVisionModelWithProjection) is_invisible_watermark_available   )MultiPipelineCallbacksPipelineCallback)PipelineImageInputVaeImageProcessor)FromSingleFileMixinIPAdapterMixin StableDiffusionXLLoraLoaderMixinTextualInversionLoaderMixin)AutoencoderKLControlNetModelImageProjectionMultiControlNetModelUNet2DConditionModel)adjust_lora_scale_text_encoder)KarrasDiffusionSchedulers)USE_PEFT_BACKEND	deprecateloggingreplace_example_docstringscale_lora_layersunscale_lora_layers)is_compiled_moduleis_torch_versionrandn_tensor   )DiffusionPipelineStableDiffusionMixin)StableDiffusionXLPipelineOutput)StableDiffusionXLWatermarker)is_torch_xla_availableTFa/  
    Examples:
        ```py
        >>> # !pip install opencv-python transformers accelerate
        >>> from diffusers import StableDiffusionXLControlNetPipeline, ControlNetModel, AutoencoderKL
        >>> from diffusers.utils import load_image
        >>> import numpy as np
        >>> import torch

        >>> import cv2
        >>> from PIL import Image

        >>> prompt = "aerial view, a futuristic research complex in a bright foggy jungle, hard lighting"
        >>> negative_prompt = "low quality, bad quality, sketches"

        >>> # download an image
        >>> image = load_image(
        ...     "https://hf.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/hf-logo.png"
        ... )

        >>> # initialize the models and pipeline
        >>> controlnet_conditioning_scale = 0.5  # recommended for good generalization
        >>> controlnet = ControlNetModel.from_pretrained(
        ...     "diffusers/controlnet-canny-sdxl-1.0", torch_dtype=torch.float16
        ... )
        >>> vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", torch_dtype=torch.float16)
        >>> pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
        ...     "stabilityai/stable-diffusion-xl-base-1.0", controlnet=controlnet, vae=vae, torch_dtype=torch.float16
        ... )
        >>> pipe.enable_model_cpu_offload()

        >>> # get canny image
        >>> image = np.array(image)
        >>> image = cv2.Canny(image, 100, 200)
        >>> image = image[:, :, None]
        >>> image = np.concatenate([image, image, image], axis=2)
        >>> canny_image = Image.fromarray(image)

        >>> # generate image
        >>> image = pipe(
        ...     prompt, controlnet_conditioning_scale=controlnet_conditioning_scale, image=canny_image
        ... ).images[0]
        ```
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           r/home/wildlama/miniconda3/lib/python3.13/site-packages/diffusers/pipelines/controlnet/pipeline_controlnet_sd_xl.pyretrieve_timestepsr>   y   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''	))    c            P         ^  \ rS rSrSrSr/ SQr/ SQr    S_S\S	\	S
\
S\S\S\S\\\   -  \\   -  \-  S\S\S\S-  S\S\4U 4S jjjr            S`S\S\S-  S\R0                  S-  S\S\S\S-  S\S-  S\R4                  S-  S\R4                  S-  S\R4                  S-  S \R4                  S-  S!\S-  S"\S-  4S# jjrSaS$ jrS% rS& r            SbS) jr S* r!  ScS, jr"SaS- jr# SaS. jr$S/ r%S0\RL                  4S1\R4                  S2\S3\RN                  S4\R4                  4S5 jjr(\)S6 5       r*\)S7 5       r+\)S8 5       r,\)S9 5       r-\)S: 5       r.\)S; 5       r/\)S< 5       r0\Rb                  " 5       \2" \35      SSSSSS=SSSS>SSSS(SSSSSSSSS?SSS'S+S(S'SS@SSS@SSSSA/4&S\\\   -  S\\\   -  S-  SB\4SC\S-  SD\S-  SE\SF\\   SG\\   SH\S-  SI\S\\\   -  S-  S\\\   -  S-  S\S-  SJ\SK\Rj                  \\Rj                     -  S-  SA\R4                  S-  S\R4                  S-  S\R4                  S-  S\R4                  S-  S \R4                  S-  SL\4S-  SM\\R4                     S-  SN\S-  SO\SP\6\\74   S-  SQ\\\   -  SR\SS\\\   -  ST\\\   -  SU\\\4   SV\\\4   SW\\\4   SX\\\4   S-  SY\\\4   SZ\\\4   S-  S"\S-  S[\8\\/S4   \9-  \:-  S-  S\\\   4LS] jj5       5       r;S^r<U =r=$ )d#StableDiffusionXLControlNetPipeline   ay
  
Pipeline for text-to-image generation using Stable Diffusion XL with ControlNet guidance.

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.StableDiffusionXLLoraLoaderMixin.load_lora_weights`] for loading LoRA weights
    - [`~loaders.StableDiffusionXLLoraLoaderMixin.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)).
    text_encoder_2 ([`~transformers.CLIPTextModelWithProjection`]):
        Second frozen text-encoder
        ([laion/CLIP-ViT-bigG-14-laion2B-39B-b160k](https://huggingface.co/laion/CLIP-ViT-bigG-14-laion2B-39B-b160k)).
    tokenizer ([`~transformers.CLIPTokenizer`]):
        A `CLIPTokenizer` to tokenize text.
    tokenizer_2 ([`~transformers.CLIPTokenizer`]):
        A `CLIPTokenizer` to tokenize text.
    unet ([`UNet2DConditionModel`]):
        A `UNet2DConditionModel` to denoise the encoded image latents.
    controlnet ([`ControlNetModel`] or `list[ControlNetModel]`):
        Provides additional conditioning to the `unet` during the denoising process. If you set multiple
        ControlNets as a list, the outputs from each ControlNet are added together to create one combined
        additional conditioning.
    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`].
    force_zeros_for_empty_prompt (`bool`, *optional*, defaults to `"True"`):
        Whether the negative prompt embeddings should always be set to 0. Also see the config of
        `stabilityai/stable-diffusion-xl-base-1-0`.
    add_watermarker (`bool`, *optional*):
        Whether to use the [invisible_watermark](https://github.com/ShieldMnt/invisible-watermark/) library to
        watermark output images. If not defined, it defaults to `True` if the package is installed; otherwise no
        watermarker is used.
z6text_encoder->text_encoder_2->image_encoder->unet->vae)	tokenizertokenizer_2text_encodertext_encoder_2feature_extractorimage_encoder)latentsprompt_embedsnegative_prompt_embedsadd_text_embedsadd_time_idsnegative_pooled_prompt_embedsnegative_add_time_idsimageTNvaerE   rF   rC   rD   unet
controlnetr9   force_zeros_for_empty_promptadd_watermarkerrG   rH   c                   > [         TU ]  5         [        U[        [        45      (       a  [        U5      nU R                  UU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        [        U R                  SS9U l        [        U R                  SSS	9U l        U
b  U
O	[!        5       n
U
(       a  [#        5       U l        OS U l        U R'                  U	S
9  g )N)
rQ   rE   rF   rC   rD   rR   rS   r9   rG   rH   rQ   r$         T)vae_scale_factordo_convert_rgbF)rY   rZ   do_normalize)rT   )super__init__
isinstancelisttupler   register_modulesgetattrr8   rQ   configblock_out_channelsrY   r   image_processorcontrol_image_processorr
   r(   	watermarkregister_to_config)selfrQ   rE   rF   rC   rD   rR   rS   r9   rT   rU   rG   rH   r7   s                r=   r]   ,StableDiffusionXLControlNetPipeline.__init__   s    	j4-00-j9J%)#!/' 	 	
 W^^bdikoVpVpc$((//*L*L&MPQ&Q Rvw0$BWBWhlm'8!224V[(
$ .=-H/NnNp9;DN!DN=YZr?   rW   promptprompt_2r+   num_images_per_promptdo_classifier_free_guidancenegative_promptnegative_prompt_2rJ   rK   pooled_prompt_embedsrN   
lora_scale	clip_skipc                    U=(       d    U R                   nUb  [        U [        5      (       a  Xl        U R                  b8  [
        (       d  [        U R                  U5        O[        U R                  U5        U R                  b8  [
        (       d  [        U R                  U5        O[        U R                  U5        [        U[        5      (       a  U/OUnUb  [        U5      nOUR                  S   nU R                  b  U R                  U R                  /OU R                  /nU R                  b  U R                  U R                  /OU R                  /nUGc  U=(       d    Un[        U[        5      (       a  U/OUn/ nX/n[        UUU5       GHQ  u  nnn[        U [        5      (       a  U R!                  UU5      nU" USUR"                  SSS9nUR$                  nU" USSS9R$                  nUR                  S	   UR                  S	   :  ah  [&        R(                  " UU5      (       dL  UR+                  USS2UR"                  S
-
  S	24   5      n[,        R/                  SUR"                   SU 35        U" UR1                  U5      SS9nU
c  US   R2                  S:X  a  US   n
Uc  UR4                  S   nOUR4                  US-   *    nUR7                  U5        GMT     [&        R8                  " US	S9nUSL =(       a    U R:                  R<                  nU(       a8  U	c5  U(       a.  [&        R>                  " U5      n	[&        R>                  " U
5      nGOU(       Ga  U	Gc  U=(       d    SnU=(       d    Un[        U[        5      (       a  X/-  OUn[        U[        5      (       a  X/-  OUnUb;  [A        U5      [A        U5      La$  [C        S[A        U5       S[A        U5       S35      eU[        U5      :w  a!  [E        SU S[        U5       SU SU S3	5      eXg/n/ n[        UUU5       H  u  nnn[        U [        5      (       a  U R!                  UU5      nUR                  S
   nU" USUSSS9nU" UR$                  R1                  U5      SS9n	Uc  U	S   R2                  S:X  a  U	S   nU	R4                  S   n	UR7                  U	5        M     [&        R8                  " US	S9n	U R                  b%  UR1                  U R                  RF                  US9nO$UR1                  U RH                  RF                  US9nUR                  u  nnn URK                  S
US
5      nURM                  UU-  US	5      nU(       a  U	R                  S
   nU R                  b%  U	R1                  U R                  RF                  US9n	O$U	R1                  U RH                  RF                  US9n	U	RK                  S
US
5      n	U	RM                  X-  US	5      n	U
RK                  S
U5      RM                  UU-  S	5      n
U(       a%  URK                  S
U5      RM                  UU-  S	5      nU R                  b6  [        U [        5      (       a!  [
        (       a  [O        U R                  U5        U R                  b6  [        U [        5      (       a!  [
        (       a  [O        U R                  U5        XX4$ )a
  
Encodes the prompt into text encoder hidden states.

Args:
    prompt (`str` or `list[str]`, *optional*):
        prompt to be encoded
    prompt_2 (`str` or `list[str]`, *optional*):
        The prompt or prompts to be sent to the `tokenizer_2` and `text_encoder_2`. If not defined, `prompt` is
        used in both text-encoders
    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`).
    negative_prompt_2 (`str` or `list[str]`, *optional*):
        The prompt or prompts not to guide the image generation to be sent to `tokenizer_2` and
        `text_encoder_2`. If not defined, `negative_prompt` is used in both text-encoders
    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.
    pooled_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting.
        If not provided, pooled text embeddings will be generated from `prompt` input argument.
    negative_pooled_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt
        weighting. If not provided, pooled 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   
max_lengthTpt)paddingru   
truncationreturn_tensorslongest)rw   ry   rW   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: output_hidden_statesr$   dim 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`.)dtyper+   )(_execution_devicer^   r   _lora_scalerE   r   r   r   rF   strr8   shaperC   rD   zipr   maybe_convert_promptmodel_max_length	input_idstorchequalbatch_decodeloggerwarningtondimhidden_statesappendconcatrc   rT   
zeros_liketype	TypeErrorr0   r   rR   repeatviewr    )!ri   rk   rl   r+   rm   rn   ro   rp   rJ   rK   rq   rN   rr   rs   
batch_size
tokenizerstext_encodersprompt_embeds_listpromptsrC   rE   text_inputstext_input_idsuntruncated_idsremoved_textzero_out_negative_promptuncond_tokensnegative_prompt_embeds_listru   uncond_inputbs_embedseq_len_s!                                    r=   encode_prompt1StableDiffusionXLControlNetPipeline.encode_prompt+  s   t 1411 !j7W&X&X)   ,''243D3DjQ%d&7&7D"".''243F3F
S%d&9&9:F'44&&VJ&,,Q/J <@>>;Udnnd&6&67\`\l\l[m
8<8I8I8UT 3 34\`\o\o[p 	  )6H%/#%>%>zHH "$(G36w
M3Z/	<d$?@@!66vyIF'((99##' "-!6!6"+FIVZ"["e"e"((,0D0DR0HHQVQ\Q\"OR R $-#9#9/!YMgMgjkMknpMpJp:q#rLNN%667yP
 !-^->->v-F]a b (/M!4D4I4IQ4N+8+;($$1$?$?$CM %2$?$?)a-@P$QM"))-8I 4[L "LL);DM $3d#:#gt{{?g?g &+A+INf%*%5%5m%D",1,<,<=Q,R)(-C-K-3O 1 D_ AK?\_@`@`j+<<fuO4>?PRU4V4V
00\m 
 !d6l$:O&OUVZ[jVkUl mV~Q(  s?33 )/)::J3K_J` ax/
| <33  "1 D*,'<?z[h<i8Ld$?@@&*&?&?QZ&[O*003
(#()##'  *6 **--f5)-*& 18=STU=V=[=[_`=`4J14M1)?)M)Mb)Q&+223IJ/ =j2 &+\\2MSU%V"*),,43F3F3L3LU[,\M),,499??6,RM,22'1%,,Q0EqI%**86K+KWVXY&,2215G"".)?)B)BI\I\IbIbkq)B)r&)?)B)Bag)B)h&%;%B%B1F[]^%_"%;%@%@Acelnp%q"3::1>STYY,,b 
 ',I,P,PQRTi,j,o,o00"-) ($ @AAFVFV#D$5$5zB*$ @AAFVFV#D$7$7D6Jiir?   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$ )	Nrv   )ry   r+   r   Tr|   r~   r   r   )nextrH   r5   r   r^   r   TensorrG   pixel_valuesr   r   repeat_interleaver   image_embeds)
ri   rP   r+   rm   r}   r   image_enc_hidden_statesuncond_image_enc_hidden_statesr   uncond_image_embedss
             r=   encode_image0StableDiffusionXLControlNetPipeline.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4r?   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  images and z IP Adapters.rW   r$   r   r   )r+   )r^   r_   r8   rR   encoder_hid_projimage_projection_layersr0   r   r   r   r   chunk	enumerater   catr   )ri   ip_adapter_imageip_adapter_image_embedsr+   rm   rn   r   negative_image_embedssingle_ip_adapter_imageimage_proj_layeroutput_hidden_statesingle_image_embedssingle_negative_image_embedsis                 r=   prepare_ip_adapter_image_embedsCStableDiffusionXLControlNetPipeline.prepare_ip_adapter_image_embeds3  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#**+>? '> '&r?   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eta	generator)r1   r2   r3   r9   stepr5   r6   )ri   r   r   accepts_etaextra_step_kwargsaccepts_generators         r=   prepare_extra_step_kwargs=StableDiffusionXLControlNetPipeline.prepare_extra_step_kwargsa  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r?         ?        c                   ^  Ub6  [        U[        5      (       a  US::  a  [        SU S[        U5       S35      eUbX  [	        U 4S jU 5       5      (       d>  [        ST R
                   SU Vs/ s H  nUT R
                  ;  d  M  UPM     sn 35      eUb  Ub  [        SU S	U S
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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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	c  [        S5      eUb  Uc  [        S5      e[        T R                  [        5      (       aW  [        U[        5      (       aB  [        R                  S[        T R                  R                  5       S[        U5       S35        [        [         S5      =(       a8    [        T R                  ["        R$                  R&                  R(                  5      n[        T R                  [*        5      (       d0  U(       a=  [        T R                  R,                  [*        5      (       a  T R/                  X1U5        GO[        T R                  [        5      (       d0  U(       a  [        T R                  R,                  [        5      (       a  [        U[        5      (       d  [1        S5      e[3        S U 5       5      (       a  [        S5      e[        U5      [        T R                  R                  5      :w  a8  [        S[        U5       S[        T R                  R                  5       S35      eU H  nT R/                  UX5        M     O e[        T R                  [*        5      (       d0  U(       aJ  [        T R                  R,                  [*        5      (       a!  [        U[4        5      (       d  [1        S 5      eO[        T R                  [        5      (       d0  U(       a  [        T R                  R,                  [        5      (       a  [        U[        5      (       a#  [3        S! U 5       5      (       a  [        S5      eOO[        U[        5      (       a7  [        U5      [        T R                  R                  5      :w  a  [        S"5      eO e[        U[6        [        45      (       d  U/n[        U[6        [        45      (       d  U/n[        U5      [        U5      :w  a$  [        S#[        U5       S$[        U5       S%35      e[        T R                  [        5      (       a  [        U5      [        T R                  R                  5      :w  a[  [        S&U S'[        U5       S([        T R                  R                  5       S)[        T R                  R                  5       S3	5      e[9        X5       HH  u  nnUU:  a  [        S*U S+U S35      eUS,:  a  [        S*U S-35      eUS.:  d  M;  [        S/U S035      e   U
b  Ub  [        S15      eUb\  [        U[        5      (       d  [        S2[        U5       35      eUS   R:                  S3;  a  [        S4US   R:                   S535      eg g s  snf )6Nr   z5`callback_steps` has to be a positive integer but is z	 of type r   c              3   @   >#    U  H  oTR                   ;   v   M     g 7fN)_callback_tensor_inputs).0kri   s     r=   	<genexpr>CStableDiffusionXLControlNetPipeline.check_inputs.<locals>.<genexpr>  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.z Cannot forward both `prompt_2`: 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 z4`prompt_2` has to be of type `str` or `list` but is z'Cannot forward both `negative_prompt`: z and `negative_prompt_embeds`: z)Cannot forward both `negative_prompt_2`: zu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` zIf `prompt_embeds` are provided, `pooled_prompt_embeds` also have to be passed. Make sure to generate `pooled_prompt_embeds` from the same text encoder that was used to generate `prompt_embeds`.zIf `negative_prompt_embeds` are provided, `negative_pooled_prompt_embeds` also have to be passed. Make sure to generate `negative_pooled_prompt_embeds` from the same text encoder that was used to generate `negative_prompt_embeds`.z	You have z! ControlNets and you have passed z= prompts. The conditionings will be fixed across the prompts.scaled_dot_product_attentionz5For multiple controlnets: `image` must be type `list`c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r^   r_   r   r   s     r=   r   r     s     8%QZ4((%   zEA single batch of multiple conditionings are supported at the moment.zbFor multiple controlnets: `image` must have the same length as the number of controlnets, but got r   z ControlNets.zLFor single controlnet: `controlnet_conditioning_scale` must be type `float`.c              3   B   #    U  H  n[        U[        5      v   M     g 7fr   r   r   s     r=   r   r     s     R4Qqz!T**4Qr   zFor multiple controlnets: When `controlnet_conditioning_scale` is specified as `list`, it must have the same length as the number of controlnetsz`control_guidance_start` has z* elements, but `control_guidance_end` has zI elements. Make sure to provide the same number of elements to each list.z`control_guidance_start`: z has z elements but there are z- controlnets available. Make sure to provide zcontrol guidance start: z4 cannot be larger or equal to control guidance end: r   z can't be smaller than 0.r   zcontrol guidance end: z can't be larger than 1.0.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^   intr0   r   allr   r   r_   r   rS   r   r   r   r8   netshasattrFr   _dynamo
eval_frameOptimizedModuler   	_orig_modcheck_imager   anyfloatr`   r   r   )ri   rk   rl   rP   callback_stepsro   rp   rJ   rK   rq   r   r   rN   controlnet_conditioning_scalecontrol_guidance_startcontrol_guidance_end"callback_on_step_end_tensor_inputsr   is_compiledimage_startends   `                     r=   check_inputs0StableDiffusionXLControlNetPipeline.check_inputsr  sc   & %z.#/N/NR`deReGGW X(), 
 .9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  bc  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  !m&?28*<RS`Ra b0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa!:h+D+DZX`bfMgMgSTXYaTbScdee&+A+M9/9J K*++]_  */E/Q;<M;N O*++]_ 
 $)?)K""&<&B&BB --:-@-@,A B.445Q8  $)=)E U  "-2O2W y  doo';<<&$''DOO$8$8 9::[\_`f\g[hST a!?@ 
ZOOU]]55EEF
 t884??44oFFUM:t(<==4??446JKKeT** WXX 8%888 !hiiUs4??#7#788 xy|  ~C  zD  yE  EQ  RU  VZ  Ve  Ve  Vj  Vj  Rk  Ql  ly  z     ?   5 t884??44oFF;UCC noo D t(<==4??446JKK7>>R4QRRR$%lmm S94@@SIfEgkn$$l F !D 
 505$-@@&<%=".>>$8#9 %&#.B*CC/4J0K/LLvwz  |P  xQ  wR  R[  \  doo';<<)*c$//2F2F.GG 01G0HcRhNiMj  kC  DG  HL  HW  HW  H\  H\  D]  C^  ^K  LO  PT  P_  P_  Pd  Pd  Le  Kf  fg  h  4KJE3| .ug5ijminnop  s{ #;E7B[!\]]Sy #9#>X!YZZ L ',C,O ^  #.5t<< PQUVmQnPop  )+00> \]tuv]w]|]|\}}~  ? /m pHs   /]]c                    [        U[        R                  R                  5      n[        U[        R                  5      n[        U[
        R                  5      n[        U[        5      =(       a'    [        US   [        R                  R                  5      n[        U[        5      =(       a    [        US   [        R                  5      n[        U[        5      =(       a    [        US   [
        R                  5      n	U(       d:  U(       d3  U(       d,  U(       d%  U(       d  U	(       d  [        S[        U5       35      eU(       a  Sn
O[        U5      n
Ub  [        U[        5      (       a  SnO6Ub!  [        U[        5      (       a  [        U5      nOUb  UR                  S   nU
S:w  a  U
W:w  a  [        SU
 SU 35      eg g )Nr   zimage must be passed and be one of PIL image, numpy array, torch tensor, list of PIL images, list of numpy arrays or list of torch tensors, but is rW   zdIf image batch size is not 1, image batch size must be same as prompt batch size. image batch size: z, prompt batch size: )r^   PILImager   r   npndarrayr_   r   r   r8   r   r   r0   )ri   rP   rk   rJ   image_is_pilimage_is_tensorimage_is_npimage_is_pil_listimage_is_tensor_listimage_is_np_listimage_batch_sizeprompt_batch_sizes               r=   r   /StableDiffusionXLControlNetPipeline.check_image0  s   !%9$UELL9 

3&ud3]
58SYY__8])%6]:eAhPUP\P\;]%eT2Wz%(BJJ7W #%($ f  gk  lq  gr  fs  t   "5z*VS"9"9 !Jvt$<$< #F& - 3 3A 6q %59J%Jv  xH  wI  I^  _p  ^q  r  &K r?   Fc
                 2   U R                   R                  XUS9R                  [        R                  S9nUR
                  S   n
U
S:X  a  UnOUnUR                  USS9nUR                  XgS9nU(       a!  U	(       d  [        R                  " U/S-  5      nU$ )N)heightwidthr   r   rW   r   r   r$   )rf   
preprocessr   r   float32r   r   r   )ri   rP   r  r  r   rm   r+   r   rn   
guess_moder  	repeat_bys               r=   prepare_image1StableDiffusionXLControlNetPipeline.prepare_imageV  s     ,,77TY7Z]]didqdq]r ;;q>q "I .I''	q'94&zIIugk*Er?   c	                 V   UU[        U5      U R                  -  [        U5      U R                  -  4n	[        U[        5      (       a*  [	        U5      U:w  a  [        S[	        U5       SU S35      eUc  [        XXeS9nOUR                  U5      nXR                  R                  -  nU$ )Nz/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+   r   )
r   rY   r^   r_   r8   r0   r#   r   r9   init_noise_sigma)
ri   r   num_channels_latentsr  r  r   r+   r   rI   r   s
             r=   prepare_latents3StableDiffusionXLControlNetPipeline.prepare_latentsu  s     K4000J$///	
 i&&3y>Z+GA#i.AQ R&<'gi 
 ?"5fZGjj(G NN;;;r?   c                 2   [        X-   U-   5      nU R                  R                  R                  [	        U5      -  U-   nU R                  R
                  R                  R                  nX:w  a  [        SU SU S35      e[        R                  " U/US9nU$ )Nz7Model expects an added time embedding vector of length z, but a vector of z was created. The model has an incorrect config. Please check `unet.config.time_embedding_type` and `text_encoder_2.config.projection_dim`.r  )r_   rR   rc   addition_time_embed_dimr8   add_embeddinglinear_1in_featuresr0   r   tensor)	ri   original_sizecrops_coords_top_lefttarget_sizer   text_encoder_projection_dimrM   passed_add_embed_dimexpected_add_embed_dims	            r=   _get_add_time_ids5StableDiffusionXLControlNetPipeline._get_add_time_ids  s     MAKOP II44s<7HHKff 	 "&!8!8!A!A!M!M!9IJ`Iaas  uI  tJ  JU  V  ||\N%@r?   c                 l    [        SSS5        U R                  R                  [        R                  S9  g )N
upcast_vae1.0.0z`upcast_vae` is deprecated. Please use `pipe.vae.to(torch.float32)`. For more details, please refer to: https://github.com/huggingface/diffusers/pull/12619#issue-3606633695.r  )r   rQ   r   r   r  ri   s    r=   r)  .StableDiffusionXLControlNetPipeline.upcast_vae  s.     |	

 	%--(r?   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)`.
rW   g     @@r$   g     @r  Nr   )r   rW   r   )r8   r   r   logr  exparanger   r   sincosnn
functionalpad)ri   r-  r.  r   half_dimembs         r=   get_guidance_scale_embedding@StableDiffusionXLControlNetPipeline.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
r?   c                     U R                   $ r   )_guidance_scaler+  s    r=   guidance_scale2StableDiffusionXLControlNetPipeline.guidance_scale  s    ###r?   c                     U R                   $ r   )
_clip_skipr+  s    r=   rs   -StableDiffusionXLControlNetPipeline.clip_skip      r?   c                 r    U R                   S:  =(       a"    U R                  R                  R                  S L $ )NrW   )r>  rR   rc   time_cond_proj_dimr+  s    r=   rn   ?StableDiffusionXLControlNetPipeline.do_classifier_free_guidance  s.    ##a'WDII,<,<,O,OSW,WWr?   c                     U R                   $ r   )_cross_attention_kwargsr+  s    r=   cross_attention_kwargs:StableDiffusionXLControlNetPipeline.cross_attention_kwargs  s    +++r?   c                     U R                   $ r   )_denoising_endr+  s    r=   denoising_end1StableDiffusionXLControlNetPipeline.denoising_end      """r?   c                     U R                   $ r   )_num_timestepsr+  s    r=   num_timesteps1StableDiffusionXLControlNetPipeline.num_timesteps  rP  r?   c                     U R                   $ r   )
_interruptr+  s    r=   	interrupt-StableDiffusionXLControlNetPipeline.interrupt  rD  r?   2   g      @pil)r   r   rI   rP   r  r  r*   r,   r-   rN  r?  r   r   r   r   output_typereturn_dictrJ  r   r  r   r   r   r!  r"  negative_original_sizenegative_crops_coords_top_leftnegative_target_sizecallback_on_step_endr   c'                 $   ^^ U'R                  SS5      n(U'R                  SS5      n)U(b  [        SSS5        U)b  [        SSS5        [        U%[        [        45      (       a  U%R
                  n&[        U R                  5      (       a  U R                  R                  OU R                  n*[        U[        5      (       d%  [        U[        5      (       a  [        U5      U/-  nO[        U[        5      (       d%  [        U[        5      (       a  [        U5      U/-  nOb[        U[        5      (       dM  [        U[        5      (       d8  [        U*[        5      (       a  [        U*R                  5      OSn+U+U/-  U+U/-  nnU R                  UUUU)UUUUUUUUUUUU&5        Xl        U$U l        UU l        Xl        S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*[        5      (       a.  [        U[,        5      (       a  U/[        U*R                  5      -  n[        U*[.        5      (       a  U*R0                  R2                  O"U*R                  S	   R0                  R2                  n.U=(       d    U.nU R4                  b  U R4                  R7                  S
S5      OSn/U R9                  UUU-UU R:                  UUUUUUU/U R<                  S9u  nnnnUc  Ub"  U R?                  UUU-U,U-  U R:                  5      n0[        U*[.        5      (       a@  U RA                  UUUU,U-  UU-U*RB                  U R:                  US9	nUR(                  SS u  pEOx[        U*[        5      (       aa  / n1U HB  n2U RA                  U2UUU,U-  UU-U*RB                  U R:                  US9	n2U1RE                  U25        MD     U1nUS	   R(                  SS u  pEO e[F        (       a  Sn3OU-n3[I        U RJ                  UU3Xx5      u  pv[        U5      U l&        U RN                  R0                  RP                  n4U RS                  U,U-  U4UUURB                  U-UU5      nSn5U RN                  R0                  RT                  b{  [V        RX                  " U RZ                  S-
  5      R]                  U,U-  5      n6U R_                  U6U RN                  R0                  RT                  S9Ra                  U-URB                  S9n5U Rc                  X5      n7/ n8[e        [        U5      5       H  n9[g        UU5       V:V;s/ s H>  u  n:n;S[-        U9[        U5      -  U::  =(       d    U9S-   [        U5      -  U;:  5      -
  PM@     n<n:n;U8RE                  [        U*[.        5      (       a  U<S	   OU<5        M     [        U[        5      (       a  U=(       d    US	   R(                  SS nOU=(       d    UR(                  SS nU =(       d    XE4n Un=U Rh                  c  [k        UR(                  S   5      n>O U Rh                  R0                  Rl                  n>U Ro                  UUU URB                  U>S9n?U!b!  U#b  U Ro                  U!U"U#URB                  U>S9n@OU?n@U R:                  (       aE  [V        Rp                  " UU/S	S9n[V        Rp                  " UU=/S	S9n=[V        Rp                  " W@U?/S	S9n?URa                  U-5      nU=Ra                  U-5      n=U?Ra                  U-5      R]                  U,U-  S5      n?[        U5      X`RJ                  Rr                  -  -
  nAU Rt                  b  [        U Rt                  [,        5      (       a  U Rt                  S	:  a  U Rt                  S:  a  [k        [w        U RJ                  R0                  Rx                  U Rt                  U RJ                  R0                  Rx                  -  -
  5      5      m^[        [        [{        U^4S jU5      5      5      nUSU n[        U RN                  5      nB[        U R                  5      nC[}        SS5      nDU R                  US9 nE[        U5       GH  u  n9nFU R                  (       a  M  [V        R                  R                  5       (       a3  WB(       a,  WC(       a%  WD(       a  [V        R                  R                  5         U R:                  (       a  [V        Rp                  " U/S-  5      OUnGU RJ                  R                  UGWF5      nGU=U?S.nHU(       am  U R:                  (       a\  UnIU RJ                  R                  UIWF5      nIUR                  S5      S   nJU=R                  S5      S   U?R                  S5      S   S.nKOWGnIUnJWHnK[        U8U9   [        5      (       a(  [g        UU8U9   5       VLV:s/ s H  u  nLn:ULU:-  PM     nMnLn:O$UnN[        UN[        5      (       a  WNS	   nNWNU8U9   -  nMU R                  WIWFWJUWMUWKSS9u  nOnPU(       ay  U R:                  (       ah  WO VQs/ s H/  nQ[V        Rp                  " [V        R                  " UQ5      UQ/5      PM1     nOnQ[V        Rp                  " [V        R                  " WP5      UP/5      nPUc  Ub  W0WHS'   U RO                  WGWFUU5U R4                  WOWPWHSS9	S	   nRU R:                  (       a  WRR                  S5      u  nSnTUSU
UTUS-
  -  -   nRU RJ                  R                  " WRWFU40 U7DSS0D6S	   nU%b  0 nUU& H  nV[        5       UV   WUUV'   M     U%" U U9WFWU5      nWUWR                  SU5      nUWR                  S U5      nUWR                  S!U5      nUWR                  S"U=5      n=UWR                  S#U5      nUWR                  S$U?5      n?UWR                  S%W@5      n@UWR                  S&U5      nU9[        U5      S-
  :X  d)  U9S-   WA:  a`  U9S-   U RJ                  Rr                  -  S	:X  a@  WER                  5         U(b-  U9U)-  S	:X  a$  U9[        U RJ                  S'S5      -  nXU(" UXWFU5        [F        (       d  GM  [        R                  " 5         GM     SSS5        US(:X  Gd  U R                  RB                  [V        R                  :H  =(       a     U R                  R0                  R                  nYUY(       a_  U R                  5         URa                  [        [        U R                  R                  R                  5       5      5      RB                  5      n[        U R                  R0                  S)5      =(       a"    U R                  R0                  R                  SLnZ[        U R                  R0                  S*5      =(       a"    U R                  R0                  R                  SLn[WZ(       Ga  W[(       a  [V        RX                  " U R                  R0                  R                  5      R                  SS+SS5      Ra                  UR                  URB                  5      n\[V        RX                  " U R                  R0                  R                  5      R                  SS+SS5      Ra                  UR                  URB                  5      n]UU]-  U R                  R0                  R                  -  U\-   nO#UU R                  R0                  R                  -  nU R                  R                  USS,9S	   nWY(       a'  U R                  Ra                  [V        R                  S-9  OUnUS(:X  dB  U R                  b  U R                  R                  U5      nU R                  R                  UUS.9nU R                  5         U(       d  U4$ [        US/9$ s  sn;n:f s  sn:nLf s  snQf ! , (       d  f       GNV= f)0u0  
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`.
    prompt_2 (`str` or `list[str]`, *optional*):
        The prompt or prompts to be sent to `tokenizer_2` and `text_encoder_2`. If not defined, `prompt` is
        used in both text-encoders.
    image (`torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, `list[torch.Tensor]`, `list[PIL.Image.Image]`, `list[np.ndarray]`,:
            `list[list[torch.Tensor]]`, `list[list[np.ndarray]]` or `list[list[PIL.Image.Image]]`):
        The ControlNet input condition to provide guidance to the `unet` for generation. If the type is
        specified as `torch.Tensor`, it is passed to ControlNet as is. `PIL.Image.Image` can also be accepted
        as an image. The dimensions of the output image defaults to `image`'s dimensions. If height and/or
        width are passed, `image` is resized accordingly. If multiple ControlNets are specified in `init`,
        images must be passed as a list such that each element of the list can be correctly batched for input
        to a single ControlNet.
    height (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
        The height in pixels of the generated image. Anything below 512 pixels won't work well for
        [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)
        and checkpoints that are not specifically fine-tuned on low resolutions.
    width (`int`, *optional*, defaults to `self.unet.config.sample_size * self.vae_scale_factor`):
        The width in pixels of the generated image. Anything below 512 pixels won't work well for
        [stabilityai/stable-diffusion-xl-base-1.0](https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0)
        and checkpoints that are not specifically fine-tuned on low resolutions.
    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.
    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.
    denoising_end (`float`, *optional*):
        When specified, determines the fraction (between 0.0 and 1.0) of the total denoising process to be
        completed before it is intentionally prematurely terminated. As a result, the returned sample will
        still retain a substantial amount of noise as determined by the discrete timesteps selected by the
        scheduler. The denoising_end parameter should ideally be utilized when this pipeline forms a part of a
        "Mixture of Denoisers" multi-pipeline setup, as elaborated in [**Refining the Image
        Output**](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion/stable_diffusion_xl#refining-the-image-output)
    guidance_scale (`float`, *optional*, defaults to 5.0):
        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`).
    negative_prompt_2 (`str` or `list[str]`, *optional*):
        The prompt or prompts to guide what to not include in image generation. This is sent to `tokenizer_2`
        and `text_encoder_2`. If not defined, `negative_prompt` is used in both text-encoders.
    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.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
        generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
        tensor is generated by sampling using the supplied random `generator`.
    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.
    pooled_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated pooled text embeddings. Can be used to easily tweak text inputs (prompt weighting). If
        not provided, pooled text embeddings are generated from `prompt` input argument.
    negative_pooled_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative pooled text embeddings. Can be used to easily tweak text inputs (prompt
        weighting). If not provided, pooled `negative_prompt_embeds` are generated from `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).
    controlnet_conditioning_scale (`float` or `list[float]`, *optional*, defaults to 1.0):
        The outputs of the ControlNet are multiplied by `controlnet_conditioning_scale` before they are added
        to the residual in the original `unet`. If multiple ControlNets are specified in `init`, you can set
        the corresponding scale as a list.
    guess_mode (`bool`, *optional*, defaults to `False`):
        The ControlNet encoder tries to recognize the content of the input image even if you remove all
        prompts. A `guidance_scale` value between 3.0 and 5.0 is recommended.
    control_guidance_start (`float` or `list[float]`, *optional*, defaults to 0.0):
        The percentage of total steps at which the ControlNet starts applying.
    control_guidance_end (`float` or `list[float]`, *optional*, defaults to 1.0):
        The percentage of total steps at which the ControlNet stops applying.
    original_size (`tuple[int]`, *optional*, defaults to (1024, 1024)):
        If `original_size` is not the same as `target_size` the image will appear to be down- or upsampled.
        `original_size` defaults to `(height, width)` if not specified. Part of SDXL's micro-conditioning as
        explained in section 2.2 of
        [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
    crops_coords_top_left (`tuple[int]`, *optional*, defaults to (0, 0)):
        `crops_coords_top_left` can be used to generate an image that appears to be "cropped" from the position
        `crops_coords_top_left` downwards. Favorable, well-centered images are usually achieved by setting
        `crops_coords_top_left` to (0, 0). Part of SDXL's micro-conditioning as explained in section 2.2 of
        [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
    target_size (`tuple[int]`, *optional*, defaults to (1024, 1024)):
        For most cases, `target_size` should be set to the desired height and width of the generated image. If
        not specified it will default to `(height, width)`. Part of SDXL's micro-conditioning as explained in
        section 2.2 of [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952).
    negative_original_size (`tuple[int]`, *optional*, defaults to (1024, 1024)):
        To negatively condition the generation process based on a specific image resolution. Part of SDXL's
        micro-conditioning as explained in section 2.2 of
        [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952). For more
        information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208.
    negative_crops_coords_top_left (`tuple[int]`, *optional*, defaults to (0, 0)):
        To negatively condition the generation process based on a specific crop coordinates. Part of SDXL's
        micro-conditioning as explained in section 2.2 of
        [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952). For more
        information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208.
    negative_target_size (`tuple[int]`, *optional*, defaults to (1024, 1024)):
        To negatively condition the generation process based on a target image resolution. It should be as same
        as the `target_size` for most cases. Part of SDXL's micro-conditioning as explained in section 2.2 of
        [https://huggingface.co/papers/2307.01952](https://huggingface.co/papers/2307.01952). For more
        information, refer to this issue thread: https://github.com/huggingface/diffusers/issues/4208.
    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.

Examples:

Returns:
    [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.stable_diffusion.StableDiffusionPipelineOutput`] is returned,
        otherwise a `tuple` is returned containing the output images.
callbackNr   r*  zjPassing `callback` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`zpPassing `callback_steps` as an input argument to `__call__` is deprecated, consider using `callback_on_step_end`rW   Fr   scale)rJ   rK   rq   rN   rr   rs   )	rP   r  r  r   rm   r+   r   rn   r  r~   cpu)r.  r   r   r{   )r   r#  r   c                    > U T:  $ r   r/   )tsdiscrete_timestep_cutoffs    r=   <lambda>>StableDiffusionXLControlNetPipeline.__call__.<locals>.<lambda>  s    RC[=[r?   z>=z2.1)totalr$   )text_embedstime_ids)encoder_hidden_statescontrolnet_condconditioning_scaler  added_cond_kwargsr\  r   )rm  timestep_condrJ  down_block_additional_residualsmid_block_additional_residualrp  r\  r\  rI   rJ   rK   rL   rN   rM   rO   rP   orderlatentlatents_meanlatents_stdr   )r\  r  )r[  )images)dpopr   r^   r   r   tensor_inputsr!   rS   r   r_   r8   r   r   r   r>  rB  rI  rM  rV  r   r   r   r   r   rc   global_pool_conditionsrJ  getr   rn   rs   r   r  r   r   XLA_AVAILABLEr>   r9   rR  rR   in_channelsr  rF  r   r  r?  r   r;  r   r   ranger   rF   r   projection_dimr&  r   rt  rN  roundnum_train_timestepsfilterr"   progress_barr   rW  cudais_available	_inductorcudagraph_mark_step_beginscale_model_inputr   r   r   localsupdaterb   xm	mark_steprQ   float16force_upcastr)  r   iterpost_quant_convr5   r   rv  rw  r   r+   scaling_factordecoderg   apply_watermarkre   postprocessmaybe_free_model_hooksr'   )_ri   rk   rl   rP   r  r  r*   r,   r-   rN  r?  ro   rp   rm   r   r   rI   rJ   rK   rq   rN   r   r   r[  r\  rJ  r   r  r   r   r   r!  r"  r]  r^  r_  rs   r`  r   r:   rb  r   rS   multr   r+   r{  text_encoder_lora_scaler   rx  r   timestep_devicer  rq  guidance_scale_tensorr   controlnet_keepr   sekeepsrL   r#  rM   rO   num_warmup_stepsis_unet_compiledis_controlnet_compiledis_torch_higher_equal_2_1r  tlatent_model_inputrp  control_model_inputcontrolnet_prompt_embedscontrolnet_added_cond_kwargsc
cond_scalecontrolnet_cond_scaledown_block_res_samplesmid_block_res_sampled
noise_prednoise_pred_uncondnoise_pred_textcallback_kwargsr   callback_outputsstep_idxneeds_upcastinghas_latents_meanhas_latents_stdrv  rw  rg  s_                                                                                                 @r=   __call__,StableDiffusionXLControlNetPipeline.__call__  s9   H ::j$/$4d;|
 %  C *-=?U,VWW1E1S1S.2DT__2U2UT__..[_[j[j
 0$77JG[]a<b<b%()=%>BXAY%Y"0$77JG]_c<d<d#&'=#>BVAW#W 2D99*MacgBhBh+5jBV+W+W3z']^D.//,-- %9" 	" #))" .!	
&  .#'=$+ *VS"9"9JJvt$<$<VJ&,,Q/J''j"677JGdfk<l<l-J,KcR\RaRaNb,b) *o66 44#**AA 	
  9#9
 ?C>Y>Y>eD''++GT:ko 	  !,,'#9!5*G.nn  
	
" )$ '+B+N?? '2200L j/22&&%(==&; &&,0,L,L% ' 
E "KK,MFE
$899F++ !),AA*?!$**040P0P) , 
 f%   E!!HNN23/MFE5 =#O$O);NN/)*
&	 ")n  $yy//;;&&.. 	
 99..:$)LL1D1Dq1H$I$P$PQ[^sQs$t! ==%TYY5E5E5X5X > bgmmb4 
 !::9J s9~&A   68LMMDAq eAI.2Rq1uI6NQR6RSSM   ""z*o/V/V58\ab ' eT"")@U1X^^BC-@M)=U[[-=M!4f_.&*-.B.H.H.L*M'*.*=*=*D*D*S*S'--!%%(C . 
 "-2F2R$($:$:&.$#)),G %; %! %1!++!II'=}&MSTUM#ii)F(X^_`O 99&;\%JPQRL%((0),,V4#v.55jCX6XZ[\ y>,?..BVBV,VV *4--u55""Q&""Q&'*NN))==))DNN,A,A,U,UUW($ #&d62[]f+g&h"i!"6#67I-dii8!3DOO!D$4T5$A!%89\!),1>>
 JJ++--).D1OO==?AEAaAaUYYy1}%=gn"%)^^%E%EFXZ[%\"4CQ]$^! $"B"B*1'*...*J*JK^`a*b'/</B/B1/Ea/H,'6'<'<Q'?'B$0$6$6q$9!$<40
 +='/<,3D0oa0$77478UWfghWi4j!k4jDAq!a%4jJ!kJ,I)!"7>>0Ea0H-!69K!KJ?C'*B$)'1)&B % @O 	@<&(< $"B"B \r-r[qVWeii9I9I!9La8P.Q[q*-r+099e6F6FG[6\^r5s+t(#/3J3V8D%n5 "YY&*7"/+/+F+F4J2F&7 % ' 
 

 339C9I9I!9L6%!2^YjGj5k!kJ ..--j!WmHYmglmnop'3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM-=-A-ABZ\r-s*&6&:&:;Lo&^O4D4H4H79V51 $4#7#7#UL,<,@,@AXZo,p),00%@E I**A9I/IqSTuX\XfXfXlXlNlpqNq '')+N0Ba0G#$(K#K 1g6 =LLNY - :^ h&"hhnn=^$((//B^B^O!!**T$txx/G/G/R/R/T*U%V%\%\]  'txxGtDHHOOLhLhptLt%dhhoo}Eq$((//JeJemqJqOOLL!=!=>CCAq!QORRSZSaSacjcpcpq  LL!<!<=BB1aANQQRYR`R`bibobop  "K/$((//2P2PPS__!DHHOO$B$BBHHOOGO?BE %--0Eh&~~)66u=((44U4TE 	##%8O.e<<AP "l, .ss :9sA    A/E.A@ =5
AA@ ,6;"G0A@ A@ 5A@ @ 
A@)rB  rI  rM  r>  rV  r   rR  rf   re   rY   rg   )TNNN)NNrW   TNNNNNNNNr   )NNNNNNNNr   r   r   N)FF)>__name__
__module____qualname____firstlineno____doc__model_cpu_offload_seq_optional_componentsr   r   r   r   r   r   r   r_   r`   r   r   boolr   r	   r]   r   r   r+   r   r   r   r   r   r   r   r   r   r  r  r&  r)  r  r   r;  propertyr?  rs   rn   rJ  rN  rS  rW  no_gradr   EXAMPLE_DOC_STRINGr   	Generatordictr   r   r   r   r  __static_attributes____classcell__)r7   s   @r=   rA   rA      s   )X U	* .2'+047;,[,[ $,[ 4	,[
 !,[ #,[ #,[ $d?&;;eO>TTWkk,[ -,[ '+,[ ,[ .,[ 5,[ ,[d  $&*%&,0&*(,-16:48=A#' $ljlj *lj t#	lj
  #lj &*lj tlj :lj ||d*lj !&t 3lj $llT1lj (-||d':lj DLlj :lj^52+'\!. #! $&*&)" +/#{|#^ %*>0 ei&) 58emm.1@E	< $ $   X X , , # # # #   ]]_12 #'+/$(! #%#"&* #2648,-DH'+-16:48=A6:=A"' 8<=@ 6947)-17'+9=:@7; $nr9BO^	=d3i^	= S	/D(^	= "	^	=
 d
^	= Tz^	= !^	= 9^	= U^	= t|^	= ^	= tCy4/^	= c?T1^	=  #Tz^	= ^	=  ??T%//%::TA!^	=" $#^	=$ ||d*%^	=& !&t 3'^	=( $llT1)^	=* (-||d':+^	=, -t3-^	=. "&ell!3d!:/^	=0 4Z1^	=2 3^	=4 !%S#X 55^	=6 (-tE{':7^	=8 9^	=: !&U 3;^	=< $d5k1=^	=> S#X?^	=@  %S#XA^	=B 38_C^	=D !&c3h$ 6E^	=F ).c3hG^	=H $CHo4I^	=J :K^	=L 'Sz4'78;KKNddgkkM^	=N -1IO^	= 3 ^	=r?   rA   )NNNN)Mr2   typingr   r   numpyr   	PIL.Imager   r   torch.nn.functionalr6  r7  r   transformersr   r   r   r   r	   diffusers.utils.import_utilsr
   	callbacksr   r   re   r   r   loadersr   r   r   r   modelsr   r   r   r   r   models.lorar   
schedulersr   utilsr   r   r   r   r   r    utils.torch_utilsr!   r"   r#   pipeline_utilsr%   r&   #stable_diffusion_xl.pipeline_outputr'   stable_diffusion_xl.watermarkr(   r)   torch_xla.core.xla_modelcore	xla_modelr  r}  
get_loggerr  r   r  r   r   r+   r_   r   r>   rA   r/   r?   r=   <module>r     s'             J A D  r q 9 3  T S D Q $%%L , ))MM			H	%+ b '+(,"&!%8*t8* %,,%8* Cy4	8*
 K$8*vQ=$Q=r?   