B
    #=6\¸  ã               @   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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 )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literalsN)Ú
basestring)Ú
caffe2_pb2ú/c               C   s   t tdƒsdt_tjS )NÚ	namescopeÚ )ÚhasattrÚ_threadlocal_scoper	   © r   r   ú6/tmp/pip-install-l3r2oljg/torch/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 d 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 d 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   )Úscoper   Z	new_scoper   r   r   r   ÚDeviceScope7   s*    


r   c              c   s"   t ƒ } zdt_dV  W d| t_dS )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.
    r
   N)r   r   r	   )r   r   r   r   ÚEmptyNameScopeY   s    
r    c              c   s"   t ƒ } zdt_dV  W d| t_dS )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)Ú
__future__r   r   r   r   Ú
contextlibÚ	threadingZpast.builtinsr   Zcaffe2.protor   r   Úlocalr   r   r   Úcontextmanagerr   r   r    r!   r   r   r   r   Ú<module>   s"   !