U
    ªÊ¹jø  ã                   @   sä   d dl Z d dlZd dlmZ 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 d dlmZ d	d
lmZ d	dlmZ e  e¡Zdd„ ZG dd„ deƒZdd„ Zdd„ Zdd„ Zeeed�Zeded� ddd„ZdS )é    N)Údefaultdict)ÚSet)ÚGraphModule)Úpartition_cudagraphs)ÚStorageWeakRef)ÚModule)Útree_mapé   )Úaot_autograd)Úregister_backendc                 C   s   t | tjƒr|  ¡ S | S d S ©N)Ú
isinstanceÚtorchZTensorÚclone)Út© r   ú`/var/www/html/bwave/python/venv/lib/python3.8/site-packages/torch/_dynamo/backends/cudagraphs.pyÚcloner   s    r   c                       sJ   e Zd ZU eed< ee ed< ‡ fdd„ZdZdZ	dZ
dZdd„ Z‡  ZS )	ÚCudaGraphModuleÚgmÚmutated_inputsc                    s   t ƒ  ¡  || _|| _d S r   )ÚsuperÚ__init__r   r   )Úselfr   r   ©Ú	__class__r   r   r      s    
zCudaGraphModule.__init__FNc              	   G   sP  | j d k	rvt|ƒt| jƒks t‚t| j|ƒD ]\}}| |¡ q,| j  ¡  | jD ]}||  | j| ¡ qPtt	| j
ƒS | jrödd„ |D ƒ| _tj ¡ | _ tj  | j ¡� | j| jŽ | _
W 5 Q R X | j  ¡  | jD ]}||  | j| ¡ qÐtt	| j
ƒS tj ¡ }| tj ¡ ¡ tj |¡� | j|Ž }W 5 Q R X tj ¡  |¡ d| _|S d S )Nc                 S   s   g | ]}|  ¡ ‘qS r   ©r   ©Ú.0Úxr   r   r   Ú
<listcomp><   s     z,CudaGraphModule.__call__.<locals>.<listcomp>T)ÚgraphÚlenÚstatic_inputsÚAssertionErrorÚzipÚcopy_Úreplayr   r   r   Ústatic_outputsÚ	warmed_upr   ÚcudaÚ	CUDAGraphr   ÚStreamÚwait_streamÚcurrent_streamÚstream)r   ÚargsÚdstÚsrcÚir/   Úrr   r   r   Ú__call__,   s0    





zCudaGraphModule.__call__)Ú__name__Ú
__module__Ú__qualname__r   Ú__annotations__r   Úintr   r)   r!   r#   r(   r5   Ú__classcell__r   r   r   r   r      s   
r   c                 C   sö   dd„ }t tƒ}d}tƒ }| jD ]Ð}|jdkrT|t||jƒ ¡ ƒ  |¡ |d7 }q |jdkr |jt	j
krlq |jj}t|jƒD ]p\}}|t|jƒk r |j| }	n|j|jkr®q~|j|j }	d}
|jrÐ|jjrÐd}
|
r~||t||	jƒ ¡ ƒ O }q~q |S )	Nc                 S   s   d| kr| d S | d S )NÚvalZfake_resultr   )Úmetar   r   r   Úmeta_fkW   s    z%find_input_mutations.<locals>.meta_fkr   Úplaceholderr	   Zcall_functionFT)r   ÚsetÚnodesÚopr   r=   Z_typed_storageÚaddÚtargetÚoperatorÚgetitemZ_schemaÚ	enumerateÚ	argumentsr"   r0   ÚnameÚkwargsZ
alias_infoZis_write)Úgr>   ÚinputsZ	input_idxr   ÚnZschemar3   ÚargÚargumentZmut_argr   r   r   Úfind_input_mutationsV   s6    



ÿ
rP   c                 C   s\   | j jD ]N}|jdkr|jr t‚|  |j¡}|  |j¡ t|j ƒ}|  	|jt
||ƒ¡ qd S )NZcall_module)r!   rA   rB   rJ   r$   Zget_submodulerD   Zdelete_submodulerP   Zadd_submoduler   )r   rM   Zsubmodr   r   r   r   Úapply_cuda_graphs{   s    


rQ   c                 C   s   t | |ƒ} t| ƒ | S r   )r   rQ   )ÚmodelrL   r   r   r   Ú
cudagraphs†   s    
rS   )Zfw_compilerZbw_compiler)rI   Zcompiler_fnTc              	      sî   t |ttfƒst‚ˆ r&dd„ |D ƒ‰nt|ƒ‰tj ¡  tj ¡ }| tj 	¡ ¡ tj 
|¡� | |Ž  W 5 Q R X | ¡  tj 	¡  |¡ tj ¡  tj ¡ ‰tjjˆ|d�� | ˆŽ ‰W 5 Q R X t ˆttfƒsÖˆf‰‡ ‡‡‡‡fdd„}|S )zBThis isn't registered as a backend, but is used in some benchmarksc                 S   s   g | ]}t  |¡‘qS r   )r   Z
zeros_liker   r   r   r   r    ˜   s     z$cudagraphs_inner.<locals>.<listcomp>)r/   c                     sX   t ˆƒt | ƒkst‚ˆ r6tˆ| ƒD ]\}}| |¡ q"ˆ ¡  ˆrPdd„ ˆD ƒS ˆS d S )Nc                 S   s   g | ]}|  ¡ ‘qS r   r   r   r   r   r   r    ´   s     z1cudagraphs_inner.<locals>.run.<locals>.<listcomp>)r"   r$   r%   r&   r'   )Z
new_inputsr1   r2   ©Úcopy_inputsÚcopy_outputsr!   r#   r(   r   r   Úrun­   s    zcudagraphs_inner.<locals>.run)r   ÚlistÚtupler$   r   r*   Zsynchronizer,   r-   r.   r/   r+   r!   )rR   rL   rV   rU   r/   rW   r   rT   r   Úcudagraphs_inner”   s&    



rZ   )TT)ÚloggingrE   Úcollectionsr   Útypingr   r   Ztorch.fxr   Z#torch.fx.passes.backends.cudagraphsr   Z torch.multiprocessing.reductionsr   Ztorch.nnr   Ztorch.utils._pytreer   Úcommonr
   Úregistryr   Ú	getLoggerr6   Úlogr   r   rP   rQ   rS   Zaot_cudagraphsrZ   r   r   r   r   Ú<module>   s(   
<%