
    `iC                     x   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
 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        j                  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j"        de
fdZ#dS )    N)	dataclass)partialwraps)Callable)tqdm)StorageWeakRef)ContentStoreWriter   )get_outputsget_placeholdersc                   d    e Zd ZU ee         ed<   ee         ed<   ej        ed<   ej        ed<   dS )LoadTensorMetasizestridedtypedeviceN)	__name__
__module____qualname__listint__annotations__torchr   r        p/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_functorch/fx_minifier.pyr   r      sK         
s)OOOI;L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	                      | _        d S N)super__init__r    r!   setseen_storages)selfmodr    r!   	__class__s       r   r%   zConcreteProp.__init__    s<    ( UUr   c                    | j                             d           t                                          |          }|j        }t          |t          j                  r| j        ||j	        d<   nt          |                                          | j        v rd |j	        d<   n| j        s9| j                            t          j                            d|          |           t%          |                                |                                |j        |j                  |j	        d<   | j                            t          |                                                     n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&   sA   	GGQva&& 	0{"+,'((!!"3"3"5"566$:LLL 04AF+,,, Q00gt1L1LaPPP/=!((**agqx0 0AF+, &**>!:K:K:M:M+N+NOOOO'/AF#$r   c                    t          dt          | j        j        j                  | j        d u           5 }|| _         t                      j        | }| j	        s|
                    d           |cd d d            S # 1 swxY w Y   d S )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    ;dk'-..K4'
 
 
 	 DIT"A$ $$[   	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	s   ;A??BB)r   r   r   r%   r0   rI   __classcell__)r*   s   @r   r   r      sy        &* # # # # # # #    2        r   r   c                 \    | j         dk    o!| 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K59/;CCr   c                    |j         dk    s|j         dk    rdS t          |          rdS |j                            dd           }t	          |t
          j                  r8d|_         |j        |_        d|_	        i |_
        |                    |           dS |dS |t          u r.d}t          |j                  D ]}t          | ||          p|}|S t	          |t                     rtd|_         t
          j        j        j        j        |_        t*          j                            d|j                  |j        |j        f|_	        |j        |j        d	|_
        dS dS )
NoutputplaceholderFr,   r   TrL   r-   )r   r   )rM   rT   r4   getr2   r   r3   r1   rN   rH   kwargsappendr;   r   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   sf   w(dg66u4   u9==!1488L,-- "i	L!!!t		u		!	!tz** 	K 	KJ,UJEEJAA
 	L.	1	1 !i*6>GLL$),,
	 #)!'
 
 t5r   c                     t          j                     d}t          j        |d           ddlm}  || ||           dS )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.stablehlorb   )minified_fx_graphinputshlo_dirrb   s       r   create_minified_hlo_graphri      s[    
 (((GK4((((AAAAAA!!"3VWEEEEEr   c           	          t          dt          | j        j                   dd |D              d| j         d           d S )Nz
# Working Repro with z nodes
inps = c                 @    g | ]}|j         |j        |j        j        fS r   )shaper   r   type).0is     r   
<listcomp>zdump_state.<locals>.<listcomp>   s'    999q!'17AHM	*999r   zo
inps = [torch.zeros(())] + [torch.ones(shape, dtype=dtype, device=device) for (shape, dtype, device) in inps]

)printrB   rD   rE   code)fx_gr]   s     r   
dump_stateru      sn    		$**++	 	99D999	 	 		 	 	    r   c                 *    | dk    rdS | | dz
  z  dk    S )Nr   Fr
   r   )r<   s    r   is_power_of_tworw      s#    AvvuQKAr   c                   J    e Zd ZU ej        ed<   eej                 ed<   d Z	dS )
ReproStaterD   r]   c                 |    t          | j                  }t          |          t          | j                  k    sJ d S r#   )r   rD   rB   r]   )r(   ph_nodess     r   __post_init__zReproState.__post_init__   s6    #DJ//8}}DI......r   N)
r   r   r   fxGraphr   r   r   r3   r|   r   r   r   ry   ry      sC         8OOO
u|
/ / / / /r   ry   F)save_diroffload_to_diskr!   skip_sanitymax_granularityfail_fru   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-          j        t	          |j        j                                      z            }|t3          ||          } |||d"$          }||}|d#z  }d%}|d&k    r  |||d%$          }||}d"}n|d#z  }|d&k     |r |d&          }||}	  |j        |j                  st          d'          t          d( d)t          j                   t#          j         |j                  }d*t4          j        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                 \    t          j        t          j        |                     j        S r#   )r}   GraphModulecopydeepcopyrD   )fx_graphr   s    r   deepcopy_fx_graphz#minifier.<locals>.deepcopy_fx_graph   s"    ~fdmH&=&=>>DDr   c                     t          j        |           } dz  t          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   sQ    e$$qnVU++	|C&&&r   r   z#Input graph did not fail the testerzStarted off with  nodesfilestrategyr1   c                 X     t                     ddt          f fd            }|S )Nr
   	old_statec                 >   t          t          j                   t          d d| dt          | j        j                   dt          | j                   d	t          j                     
| j                  t          | j                  |          }|bt          |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                   ||k    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                   d S |S t          d t          j                   d S )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: )
rr   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   sz""""VT V V V V	-..V V8;IN8K8KV V VZ   
 !!!)/22D4H4H+ I $	 566		 566	y~..y~..{9?;;<<{9?;;<< %y(($(MNiNNYNNN Z    h&&$(MMhMMHMMM Z    h&&$(MNhNNHNNN Z   
 % O&'MNNN"{9?INCC  W Z     4  otooCJ77774r   )r
   )r   ry   )r   r1   r   r   r   s   `` r   _register_strategyz$minifier.<locals>._register_strategy   sV    	x1	 1	
 1	 1	 1	 1	 1	 1	 1	 1	 
1	f r   c                 &    t          |           S )N)r1   )r   )r1   r   s    r   register_strategyz#minifier.<locals>.register_strategy  s    )5555r   zTruncate suffixc                   	 t                      }t          j                    }i 	t          | j                  D ]\  }}|                    |	fd          }|j        dvr||z  dk    r||dz  z  dk    r||vr|                    |f          }t          |j                  t          | j                  k     r 
||          rt          ||          c S |
                    |           |                    |           |	|<   d S )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   ry   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/   HJJ	"9?33 	! 	!IC **41A1A1A1ABBHw777 +%**a0A556))"+"2"2H;"?"?K9?++c)/.B.BBB{{!8H HB  *)X>>>>>

3!,,[999 CIItr   zRemove outputsc                    t          d|dz            }t          | j                  D ]\  }}||_        |j        dk    r|} nt          |j        d         t          j                  rd S t          |j        d         d           }t          |          dk    rd S t          dt          |          |          D ]>}|d |         |||z   d          z   f|_         | |          rt          | |          c S ?d S )Nr
   r   rV   r   c                 b    t          | t          j                  r| j        nt	          d          S )Ng    eA)r2   r}   Noder   r   )r   s    r   r   z2minifier.<locals>.remove_outputs.<locals>.<lambda>F  s$    :a3I3I*W!%%sSVxx r   )key)maxr   rE   r   rM   r2   rH   r}   r   sortedrB   rangery   )r   r   r   r   rS   rV   output_argsr   s          r   remove_outputsz minifier.<locals>.remove_outputs9  s4   ![A-.."9?33 	 	ICDHw("" # fk!nbg.. 	4KN W W
 
 
 {q  4C,,k:: 	7 	7C&tt,{3;L;N;N/OOQFK{9h// 7!)X666667tr   	cur_statec                    | j         }| j        }t          |          }t          |          t          |          k    sJ g }t	          t          |                    D ]W}t          ||         j                  dk    r|                    ||                    <|                    ||                    Xt          |          t          |          k     rt          ||          S d S )Nr   )	rD   r]   r   rB   r   r[   r   rZ   ry   )r   r   r   r{   r   r   s         r   remove_unused_inputs_uncheckedz0minifier.<locals>.remove_unused_inputs_uncheckedQ  s    O	>#I..8}}H----X'' 	/ 	/C8C=&''1,,$$Xc]3333....x==3x==((i222tr   c                 R     |           }| |j         |j                  r|S d S r#   )rD   r]   )r   r   r   r   s     r   remove_unused_inputs_checkedz.minifier.<locals>.remove_unused_inputs_checkeda  s9    229==	 [[).%Q%Q tr   c                 6     t          | |                    S r#   )ry   )r   r   r   r   s      r   _remove_unused_wrapperz(minifier.<locals>._remove_unused_wrapperg  s    ++Jy(,K,KLLLr   zRemove unused inputszEliminate dead codec                 h    |                                  r | |          rt          | |          S d S r#   )eliminate_dead_codery   )r   r   r   r   s      r   r   z%minifier.<locals>.eliminate_dead_coden  s?    ((** 	3{{9h/O/O 	3i222tr   c                    t          j                    }i d}| j        D ]}|j        dk    r|                    |fd          }||<   ,|slt          |          r]|                    |j                  }||<   |                    t          j
        j        j        j        |j        i |j                   d}| j        D ]$}|vr|                    |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)   HJJ	$
 O 	, 	,Dw-''$..t5E5E5E5EFF$D		) ,.A$.G.G ,$00;;$D	I(4<diW4;WW    (,$$ O 	% 	%D3$..t5E5E5E5EFF$D	r   zDelta Debuggingr   c                 ,   t          | j                  }t          d||          D ]}d} |           }|d d          }t          |||z             }t          ||          D ]/}	t	          |j                  |	         }
t          ||
|          rd}0|so|                                  ||          } t          ||                    }|t          ||          } |j        |j	                  rt          |j        |j	                  c S d S )Nr   FT)
rB   rE   r   minr   r\   r   ry   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  sJ   	((	 I{;; 	C 	CKK)))44I{HI{['@AAI[)44 ' '	005/	8XNN '"&K ))+++11)XFFI66z)X7V7VWWI &y(;;	{9?IN;; C!)/9>BBBBBC tr   zConsolidate Inputsc                     t          |          } | |          } t          |          |k    r | |          rt          | |          S d S r#   )rB   ry   )r   r   r   old_lenr   r   s       r   consolidate_inputsz$minifier.<locals>.consolidate_inputs  sW    h----iBB	x==7""{{9h'G'G"i222tr   c                 *   t          d| t          j                   g }t          | j        j                  }t          t          | j                            }||dz  k    r|gz  }|r|
gz  }|	gz  }|D ]} || |          }||c S d S )NzTrying granularity r   r   )rr   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    1K11
CCCC
+122	+m&9::;;a''>**J 	#$" J 	}o66
" 	! 	!H <<I$     %tr   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   rw   r   r	   r   rI   rr   r   r   r   strry   r}   r~   r   r]   r   mathfloorlog2r   r7   environri   )!r   r]   r   ru   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      so   4 dUDM*****LM=&''H"??+K+K"PoPPPQQQKE E E E E' ' ' ' ' ' ' F .#H--LL\BBBLdSS B{{=$?? B@AAA	
.h
.
.
.SZ@@@@5X 5S 5 5 5 5 5 5 5n6 6 6 6 6 6 6 ())    *)0 '((    )(.*     
       M M M M M E,,-CDD  ,--    .-
  8 ())28         *). +,,     -, }d33M         0
2>&-*=>>@RSSS!
49S9L9R5S5S+T+T U UVWW&o{;;K#OM;QUVVV	 %MQ'{U  I $ )#AK Q  	"N=!44	 %M;}*M,>?? XVWWW	
'+
'
'
'cj9999(;<<J "*$$!*m.@AAAJz=-...	
/cjAAAA})))r   )$r   r   r7   r   dataclassesr   	functoolsr   r   typingr   r   torch.fxr}   	torch.hubr    torch.multiprocessing.reductionsr   torch.utils._content_storer	   compile_utilsr   r   objectr;   r   Interpreterr   rT   r\   ri   ru   rw   ry   r   r   r   r   r   <module>r      s&     				 



 ! ! ! ! ! ! $ $ $ $ $ $ $ $                    ; ; ; ; ; ; 9 9 9 9 9 9 8 8 8 8 8 8 8 8 688        , , , , ,58' , , ,^  - - -`
F 
F 
F     / / / / / / / / &	G* G* G* G*NG* 	G* G* G* G* G* G*r   