
    i                        d dl Z d dlmZ d dlmZ ddlmZ  ej                  e
      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   )logging	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-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signals                j/home/obispo/Crisostomo_bridge/mision_env/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                       e Zd ZU dZej                  d       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.
    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                      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__\   s\    Orxx++4::JOPTUwwt}Orxx++4::JOPTUr'   N)__name__
__module____qualname____doc__loggerwarningr,   int__annotations__r   r   r   r1   compactr6    r'   r%   r)   r)   I   sO     NN	W
 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       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   r*   c                 \    t        || j                  | j                  | j                        S )N)r   r
   r   )r&   rC   r
   r   )r4   r   s     r%   r6   zFlaxTimesteps.__call__z   s)    (TXXt?S?S`d`o`o
 	
r'   N)r7   r8   r9   r:   rC   r=   r>   r
   boolr   r   r;   r<   r1   r?   r6   r@   r'   r%   rB   rB   d   sP    
 CM!OT!J
NN	W
 ZZ
 
r'   rB   )r   r   g     @Fg      ?)r   
flax.linenlinenr1   	jax.numpynumpyr   utilsr   
get_loggerr7   r;   ndarrayr=   r   rE   r&   Moduler)   rB   r@   r'   r%   <module>rN      s        
		H	%  !-{{-- - 	-
 - - - 	[[-`BII 6
BII 
r'   