U
    KºcÌ  ã                   @   s    d dl Z d dlmZ d dlmZ e jjZ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e jjjdœdd„Zdd	„ Zd
d„ Zdd„ ZdS )é    N)Útree_flattenc                 C   s   t | jdƒr| jjS | jS )NÚoverloadpacket)ÚhasattrÚtargetr   )Únode© r   ú@/tmp/pip-unpacked-wheel-gikjz4vx/functorch/_src/compile_utils.pyÚget_aten_target	   s    r	   )Úfx_gc                    s  t  ¡ }i ‰ i }i }| jD ]ò}|jdksH|jdksH|jdksHt|ƒtkrf| |‡ fdd„¡}|ˆ |< q‡ fdd„}||jƒ\}}||jƒ\}	}
|j	|||	|
dœ}t
||	fƒ}|j	|f}||k}|rÜ|| |krÜ|| ˆ |< q| |‡ fd	d„¡}|ˆ |< |s|||< |||< q|S )
NÚplaceholderÚoutputZget_attrc                    s   ˆ |  S ©Nr   ©Úx©Úenvr   r   Ú<lambda>   ó    zfx_graph_cse.<locals>.<lambda>c                    sV   t | ƒ\} }tt| ƒƒD ]0}| | }t|tjjjƒr|ˆ krˆ | | |< qt| ƒ|fS r   )	r   ÚrangeÚlenÚ
isinstanceÚtorchÚfxr   ÚNodeÚtuple)Úarg_listÚspecÚiÚvr   r   r   Ú
substitute$   s    z fx_graph_cse.<locals>.substitute)r   ÚargsÚ	args_specÚkwargsÚkwargs_specc                    s   ˆ |  S r   r   r   r   r   r   r   =   r   )r   ÚGraphÚnodesÚopr	   Úrand_opsZ	node_copyr    r"   r   Úhash)r
   Z	new_graphZhash_envZ	token_mapÚnZnew_noder   r    r!   r"   r#   ÚtokenZhash_argZhash_valZhash_val_in_hash_envr   r   r   Úfx_graph_cse   s6    
*
 ÿ

r+   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_overloadsF   s    r-   c                 C   s   t tdd„ | jƒƒS )Nc                 S   s
   | j dkS )Nr   )r&   r   r   r   r   r   T   r   z"get_placeholders.<locals>.<lambda>)ÚlistÚfilterr%   )r,   r   r   r   Úget_placeholdersS   s    r0   c                 C   s8   | j D ]$}|jdkrt|jd ƒd   S qtdƒ‚d S )Nr   r   zNo output node found)r%   r&   r   r    ÚAssertionError)r,   r   r   r   r   Úget_outputsV   s    

r2   )r   Ztorch.fxr   Ztorch.utils._pytreer   ÚopsZatenr	   ZdropoutZ_fused_dropoutZ_standard_gammaZ	bernoulliZmultinomialZnative_dropoutÚnormalZpoissonZbinomialZrreluZ	rand_likeZrandÚrandintZrandnZrandpermr'   r,   r$   r+   r-   r0   r2   r   r   r   r   Ú<module>   s,            ý0