o
    h                     @   s   d dl Z d dlZd dlmZ d dlZd dl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 ZG dd dZe jdd Z e jdd Z!dS )    N)List)	call_hookGetItemSourceLocalSource)countersclone_preserve_strides)FakeTensorMode)GraphModule)	decomposedisable_autocast_cachedisable_proxy_modes_tracingfetch_tensor_proxyProxyTorchDispatchModePythonKeyTracertrack_tensor_tree)
DimDynamicShapeEnvc                 C   s   | d urt | S | S Nr   )x r   U/var/www/html/ai/venv/lib/python3.10/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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S )AutogradCompilerInstancereturnNc                 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__   s   


z!AutogradCompilerInstance.__init__c                 C   s    t |tjsJ | jj||dS )N)source)
isinstancetorchTensorr%   from_tensor)r)   r   r+   r   r   r   	wrap_fake-   s   z"AutogradCompilerInstance.wrap_fakec                 C   s   t t| |S r   r   )nameidxr   r   r   r+   1   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 )Ncompiled_autogradcaptures   )
tracer_clsplaceholderr3   r   r4   hooksc              	      s$   g | ]\}}  | d |qS )r3   )r0   r+   ).0r2   r   r)   r   r   
<listcomp>?   s    z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>c                       g | ]} | qS r   r   r;   i)
args_proxyr   r   r=   C       c              	      s*   g | ]\}} j | d |tjqS )r4   )r   $create_unspecified_symint_and_symbolr+   r   DYNAMIC)r;   r2   valr<   r   r   r=   G   s    
T)enable_current)r   r-   nnModuler&   rootfxGraphr   graphtensor_attrscreate_proxyr(   	enumeraterangelenbind_tensors_to_proxiesr#   enter_contextr   r%   r'   sym_moder   r   )r)   r3   r4   sizes_proxyproxiesr   )rA   r)   r   begin_capture5   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r;   r   r<   r   r   r=   `       z<AutogradCompilerInstance.proxy_call_hook.<locals>.<listcomp>)r&   rN   r   )r)   hookargsr   r<   r   proxy_call_hookZ   s   z(AutogradCompilerInstance.proxy_call_hookr@   c                 C   sj   | j | }| ||| }t  t|| ||< | || g|g W d    |S 1 s.w   Y  |S r   )r(   r`   r   r   rR   )r)   r3   hook_idr@   r^   proxyr   r   r   tensor_pre_hooke   s   

z(AutogradCompilerInstance.tensor_pre_hookc                 C   s\   | j | }| ||}t  dd |D }| || W d    |S 1 s'w   Y  |S )Nc                 S      g | ]}t |qS r   r   r\   r   r   r   r=   w   rB   z5AutogradCompilerInstance.pre_hook.<locals>.<listcomp>r(   r`   r   rR   )r)   r3   ra   r^   rV   r   r   r   pre_hookp   s   

z!AutogradCompilerInstance.pre_hookc                 C   s^   | j | }| |||}t  dd |D }| || W d    |S 1 s(w   Y  |S )Nc                 S   rd   r   re   r\   r   r   r   r=      rB   z6AutogradCompilerInstance.post_hook.<locals>.<listcomp>rf   )r)   outputsr3   ra   r^   rV   r   r   r   	post_hook{   s   

z"AutogradCompilerInstance.post_hookc              	   C   sF   | j   | jdd| j| |fi  | t| jj| jj	dS )NoutputCompiledAutograd)
r#   r$   r&   create_node
create_argr[   r    r   rI   rL   )r)   rh   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                    rY   r   rZ   r\   r<   r   r   r=      r]   z5AutogradCompilerInstance.to_proxy.<locals>.<listcomp>c                 3   s    | ]}  |V  qd S r   rZ   r\   r<   r   r   	<genexpr>   s    z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>)	r,   listtupler-   r.   SymIntr   r&   rb   )r)   tr   r<   r   r[      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?   rV   r   r   r=      rB   zDAutogradCompilerInstance.bind_tensors_to_proxies.<locals>.<listcomp>)constanttracer)r,   r-   rJ   ProxyrP   rQ   r   r&   )r)   tensorsrV   r   rt   r   rR      s   z0AutogradCompilerInstance.bind_tensors_to_proxies)r   N)__name__
__module____qualname__r*   r0   staticmethodr+   r   r-   r.   intrW   r`   rc   rg   ri   rn   r[   rR   r   r   r   r   r      s    

%
r   c                 c   s`    t jjjtt| }t j	d d V  W d    n1 s!w   Y  t jjj| d S )NF)
r-   _C_dynamor5   set_autograd_compiler	functoolspartialr   autogradset_multithreading_enabled)r    priorr   r   r   enable   s   

r   c                  c   s,    t jjjd } d V  t jjj|  d S r   )r-   r~   r   r5   r   )r   r   r   r   disable   s   r   )"r!   r   typingr   r-   torch._dynamo.external_utilsr   torch._dynamo.sourcer   r   torch._dynamo.utilsr   torch._prims_commonr	   torch._subclassesr
   torch.fxr   "torch.fx.experimental.proxy_tensorr   r   r   r   r   r   r   %torch.fx.experimental.symbolic_shapesr   r   r   r   contextmanagerr   r   r   r   r   r   <module>   s&    $	 
	