
    rh                        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j0                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  ej>                  ej@                  ejB                  ejD                  gZ#d
ej                  jH                  jJ                  fdZ&dejN                  dejN                  fdZ(d Z)d Z*d Z+y)    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    q/var/www/html/ai-insurance-compliance-backend/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 ]  }|j                  d	k(  s|j                  dk(  s|j                  d
k(  s}t        |      t        v slt        |      t         j"                  u sQ||
v sMd|j                  v rZt%        |j                  d   t&        j(                        r3t+        |j                  d         r|j-                  |fd      }||<   Ɉfd} ||j.                        \  }} ||j0                        \  }}|j2                  ||||d}t5        t7        d |D              t7        d |D              f      }|j2                  |f}||v }d}|r||   |k(  r||   } |||      r||<   jd}|j-                  |fd      }||<   |s|r|||<   |||<    |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_nodeA   sZ    		!DIIe4Dell)S	IIe,,.  # 		s   A 	A"!A"r   placeholderget_attrc                     |    S N xenvs    r   <lambda>zfx_graph_cse.<locals>.<lambda>u       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>.substitutez   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   rC   zfx_graph_cse.<locals>.<genexpr>   s     9W11d1g,9WrD   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emptyr   sympySymbolr   	node_copyr9   r;   r   hashr3   )r   	new_graphhash_env	token_mapr   r   output_noder    noutput_storagesnodes_that_alias_outputsnew_noder8   r9   r:   r;   r<   tokenhash_arghash_valhash_val_in_hash_envoverwrite_due_to_mutationduplicate_n_prevr(   s                          @r   fx_graph_cserc   .   s   
I
CHI
  %
  

+B/K>>X%%%
RWW 
 
 ,,! 	qvve}4467O   !166%=88:;N 	
    ZZ P, DDM!ttxttz!q!X- q!TZZ/,, qvve}ell;)!&&-8 !**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%-"&+	(#aP,d }
 s   =5J$9J)gmc                 "   t        | j                  j                  dt        j                              }t        |      D ]<  }t        |j                        dk(  sJ |j                  d   }|j                  |       > | j                          | S )Ncall_function)rK   r      r   )
rI   graph
find_nodesoperatorgetitemreversedr-   rM   append	recompile)rd   getitem_nodesr   parents       r   raise_getitemsrq      s     
x7G7GHM ' 4''(A---%%a(d
 LLNIr   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)	rh   rJ   r   r   r   _ops
OpOverloadr   rn   )rd   r   s     r   strip_overloadsru      sO      5dkk5::#8#89++44DK5 LLNr   c                 &    | j                  d      S )Nr!   rK   )ri   )rh   s    r   get_placeholdersrx      s    }--r   c                     | j                  d      D ]%  }t        j                  |j                  d         c S  t	        d      )Nr   rw   r   zNo output node found)ri   pytreetree_leavesr9   AssertionError)rh   r   s     r   get_outputsr}      sD      H - 0!!$))A,//0
/
00r   ),rj   typingr   rQ   r   torch.fxr.   %torch.fx.experimental.symbolic_shapesr    torch.multiprocessing.reductionsr   torch.utilsr   rz   torch.utils._pytreer   opsrO   r/   r   dropout_fused_dropout_standard_gamma	bernoullimultinomialnative_dropoutnormalpoissonbinomialrrelu	rand_likerandrandintrandnrandpermrN   rh   rG   rc   GraphModulerq   ru   rx   r}   r%   r   r   <module>r      s	        G ; ) , yy~~"'' h  	LLNNKKLLMMJJNNIILLJJMM(}uxx~~++ }@r~~ ".. $
.1r   