o
    Gi	                     @   s   d dl Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZmZ e r7d dlm  mZ d	Znd
ZeeZG dd deeZdS )    N   )UNet1DModel)SchedulerMixin)is_torch_xla_availablelogging)randn_tensor   )AudioPipelineOutputDeprecatedPipelineMixinDiffusionPipelineTFc                       s   e Zd ZdZdZdZdedef fddZe	
 							
ddedede	jee	j B d	B ded	B dedeeB fddZ  ZS )DanceDiffusionPipelinea  
    Pipeline for audio 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 ([`UNet1DModel`]):
            A `UNet1DModel` to denoise the encoded audio.
        scheduler ([`SchedulerMixin`]):
            A scheduler to be used in combination with `unet` to denoise the encoded audio latents. Can be one of
            [`IPNDMScheduler`].
    z0.33.1unet	schedulerc                    s   t    | j||d d S )N)r   r   )super__init__register_modules)selfr   r   	__class__ p/home/ubuntu/.local/lib/python3.10/site-packages/diffusers/pipelines/dance_diffusion/pipeline_dance_diffusion.pyr   5   s   
zDanceDiffusionPipeline.__init__   d   NT
batch_sizenum_inference_steps	generatoraudio_length_in_sreturn_dictreturnc                 C   s  |du r| j jj| j jj }|| j jj }dt| j j }|d| k r4t| dd| | j jj  dt|}|| dkrc|| j jj | d | }t	| d|| j jj  d	|| j jj  d
 t|}t
| j  j}	|| j jj|f}
t|trt||krtdt| d| dt|
|| j|	d}| jj||jd | jj|	| j_| | jjD ]}|  ||j}| j|||j}trt  q|dd   ! }|ddddd|f }|s|fS t"|dS )a  
        The call function to the pipeline for generation.

        Args:
            batch_size (`int`, *optional*, defaults to 1):
                The number of audio samples to generate.
            num_inference_steps (`int`, *optional*, defaults to 50):
                The number of denoising steps. More denoising steps usually lead to a higher-quality audio sample at
                the expense of slower inference.
            generator (`torch.Generator`, *optional*):
                A [`torch.Generator`](https://pytorch.org/docs/stable/generated/torch.Generator.html) to make
                generation deterministic.
            audio_length_in_s (`float`, *optional*, defaults to `self.unet.config.sample_size/self.unet.config.sample_rate`):
                The length of the generated audio sample in seconds.
            return_dict (`bool`, *optional*, defaults to `True`):
                Whether or not to return a [`~pipelines.AudioPipelineOutput`] instead of a plain tuple.

        Example:

        ```py
        from diffusers import DiffusionPipeline
        from scipy.io.wavfile import write

        model_id = "harmonai/maestro-150k"
        pipe = DiffusionPipeline.from_pretrained(model_id)
        pipe = pipe.to("cuda")

        audios = pipe(audio_length_in_s=4.0).audios

        # To save locally
        for i, audio in enumerate(audios):
            write(f"maestro_test_{i}.wav", pipe.unet.sample_rate, audio.transpose())

        # To display in google colab
        import IPython.display as ipd

        for audio in audios:
            display(ipd.Audio(audio, rate=pipe.unet.sample_rate))
        ```

        Returns:
            [`~pipelines.AudioPipelineOutput`] or `tuple`:
                If `return_dict` is `True`, [`~pipelines.AudioPipelineOutput`] is returned, otherwise a `tuple` is
                returned where the first element is a list with the generated audio.
        Nr   r   z1 is too small. Make sure it's bigger or equal to .r   r   z is increased to z; so that it can be handled by the model. It will be cut to z after the denoising process.z/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   devicedtype)r    )audios)#r   configsample_sizesample_ratelen	up_blocks
ValueErrorintloggerinfonext
parametersr!   in_channels
isinstancelistr   _execution_devicer   set_timestepsr    	timestepstoprogress_barsamplestepprev_sampleXLA_AVAILABLExm	mark_stepclampfloatcpunumpyr	   )r   r   r   r   r   r   r%   down_scale_factororiginal_sample_sizer!   shapeaudiotmodel_outputr   r   r   __call__9   sV   7
zDanceDiffusionPipeline.__call__)r   r   NNT)__name__
__module____qualname____doc___last_supported_versionmodel_cpu_offload_seqr   r   r   torchno_gradr*   	Generatorr1   r>   boolr	   tuplerG   __classcell__r   r   r   r   r   #   s0    r   )rN   modelsr   
schedulersr   utilsr   r   utils.torch_utilsr   pipeline_utilsr	   r
   r   torch_xla.core.xla_modelcore	xla_modelr;   r:   
get_loggerrH   r+   r   r   r   r   r   <module>   s   
