o
    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 G d
d deZe 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)_pytreec                       s(   e Zd Z fddZ fddZ  ZS )ExportTracepointc                    s   t  d d S )N_export_tracepoint)super__init__)self	__class__ E/var/www/auris/lib/python3.10/site-packages/torch/_export/wrappers.pyr      s   zExportTracepoint.__init__c                    s   t  j|i |S N)r   __call__)r   argskwargsr   r   r   r      s   zExportTracepoint.__call__)__name__
__module____qualname__r   r   __classcell__r   r   r   r   r      s    r   c                 O   s<   t | jj||f\}}| jdt||}t||d | jdS )NZcall_function)Zconstanttracer)pytreeZtree_mapr   Zunwrap_proxyZcreate_proxyr   r
   )moder   r   Zp_argsZp_kwargsproxyr   r   r   export_tracepoint_dispatch_mode   s
   r!   c                 O   s,   | 
 |W  d    S 1 sw   Y  d S r   r   )r   r   r   r   r   r   "export_tracepoint_fake_tensor_mode#   s   $r"   c                 O   sR   |  |}|  |}|   t|i | |W  d    S 1 s"w   Y  d S r   )Zunwrap_tensorsZredispatch_to_nextr   )ctxr   r   Zunwrapped_argsZunwrapped_kwargsr   r   r   export_tracepoint_functional)   s   


$r$   T)Zdeferred_errorc                  O   s   | S r   r   )r   r   r   r   r   export_tracepoint_cpu8   s   r%   c                    s   t | tjjs	J dksJ tjj| }fdddd   fdd} fdd	}|j|d
d}|j|d
d}||fS )N c                    sB   |  v r |  d |ksJ  |  d |ksJ ||d | < d S )Nin_specout_spec)r'   r(   r   )pathr'   r(   )module_call_specsr   r   update_module_call_signaturesB   s   z6_wrap_submodule.<locals>.update_module_call_signaturesc                 S   s:   | D ]}t |tjttttfs|d u std| qd S )NzGOnly Tensors or scalars are supported as pytree flattened inputs, got: )
isinstancetorchZTensorstrintfloatboolAssertionError)	flat_argsar   r   r   check_flattenedH   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   r3   r'   )r5   r)   r   r   pre_hookO   s
   z!_wrap_submodule.<locals>.pre_hookc                    sP   t ||f\}}t |\}} | t|dd}|| t ||S )NZmodule_call_outputsr6   r8   )r9   r   r   res_r'   Zflat_resr(   )r5   r)   r+   r   r   	post_hookV   s   z"_wrap_submodule.<locals>.post_hookT)Zwith_kwargs)	r,   r-   nnModuleZfxZgraph_moduleZ	_get_attrZregister_forward_pre_hookZregister_forward_hook)modr)   r*   	submoduler:   r=   Z
pre_handleZpost_handler   )r5   r*   r)   r+   r   _wrap_submodule=   s   rB   c              	   c   sV    g }z|D ]}| t| || qd V  W |D ]}|  qd S |D ]}|  q#w r   )extendrB   remove)fZpreserve_signatureZmodule_call_signaturesZhandlesr)   handler   r   r   _wrap_submodulesc   s   

rG   c                 C   s   dd }|| _ | S )Nc                 W   s
   t | |S r   r   )r   r   r   r   r   callq   s   
z'_mark_strict_experimental.<locals>.call)r   )clsrH   r   r   r   _mark_strict_experimentalp   s   rJ   )!
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   r   Zpy_implr!   r"   Zpy_functionalize_implr$   ZAutogradZCPUr%   rB   rG   rJ   r   r   r   r   <module>   s6   



	


&
