U
    zh                     @   s   d dl mZ d dlZd dl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 ed
Zeedd Zeedd Zejdd Zeeje	edd eejdd Zdd Zedd Zdd ZdS )    )contextmanagerN)DispatchKeystrict_mode)autograd_not_implemented)HigherOrderOperator)FakeTensorMode)ProxyTorchDispatchModetrack_tensor_tree)_pytree_export_tracepointc                 O   sL   | j st||S t| jj||f\}}| jdt||}t||d | jdS )NZcall_function)Zconstanttracer)Zenable_tracingr   pytreeZtree_mapr   Zunwrap_proxyZcreate_proxyr
   )modeargskwargsZp_argsZp_kwargsproxy r   H/var/www/html/venv/lib/python3.8/site-packages/torch/_export/wrappers.pyexport_tracepoint_dispatch_mode   s    
   r   c              
   O   s    |  |W  5 Q R  S Q R X d S Nr   )r   r   r   r   r   r   "export_tracepoint_fake_tensor_mode   s    r   c              
   O   sH   |  |}|  |}|  " t||}| |W  5 Q R  S Q R X d S r   )Zunwrap_tensorsZredispatch_to_nextr   Zwrap_tensors)ctxr   r   Zunwrapped_argsZunwrapped_kwargsoutr   r   r   export_tracepoint_functional#   s
    



r   T)Zdeferred_errorc                  O   s   | S r   r   )r   r   r   r   r   export_tracepoint_cpu2   s    r   c           	         s   t | tjjstdkst| }dD ]&}t||sHtd t||}q,fdddd   fdd	} fd
d}|j	|dd}|j
|dd}||fS )N .z Couldn't find submodule at path c                    sB   |  kr0 |  d |kst  |  d |ks0t ||d | < d S )Nin_specout_spec)r   r   )AssertionError)pathr   r   )module_call_specsr   r   update_module_call_signatures@   s    z6_wrap_submodule.<locals>.update_module_call_signaturesc                 S   s:   | D ]0}t |tjttttfs|d kstd| qd S )NzGOnly Tensors or scalars are supported as pytree flattened inputs, got: )
isinstancetorchZTensorstrintfloatboolr    )	flat_argsar   r   r   check_flattenedF   s
    z(_wrap_submodule.<locals>.check_flattenedc                    sB   t ||f\}} | t|dd}t ||\}}||fS )NZmodule_call_inputskindr!   r   Ztree_flattenr   Ztree_unflatten)moduler   r   r*   r   )r,   r!   r   r   pre_hookM   s
    z!_wrap_submodule.<locals>.pre_hookc                    sP   t ||f\}}t |\}} | t|dd}|| t ||S )NZmodule_call_outputsr-   r/   )r0   r   r   res_r   Zflat_resr   )r,   r!   r#   r   r   	post_hookT   s    z"_wrap_submodule.<locals>.post_hookT)Zwith_kwargs)r$   r%   nnModuler    splithasattrRuntimeErrorgetattrZregister_forward_pre_hookZregister_forward_hook)	modr!   r"   	submodulenamer1   r4   Z
pre_handleZpost_handler   )r,   r"   r!   r#   r   _wrap_submodule7   s    
r>   c              	   c   sD   g }z&|D ]}|t| || q
d V  W 5 |D ]}|   q0X d S r   )removeextendr>   )fZpreserve_signatureZmodule_call_signaturesZhandleshandler!   r   r   r   _wrap_submodulesa   s    
rC   c                 C   s   dd }|| _ | S )Nc                 W   s
   t | |S r   r   )selfr   r   r   r   callo   s    z'_mark_strict_experimental.<locals>.call)__call__)clsrE   r   r   r   _mark_strict_experimentaln   s    rH   ) 
contextlibr   r%   Ztorch._custom_opsZtorch._Cr   Z#torch._higher_order_ops.strict_moder   Ztorch._higher_order_ops.utilsr   Z
torch._opsr   Ztorch._subclasses.fake_tensorr   Z"torch.fx.experimental.proxy_tensorr	   r
   Ztorch.utilsr   r   r   Zpy_implr   r   Zpy_functionalize_implr   ZAutogradZCPUr   r>   rC   rH   r   r   r   r   <module>   s2   



	



*
