U
    D‹Ñh5A  ã                   @   sš  d dl mZ d dlmZmZ d dlmZ d dlmZm	Z	m
Z
mZmZmZmZmZ d dlZd dlmZ d dlm  mZ d dlmZ d dlZd dlmZ d dlm  mZ d dlZd dl Z d dl!Z!d dl"m#Z# e$ee$e$f dœd	d
„Z%ee ej&e	e$e
f dœdd„Z'ej&e	e$e
f ejj(dœdd„Z)d0ejj(ejj(dœdd„Z*ej(ej(dœdd„Z+ej(eej& eej& eej& dœdd„Z,ej-ej.ej/ej0ej1ej2ej3ej4ej5ej6ej4ej7ej8gZ9ej:ej;gZ<ej-ej=ej.ej>ej/dd„ iZ?eej& e	e$ej(f dœdd„Z@eej& e	e$ej(f e	ej(ej(f dœd d!„ZAG d"d#„ d#ƒZBd1d&d'„ZCeBeDd(œd)d*„ZEG d+d,„ d,ƒZFdejGfejj(ee	e$e
f  eejG ejj(d-œd.d/„ZHdS )2é    N)ÚArgumentÚTarget)Úfuse_conv_bn_eval)ÚTypeÚDictÚAnyÚTupleÚIterableÚOptionalÚListÚcast)Ú	ShapeProp)Údefaultdict)ÚEnum)ÚtargetÚreturnc                 C   s&   |   dd¡�^ }}|r|d nd|fS )zp
    Splits a qualname into parent path and last atom.
    For example, `foo.bar.baz` -> (`foo.bar`, `baz`)
    Ú.é   r   Ú )Úrsplit)r   ÚparentÚname© r   úP/tmp/user/2202/pip-unpacked-wheel-bf9o3bgk/torch/fx/experimental/optimization.pyÚ_parent_name   s    r   )ÚpatternÚnodeÚmodulesc                 C   s”   t |jƒdkrdS |jd |f}t| |ƒD ]d\}}t|tjƒsD dS |jdkrT dS t|jtƒsf dS |j|krv dS t	||j ƒ|k	r* dS q*dS )Nr   FÚcall_moduleT)
ÚlenÚargsÚzipÚ
isinstanceÚfxÚNodeÚopr   ÚstrÚtype)r   r   r   ÚnodesZexpected_typeZcurrent_noder   r   r   Úmatches_module_pattern   s    

r)   )r   r   Ú
new_modulec                 C   s<   t | jtƒst‚t| jƒ\}}||| j< t|| ||ƒ d S ©N)r"   r   r&   ÚAssertionErrorr   Úsetattr)r   r   r*   Úparent_namer   r   r   r   Úreplace_node_module-   s    
r/   F)Úmodelr   c                 C   s  t jt jft jt jft jt jfg}|s0t | ¡} |rBt	| t
jjƒsNt | ¡}n| }t| ¡ ƒ}t |j¡}|D ]Š}|jD ]~}t|||ƒrxt|jd jƒdkržqx||jd j }	||j }
|
jsÀqxt|	|
ƒ}t|jd ||ƒ | |jd ¡ | |¡ qxqnt ||¡S )z�
    Fuses convolution/BN layers for inference purposes. Will deepcopy your
    model by default, but can modify the model inplace as well.
    r   r   )ÚnnZConv1dZBatchNorm1dÚConv2dÚBatchNorm2dZConv3dZBatchNorm3dÚcopyÚdeepcopyr"   Útorchr#   ÚGraphModuleÚsymbolic_traceÚdictÚnamed_modulesÚgraphr(   r)   r   r    Úusersr   Ztrack_running_statsr   r/   Úreplace_all_uses_withÚ
erase_node)r0   ZinplaceZno_traceÚpatternsÚfx_modelr   Ú	new_graphr   r   ÚconvZbnZ
fused_convr   r   r   Úfuse3   s2    


þ



rC   c                 C   s*   t  | ¡}G dd„ dtj jƒ}||ƒ ¡ S )z5
    Removes all dropout layers from the module.
    c                       s8   e Zd Zeeedf eeef edœ‡ fdd„Z	‡  Z
S )z&remove_dropout.<locals>.DropoutRemover.)r   r    Úkwargsr   c                    s>   t | j| tjƒr*t|ƒdks"t‚|d S tƒ  |||¡S d S )Nr   r   )r"   Z
submodulesr1   ZDropoutr   r,   Úsuperr   )Úselfr   r    rD   ©Ú	__class__r   r   r   Z   s    z2remove_dropout.<locals>.DropoutRemover.call_module)Ú__name__Ú
__module__Ú__qualname__r   r   r   r   r&   r   r   Ú__classcell__r   r   rG   r   ÚDropoutRemoverY   s   rM   )r#   r8   r6   ZTransformerZ	transform)r0   r@   rM   r   r   r   Úremove_dropoutS   s    
rN   )Úorig_moduler(   ÚinputsÚoutputsc                    s|   t  ¡ }i ‰ |D ]}| |j¡}|ˆ |< q|D ] }| |‡ fdd„¡}|ˆ |< q.| ‡ fdd„|D ƒ¡ | ¡  t  | |¡S )z�
    Given lists of nodes from an existing graph that represent a subgraph, returns a submodule that executes that subgraph.
    c                    s   ˆ |  S r+   r   )Úx©Úenvr   r   Ú<lambda>l   ó    z"extract_subgraph.<locals>.<lambda>c                    s   g | ]}ˆ | ‘qS r   r   )Ú.0ÚoutputrS   r   r   Ú
<listcomp>n   s     z$extract_subgraph.<locals>.<listcomp>)r#   ÚGraphÚplaceholderr   Z	node_copyrX   Úlintr7   )rO   r(   rP   rQ   rA   ÚinputZnew_noder   r   rS   r   Úextract_subgraphb   s    

r^   c                 C   s
   t  | ¡S r+   )Ú	th_mkldnnZMkldnnBatchNorm)ÚaÚ_r   r   r   rU      rV   rU   )r(   r   c                 C   s€   i }| D ]r}|j dkrt|jtƒs&t‚||j }t|ƒtkrtt|ƒ |tjƒ}t|t	j
ƒs`t‚t |¡||< t|||ƒ q|S )zÈ
    For each node, if it's a module that can be preconverted into MKLDNN,
    then we do so and create a mapping to allow us to convert from the MKLDNN
    version of the module to the original.
    r   )r%   r"   r   r&   r,   r'   Ú
mkldnn_mapr6   Úfloatr1   ÚModuler4   r5   r/   )r(   r   Úold_modulesr   Ú
cur_moduler*   r   r   r   Úmodules_to_mkldnnƒ   s    

rg   )r(   r   re   c                 C   sJ   | D ]@}|j dkrt|jtƒs"t‚||j }||krt|||| ƒ qdS )za
    Maps each module that's been changed with `modules_to_mkldnn` back to its
    original.
    r   N)r%   r"   r   r&   r,   r/   )r(   r   re   r   rf   r   r   r   Úreset_modules•   s    

rh   c                   @   s   e Zd Zejdœdd„ZdS )ÚMklSubgraph©Úfx_graphc                 C   s   || _ g | _g | _g | _d S r+   )rk   r(   Ústart_nodesÚ	end_nodes)rF   rk   r   r   r   Ú__init__¢   s    zMklSubgraph.__init__N)rI   rJ   rK   r#   rZ   rn   r   r   r   r   ri   ¡   s   ri   é
   r   c                    s(   d‰d‰t tdœ‡ ‡‡‡‡fdd„}|S )aW  
    This generates a heuristic that can be passed into `optimize_for_inference` that
    determines whether a subgraph should be run in MKL by running it with the example_inputs.

    Example usage:
        heuristic = gen_mkl_autotuner(example_inputs, iters=10)
        fast_model = optimization.optimize_for_inference(model, heuristic)
    N©r;   r   c                    s¸   | j }ˆd kr,| jj‰| jj‰tˆƒ ˆ¡ dd„ |D ƒ‰ tttj	 dd„ | j
D ƒƒ}tˆ| j||ƒ‰‡‡fdd„}|‡ ‡fdd„ƒ}tˆjjtˆ ¡ ƒˆƒ |‡ ‡fdd„ƒ}||k S )	Nc                 S   s   g | ]}t  |j¡‘qS r   )r6   ZrandnÚshape©rW   r   r   r   r   rY   »   s     z@gen_mkl_autotuner.<locals>.use_mkl_heuristic.<locals>.<listcomp>c                 S   s   g | ]}|j d  ‘qS )r   )r    rr   r   r   r   rY   ¼   s     c                    s<   t ˆƒD ]
}| ƒ  qt ¡ }t ˆ ƒD ]
}| ƒ }q$t ¡ | S r+   )ÚrangeÚtime)Úfra   ÚbeginÚout)ÚitersÚwarmupr   r   Ú	benchmark¿   s    z?gen_mkl_autotuner.<locals>.use_mkl_heuristic.<locals>.benchmarkc                      s   dd„ ˆdd„ ˆ D ƒŽ D ƒS )Nc                 S   s   g | ]}|  ¡ ‘qS r   )Úto_dense©rW   Úir   r   r   rY   Ç   s     zRgen_mkl_autotuner.<locals>.use_mkl_heuristic.<locals>.<lambda>.<locals>.<listcomp>c                 S   s   g | ]}|  ¡ ‘qS r   )Ú	to_mkldnnr|   r   r   r   rY   Ç   s     r   r   ©Zsample_inputsÚ	submoduler   r   rU   Ç   rV   z>gen_mkl_autotuner.<locals>.use_mkl_heuristic.<locals>.<lambda>c                      s   ˆˆ Ž S r+   r   r   r   r   r   rU   Ê   rV   )rl   rk   Zowning_modulere   r   Ú	propagater   r   r#   r$   rm   r^   r(   rh   r;   r9   r:   )r;   Zinput_nodesZoutput_argsrz   Zmkl_timeZno_mkl_time©Úexample_inputsr@   rx   re   ry   r   r   Úuse_mkl_heuristic´   s    z,gen_mkl_autotuner.<locals>.use_mkl_heuristic)ri   Úbool)rƒ   rx   ry   r„   r   r‚   r   Úgen_mkl_autotuner¨   s    	r†   rp   c                 C   s   t | jƒdkS )z¿
    This is a heuristic that can be passed into `optimize_for_inference` that
    determines whether a subgraph should be run in MKL by checking if there
    are more than 2 nodes in it
    é   )r   r(   )r;   r   r   r   Úuse_mkl_lengthÎ   s    rˆ   c                   @   sB   e Zd Zdd„ Zedœdd„Zeedœdd„Zeed	œd
d„ZdS )Ú	UnionFindc                 C   s   d g| | _ dg| | _d S )Nr   ©r   Úsize)rF   Únr   r   r   rn   ×   s    zUnionFind.__init__)Úvc                 C   s   || j |< d| j|< d S )Nr   rŠ   )rF   r�   r   r   r   Úmake_setÛ   s    
zUnionFind.make_set)r�   r   c                 C   sB   | j | }||kr|S |d k	s"t‚|  |¡| j |< tt| j | ƒS r+   )r   r,   Úfindr   Úint)rF   r�   Úparr   r   r   r�   ß   s    
zUnionFind.find)r`   Úbc                 C   sf   |   |¡|   |¡ }}||kr"|S | j| | j| k r@|| }}|| j|< | j|  | j| 7  < d S r+   )r�   r‹   r   )rF   r`   r’   r   r   r   Újoinç   s    

zUnionFind.joinN)rI   rJ   rK   rn   r�   rŽ   r�   r“   r   r   r   r   r‰   Ö   s   r‰   )r0   Úpass_configÚtracerr   c              
      s&  dddt idœ}|dkri }| |¡ |d r6t| ƒ} |d rFt| ƒ} |d dkrV| S t|d tƒsltd	ƒ‚d|d kr€td
ƒ‚|d d }|ƒ }| t 	| ¡¡‰ t
 |jˆ ¡}t|  ¡ ƒ}G dd„ dtƒ}tˆ jƒD �]V}	|j}
|	jdk�rV||	j }t|ƒtk�rˆ|j}
t| ¡ dƒ}|dk	�rˆ|jtjk�s:tdƒ‚|jt d¡k�sˆtdƒ‚n2|	jdk�rˆ|	jtk�rv|j}
n|	jtk�rˆ|j}
|
|jkrÖ|
|jk�r¶t dd„ |	j!D ƒƒ�s¶qÖˆ  "|	¡� t
 #|	j!‡ fdd„¡}W 5 Q R X t$t%t
j&j' |ƒ|	_!ˆ  (|	¡�( ˆ  )dd|	f¡}|	 *|¡ |	f|_!W 5 Q R X qÖt+tˆ jƒ|ƒ}|ˆ _,ˆ jD ]„}	|	jdk�rL|	jdk�rL|	j!d }t|	j-ƒ}|D ]2}|jdk�r€|jdk�r€| *|¡ ˆ  .|¡ �q€t/|	j-ƒdk�rLˆ  .|	¡ �qLt/ˆ jƒ}t0|ƒ‰‡fdd„‰t1ˆ jƒD ]ê\}}	|	jdk�r,|	jdk�r,||	_2ˆ 3|¡ n¶|	jdk�rn|	jdk�rnˆ|	j!d ƒdk	�s\t‚ˆ|	j!d ƒ|	_4nt‡fdd„|	j5D ƒ}t/|ƒdk�r”�qút dd„ |D ƒƒ�r¬t‚t6|ƒ}|d |	_7|dd… D ]}ˆ 8|d |¡ �qÊ�qút9‡ fd d„ƒ}ˆ jD ]r}	t:|	d!ƒ�r$|ˆ ;|	j7¡ j <|	¡ t:|	d"ƒ�rH|ˆ ;|	j2¡ j= <|	¡ t:|	d#ƒ�rü|ˆ ;|	j4¡ j> <|	¡ �qü| ?¡ D ]P}||ƒ�sx|j=|j> D ]$}	|	j!d }|	 *|¡ ˆ  .|	¡ �q’t@|j||ƒ �qxd}ˆ jD ]&}	|	jdk�sð|	jdk�rÔ|d7 }�qÔtA BtC¡ Dd$|¡ ˆ  E¡  t
 | ˆ ¡}|S )%a  
    Performs a set of optimization passes to optimize a model for the
    purposes of inference. Specifically, the passes that are run are:
    1. Conv/BN fusion
    2. Dropout removal
    3. MKL layout optimizations

    The third optimization takes a function `use_mkl_heuristic` that's used
    to determine whether a subgraph should be explicitly run in MKL layout.

    Note: As FX does not currently handle aliasing, this pass currently
    assumes nothing aliases. If that isn't true, use at your own risk.
    TÚ	heuristic)Úconv_bn_fuserN   Úmkldnn_layout_optimizeNr—   rN   r˜   Fz+mkldnn_layout_optimize config is not a dictz4Heuristic not found in mkldnn_layout_optimize configc                   @   s   e Zd ZdZdZdZdS )z*optimize_for_inference.<locals>.MklSupportr   r‡   é   N)rI   rJ   rK   ÚNOÚYESÚUNKNOWNr   r   r   r   Ú
MklSupport  s   r�   r   z)this pass is only for torch.float modulesÚcpuz!this pass is only for CPU modulesÚcall_functionc                 s   s   | ]}|j d kV  qdS )r{   N)r   )rW   Úargr   r   r   Ú	<genexpr>7  s     z)optimize_for_inference.<locals>.<genexpr>c                    s   ˆ   d| f¡S )Nr~   )Úcall_method©rŒ   rj   r   r   rU   :  rV   z(optimize_for_inference.<locals>.<lambda>r¢   r{   r   r~   c                    s0   t | dƒrˆ  | j¡S t | dƒr,ˆ  | j¡S d S )NÚcolorÚstart_color)Úhasattrr�   r¤   r¥   r£   )Úufr   r   Ú	get_colorW  s
    

z)optimize_for_inference.<locals>.get_colorc                    s,   g | ]$}t |tjƒrˆ |ƒd k	rˆ |ƒ‘qS r+   )r"   r#   r$   r|   )r¨   r   r   rY   r  s       z*optimize_for_inference.<locals>.<listcomp>c                 s   s   | ]}|d kV  qd S r+   r   r|   r   r   r   r¡   v  s     r   c                      s   t ˆ ƒS r+   )ri   r   rj   r   r   rU   }  rV   r¤   r¥   Ú	end_colorzmkldnn conversions: %s)Frˆ   ÚupdaterC   rN   r"   r9   ÚRuntimeErrorÚtracer4   r5   r#   r7   Úrootr:   r   Úlistr(   rš   r%   r   r'   Úmkldnn_supportedr›   ÚnextÚ
parametersZdtyper6   rc   r,   ZdeviceÚmkldnn_supported_unknownrœ   Úanyr    Zinserting_beforeZmap_argr   r   r   r   Zinserting_afterZcreate_noder=   rg   re   r<   r>   r   r‰   Ú	enumerater¥   rŽ   r©   Zall_input_nodesÚsortedr¤   r“   r   r¦   r�   Úappendrl   rm   Úvaluesrh   ÚloggingÚ	getLoggerrI   Úinfor\   )r0   r”   r•   Zdefault_pass_configr„   Z
cur_tracerr@   r   r�   r   Zsupports_mkldnnrf   Zsample_parameterZmkldnn_argsZdense_xre   Zprv_noder<   ÚuserZ	num_nodesZcur_idxZ
cur_colorsZother_colorZmkldnn_graphsr;   ZprvZmkldnn_conversionsÚresultr   )rk   r¨   r§   r   Úoptimize_for_inferenceð   sÌ    ý
	


 











r½   )FF)ro   r   )IZtorch.fxr#   Ztorch.fx.noder   r   Ztorch.nn.utils.fusionr   Útypingr   r   r   r   r	   r
   r   r   r6   Ztorch.nnr1   Ztorch.nn.functionalZ
functionalÚFZtorch.fx.passes.shape_propr   r4   Úcollectionsr   Ztorch.utils.mkldnnÚutilsZmkldnnr_   Úoperatorrt   r¸   Úenumr   r&   r   r$   r)   rd   r/   rC   rN   r^   r2   ZLinearr3   ZReLUZ	MaxPool2dZ	AvgPool2dZAdaptiveAvgPool2dZreluZ	transposeZsigmoidZ
avg_pool2dZadaptive_avg_pool2dr¯   ÚaddÚmulr²   ZMkldnnConv2dZMkldnnLinearrb   rg   rh   ri   r†   r…   rˆ   r‰   ZTracerr½   r   r   r   r   Ú<module>   sp   (	   (          ý	   ý .
&ýü