U
    ©Ê¹jç  ã                   @   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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   úb/var/www/html/bwave/python/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_nodesr   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.utils._pytreer   r   r   r%   r   r   r   r   Ú<module>   s    