
    bi	                         d dl Z d dlmZ d dlmZ 	 	 	 	 	 ddej                  dede	de	de	de
de	d	ej                  fd
Z G d dej                        Z G d dej                        Zy)    N	timestepsembedding_dim
freq_shiftmin_timescalemax_timescaleflip_sin_to_cosscalereturnc                    | j                   dk(  sJ d       |dz  dk(  sJ d| d       t        |dz        }t        j                  ||z        ||z
  z  }|t	        j
                  t	        j                  |t        j                        | z        z  }	t	        j                  | d      t	        j                  |	d      z  }
||
z  }|r@t	        j                  t	        j                  |      t	        j                  |      gd      }n?t	        j                  t	        j                  |      t	        j                  |      gd      }t	        j                  |t	        j                  |       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]
       zTimesteps should be a 1d-array   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signals                [/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/diffusers/models/embeddings_flax.pyget_sinusoidal_embeddingsr%      s=   < >>Q@ @@1!X%9-#XX!=A-.N"hh}}'DEZdIde"SWWSZZckk-Z^u]u-u%vvN
//)Q
'#//.!*L
LC #+K#''+"68L!MTUV#''+"68L!MTUV[[#))I"6q"9=!IJFM    c                   z    e Zd ZU dZdZeed<   ej                  Z	ej                  ed<   e
j                  d        Zy)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.
        time_embed_dimr   c                      t        j                  | j                  | j                  d      |      }t        j                  |      } t        j                  | j                  | j                  d      |      }|S )Nlinear_1)r   namelinear_2)nnDenser*   r   silu)selftembs     r$   __call__zFlaxTimestepEmbedding.__call__R   s\    Orxx++4::JOPTUwwt}Orxx++4::JOPTUr&   N)__name__
__module____qualname____doc__r*   int__annotations__r   r   r   r/   compactr4    r&   r$   r(   r(   D   s;     NC{{E399"ZZ r&   r(   c                   `    e Zd ZU dZdZeed<   dZeed<   dZ	e
ed<   ej                  d        Zy	)
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   c                 \    t        || j                  | j                  | j                        S )N)r   r   r   )r%   r?   r   r   )r2   r   s     r$   r4   zFlaxTimesteps.__call__k   s)    (TXXt?S?S`d`o`o
 	
r&   N)r5   r6   r7   r8   r?   r9   r:   r   boolr   r   r/   r;   r4   r<   r&   r$   r>   r>   Z   s<    
 CM!OT!JZZ
 
r&   r>   )r   r   g     @Fg      ?)r   
flax.linenlinenr/   	jax.numpynumpyr   ndarrayr9   r   rA   r%   Moduler(   r>   r<   r&   r$   <module>rH      s        !-{{-- - 	-
 - - - 	[[-`BII ,
BII 
r&   