o
    I&i                     @   s  d dl Z d dlZd dlmZmZ d dlZd dlmZ d dl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" ee#dZ$dd Z%G dd dZ&da'e j(dd Z)e j(dd Z*dS )    N)ListOptional)	call_hookGetItemSourceLocalSource)counterslazy_format_graph_code)getArtifactLoggerclone_preserve_strides)FakeTensorMode)GraphModule)	decomposedisable_autocast_cachedisable_proxy_modes_tracingfetch_tensor_proxyProxyTorchDispatchModePythonKeyTracertrack_tensor_tree)
DimDynamicShapeEnv)Proxycompiled_autogradc                 C   s   | d urt | S | S Nr   )x r   JC:\wamp64\www\opt\env\Lib\site-packages\torch/_dynamo/compiled_autograd.pymaybe_clone   s   r   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d ZdefddZdd Zdd Zdd Zdd Zdd Zdd ZdS )AutogradCompilerInstancereturnNc                 C   sT   || _ t | _| jj| _t | _tdd| jd| _t	 | _
t| j
d| _d | _d S )NT)Zallow_fallback_kernelsZallow_non_fake_inputs	shape_envZsymbolic)compiler_fn
contextlib	ExitStackstackcloser   r!   r   fake_tensor_moder   	fx_tracerr   
proxy_modehooks_proxy)selfr"   r   r   r   __init__#   s   


z!AutogradCompilerInstance.__init__c                 C   s    t |tjsJ | jj||dS )N)source)
isinstancetorchTensorr'   Zfrom_tensor)r+   r   r-   r   r   r   	wrap_fake1   s   z"AutogradCompilerInstance.wrap_fakec                 C   s   t t| |S r   r   )nameidxr   r   r   r-   5   s   zAutogradCompilerInstance.sourceinputssizesc                    s6  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dd ||fS )Nr   Zcaptures   )Z
tracer_clsplaceholderr4   r   r5   hooksc              	      s$   g | ]\}}  | d |qS )r4   )r1   r-   ).0r3   r   r+   r   r   
<listcomp>C   s    z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>c                       g | ]} | qS r   r   r9   i)
args_proxyr   r   r;   G       c              	      s*   g | ]\}} j | d |tjqS )r5   )r!   Z$create_unspecified_symint_and_symbolr-   r   ZDYNAMIC)r9   r3   valr:   r   r   r;   K   s    
T)Zenable_current)r   r/   nnModuler(   rootfxZGraphr   graphZtensor_attrscreate_proxyr*   	enumeraterangelenbind_tensors_to_proxiesr%   enter_contextr   r'   r)   Zsym_moder   r   )r+   r4   r5   Zsizes_proxyproxiesr   )r?   r+   r   begin_capture9   s.   

z&AutogradCompilerInstance.begin_capturec                    s(    j dt|g fdd|D R i S )Ncall_functionc                       g | ]}  |qS r   to_proxyr9   r   r:   r   r   r;   d       z<AutogradCompilerInstance.proxy_call_hook.<locals>.<listcomp>)r(   rG   r   )r+   hookargsr   r:   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   )r*   rW   r   r   rK   )r+   r4   hook_idr>   rU   proxyr   r   r   tensor_pre_hooki   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   r   rS   r   r   r   r;   }   r@   z5AutogradCompilerInstance.pre_hook.<locals>.<listcomp>r*   rW   r   rK   )r+   r4   rX   rU   rM   r   r   r   pre_hooku   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\   rS   r   r   r   r;      r@   z6AutogradCompilerInstance.post_hook.<locals>.<listcomp>r]   )r+   outputsr4   rX   rU   rM   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   )r.   r/   r0   r*   rW   r   r   rK   )r+   inputrX   rU   rM   r   r   r   post_acc_grad_hook   s   


z+AutogradCompilerInstance.post_acc_grad_hookc              	   C   s\   | j   | jdd| j| |fi  t| jj| jjd}t	
dtd| | |S )NoutputZCompiledAutogradz%szCompiled autograd graph)r%   r&   r(   Zcreate_nodeZ
create_argrR   r   rD   rF   compiled_autograd_loginfor	   r"   )r+   r_   rF   r   r   r   end_capture   s   


z$AutogradCompilerInstance.end_capturec                    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                    rP   r   rQ   rS   r:   r   r   r;      rT   z5AutogradCompilerInstance.to_proxy.<locals>.<listcomp>c                 3   s    | ]}  |V  qd S r   rQ   rS   r:   r   r   	<genexpr>   s    z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>)	r.   listtupler/   r0   ZSymIntr   r(   rY   )r+   tr   r:   r   rR      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                    r<   r   r   r=   rM   r   r   r;      r@   zDAutogradCompilerInstance.bind_tensors_to_proxies.<locals>.<listcomp>)ZconstantZtracer)r.   r/   rE   r   rI   rJ   r   r(   )r+   ZtensorsrM   r   rk   r   rK      s   z0AutogradCompilerInstance.bind_tensors_to_proxies)r    N)__name__
__module____qualname__r,   r1   staticmethodr   r-   r   r/   r0   intrN   rW   rZ   r^   r`   rb   rf   rR   rK   r   r   r   r   r   "   s    
%
r   Fc              	   c   s    t jjjtt| }daz(t j	
d d V  W d    n1 s$w   Y  W |s.dat jjj| d S |s<dat jjj| w )NTF)r/   _C_dynamor   set_autograd_compiler	functoolspartialr   compiled_autograd_enabledZautogradZset_multithreading_enabled)r"   priorr   r   r   enable   s    

rx   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/   rq   rr   r   rs   rv   )rw   r   r   r   disable   s   ry   )+r#   rt   typingr   r   r/   Ztorch._dynamo.external_utilsr   Ztorch._dynamo.sourcer   r   Ztorch._dynamo.utilsr   r	   Ztorch._loggingr
   Ztorch._prims_commonr   Ztorch._subclassesr   Ztorch.fxr   Z"torch.fx.experimental.proxy_tensorr   r   r   r   r   r   r   Z%torch.fx.experimental.symbolic_shapesr   r   Ztorch.fx.proxyr   rl   rd   r   r   rv   contextmanagerrx   ry   r   r   r   r   <module>   s.    $	
 
