
    +h                        S SK r S SKrS SKrS SKJr  S SKJrJrJ	r	J
r
JrJr  S SKrS SKrS SKrS SKJrJrJr  SSKJr  SSKJr  SSKJr  SSKJrJr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)  SSK*J+r+  SSK,J-r-  \!" 5       (       a  S SK.J/s  J0r1  Sr2OSr2\"Rf                  " \45      r5\" 5       (       a  S SK6J7r7  \ " 5       (       a  S SK8r8S\Rr                  Rr                  S\:S\Rr                  Rr                  4S jr;Sr< " S S\'\5      r=g)    N)AnyCallableDictListOptionalUnion)CLIPImageProcessorT5EncoderModelT5Tokenizer   )StableDiffusionLoraLoaderMixin)UNet2DConditionModel)DDPMScheduler)BACKENDS_MAPPINGPIL_INTERPOLATIONis_bs4_availableis_ftfy_availableis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )IFPipelineOutput)IFSafetyChecker)IFWatermarkerTF)BeautifulSoupimagesimg_sizereturnc                     U R                   u  p#X#-  nXp2US:  a  [        [        US-  U-  5      S-  5      nO[        [        US-  U-  5      S-  5      nU R                  X#4[        S   S S9n U $ )Nr      bicubic)resamplereducing_gap)sizeintroundresizer   )r   r    whcoefs        q/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/deepfloyd_if/pipeline_if_inpainting.pyr*   r*   1   s|    ;;DA5DqqyhlT)*Q./hlT)*Q./]]A6,=i,HW[]\FM    aL  
    Examples:
        ```py
        >>> from diffusers import IFInpaintingPipeline, IFInpaintingSuperResolutionPipeline, DiffusionPipeline
        >>> from diffusers.utils import pt_to_pil
        >>> import torch
        >>> from PIL import Image
        >>> import requests
        >>> from io import BytesIO

        >>> url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/if/person.png"
        >>> response = requests.get(url)
        >>> original_image = Image.open(BytesIO(response.content)).convert("RGB")
        >>> original_image = original_image

        >>> url = "https://huggingface.co/datasets/diffusers/docs-images/resolve/main/if/glasses_mask.png"
        >>> response = requests.get(url)
        >>> mask_image = Image.open(BytesIO(response.content))
        >>> mask_image = mask_image

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

        >>> prompt = "blue sunglasses"
        >>> prompt_embeds, negative_embeds = pipe.encode_prompt(prompt)

        >>> image = pipe(
        ...     image=original_image,
        ...     mask_image=mask_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")

        >>> super_res_1_pipe = IFInpaintingSuperResolutionPipeline.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,
        ...     mask_image=mask_image,
        ...     original_image=original_image,
        ...     prompt_embeds=prompt_embeds,
        ...     negative_prompt_embeds=negative_embeds,
        ... ).images
        >>> image[0].save("./if_stage_II.png")
        ```
c            ,         ^  \ rS rSr% \\S'   \\S'   \\S'   \\S'   \	\
   \S'   \	\   \S'   \	\   \S'   \R                  " S	5      r/ S
QrSrS/r S1S\S\S\S\S\	\   S\	\
   S\	\   S\4U 4S jjjr\R,                  " 5              S2S\\\\   4   S\S\S\	\R6                     S\	\\\\   4      S\	\R8                     S\	\R8                     S\4S jj5       rS rS r   S3S jr S4S jr!S r"S\#RH                  RH                  S\R8                  4S jr%S\R8                  4S  jr&S! r' S5S" jr(\R,                  " 5       \)" \*5                         S6S\\\\   4   S\\#RH                  RH                  \R8                  \+RX                  \\#RH                  RH                     \\R8                     \\+RX                     4   S#\\#RH                  RH                  \R8                  \+RX                  \\#RH                  RH                     \\R8                     \\+RX                     4   S$\-S%\S&\\   S'\-S\	\\\\   4      S\	\   S(\-S)\	\\R\                  \\R\                     4      S\	\R8                     S\	\R8                     S*\	\   S+\S,\	\/\\\R8                  /S4      S-\S\S.\	\0\\14      4&S/ jj5       5       r2S0r3U =r4$ )7IFInpaintingPipeline{   	tokenizertext_encoderunet	schedulerfeature_extractorsafety_checkerwatermarkeru5   [#®•©™&@·º½¾¿¡§~\)\(\]\[\}\{\|\\/\*]{1,})r3   r4   r8   r7   r9   ztext_encoder->unetrequires_safety_checkerc	           
         > [         T	U ]  5         Uc*  U(       a#  [        R                  SU R                   S35        Ub  Uc  [        S5      eU R                  UUUUUUUS9  U R                  US9  g )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.)r3   r4   r5   r6   r8   r7   r9   )r:   )super__init__loggerwarning	__class__
ValueErrorregister_modulesregister_to_config)
selfr3   r4   r5   r6   r8   r7   r9   r:   r@   s
            r.   r=   IFInpaintingPipeline.__init__   s     	!&=NN;DNN;K Lj j %*;*Cx 
 	%)/# 	 	
 	8OPr/   Npromptdo_classifier_free_guidancenum_images_per_promptdevicenegative_promptprompt_embedsnegative_prompt_embedsclean_captionc	                    Ub>  Ub;  [        U5      [        U5      La$  [        S[        U5       S[        U5       S35      eUc  U R                  nUb  [        U[        5      (       a  Sn	O3Ub!  [        U[
        5      (       a  [        U5      n	OUR                  S   n	Sn
UGc  U R                  XS9nU R                  US	U
S
S
SS9nUR                  nU R                  USSS9R                  nUR                  S   UR                  S   :  a]  [        R                  " X5      (       dB  U R                  R                  USS2U
S-
  S24   5      n[        R                  SU
 SU 35        UR                   R#                  U5      nU R%                  UR#                  U5      US9nUS   nU R$                  b  U R$                  R&                  nO&U R(                  b  U R(                  R&                  nOSnUR#                  UUS9nUR                  u  nnnUR+                  SUS5      nUR-                  UU-  US5      nU(       a  Uc  Uc  S/U	-  nOK[        U[        5      (       a  U/nO2U	[        U5      :w  a!  [/        SU S[        U5       SU SU	 S3	5      eUnU R                  UUS9nUR                  S   n
U R                  US	U
S
S
S
SS9nUR                   R#                  U5      nU R%                  UR                  R#                  U5      US9nUS   nU(       aJ  UR                  S   nUR#                  UUS9nUR+                  SUS5      nUR-                  X-  US5      nXg4$ SnXg4$ )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   )rM   
max_lengthTpt)paddingrQ   
truncationadd_special_tokensreturn_tensorslongest)rS   rV   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: )attention_mask)dtyperI    z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.)rS   rQ   rT   return_attention_maskrU   rV   )type	TypeError_execution_device
isinstancestrlistlenshape_text_preprocessingr3   	input_idstorchequalbatch_decoder>   r?   rY   tor4   rZ   r5   repeatviewrA   )rD   rF   rG   rH   rI   rJ   rK   rL   rM   
batch_sizerQ   text_inputstext_input_idsuntruncated_idsremoved_textrY   rZ   bs_embedseq_len_uncond_tokensuncond_inputs                         r.   encode_prompt"IFInpaintingPipeline.encode_prompt   s   N /"=F|4#88UVZ[jVkUl mV~Q( 
 >++F*VS"9"9JJvt$<$<VJ&,,Q/J 
 --f-RF..$%#'# ) K )22N"nnVYW[n\ffO$$R(N,@,@,DDU[[N 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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[]^%_"%;%@%@Acelnp%q" 44 &*"44r/   c                     U R                   bY  U R                  U R                  U5      SS9R                  U5      nU R                  UUR                  R                  US9S9u  pnOS nS nXU4$ )NrR   )rV   )rZ   )r   
clip_input)r8   r7   numpy_to_pilrj   pixel_values)rD   imagerI   rZ   safety_checker_inputnsfw_detectedwatermark_detecteds          r.   run_safety_checker'IFInpaintingPipeline.run_safety_checker_  s    *#'#9#9$:K:KE:Rcg#9#h#k#klr#s 7;7J7J/<<??e?L 8K 84E"4
 !M!%%777r/   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)setinspect	signaturer6   step
parameterskeys)rD   r   r   accepts_etaextra_step_kwargsaccepts_generators         r.   prepare_extra_step_kwargs.IFInpaintingPipeline.prepare_extra_step_kwargsm  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r/   c	                    Ub  Ub6  [        U[        5      (       a  US::  a  [        SU S[        U5       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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[
        5      (       a  US   n	OUn	[        U	[        R                  5      (       d_  [        U	[        R                  R                  5      (       d6  [        U	[        R                  5      (       d  [        S[        U	5       35      e[        U[
        5      (       a  [        U5      n
O[        U[        R                  5      (       a  UR                  S   n
O][        U[        R                  R                  5      (       a  Sn
O1[        U[        R                  5      (       a  UR                  S   n
O eXJ:w  a  [        SU
 SU 35      e[        U[
        5      (       a  US   n	OUn	[        U	[        R                  5      (       d_  [        U	[        R                  R                  5      (       d6  [        U	[        R                  5      (       d  [        S[        U	5       35      e[        U[
        5      (       a  [        U5      n
O[        U[        R                  5      (       a  UR                  S   n
O][        U[        R                  R                  5      (       a  Sn
O1[        U[        R                  5      (       a  UR                  S   n
O eU
S:w  a  XJ:w  a  [        SU
 SU 35      eg g )Nr   z5`callback_steps` has to be a positive integer but is z	 of type rO   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` z_`image` has to be of type `torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, or List[...] but is r   zimage batch size: z# must be same as prompt batch size zd`mask_image` has to be of type `torch.Tensor`, `PIL.Image.Image`, `np.ndarray`, or List[...] but is zmask_image batch size: z. must be `1` or the same as prompt batch size )r`   r(   rA   r]   ra   rb   rd   rg   TensorPILImagenpndarrayrc   )rD   rF   r}   
mask_imagerm   callback_stepsrJ   rK   rL   check_image_typeimage_batch_sizes              r.   check_inputs!IFInpaintingPipeline.check_inputs~  s    "&
>30O0OSaefSfGGW X(), 
 -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa&+A+M9/9J K*++]_ 
 $)?)K""&<&B&BB --:-@-@,A B.445Q8  eT""$Qx$ +U\\::/AA/<<)*+- 
 eT"""5zu||,,${{1~syy// rzz**${{1~5)12B1CCfgqfrstt j$'')!}) +U\\::/AA/<<)*+- 
 j$''":
ELL11)//2
CIIOO44 
BJJ//)//25q Z%C)*:);;ijtiuv  &D r/   c                   ^ ^ T(       aT  [        5       (       dE  [        R                  [        S   S   R	                  S5      5        [        R                  S5        SmT(       aT  [        5       (       dE  [        R                  [        S   S   R	                  S5      5        [        R                  S5        Sm[        U[        [        45      (       d  U/nS[        4UU 4S jjnU Vs/ s H
  oC" U5      PM     sn$ s  snf )	Nbs4rX   zSetting `clean_caption=True`z#Setting `clean_caption` to False...Fftfytextc                    > T(       a$  TR                  U 5      n TR                  U 5      n U $ U R                  5       R                  5       n U $ N)_clean_captionlowerstrip)r   rM   rD   s    r.   process9IFInpaintingPipeline._text_preprocessing.<locals>.process  sH    **40**40 K zz|))+Kr/   )
r   r>   r?   r   formatr   r`   tuplerb   ra   )rD   r   rM   r   ts   ` `  r.   re   (IFInpaintingPipeline._text_preprocessing  s    !1!3!3NN+E226==>\]^NN@A!M!2!4!4NN+F3B7>>?]^_NN@A!M$..6D	# 	 	 %))Dq
D)))s   *C>c                 ^
   [        U5      n[        R                  " U5      nUR                  5       R	                  5       n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[        USS9R                  n[
        R                  " SSU5      n[
        R                  " S	SU5      n[
        R                  " S
SU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " S SU5      n[
        R                  " S!SU5      n[
        R                  " U R                  SU5      n[
        R                  " S"SU5      n[
        R                  " S#5      n[        [
        R                  " X!5      5      S$:  a  [
        R                  " USU5      n[        R                  " U5      n[        R                   " [        R                   " U5      5      n[
        R                  " S%SU5      n[
        R                  " S&SU5      n[
        R                  " S'SU5      n[
        R                  " S(SU5      n[
        R                  " S)SU5      n[
        R                  " S*SU5      n[
        R                  " S+SU5      n[
        R                  " S,SU5      n[
        R                  " S-SU5      n[
        R                  " S.SU5      n[
        R                  " S/S0U5      n[
        R                  " S1S2U5      n[
        R                  " S3SU5      nUR                  5         [
        R                  " S4S5U5      n[
        R                  " S6SU5      n[
        R                  " S7SU5      n[
        R                  " S8SU5      nUR                  5       $ )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\/?(?!@)))r[   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+$)ra   ulunquote_plusr   r   resubr   r   bad_punct_regexcompilerc   findallr   fix_texthtmlunescape)rD   captionregex2s      r.   r   #IFInpaintingPipeline._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}}r/   r}   r!   c                    [        U[        5      (       d  U/nS n[        US   [        R                  R                  5      (       a  / nU H  nUR	                  S5      n[        X@R                  R                  R                  5      n[        R                  " U5      nUR                  [        R                  5      nUS-  S-
  nUR                  U5        M     Un[        R                  " USS9nU" U5      nU$ [        US   [        R                  5      (       aG  US   R                   S:X  a  [        R"                  " USS9O[        R                  " USS9nU" U5      nU$ [        US   [$        R&                  5      (       a=  US   R                   S:X  a  [$        R(                  " USS9O[$        R                  " USS9nU$ )Nc                     U R                   S:X  a  U S   n [        R                  " U R                  SSSS5      5      n U $ )Nr   ).Nr   r   r   )ndimrg   
from_numpy	transpose)r   s    r.   numpy_to_pt:IFInpaintingPipeline.preprocess_image.<locals>.numpy_to_pt|  s>    {{a	*%%f&6&6q!Q&BCFMr/   r   RGBg     _@r   axis   )r`   rb   r   r   convertr*   r5   configsample_sizer   arrayastypefloat32appendstackr   r   concatenaterg   r   cat)rD   r}   r   	new_imageimage_s        r.   preprocess_image%IFInpaintingPipeline.preprocess_imagex  sq   %&&GE	 eAh		00I.		(8(8(D(DE&)rzz2%!+  (   EHHU+E&E  a"**--5:1X]]a5GBNN5q1RXXV[bcMdE&E
  a%,,//05a0BEIIe!,TY`aHbEr/   c           
      :   [        U[        5      (       d  U/n[        US   [        R                  5      (       a  US   R                  S:X  a  [        R
                  " USS9O[        R                  " USS9nUR                  S:X  a!  UR                  S5      R                  S5      nOiUR                  S:X  a%  UR                  S   S:X  a  UR                  S5      nO4UR                  S:X  a$  UR                  S   S:w  a  UR                  S5      nSXS:  '   SXS:  '   U$ [        US   [        R                  R                  5      (       a  / nU Hn  nUR                  S5      n[        X0R                  R                  R                  5      n[         R"                  " U5      nUS S S S 24   nUR%                  U5        Mp     Un[         R&                  " USS9nUR)                  [         R*                  5      S	-  nSXS:  '   SXS:  '   [        R,                  " U5      nU$ [        US   [         R.                  5      (       aQ  [         R&                  " U Vs/ s H  oDS S S S 24   PM     snSS9nSXS:  '   SXS:  '   [        R,                  " U5      nU$ s  snf )
Nr   r   r   r   r   r         ?Lg     o@)r`   rb   rg   r   r   r   r   	unsqueezerd   r   r   r   r*   r5   r   r   r   r   r   r   r   r   r   r   )rD   r   new_mask_imagemask_image_ms        r.   preprocess_mask_image*IFInpaintingPipeline.preprocess_mask_image  s`   *d++$JjmU\\22:DQ-:L:LPQ:Q:A6W\WbWbcmtuWvJ!#'11!4>>qA
A%**:*:1*=*B (11!4
A%**:*:1*=*B (11!4
+,JC'(,-JS()6 3 
1syy77N))11#6$[))2B2B2N2NO hh{3)$a-8%%k2  * (J
;J#**2::6>J+,JC'(,-JS()))*5J  
1rzz22:(N:a4q=)9:(NUVWJ+,JC'(,-JS()))*5J )Os   Jc                 N   [        [        X-  5      U5      n[        X-
  S5      nU R                  R                  X@R                  R
                  -  S  n[        U R                  S5      (       a1  U R                  R                  X@R                  R
                  -  5        XQU-
  4$ )Nr   set_begin_index)minr(   maxr6   	timestepsorderhasattrr   )rD   num_inference_stepsstrengthinit_timestept_startr   s         r.   get_timesteps"IFInpaintingPipeline.get_timesteps  s    C 3 >?ATU)91=NN,,W~~7K7K-K-MN	4>>#455NN**7^^5I5I+IJ777r/   c	                 8   UR                   u  ppX4-  nX:X4n[        U[        5      (       a*  [        U5      U:w  a  [	        S[        U5       SU S35      e[        XXeS9nUR                  USS9nU R                  R                  XU5      nSU-
  U-  X-  -   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   rI   rZ   r   dimr   )	rd   r`   rb   rc   rA   r   repeat_interleaver6   	add_noise)rD   r}   timesteprm   rH   rZ   rI   r   r   r   channelsheightwidthrd   noisenoised_images                   r.   prepare_intermediate_images0IFInpaintingPipeline.prepare_intermediate_images  s     5:KK1F7
v5i&&3y>Z+GA#i.AQ R&<'gi 
 UT''(=1'E~~//hGZ5(:+DDr/   r   r   r   r   guidance_scaler   r   output_typereturn_dictcallbackr   cross_attention_kwargsc                 6   Ub  [        U[        5      (       a  SnO3Ub!  [        U[        5      (       a  [        U5      nOUR                  S   nU R                  UUUUUUUU5        U R                  nUS:  nU R                  UUU	UUUUUS9u  pU(       a  [        R                  " X/5      nUR                  nUb<  U R                  R                  UUS9  U R                  R                  n[        U5      nO0U R                  R                  UUS9  U R                  R                  nU R                  XT5      u  peU R                  U5      nUR!                  UUS9nU R#                  U5      nUR!                  UUS9nUR                  S   S:X  a  UR%                  UU	-  SS	9nOUR%                  U	SS	9nUSS nUR'                  UU	-  5      nU R)                  UUUU	UUX;5      nU R+                  X5      n[-        U S
5      (       a'  U R.                  b  U R.                  R1                  5         [        U5      XPR                  R2                  -  -
  nU R5                  US9 n[7        U5       GH  u  nnU(       a  [        R                  " U/S-  5      OUnU R                  R9                  UU5      nU R;                  UUUUSS9S   n U(       av  U R=                  S5      u  n!n"U!R?                  UR                  S   SS	9u  n!n#U"R?                  UR                  S   SS	9u  n"n$U!UU"U!-
  -  -   n [        R                  " U U$/SS	9n U R                  R@                  RB                  S;  a   U R?                  UR                  S   SS	9u  n n#Un%U R                  RD                  " U UU40 UDSS0D6S   nSU-
  U%-  UU-  -   nU[        U5      S-
  :X  d)  US-   U:  aF  US-   U R                  R2                  -  S:X  a&  URG                  5         Ub  UU-  S:X  a
  U" UUU5        [H        (       d  GM  [J        RL                  " 5         GM     SSS5        UnUS:X  a  US-  S-   RO                  SS5      nURQ                  5       RS                  SSSS5      RU                  5       RW                  5       nU RY                  UUUR                  5      u  nn&n'U R[                  U5      nU R\                  b9  U R\                  R_                  X R:                  R@                  R`                  5        OUS:X  a=  Sn&Sn'[-        U S5      (       a'  U Rb                  b  U Rb                  R1                  5         OwUS-  S-   RO                  SS5      nURQ                  5       RS                  SSSS5      RU                  5       RW                  5       nU RY                  UUUR                  5      u  nn&n'U Re                  5         U(       d  UU&U'4$ [g        UU&U'S9$ ! , (       d  f       GN= f)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.
    image (`torch.Tensor` or `PIL.Image.Image`):
        `Image`, or tensor representing an image batch, that will be used as the starting point for the
        process.
    mask_image (`PIL.Image.Image`):
        `Image`, or tensor representing an image batch, to mask `image`. White pixels in the mask will be
        repainted, while black pixels will be preserved. If `mask_image` is a PIL image, it will be converted
        to a single channel (luminance) before use. If it's a tensor, it should contain one color channel (L)
        instead of 3, so the expected shape would be `(B, H, W, 1)`.
    strength (`float`, *optional*, defaults to 1.0):
        Conceptually, indicates how much to transform the reference `image`. Must be between 0 and 1. `image`
        will be used as a starting point, adding more noise to it the larger the `strength`. The number of
        denoising steps depends on the amount of noise initially added. When `strength` is 1, added noise will
        be maximum and the denoising process will run for the full number of iterations specified in
        `num_inference_steps`. A value of 1, therefore, essentially ignores `image`.
    num_inference_steps (`int`, *optional*, defaults to 50):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    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.
    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         ?)rH   rI   rJ   rK   rL   rM   )r   rI   )rI   )rI   rZ   r   text_encoder_offload_hook)totalr   F)encoder_hidden_statesr   r   )learnedlearned_ranger   pilr   r   rR   unet_offload_hook)r   r   r   )4r`   ra   rb   rc   rd   r   r_   rw   rg   r   rZ   r6   set_timestepsr   r   r   rj   r   r   rk   r   r   r   r  offloadr   progress_bar	enumeratescale_model_inputr5   chunksplitr   variance_typer   updateXLA_AVAILABLExm	mark_stepclampcpupermutefloatnumpyr   r{   r9   apply_watermarkr   r	  maybe_free_model_hooksr   )(rD   rF   r}   r   r   r   r   r   rJ   rH   r   r   rK   rL   r   r   r   r   rM   r   rm   rI   rG   rZ   noise_timestepintermediate_imagesr   num_warmup_stepsr  ir   model_input
noise_prednoise_pred_uncondnoise_pred_textrt   predicted_varianceprev_intermediate_imagesr   r   s(                                           r.   __call__IFInpaintingPipeline.__call__  sa   \ *VS"9"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)-););<O)Z&	 %%e,e4//
;
]]&]>
A!##55jCX6X^_5`J#556KQR5SJ"1Q'..z<Q/QR">>>:/DeVU_

 !::9J 445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 ,?(&*nn&9&9#6':K'Y^''# ():~9Q&QT^atTt&t# I**A9I/IqSTuX\XfXfXlXlNlpqNq '')+N0Ba0G A':; =LLNU - :Z $%QY_++Aq1EIIK''1a399;AACE 8<7N7NuV\^k^q^q7r4E="4 %%e,E +  00		8H8H8T8TUD  M!%t011d6L6L6X&&..0 QY_++Aq1EIIK''1a399;AACE 8<7N7NuV\^k^q^q7r4E="4 	##%=*<==uM^pqqc :9s   !G	X	/X		
X )T)Tr   NNNNF)NNN)Fr   )NNNr  2   Ng      @Nr   g        NNNr  TNr   TN)5__name__
__module____qualname____firstlineno__r   __annotations__r
   r   r   r   r	   r   r   r   r   r   _optional_componentsmodel_cpu_offload_seq_exclude_from_cpu_offloadboolr=   rg   no_gradr   ra   r   r(   rI   r   rw   r   r   r   re   r   r   r   r   r   r   r   r   EXAMPLE_DOC_STRINGr   r   r  	Generatorr   r   r   r'  __static_attributes____classcell__)r@   s   @r.   r1   r1   {   sp     
 233_---((jj	O  o0!. )-&Q&Q %&Q #	&Q
 !&Q !1&Q $$67&Q m,&Q "&&Q &QP ]]_
 -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5t8!0 #k\*2pf"ciioo "%,, "H/5<< /d	8 hl0 ]]_12 )-  #%# #;?/0MQ049=%* GK";?1Orc49n%Or IIOOU\\2::tCIIOO7LdSXS_S_N`bfgigqgqbrr
Or IIOOU\\2::tCIIOO7LdSXS_S_N`bfgigqgqbrr
Or Or !Or 9Or Or "%T#Y"78Or  (}Or Or  E%//43H"HIJ!Or"  -#Or$ !) 6%Or& c]'Or( )Or* 8S#u||$<d$BCD+Or, -Or. /Or0 !)c3h 81Or 3 Orr/   r1   )>r   r   r   urllib.parseparser   typingr   r   r   r   r   r   r  r   	PIL.Imager   rg   transformersr	   r
   r   loadersr   modelsr   
schedulersr   utilsr   r   r   r   r   r   r   utils.torch_utilsr   pipeline_utilsr   pipeline_outputr   r8   r   	watermarkr   torch_xla.core.xla_modelcore	xla_modelr  r  
get_loggerr+  r>   r   r   r   r   r(   r*   r5  r1   r)  r/   r.   <module>rJ     s      	  = =    H H 5 * '   . . - + $ ))MM			H	% !399?? c ciioo "6 rGr,.L Grr/   