U
    JcT                     @   s  d dl Z d dlmZ d dlmZmZmZmZmZm	Z	 d dl
Z
d dl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mZmZmZmZmZ d
ddgZe eZeddG dd
 d
e Z!eddG dd de Z"eddG dd de Z#eG dd dZ$G dd dZ%dS )    N)	dataclass)AnyCallableDictListOptionalTuple)compatibility)map_arg   )	ShapeProp)split_by_tags)CALLABLE_NODE_OPSFxNetAccFusionsFinderNamesNodeListNodeSetTensorOrTensorsTensorsFxNetMinimizerBadModuleErrorFxNetMinimizerRunFuncError!FxNetMinimizerResultMismatchErrorF)Zis_backward_compatiblec                   @   s   e Zd ZdZdS )r   z9
    Raised if failed to split out a minimize module
    N__name__
__module____qualname____doc__ r   r   @/tmp/pip-unpacked-wheel-gikjz4vx/torch/fx/passes/net_min_base.pyr      s   c                   @   s   e Zd ZdZdS )r   z@
    Raised if error occurs during run_a or run_b functions
    Nr   r   r   r   r   r   (   s   c                   @   s   e Zd ZdZdS )r   zJ
    Raised if comparing function thinks the results are mismatching.
    Nr   r   r   r   r   r   1   s   c                   @   sJ   e Zd ZU dZdZeed< dZeed< dZ	eed< dZ
eed< dd	 Zd
S )_MinimizerSettingBasea;  
    Args:
    `accumulate_error`: Instead of using a's input for both converted module to verify
    , use the previous outputs of each converted module as input to accumulate the
    errors.

    `traverse_method`: "sequential" or "binary" or "accumulate"
    Determine the way of traverse the nodes in FX module.

    `find_all`: Minimizer will go through the entire model and return all problematic nodes.

    `return_intermediate`: If true, when using `run_nodes()` function to run the
    model, intermediate results of all the ops will be returned as output.
    Faccumulate_error
sequentialtraverse_methodfind_allreturn_intermediatec                 C   s4   d}t |  D ]\}}|d| d| d7 }q|S )NzFX Minimizer Settings:
	z: 
)varsitems)selfZsettings_strkvr   r   r   __str__P   s    z_MinimizerSettingBase.__str__N)r   r   r   r   r    bool__annotations__r"   strr#   r$   r,   r   r   r   r   r   :   s   
r   c                   @   s  e Zd ZdZejjeee	e	e
geeef f edddZejjee	dddZejjee	ddd	Ze	e	ejjd
ddZejjeeeef dddZedddZeeejjef dddZejjee
dddZeeeedddZeedddZeedddZeedd d!Zee ee ed"d#d$Z d0ee ee d&d'd(Z!e"e d)d*d+Z#d,d- Z$d1ee ee ed"d.d/Z%d%S )2_MinimizerBasea  
    This class is used to automatically find problematic nodes in a model. It takes a FX
    graphmodule and generate some submodules while traverse the graph. Then two functions
    `run_a` and `run_b` will be used to run the same submodule and a function `compare_fn`
    will be used to compare the results.

    Currently we provides two ways to traverse the graph and generate submodules.
        1. Sequential traversal: this will traverse the graph node by node and generate
           one submodule with one sigle node.
        2. Binary searching: this will do a binary search style traversal on the graph.

    For internal Users, a guide can be found here https://fb.quip.com/HDtuAgiKGfkP.
    )modulesample_input
compare_fnsettingsc           	      C   s   t |tjjst|| _|| _|| _|| _i | _	i | _
i | _g | _d| _dd | jjjD }t| jj| j  t| j| | _dd | jjjD }t|t| jkstt|D ]$\}}|| | j	|< || | j
|< qd S )Nr   c                 S   s   h | ]}|j tkr|qS r   )opr   .0noder   r   r   	<setcomp>   s    
 z*_MinimizerBase.__init__.<locals>.<setcomp>c                 S   s   g | ]}|j d kr|jqS placeholderr5   namer6   r   r   r   
<listcomp>   s    
 z+_MinimizerBase.__init__.<locals>.<listcomp>)
isinstancetorchfxGraphModuleAssertionErrorr1   r2   r3   r4   	a_outputs	b_outputsresultsreports	iterationgraphnodesr   	propagater   fusionslen	enumerate)	r)   r1   r2   r3   r4   Zcallable_nodesplaceholdersir=   r   r   r   __init__h   s,    	z_MinimizerBase.__init__)modinputsreturnc                 C   s   t ddS )zz
        Run `mod` with `inputs` and generate output. The output will be compared with
        output of run_b().
        zrun_a() is not implemented.NRuntimeErrorr)   rR   rS   r   r   r   run_a   s    z_MinimizerBase.run_ac                 C   s   t ddS )zz
        Run `mod` with `inputs` and generate output. The output will be compared with
        output of run_a().
        zrun_b() is not implemented.NrU   rW   r   r   r   run_b   s    z_MinimizerBase.run_b)a_resultb_result	submodulec                 C   s   t dd |jjD }t|jd tjjrP|| j|jd j	< || j
|jd j	< n8t|jd D ](\}}|| | j|j	< || | j
|j	< q^dS )a  
        Store the outputs of self.run_a() and self.run_b() into self.a_outputs and
        self.b_outputs, so that we can use them when execute preceding nodes that
        use those outputs as inputs.

        Args:
            a_result: Output of self.run_a(). Could be a tensor or tensors.
            b_result: Output of self.run_b(). Could be a tensor or tensors.
            submodule: The module that generates a_result and b_result.
        c                 s   s   | ]}|j d kr|V  qdS )outputN)r5   r6   r   r   r   	<genexpr>   s    
 z0_MinimizerBase._store_outputs.<locals>.<genexpr>r   N)nextrI   rJ   r?   argsr@   rA   NoderD   r=   rE   rN   )r)   rZ   r[   r\   Zoutput_noderP   argr   r   r   _store_outputs   s    z_MinimizerBase._store_outputs)main_modulesubmod_pathrT   c           	         s   g  g }t ||}dd |jjD }t|| j krb|D ]$} | j|  || j|  q:nP| jj	rzt
d| d tjjtd fdd}||}|| j  |   }| jj	s  fS  |fS )al  
        Try get submodule inputs from stored outputs. If not found then use
        torch_glow.get_submod_inputs to get the inputs.

        If accumulate_error is False, use a_input for run_a() and run_b()
        otherwise use a_input for run_a and b_input for run_b.

        Args:
            main_module: Top-levlel fx module.
            submod_path: Path to the submodule we want to run and compare results.

        Returns:
            a_input: List of tensor(s) that will be used by run_a() as submodule inputs.
            b_input: List of tensor(s) that will be used by run_b() as submodule inputs.
        c                 S   s   g | ]}|j d kr|jqS r:   r<   r6   r   r   r   r>      s    
 z5_MinimizerBase._get_submod_inputs.<locals>.<listcomp>z)Can't find previous stored outputs named !r)   rS   c                    s   | d S Nr   rg   a_inputr   r   
get_inputs   s    z5_MinimizerBase._get_submod_inputs.<locals>.get_inputs)getattrrI   rJ   setrD   keysappendrE   r4   r    printr@   nnModuler   Zregister_forward_pre_hookr2   remove)	r)   rd   re   b_inputr\   rO   r=   rk   handler   ri   r   _get_submod_inputs   s(    


z!_MinimizerBase._get_submod_inputs)selected_nodesc                 C   sR   | j jjD ]B}|jtkrq
||kr*d|_q
tdd |jD rFd|_q
d|_q
dS )ag  
        Tag selected nodes with tag "minimize". Nodes with the same tags will
        be split to the same submodule afterwards.

        Args:
            selected_nodes: Nodes that we want to minimize. We will tag those nodes
                with "minimize", all preceding nodes with "main_0" and all following
                nodes with "main_1".
        minimizec                 s   s"   | ]}|j tkr|jd kV  qdS )>   main_1rx   N)r5   r   tag)r7   nr   r   r   r^     s   
z,_MinimizerBase._tag_nodes.<locals>.<genexpr>ry   main_0N)r1   rI   rJ   r5   r   rz   anyZall_input_nodes)r)   rw   r8   r   r   r   
_tag_nodes   s    

z_MinimizerBase._tag_nodes)rJ   rT   c                 C   sv   |  | t| jdddg}d}| D ].\}}d|kr:q(|dkrH|}q(td| q(|dkrntd| ||fS )ak  
        Split self.module so that one submodule consists of `nodes` and only `nodes`.

        Args:
            nodes: Nodes that we want to include in the minimize submodule.

        Returns:
            split_module (torch.fx.GraphModule): the module after split.
            submodule_name (str): the name of the submodule that consists of `nodes`.
        r|   rx   ry    z0Expected only one minimize submodule with nodes z,Minimize submodule was not found with nodes )r~   r   r1   Znamed_childrenr   )r)   rJ   split_moduleZsubmodule_nameZ
child_name_r   r   r   _build_submodule  s     
z_MinimizerBase._build_submodule)r   submod_nameoutput_namesc                 C   s  t ||}| ||\}}t| jdkr:| jg  d| _| j| jd  }|d |rg }|jjD ].}	|	jdkr~|j	|	 |	j
|krd||	 qd|jt|dkr|d nt| |j  |  |jjD ]}	|	jdkrt|	jdd }
q| ||}| ||}| ||| |}|dkr4dd	 |
D }| |||\}}|| j|
< |d
|  |s|d|
  td|
 dS )a  
        Run the submodule in `split_module` that has name `submod_name`
        using `self.run_a` and `self.run_b` and compare their results.

        Args:
            split_module: Main module that contains the minimize submodule.
            submod_name: Name of the minimize submodule.
            output_names: Names of the node we want to output. If None, we
                will use the original output.
        r   r   zRun and compare ...r]   c                 S   s   | j S rh   r=   )xr   r   r   <lambda>`      z1_MinimizerBase._run_and_compare.<locals>.<lambda>Nc                 S   s   g | ]}t |qS r   )r/   )r7   r+   r   r   r   r>   i  s     z3_MinimizerBase._run_and_compare.<locals>.<listcomp>zNumerical accuracy = zResult mismatch for )rl   rv   rM   rG   ro   rH   rI   rJ   r5   Z
erase_noder=   r]   tupleZlintZ	recompiler
   r`   rX   rY   rc   r3   rF   r   )r)   r   r   r   r\   rj   rt   reportZoutput_nodesr8   Z
result_keyrZ   r[   namesZnumeric_resultZbool_resultr   r   r   _run_and_compare7  sD    







z_MinimizerBase._run_and_compare)	all_nodes	start_idxend_idxrT   c              	   C   s  ||| }g }| j | |  jd7  _|d| j d |d| d|d  dt|  t|}|D ]}|| jkrn|| j|  qnz | |\}}	| ||	g  W n t	t
fk
r   t|dkr|d| d | | | Y S |d | | t|d	 }
| ||||
 }t|d
krF| jjsF| Y S | |||
 |}t|d
kr|d|  | | | Y S X |d | | t S dS )z9
        Recursive binary search implementation.
        r   zBinary search iteration .zFrom node index z to z&. Size of the interested node list is zcThis is the last node in the sub-module. Search in the current branch is successful with culprit = zMProceed to split and lower the halves of the current sub-module individually.   r   zaFurther split and lowering found no errors. Unable to minimize the submodule with list of nodes: zNo discrepancy found.N)rG   ro   rH   rM   rm   rL   updater   r   r   r   print_report_binary_search_implr4   r#   )r)   r   r   r   rJ   r   	cur_nodesr8   r   r   Zmidculpritsr   r   r   r   s  sP    







z"_MinimizerBase._binary_search_implc                 C   s   |  |dt|S )z7
        Binary search on `nodes` for culprit.
        r   )r   rM   )r)   rJ   r   r   r   _binary_traverse  s    z_MinimizerBase._binary_traversec              	   C   sP  t  }|D ]>}g }| j| |  jd7  _|d| j d |d|j  td|j  |h}|| jkr| j| }z.| |\}}| 	|||jg | 
| W q
 tk
r   || |d|  | 
| | jjs| Y   S Y q
 tk
rH   || |d|  | 
| | jjsD| Y   S Y q
X q
|S )zX
        Traverse `nodes` one by one and determine if any of them is a culprit.
        r   zSequential traverse iteration r   zVisit node: z"Found culprit from numeric error: zFound culprit from run error: )rm   rG   ro   rH   r=   _LOGGERinforL   r   r   r   r   addr4   r#   r   r   )r)   rJ   r   r8   r   r   r   r   r   r   r   _sequential_traverse  s8    







z#_MinimizerBase._sequential_traversec           	   
   C   s0  t  }t  }| jjr td |S |D ]}g }| j| |  jd7  _|d| j d || |j}|d k	rt	|t
r|d }|d k	rt	|tstd| |d|  z,| |\}}| |||g | | W q$ ttfk
r(   || |d|  | | | Y   S X q$|S )	Nz9'Find All' mode is not supported in accumulate traversal.r   zAccumulate traverse iteration r   r   zminimize: node_name: z
Add node: zFound culprit )rm   r4   r#   rp   rG   ro   rH   r   r=   r?   r   r/   rC   r   r   r   r   r   )	r)   rJ   r   Znodes_to_runr8   r   Z	node_namer   r   r   r   r   _accumulate_traverse  s>    


 

z#_MinimizerBase._accumulate_traverse)startendrT   c                 C   sV   g }|dk}| j jjD ]:}|jtkr&q|j|kr4d}|rB|| |j|kr qRq|S )z
        Collect nodes in the model that between nodes with name of `start` and `end`.
        These two nodes are also included.
        NT)r1   rI   rJ   r5   r   r=   ro   )r)   r   r   rJ   add_noder8   r   r   r   _collect_nodes  s    



z_MinimizerBase._collect_nodesN)r   r   c           
   
   C   s   |  ||}t|}|D ]}|| jkr|| j|  qg }| jjrRdd |D }z | |\}}| ||| W n. tt	fk
r }	 zt
|	 W 5 d}	~	X Y nX dS )a]  
        Run part of the model from `start` node to `end` node. If `start` is None
        then we start from the beginning of the model. If `end` is None then we
        stop at the end of the model.

        Args:
            start: The name of the node which is the first node of the submodule
                we want to run. If set to None, then we'll start with the first
                node of the model.
            end: The name of the node which is the last node of the submodule we
                want to run. If set to None, we'll end with the last node of the
                model.
        c                 S   s   g | ]
}|j qS r   r   r6   r   r   r   r>   3  s     z,_MinimizerBase.run_nodes.<locals>.<listcomp>N)r   rm   rL   r   r4   r$   r   r   r   r   rp   )
r)   r   r   rJ   r   r8   r   r   r   er   r   r   	run_nodes  s     
z_MinimizerBase.run_nodes)r   c                 C   s<   t t|D ]*}|dkr*td||   qt||  qd S )Nr   z . )rangerM   rp   )r)   r   rP   r   r   r   r   >  s    z_MinimizerBase.print_reportc                 C   s   | j D ]}| | qd S rh   )rG   r   )r)   r   r   r   r   print_reportsE  s    
z_MinimizerBase.print_reportsc                 C   s|   t | j t | jj | ||}| jjdkr8| |S | jjdkrN| |S | jjdkrd| |S t	d| jj ddS )a  
        Minimizing the model from node with name `start` to node with name `end` base
        on self.settings. Find culprits that causes FxNetMinimizerRunFuncError or
        FxNetMinimizerResultMismatchError errors.

        Args:
            start: The name of the node where we want to start minimizing. If set
                to None, then we'll start with the first node of the model.
            end: The name of the node where we want to terminate minimizing. If
                set to None, we'll end with the last node of the model.

        Returns:
            nodes: A list of nodes that causes FxNetMinimizerRunFuncError or
                FxNetMinimizerResultMismatchError errors during minimizing.
        r!   binary
accumulatezUnknow traverse method rf   N)
rp   r4   r1   rI   r   r"   r   r   r   rV   )r)   r   r   rJ   r   r   r   rx   I  s    



z_MinimizerBase.minimize)NN)NN)&r   r   r   r   r@   rA   rB   r   r   r   r   r   floatr-   r   rQ   rX   rY   rc   r/   rv   r   r~   r   r   r   intr   r   r   r   r   r   r   r   r   r   rx   r   r   r   r   r0   Y   sV   0	 
4'  =  ?&'"    r0   )&loggingZdataclassesr   typingr   r   r   r   r   r   r@   Ztorch.fxZtorch.fx._compatibilityr	   Ztorch.fx.noder
   Z
shape_propr   Zsplit_utilsr   Ztools_commonr   r   r   r   r   r   r   __all__	getLoggerr   r   	Exceptionr   r   r   r   r0   r   r   r   r   <module>   s.    $
