
    `i                        d dl Z d dl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 ej        j        Zdej        defd	Zej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej        ej         ej!        ej"        gZ#d
ej        j$        j%        fdZ&dej'        dej'        fdZ(d Z)d Z*d Z+dS )    N)Callable)free_unbacked_symbols)StorageWeakRef)_pytree)tree_flattennodereturnc                 R    t          | j        d          r| j        j        S | j        S )Noverloadpacket)hasattrtargetr   r   s    r/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/torch/_functorch/compile_utils.pyget_aten_targetr      s*    t{,-- *{));    fx_gc                 z   t          j                    }i i }i }ddlm}m}  ||            t          | j                  d         }|j        dk    sJ dt           j        dt          fdfd|j
        D             fd	| j        D             }| j        D ]}|j        d
k    s|j        dk    s|j        dk    s}t          |          t          v sgt          |          t          j        u sL||v sHd|j        v r^t!          |j        d         t"          j                  r9t'          |j        d                   r|                    |fd          }	|	|<   fd}
 |
|j                  \  }} |
|j                  \  }}|j        ||||d}t1          t3          d |D                       t3          d |D                       f          }|j        |f}||v }d}|r)||         |k    r||         } |||          r||<   od}|                    |fd          }	|	|<   |s|s
|	||<   |||<   |S )Nr   )compute_mutation_region_idssame_mutation_regionsoutputr   r	   c                     d| j         vs%t          | j         d         t          j                  sdS 	 | j         d                                          n# t
          $ r Y dS w xY wdS )zGWe can evaluate only nodes that represent tensors with defined storage.valFT)meta
isinstancetorchTensoruntyped_storageNotImplementedErrorr   s    r   checkable_nodez$fx_graph_cse.<locals>.checkable_nodeA   sv    	!!DIe4Del)S)S!5	Ie,,...." 	 	 	55	 ts   A 
A A c                 ~    h | ]9} |          t          |j        d                                                    :S r   r   r   r   ).0nr    s     r   	<setcomp>zfx_graph_cse.<locals>.<setcomp>M   sS       >!qve}446677  r   c                     h | ]=} |          r0t          |j        d                                                    v ;|>S r"   r#   )r$   r%   r    output_storagess     r   r&   zfx_graph_cse.<locals>.<setcomp>R   s_          >!  16%=88::;;NN 	
 ONNr   placeholderget_attrr   c                     |          S N xenvs    r   <lambda>zfx_graph_cse.<locals>.<lambda>u       A r   c                 t   t          |           \  } }t          t          |                     D ]x}| |         }t          |t          j        j        j                  r|v r|         | |<   t          |t          j        t          j	        t          j
        f          r
|j        | |<   yt          |           |fS r,   )r   rangelenr   r   fxr   NodeSymBoolSymIntSymFloattuple)arg_listspecivr0   s       r   
substitutez fx_graph_cse.<locals>.substitutez   s    !-h!7!7$s8}}-- - -A A!!UX]%788 -Q#XX&)!f!!emU\5>%RSS -&'fX,,r   )r   args	args_speckwargskwargs_specc              3   8   K   | ]}|t          |          fV  d S r,   typer$   as     r   	<genexpr>zfx_graph_cse.<locals>.<genexpr>   s,      22477|222222r   c              3   8   K   | ]}|t          |          fV  d S r,   rF   rH   s     r   rJ   zfx_graph_cse.<locals>.<genexpr>   s,      9W9W11d1gg,9W9W9W9W9W9Wr   FTc                     |          S r,   r-   r.   s    r   r1   zfx_graph_cse.<locals>.<lambda>   r2   r   )r6   Graphtorch._inductor.pattern_matcherr   r   listnodesopr7   boolall_input_nodesr   rand_opsatenemptyr   r   sympySymbolr   	node_copyrA   rC   r   hashr;   )r   	new_graphhash_env	token_mapr   r   output_nodenodes_that_alias_outputsr%   new_noder@   rA   rB   rC   rD   tokenhash_arghash_valhash_val_in_hash_envoverwrite_due_to_mutationduplicate_n_prevr    r0   r(   s                        @@@r   fx_graph_cserg   .   sL   

I
CHI       
  %%%
  
++B/K>X%%%%
RW 
 
 
 
 
   ,  O
               Z P, P, DM!!txtz!!q!!X-- q!!TZ//,,, qve}el;;  )!&-88  
 !**1.>.>.>.>??HCFF- - - - - )j00OD)",*QX"6"6FK
 (& * E 22T22222E9W9WPV9W9W9W4W4WX H (+H $,x#7 (-%# 5	((;u(D(D#+H#5 ((,<== 5-CF 15- **1.>.>.>.>??HCF( ,0D ,%-"&+	(#r   gmc                 0   t          | j                            dt          j                            }t          |          D ]>}t          |j                  dk    sJ |j        d         }|                    |           ?| 	                                 | S )Ncall_function)rQ   r      r   )
rO   graph
find_nodesoperatorgetitemreversedr5   rS   append	recompile)rh   getitem_nodesr   parents       r   raise_getitemsru      s     
x7GHH M ''  4'((A----%a(dLLNNNIr   c                     | j         j        D ]7}t          |j        t          j        j                  r|j        j        |_        8|                                  dS )z
    Modifies the target of graph nodes in :attr:`gm` to strip overloads.

    Args:
        gm(fx.GraphModule): The input Fx graph module to be modified
    N)	rl   rP   r   r   r   _ops
OpOverloadr   rr   )rh   r   s     r   strip_overloadsry      sN      5 5dk5:#899 	5+4DKLLNNNNNr   c                 .    |                      d          S )Nr)   rQ   )rm   )rl   s    r   get_placeholdersr|      s    }---r   c                     |                      d          D ]"}t          j        |j        d                   c S t	          d          )Nr   r{   r   zNo output node found)rm   pytreetree_leavesrA   AssertionError)rl   r   s     r   get_outputsr      sN      H -- 0 0!$)A,/////
/
0
00r   ),rn   typingr   rW   r   torch.fxr6   %torch.fx.experimental.symbolic_shapesr    torch.multiprocessing.reductionsr   torch.utilsr   r~   torch.utils._pytreer   opsrU   r7   r   dropout_fused_dropout_standard_gamma	bernoullimultinomialnative_dropoutnormalpoissonbinomialrrelu	rand_likerandrandintrandnrandpermrT   rl   rM   rg   GraphModuleru   ry   r|   r   r-   r   r   <module>r      s                  G G G G G G ; ; ; ; ; ; ) ) ) ) ) ) , , , , , , y~"' h     	LNKLMJNILJM(}ux~+ } } } }@r~ ".    $
 
 
. . .1 1 1 1 1r   