3
Ðñß^�  ã               @   sl   d dl Z d dlZ d dlmZ d dlmZmZ ddlT ddlm	Z	 dd„ Z
G d	d
„ d
eƒZG dd„ deƒZdS )é    N)ÚFunction)ÚModuleÚ	Parameteré   )Ú*)ÚSparseConvNetTensorc             C   sº   t j| | ƒ}xšt| ƒD ]Ž}xPt|ƒD ]D}||dd…f ||dd…f  jƒ }d| |||f  |||f< q$W ||dd…f ||dd…f  jƒ }d| d |||f< qW t j|ƒ}||fS )z§
    Calculate two matrices: a, a_inverse
    Use torch.mm(coordinates, a_inverse) to map into permutohedral coordinates space, before input goes to SparseConvNet
    Ng      à?r   )ÚtorchÚzerosÚrangeÚsumÚinverse)Ú	dimensionÚaÚiÚjZdpÚai© r   úP/home/yuhw/wc/SparseConvNet/sparseconvnet/permutohedralSubmanifoldConvolution.pyÚpermutohedral_basis   s    $ $
r   c               @   s.   e Zd Zddd„Zdd„ Zdd„ Zdd	„ Zd
S )Ú#PermutohedralSubmanifoldConvolutionr   c             C   sŠ   t j| ƒ || _|| _|| _|| _|d | d | _d| | j d }ttj	| j||| || ƒj
d|ƒƒ| _|r†ttj	|ƒjƒ ƒ| _d S )Né   r   g       @g      à?r   )r   Ú__init__r   ÚgroupsÚnInÚnOutÚfilter_volumer   r   ÚTensorÚnormal_ÚweightÚzero_Úbias)Úselfr   r   r   r    r   Ústdr   r   r   r      s    
z,PermutohedralSubmanifoldConvolution.__init__c             C   sd   |j jƒ dks$|j jdƒ| jks$t‚tƒ }|j|_|j|_tj	|j | j
t| dƒ|j|j| jƒ|_ |S )Nr   r   r    )ÚfeaturesÚnelementÚsizer   ÚAssertionErrorr   ÚmetadataÚspatial_sizeÚ+PermutohedralSubmanifoldConvolutionFunctionÚapplyr   ÚoptionalTensorr   )r!   ÚinputÚoutputr   r   r   Úforward,   s    $
z+PermutohedralSubmanifoldConvolution.forwardc             C   s   d}|S )Nr   r   )r!   Úsr   r   r   Ú__repr__:   s    z,PermutohedralSubmanifoldConvolution.__repr__c             C   s   |S )Nr   )r!   Úout_sizer   r   r   Úinput_spatial_size>   s    z6PermutohedralSubmanifoldConvolution.input_spatial_sizeN)r   )Ú__name__Ú
__module__Ú__qualname__r   r.   r0   r2   r   r   r   r   r      s   
r   c               @   s$   e Zd Zedd„ ƒZedd„ ƒZdS )r)   c          	   C   sZ   || _ || _|jƒ }| j||||ƒ t jtjj||||||ƒ7  _t j|j	ƒ 7  _|S )N)
Úinput_metadatar   ÚnewÚsave_for_backwardÚsparseconvnetÚforward_pass_multiplyAdd_countÚSCNÚ0PermutohedralSubmanifoldConvolution_updateOutputÚforward_pass_hidden_statesr$   )ÚctxÚinput_featuresr   r    r6   r(   r   Úoutput_featuresr   r   r   r.   C   s$    	
z3PermutohedralSubmanifoldConvolutionFunction.forwardc       	   	   C   s`   | j \}}}}|jƒ }tj|ƒ}tj|ƒ}tjj|| j|||jƒ |||ƒ ||t	|ƒd d d d fS )N)
Úsaved_tensorsr7   r   Ú
zeros_liker9   r;   Ú,PermutohedralSubmanifoldConvolution_backwardr6   Ú
contiguousÚoptionalTensorReturn)	r>   Úgrad_outputr?   r(   r   r    Ú
grad_inputÚgrad_weightÚ	grad_biasr   r   r   Úbackward`   s    

z4PermutohedralSubmanifoldConvolutionFunction.backwardN)r3   r4   r5   Ústaticmethodr.   rJ   r   r   r   r   r)   B   s   r)   )r9   Úsparseconvnet.SCNZtorch.autogradr   Útorch.nnr   r   ÚutilsÚsparseConvNetTensorr   r   r   r)   r   r   r   r   Ú<module>   s   %