U
    do*                     @   s,   d dl mZ dd Zd	ddZd
ddZdS )    )corec           
      C   s~   |   }t|\}}t|}|D ] }t||ks$td| d q$t }|jD ]"}|jD ]}	|	|kr\|	|	 q\qR||fS )z
    Returns a set of blobs a given net depends on and a set of
    output blobs that are written by the net
    Inputs:
        net - net to return input/output blobs for;
        lexical_scope - all external blob names visible to the net
    zInput blob z is undefined)
Protor   Zget_ssaZget_undefined_blobsstrAssertionErrorsetopoutputadd)
netlexical_scopeZ	net_protoZnet_ssa_Zinput_namesZ
input_nameZoutput_namesr   r    r   B/tmp/pip-unpacked-wheel-ua33x9lu/caffe2/python/control_ops_util.pyget_external_blob_names   s    	



r   Nc                    s,  t ||\}}t }t }|r,t ||\}}||B }	||B }
|g}|dd |	D 7 }dd |
D }td}dd |D }dd |D }dd || D t||B }fdd|D }| |  d	 }|| || || || |j|||	 ||d
 |j
|  i }|	 |d< d}|rtd}dd |D }dd |D }dd || D  t||B } fdd|D }| |  d }|| || || || |j|||	 ||d
 |j
|  |	 |d< | g |g |r| g |g | j||f| | j
|  dS )a  
    A helper function to add an If op to the net.
    Automatically determines whether blobs in the then/else subnets are external
    (from the outer workspace) or local (visible only inside subnet's workspace)
    based on lexical scope - set of all outer blob names visible to the 'If'
    operator. All the blobs in then/else subnets with names matching a name in lexical
    scope and all the blobs that are first used as the operators' inputs are
    considered outer blobs - these blobs must exist in the outer workspace,
    then/else subnets can read their values and new values written into these blobs
    will be visible outside of the 'If' operator. All other blobs are local - exist
    only within inner workspaces for then/else.
    Inputs:
        if_net - net to add an If op to;
        cond_blob - scalar bool blob reference, used as If condition;
        lexical_scope - a set of outer blob names visible to then/else branches;
        then_net/else_net - nets (core.Net) for then/else branches
    c                 S   s   g | ]}t j|d dqS N)namer
   r   ZBlobReference.0br   r   r   
<listcomp>C   s     zadd_if_op.<locals>.<listcomp>c                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r   D   s     do_then_netc                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r   I   s     c                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r   K   s     c                 S   s   g | ]}t |qS r   r   r   r   r   r   r   L   s    c                    s   g | ]}  |qS r   indexr   )then_input_output_names_orderedr   r   r   P   s    z/workspace_if_thenr
   inner_blobsouter_blobs_idxthen_netNdo_else_netc                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r   k   s     c                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r   m   s     c                 S   s   g | ]}t |qS r   r   r   r   r   r   r   n   s    c                    s   g | ]}  |qS r   r   r   )else_input_output_names_orderedr   r   r   r   s    z/workspace_if_elseelse_net)r   r   r   NetlistNextScopedBlobNameappendDor   AddExternalOutputCreateScopeZIf)Zif_net	cond_blobr   r   r"   Zthen_input_blob_namesZthen_output_blob_namesZelse_input_blob_namesZelse_output_blob_namesinput_blob_namesoutput_blob_namesZ	if_inputsZ
if_outputsr   Zthen_input_blobsZthen_output_blobsZthen_outer_blob_namesZthen_outer_blob_names_idxZdo_then_workspace_blobZif_argsZdo_else_workspace_blobr    Zelse_input_blobsZelse_output_blobsZelse_outer_blob_namesZelse_outer_blob_names_idxr   )r!   r   r   	add_if_op$   s      













r.   c                    s  t ||\O dd D }dd D }|g| }g | }td}	dd || D tB }
fdd|
D }| |  d }|| || || || |	j||| |
|dd	 |	j	|  i }|	 |d
< d}|rnt ||\}}d}| j
D ] }t||jkrd} q4q|sBtdt||kr^|t| dd |D }t||kstddd |D }td}dd || D  t||B } fdd|D }| |  d }|| || |j||| ||d |j	|  | |d< |fdd|D 7 }|fdd|D 7 }t||krn| jg |tjjdd | g |g |r| g |g | j||f| | j	|  dS )a  
    A helper function to add a While op to the net. Same rules for determining
    outer and inner blobs as for the 'If' operator apply for the 'While' operator
    loop and condition subnets. If specified, condition net is executed in a separate
    workspace before the first and after each iteration, the last operator must have
    a single scalar boolean output that is written into the condition blob.
    Inputs:
        while_net - net to add a While op to;
        cond_blob - scalar bool blob reference, used as a stop condition;
        lexical_scope - a set of outer blob names visible to the loop's body;
        loop_body_net - net to execute on each iteration;
        condition_body_net - net to compute condition value
    c                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r      s     z add_while_op.<locals>.<listcomp>c                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r      s     do_loop_body_netc                 S   s   g | ]}t |qS r   r   r   r   r   r   r      s    c                    s   g | ]}  |qS r   r   r   )loop_input_output_names_orderedr   r   r      s    z/workspace_loop_bodyT)r
   r   r   Zcopy_external_blobsZloop_netNFz0Condition net does not write into condition blobc                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r      s   z/Condition blob expected in condition net outputc                 S   s   g | ]}t j|d dqS r   r   r   r   r   r   r      s   Zdo_loop_condition_netc                 S   s   g | ]}t |qS r   r   r   r   r   r   r      s    c                    s   g | ]}  |qS r   r   r   )cond_input_output_names_orderedr   r   r      s   z/workspace_loop_condr   Zcond_netc                    s   g | ]}t | kr|qS r   r   r   )r,   r   r   r      s    c                    s   g | ]}t | kr|qS r   r   r   )r-   r   r   r      s    )Zdtypevalue)r   r   r#   r$   r%   r&   r'   r(   r   r)   r   r   r   r   r	   ZConstantFillZDataTypeZBOOLr*   ZWhile)Z	while_netr+   r   Zloop_body_netZcondition_body_netZloop_inputsZloop_outputsZwhile_inputsZwhile_outputsr/   Zloop_body_outer_blob_namesZloop_body_outer_blob_names_idxZdo_loop_body_workspace_blobZ
while_argsZcond_workspace_blobZcond_input_blob_namesZcond_output_blob_namesZfound_condition_outputr   Zcond_inputsZcond_outputsZcondition_netZcond_body_outer_blob_namesZcond_body_outer_blob_names_idxr   )r1   r,   r0   r-   r   add_while_op   s     







 






r3   )N)N)Zcaffe2.pythonr   r   r.   r3   r   r   r   r   <module>   s   
j 