
    biX                        d dl Z d dlmZ d dlmZmZmZmZ d dlZd dl	m
Z d dl
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mZmZ d	d
lmZmZmZ d	dlmZmZm Z m!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+  e$jX                  e-      Z.dZ/dZ0 G d de'      Z1 eejd                  dd      d        Z3 eejd                  d      d        Z4dejj                  fdZ6d Z7y)    N)partial)DictListOptionalUnion)
FrozenDict)unreplicate)shard)Image)CLIPImageProcessorCLIPTokenizerFlaxCLIPTextModel   )FlaxAutoencoderKLFlaxControlNetModelFlaxUNet2DConditionModel)FlaxDDIMSchedulerFlaxDPMSolverMultistepSchedulerFlaxLMSDiscreteSchedulerFlaxPNDMScheduler)PIL_INTERPOLATIONloggingreplace_example_docstring   )FlaxDiffusionPipeline)!FlaxStableDiffusionPipelineOutput) FlaxStableDiffusionSafetyCheckerFa
  
    Examples:
        ```py
        >>> import jax
        >>> import numpy as np
        >>> import jax.numpy as jnp
        >>> from flax.jax_utils import replicate
        >>> from flax.training.common_utils import shard
        >>> from diffusers.utils import load_image, make_image_grid
        >>> from PIL import Image
        >>> from diffusers import FlaxStableDiffusionControlNetPipeline, FlaxControlNetModel


        >>> def create_key(seed=0):
        ...     return jax.random.PRNGKey(seed)


        >>> rng = create_key(0)

        >>> # get canny image
        >>> canny_image = load_image(
        ...     "https://huggingface.co/datasets/YiYiXu/test-doc-assets/resolve/main/blog_post_cell_10_output_0.jpeg"
        ... )

        >>> prompts = "best quality, extremely detailed"
        >>> negative_prompts = "monochrome, lowres, bad anatomy, worst quality, low quality"

        >>> # load control net and stable diffusion v1-5
        >>> controlnet, controlnet_params = FlaxControlNetModel.from_pretrained(
        ...     "lllyasviel/sd-controlnet-canny", from_pt=True, dtype=jnp.float32
        ... )
        >>> pipe, params = FlaxStableDiffusionControlNetPipeline.from_pretrained(
        ...     "stable-diffusion-v1-5/stable-diffusion-v1-5",
        ...     controlnet=controlnet,
        ...     revision="flax",
        ...     dtype=jnp.float32,
        ... )
        >>> params["controlnet"] = controlnet_params

        >>> num_samples = jax.device_count()
        >>> rng = jax.random.split(rng, jax.device_count())

        >>> prompt_ids = pipe.prepare_text_inputs([prompts] * num_samples)
        >>> negative_prompt_ids = pipe.prepare_text_inputs([negative_prompts] * num_samples)
        >>> processed_image = pipe.prepare_image_inputs([canny_image] * num_samples)

        >>> p_params = replicate(params)
        >>> prompt_ids = shard(prompt_ids)
        >>> negative_prompt_ids = shard(negative_prompt_ids)
        >>> processed_image = shard(processed_image)

        >>> output = pipe(
        ...     prompt_ids=prompt_ids,
        ...     image=processed_image,
        ...     params=p_params,
        ...     prng_seed=rng,
        ...     num_inference_steps=50,
        ...     neg_prompt_ids=negative_prompt_ids,
        ...     jit=True,
        ... ).images

        >>> output_images = pipe.numpy_to_pil(np.asarray(output.reshape((num_samples,) + output.shape[-3:])))
        >>> output_images = make_image_grid(output_images, num_samples // 4, 4)
        >>> output_images.save("generated_image.png")
        ```
c                       e Zd ZdZej
                  fdededede	de
deeeeef   ded	ed
ej$                  f fdZdeeee   f   fdZdeej.                  eej.                     f   fdZd ZddZ	 	 	 ddej6                  dej6                  deeef   dej>                  de de!de"ej6                     de"ej6                     de!fdZ# e$e%      	 	 	 	 	 	 	 d dej6                  dej6                  deeef   dej>                  de dee!ej6                  f   dej6                  dej6                  dee!ej6                  f   de&de&fd       Z' xZ(S )!%FlaxStableDiffusionControlNetPipelinea4  
    Flax-based pipeline for text-to-image generation using Stable Diffusion with ControlNet Guidance.

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

    Args:
        vae ([`FlaxAutoencoderKL`]):
            Variational Auto-Encoder (VAE) model to encode and decode images to and from latent representations.
        text_encoder ([`~transformers.FlaxCLIPTextModel`]):
            Frozen text-encoder ([clip-vit-large-patch14](https://huggingface.co/openai/clip-vit-large-patch14)).
        tokenizer ([`~transformers.CLIPTokenizer`]):
            A `CLIPTokenizer` to tokenize text.
        unet ([`FlaxUNet2DConditionModel`]):
            A `FlaxUNet2DConditionModel` to denoise the encoded image latents.
        controlnet ([`FlaxControlNetModel`]:
            Provides additional conditioning to the `unet` during the denoising process.
        scheduler ([`SchedulerMixin`]):
            A scheduler to be used in combination with `unet` to denoise the encoded image latents. Can be one of
            [`FlaxDDIMScheduler`], [`FlaxLMSDiscreteScheduler`], [`FlaxPNDMScheduler`], or
            [`FlaxDPMSolverMultistepScheduler`].
        safety_checker ([`FlaxStableDiffusionSafetyChecker`]):
            Classification module that estimates whether generated images could be considered offensive or harmful.
            Please refer to the [model card](https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5) for
            more details about a model's potential harms.
        feature_extractor ([`~transformers.CLIPImageProcessor`]):
            A `CLIPImageProcessor` to extract features from generated images; used as inputs to the `safety_checker`.
    vaetext_encoder	tokenizerunet
controlnet	schedulersafety_checkerfeature_extractordtypec
           
      >   t         
|           |	| _        |#t        j	                  d| j
                   d       | j                  ||||||||       t        | dd       r5dt        | j                  j                  j                        dz
  z  | _        y d| _        y )Nz)You have disabled the safety checker for a   by passing `safety_checker=None`. Ensure that you abide to the conditions of the Stable Diffusion license and do not expose unfiltered results in services or applications open to the public. Both the diffusers team and Hugging Face strongly recommend to keep the safety filter enabled in all public facing circumstances, disabling it only for use-cases that involve analyzing network behavior or auditing its results. For more information, please have a look at https://github.com/huggingface/diffusers/pull/254 .)r    r!   r"   r#   r$   r%   r&   r'   r    r         )super__init__r(   loggerwarning	__class__register_modulesgetattrlenr    configblock_out_channelsvae_scale_factor)selfr    r!   r"   r#   r$   r%   r&   r'   r(   r0   s             r/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/pipelines/controlnet/pipeline_flax_controlnet.pyr-   z.FlaxStableDiffusionControlNetPipeline.__init__   s     	
!NN;DNN;K Lj j 	%!)/ 	 		
 W^^bdikoVpc$((//*L*L&MPQ&Q Rvw    promptc                     t        |t        t        f      st        dt	        |             | j                  |d| j
                  j                  dd      }|j                  S )Nz2`prompt` has to be of type `str` or `list` but is 
max_lengthTnp)paddingr<   
truncationreturn_tensors)
isinstancestrlist
ValueErrortyper"   model_max_length	input_ids)r7   r:   
text_inputs      r8   prepare_text_inputsz9FlaxStableDiffusionControlNetPipeline.prepare_text_inputs   se    &3+.QRVW]R^Q_`aa^^ ~~66 $ 

 ###r9   imagec           	      .   t        |t        j                  t        f      st        dt	        |             t        |t        j                        r|g}t        j                  |D cg c]  }t        |t
        j                         c}      }|S c c}w )Nz9image has to be of type `PIL.Image.Image` or list but is )	rA   r   rC   rD   rE   jnpconcatenate
preprocessfloat32)r7   rJ   imgprocessed_imagess       r8   prepare_image_inputsz:FlaxStableDiffusionControlNetPipeline.prepare_image_inputs   sw    %%++t!45XY]^cYdXefggeU[[)GE??TY+ZSJsCKK,H+Z[ ,[s   (!Bc                 *    | j                  ||      }|S N)r&   )r7   featuresparamshas_nsfw_conceptss       r8   _get_has_nsfw_conceptsz<FlaxStableDiffusionControlNetPipeline._get_has_nsfw_concepts   s     //&A  r9   c                    |D cg c]  }t        j                  |       }}| j                  |d      j                  }|r/t	        |      }t        | ||      }t        |      }t        |      }n| j                  ||      }d}t        |      D ]q  \  }	}
|
rI|sd}|j                         }t        j                  ||	   j                  t        j                        ||	<   t        |      s]t!        j"                  d       s ||fS c c}w )Nr=   )r@   FTr(   zPotential NSFW content was detected in one or more images. A black image will be returned instead. Try again with a different prompt and/or seed.)r   	fromarrayr'   pixel_valuesr
   _p_get_has_nsfw_conceptsunshardr	   rX   	enumeratecopyr=   zerosshapeuint8anywarningswarn)r7   imagessafety_model_paramsjitrJ   
pil_imagesrU   rW   images_was_copiedidxhas_nsfw_concepts              r8   _run_safety_checkerz9FlaxStableDiffusionControlNetPipeline._run_safety_checker   s
   :@Aeooe,A
A))*T)JWWXH 8xI\ ] '(9 :"-.A"B $ ; ;HFY Z!%./@%A 	!C!((,%#[[]F hhvc{'8'8Is$%O	 (((5 Bs   D
prompt_idsrV   	prng_seednum_inference_stepsguidance_scalelatentsneg_prompt_idscontrolnet_conditioning_scalec
                 4   	 j                   dd  \  }
}|
dz  dk7  s|dz  dk7  rt        d|
 d| d       j                  |d         d   }|j                   d   }|j                   d	   }|$ j                  d
g|z  d|d      j                  }n|} j                  |d         d   }t        j                  ||g      t        j                  gdz        | j                  j                  j                  |
 j                  z  | j                  z  f}|1t        j                  j                  ||t
        j                        }n*|j                   |k7  rt        d|j                    d|       	 fd} j                  j!                  d   ||      }|d   j"                  z  }t$        rt'        |      D ]  } ||||f      \  }} n't        j(                  j+                  d||||f      \  }}d j,                  j                  j.                  z  |z  } j,                  j1                  dd   i| j,                  j2                        j4                  dz  dz   j7                  dd      j9                  dddd      S )N@   r   z8`height` and `width` have to be divisible by 64 but are z and .r!   )rV    r<   r=   )r>   r<   r@   r   )rb   r(   zUnexpected latents shape, got z, expected c           	      b   |\  }}t        j                  |gdz        }t        j                  |j                  t         j                        |    }t        j
                  ||j                  d         }j                  j                  |||      }j                  j                  dd   it        j                  |      t        j                  |t         j                        d      \  }}j                  j                  dd   it        j                  |      t        j                  |t         j                        ||	      j                  }	t        j                  |	dd
      \  }
}|
||
z
  z  z   }	j                  j                  ||	||      j                         \  }}||fS )Nr   rZ   r   rV   r$   F)encoder_hidden_statescontrolnet_condconditioning_scalereturn_dictr#   )r}   down_block_additional_residualsmid_block_additional_residual)axis)rL   rM   array	timestepsint32broadcast_torb   r%   scale_model_inputr$   applyr#   samplesplitstepto_tuple)r   argsrs   scheduler_statelatents_inputttimestepdown_block_res_samplesmid_block_res_sample
noise_prednoise_pred_uncondnoise_prediction_textcontextru   rr   rJ   rV   r7   s               r8   	loop_bodyzBFlaxStableDiffusionControlNetPipeline._generate.<locals>.loop_body"  s   '+$G_  OOWIM:M		/33399EdKA''=+>+>q+ABH NN<<_m]^_M;???;P;P6,/0		-(		(#))4&- %#@! <Q <8"$8 6&>*		-(		(#))4&-0F.B )  f  8;yyQUV7W44*^?TWh?h-iiJ (,~~':':?JXY[b'c'l'l'n$G_O++r9   r%   )rq   rb   r*   rV   r    )methodg      ?r   )rb   rD   r!   r"   rG   rL   rM   r#   r4   in_channelsr6   jaxrandomnormalrO   r%   set_timestepsinit_noise_sigmaDEBUGrangelax	fori_loopr    scaling_factorr   decoder   clip	transpose)r7   ro   rJ   rV   rp   rq   rr   rs   rt   ru   heightwidthprompt_embeds
batch_sizer<   uncond_inputnegative_prompt_embedslatents_shaper   r   i_r   s   ` ``  `  `            @r8   	_generatez/FlaxStableDiffusionControlNetPipeline._generate   s    BC(B;!urzQWX^W__dejdkklmnn ))*VN=S)TUVW  %%a(
%%b)
!>>z!<J_c * i  *L!%!2!2<~H^!2!_`a!b//#9="IJ!, II((d+++T***	
 ?jj''	ckk'ZG}}- #A'--P[\i[j!kll&	, &	,P ..66;5HP] 7 

 F;/@@@./ T+4Q/8R+S(T **1.A9wXgNhiJGQ dhhoo444w>&-8'$((//ZaaS&&q!,66q!QBr9   r   ri   c                    |j                   dd \  }}t        |t              rGt        j                  |g|j                   d   z        }t        |j                         dkD  r	|dddf   }t        |	t              rGt        j                  |	g|j                   d   z        }	t        |j                         dkD  r	|	dddf   }	|rt        | |||||||||	
      }n| j                  |||||||||		      }| j                  |d   }|dz  j                         j                  d      }|j                   dd \  }}t        j                  |      j                  ||z  ||d      }| j                  |||      \  }}t        j                  |      }t        |      r1t!        |      D ]#  \  }}|s	t        j                  ||         ||<   % |j                  ||||d      }nt        j                  |      }d	}|
s||fS t#        ||
      S )a  
        The call function to the pipeline for generation.

        Args:
            prompt_ids (`jnp.ndarray`):
                The prompt or prompts to guide the image generation.
            image (`jnp.ndarray`):
                Array representing the ControlNet input condition to provide guidance to the `unet` for generation.
            params (`Dict` or `FrozenDict`):
                Dictionary containing the model parameters/weights.
            prng_seed (`jax.Array`):
                Array containing random number generator key.
            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.
            guidance_scale (`float`, *optional*, defaults to 7.5):
                A higher guidance scale value encourages the model to generate images closely linked to the text
                `prompt` at the expense of lower image quality. Guidance scale is enabled when `guidance_scale > 1`.
            latents (`jnp.ndarray`, *optional*):
                Pre-generated noisy latents sampled from a Gaussian distribution, to be used as inputs for image
                generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
                array is generated by sampling using the supplied random `generator`.
            controlnet_conditioning_scale (`float` or `jnp.ndarray`, *optional*, defaults to 1.0):
                The outputs of the ControlNet are multiplied by `controlnet_conditioning_scale` before they are added
                to the residual in the original `unet`.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`~pipelines.stable_diffusion.FlaxStableDiffusionPipelineOutput`] instead of
                a plain tuple.
            jit (`bool`, defaults to `False`):
                Whether to run `pmap` versions of the generation and safety scoring functions.

                    <Tip warning={true}>

                    This argument exists because `__call__` is not yet end-to-end pmap-able. It will be removed in a
                    future release.

                    </Tip>

        Examples:

        Returns:
            [`~pipelines.stable_diffusion.FlaxStableDiffusionPipelineOutput`] or `tuple`:
                If `return_dict` is `True`, [`~pipelines.stable_diffusion.FlaxStableDiffusionPipelineOutput`] is
                returned, otherwise a `tuple` is returned where the first element is a list with the generated images
                and the second element is a list of `bool`s indicating whether the corresponding generated image
                contains "not-safe-for-work" (nsfw) content.
        rw   Nr   r   r&      rc   r   F)rg   nsfw_content_detected)rb   rA   floatrL   r   r3   _p_generater   r&   roundastyper=   asarrayreshapern   rd   r_   r   )r7   ro   rJ   rV   rp   rq   rr   rs   rt   ru   r   ri   r   r   rg   safety_paramsimages_uint8_castednum_devicesr   rm   r   is_nsfws                         r8   __call__z.FlaxStableDiffusionControlNetPipeline.__call___  s>   ~ BC(ne, !YY'7*:J:J1:M'MNN:##$q(!/4!83U; -0II7T6UXbXhXhijXk6k,l):##$q(0MaQUg0V- #-F ^^#-
F *"#34M#)C<"6"6"8"?"?"H&,ll2A&6#K"$**-@"A"I"I+XbJbdjlqst"u484L4LM`boqt4u1!1XXf%F #$"+,<"= GJAw$&JJ/B1/E$Fq	G ^^KVUANFZZ'F$,--0Vfggr9   )F)NN      ?)2   g      @NNr   TF))__name__
__module____qualname____doc__rL   rO   r   r   r   r   r   r   r   r   r   r   r   r   r(   r-   rB   r   rI   r   rR   rX   rn   ndarrayr   r   r   Arrayintr   r   r   r   EXAMPLE_DOC_STRINGboolr   __classcell__)r0   s   @r8   r   r   r   se   R ;;%x%x (%x !	%x
 '%x (%x 02JLkk
%x 9%x .%x yy%xN$%T#Y*? $	 %T%++=N0N*O 	 !)L *.04/2kKKk {{k dJ&'	k
 99k !k k #++&k !-k (-kZ 12 $&47#&*CF AhKKAh {{Ah dJ&'	Ah
 99Ah !Ah eS[[01Ah Ah Ah (-UCKK-?'@Ah Ah Ah 3Ahr9   r   )
Nr   r   r   r   Nr   r   r   r   )r      )in_axesstatic_broadcasted_argnumsc
                 4    | j                  |||||||||		      S rT   )r   )
pipero   rJ   rV   rp   rq   rr   rs   rt   ru   s
             r8   r   r     s0    " >>%
 
r9   )r   )r   c                 &    | j                  ||      S rT   )rX   )r   rU   rV   s      r8   r]   r]     s    &&x88r9   xc                 p    | j                   d d \  }}| j                   dd  } | j                  ||z  g| S )Nr   )rb   r   )r   r   r   rests       r8   r^   r^   	  s@    ggbqkK7712;D199[:-555r9   c                    | j                  d      } | j                  \  }}d ||fD        \  }}| j                  ||ft        d         } t	        j
                  |       j                  |      dz  } | d    j                  dddd	      } | S )
NRGBc              3   ,   K   | ]  }||d z  z
    yw)rx   N ).0r   s     r8   	<genexpr>zpreprocess.<locals>.<genexpr>  s     '1ABJ's   lanczos)resampleg     o@r   r   r*   r   )convertsizeresizer   rL   r   r   r   )rJ   r(   whs       r8   rN   rN     s    MM% E::DAq'A'DAqLL!Q*;I*FLGEIIe##E*U2E$K!!!Q1-ELr9   )8re   	functoolsr   typingr   r   r   r   r   	jax.numpynumpyrL   r=   flax.core.frozen_dictr   flax.jax_utilsr	   flax.training.common_utilsr
   PILr   transformersr   r   r   modelsr   r   r   
schedulersr   r   r   r   utilsr   r   r   pipeline_flax_utilsr   stable_diffusionr   $stable_diffusion.safety_checker_flaxr   
get_loggerr   r.   r   r   r   pmapr   r]   r   r^   rN   r   r9   r8   <module>r      s      . . 
   , & ,  M M V V  K J 7 @ S 
		H	% 	A Hoh,A ohh 	HH0%

2 	d39 496s{{ 6r9   