3
H4W^˜  ã               @   s†   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Zd dlZej	e
ƒZG dd„ deƒZeƒ Zd	d
„ Zejdd„ ƒZdS )é    )Úabsolute_import)Údivision)Úprint_function)Úunicode_literals)ÚscopeNc               @   s8   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
d„ ZdS )ÚParameterSharingContextzc
    This class manages scope driven way of parameter sharing across different
    NameScopes.
    c             C   s   i | _ g | _d S )N)Ú_scope_overridesÚ	_contexts)Úself© r   úI/tmp/pip-build-e66j97b2/torch/caffe2/python/modeling/parameter_sharing.pyÚ__init__   s    z ParameterSharingContext.__init__c             C   s†   |}d}|j tjƒ}d}x:t|ƒD ].\}}|| tj }|| jkr"| j| }|}q"W ||kr`|S | j|ƒtjj||d d… ƒ S dS )aH  
        Recursively resolves all scope overrides, i.e multiple steps of
        override can be used.

        For example, if one provides following scope overrides:
        {'scope_b': 'scope_a'} and within 'scope_b' - {'shared_child': ''},
        then name 'w' will get resolved to the following blobs depending on the
        namescope:
          a. 'scope_a' -> 'scope_a/w'
          b. 'scope_b' -> 'scope_a/w'
          c. 'scope_c' -> 'scope_c/w'
          d. 'scope_b/shared_child' -> 'scope_a/w'
          d. 'scope_b/unshared_child' -> 'scope_a/unshared_child/w'
        r   Ú é   N)Úsplitr   Ú_NAMESCOPE_SEPARATORÚ	enumerater   Ú_resolve_scope_overridesÚjoin)r
   Úcandidate_scopeÚ
best_scopeZbest_scope_idxZ
sub_scopesZ	cur_scopeÚidxZ	sub_scoper   r   r   r      s    

z0ParameterSharingContext._resolve_scope_overridesc             C   s4   t jƒ }| j|ƒ}||kr,tjdj||ƒƒ || S )Nz#Overwiting scope {0} with scope {1})r   ÚCurrentNameScoper   ÚloggerÚinfoÚformat)r
   Únamer   r   r   r   r   Úget_parameter_name8   s    

z*ParameterSharingContext.get_parameter_namec             C   s   | j j|ƒ | jj|ƒ d S )N)r	   Úappendr   Úupdate)r
   Úshared_scopesr   r   r   Úadd_scope_overridesA   s    z+ParameterSharingContext.add_scope_overridesc             C   sB   t | jƒdkst‚| jjƒ  i | _x| jD ]}| jj|ƒ q*W d S )Nr   )Úlenr	   ÚAssertionErrorÚpopr   r   )r
   Úxr   r   r   r$   E   s
    
zParameterSharingContext.popN)	Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r!   r$   r   r   r   r   r      s    	r   c             C   s$   | r| d t jkr| t j S | S d S )Nr   éÿÿÿÿ)r   r   )Z	namescoper   r   r   Ú_normalize_namescopeP   s    
r+   c          
   c   s˜   t | tƒst‚i }tjƒ }xX| jƒ D ]L\}}|j|ƒ sHtdj||ƒƒ‚|| }|| }t|ƒ}t|ƒ}|||< q$W zt	j
|ƒ dV  W dt	jƒ  X dS )až  
    Helper function for sharing scopes.
    All the parameters within the shared_scopes, will be remapped with the
    respect of CurrentNamescope()

    I.e. if one calls ParameterSharing with {'scope_b': 'scope_'a'}, from the
    scope 'some_global_scope', it'll effectively mean, that all parameters from
    'some_global_scope/scope_b' will shared with the parameters from
    'some_global_scope/scope_a'
    z:Illegal override for parameter sharing. {} is prefix of {}N)Ú
isinstanceÚdictr#   r   r   ÚitemsÚ
startswithr   r+   Úparameter_sharing_contextr!   r$   )r    Zshared_scope_overridesZcurrent_scopeÚkÚvr   r   r   ÚParameterSharingW   s     


r3   )Ú
__future__r   r   r   r   Zcaffe2.pythonr   Ú
contextlibÚloggingÚ	getLoggerr&   r   Úobjectr   r0   r+   Úcontextmanagerr3   r   r   r   r   Ú<module>   s   
?