U
    ‰d'  ã                   @   s„   d dl Z d dlZd dlmZ d dlmZ dZe ¡ add„ Z	dd„ Z
e jdd
d„ƒZe jddd„ƒZe jdd„ ƒZe jdd„ ƒZdS )é    N)Ú
basestring)Ú
caffe2_pb2ú/c                   C   s   t tdƒsdt_tjS )NÚ	namescopeÚ )ÚhasattrÚ_threadlocal_scoper   © r	   r	   ú7/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/scope.pyÚCurrentNameScope   s    
r   c                   C   s   t tdƒsd t_tjS )NÚdevicescope)r   r   r   r	   r	   r	   r
   ÚCurrentDeviceScope   s    
r   Fc              	   c   st   t | tƒs| d kstdƒ‚tƒ }| r,| t nd} |r<| t_ntj|  t_z
d V  W 5 tj | ¡shtdƒ‚|t_X d S )Nz,NameScope takes in a string as its argument.r   zAThe namescope variable is changed from outside NameScope() calls.)Ú
isinstancer   ÚAssertionErrorr   Ú_NAMESCOPE_SEPARATORr   r   Úendswith)ÚprefixÚresetÚ	old_scoper	   r	   r
   Ú	NameScope#   s    ÿ
ÿr   c                 c   s¾   t  ¡ }| r,t| t jƒs tdƒ‚| | ¡ n|s8tdƒ‚|rB||_tƒ }|rh| d¡rh| d¡sh|j|_|r„t|dƒr„|j	 
|j	¡ |j	 ¡  |t_z
d V  W 5 tj|ks²tdƒ‚|t_X d S )Nz?DeviceScope takes in a caffe2_pb2.DeviceOption as its argument.z7At least one argument should be non-null in DeviceScopeÚ	node_nameÚ
extra_infoz=The device scope is changed from outside DeviceScope() calls.)r   ZDeviceOptionr   r   ZCopyFromr   r   ZHasFieldr   r   ÚextendÚsortr   r   )Zscoper   Z	new_scoper   r	   r	   r
   ÚDeviceScope7   s0    ÿÿ

ÿr   c               	   c   s,   t ƒ } dzdt_dV  W 5 | t_£  dS X  )z©
    Allow users to 'disable' the name scope behaviour.

    This sets the CurrentNameScope() to None, so that the field is
    not set in CreateOperator(...), etc.
    Nr   )r   r   r   ©r   r	   r	   r
   ÚEmptyNameScopeY   s    
r   c               	   c   s,   t ƒ } dzdt_dV  W 5 | t_£  dS X  )a"  
    Allow users to 'disable' the device scope behaviour (so it can be
    controlled at a NetDef::DeviceOption level, not overridden at
    OperatorDef::DeviceOption level).

    This sets the CurrentDeviceScope() to None, so that the field is
    not set in CreateOperator(...), etc.
    N)r   r   r   r   r	   r	   r
   ÚEmptyDeviceScopej   s    

r   )F)N)Ú
contextlibÚ	threadingZpast.builtinsr   Zcaffe2.protor   r   Úlocalr   r   r   Úcontextmanagerr   r   r   r   r	   r	   r	   r
   Ú<module>   s   !
