3
I4W^h6  ã               @   sZ  d dl mZ d dl mZ d dl mZ d dl mZ d dlmZ d dlmZ d dl	m
Z
 d dlmZmZ d d	lmZ d d
lZd d
lZd d
lZd d
lZd dlmZmZmZmZ dZdZd4dd„Zdd„ Zdd„ Zd5dd„Zdd„ Z dd„ Z!dd„ Z"dd„ Z#dd„ Z$d d!„ Z%d"d#„ Z&G d$d%„ d%e'ƒZ(d&d'„ Z)d(d)„ Z*d6d*d+„Z+d,d-„ Z,d.d/„ Z-d0d1„ Z.d2d3„ Z/d
S )7é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)Ú
caffe2_pb2)Úcontainer_abcs)Ú	viewitems)ÚDecodeErrorÚMessage)Útext_formatN)Úinteger_typesÚbinary_typeÚ	text_typeÚstring_typesZoptimizer_iterationZiteration_mutexc                s\   ˆ pg ‰ t ˆ tƒsˆ g‰ tdd„ ˆ D ƒƒs8tdjˆ ƒƒ‚‡ fdd„}|| ƒ} ||ƒ}| |kS )zM
    Two ops are identical except for each field in the `ignore_fields`.
    c             s   s   | ]}t |tƒV  qd S )N)Ú
isinstancer   )Ú.0Úf© r   ú4/tmp/pip-build-e66j97b2/torch/caffe2/python/utils.pyú	<genexpr>    s    z OpAlmostEqual.<locals>.<genexpr>z*Expect each field is text type, but got {}c                s0   t j| ƒ} x ˆ D ]}| j|ƒr| j|ƒ qW | S )N)ÚcopyÚdeepcopyÚHasFieldZ
ClearField)ÚopÚfield)Úignore_fieldsr   r   Úclean_op#   s
    


zOpAlmostEqual.<locals>.clean_op)r   ÚlistÚallÚAssertionErrorÚformat)Zop_aZop_br   r   r   )r   r   ÚOpAlmostEqual   s    
r!   c             C   sP   | j dkr0tj| jtjd�j| j | j| j| jƒS tj| jtjd�j| j	j
ƒS d S )Nr   )Údtype)ÚnumÚnpÚasarrayÚdataÚfloat32ÚreshapeZchannelsÚheightÚwidthÚshapeZdim)Zblobr   r   r   ÚCaffeBlobToNumpyArray/   s
    
r,   c             C   s0  | j tjjkr(tj| jtjd�j| j	ƒS | j tjj
krPtj| jtjd�j| j	ƒS | j tjjkrxtj| jtjd�j| j	ƒS | j tjjkr tj| jtjd�j| j	ƒS | j tjjkrÈtj| jtjd�j| j	ƒS | j tjjkrðtj| jtjd�j| j	ƒS | j tjjk�rtj| jtjd�j| j	ƒS tdt| j ƒ ƒ‚d S )N)r"   z$Tensor data type not supported yet: )Ú	data_typer   ÚTensorProtoÚFLOATr$   r%   Ú
float_datar'   r(   ÚdimsÚDOUBLEÚdouble_dataÚfloat64ÚINT32Ú
int32_dataÚintÚINT16Úint16ÚUINT16Úuint16ÚINT8Úint8ÚUINT8Úuint8ÚRuntimeErrorÚstr)Útensorr   r   r   ÚCaffe2TensorToNumpyArray:   s.    rC   c             C   s¶  t jƒ }|jj| jƒ |r ||_| jtjkrTt jj	|_
|jjt| jƒ jtƒƒƒ �n^| jtjkrŠt jj|_
|jjt| jƒ jtjƒƒƒ �n(| jtjks¢| jtjkrÊt jj|_
|jj| jƒ jtjƒjƒ ƒ nè| jtjkrþt jj|_
|jjt| jƒ jtjƒƒƒ n´| jtjk�r4t jj|_
|jjt| jƒ jtjƒƒƒ n~| jtjk�rjt jj|_
|jjt| jƒ jtjƒƒƒ nH| jtjk�r t jj|_
|jjt| jƒ jtjƒƒƒ nt dt!| jƒ ƒ‚|S )Nz#Numpy data type not supported yet: )"r   r.   r1   Úextendr+   Únamer"   r$   r'   r/   r-   r0   r   ÚflattenZastypeÚfloatr4   r2   r3   r7   Zint32r5   r6   Útolistr9   r8   r;   r:   r=   r<   r?   r>   r@   rA   )ZarrrE   rB   r   r   r   ÚNumpyArrayToCaffe2TensorV   s8    

 




rI   c             C   sR  t jƒ }| |_t|tjƒ}t|tjƒrL|jj	tj
krL|jj|jƒ jƒ ƒ |S t|tjƒrf|jƒ jƒ }nt|tjƒr|tj|ƒ}t	|ƒtkr’||_�n¼t	|ƒtksªt	|ƒtkr´||_�nšt|tƒrÈ||_�n†t|tƒrâ|jdƒ|_�nlt|t jƒrþ|jj|ƒ �nPt|tƒ�r|jƒ |_�n6|�rLtdd„ |D ƒƒ�rL|jjdd„ |D ƒƒ �n|�r~tdd„ |D ƒƒ�r~|j jdd„ |D ƒƒ nÐ|�r°tdd„ |D ƒƒ�r°|j!jdd„ |D ƒƒ nž|�rØtd	d„ |D ƒƒ�rØ|j"j|ƒ nv|�r
td
d„ |D ƒƒ�r
|j!jdd„ |D ƒƒ nD|�r8t#dj$| |t	|ƒt%dd„ |D ƒƒƒƒ‚nt#dj$| |t	|ƒƒƒ‚|S )z*Makes an argument based on the value type.zutf-8c             s   s    | ]}t |ƒttjgkV  qd S )N)ÚtyperG   r$   Úfloat_)r   Úvr   r   r   r   ™   s    zMakeArgument.<locals>.<genexpr>c             s   s(   | ] }t |ƒtjkr|jƒ n|V  qd S )N)rJ   r$   rK   Úitem)r   rL   r   r   r   r   ›   s    c             s   s,   | ]$}t |ƒtkp"t |ƒttjgkV  qd S )N)rJ   r   Úboolr$   Úint_)r   rL   r   r   r   r   ž   s    c             s   s(   | ] }t |ƒtjkr|jƒ n|V  qd S )N)rJ   r$   rO   rM   )r   rL   r   r   r   r   ¡   s    c             s   s"   | ]}t |tƒpt |tƒV  qd S )N)r   r   r   )r   rL   r   r   r   r   ¤   s    c             s   s&   | ]}t |tƒr|jd ƒn|V  qdS )zutf-8N)r   r   Úencode)r   rL   r   r   r   r   §   s   c             s   s   | ]}t |tjƒV  qd S )N)r   r   ÚNetDef)r   rL   r   r   r   r   ª   s    c             s   s   | ]}t |tƒV  qd S )N)r   r
   )r   rL   r   r   r   r   ¬   s    c             s   s   | ]}|j ƒ V  qd S )N)ÚSerializeToString)r   rL   r   r   r   r   ­   s    zBUnknown iterable argument type: key={} value={}, value type={}[{}]c             s   s   | ]}t |ƒV  qd S )N)rJ   )r   rL   r   r   r   r   ³   s    z5Unknown argument type: key={} value={}, value type={})&r   ZArgumentrE   r   r   ÚIterabler$   Zndarrayr"   rJ   r'   ZfloatsrD   rF   rH   ZgenericZasscalarrG   r   r   rN   Úir   Úsr   rP   rQ   ÚnZCopyFromr
   rR   r   ÚintsÚstringsÚnetsÚ
ValueErrorr    Úset)ÚkeyÚvalueÚargumentÚiterabler   r   r   ÚMakeArgumentw   s\    





"r`   c             C   s@   | ƒ }yt j||ƒ |S  t jtfk
r:   |j|ƒ |S X dS )a,  Reads a protobuffer with the given proto class.

    Inputs:
      cls: a protobuffer class.
      s: a string of either binary or text protobuffer content.

    Outputs:
      proto: the protobuffer of cls

    Throws:
      google.protobuf.message.DecodeError: if we cannot decode the message.
    N)r   ÚParseÚ
ParseErrorÚUnicodeDecodeErrorZParseFromString)ÚclsrU   Úobjr   r   r   ÚTryReadProtoWithClass¿   s    
rf   c             C   s.   x(t |ƒD ]\}}t| ƒ|kr
|| ƒS q
W dS )zNGets a specific field from a protocol buffer that matches the given class
    N)r   rJ   )re   Úfunction_maprd   Úfuncr   r   r   ÚGetContentFromProtoÕ   s    ri   c             C   sL   xFt |ƒD ]2\}}yt|| ƒ}||ƒS  tk
r:   w
Y q
X q
W tdƒ‚d S )Nz$Cannot find a fit protobuffer class.)r   rf   r	   )rU   rg   rd   rh   re   r   r   r   ÚGetContentFromProtoStringÝ   s    
rj   c             C   sJ   t |ƒ�}t| |jƒ ƒ}W dQ R X t |dƒ�}|j|jƒ ƒ W dQ R X dS )z:Convert a text file of the given protobuf class to binary.NÚw)Úopenrf   ÚreadÚwriterR   )Zproto_classÚfilenameZout_filenamer   ÚprotoZfidr   r   r   ÚConvertProtoToBinaryè   s    
rq   c              C   sˆ   ddl m} m} | j|jdg dg|j| jdƒd�ƒ | jdƒ}|ddd…f |ddd…f tj	|ddd…f ƒtj	|ddd…f ƒdœS )	z|Get GPU memory usage stats from CUDAContext/HIPContext. This requires flag
       --caffe2_gpu_memory_tracking to be enabledr   )Ú	workspaceÚcoreZGetGPUMemoryUsageZ____mem____)Údevice_optionNé   )Ztotal_by_gpuZ
max_by_gpuÚtotalZ	max_total)
Úcaffe2.pythonrr   rs   ÚRunOperatorOnceÚCreateOperatorÚDeviceOptionZGpuDeviceTypeZ	FetchBlobr$   Úsum)rr   rs   Úbr   r   r   ÚGetGPUMemoryUsageStatsð   s    
r}   c             C   s<   ddl m}m} |j|jdt| ƒt| ƒ|jtjƒd�ƒ d S )Nr   )rr   rs   ZFree)rt   )	rw   rr   rs   rx   ry   r   rz   r   ÚCPU)Zblobsrr   rs   r   r   r   Ú
ResetBlobs  s    r   c               @   s   e Zd ZdZedd„ ƒZdS )Ú	DebugModea=  
    This class allows to drop you into an interactive debugger
    if there is an unhandled exception in your python script

    Example of usage:

    def main():
        # your code here
        pass

    if __name__ == '__main__':
        from caffe2.python.utils import DebugMode
        DebugMode.run(main)
    c             C   sl   y|ƒ S  t k
r   ‚ Y nL tk
rf   dd l}tdƒ ttjƒ d ƒ t |jƒ  tjdƒ ‚ Y nX d S )Nr   zjEntering interactive debugger. Type "bt" to print the full stacktrace. Type "help" to see command listing.ru   )ÚKeyboardInterruptÚ	ExceptionÚpdbÚprintÚsysÚexc_infoZpost_mortemÚexit)rd   rh   rƒ   r   r   r   Úrun!  s    
zDebugMode.runN)Ú__name__Ú
__module__Ú__qualname__Ú__doc__Úclassmethodrˆ   r   r   r   r   r€     s   r€   c             C   s   | |krt dj|| |ƒƒ‚d S )Nz{}. {} != {})r‚   r    )Úar|   Úmsgr   r   r   ÚraiseIfNotEqual5  s    r�   c                s   t jˆ ƒ‡ fdd„ƒ}|S )z¤
    Use this method to decorate your function with DebugMode's functionality

    Example:

    @debug
    def test_foo(self):
        raise Exception("Bar")

    c                 s   ‡ ‡‡fdd„}t j|ƒS )Nc                  s
   ˆˆ ˆŽS )Nr   r   )Úargsr   Úkwargsr   r   rh   H  s    z$debug.<locals>.wrapper.<locals>.func)r€   rˆ   )r‘   r’   rh   )r   )r‘   r’   r   ÚwrapperF  s    zdebug.<locals>.wrapper)Ú	functoolsÚwraps)r   r“   r   )r   r   Údebug:  s    r–   c             C   sž   |dk	r|nt }|dk	r|nt}ddlm} | j|ƒs�|j|jtjƒƒ�@ | j	g |dg||j
jd�}| jg |gƒ}|j||g|gƒ W dQ R X n
| j|ƒ}|S )zÕ
    Often, a mutex guarded iteration counter is needed. This function creates a
    mutex iter in the net uniquely (if the iter already existing, it does
    nothing)

    This function returns the iter blob
    Nr   )rs   ru   )r+   r]   r"   )ÚOPTIMIZER_ITERATION_NAMEÚITERATION_MUTEX_NAMErw   rs   ZBlobIsDefinedZDeviceScoperz   r   r~   ZConstantFillZDataTypeZINT64ZCreateMutexZ
AtomicIterZ
GetBlobRef)Zinit_netÚnetÚiterZ
iter_mutexZiter_valrs   Ú	iterationr   r   r   ÚBuildUniqueMutexIterO  s    

rœ   c          	      sˆ   t | ƒt kst‚i ‰ xnt| ƒD ]b}||jƒ krt| |ƒ‰tˆtƒrˆˆ jƒ ksxtdj| |ˆ‡ ‡fdd„ˆ D ƒd ˆƒƒ‚ˆˆ |< qW ˆ S )Nz<Failed to resolve {} as Enum: duplicate entries {}={}, {}={}c                s   g | ]}ˆ | ˆkr|‘qS r   r   )r   r\   )ÚenumrL   r   r   ú
<listcomp>}  s    z$EnumClassKeyVals.<locals>.<listcomp>r   )	rJ   r   ÚdirÚupperÚgetattrr   r   Úvaluesr    )rd   Úkr   )r�   rL   r   ÚEnumClassKeyValsq  s    

$r¤   c             C   s¨   i }xž| D ]–}|j dƒsq
x„|jjD ]n}|jdkr4q$|j|jkrb|j |jƒrbt||jƒ||j< P q$|j|jkr$t||jƒ}t|ƒdkr$|||j< P q$W d||j< q
W |S )z‘
    Convert a list of arguments to a name, value dictionary. Assumes that
    each argument has a name. Otherwise, the argument is skipped.
    rE   r   N)	r   Z
DESCRIPTORÚfieldsrE   ÚlabelZLABEL_OPTIONALr¡   ZLABEL_REPEATEDÚlen)r‘   ZansÚargÚdÚlist_r   r   r   Ú
ArgsToDict„  s"    



r«   c             C   s6   | j dkst‚| jd| j d fttd| j d ƒƒ ƒS )Nru   r   )Úndimr   Ú	transposeÚtupleÚrange)rB   r   r   r   Ú	NHWC2NCHW�  s    r°   c             C   s,   | j dkst‚| jdttd| j ƒƒ d ƒS )Né   r   ru   )r   )ru   )r¬   r   r­   r®   r¯   )rB   r   r   r   Ú	NCHW2NHWC¢  s    r²   )N)N)NNr   )0Ú
__future__r   r   r   r   Zcaffe2.protor   Zcaffe2.python.compatibilityr   Zfuture.utilsr   Zgoogle.protobuf.messager	   r
   Zgoogle.protobufr   r…   r   r”   Znumpyr$   Úsixr   r   r   r   r—   r˜   r!   r,   rC   rI   r`   rf   ri   rj   rq   r}   r   Úobjectr€   r�   r–   rœ   r¤   r«   r°   r²   r   r   r   r   Ú<module>   sH   

!H$  
