o
    ۷i                     @   s   d dl Z d dlmZ d dlmZ ddlmZ e	e
Z					ddejd	ed
edededededejfddZG dd dejZG dd dejZdS )    N   )logging        @F      ?	timestepsembedding_dim
freq_shiftmin_timescalemax_timescaleflip_sin_to_cosscalereturnc                 C   s   | j dks	J d|d dksJ d| dt|d }t|| ||  }|ttj|tjd|   }	t| dt|	d }
||
 }|rZtj	t
|t|gdd}ntj	t|t
|gdd}t|t| d |g}|S )	a  Returns the positional encoding (same as Tensor2Tensor).

    Args:
        timesteps (`jnp.ndarray` of shape `(N,)`):
            A 1-D array of N indices, one per batch element. These may be fractional.
        embedding_dim (`int`):
            The number of output channels.
        freq_shift (`float`, *optional*, defaults to `1`):
            Shift applied to the frequency scaling of the embeddings.
        min_timescale (`float`, *optional*, defaults to `1`):
            The smallest time unit used in the sinusoidal calculation (should probably be 0.0).
        max_timescale (`float`, *optional*, defaults to `1.0e4`):
            The largest time unit used in the sinusoidal calculation.
        flip_sin_to_cos (`bool`, *optional*, defaults to `False`):
            Whether to flip the order of sinusoidal components to cosine first.
        scale (`float`, *optional*, defaults to `1.0`):
            A scaling factor applied to the positional embeddings.

    Returns:
        a Tensor of timing signals [N, num_channels]
    r   zTimesteps should be a 1d-arrayr   r   zEmbedding dimension z should be even)dtype)axis)ndimfloatmathlogjnpexparangefloat32expand_dimsconcatenatecossinreshapeshape)r   r   r	   r
   r   r   r   num_timescaleslog_timescale_incrementinv_timescalesembscaled_timesignal r%   V/home/ubuntu/vllm_env/lib/python3.10/site-packages/diffusers/models/embeddings_flax.pyget_sinusoidal_embeddings   s     r'   c                   @   sF   e Zd ZU dZed dZeed< e	j
Ze	jed< ejdd ZdS )	FlaxTimestepEmbeddinga=  
    Time step Embedding Module. Learns embeddings for input time steps.

    Args:
        time_embed_dim (`int`, *optional*, defaults to `32`):
            Time step embedding dimension.
        dtype (`jnp.dtype`, *optional*, defaults to `jnp.float32`):
            The data type for the embedding parameters.
    Flax classes are deprecated and will be removed in Diffusers v1.0.0. We recommend migrating to PyTorch classes or pinning your version of Diffusers.    time_embed_dimr   c                 C   s>   t j| j| jdd|}t |}t j| j| jdd|}|S )Nlinear_1)r   namelinear_2)nnDenser+   r   silu)selftembr%   r%   r&   __call__\   s   
zFlaxTimestepEmbedding.__call__N)__name__
__module____qualname____doc__loggerwarningr+   int__annotations__r   r   r   r/   compactr4   r%   r%   r%   r&   r(   I   s   
 
r(   c                   @   sN   e Zd ZU dZdZeed< dZeed< dZ	e
ed< ed ejd	d
 ZdS )FlaxTimestepsa  
    Wrapper Module for sinusoidal Time step Embeddings as described in https://huggingface.co/papers/2006.11239

    Args:
        dim (`int`, *optional*, defaults to `32`):
            Time step embedding dimension.
        flip_sin_to_cos (`bool`, *optional*, defaults to `False`):
            Whether to flip the sinusoidal function from sine to cosine.
        freq_shift (`float`, *optional*, defaults to `1`):
            Frequency shift applied to the sinusoidal embeddings.
    r*   dimFr   r   r	   r)   c                 C   s   t || j| j| jdS )N)r   r   r	   )r'   r?   r   r	   )r2   r   r%   r%   r&   r4   z   s   zFlaxTimesteps.__call__N)r5   r6   r7   r8   r?   r;   r<   r   boolr	   r   r9   r:   r/   r=   r4   r%   r%   r%   r&   r>   d   s   
 r>   )r   r   r   Fr   )r   
flax.linenlinenr/   	jax.numpynumpyr   utilsr   
get_loggerr5   r9   ndarrayr;   r   r@   r'   Moduler(   r>   r%   r%   r%   r&   <module>   s:   

0