o
    h                     @   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)tree_mapc                   @   s"   e Zd ZdejjdefddZdS )CudaGraphsSupportnodereturnc                    s   |j tvrdS |jtjjjjfv rdS |jtj	fv rdS d dd } fdd}|j
D ]
}t|||j q+t|||j   S )NFTc                 S   s   d| v r| d S | d S )Nvalfake_result )metar   r   Y/var/www/html/ai/venv/lib/python3.10/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 d S d S )NcudaT)
isinstancetorchTensordevicetype)tfound_not_cudar   r   find_not_cuda   s   z:CudaGraphsSupport.is_node_supported.<locals>.find_not_cuda)opr   targetr   opsatenembedding_dense_backwarddefaultoperatorgetitemall_input_nodesr   r   )self
submodulesr   r   r   nr   r   r   is_node_supported   s   

z#CudaGraphsSupport.is_node_supportedN)__name__
__module____qualname__r   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)allows_single_node_partition)r   	propagater   r   propose_partitionsfuse_partitions)gminputssupported_opspartitioner
partitionsfused_graphr   r   r   partition_cudagraphs*   s   
r7   )r   !torch.fx.passes.infra.partitionerr    torch.fx.passes.operator_supportr   torch.fx.passes.tools_commonr    torch.fx.passes.fake_tensor_propr   torch.utils._pytreer   r    r   r7   r   r   r   r   <module>   s     