
    wi_                     ^    d dl mZmZmZmZ d dlZddlmZ ddlm	Z	m
Z
  G d de	          ZdS )	    )ListOptionalTupleUnionN   )randn_tensor   )DiffusionPipelineImagePipelineOutputc                        e Zd ZdZdZ fdZ ej                    	 	 	 	 	 dd	ed
e	e
ej        eej                 f                  dede	e         dede
eef         fd            Z xZS )DDPMPipelinea1  
    Pipeline for image generation.

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

    Parameters:
        unet ([`UNet2DModel`]):
            A `UNet2DModel` to denoise the encoded image latents.
        scheduler ([`SchedulerMixin`]):
            A scheduler to be used in combination with `unet` to denoise the encoded image. Can be one of
            [`DDPMScheduler`], or [`DDIMScheduler`].
    unetc                 v    t                                                       |                     ||           d S )N)r   	scheduler)super__init__register_modules)selfr   r   	__class__s      v/root/.openclaw/workspace/chatterbox_venv_py311/lib/python3.11/site-packages/diffusers/pipelines/ddpm/pipeline_ddpm.pyr   zDDPMPipeline.__init__)   s8    49=====       N  pilT
batch_size	generatornum_inference_stepsoutput_typereturn_dictreturnc                    t          | j        j        j        t                    r4|| j        j        j        | j        j        j        | j        j        j        f}n%|| j        j        j        g| j        j        j        R }| j        j        dk    r,t          ||          }|	                    | j                  }nt          ||| j                  }| j
                            |           |                     | j
        j                  D ]@}|                     ||          j        }	| j
                            |	|||          j        }A|dz  dz                       dd          }|                                                    dddd                                          }|d	k    r|                     |          }|s|fS t-          |
          S )aa  
        The call function to the pipeline for generation.

        Args:
            batch_size (`int`, *optional*, defaults to 1):
                The number of images to generate.
            generator (`torch.Generator`, *optional*):
                A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
                generation deterministic.
            num_inference_steps (`int`, *optional*, defaults to 1000):
                The number of denoising steps. More denoising steps usually lead to a higher quality image at the
                expense of slower inference.
            output_type (`str`, *optional*, defaults to `"pil"`):
                The output format of the generated image. Choose between `PIL.Image` or `np.array`.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`~pipelines.ImagePipelineOutput`] instead of a plain tuple.

        Example:

        ```py
        >>> from diffusers import DDPMPipeline

        >>> # load model and scheduler
        >>> pipe = DDPMPipeline.from_pretrained("google/ddpm-cat-256")

        >>> # run pipeline in inference (sample random noise and denoise)
        >>> image = pipe().images[0]

        >>> # save image
        >>> image.save("ddpm_generated_image.png")
        ```

        Returns:
            [`~pipelines.ImagePipelineOutput`] or `tuple`:
                If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is
                returned where the first element is a list with the generated images
        mps)r   )r   devicer	   g      ?r   r   r   r   )images)
isinstancer   configsample_sizeintin_channelsr#   typer   tor   set_timestepsprogress_bar	timestepssamplestepprev_sampleclampcpupermutenumpynumpy_to_pilr   )
r   r   r   r   r   r   image_shapeimagetmodel_outputs
             r   __call__zDDPMPipeline.__call__-   s   ^ di&2C88 	d	 ,	 ,	 ,	KK &ty'7'CcdiFVFbccK;u$$ 	BBBEHHT[))EE 	$+VVVE 	$$%8999""4>#;<< 	a 	aA99UA..5L N''a)'TT`EES''1--		##Aq!Q//5577%%%e,,E 	8O"%0000r   )r   Nr   r   T)__name__
__module____qualname____doc__model_cpu_offload_seqr   torchno_gradr(   r   r   	Generatorr   strboolr   r   r;   __classcell__)r   s   @r   r   r      s          #> > > > > U]__ MQ#'%* Q1 Q1Q1 E%/43H"HIJQ1 !	Q1
 c]Q1 Q1 
"E)	*Q1 Q1 Q1 _Q1 Q1 Q1 Q1 Q1r   r   )typingr   r   r   r   rA   utils.torch_utilsr   pipeline_utilsr
   r   r    r   r   <module>rK      s     0 / / / / / / / / / / /  - - - - - - C C C C C C C Cg1 g1 g1 g1 g1$ g1 g1 g1 g1 g1r   