
    sg~                        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 g d
Z e j:                  e      Z ed       G d de              Z! ed       G d de              Z" ed       G d de              Z#e G d d             Z$ G d d      Z%y)    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)is_backward_compatiblec                       e Zd ZdZy)r   z9
    Raised if failed to split out a minimize module
    N__name__
__module____qualname____doc__     O/var/www/html/venv/lib/python3.12/site-packages/torch/fx/passes/net_min_base.pyr   r   !       r!   r   c                       e Zd ZdZy)r   z@
    Raised if error occurs during run_a or run_b functions
    Nr   r    r!   r"   r   r   )   r#   r!   r   c                       e Zd ZdZy)r   zJ
    Raised if comparing function thinks the results are mismatching.
    Nr   r    r!   r"   r   r   1   r#   r!   r   c                   P    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 Zy	)
_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                 d    d}t        |       j                         D ]  \  }}|d| d| dz  } |S )NzFX Minimizer Settings:
	: 
)varsitems)selfsettings_strkvs       r"   __str__z_MinimizerSettingBase.__str__O   sG    1J$$& 	,DAqb2aSO+L	, r!   N)r   r   r   r   r(   bool__annotations__r*   strr+   r,   r7   r    r!   r"   r'   r'   9   s8     #d"'OS'Hd %%r!   r'   c                      e Zd ZdZ	 	 d5dej
                  j                  dedee	e	e
geeef   f   dedeeeej
                  j                  egdf      deeeeegdf      fd	Zd6d
ej
                  j                  dedede	fdZd6d
ej
                  j                  dedede	fdZde	de	dej
                  j                  fdZdej
                  j                  dedeeef   fdZdefdZdedeej
                  j                  ef   fdZ	 d6dej
                  j                  dede
defdZded ed!edefd"Zdedefd#Zdedefd$Zded ed!ed%edef
d&Zded%ee   defd'Z dedefd(Z!dedefd)Z"ded ed!edefd*Z#ded+e$defd,Z%d-ee   d.ee   defd/Z&d5d-ee   d.ee   fd0Z'd1e$e   fd2Z(d3 Z)	 	 	 	 d7d-ee   d.ee   d+ee$   d%ee   def
d4Z*y)8_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.
    Nmodulesample_input
compare_fnsettingsmodule_exporterexclusion_fnc                 X   t        |t        j                  j                        sJ || _        || _        || _        || _        || _        || _	        i | _
        i | _        i | _        g | _        d| _        | j                  j                  j                   D ch c]  }|j"                  t$        v s| }} t'        | j                        j(                  | j
                     t+        | j                  |             | _        | j                  j                  j                   D cg c]  }|j"                  dk(  s|j.                    }	}t1        |	      t1        | j
                        k(  sJ t3        |	      D ])  \  }
}||
   | j                  |<   ||
   | j                  |<   + y c c}w c c}w )Nr   placeholder)
isinstancetorchfxGraphModuler=   r>   r?   rA   r@   rB   	a_outputs	b_outputsresultsreports	iterationgraphnodesopr   r   	propagater   fusionsnamelen	enumerate)r3   r=   r>   r?   r@   rA   rB   nodecallable_nodesplaceholdersirS   s               r"   __init__z_MinimizerBase.__init__g   s   $ &%(("6"6777($. ( *, *, (* )+   "[[..44
CT8TD
 
 	)	$++(($*;*;<I,T[[.IK #'++"3"3"9"9
TWW=UDII
 
 < C(9(9$:::: !. 	3GAt#/?DNN4 #/?DNN4 	3

s   F".F""F'7F'modinputs
report_idxreturnc                     t        d      )zz
        Run `mod` with `inputs` and generate output. The output will be compared with
        output of run_b().
        zrun_a() is not implemented.RuntimeErrorr3   r[   r\   r]   s       r"   run_az_MinimizerBase.run_a       
 899r!   c                     t        d      )zz
        Run `mod` with `inputs` and generate output. The output will be compared with
        output of run_a().
        zrun_b() is not implemented.r`   rb   s       r"   run_bz_MinimizerBase.run_b   rd   r!   a_resultb_result	submodulec                    t        d |j                  j                  D              }t        |j                  d   t
        j                  j                        rM|| j                  |j                  d   j                  <   || j                  |j                  d   j                  <   yt        |j                  d         D ]=  \  }}||   | j                  |j                  <   ||   | j                  |j                  <   ? y)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              3   @   K   | ]  }|j                   d k(  s|  yw)outputN)rP   ).0rV   s     r"   	<genexpr>z0_MinimizerBase._store_outputs.<locals>.<genexpr>   s       
dgg6ID
s   r   N)nextrN   rO   rE   argsrF   rG   NoderI   rS   rJ   rU   )r3   rg   rh   ri   output_noderY   args          r"   _store_outputsz_MinimizerBase._store_outputs   s       
&__22
 

 k&&q)588==97?DNN;++A.3347?DNN;++A.334 $K$4$4Q$78 73+3A;sxx(+3A;sxx(7r!   main_modulesubmod_pathc                   
 g 
g }t        ||      }|j                  j                  D cg c]  }|j                  dk(  s|j                    }}t        |      | j                  j                         k  rD|D ]>  }
j                  | j                  |          |j                  | j                  |          @ n}| j                  j                  rt        d| d       dt        j                  j                  dt         f
fd}|j#                  |      }	 || j$                    |	j'                          
}| j                  j                  s

fS 
|fS c c}w )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.
        rD   z)Can't find previous stored outputs named !r3   r\   c                 
    |y Nr    )r3   r\   a_inputs     r"   
get_inputsz5_MinimizerBase._get_submod_inputs.<locals>.get_inputs   s     r!   )getattrrN   rO   rP   rS   setrI   keysappendrJ   r@   r(   printrF   nnModuler   register_forward_pre_hookr>   remove)r3   ru   rv   b_inputri   rV   rX   rS   r|   handler{   s             @r"   _get_submod_inputsz!_MinimizerBase._get_submod_inputs   s5   $ K5	"+//"7"7
477m;SDII
 
 | 3 3 55$ 5t~~d34t~~d345 }}--A,qQR! !# !
 88DF**+MMOG}}--G##;
s
   EEselected_nodesc                     | j                   j                  j                  D ]L  }|j                  t        vr||v rd|_        "t        d |j                  D              rd|_        Fd|_        N y)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              3   \   K   | ]$  }|j                   t        v r|j                  d v  & yw)>   main_1r   N)rP   r   tag)rm   ns     r"   rn   z,_MinimizerBase._tag_nodes.<locals>.<genexpr>  s/      44,, //s   *,r   main_0N)r=   rN   rO   rP   r   r   anyall_input_nodes)r3   r   rV   s      r"   
_tag_nodesz_MinimizerBase._tag_nodes  sm     KK%%++ 	$Dww//~%% -- 
 $#	$r!   rO   c                     | j                  |       t        | j                  g d      }d}|j                         D ]  \  }}d|vr|dk(  r|}t	        d|        |dk(  rt	        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   r   r    r   z0Expected only one minimize submodule with nodes z,Minimize submodule was not found with nodes )r   r   r=   named_childrenr   )r3   rO   split_modulesubmodule_name
child_name_s         r"   _build_submodulez_MinimizerBase._build_submodule  s     	 %T[[2RS !)88: 
	MJ+#!+2FugN 
	 R.>ugF  ^++r!   r   submod_nameoutput_namesc                    t        ||      }| j                  ||      \  }}t        | j                        dk(  r"| j                  j	                  g        d| _        | j                  |dk\  r|n| j
                  dz
     }|j	                  d       |rg }	|j                  j                  D ]L  }
|
j                  dk(  r|j                  j                  |
       |
j                  |v s<|	j	                  |
       N |j                  j                  t        |	      dk(  r|	d   n
t        |	             |j                  j                          |j                          |j                  j                  D ])  }
|
j                  dk(  st        |
j                   d       }+ 	 | j#                  |||      }| j%                  |||      }| j'                  |||       |}|D cg c]  }t-        |       }}| j/                  |||      \  }}|| j0                  <   |j	                  d	|        |sr|j	                  d
|        | j2                  rD| j3                  ||t-        |d         dz          | j3                  ||t-        |d         dz          t5        d
|       y# t(        $ r-}|j	                  d| d|        t+        d| d|       d}~ww xY wc c}w )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 ...rl   c                     | j                   S rz   )rS   )xs    r"   <lambda>z1_MinimizerBase._run_and_compare.<locals>.<lambda>n  s
    !&& r!   zException raised when running r/   NzNumerical accuracy = zResult mismatch for _cpu_acc)r}   r   rT   rL   r   rM   rN   rO   rP   
erase_noderS   rl   tuplelint	recompiler   rp   rc   rf   rt   	Exceptionr   r:   r?   rK   rA   r   )r3   r   r   r   r]   ri   r{   r   reportoutput_nodesrV   
result_keyrg   rh   enamesr6   numeric_resultbool_results                      r"   _run_and_comparez_MinimizerBase._run_and_compareA  s   " L+6	22<Mt||!LL#DNJ!OjRSAST+,%'L!-- .77h&OO..t499, ''-. OO""#&|#4#9Qu\?R OO  "! OO)) 	BDww("$TYY0@A
	B	zz)WjAHzz)WjAH(I> $%/0SV0E0&*ooh%&P##1Z -n-=>?MM0=>##$$YJqM(:V(C $$YJqM(:V(C 46J:,4WXX   	MM:;-r!MN,0RsC 	 1s   9J  K 	K)(KK	all_nodes	start_idxend_idxc                    t               }||| }g }| j                  A| j                  |||       t        |      dk(  r dg}| j                  j	                  |       |S |d   j
                  }|d   j
                  }| xj                  dz  c_        | j                  j	                  |       |j	                  d| j                          |j	                  d| d| d|dz
   d| d	t        |       
       t        |      }		 | j                  |	      \  }
}| j                  |
||g       |j	                  d
       | j                  |       t               S # t        t        f$ r t        |      dk(  r*|j	                  d|	 d       | j                  |       |	cY S |j	                  d       | j                  |       t        |      dz  }| j                  ||||z         }t        |      dk7  r| j                  j                  s|cY S | j                  |||z   |      }t        |      dk(  r%|j	                  d|        | j                  |       |cY S w xY w)z9
        Recursive binary search implementation.
        r   All nodes are excluded by userr   zBinary search iteration From node index : to &. Size of the interested node list is No discrepancy found.zcThis is the last node in the sub-module. Search in the current branch is successful with culprit = .MProceed to split and lower the halves of the current sub-module individually.   zaFurther split and lowering found no errors. Unable to minimize the submodule with list of nodes: )r~   rB   rT   rL   r   rS   rM   r   r   print_reportr   r   _binary_search_implr@   r+   )r3   r   r   r   culpritsrO   r   first_node_nameoutput_node_name	cur_nodesr   r   mids                r"   r   z"_MinimizerBase._binary_search_impl  sm     E#Ig6(eY85zQ:;##F+(-- 9>>!F#00@AByk?*;4	{!L\K] ^336u:,@	
 !Z	'	(,(=(=i(H%L+!!,>N=OPF MM12f%5LG +,MN 	5zQQQZP[[\^ !!&)  MM+ f%e*/C//	9iRUoVH8}!$--*@*@//	9s?GTH8}!LLQ7T !!&)O?	s!   (E AIA,IAIIc                 :    | j                  |dt        |            S )z7
        Binary search on `nodes` for culprit.
        r   )r   rT   )r3   rO   s     r"   _binary_traversez_MinimizerBase._binary_traverse  s     ''q#e*==r!   c                    t               }|D ]q  }g }| j                  j                  |       | xj                  dz  c_        |j                  d| j                   d       |j                  d|j                          t
        j                  d|j                         |g}| j                  l| j                  |dd       t        |      dk(  rK|j                  d|j                          | j                  |       | j                  j                  s|c S |h}|| j                  v r| j                  |   }	 | j                  |      \  }}| j                  |||j                  g       | j                  |       t |S # t        $ rV |j!                  |       |j                  d	|        | j                  |       | j                  j                  s|cY c S Y t"        $ rV |j%                  |       |j                  d
|        | j                  |       | j                  j                  s|cY c S Y 2w xY w)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Visit node: %sr   r   zUser exclusion : "Found culprit from numeric error: Found culprit from run error: )r~   rL   r   rM   rS   _LOGGERinforB   rT   r   r@   r+   rR   r   r   r   addr   update)	r3   rO   r   rV   r   	node_listr   r   r   s	            r"   _sequential_traversez#_MinimizerBase._sequential_traverse  s     E '	$D "FLL'NNaNMM:4>>:J!LMMML45LL)4995#'&I  ,!!)R4y>Q&MM$5dii["AB%%f-==11' "&It||# LL.	$,0,A,A),L)k%%lK$))M!!&)7'	$R  6 $T" B4&IJ!!&)}}--#O .. $	* >tfEF!!&)}}--#O .	$s   <AFAI "AI ?I find_last_nodec                    g }||z   dz  }|r|d|dz    n||d }| j                   r| j                  |dd       t        |      }|d   j                  }	|d   j                  }
|r|
n|	}| xj                  dz  c_        | j                  j                  |       |j                  dd| j                   g       |j                  d|rd	nd
 dd| d||   j                   d| d||   j                   d	d|	 d|
 d| d||z
  dz    g       t        | j                        dz
  }	 | j                  |      \  }}| j                  |||
g|       |j                  d| d||   j                   d       ||k(  rD|j                  dd| d||   j                   dg       | j                  |       |r|dz   S |dz
  S |j                  d       | j                  |       |r| j                  ||dz   ||      S | j                  ||||      S # t        t        f$ r |j                  d|	 d|
 d       ||k(  r<|j                  dd| d||   j                   dg       | j                  |       |cY S |j                  d       | j                  |       |r| j                  ||||      cY S | j                  ||dz   ||      cY S w xY w)z
        Recursive block search implementation.
        find_last_node: If True, search for the last node which result in numerics difference
        if False: find first node in sorted node list
        r   Nr   r   r   z==============================zBlock search iteration zSearch for lastfirstz node in culpritsr   r   r   z. zSubgraph constructed by zTargeting node: z$Size of the interested node list is z&Culprits not found from node start to r   z)This is the last node in the sub-module. z4Search in the current branch is successful with nodez, node name: r   zCulprits found from node z6Search in the current branch is successful with node :)rB   r~   rS   rM   rL   r   extendrT   r   r   r   _block_traverse_implr   r   )r3   rO   r   r   r   r   r   cur_nodes_listr   r   last_node_nametarget_node_namer]   r   r   s                  r"   r   z#_MinimizerBase._block_traverse_impl  s\    7"q(6D5#'?%PSPT+nb"5'	(+00'+00-;>!F#)$..)9:	
 	fGDDUV"9+QuY/?/D/D.ET'RSTYZaTbTgTgShhjk*?*;4?OP"#3"456w7JQ7N6OP	
 &*
2	X(,(=(=i(H%L+!!,n=MzZ4 MMB3%qsHYYZ[\CCN$+]53C3H3H2IK !!&)(6y1}IIMIMM+ f%00a.YY00	3WW_ 23MN 	ZMM5o5Fd>JZZ[\]CCP$+]53C3H3H2IK !!&)  MM+ f%00	3WW00a.YY/	Zs   ()H A&K
68K
0K
	K
c                    t               }|d   j                  }|d   j                  }d| d| g}|j                  d       | j                  j                  |       d}t	        |      dz
  }|dnd}	|	s|rm|j                  d	       | j                  |       | j                  |||d      }|j                  d
d| d||   j                   g       | j                  |       |	s|sdg}
| j                  |
       | j                  |d|dz    ||d      }|
j                  d       | j                  j                  |
       |
j                  dd| d||   j                   g       | j                  |
       |j                  |||dz           d||    d||    dg}| j                  j                  |       | j                  |       |S )a  
        Traverse topologically sorted node list
        Find minimium block (start_idx, end_idx) which contains the culprit
        1st pass: search for end_idx by finding the last node in culprit block
        where Numerical accuracy (0, end_idx) > threshold
        2nd pass: search for start_idx by finding the first node in culprit block
        where Numerical accuracy (start_idx, end_idx) < threshold
        Form minimum block by (start_idx - 1, end_idx)
        r   r   zBlock search from r   z2**************************************************r   TFz(Start searching for last node in culpritzFinish Pass 1zFind end_idx = r   z)Start searching for first node in culpritzFinish Pass 2zFind start_idx = z'Finish searching, found minimum block (,))	r~   rS   r   rL   rT   r   r   r   r   )r3   rO   r   r   r   r   last_node_reportr   r   run_bothfirst_node_reportresult_reports               r"   _block_traversez_MinimizerBase._block_traverseg  s     E(--r00AnEUVW),-	e*q.)14u ~##$NO.///y'4PG###%gYag0C0C/DE ./ >!L M/011%'A+2F	SZ\abI$$X.LL 12$$#'	{!E)4D4I4I3JK /0 	i!45B5CSBTTUV[\cVdUeefghM*-(r!   c                    t               }| j                  | j                  |dd       t        |      dk(  r dg}| j                  j	                  |       |S |d   j
                  }|d   j
                  }d| d| g}t        |      }	 | j                  |      \  }}| j                  |||g       | j                  |       |S # t        t        f$ r* |j	                  d|        | j                  |       |cY S w xY w)zL
        run user defined `nodes` and determine if it is a culprit.
        r   r   r   zDefined graph from r   Found culprit )r~   rB   rT   rL   r   rS   r   r   r   r   r   )	r3   rO   r   r   r   r   r   r   r   s	            r"   _defined_traversez _MinimizerBase._defined_traverse  s     E(eR,u:?67FLL'O(-- 9>>''8=M<NOP Z		(,(=(=i(H%L+!!,>N=OPf%  23MN 	MMN9+67f%O	s   9C 6C?>C?c                    t               }t               }| j                  j                  rt        d       |S |D ]  }g }| j                  j                  |       | xj                  dz  c_        |j                  d| j                   d       |j                  |       |j                  }|t        |t              r|d   }|t        |t              s
J d|        |j                  d|        	 | j                  |      \  }}| j                  |||g       | j                  |        |S # t        t         f$ r= |j                  |       |j                  d|        | j                  |       |cY c S w xY w)	Nz9'Find All' mode is not supported in accumulate traversal.r   zAccumulate traverse iteration r   r   zminimize: node_name: z
Add node: r   )r~   r@   r+   r   rL   r   rM   r   rS   rE   r   r:   r   r   r   r   r   )	r3   rO   r   nodes_to_runrV   r   	node_namer   r   s	            r"   _accumulate_traversez#_MinimizerBase._accumulate_traverse  sw   E # ==!!MNO 	 D "FLL'NNaNMM:4>>:J!LMT"		I$Iu)E%aL	(Z3. 3&yk23  MMJyk23 ,0,A,A,,O)k%%lK)M!!&))	 6  67QR  T"tf56!!&)	 s   39D00AE<;E<c           
         t               }||| }t        |      }| j                  | j                  |||       t        |      }n4|D ]/  }|| j                  v s|j                  | j                  |          1 g }| j                  j                  |       | xj                  dz  c_        |j                  d| j                   d       |j                  d| d|dz
   dt        |              	 | j                  |      \  }	}
| j                  |	|
g        |j                  d       | j                  |       t               S # t        $ r; |j                  |       |j                  d|        | j                  |       |cY S t        $ r; |j                  |       |j                  d	|        | j                  |       |cY S w xY w)
?
        Skip certain nodes in graph based on settings
        r   z Nodes block r   r   r   r   r   r   r   )r~   rB   rR   r   rL   r   rM   rT   r   r   r   r   r   )r3   r   r   r   r   rO   r   rV   r   r   r   s              r"   _skip_traverse_implz"_MinimizerBase._skip_traverse_impl  s     E#Ig6 Z	(eY8E
I 94<<'$$T\\$%789 F#!dnn%5Q78ykgai[ 9336u:,@	

	(,(=(=i(H%L+!!,R@ MM12f%5L 2 	OOI&MM>ykJKf%O* 	OOI&MM:9+FGf%O		s   2'E AGA G
G
skip_nodesc                     d}t        |      }d}t               }||k  r_||   }|j                  |v r||kD  r| j                  |||      }|dz   }n#||dz
  k(  r||k  r| j                  |||dz         }|dz  }||k  r_|S )r   r   r   )rT   r~   rS   r   )r3   r   r   r   	num_nodesidxr   rV   s           r"   _skip_traversez_MinimizerBase._skip_traverse  s     		N	5IoS>D		Z'?#77	9cRH!G		A%)s*:33Iy#PQ'R1HC Io r!   startendc                     g }|du }| j                   j                  j                  D ]K  }|j                  t        vr|j
                  |k(  rd}|r|j                  |       |j
                  |k(  sJ |S  |S )z
        Collect nodes in the model that between nodes with name of `start` and `end`.
        These two nodes are also included.
        NT)r=   rN   rO   rP   r   rS   r   )r3   r   r   rO   add_noderV   s         r"   _collect_nodesz_MinimizerBase._collect_nodes"  s|    
 D=KK%%++ 	Dww//yyE!T"yyC	 r!   c                    | j                  ||      }t        |      }|D ]/  }|| j                  v s|j                  | j                  |          1 g }| j                  j
                  r|D cg c]  }|j                   }}	 | j                  |      \  }}| j                  |||       yc c}w # t        t        f$ r}	t        |	       Y d}	~	yd}	~	ww xY w)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.
        N)r   r~   rR   r   r@   r,   rS   r   r   r   r   r   )
r3   r   r   rO   r   rV   r   r   r   r   s
             r"   	run_nodesz_MinimizerBase.run_nodes9  s     ##E3/J	 	5Dt||#  d!34	5 ==,,278$DII8L8	(,(=(=i(H%L+!!,\J	 9 '-
 	 !HH		s   .B,'B1 1C CCr   c                     t        t        |            D ]'  }|dkD  rt        d||   z          t        ||          ) y )Nr   z . )rangerT   r   )r3   r   rY   s      r"   r   z_MinimizerBase.print_report[  s>    s6{# 	!A1uefQi'(fQi 		!r!   c                 H    | j                   D ]  }| j                  |        y rz   )rL   r   )r3   r   s     r"   print_reportsz_MinimizerBase.print_reportsb  s#    ll 	&Ff%	&r!   c                    t        | j                         t        | j                  j                         | j	                  ||      }| j                  j
                  dk(  r| j                  |      S | j                  j
                  dk(  r| j                  |      S | j                  j
                  dk(  r| j                  |      S | j                  j
                  dk(  r|t        d      | j                  ||      S | j                  j
                  dk(  r| j                  |      S | j                  j
                  dk(  r| j                  ||      S t        d| j                  j
                   d	      )
az  
        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.
            skip_nodes: The names of nodes where we want to skip during minimizing.
                It'll create subgraphs without these skip nodes under the hood.
                Only applicable in mode "skip".
            find_last_node: True if only last_node of a culprits is needed in mode "block".
                False if only the first_node of a culprits is needed.
                Only applicable in mode "block".

        Returns:
            nodes: A list of nodes that causes FxNetMinimizerRunFuncError or
                FxNetMinimizerResultMismatchError errors during minimizing.
        r)   binary
accumulateskipz<'skip_nodes' can't be None when 'traverse_method' is 'skip'.definedblockzUnknown traverse method rx   )r   r@   r=   rN   r   r*   r   r   r   ra   r   r   r   )r3   r   r   r   r   rO   s         r"   r   z_MinimizerBase.minimizef  s=   : 	dmmdkk ##E3/==((L8,,U33==((H4((//==((L8,,U33==((F2""#abb&&uj99==((I5))%00==((G3''~>>5dmm6S6S5TTUVWWr!   )NN)r   )NNNN)+r   r   r   r   rF   rG   rH   r   r   r   r   r	   floatr8   r'   r   r:   r   intrZ   rc   rf   rt   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r    r!   r"   r<   r<   X   s   6  !93$$93 93 ou5uUD[7II
	93 (93 "%((..4
93 hS)4/0
93v:-- :w :C :Yh ::-- :w :C :Yh :7!7 "7 88''	7<2  88//2 >A2 	w	 2 h$ $2$,g $,%8L8Lc8Q2R $,V KYhh**KY KY 	KY
 KYZC!C.1C<?C	CJ>h >7 >/( /w /dYX( YXs YXS YXbf YXkn YXx6X 6x~ 6RY 6rx G 6%( %w %N'X '# 'PS 'X_ 'T d w ,HSM  ( . x}  (3-  D!49 !&  $!%))-6X}6X c]6X TN	6X
 !6X 
6Xr!   r<   )&loggingdataclassesr   typingr   r   r   r   r   r	   rF   torch.fxtorch.fx._compatibilityr
   torch.fx.noder   
shape_propr   split_utilsr   tools_commonr   r   r   r   r   r   r   __all__	getLoggerr   r   r   r   r   r   r'   r<   r    r!   r"   <module>r     s     ! = =   1 ! ! &   '

H
% e,9  - e,  - e,	  -   <DX DXr!   