
    _he                          d dl Zd dlZd dlmZ d dlZg dZ G d dej                  Z G d dej                  Z	 G d dej                  Z
d	 ZddZ	 	 	 ddZdS )    N)identityfourierhashgridsphere_harmonictriplane_fourierc                   r     e Zd ZdZ	 	 	 	 	 ddedededed	ed
df fdZd Zdej	        d
ej	        fdZ
 xZS )FourierEmbeddera  The sin/cosine positional embedding. Given an input tensor `x` of shape [n_batch, ..., c_dim], it converts
    each feature dimension of `x[..., i]` into:
        [
            sin(x[..., i]),
            sin(f_1*x[..., i]),
            sin(f_2*x[..., i]),
            ...
            sin(f_N * x[..., i]),
            cos(x[..., i]),
            cos(f_1*x[..., i]),
            cos(f_2*x[..., i]),
            ...
            cos(f_N * x[..., i]),
            x[..., i]     # only present if include_input is True.
        ], here f_i is the frequency.

    Denote the space is [0 / num_freqs, 1 / num_freqs, 2 / num_freqs, 3 / num_freqs, ..., (num_freqs - 1) / num_freqs].
    If logspace is True, then the frequency f_i is [2^(0 / num_freqs), ..., 2^(i / num_freqs), ...];
    Otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1)].

    Args:
        num_freqs (int): the number of frequencies, default is 6;
        logspace (bool): If logspace is True, then the frequency f_i is [..., 2^(i / num_freqs), ...],
            otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1)];
        input_dim (int): the input dimension, default is 3;
        include_input (bool): include the input tensor or not, default is True.

    Attributes:
        frequencies (torch.Tensor): If logspace is True, then the frequency f_i is [..., 2^(i / num_freqs), ...],
                otherwise, the frequencies are linearly spaced between [1.0, 2^(num_freqs - 1);

        out_dim (int): the embedding size, if include_input is True, it is input_dim * (num_freqs * 2 + 1),
            otherwise, it is input_dim * num_freqs * 2.

       T   	num_freqslogspace	input_diminclude_input
include_pireturnNc                    t                                                       |r$dt          j        |t          j                  z  }n(t          j        dd|dz
  z  |t          j                  }|r|t          j        z  }|                     d|d           || _        || _	        | 
                    |          | _        dS )	zThe initialization       @dtype      ?   frequenciesF)
persistentN)super__init__torcharangefloat32linspacepiregister_bufferr   r   get_dimsout_dim)selfr   r   r   r   r   r   	__class__s          E/tmp/cameron/UniRig/src/model/michelangelo/models/modules/embedder.pyr   zFourierEmbedder.__init__D   s     	 	m" " " KK
  .	A&m	  K  	$58#K]KEJJJ*"}}Y//    c                 R    | j         s| j        dk    rdnd}|| j        dz  |z   z  }|S )Nr   r      )r   r   )r$   r   tempr#   s       r&   r"   zFourierEmbedder.get_dimse   s:    &D$.A*=*=qq1t~1D89r'   xc                    | j         dk    r |d                                         | j        z  j        g |j        dd         dR  }| j        r=t          j        ||                                |	                                fd          S t          j        |                                |	                                fd          S |S )a   Forward process.

        Args:
            x: tensor of shape [..., dim]

        Returns:
            embedding: an embedding of `x` of shape [..., dim * (num_freqs * 2 + temp)]
                where temp is 1 if include_input is True and 0 otherwise.
        r   .NNdim)
r   
contiguousr   viewshaper   r   catsincos)r$   r+   embeds      r&   forwardzFourierEmbedder.forwardk   s     >AGQy\,,..1AAGZQTRTQTZWYZZZE! Ey!UYY[[%))++!>BGGGGy%))++uyy{{!;DDDDHr'   )r
   Tr   TT)__name__
__module____qualname____doc__intboolr   r"   r   Tensorr8   __classcell__r%   s   @r&   r	   r	      s        " "J #$"&"#'+$(0 000  0 !%	0
 "0
 .20 0 0 0 0 0B   %,        r'   r	   c                   *     e Zd ZdZ	  fdZd Z xZS )LearnedFourierEmbedderz> following @crowsonkb "s lead with learned sinusoidal pos emb c                     t                                                       |dz  dk    sJ |dz  }||z  }t          j        t	          j        |                    | _        d S )Nr)   r   )r   r   nn	Parameterr   randnweights)r$   in_channelsr0   half_dimper_channel_dimr%   s        r&   r   zLearnedFourierEmbedder.__init__   s\    aA~~~~!8"k1|EK$@$@AAr'   c                      |d         | j         d         z  dz  t          j        z  j        g |j        dd         dR  }t          j        ||                                |                                fd          }|S )z

        Args:
            x (torch.FloatTensor): [..., c]

        Returns:
            x (torch.FloatTensor): [..., d]
        r-   Nr)   r.   r/   )	rH   npr    r2   r3   r   r4   r5   r6   )r$   r+   freqs	fouriereds       r&   r8   zLearnedFourierEmbedder.forward   sz     E9T 22Q6>DWagcrclWTVWWWIq%))++uyy{{;DDD	r'   )r9   r:   r;   r<   r   r8   r@   rA   s   @r&   rC   rC      sU        HHhB B B B B      r'   rC   c                   $     e Zd Z fdZd Z xZS )TriplaneLearnedFourierEmbedderc                     t                                                       t          ||          | _        t          ||          | _        t          ||          | _        ||z   | _        d S N)r   r   rC   yz_plane_embedderxz_plane_embedderxy_plane_embedderr#   )r$   rI   r0   r%   s      r&   r   z'TriplaneLearnedFourierEmbedder.__init__   s_    !7S!I!I!7S!I!I!7S!I!I"S(r'   c                     |                      |          }|                     |          }|                     |          }||z   |z   }|S rS   )rT   rU   rV   )r$   r+   yz_embedxz_embedxy_embedr7   s         r&   r8   z&TriplaneLearnedFourierEmbedder.forward   sO    ))!,,))!,,))!,,8#h.r'   )r9   r:   r;   r   r8   r@   rA   s   @r&   rQ   rQ      sG        ) ) ) ) )      r'   rQ   c                    |dz  dk    sJ t          j        | t           j                  }t          j        |dz  t           j                  }||dz  z  }dd|z  z  }|                    d          }t          j        d||          }t          j        |          }t          j        |          }t          j        ||gd	
          }|S )Nr)   r   r   r   r   '  r.   zm,d->mdr   r/   )r   r   r   reshapeeinsumr5   r6   r4   )num_len	embed_dimposomegaoutemb_sinemb_cos
embeddingss           r&   sequential_pos_embedrg      s    q=A
,wem
4
4
4CLau}===E	Y^E%E
++b//C
,y#u
-
-CinnGinnGGW-1555Jr'   r\   c           	      "   |dz  }t          j        t          j        |           t          j        d|t           j                  z  |z                                | j                  }| dddf                             | j                  |d         z  }t          j	        t          j
        |          t          j        |          gd          }|dz  r6t          j	        |t          j        |ddddf                   gd          }|S )	aX  
    Create sinusoidal timestep embeddings.
    :param timesteps: a 1-D Tensor of N indices, one per batch element.
                      These may be fractional.
    :param dim: the dimension of the output.
    :param max_period: controls the minimum frequency of the embeddings.
    :return: an [N x dim] Tensor of positional embeddings.
    r)   r   )startendr   )deviceNr.   r/   r   )r   expmathlogr   r   tork   r   r4   r6   r5   
zeros_like)	timestepsr0   
max_periodhalfrN   args	embeddings          r&   timestep_embeddingrv      s     !8DI	*		1$em T T TTW[[ b	 b!! 
 QQQW  11E$K?D	59T??EIdOO<"EEEI
Qw WIy%*:9QQQU;K*L*LMSUVVV	r'   r   r.   r         r)      c
                     | dk    s| dk    r|dk    rt          j                    |fS | dk    rt          ||dd          }
|
|
j        fS | dk    rt          | dk    rt          t          |  dt                     )	Nr   r   r.   T)r   r   r   r   r   r   z& is not valid. Currently only supprts )rE   Identityr	   r#   NotImplementedError
ValueErrorVALID_EMBED_TYPES)
embed_typer   r   degree
num_levels	level_dimper_level_scalebase_resolutionlog2_hashmap_sizedesired_resolutionembedder_objs              r&   get_embedderr      s     ZJ)$;$;	R{}}i''	y	 	 &i04DJ J J\111	z	!	!!!	(	(	(!! JaaN_aabbbr'   )r\   )
r   r.   r   rw   rx   r)   r)   rx   ry   N)numpyrM   r   torch.nnrE   rm   r~   Moduler	   rC   rQ   rg   rv   r    r'   r&   <module>r      s$  .            ^^^ ^ ^ ^ ^ ^bi ^ ^ ^B    RY   6    RY   *  &   ( JKPR:>c c c c c cr'   