U
    9%e                     @   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 k	rt | S | S Nr   )x r   ^/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_dynamo/compiled_autograd.pymaybe_clone   s    r   c                   @   s   e Zd ZddddZdd Zedd Zeej	 ee
 d	d
dZdd Ze
dddZdd Zdd Zdd Zdd Zdd ZdS )AutogradCompilerInstanceN)returnc                 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t| jj||dS )N)source)
isinstancetorchTensorAssertionErrorr"   Z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_autogradZcaptures   )Z
tracer_clsplaceholderr0   r   r1   hooksc              	      s$   g | ]\}}  | d |qS )r0   )r-   r(   ).0r/   r   r&   r   r   
<listcomp>?   s   z:AutogradCompilerInstance.begin_capture.<locals>.<listcomp>c                    s   g | ]} | qS r   r   r6   i)
args_proxyr   r   r8   C   s     c              	      s*   g | ]"\}} j | d |tjqS )r1   )r   Z$create_unspecified_symint_and_symbolr(   r   ZDYNAMIC)r6   r/   valr7   r   r   r8   G   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&   r0   r1   Zsizes_proxyproxiesr   )r;   r&   r   begin_capture5   s.    

z&AutogradCompilerInstance.begin_capturec                    s&    j dt|f fdd|D i S )NZcall_functionc                    s   g | ]}  |qS r   to_proxyr6   r   r7   r   r   r8   `   s     z<AutogradCompilerInstance.proxy_call_hook.<locals>.<listcomp>)r#   rB   r   )r&   hookargsr   r7   r   proxy_call_hookZ   s    z(AutogradCompilerInstance.proxy_call_hook)r:   c              	   C   sT   | j | }| ||| }t * t|| ||< | || g|g W 5 Q R X |S r   )r%   rO   r   r   rF   )r&   r0   hook_idr:   rM   proxyr   r   r   tensor_pre_hooke   s    
z(AutogradCompilerInstance.tensor_pre_hookc              	   C   sF   | j | }| ||}t   dd |D }| || W 5 Q R X |S )Nc                 S   s   g | ]}t |qS r   r   rL   r   r   r   r8   w   s     z5AutogradCompilerInstance.pre_hook.<locals>.<listcomp>r%   rO   r   rF   )r&   r0   rP   rM   rH   r   r   r   pre_hookp   s    
z!AutogradCompilerInstance.pre_hookc              	   C   sH   | j | }| |||}t   dd |D }| || W 5 Q R X |S )Nc                 S   s   g | ]}t |qS r   rS   rL   r   r   r   r8      s     z6AutogradCompilerInstance.post_hook.<locals>.<listcomp>rT   )r&   outputsr0   rP   rM   rH   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outputZCompiledAutograd)
r    r!   r#   Zcreate_nodeZ
create_argrK   r   r   r?   rA   )r&   rV   r   r   r   end_capture   s    
z$AutogradCompilerInstance.end_capturec                    sn   |d krd S t |tr( fdd|D S t |trHt fdd|D S t |tjtjfs^tt j|j	S )Nc                    s   g | ]}  |qS r   rJ   rL   r7   r   r   r8      s     z5AutogradCompilerInstance.to_proxy.<locals>.<listcomp>c                 3   s   | ]}  |V  qd S r   rJ   rL   r7   r   r   	<genexpr>   s     z4AutogradCompilerInstance.to_proxy.<locals>.<genexpr>)
r)   listtupler*   r+   ZSymIntr,   r   r#   rQ   )r&   tr   r7   r   rK      s    

z!AutogradCompilerInstance.to_proxyc                    sR   t  tjjr( fddtt|D  t|t ks<tt| d | jd d S )Nc                    s   g | ]} | qS r   r   r9   rH   r   r   r8      s     zDAutogradCompilerInstance.bind_tensors_to_proxies.<locals>.<listcomp>)ZconstantZtracer)	r)   r*   r@   ZProxyrD   rE   r,   r   r#   )r&   ZtensorsrH   r   r^   r   rF      s    z0AutogradCompilerInstance.bind_tensors_to_proxies)__name__
__module____qualname__r'   r-   staticmethodr(   r   r*   r+   intrI   rO   rR   rU   rW   rY   rK   rF   r   r   r   r   r      s   
%
r   c              	   c   sJ   t jjjtt| }t j	d d V  W 5 Q R X t jjj| d S )NF)
r*   _C_dynamor2   set_autograd_compiler	functoolspartialr   ZautogradZset_multithreading_enabled)r   priorr   r   r   enable   s    

rj   c                  c   s*   t jjjd } d V  t jjj|  d S r   )r*   rd   re   r2   rf   )ri   r   r   r   disable   s    rk   )"r   rg   typingr   r*   Ztorch._dynamo.external_utilsr   Ztorch._dynamo.sourcer   r   Ztorch._dynamo.utilsr   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   r   r   contextmanagerrj   rk   r   r   r   r   <module>   s$   $	 
	