
    ɯwg                     @   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 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j0                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  gZd	ej                  j@                  jB                  fd
Z"d Z#d Z$d Z%y)    )CallableN)StorageWeakRef)_pytree)tree_flattennodereturnc                 r    t        | j                  d      r| j                  j                  S | j                  S )Noverloadpacket)hasattrtargetr
   r   s    c/home/mcse/projects/flask/flask-venv/lib/python3.12/site-packages/torch/_functorch/compile_utils.pyget_aten_targetr      s+    t{{,-{{)));;    fx_gc                    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}|j                  D ch c]0  } ||      r&t        |j                  d   j                               2 }	}| j                  D ch c]4  } ||      r*t        |j                  d   j                               |	v r|6 }
}| j                  D ]H  }|j                  d	k(  sN|j                  dk(  s?|j                  d
k(  s0t        |      t        v st        |      t         j"                  u s||
v r|j%                  |fd      }||<   |fd} ||j&                        \  }} ||j(                        \  }}|j*                  ||||d}t-        t/        d |D              t/        d |D              f      }|j*                  |f}||v }d}|r||   |k(  r||   } |||      r||<   d}|j%                  |fd      }||<   |s|r?|||<   |||<   K |S c c}w c c}w )Nr   )compute_mutation_region_idssame_mutation_regionsoutputr   r   c                     d| j                   vs't        | j                   d   t        j                        sy	 | j                   d   j	                          y# t
        $ r Y yw xY w)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_node=   sZ    		!DIIe4Dell)S	IIe,,.  # 		s   A 	A"!A"r   placeholderget_attrc                     |    S N xenvs    r   <lambda>zfx_graph_cse.<locals>.<lambda>c       A r   c                 |   t        |       \  } }t        t        |             D ]  }| |   }t        |t        j
                  j                  j                        r|v r|   | |<   t        |t        j                  t        j                  t        j                  f      s||j                  | |<    t        |       |fS r#   )r   rangelenr   r   fxr   NodeSymBoolSymIntSymFloattuple)arg_listspecivr'   s       r   
substitutez fx_graph_cse.<locals>.substituteh   s    !-h!7$s8}- -A A!!UXX]]%7%78Q#X&)!f!!emmU\\5>>%RS&'ff- X,,r   )r   args	args_speckwargskwargs_specc              3   6   K   | ]  }|t        |      f  y wr#   type.0as     r   	<genexpr>zfx_graph_cse.<locals>.<genexpr>   s     247|2   c              3   6   K   | ]  }|t        |      f  y wr#   r=   r?   s     r   rB   zfx_graph_cse.<locals>.<genexpr>   s     9W11d1g,9WrC   FTc                     |    S r#   r$   r%   s    r   r(   zfx_graph_cse.<locals>.<lambda>   r)   r   )r-   Graphtorch._inductor.pattern_matcherr   r   listnodesopr.   boolall_input_nodesr   r   r   r   rand_opsatenempty	node_copyr8   r:   r   hashr2   )r   	new_graphhash_env	token_mapr   r   output_noder   noutput_storagesnodes_that_alias_outputsnew_noder7   r8   r9   r:   r;   tokenhash_arghash_valhash_val_in_hash_envoverwrite_due_to_mutationduplicate_n_prevr'   s                          @r   fx_graph_cser`   *   s   
I
CHI
  %
  

+B/K>>X%%%
RWW 
 
 ,,! 	qvve}4467O   !166%=88:;N 	
    ZZ B, DDM!ttxttz!q!X- q!TZZ/,, **1.>?HCF- )0OD)",QXX"6FK
 ((& *E 2T22E9WPV9W4WXH (+H $,x#7 (-%#	((;u(D#+H#5 (,<=-CF 15- **1.>?HCF(0D%-"&+	(#EB,H a
 s   =5I9Ic                     | j                   j                  D ]L  }t        |j                  t        j
                  j                        s2|j                  j                  |_        N | j                          y)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)	graphrI   r   r   r   _ops
OpOverloadr
   	recompile)gmr   s     r   strip_overloadsrg      sO      5dkk5::#8#89++44DK5 LLNr   c                 &    | j                  d      S )Nr    rJ   )
find_nodes)rb   s    r   get_placeholdersrk      s    }--r   c                     | j                  d      D ]%  }t        j                  |j                  d         c S  t	        d      )Nr   ri   r   zNo output node found)rj   pytreetree_leavesr8   AssertionError)rb   r   s     r   get_outputsrp      sD      H - 0!!$))A,//0
/
00r   )&typingr   r   torch.fxr-    torch.multiprocessing.reductionsr   torch.utilsr   rm   torch.utils._pytreer   opsrN   r.   r   dropout_fused_dropout_standard_gamma	bernoullimultinomialnative_dropoutnormalpoissonbinomialrrelu	rand_likerandrandintrandnrandpermrM   rb   rF   r`   rg   rk   rp   r$   r   r   <module>r      s       ; ) , yy~~"'' h  	LLNNKKLLMMJJNNIILLJJMM(ouxx~~++ od
.1r   