
    +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s  Jr  S SKJrJrJr  SSKJr  SSKJr  SSKJr  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+  SSK,J-r-  SSK.J/r/  \"" 5       (       a  S SK0J1r1  \#" 5       (       a  S SK2r2SSK J3r3  \3" 5       (       a  S SK4J5s  J6r7  Sr8OSr8\$Rr                  " \:5      r;Sr< " S S\)\5      r=g)    N)AnyCallableDictListOptionalUnion)CLIPImageProcessorT5EncoderModelT5Tokenizer   )StableDiffusionLoraLoaderMixin)UNet2DConditionModel)DDPMScheduler)BACKENDS_MAPPINGis_bs4_availableis_ftfy_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipeline   )IFPipelineOutput)IFSafetyChecker)IFWatermarker)BeautifulSoup)is_torch_xla_availableTFa$  
    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
        ... ).images
        >>> image[0].save("./if_stage_II.png")
        ```
c            +         ^  \ rS rSr% \\S'   \\S'   \\S'   \\S'   \\S'   \	\
   \S'   \	\   \S'   \	\   \S	'   \R                  " S
5      r/ SQrSrS	/r S0S\S\S\S\S\S\	\   S\	\
   S	\	\   S\4U 4S jjjrS1S jrS r\R0                  " 5              S2S\\\\   4   S\S\S\	\R:                     S\	\\\\   4      S\	\R<                     S\	\R<                     S\4S jj5       rS r S r!   S3S jr"S r#S r$\R0                  " 5       \%" \&5                          S4S\\\\   4   S \S!\S"\\'RP                  RP                  \)RT                  \R<                  4   S#\S$\\   S%\+S\	\\\\   4      S\	\   S&\+S'\	\\RX                  \\RX                     4      S\	\R<                     S\	\R<                     S(\	\   S)\S*\	\-\\\R<                  /S4      S+\S,\	\.\\/4      S-\S\4(S. jj5       5       r0S/r1U =r2$ )5IFSuperResolutionPipelineR   	tokenizertext_encoderunet	schedulerimage_noising_schedulerfeature_extractorsafety_checkerwatermarkeru5   [#®•©™&@·º½¾¿¡§~\)\(\]\[\}\{\|\\/\*]{1,})r!   r"   r'   r&   r(   ztext_encoder->unetrequires_safety_checkerc
                 L  > [         T
U ]  5         Uc*  U	(       a#  [        R                  SU R                   S35        Ub  Uc  [        S5      eUb/  UR                  R                  S:w  a  [        R                  S5        U R                  U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.   aQ  It seems like you have loaded a checkpoint that shall not be used for super resolution from {unet.config._name_or_path} as it accepts {unet.config.in_channels} input channels instead of 6. Please make sure to pass a super resolution checkpoint as the `'unet'`: IFSuperResolutionPipeline.from_pretrained(unet=super_resolution_unet, ...)`.)r!   r"   r#   r$   r%   r'   r&   r(   )r)   )
super__init__loggerwarning	__class__
ValueErrorconfigin_channelsregister_modulesregister_to_config)selfr!   r"   r#   r$   r%   r'   r&   r(   r)   r0   s             v/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/deepfloyd_if/pipeline_if_superresolution.pyr-   "IFSuperResolutionPipeline.__init__s   s     	!&=NN;DNN;K Lj j %*;*Cx 
  7 71 <NN d 	%$;)/# 	 		
 	8OP    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bs4z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>   clean_captionr6   s    r7   process>IFSuperResolutionPipeline._text_preprocessing.<locals>.process   sH    **40**40 K zz|))+Kr9   )
r   r.   r/   r   formatr   
isinstancetupleliststr)r6   r>   rC   rD   ts   ` `  r7   _text_preprocessing-IFSuperResolutionPipeline._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\/?(?!@))) 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+$)rJ   ulunquote_plusrB   rA   resubr   r>   bad_punct_regexcompilelenfindallr=   fix_texthtmlunescape)r6   captionregex2s      r7   r@   (IFSuperResolutionPipeline._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}}r9   Npromptdo_classifier_free_guidancenum_images_per_promptdevicenegative_promptprompt_embedsnegative_prompt_embedsrC   c	                    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   )rC   
max_lengthTpt)paddingrn   
truncationadd_special_tokensreturn_tensorslongest)rp   rs   r<   z\The following part of your input was truncated because CLIP can only handle sequences up to z	 tokens: )attention_mask)dtyperg   rP   z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.)rp   rn   rq   return_attention_maskrr   rs   )type	TypeError_execution_devicerG   rJ   rI   r\   shaperL   r!   	input_idstorchequalbatch_decoder.   r/   ru   tor"   rv   r#   repeatviewr1   )r6   rd   re   rf   rg   rh   ri   rj   rC   
batch_sizern   text_inputstext_input_idsuntruncated_idsremoved_textru   rv   bs_embedseq_len_uncond_tokensuncond_inputs                         r7   encode_prompt'IFSuperResolutionPipeline.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4r9   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$ )Nro   )rs   )rv   )images
clip_input)r'   r&   numpy_to_pilr   pixel_values)r6   imagerg   rv   safety_checker_inputnsfw_detectedwatermark_detecteds          r7   run_safety_checker,IFSuperResolutionPipeline.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7r9   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	signaturer$   step
parameterskeys)r6   r   r   accepts_etaextra_step_kwargsaccepts_generators         r7   prepare_extra_step_kwargs3IFSuperResolutionPipeline.prepare_extra_step_kwargs  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r9   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S:  d#  X@R                  R                  R                  :  a0  [        SU SU R                  R                  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X::w  a  [        SU
 SU 35      eg )Nr   z5`callback_steps` has to be a positive integer but is z	 of type rl   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`noise_level`: z; must be a valid timestep in `self.noising_scheduler`, [0, )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 )rG   intr1   rx   rJ   rI   r{   r%   r2   num_train_timestepsr}   TensorPILImagenpndarrayr\   )r6   rd   r   r   noise_levelcallback_stepsrh   ri   rj   check_image_typeimage_batch_sizes              r7   check_inputs&IFSuperResolutionPipeline.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  ?k-I-I-P-P-d-dd!+.ijn  kG  kG  kN  kN  kb  kb  jc  cd  e  eT""$Qx$ +U\\::/AA/<<)*+- 
 eT"""5zu||,,${{1~syy// rzz**${{1~5)12B1CCfgqfrstt *r9   c                     XX44n[        U[        5      (       a*  [        U5      U:w  a  [        S[        U5       SU S35      e[	        XXeS9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   rg   rv   )rG   rI   r\   r1   r   r$   init_noise_sigma)
r6   r   num_channelsheightwidthrv   rg   r   r{   intermediate_imagess
             r7   prepare_intermediate_images5IFSuperResolutionPipeline.prepare_intermediate_images9  s{    69i&&3y>Z+GA#i.AQ R&<'gi 
 +5fb 2NN4S4SS""r9   c                 N   [        U[        R                  5      (       d  [        U[        5      (       d  U/n[        US   [        R
                  R
                  5      (       a  U Vs/ s H<  n[        R                  " U5      R                  [        R                  5      S-  S-
  PM>     nn[        R                  " USS9n[        R                  " UR                  SSSS5      5      nGO[        US   [        R                  5      (       aS  [        R                  " USS9nUR                  S:X  a  US   n[        R                  " UR                  SSSS5      5      nO[        U[        5      (       aw  [        US   [        R                  5      (       aU  US   R                  nUS:X  a  [        R                  " USS	9nO*US
:X  a  [        R                  " USS	9nO[!        SU 35      eUR#                  X0R$                  R&                  S9nUR)                  USS	9nU$ s  snf )Nr   g     _@      ?)axisr   r   r      dim   z/Image must have 3 or 4 dimensions, instead got )rg   rv   )rG   r}   r   rI   r   r   r   arrayastypefloat32stack
from_numpy	transposer   ndimconcatr1   r   r#   rv   repeat_interleave)r6   r   rf   rg   idimss         r7   preprocess_image*IFSuperResolutionPipeline.preprocess_imageG  s   %..z%7N7NGEeAh		00KPQ5aRXXa[''

3e;cA5EQHHU+E$$U__Q1a%@AEa"**--HHU+EzzQa$$U__Q1a%@AEt$$E!Hell)K)K8==DqyEq1U2 #RSWRX!YZZiioo>''(=1'E1 Rs   (AH"r   r   r   num_inference_steps	timestepsguidance_scaler   r   output_typereturn_dictcallbackr   cross_attention_kwargsr   c                    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=(       d     U R                  R                  R                  nU=(       d     U R                  R                  R                  n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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                  S5      (       a  U R                  R#                  S5        U R                  R                  R$                  S	-  nU R'                  UU	-  UUUUR(                  UU5      nU R+                  X5      nU R-                  XIU5      n[.        R0                  " XBU4S
SS9n[        R2                  " U/UR                  S   -  UR4                  S9n[7        UR                  UUR4                  UR(                  S9nU R8                  R;                  UUUS9nU(       a  [        R                  " U/S	-  5      n[!        U S5      (       a'  U R<                  b  U R<                  R?                  5         [        U5      XPR                  R@                  -  -
  nU RC                  US9 n[E        U5       GH  u  nn [        R                  " UU/SS9n!U(       a  [        R                  " U!/S	-  5      OU!n!U R                  RG                  U!U 5      n!U R                  U!U UUUSS9S   n"U(       a|  U"RI                  S	5      u  n#n$U#RK                  U!R                  S   S	-  SS9u  n#n%U$RK                  U!R                  S   S	-  SS9u  n$n&U#UU$U#-
  -  -   n"[        R                  " U"U&/SS9n"U R                  R                  RL                  S;  a   U"RK                  UR                  S   SS9u  n"n%U R                  RN                  " U"U U40 UDSS0D6S   nU[        U5      S-
  :X  d)  US-   U:  aF  US-   U R                  R@                  -  S:X  a&  URQ                  5         Ub  UU-  S:X  a
  U" UU U5        [R        (       d  GM  [T        RV                  " 5         GM     SSS5        UnUS:X  a  US	-  S-   RY                  SS5      nUR[                  5       R]                  SS	SS5      R_                  5       Ra                  5       nU Rc                  UUUR(                  5      u  nn'n(U Re                  U5      nU Rf                  b9  U Rf                  Ri                  X@R                  R                  R                  5        OUS:X  a=  Sn'Sn([!        U S5      (       a'  U Rj                  b  U Rj                  R?                  5         OwUS	-  S-   RY                  SS5      nUR[                  5       R]                  SS	SS5      R_                  5       Ra                  5       nU Rc                  UUUR(                  5      u  nn'n(U Rm                  5         U(       d  UU'U(4$ [o        UU'U(S9$ ! , (       d  f       GN= f)uV  
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.
    height (`int`, *optional*, defaults to None):
        The height in pixels of the generated image.
    width (`int`, *optional*, defaults to None):
        The width in pixels of the generated image.
    image (`PIL.Image.Image`, `np.ndarray`, `torch.Tensor`):
        The image to be upscaled.
    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*, defaults to None):
        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 4.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.
    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).
    noise_level (`int`, *optional*, defaults to 250):
        The amount of noise to add to the upscaled image. Must be in the range `[0, 1000)`
    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.

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   r   )rf   rg   rh   ri   rj   rC   )r   rg   )rg   set_begin_indexr   bilinearT)modealign_cornersr   )r   text_encoder_offload_hook)totalr   F)encoder_hidden_statesclass_labelsr   r   )learnedlearned_ranger   pilg      ?r   ro   unet_offload_hook)r   r   r   )8rG   rJ   rI   r\   r{   r   r#   r2   sample_sizerz   r   r}   catr$   set_timestepsr   hasattrr   r3   r   rv   r   r   Finterpolatetensorrg   r   r%   	add_noiser   offloadorderprogress_bar	enumeratescale_model_inputchunksplitvariance_typer   updateXLA_AVAILABLExm	mark_stepclampcpupermutefloatnumpyr   r   r(   apply_watermarkr   maybe_free_model_hooksr   ))r6   rd   r   r   r   r   r   r   rh   rf   r   r   ri   rj   r   r   r   r   r   r   rC   r   rg   re   r   r   r   upscalednoisenum_warmup_stepsr   r   rK   model_input
noise_prednoise_pred_uncondnoise_pred_textr   predicted_variancer   r   s)                                            r7   __call__"IFSuperResolutionPipeline.__call__f  s   L *VS"9"9JJvt$<$<VJ&,,Q/J"		
 7499++775))55''
 '5s&:# 150B0B'"7+'#9' 1C 	1
- '!II'=&MNM  NN((9V(L00I"%i.NN(()<V(L00I4>>#455NN**1- yy''33q8">>..
 !::9J %%eFK==jX\]llK=8>>!3D#DX__]X^^y`h`n`no//99(EU`9a&))[MA$56K 4455$:X:X:d**224 y>,?..BVBV,VV%89\!),1#ii)<h(GQO>Yeii(9:_j"nn>>{AN "YY*7!,+A % '  
 /9C9I9I!9L6%+<+B+B;CTCTUVCW[\C\bc+B+d(%q:I:O:OP[PaPabcPdhiPiop:O:q7O%7!2^YjGj5k!kJ!&J8J+KQR!SJ>>((66>ZZ$.$4$45H5N5Nq5QWX$4$YMJ '+nn&9&9#6':K'Y^''#
 I**A9I/IqSTuX\XfXfXlXlNlpqNq '')+N0Ba0G A':; =LLNO - :T $%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q] :9s   (G[[
[- )T)F)Tr   NNNNF)NNN)NNNN2   Ng      @Nr   g        NNNr   TNr   N   T)3__name__
__module____qualname____firstlineno__r   __annotations__r
   r   r   r   r	   r   r   rX   r[   rZ   _optional_componentsmodel_cpu_offload_seq_exclude_from_cpu_offloadboolr-   rL   r@   r}   no_gradr   rJ   r   r   rg   r   r   r   r   r   r   r   r   EXAMPLE_DOC_STRINGr   r   r   r   r   	Generatorr   r   r   r  __static_attributes____classcell__)r0   s   @r7   r   r   R   s     
** 233_---((jj	O  o0!. )--Q-Q %-Q #	-Q
 !-Q "/-Q !1-Q $$67-Q m,-Q "&-Q -Q`*2pd ]]_
 -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 #Mu`#> ]]_12 )-BF#%# #;?/0MQ049=%* GK;?"+Krc49n%Kr Kr 	Kr
 SYY__bjj%,,>?Kr !Kr 9Kr Kr "%T#Y"78Kr  (}Kr Kr E%//43H"HIJKr  -Kr !) 6Kr c]Kr  !Kr" 8S#u||$<d$BCD#Kr$ %Kr& !)c3h 8'Kr( )Kr* +Kr 3 Krr9   r   )>r_   r   rX   urllib.parseparserV   typingr   r   r   r   r   r   r   r   	PIL.Imager   r}   torch.nn.functionalnn
functionalr   transformersr	   r
   r   loadersr   modelsr   
schedulersr   utilsr   r   r   r   r   utils.torch_utilsr   pipeline_utilsr   pipeline_outputr   r'   r   	watermarkr   r;   r   r=   r   torch_xla.core.xla_modelcore	xla_modelr   r   
get_loggerr  r.   r  r   r  r9   r7   <module>r2     s      	  = =      H H 5 * '  . . - + $ ! , ))MM			H	% @ar 13Q arr9   