
    bi(                     ~   d dl Z d dlZd dlZd dlmZ d dlmZmZm	Z	m
Z
mZmZ d dlZd dlmZmZmZ ddlmZ ddlmZ ddlmZ ddlmZmZmZmZmZmZ dd	lm Z  d
dl!m"Z" ddl#m$Z$ ddl%m&Z& ddl'm(Z(  e       rd dl)m*c m+Z, dZ-ndZ- ej\                  e/      Z0 e       rd dl1m2Z2  e       rd dl3Z3dZ4 G d de"e      Z5y)    N)AnyCallableDictListOptionalUnion)CLIPImageProcessorT5EncoderModelT5Tokenizer   )StableDiffusionLoraLoaderMixin)UNet2DConditionModel)DDPMScheduler)BACKENDS_MAPPINGis_bs4_availableis_ftfy_availableis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )IFPipelineOutput)IFSafetyChecker)IFWatermarkerTF)BeautifulSoupa  
    Examples:
        ```py
        >>> from diffusers import IFPipeline, IFSuperResolutionPipeline, DiffusionPipeline
        >>> from diffusers.utils import pt_to_pil
        >>> import torch

        >>> pipe = IFPipeline.from_pretrained("DeepFloyd/IF-I-XL-v1.0", variant="fp16", torch_dtype=torch.float16)
        >>> pipe.enable_model_cpu_offload()

        >>> prompt = 'a photo of a kangaroo wearing an orange hoodie and blue sunglasses standing in front of the eiffel tower holding a sign that says "very deep learning"'
        >>> prompt_embeds, negative_embeds = pipe.encode_prompt(prompt)

        >>> image = pipe(prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_embeds, output_type="pt").images

        >>> # save intermediate image
        >>> pil_image = pt_to_pil(image)
        >>> pil_image[0].save("./if_stage_I.png")

        >>> super_res_1_pipe = IFSuperResolutionPipeline.from_pretrained(
        ...     "DeepFloyd/IF-II-L-v1.0", text_encoder=None, variant="fp16", torch_dtype=torch.float16
        ... )
        >>> super_res_1_pipe.enable_model_cpu_offload()

        >>> image = super_res_1_pipe(
        ...     image=image, prompt_embeds=prompt_embeds, negative_prompt_embeds=negative_embeds, output_type="pt"
        ... ).images

        >>> # save intermediate image
        >>> pil_image = pt_to_pil(image)
        >>> pil_image[0].save("./if_stage_I.png")

        >>> safety_modules = {
        ...     "feature_extractor": pipe.feature_extractor,
        ...     "safety_checker": pipe.safety_checker,
        ...     "watermarker": pipe.watermarker,
        ... }
        >>> super_res_2_pipe = DiffusionPipeline.from_pretrained(
        ...     "stabilityai/stable-diffusion-x4-upscaler", **safety_modules, torch_dtype=torch.float16
        ... )
        >>> super_res_2_pipe.enable_model_cpu_offload()

        >>> image = super_res_2_pipe(
        ...     prompt=prompt,
        ...     image=image,
        ... ).images
        >>> image[0].save("./if_stage_II.png")
        ```
c            *           e Zd ZU eed<   eed<   eed<   eed<   ee	   ed<   ee
   ed<   ee   ed<    ej                  d      Zg d	Zd
ZdgZ	 d*dededededee
   dee	   dee   def fdZ ej*                         	 	 	 	 	 	 	 d+deeee   f   dededeej4                     deeeee   f      deej6                     deej6                     defd       Zd Zd Z	 	 	 d,dZd Z d-dZ!d Z" ej*                          e#e$      	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d.deeee   f   dedee   de%deeeee   f      dee   d ee   d!ee   d"e%d#eeejL                  eejL                     f      deej6                     deej6                     d$ee   d%ed&ee'eeej6                  gdf      d'eded(ee(ee)f      f$d)              Z* xZ+S )/
IFPipeline	tokenizertext_encoderunet	schedulerfeature_extractorsafety_checkerwatermarkeru5   [#®•©™&@·º½¾¿¡§~\)\(\]\[\}\{\|\\/\*]{1,})r    r!   r%   r$   r&   ztext_encoder->unetrequires_safety_checkerc	           	          t         	|           |%|r#t        j                  d| j                   d       ||t        d      | j                  |||||||       | j                  |       y )Nz)You have disabled the safety checker for a   by passing `safety_checker=None`. Ensure that you abide to the conditions of the IF 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.)r    r!   r"   r#   r%   r$   r&   )r'   )super__init__loggerwarning	__class__
ValueErrorregister_modulesregister_to_config)
selfr    r!   r"   r#   r%   r$   r&   r'   r-   s
            g/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/deepfloyd_if/pipeline_if.pyr*   zIFPipeline.__init__   s     	!&=NN;DNN;K Lj j %*;*Cx 
 	%)/# 	 	
 	8OP    Npromptdo_classifier_free_guidancenum_images_per_promptdevicenegative_promptprompt_embedsnegative_prompt_embedsclean_captionc	                    |<|:t        |      t        |      ur$t        dt        |       dt        |       d      || j                  }|t        |t              rd}	n-|t        |t
              rt        |      }	n|j                  d   }	d}
|| j                  ||      }| j                  |d	|
d
d
d      }|j                  }| j                  |dd      j                  }|j                  d   |j                  d   k\  rXt        j                  ||      sB| j                  j                  |dd|
dz
  df         }t        j                  d|
 d|        |j                   j#                  |      }| j%                  |j#                  |      |      }|d   }| j$                  | j$                  j&                  }n%| j(                  | j(                  j&                  }nd}|j#                  ||      }|j                  \  }}}|j+                  d|d      }|j-                  ||z  |d      }|r||dg|	z  }nEt        |t              r|g}n1|	t        |      k7  r!t/        d| dt        |       d| d|	 d	      |}| j                  ||      }|j                  d   }
| j                  |d	|
d
d
d
d      }|j                   j#                  |      }| j%                  |j                  j#                  |      |      }|d   }|rO|j                  d   }|j#                  ||      }|j+                  d|d      }|j-                  |	|z  |d      }||fS d}||fS )a  
        Encodes the prompt into text encoder hidden states.

        Args:
            prompt (`str` or `List[str]`, *optional*):
                prompt to be encoded
            do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
                whether to use classifier free guidance or not
            num_images_per_prompt (`int`, *optional*, defaults to 1):
                number of images that should be generated per prompt
            device: (`torch.device`, *optional*):
                torch device to place the resulting embeddings on
            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. 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.
            clean_caption (bool, defaults to `False`):
                If `True`, the function will preprocess and clean the provided caption before encoding.
        Nz?`negative_prompt` should be the same type to `prompt`, but got z != .r   r   M   )r;   
max_lengthTpt)paddingr?   
truncationadd_special_tokensreturn_tensorslongest)rA   rD   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: )attention_mask)dtyper7    z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.)rA   r?   rB   return_attention_maskrC   rD   )type	TypeError_execution_device
isinstancestrlistlenshape_text_preprocessingr    	input_idstorchequalbatch_decoder+   r,   rG   tor!   rH   r"   repeatviewr.   )r1   r4   r5   r6   r7   r8   r9   r:   r;   
batch_sizer?   text_inputstext_input_idsuntruncated_idsremoved_textrG   rH   bs_embedseq_len_uncond_tokensuncond_inputs                         r2   encode_promptzIFPipeline.encode_prompt   s   L /"=F|4#88UVZ[jVkUl mV~Q( 
 >++F*VS"9JJvt$<VJ&,,Q/J 
 --fM-RF..$%#'# ) K )22N"nnVYW[n\ffO$$R(N,@,@,DDU[[N  $~~::?1j[\n_aNaKa;bc"|9\N<
 )77::6BN --!!&)- . M *!,M(%%++EYY"IIOOEE%((uV(D,22'1%,,Q0EqI%**86K+KWVXY '+A+I&!#z 1OS1!0 1s?33 )/)::J3K_J` ax/
| <33  !0 44]R_4`M&,,Q/J>>$%&*#'# * L *88;;FCN%)%6%6&&))&1- &7 &" &<A%>"&,2215G%;%>%>USY%>%Z"%;%B%B1F[]^%_"%;%@%@NcAcelnp%q" 444 &*"444r3   c                     | j                   c| j                  | j                  |      d      j                  |      }| j                  ||j                  j                  |            \  }}}nd }d }|||fS )Nr@   )rD   )rH   )images
clip_input)r%   r$   numpy_to_pilrX   pixel_values)r1   imager7   rH   safety_checker_inputnsfw_detectedwatermark_detecteds          r2   run_safety_checkerzIFPipeline.run_safety_checkerB  s    *#'#9#9$:K:KE:Rcg#9#h#k#klr#s 7;7J7J/<<??e?L 8K 84E="4
 !M!%m%777r3   c                 V   dt        t        j                  | j                  j                        j
                  j                               v }i }|r||d<   dt        t        j                  | j                  j                        j
                  j                               v }|r||d<   |S )Neta	generator)setinspect	signaturer#   step
parameterskeys)r1   rr   rq   accepts_etaextra_step_kwargsaccepts_generators         r2   prepare_extra_step_kwargsz$IFPipeline.prepare_extra_step_kwargsP  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r3   c                    ||0t        |t              r|dk  rt        d| dt        |       d      ||t        d| d| d      ||t        d      |7t        |t              s't        |t
              st        d	t        |             ||t        d
| d| d      |C|@|j                  |j                  k7  r&t        d|j                   d|j                   d      y y y )Nr   z5`callback_steps` has to be a positive integer but is z	 of type r=   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` )rN   intr.   rK   rO   rP   rR   )r1   r4   callback_stepsr8   r9   r:   s         r2   check_inputszIFPipeline.check_inputsa  st    "&
>30OSaefSfGGW X(), 
 -";08N}o ^0 0  ^ 5w  FC)@TZ\`IaQRVW]R^Q_`aa&+A+M9/9J K*++]_ 
 $)?)K""&<&B&BB --:-@-@,A B.445Q8  C *L$r3   c                     ||||f}t        |t              r)t        |      |k7  rt        dt        |       d| d      t	        ||||      }	|	| j
                  j                  z  }	|	S )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.)rr   r7   rH   )rN   rP   rQ   r.   r   r#   init_noise_sigma)
r1   r[   num_channelsheightwidthrH   r7   rr   rR   intermediate_imagess
             r2   prepare_intermediate_imagesz&IFPipeline.prepare_intermediate_images  s    \659i&3y>Z+GA#i.AQ R&<'gi 
 +5If\ab 2DNN4S4SS""r3   c                     rOt               sEt        j                  t        d   d   j	                  d             t        j                  d       drOt               sEt        j                  t        d   d   j	                  d             t        j                  d       dt        |t        t        f      s|g}dt        f fd}|D cg c]
  } ||       c}S c c}w )	Nbs4rF   zSetting `clean_caption=True`z#Setting `clean_caption` to False...Fftfytextc                     r$j                  |       } j                  |       } | S | j                         j                         } | S )N)_clean_captionlowerstrip)r   r;   r1   s    r2   processz/IFPipeline._text_preprocessing.<locals>.process  sH    **40**40 K zz|))+Kr3   )
r   r+   r,   r   formatr   rN   tuplerP   rO   )r1   r   r;   r   ts   ` `  r2   rS   zIFPipeline._text_preprocessing  s    !1!3NN+E226==>\]^NN@A!M!2!4NN+F3B7>>?]^_NN@A!M$.6D	# 	 %))q
)))s   C!c                 	   t        |      }t        j                  |      }|j                         j	                         }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        |d      j                  }t        j                  dd|      }t        j                  d	d|      }t        j                  d
d|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  dd|      }t        j                  d d|      }t        j                  d!d|      }t        j                  | j                  d|      }t        j                  d"d|      }t        j                  d#      }t        t        j                  ||            d$kD  rt        j                  |d|      }t        j                  |      }t        j                   t        j                   |            }t        j                  d%d|      }t        j                  d&d|      }t        j                  d'd|      }t        j                  d(d|      }t        j                  d)d|      }t        j                  d*d|      }t        j                  d+d|      }t        j                  d,d|      }t        j                  d-d|      }t        j                  d.d|      }t        j                  d/d0|      }t        j                  d1d2|      }t        j                  d3d|      }|j                          t        j                  d4d5|      }t        j                  d6d|      }t        j                  d7d|      }t        j                  d8d|      }|j                         S )9Nz<person>personzk\b((?:https?:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))rI   zh\b((?:www:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))zhtml.parser)featuresz
@[\w\d]+\bz[\u31c0-\u31ef]+z[\u31f0-\u31ff]+z[\u3200-\u32ff]+z[\u3300-\u33ff]+z[\u3400-\u4dbf]+z[\u4dc0-\u4dff]+z[\u4e00-\u9fff]+z|[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D]+-u   [`´«»“”¨]"u   [‘’]'z&quot;?z&ampz"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} z\d:\d\d\s+$z\\nz
#\d{1,3}\bz	#\d{5,}\bz
\b\d{6,}\bz0[\S]+\.(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)z
[\"\']{2,}z[\.]{2,}z\s+\.\s+z	(?:\-|\_)r   z\b[a-zA-Z]{1,3}\d{3,15}\bz\b[a-zA-Z]+\d+[a-zA-Z]+\bz\b\d+[a-zA-Z]+\d+\bz!(worldwide\s+)?(free\s+)?shippingz(free\s)?download(\sfree)?z\bclick\b\s(?:for|on)\s\w+z9\b(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)(\simage[s]?)?z\bpage\s+\d+\bz*\b\d*[a-zA-Z]+\d+[a-zA-Z]+\d+[a-zA-Z\d]*\bu   \b\d+\.?\d*[xх×]\d+\.?\d*\bz
\b\s+\:\s+z: z(\D[,\./])\bz\1 z\s+z^[\"\']([\w\W]+)[\"\']$z\1z^[\'\_,\-\:;]z[\'\_,\-\:\-\+]$z^\.\S+$)rO   ulunquote_plusr   r   resubr   r   bad_punct_regexcompilerQ   findallr   fix_texthtmlunescape)r1   captionregex2s      r2   r   zIFPipeline._clean_caption  s   g,//'*--/'')&&Xw7&&z

 &&w
  -@EE &&G4 &&,b':&&,b':&&,b':&&,b':&&,b':&&,b':&&,b': && L
 &&-sG<&&c73 &&R1&&"g. &&>WM &&W5 &&g. &&G4&&r73&&G4&&LbRYZ &&g6&&dG4&&--tW=&&dG4 L)rzz&'*+a/ffVS'2G--(--g 67&&5r7C&&5r7C&&/W=&&=r7K&&6GD&&6GD&&UWY[bc&&*B8&&FgV&&92wG&&w7&&&':&&g.&&3UGD&&)38&&,c7;&&R1}}r3   num_inference_steps	timestepsguidance_scaler   r   rq   rr   output_typereturn_dictcallbackr   cross_attention_kwargsc           
         | j                  |||||       |xs  | j                  j                  j                  }|xs  | j                  j                  j                  }|t	        |t
              rd}n-|t	        |t              rt        |      }n|j                  d   }| j                  }|dkD  }| j                  ||||||||      \  }}|rt        j                  ||g      }|?| j                  j                  ||       | j                  j                  }t        |      }n3| j                  j                  ||       | j                  j                  }t!        | j                  d      r| j                  j#                  d       | j%                  ||z  | j                  j                  j&                  |||j(                  ||
      }| j+                  |
|	      }t!        | d	      r&| j,                  | j,                  j/                          t        |      || j                  j0                  z  z
  }| j3                  |
      5 }t5        |      D ]  \  }}|rt        j                  |gdz        n|}| j                  j7                  ||      }| j                  ||||d      d   }|r~|j9                  d      \  }}|j;                  |j                  d   d      \  }} |j;                  |j                  d   d      \  }}!||||z
  z  z   }t        j                  ||!gd      }| j                  j                  j<                  dvr#|j;                  |j                  d   d      \  }}  | j                  j>                  |||fi |ddid   }|t        |      dz
  k(  s'|dz   |kD  rC|dz   | j                  j0                  z  dk(  r$|jA                          |||z  dk(  r
 ||||       tB        stE        jF                           	 ddd       |}"|dk(  r|"dz  dz   jI                  dd      }"|"jK                         jM                  dddd      jO                         jQ                         }"| jS                  |"||j(                        \  }"}#}$| jU                  |"      }"| jV                  | jV                  jY                  |"| j                  j                  j                        }"n|dk(  r7d}#d}$t!        | d      r| jZ                  | jZ                  j/                          nw|"dz  dz   jI                  dd      }"|"jK                         jM                  dddd      jO                         jQ                         }"| jS                  |"||j(                        \  }"}#}$| j]                          |s|"|#|$fS t_        |"|#|$      S # 1 sw Y   xY w)u  
        Function invoked when calling the pipeline for generation.

        Args:
            prompt (`str` or `List[str]`, *optional*):
                The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
                instead.
            num_inference_steps (`int`, *optional*, defaults to 100):
                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. If not defined, equal spaced `num_inference_steps`
                timesteps are used. Must be in descending order.
            guidance_scale (`float`, *optional*, defaults to 7.0):
                Guidance scale as defined in [Classifier-Free Diffusion
                Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
                of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
                `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
                the text `prompt`, usually at the expense of lower image quality.
            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`).
            num_images_per_prompt (`int`, *optional*, defaults to 1):
                The number of images to generate per prompt.
            height (`int`, *optional*, defaults to self.unet.config.sample_size):
                The height in pixels of the generated image.
            width (`int`, *optional*, defaults to self.unet.config.sample_size):
                The width in pixels of the generated image.
            eta (`float`, *optional*, defaults to 0.0):
                Corresponds to parameter eta (η) in the DDIM paper: https://huggingface.co/papers/2010.02502. Only
                applies to [`schedulers.DDIMScheduler`], will be ignored for others.
            generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
                One or a list of [torch generator(s)](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, *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.
            output_type (`str`, *optional*, defaults to `"pil"`):
                The output format of the generate image. Choose between
                [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`~pipelines.stable_diffusion.IFPipelineOutput`] instead of a plain tuple.
            callback (`Callable`, *optional*):
                A function that will be called every `callback_steps` steps during inference. The function will be
                called with the following arguments: `callback(step: int, timestep: int, latents: torch.Tensor)`.
            callback_steps (`int`, *optional*, defaults to 1):
                The frequency at which the `callback` function will be called. If not specified, the callback will be
                called at every step.
            clean_caption (`bool`, *optional*, defaults to `True`):
                Whether or not to clean the caption before creating embeddings. Requires `beautifulsoup4` and `ftfy` to
                be installed. If the dependencies are not installed, the embeddings will be created from the raw
                prompt.
            cross_attention_kwargs (`dict`, *optional*):
                A kwargs dictionary that if specified is passed along to the `AttentionProcessor` as defined under
                `self.processor` in
                [diffusers.models.attention_processor](https://github.com/huggingface/diffusers/blob/main/src/diffusers/models/attention_processor.py).

        Examples:

        Returns:
            [`~pipelines.stable_diffusion.IFPipelineOutput`] or `tuple`:
            [`~pipelines.stable_diffusion.IFPipelineOutput`] if `return_dict` is True, otherwise a `tuple. When
            returning a tuple, the first element is a list with the generated images, and the second element is a list
            of `bool`s denoting whether the corresponding generated image likely represents "not-safe-for-work" (nsfw)
            or watermarked content, according to the `safety_checker`.
        Nr   r   g      ?)r6   r7   r8   r9   r:   r;   )r   r7   )r7   set_begin_indextext_encoder_offload_hook)totalr   F)encoder_hidden_statesr   r   )dim)learnedlearned_ranger   pilg      ?r   r@   unet_offload_hook)rg   rm   rn   )0r   r"   configsample_sizerN   rO   rP   rQ   rR   rM   re   rU   catr#   set_timestepsr   hasattrr   r   in_channelsrH   r|   r   offloadorderprogress_bar	enumeratescale_model_inputchunksplitvariance_typerv   updateXLA_AVAILABLExm	mark_stepclampcpupermutefloatnumpyro   ri   r&   apply_watermarkr   maybe_free_model_hooksr   )%r1   r4   r   r   r   r8   r6   r   r   rq   rr   r9   r:   r   r   r   r   r;   r   r[   r7   r5   r   rz   num_warmup_stepsr   ir   model_input
noise_prednoise_pred_uncondnoise_pred_textrb   predicted_variancerk   rm   rn   s%                                        r2   __call__zIFPipeline.__call__#  s   ~ 	&./=Rhi 7499++775))55*VS"9JJvt$<VJ&,,Q/J''
 '5s&:# 150B0B'"7+'#9' 1C 	1
-- '!II'=}&MNM  NN((9V(L00I"%i.NN(()<V(L00I4>>#45NN**1- #>>..II((
 !::9cJ 445$:X:X:d**224 y>,?$..BVBV,VV%89 '	#\!), &#1<WEII23a78]p  #nn>>{AN "YY*7+A % '  
 /9C9I9I!9L6%+<+B+B;CTCTUVCW]^+B+_(%q:I:O:OP[PaPabcPdjk:O:l7O%7!2^YjGj5k!kJ!&J8J+KQR!SJ>>((66>ZZ$.$4$4[5F5Fq5Iq$4$QMJ ':dnn&9&9#6':K'Y^''#
 I**A9I/IqSTuX\XfXfXlXlNlpqNq '')+N0Ba0G A':; LLNM&#'	#R $%QY_++Aq1EIIK''1a399;AACE 8<7N7NuV\^k^q^q7r4E="4 %%e,E +((88		@P@P@\@\]D  M!%t01d6L6L6X&&..0 QY_++Aq1EIIK''1a399;AACE 8<7N7NuV\^k^q^q7r4E="4 	##%=*<==uM^pqq['	# '	#s   F2W
<W

W)T)Tr   NNNNF)NNN)F)Nd   Ng      @Nr   NNg        NNNr   TNr   TN),__name__
__module____qualname__r   __annotations__r
   r   r   r   r	   r   r   r   r   r   _optional_componentsmodel_cpu_offload_seq_exclude_from_cpu_offloadboolr*   rU   no_gradr   rO   r   r~   r7   Tensorre   ro   r|   r   r   rS   r   r   EXAMPLE_DOC_STRINGr   	Generatorr   r   r   r   __classcell__)r-   s   @r2   r   r   `   s     
 233_---(( bjj	O  o0!. )-&Q&Q %&Q #	&Q
 !&Q !1&Q $$67&Q m,&Q "&&QP U]]_ -1%&)-;?049=#W5c49n%W5 &*W5  #	W5
 &W5 "%T#Y"78W5  -W5 !) 6W5 W5 W5r8!* #(T#*0pd U]]_12 )-#&# #;?/0 $#MQ049=%* GK";?'mrc49n%mr !mr 9	mr
 mr "%T#Y"78mr  (}mr mr }mr mr E%//43H"HIJmr  -mr !) 6mr c]mr mr  8S#u||$<d$BCD!mr" #mr$ %mr& !)c3h 8'mr 3 mrr3   r   )6r   rt   r   urllib.parseparser   typingr   r   r   r   r   r   rU   transformersr	   r
   r   loadersr   modelsr   
schedulersr   utilsr   r   r   r   r   r   utils.torch_utilsr   pipeline_utilsr   pipeline_outputr   r%   r   	watermarkr   torch_xla.core.xla_modelcore	xla_modelr   r   
get_loggerr   r+   r   r   r   r   r    r3   r2   <module>r      s      	  = =  H H 5 * '  . . - + $ ))MM			H	% !0 fr
r"$B r
rr3   