a
    d                     @   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   j/var/www/html/stable-diffusion-webui/venv/lib/python3.9/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   s|  | j d urvt|t| jks J 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 d    n1 s0    Y  | j   | jD ]}|| | j|  qtt| j	S tj }|tj  tj| | j| }W d    n1 sT0    Y  tj | d| _
|S d S )Nc                 S   s   g | ]}|  qS r   r   .0xr   r   r   
<listcomp><       z,CudaGraphModule.__call__.<locals>.<listcomp>T)graphlenstatic_inputs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
u rlq |jj}t|jD ]p\}}|t|jk r|j| }	n|j|jvrq~|j|j }	d}
|jr|jjrd}
|
r~||t||	j  O }q~q |S )	Nc                 S   s   d| v r| d S | d S )NvalZfake_resultr   )metar   r   r   meta_fkW   s    z%find_input_mutations.<locals>.meta_fkr   placeholderr	   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    




rQ   c                 C   s\   | j jD ]N}|jdkr|jr J | |j}| |j t|j }| |jt	|| qd S )NZcall_module)
r"   rB   rC   rK   Zget_submodulerE   Zdelete_submodulerQ   Zadd_submoduler   )r   rN   Zsubmodr   r   r   r   apply_cuda_graphs{   s    


rR   c                 C   s   t | |} t|  | S r   )r   rR   )modelrM   r   r   r   
cudagraphs   s    
rT   )Zfw_compilerZbw_compiler)rJ   Zcompiler_fnTc                    s  t |ttfsJ dd |D tj  tj }|tj  tj	| | |  W d   n1 sn0    Y  |  tj | tj  tj
 tjj|d |  W d   n1 s0    Y  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       r!   z$cudagraphs_inner.<locals>.<listcomp>N)r/   c                     sT   t t | ksJ t| D ]\}}|| q   rLdd D S S d S )Nc                 S   s   g | ]}|  qS r   r   r   r   r   r   r       r!   z1cudagraphs_inner.<locals>.run.<locals>.<listcomp>)r#   r%   r&   r'   )Z
new_inputsr1   r2   copy_outputsr"   r$   r(   r   r   run   s    zcudagraphs_inner.<locals>.run)r   listtupler   r*   Zsynchronizer,   r-   r.   r/   r+   r"   )rS   rM   rV   r/   rW   r   rU   r   cudagraphs_inner   s"    

&

&
rZ   )T)loggingrF   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   rQ   rR   rT   Zaot_cudagraphsrZ   r   r   r   r   <module>   s(   
<%