3
I4W^{1  ã               @   sœ   d dl mZ d dl mZ d dl mZ d dl mZ d dlZd dlmZm	Z	m
Z
 d dlmZ dd	„ Zd
d„ Zdd„ Zddd„ZG dd„ deƒZG dd„ dƒZdS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)ÚcoreÚ	workspaceÚ
net_drawer)Ú
caffe2_pb2c             C   s   t jj| dd„ | jD ƒƒS )Nc             S   s   g | ]}|d  ‘qS )Ú_grad© )Ú.0Úsr   r   ú?/tmp/pip-build-e66j97b2/torch/caffe2/python/gradient_checker.pyú
<listcomp>   s    z$getGradientForOp.<locals>.<listcomp>)r   ZGradientRegistryZGetGradientForOpÚoutput)Úopr   r   r   ÚgetGradientForOp   s    r   c             C   s^   | | }t |tjƒrtj| S t |tjƒs.t‚d}tjd|j|j	|g|ƒ}tj
|ƒ tj| S )NZtmp_dense_gradZSparseToDense)Ú
isinstancer   ZBlobReferencer   ÚblobsÚGradientSliceÚAssertionErrorÚCreateOperatorÚindicesÚvaluesÚRunOperatorOnce)Úgrad_mapÚinput_to_checkZ	grad_blobZ
dense_gradÚsparse_to_dense_opr   r   r   Ú_get_grad_blob   s    

r   c                sª   | j | jƒ d ƒ}|j|ƒ‰ x"|p$i jƒ D ]\}}|tj|< q*W x4|D ],}|ˆ ks`tdj|ƒƒ‚t|ƒtjksFt‚qFW tj	|ƒ dd„ |D ƒ}	‡ fdd„|D ƒ}
|	|
|fS )NÚ_copyz.{} has no gradient, cannot check net gradient.c             S   s   g | ]}|t j| f‘qS r   )r   r   )r   r   r   r   r   r   5   s    z_get_grad.<locals>.<listcomp>c                s   i | ]}t ˆ |ƒ|“qS r   )r   )r   r   )r   r   r   ú
<dictcomp>6   s   z_get_grad.<locals>.<dictcomp>)
ZCloneÚNameZAddGradientOperatorsÚitemsr   r   r   ÚformatÚstrÚ
RunNetOnce)ÚnetÚoutputsÚoutputs_with_gradÚinput_valuesÚinputs_with_gradsZgrad_netÚnameÚvaluer   Zforward_resultsZgradsr   )r   r   Ú	_get_grad'   s    




r-   Ú c             C   s:   t jj| ||||d� t j| | ƒjƒ }t j|ƒt|ƒfS )N)ÚatolÚrtolÚerr_msg)ÚnpZtestingZassert_allcloseÚabsÚflattenZmeanÚmax)Zvalue1Zvalue2Ú	thresholdr1   Údeltar   r   r   Ú_assert_close<   s    r8   c               @   s(   e Zd Zeddd„ƒZedd	d
„ƒZdS )ÚNetGradientCheckerNçH¯¼šò×z>Fc          
      sÀ  ‡fdd„‰ |rhxVt | ƒD ]J\}}tj|ƒjƒ }	tdt|ƒ |jƒ  d dƒ�}
|
j|	ƒ W d Q R X qW ‡ ‡‡fdd„t| |ƒD ƒ}|rît|Ž \}}}xVt |ƒD ]J\}}tj|ƒjƒ }	tdt|ƒ |jƒ  d dƒ�}
|
j|	ƒ W d Q R X q W |d	 \}}}x¾|d
d … D ]®\}}}t	|ƒt	|ƒk�s*t
‚xBt t||ƒƒD ]0\}\\}}\}}t|||dj|||ƒd� �q:W |jƒ |jƒ k�s„t
‚x0|jƒ D ]$\}}t|| ||dj|ƒd� �qŽW �q
W d S )Nc                s   ‡ fdd„ˆD ƒS )Nc                s   g | ]}ˆ | ‘qS r   r   )r   Úi)Únet_outputsr   r   r   M   s    zWNetGradientChecker.CompareNets.<locals>._get_output_with_grad_names.<locals>.<listcomp>r   )r<   )Úoutputs_with_grad_ids)r<   r   Ú_get_output_with_grad_namesL   s    zCNetGradientChecker.CompareNets.<locals>._get_output_with_grad_namesZcaffe2_net_forward_z.pngÚwbc                s$   g | ]\}}t ||ˆ |ƒˆˆƒ‘qS r   )r-   )r   r&   r<   )r>   r)   r*   r   r   r   X   s   z2NetGradientChecker.CompareNets.<locals>.<listcomp>Zcaffe2_net_r   é   zVDifferent forward pass results for output id {}. Corresponding output blobs: {} and {})r1   z Different gradients for input {})Ú	enumerater   ZGetPydotGraphZ
create_pngÚopenr$   r!   ÚwriteÚzipÚlenr   r8   r#   Úkeysr"   )Únetsr'   r=   r*   r)   r6   Zprint_net_imagesr;   r&   ZpngÚfÚresultsÚ_Zbackward_netsZfirst_net_resultsZfirst_net_gradsÚnet_resultsÚ	net_gradsÚidxZblob1Zblob_value1Zblob2Zblob_value2ZblobZblob_grad_valuer   )r>   r)   r*   r=   r   ÚCompareNetsH   s>     
zNetGradientChecker.CompareNetsç-Cëâ6?çš™™™™™©?Tc                sÊ   t | g ˆˆˆgƒ\}}‰ |ˆ }	‡ ‡‡fdd„}
‡‡fdd„}tjˆˆ ƒ}xJtˆˆ jƒD ]8}|
|||ƒƒ}|
||| ƒƒ}|| | d |j|< q\W dj| jƒ ƒ}|r¼|dj| jƒ ƒ7 }t	|	|||ƒS )Nc                s*   | t jˆ< t jˆ ƒ tdd„ ˆD ƒƒjƒ S )Nc             S   s   g | ]}t j| ‘qS r   )r   r   )r   r   r   r   r   r   „   s   z=NetGradientChecker.Check.<locals>.GetLoss.<locals>.<listcomp>)r   r   r%   Úsum)Z	new_value)Úfull_netr   r(   r   r   ÚGetLoss€   s
    

z)NetGradientChecker.Check.<locals>.GetLossc                s"   ˆˆ  j ƒ }|j|   |7  < |S )N)ÚcopyÚflat)Údimr7   Zinput_value)r   r)   r   r   ÚGetValueˆ   s    z*NetGradientChecker.Check.<locals>.GetValueé   z'Error in gradient check for net_copy {}z: {})
r-   r2   Ú
zeros_likeÚrangeÚsizerU   r#   r!   ZProtor8   )r&   r(   r)   r   Z	step_sizer6   Z	print_netrK   rL   Zanalytic_gradrS   rW   Úgrad_estimaterV   Úpos_lossÚneg_lossr1   r   )rR   r   r)   r(   r   ÚCheckw   s    
zNetGradientChecker.Check)Nr:   F)rO   rP   T)Ú__name__Ú
__module__Ú__qualname__ÚstaticmethodrN   r_   r   r   r   r   r9   G   s    , r9   c               @   s,   e Zd ZdZd
dd„Zdd„ Zddd	„ZdS )ÚGradientCheckerzÌA gradient checker in Python.

    This is not the most efficient way to check gradients, as the Python
    interface will involve a lot of copies back and forth operations. Use at your
    own risk.
    NÚgradient_checkc             C   s:   || _ || _|ptjƒ | _|| _|d kr0i | _n|| _d S )N)Ú	_stepsizeÚ
_thresholdr	   ZDeviceOptionÚ_device_optionÚ_workspace_nameÚ_input_device_options)ÚselfZstepsizer6   Údevice_optionZworkspace_nameÚinput_device_optionsr   r   r   Ú__init__£   s    zGradientChecker.__init__c             C   s€  x:t t|ƒƒD ]*}tj|| || | jj|| | jƒƒ qW || }	tj|ƒ d}
xD|D ]<}|j| }tj	|ƒ}|
|d j
ƒ 7 }
tj|d || jƒ qXW |
d }
tj|ƒ t|tjƒ�rntjdtj|	tjd�ƒ tjdtjdtjd�ƒ tjd	|j|jd
 | jd�}tjd	|j|jd
 | jd�}tjddd|jd
 |jd
 dgdƒ}tj|ƒ tj|ƒ tj|ƒ tj	dƒ}n
tj	|ƒ}|
|fS )Ng        rX   r
   g       @Úzeros)ZdtypeÚonesr@   ZEnsureCPUOutputZ_cpu)rl   ZScatterWeightedSum)rZ   rE   r   ÚFeedBlobrj   Úgetrh   r   r   Z	FetchBlobrQ   ZRunOperatorsOncer   r   r   r2   rY   Zfloat32rp   r   r   r   )rk   r   Úgrad_opsÚinputsZinput_namesr   Ú	grad_nameÚoutputs_with_gradsr;   ÚxÚlossrM   r+   ÚarrZ	gv_cpu_opZ	gi_cpu_opr   Úgradr   r   r   ÚGetLossAndGrad´   sD    










zGradientChecker.GetLossAndGradc          	   C   s"  t jƒ }| j|kr t j| jdƒ |jj| jƒ |dkrBt|ƒ\}}|pRtj	|ƒd }	x8t
|ƒD ],\}
}t j|j|
 ||	j|j|
 | jƒƒ q^W || }| j||||j|||ƒ\}}tj|| ƒ}|j|jkràtdj|j|jƒƒ‚|| j}x¬t|ƒD ] }|| j|  | j7  < | j||||j|||ƒ\}}|| j|  | jd 8  < | j||||j|||ƒ\}}|| j|  | j7  < || | j d |j|< qôW tj||| j| jd� }tj|ƒ�rötj|ƒ}tdƒ ttj||j| |j| gƒjƒ d}nd}| j|k�rt jƒ  t j|ƒ |||fS )	aq  Checks the operator in a very simple fashion by stacking a sum of
        squares on the top.

        Inputs:
          op: the operator to be checked.
          inputs: the input data in numpy arrays.
          input_to_check: an index specifying which input blob we should
              check.
          outputs_with_grads: indices specifying which output blobs will we
              need to check gradients with. For these outputs, we will collect a
              squared sum and also feed in their gradients.
          grad_operator: the gradient operator. If not given, we will get the
              gradient operator from the gradient registry.
          input_device_options: an optional mapping from input names to
              DeviceOptions (to override the default DeviceOption)
        Outputs:
          boolean: True if it passes, False if it does not pass.
        TNr   z5Mismatched gradient shapes: estimated ({}), grad ({})rX   )r/   r0   z'Failed. [idx, grad, grad_estimate] are:F) r   ZCurrentWorkspaceri   ZSwitchWorkspacerl   ZCopyFromrh   r   r   ZInferOpBlobDevicesAsDictrA   rq   Úinputrr   r{   r2   rY   ÚshapeÚ	Exceptionr#   r[   rZ   rU   rf   Úiscloserg   ÚanyZflatnonzeroÚprintZvstackÚTZResetWorkspace)rk   r   rt   r   rv   rs   rm   Zold_ws_nameZg_inputrj   r;   ry   ru   rx   rz   r\   Zdims_to_checkZcurrent_dimr]   rJ   r^   Zfail_matrM   Úretr   r   r   ÚCheckSimpleå   s\    






"
zGradientChecker.CheckSimple)Nre   N)NN)r`   ra   rb   Ú__doc__rn   r{   r„   r   r   r   r   rd   ›   s     
7 rd   )r.   )Ú
__future__r   r   r   r   Znumpyr2   Zcaffe2.pythonr   r   r   Zcaffe2.protor	   r   r   r-   r8   Úobjectr9   rd   r   r   r   r   Ú<module>   s   
T