
    sgC                     t   d dl Z d dlZd dlZd dlZd dlmZ d dlmZmZ d dl	m
Z
m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  e       Ze G d
 d             Z G d dej                  j6                        Zd Zd Zd Zd Z d Z!e G d d             Z"e fdddddddejF                  de
fdZ$y)    N)	dataclass)partialwraps)CallableList)tqdm)StorageWeakRef)ContentStoreWriter   )get_outputsget_placeholdersc                   j    e Zd ZU ee   ed<   ee   ed<   ej                  ed<   ej                  ed<   y)LoadTensorMetasizestridedtypedeviceN)	__name__
__module____qualname__r   int__annotations__torchr   r        O/var/www/html/venv/lib/python3.12/site-packages/torch/_functorch/fx_minifier.pyr   r      s)    
s)OI;;LLr   r   c                   :     e Zd Zddd fd
Z fdZ fdZ xZS )ConcretePropNFwriterskip_offloadc                ^    t         |   |       || _        || _        t	               | _        y N)super__init__r    r!   setseen_storages)selfmodr    r!   	__class__s       r   r%   zConcreteProp.__init__    s)    ( Ur   c                     | j                   j                  d       t        |   |      }|j                  }t        |t        j                        r| j                  ||j                  d<   |S t        |j                               | j                  v rd |j                  d<   |S | j                  s:| j                  j                  t        j                   j#                  d|      |       t%        |j'                         |j)                         |j*                  |j,                        |j                  d<   | j                  j/                  t        |j                                      |S t0        |j                  d<   |S )Nr   concrete_valueeager)pbarupdater$   run_nodename
isinstancer   Tensorr    metar	   untyped_storager'   r!   write_tensorospathjoinr   r   r   r   r   addis_tuple)r(   nrr1   r*   s       r   r0   zConcreteProp.run_node&   s1   		GQvva&{{"+,'(   "!"3"3"56$:L:LL 04AFF+,   ,,00gt1LaP/=!((*aggqxx0AFF+, &&**>!:K:K:M+NO  (0AFF#$r   c                    t        dt        | j                  j                  j                        | j
                  d u       5 }|| _        t        |    | }| j                  s|j                  d       |cd d d        S # 1 sw Y   y xY w)Nz(Saving intermediates for delta debugging)desctotaldisablezESaved!  To skip next time, run with --skip-saving-eager-intermediates)r   lenmodulegraphnodesr    r.   r$   runr!   set_description)r(   argsr.   r=   r*   s       r   	propagatezConcreteProp.propagate?   s    ;dkk''--.KK4'
 	 DIT"A$$$$[ 	 	 	s   2A??B)r   r   r   r%   r0   rI   __classcell__)r*   s   @r   r   r      s    &* #2 r   r   c                     | j                   dk(  xr: | j                  t        j                  j                  j
                  j                  u S )Ncall_function)optargetr   ops
debugprimsload_tensordefault)nodes    r   is_load_tensor_noderT   N   s;    ?" 	DKK599//;;CCCr   c                    |j                   dk(  s|j                   dk(  ryt        |      ry|j                  j                  dd       }t	        |t
        j                        r8d|_         |j                  |_        d|_	        i |_
        |j                  |       y|y|t        u r/d}t        |j                        D ]  }t        | ||      xs |} |S t	        |t               rd|_         t
        j"                  j$                  j&                  j(                  |_        t*        j,                  j/                  d|j                        |j0                  |j2                  f|_	        |j4                  |j6                  d	|_
        yy)
NoutputplaceholderFr,   r   TrL   r-   )r   r   )rM   rT   r4   getr2   r   r3   r1   rN   rH   kwargsappendr;   listusers_convert_node_to_placeholderr   rO   rP   rQ   rR   r7   r8   r9   r   r   r   r   )rD   rS   inpsconcrete_valr=   
tuple_users         r   r]   r]   V   sG   ww(dgg64 99==!148L,-ii	L!				!tzz* 	KJ,UJEJA	K 	L.	1!ii**66>>GGLL$)),
	 #))!''
 r   c                     t        j                          d}t        j                  |d       ddlm}  || ||       y)z
    Takes minified FX graph as primary input, and ports it to HLO via StableHLO
    Provides minified HLO graph as output, and archive them to local directory
    z
/hlo_filesT)	exists_okr   )save_torch_model_as_stablehloN)r7   getcwdmakedirstorch_xla.stablehlorc   )minified_fx_graphinputshlo_dirrc   s       r   create_minified_hlo_graphrj      s4    
 Z(GKK4(A!"3VWEr   c                     t        dt        | j                  j                         d|D cg c]/  }|j                  |j
                  |j                  j                  f1 c} d| j                   d       y c c}w )Nz
# Working Repro with z nodes
inps = zo
inps = [torch.zeros(())] + [torch.ones(shape, dtype=dtype, device=device) for (shape, dtype, device) in inps]

)	printrB   rD   rE   shaper   r   typecode)fx_gr^   is      r   
dump_staters      sn    	$****+, -489q!''177AHHMM	*9: ; 		 	:s   4A7c                 $    | dk(  ry| | dz
  z  dk(  S )Nr   Fr   r   )r<   s    r   is_power_of_tworu      s    AvQKAr   c                   V    e Zd ZU ej                  ed<   eej                     ed<   d Z	y)
ReproStaterD   r^   c                 t    t        | j                        }t        |      t        | j                        k(  sJ y r#   )r   rD   rB   r^   )r(   ph_nodess     r   __post_init__zReproState.__post_init__   s*    #DJJ/8}DII...r   N)
r   r   r   fxGraphr   r   r   r3   rz   r   r   r   rw   rw      s    88O
u||
/r   rw   F)save_diroffload_to_diskr!   skip_sanitymax_granularityfail_frs   c          
           t        |t        t        f      sJ  j                  }	t	        |	j
                        }
|t        |      st        d| d      d fd fdd}|rt        |      } t         ||      j                  |  |s |	|      st        d      t        d	|
 d
t        j                         dt        dt        ffddt        ffd} |d      fd        |d      fd       dt         fd dt         f fdfd}  |d      |       |d      fd       d  |d      dt"        j$                  f fd        |d      fd        t!        |	|      }fd!}	  |t#        j&                   |j                        |j(                         t+        d#t-        j.                  t-        j0                  t	        |j                  j
                                    z        }|t3        ||      } |||d"$      }||}|d#z  }d%}|d&k\  r |||d%$      }||}d"}n|d#z  }|d&k\  r|r |d&      }||}	  |j                  |j(                        st        d'      t        d( d)t        j                         t#        j&                   |j                        }d*t4        j6                  v rt9        ||j(                          |||j(                         t        d+t        j                         ||j(                  fS ),aF  
    Minimizes a FX graph with given inputs, such that the resulting FX graph still returns True for module_fails.

    Does 2 main strategies:
    1. Truncates suffix: Removes some suffix from the graph and sets a new output.
    2. Delta Debugging: Tries replacing half of the graph with inputs. If fails,
        tries replacing quarter of the graph, etc.

    >>> # xdoctest: +SKIP(failing)
    >>> failing_function = fx.symbolic_trace(f)
    >>> minimize(failing_function, [torch.randn(5)], lambda fx_g, inps: fx_g(*inps))

    note: module_fails returns True if it fails.
    Nzmax_granularity z not power of twor   c                 j    t        j                  t        j                  |             j                  S r#   )r{   GraphModulecopydeepcopyrD   )fx_graphr   s    r   deepcopy_fx_graphz#minifier.<locals>.deepcopy_fx_graph   s#    ~~fdmmH&=>DDDr   c                     t        j                  |       } dz  t        j                  |       }|j                  j                           ||      S )Nr   )r   r   r{   r   rD   lint)rD   r^   r)   r   module_failsnum_queriess      r   graph_failszminifier.<locals>.graph_fails   sF    e$qnnVU+		C&&r   r   z#Input graph did not fail the testerzStarted off with  nodesfilestrategyr1   c                 H     t               ddt        f fd       }|S )N	old_statec                 X   t        t        j                         t        d d| dt        | j                  j
                         dt        | j                         d	t        j                           
| j                        t        | j                        |      }|it        |j                  j
                        }t        | j                  j
                        }t        |j                        }t        | j                        }t        t        |j                              }t        t        | j                              }d}	||k  r$d}	t        d	| d
| dt        j                         ||kD  r$d}	t        d	| d
| dt        j                         ||k  r$d}	t        d	| d
| dt        j                         |	st        d       |j                  |j                        st        dt        j                         y |S t        d t        j                         y )Nr   z
Strategy: z (G: z) (z nodes, z inputs)FTzSUCCESS: Went from z to r   z inputsz outputsz$Success raised but no progress made?z=WARNING: Something went wrong, not applying this minificationzFAIL: )
rm   sysstderrrB   rD   rE   r^   r[   r   RuntimeError)r   granularity	new_state	new_nodes	old_nodesnew_inpsold_inpsnew_outsold_outsprogress_mader   r   r1   r   s             r   new_funcz6minifier.<locals>._register_strategy.<locals>.new_func   s   szz"TF%} 5	--./xINN8K7LHVZZ
 !!)//2D4H+I $	 5 56		 5 56	y~~.y~~.{9??;<{9??;< %y($(M-i[YKvN ZZ h&$(M-hZtH:WM ZZ h&$(M-hZtH:XN ZZ
 %&'MNN"9??INNCW ZZ    tfoCJJ7r   )r   )r   rw   )r   r1   r   r   r   s   `` r   _register_strategyz$minifier.<locals>._register_strategy   s(    	x1	
 1	 
1	f r   c                     t        |       S )N)r1   )r   )r1   r   s    r   register_strategyz#minifier.<locals>.register_strategy  s    )55r   zTruncate suffixc                   	 t               }t        j                         }i 	t        | j                        D ]  \  }}|j                  |	fd      }|j                  dvr||z  dk(  r||dz  z  dk7  rz||vrv|j                  |f      }t        |j                        t        | j                        k  r 
||      rt        ||      c S |j                  |       |j                  |       |	|<    y )Nc                     |    S r#   r   xenvs    r   <lambda>z1minifier.<locals>.remove_suffix.<locals>.<lambda>&  s    3q6 r   )rW   rV   r      )r&   r{   r|   	enumeraterE   	node_copyrM   rV   rB   rw   r:   
erase_node)	cur_graphcur_inpsr   tested	new_graphidxrS   new_nodeoutput_noder   r   s            @r   remove_suffixzminifier.<locals>.remove_suffix   s    HHJ	"9??3 	!IC **41ABHww77 +%*a0A56)"+"2"2H;"?K9??+c)//.BB{!8H  *)X>>

3!,,[9 CI#	!$ r   zRemove outputsc                    t        d|dz        }t        | j                        D ]  \  }}||_        |j                  dk(  s|} n t        j                  d   t        j                        ry t        |j                  d   d       }t        |      dk(  ry t        dt        |      |      D ]-  }|d | |||z   d  z   f|_         | |      s!t        | |      c S  y )Nr   r   rV   r   c                 d    t        | t        j                        r| j                  S t	        d      S )Ng    eA)r2   r{   Noder   r   )r   s    r   r   z2minifier.<locals>.remove_outputs.<locals>.<lambda>F  s     :a3I!%% sSVx r   )key)maxr   rE   r   rM   r2   rH   r{   r   sortedrB   rangerw   )r   r   r   r   rS   rV   output_argsr   s          r   remove_outputsz minifier.<locals>.remove_outputs9  s    ![A-."9??3 	ICDHww("		 fkk!nbgg.KKN W
 {q C,k: 	7C&t,{3;L;N/OOQFK9h/!)X66	7 r   	cur_statec                    | j                   }| j                  }t        |      }t        |      t        |      k(  sJ g }t	        t        |            D ]F  }t        ||   j
                        dk(  r|j                  ||          3|j                  ||          H t        |      t        |      k  rt        ||      S y )Nr   )	rD   r^   r   rB   r   r\   r   rZ   rw   )r   r   r   ry   r   r   s         r   remove_unused_inputs_uncheckedz0minifier.<locals>.remove_unused_inputs_uncheckedQ  s    OO	>>#I.8}H---X' 	/C8C=&&'1,$$Xc]3.		/
 x=3x=(i22r   c                 X     |       }| |j                   |j                        r|S y r#   )rD   r^   )r   r   r   r   s     r   remove_unused_inputs_checkedz.minifier.<locals>.remove_unused_inputs_checkeda  s-    29=	 [)..%Qr   c                 (     t        | |            S r#   )rw   )r   r   r   r   s      r   _remove_unused_wrapperz(minifier.<locals>._remove_unused_wrapperg  s    +Jy(,KLLr   zRemove unused inputszEliminate dead codec                 P    | j                         r | |      rt        | |      S y r#   )eliminate_dead_coderw   )r   r   r   r   s      r   r   z%minifier.<locals>.eliminate_dead_coden  s(    ((*{9h/Oi22r   c                    t        j                         }i d}| j                  D ]  }|j                  dk(  r|j	                  |fd      }||<   -|st        |      rw|j                  |j                        }||<   |j                  t        j                  j                  j                  j                  |j                  i |j                         d} | j                  D ]!  }|vs|j	                  |fd      }||<   # |S )NFrW   c                     |    S r#   r   r   s    r   r   z=minifier.<locals>._consolidate_placeholders.<locals>.<lambda>~      s1v r   Tc                     |    S r#   r   r   s    r   r   z=minifier.<locals>._consolidate_placeholders.<locals>.<lambda>  r   r   )r{   r|   rE   rM   r   rT   rW   r1   rZ   r   rO   rP   rQ   rR   rH   rY   )r   r^   r   seen_non_placeholderrS   r   r   s         @r   _consolidate_placeholdersz+minifier.<locals>._consolidate_placeholderst  s    HHJ	$
 OO 	,Dww-'$..t5EF$D	).A$.G$00;$D	II((44<<diiW4;;W (,$	, OO 	%D3$..t5EF$D		% r   zDelta Debuggingr   c                    t        | j                        }t        d||      D ]  }d} |       }|d d  }t        |||z         }t        ||      D ]*  }	t	        |j                        |	   }
t        ||
|      s)d}, |s]|j                           ||      } t        ||            }|t        ||      } |j                  |j                        st        |j                  |j                        c S  y )Nr   FT)
rB   rE   r   minr[   r]   r   rw   rD   r^   )r   r   r   	num_nodesstart_rangeis_removingr   r   	end_ranger   r   r   r   r   r   r   s               r   delta_debuggingz!minifier.<locals>.delta_debugging  s    	(	 I{; 	CKK))4I{HI{['@AI[)4 '	05/	8XN"&K' ))+1)XFI6z)X7VWI &y(;	9??INN;!)//9>>BB#	C& r   zConsolidate Inputsc                 t    t        |      } | |      } t        |      |kD  r | |      rt        | |      S y r#   )rB   rw   )r   r   r   old_lenr   r   s       r   consolidate_inputsz$minifier.<locals>.consolidate_inputs  s=    h--iB	x=7"{9h'Gi22r   c                 *   t        d| t        j                         g }t        | j                  j
                        }t        t        | j                              }||dz  kD  r|gz  }|r|
gz  }|	gz  }|D ]  } || |      }||c S  y )NzTrying granularity r   r   )rm   r   r   rB   rD   rE   r   )failing_stater   use_non_granular
strategiesr   num_outputsr   r   r   r   r   r   r   remove_unused_inputss           r   try_granularityz!minifier.<locals>.try_granularity  s    #K=1

C
++112	+m&9&9:;a'>**J#$" J 	}o66
" 	!H <I$  	! r   Tr   )r   Fr   z9Uh oh, something went wrong :( Final graph is not failingzMade z queriesXLA_HLO_DEBUGz#Wrote minimal repro out to repro.py)r2   tupler[   rD   rB   rE   ru   r   r
   r   rI   rm   r   r   r   strrw   r{   r|   r   r^   r   mathfloorlog2r   r7   environrj   )!r   r^   r   rs   r}   r~   r!   r   r   failing_graphcur_sizer    r   r   r   r   r   r   has_progress
failing_fxr   r   r   r   r   r   r   r   r   r   r   r   r   s!   ` `                 @@@@@@@@@@@@@r   minifierr      s`   4 dUDM***LLM=&&'H"??+K-o->>OPQQKE' F#H-LL\BLLdS{=$?@AA	hZv
.SZZ@5X 5S 5n6 6 () *0 '( ).*  
 M E,-CD ,- .
8 ()288  *. +, - }d3M 0 2>>&-*=*=>@R@RS!

499S9L9L9R9R5S+T UVW&o{;K#M;QUV	 %MQ'{UI $ )#AK Q "=!4	 %M}**M,>,>?VWW	E+h
'cjj9(;(;<J "**$!*m.@.@Az=--.	
/cjjA}))))r   )%r   r   r7   r   dataclassesr   	functoolsr   r   typingr   r   r   torch.fxr{   	torch.hubr    torch.multiprocessing.reductionsr	   torch.utils._content_storer
   compile_utilsr   r   objectr;   r   Interpreterr   rT   r]   rj   rs   ru   rw   r   r   r   r   r   <module>r      s      	 
 ! $ !    ; 9 8 8   ,588'' ,^-`
F / / / &	G* G*NNG* 	G*r   