o
    Zh                     @   s   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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dZ"dd Z#dd Z$dd Z%dS )    )CallableN)StorageWeakRef)_pytree)tree_flattennodereturnc                 C   s   t | jdr
| jjS | jS )Noverloadpacket)hasattrtargetr   r    r   M/var/www/auris/lib/python3.10/site-packages/torch/_functorch/compile_utils.pyget_aten_target   s   r   fx_gc                    s  t  }i i }i }ddlm}m} ||  t| jd }|jdks$J dt jdt	fdd  fd	d
|j
D  fdd
| jD }| jD ]}|jdksh|jdksh|jdksht|tv sht|tju sh||v rw||fdd}	|	|< qFfdd}
|
|j\}}|
|j\}}|j||||d}ttdd |D tdd |D f}|j|f}||v }d}|r|| |kr|| }|||r||< qFd}||fdd}	|	|< |s|s|	||< |||< qF|S )Nr   )compute_mutation_region_idssame_mutation_regionsoutputr   r   c                 S   sJ   d| j vst| j d tjsdS z
| j d   W dS  ty$   Y dS w )zGWe can evaluate only nodes that represent tensors with defined storage.valFT)meta
isinstancetorchZTensoruntyped_storageNotImplementedErrorr   r   r   r   checkable_node=   s   z$fx_graph_cse.<locals>.checkable_nodec                    s&   h | ]} |rt |jd   qS r   r   r   r   .0n)r   r   r   	<setcomp>I   s    zfx_graph_cse.<locals>.<setcomp>c                    s.   h | ]} |rt |jd   v r|qS r   r   r   )r   output_storagesr   r   r    N   s    placeholderZget_attrc                        |  S Nr   xenvr   r   <lambda>c       zfx_graph_cse.<locals>.<lambda>c                    sv   t | \} }tt| D ](}| | }t|tjjjr$| v r$ | | |< t|tjtj	tj
fr4|j| |< qt| |fS r$   )r   rangelenr   r   fxr   NodeZSymBoolZSymIntZSymFloattuple)arg_listspecivr'   r   r   
substituteh   s   
z fx_graph_cse.<locals>.substitute)r
   args	args_speckwargskwargs_specc                 s   s    | ]	}|t |fV  qd S r$   )type)r   ar   r   r   	<genexpr>   s    zfx_graph_cse.<locals>.<genexpr>FTc                    r#   r$   r   r%   r'   r   r   r)      r*   )r-   GraphZtorch._inductor.pattern_matcherr   r   listnodesopr.   boolZall_input_nodesr   rand_opsatenemptyZ	node_copyr5   r7   r
   hashr/   )r   Z	new_graphZhash_env	token_mapr   r   Zoutput_nodeZnodes_that_alias_outputsr   new_noder4   r5   r6   r7   r8   tokenZhash_argZhash_valZhash_val_in_hash_envZoverwrite_due_to_mutationZduplicate_n_prevr   )r   r(   r!   r   fx_graph_cse*   sh   






"

rH   c                 C   s4   | j jD ]}t|jtjjr|jj|_q|   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)	graphr>   r   r
   r   Z_opsZ
OpOverloadr   Z	recompile)Zgmr   r   r   r   strip_overloads   s
   
rJ   c                 C   s   | j ddS )Nr"   r?   )
find_nodes)rI   r   r   r   get_placeholders   s   rM   c                 C   s,   | j ddD ]}t|jd   S td)Nr   rK   r   zNo output node found)rL   pytreeZtree_leavesr5   AssertionError)rI   r   r   r   r   get_outputs   s   rP   )&typingr   r   Ztorch.fxr-   Z torch.multiprocessing.reductionsr   Ztorch.utilsr   rN   Ztorch.utils._pytreer   opsrB   r.   r   ZdropoutZ_fused_dropoutZ_standard_gammaZ	bernoulliZmultinomialZnative_dropoutnormalZpoissonZbinomialZrreluZ	rand_likeZrandrandintZrandnZrandpermrA   rI   r<   rH   rJ   rM   rP   r   r   r   r   <module>   s8   r