U
    yh                     @   sh   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 d dlZG dd deZd	d
 ZdS )    N)CapabilityBasedPartitioner)OperatorSupport)CALLABLE_NODE_OPS)FakeTensorProp)_pytreec                   @   s    e Zd ZejjedddZdS )CudaGraphsSupport)nodereturnc                    s   |j tkrdS |jtjjjjfkr&dS |jtj	fkr8dS d dd } fdd}|j
D ]}t|||j qVt|||j   S )NFTc                 S   s   d| kr| d S | d S )NvalZfake_result )metar   r   U/var/www/html/venv/lib/python3.8/site-packages/torch/fx/passes/backends/cudagraphs.pymeta_fk   s    z4CudaGraphsSupport.is_node_supported.<locals>.meta_fkc                    s    t | tjr| jjdkrd d S )NcudaT)
isinstancetorchZTensorZdevicetype)tZfound_not_cudar   r   find_not_cuda   s    z:CudaGraphsSupport.is_node_supported.<locals>.find_not_cuda)opr   targetr   ZopsZatenZembedding_dense_backwarddefaultoperatorgetitemZall_input_nodespytreeZ	tree_map_r   )selfZ
submodulesr   r   r   nr   r   r   is_node_supported   s    

z#CudaGraphsSupport.is_node_supportedN)__name__
__module____qualname__r   ZfxNodeboolr   r   r   r   r   r      s   r   c                 C   s8   t | j|  t }t| |dd}| }||}|S )z
    Partition an FX graph into sub-GraphModules that can be validly run under
    CUDA graphs.  For a subgraph to be runnable under CUDA, all of the operations
    must involve CUDA tensors only/
    T)Zallows_single_node_partition)r   	propagater   r   Zpropose_partitionsZfuse_partitions)ZgminputsZsupported_opsZpartitionerZ
partitionsZfused_graphr   r   r   partition_cudagraphs+   s    
r&   )r   Z!torch.fx.passes.infra.partitionerr   Z torch.fx.passes.operator_supportr   Ztorch.fx.passes.tools_commonr   Z torch.fx.passes.fake_tensor_propr   Ztorch.utilsr   r   r   r   r&   r   r   r   r   <module>   s    