U
    9%e                     @   sL  d dl mZ d dlZd dlZd dlmZ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 d d	lmZmZ d d
lmZ d dlmZmZ edZeej eej eej eej  eej! eej" e#edd Z$e#edd Z%e#ej&dd Z'e#ej(dd Z)dd Z*edd Z+dS )    )contextmanagerN)_ExcludeDispatchKeyGuardDispatchKeyDispatchKeySet)ModuleCallSignature)#_unwrap_all_tensors_from_functional)wrap)HigherOrderOperator)FakeTensorMode)ProxyTorchDispatchModetrack_tensor_tree)_pytree)_get_current_dispatch_mode_pop_mode_temporarily_export_tracepointc               
   O   s   t  }|d k	stdt f}|js:t| |W  5 Q R  S t|jj| |f\}}|j	dt||}t
| |d |jdW  5 Q R  S Q R X d S )Nz5Mode should always be enabled for python fallback keyZcall_function)Zconstanttracer)r   AssertionErrorr   Zenable_tracingr   pytreeZtree_mapr   Zunwrap_proxyZcreate_proxyr   )argskwargsmodeZp_argsZp_kwargsproxy r   U/var/www/html/Darija-Ai-API/env/lib/python3.8/site-packages/torch/_export/wrappers.pyexport_tracepoint_dispatch_mode   s       r   c                  O   s   | S Nr   r   r   r   r   r   "export_tracepoint_fake_tensor_mode,   s    r   c               
   O   sR   t j }t| |d}t||d}tttj t||W  5 Q R  S Q R X d S )N)reapply_views)	torchZ_CZ$_functionalization_reapply_views_tlsr   r   r   r   Functionalizer   )r   r   r   Zunwrapped_argsZunwrapped_kwargsr   r   r   export_tracepoint_functionalize1   s    
  r!   c                  O   s   | S r   r   r   r   r   r   export_tracepoint_cpu>   s    r"   c                    s   t | tjjstdkstd }| dD ]*}}t|sPtd t|q0ddl	m
} | fddG  fdd	d	}t|||  ||fS )
N .z Couldn't find submodule at path r   )assume_constant_resultc                    s$   |  kst tg g ||d | < d S )N)inputsoutputsin_specout_spec)r   r   )pathr(   r)   )module_call_signaturesr   r   update_module_call_signaturesQ   s       z6_wrap_submodule.<locals>.update_module_call_signaturesc                       s*   e Zd Z fddZfddZdS )z&_wrap_submodule.<locals>.WrappedModulec                    s:   t jj| jjfi | _j| _t| dr0t | _d S )Nr+   )type	__class____name____dict__hasattrr   r+   )self)r+   	submoduler   r   __init__Y   s    
z/_wrap_submodule.<locals>.WrappedModule.__init__c                    sF   t ||f\}  fdd}t|f| }t || j jS )Nc                     sX   t | dd} t|  \}}||}t|\}}t |dd} | |S )NZmodule_call_inputs)kindr*   Zmodule_call_outputs)r   r   tree_unflattentree_flatten)	flat_argsr   r   resflat_resr)   )r(   r*   r3   r,   r   r   flat_gmf   s      
  z?_wrap_submodule.<locals>.WrappedModule.forward.<locals>.flat_gm)r   r7   r   r6   r+   r)   )r2   r   r   r8   r;   r:   )r*   r3   r,   )r(   r   forwardc   s     
z._wrap_submodule.<locals>.WrappedModule.forwardN)r/   
__module____qualname__r4   r<   r   r+   r*   r3   r,   r   r   WrappedModuleX   s   
r@   )
isinstancer   nnModuler   splitr1   RuntimeErrorgetattrZtorch._dynamor%   setattr)modr*   r+   parentnamer%   r@   r   r?   r   _wrap_submoduleC   s    
 rK   c                 c   sN   g }z&|D ]}|t| || q
d V  W 5 |D ]\}}}t ||| q0X d S r   )rG   appendrK   )fZpreserve_signaturer+   tasksrI   rJ   r3   r*   r   r   r   _wrap_submodules|   s    
rO   ),
contextlibr   r   Ztorch._custom_opsZtorch._Cr   r   r   Ztorch._export.exported_programr   Z!torch._functorch.eager_transformsr   Ztorch._higher_order_ops.wrapr   Z
torch._opsr	   Ztorch._subclasses.fake_tensorr
   Z"torch.fx.experimental.proxy_tensorr   r   Ztorch.utilsr   r   Ztorch.utils._python_dispatchr   r   r   ZfallthroughZPythonDispatcherZPythonTLSSnapshotZADInplaceOrViewZBackendSelectZAutocastCPUZAutogradCPUZpy_implr   r   r    r!   ZCPUr"   rK   rO   r   r   r   r   <module>   s:   





9