o
    "iu9                     @   s  d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	Z	m
Z
 d dlmZmZ d dlmZmZmZ d dlmZ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mZm Z m!Z!m"Z"m#Z#m$Z$ d dl%m&Z&m'Z' d dl(m)Z)m*Z* d dl+m,Z, erd dl-m.Z. ee/dZ0ee/dZ1dd Z2de3ddfddZ4dd Z5dd Z6G dd dZ7da8d a9e j:dd  Z;e j:d!d" Z<d%d#d$Z=dS )&    N)DictListOptionalTYPE_CHECKING)call_backward	call_hookGetItemSourceLocalSource)counterslazy_format_graph_codeset_locals_to_steal)getArtifactLoggertrace_structuredclone_preserve_strides)FakeTensorMode)GraphModule)BackwardState)	decomposedisable_autocast_cachedisable_proxy_modes_tracingfetch_object_proxyProxyTorchDispatchModePythonKeyTracertrack_tensor_tree)
DimDynamicShapeEnv)preserve_node_metaset_stack_trace)CapturedTraceback)Proxycompiled_autogradcompiled_autograd_verbosec                   C   s   t jjjdS )Nr#   )torch_logging	_internal	log_stateis_artifact_enabled r)   r)   ]/home/ubuntu/SoloSpeech/.venv/lib/python3.10/site-packages/torch/_dynamo/compiled_autograd.py snapshot_verbose_logging_enabled#   s   
r+   msgreturnc                 C   s   t |  d S N)verbose_logdebug)r,   r)   r)   r*   cpp_verbose_log_fn)   s   r1   c                   C   s   t jjjjS r.   )r$   	_inductorconfigtriton
cudagraphsr)   r)   r)   r*   snapshot_cudagraph_enabled-   s   r6   c                 C   s   | d urt | S | S r.   r   )xr)   r)   r*   maybe_clone1   s   r8   c                   @   s   e Zd Zd*ddZdd ZedefddZd	ee	j
 d
ee fddZdefddZdd ZdefddZdd Zdd Zdd Zdee fddZdd Zdd  Zd!d" Zd#d$ Zd%efd&d'Zd(d) ZdS )+AutogradCompilerInstancer-   Nc                 C   sT   || _ t | _| jj| _t | _tdd| jd| _t	 | _
t| j
d| _d | _d S )NT)allow_fallback_kernelsallow_non_fake_inputs	shape_envsymbolic)compiler_fn
contextlib	ExitStackstackcloser   r<   r   fake_tensor_moder   	fx_tracerr   
proxy_modehooks_proxy)selfr>   r)   r)   r*   __init__8   s   


z!AutogradCompilerInstance.__init__c                 C   s    t |tjsJ | jj||dS )N)source)
isinstancer$   TensorrC   from_tensor)rG   r7   rI   r)   r)   r*   	wrap_fakeF   s   z"AutogradCompilerInstance.wrap_fakec                 C   s   t t| |S r.   r   )nameidxr)   r)   r*   rI   J   s   zAutogradCompilerInstance.sourceinputssizesc                    s2  t d d  d7  < tj j_tjjtdj_	i j_
jdddi  jdddi }jdd	di _fd
dt|D } fddtt|D }|| fddt|D }|| jti  jj jjj jj jt  jt  ||fS )Nr"   captures   )
tracer_clsplaceholderrP   r)   rQ   hooksc              	      s$   g | ]\}}  | d |qS )rP   )rM   rI   ).0rO   r7   rG   r)   r*   
<listcomp>X   s    z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>c                       g | ]} | qS r)   r)   rW   i)
args_proxyr)   r*   rY   \       c              	      s*   g | ]\}} j | d |tjqS )rQ   )r<   $create_unspecified_symint_and_symbolrI   r   DYNAMIC)rW   rO   valrX   r)   r*   rY   `   s    
)r   r$   nnModulerD   rootfxGraphr   graphtensor_attrscreate_proxyrF   	enumeraterangelenbind_tensors_to_proxiesrA   enter_contextr   rC   rE   sym_moder   r   )rG   rP   rQ   sizes_proxyproxiesr)   )r]   rG   r*   begin_captureN   s.   

z&AutogradCompilerInstance.begin_capturebackward_idxc              
   C   s   | j d usJ | j | }| jjdt|| |g| |R i d}t 4 g }|D ]}|d u r5|d  q)|\}	}
}}|tj|||	|
d q)| 	|| W d    t
|S 1 s[w   Y  t
|S )Ncall_function)kindtargetargskwargs)sizedtypelayoutdevice)rF   rD   ri   r   to_proxyr   appendr$   emptyrm   tuple)rG   rP   output_metadatassaved_tensorsrs   backward_c_functionrq   grad_insoutput_metadatar{   r|   rz   ry   r)   r)   r*   proxy_call_backwards   s6   


z,AutogradCompilerInstance.proxy_call_backwardc                    s(    j dt|g fdd|D R i S )Nrt   c                       g | ]}  |qS r)   r}   rW   r7   rX   r)   r*   rY          z<AutogradCompilerInstance.proxy_call_hook.<locals>.<listcomp>)rD   ri   r   )rG   hookrw   r)   rX   r*   proxy_call_hook   s   z(AutogradCompilerInstance.proxy_call_hookr\   c                 C   sx   | j d usJ | j | }| ||| }t  t|| ||< | || g|g W d    |S 1 s5w   Y  |S r.   )rF   r   r   r8   rm   )rG   rP   hook_idr\   r   proxyr)   r)   r*   tensor_pre_hook   s   

z(AutogradCompilerInstance.tensor_pre_hookc                 C   sj   | j d usJ | j | }| ||}t  dd |D }| || W d    |S 1 s.w   Y  |S )Nc                 S      g | ]}t |qS r)   r8   r   r)   r)   r*   rY      r^   z5AutogradCompilerInstance.pre_hook.<locals>.<listcomp>rF   r   r   rm   )rG   rP   r   r   rq   r)   r)   r*   pre_hook   s   

z!AutogradCompilerInstance.pre_hookc                 C   sl   | j d usJ | j | }| |||}t  dd |D }| || W d    |S 1 s/w   Y  |S )Nc                 S   r   r)   r   r   r)   r)   r*   rY      r^   z6AutogradCompilerInstance.post_hook.<locals>.<listcomp>r   )rG   outputsrP   r   r   rq   r)   r)   r*   	post_hook   s   

z"AutogradCompilerInstance.post_hookc                 C   sv   t |tjsJ | jd usJ | j| }| ||}t  t|g}| || W d    |S 1 s4w   Y  |S r.   )rJ   r$   rK   rF   r   r   r8   rm   )rG   inputr   r   rq   r)   r)   r*   post_acc_grad_hook   s   


z+AutogradCompilerInstance.post_acc_grad_hookc                 C   s2  i }d}t |j}|d jdksJ |d }t |j }d}|| |d ks)J |t| d }|| |d ks;J t|D ]>\}	}
|sQ|
jd jj	dkrQd	}q?|
jd jj	d
k}t|
jd 
 dk}|r}|r}t |
j }tdd |D r}|
||	< q?|r| D ]}
|
jd  |
jd< qt | S g S )NFr   rP      rS   ra   cudaTcpuc                 s   s,    | ]}t |jtjjo|jjd v V  qdS ))primsatenN)rJ   rv   r$   _ops
OpOverload	namespace)rW   userr)   r)   r*   	<genexpr>   s    

zDAutogradCompilerInstance.move_graph_nodes_to_cuda.<locals>.<genexpr>)listnodesrv   userskeysrl   rj   metar|   typery   allvaluesr   )rG   rg   to_movehas_cuda_inputsr   rP   inputs_usersfirst_getitem_idxlast_getitem_idxr\   nodeis_cpu	is_scalar
node_usersr)   r)   r*   move_graph_nodes_to_cuda   s8   
z1AutogradCompilerInstance.move_graph_nodes_to_cudac              	      s   | j   | jdd| j| |fi  |   g t r&| | jj	t
| jj| jj	d t dg tdtd  tdtd dd td fd	d
d fdd}||  fS )NoutputCompiledAutogradrP   z%szCompiled autograd graphT)include_devicecompiled_autograd_graphc                      s    j ddS )NF)print_output)print_readabler)   )rg   r)   r*   <lambda>  s    z6AutogradCompilerInstance.end_capture.<locals>.<lambda>)
payload_fnc                    s&    D ]
}||   ||< q| |||S r.   )r   )compiled_fnrP   rQ   rV   r\   )runtime_inputs_to_mover)   r*   runtime_wrapper   s   z=AutogradCompilerInstance.end_capture.<locals>.runtime_wrapper)rA   rB   rD   create_node
create_argr}   reorder_accumulate_grad_nodesr6   r   rg   r   rd   r   compiled_autograd_loginfor   r/   r0   r   r>   )rG   r   r   r)   )rg   r   r*   end_capture  s<   


z$AutogradCompilerInstance.end_capturec                 C   sL   | j jjdtjjjjdD ]}t|j	}||j
ur#|jdkr#|| qdS )a  
        Usage of AOTAutograd causes all the accumulate_grad_ nodes to get pushed to the end of
        the graph.  This differs from eager mode, which schedules them as soon as possible. This
        pass attempts to reorder the graph to mimic eager behavior.
        rt   )oprv   rU   N)rD   rg   
find_nodesr$   opsinductoraccumulate_grad_defaultmaxrw   prevr   r~   )rG   r   argr)   r)   r*   r   (  s   


z6AutogradCompilerInstance.reorder_accumulate_grad_nodesc                    sn   |d u rd S t |tr fdd|D S t |tr$t fdd|D S t |tjtjfs/J t j|jS )Nc                    r   r)   r   r   rX   r)   r*   rY   9  r   z5AutogradCompilerInstance.to_proxy.<locals>.<listcomp>c                 3   s    | ]}  |V  qd S r.   r   r   rX   r)   r*   r   ;  s    z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>)	rJ   r   r   r$   rK   SymIntr   rD   r   )rG   tr)   rX   r*   r}   5  s   

z!AutogradCompilerInstance.to_proxyc                    sR   t  tjjr fddtt|D  t|t ksJ t| d | jd d S )Nc                    rZ   r)   r)   r[   rq   r)   r*   rY   A  r^   zDAutogradCompilerInstance.bind_tensors_to_proxies.<locals>.<listcomp>constanttracer)rJ   r$   re   r!   rk   rl   r   rD   )rG   tensorsrq   r)   r   r*   rm   ?  s   z0AutogradCompilerInstance.bind_tensors_to_proxiesindexc                 C   s4   | j d usJ | j | }t }t||d | jd |S )Nr   )rF   r   r   rD   )rG   r   r   bw_stater)   r)   r*   bind_backward_stateE  s
   
z,AutogradCompilerInstance.bind_backward_statec                 C   s8   t   d }| d| d}|d|}t| d S )Nr   z (NodeCall )z:raw_stack_trace = CapturedTraceback.extract().format()[-1])r    extractformatreplacer   )rG   	node_name
node_indexraw_stack_tracenew_codenew_stack_tracer)   r)   r*   set_node_originL  s   z(AutogradCompilerInstance.set_node_originr-   N)__name__
__module____qualname__rH   rM   staticmethodr	   rI   r   r$   rK   intrr   r   r   r   r   r   r   r   r   r   r}   rm   r   r   r)   r)   r)   r*   r9   7   s*    
%
#)'
r9   Fc              	   c   s    t jjjtt| }t rt jjj	t
 datd7 az,t jd d V  W d    n1 s3w   Y  W td8 a|sAdat jjj| d S td8 a|sSdat jjj| w )NTrS   F)r$   _C_dynamor"   set_autograd_compiler	functoolspartialr9   r+   set_verbose_loggerr1   compiled_autograd_enabledcompiled_autograd_enabled_countautogradset_multithreading_enabled)r>   priorr)   r)   r*   enableg  s*   

r   c               	   c   sV    t jjjd } dazd V  W | rdat jjj|  d S | r"dat jjj|  w )NFT)r$   r   r   r"   r   r   )r   r)   r)   r*   disable{  s   r   c                  C   s4   d} t dksJ tjjjd  tjjjd  d S )NFr   )r   r$   r   r   r"   r   r   )compiled_autograd_enabler)   r)   r*   reset  s   r   r   )>r?   r   typingr   r   r   r   r$   torch._dynamo.external_utilsr   r   torch._dynamo.sourcer	   r
   torch._dynamo.utilsr   r   r   torch._loggingr   r   torch._prims_commonr   torch._subclassesr   torch.fxr   %torch.fx.experimental._backward_stater   "torch.fx.experimental.proxy_tensorr   r   r   r   r   r   r   %torch.fx.experimental.symbolic_shapesr   r   torch.fx.tracebackr   r   torch.utils._tracebackr    torch.fx.proxyr!   r   r   r/   r+   strr1   r6   r8   r9   r   r   contextmanagerr   r   r   r)   r)   r)   r*   <module>   sD   $	

   

